From 54e2390cefd1dc664a15cd3a84347227f889f48b Mon Sep 17 00:00:00 2001 From: Holger Knust Date: Mon, 7 Jul 2014 13:09:56 -0400 Subject: [PATCH] Added csv output option similar to the existing JSON output option. Requires an output filename. --- .gitignore | 1 + bin/license-checker | 12 ++++++++++-- lib/args.js | 4 ++-- lib/index.js | 29 ++++++++++++++++++++++++++++- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 5f94f65..67900bc 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ CVS/ *~ .com.apple.timemachine.supported coverage/ +.idea/ diff --git a/bin/license-checker b/bin/license-checker index 7f8b9dc..e525009 100755 --- a/bin/license-checker +++ b/bin/license-checker @@ -14,12 +14,20 @@ var path = require('path'); var fs = require('fs'); checker.init(args, function(json) { + var dir; if (args.json) { - var dir = path.dirname(args.json); + dir = path.dirname(args.json); mkdirp.sync(dir); fs.writeFileSync(args.json, JSON.stringify(json, null, 4) + '\n', 'utf8'); console.log('file written', args.json); - } else { + } + else if (args.csv) { + dir = path.dirname(args.csv); + mkdirp.sync(dir); + fs.writeFileSync(args.csv, checker.asCSV(json), 'utf8'); + console.log('file written', args.csv); + } + else { checker.print(json); } }); diff --git a/lib/args.js b/lib/args.js index 7b5542f..5171ac4 100644 --- a/lib/args.js +++ b/lib/args.js @@ -7,6 +7,7 @@ http://yuilibrary.com/license/ var nopt = require('nopt'), known = { json: require('path'), + csv: require('path'), unknown: Boolean, version: Boolean, start: String, @@ -18,8 +19,7 @@ var nopt = require('nopt'), }; var raw = function (args) { - var parsed = nopt(known, shorts, (args || process.argv)); - return parsed; + return nopt(known, shorts, (args || process.argv)); }; var has = function (a) { diff --git a/lib/index.js b/lib/index.js index 92a7303..8343de8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -77,7 +77,7 @@ var flatten = function(json) { exports.init = function(options, callback) { - console.log('scanning', options.start); + console.log('scanning' , options.start); read(options.start, function(err, json) { flatten(json); @@ -101,3 +101,30 @@ exports.init = function(options, callback) { exports.print = function(sorted) { console.log(treeify.asTree(sorted, true)); }; + +exports.asCSV = function(sorted) { + var text = '"module name","license","repository"\n'; + + for(var key in sorted) { + if (sorted.hasOwnProperty(key)) { + var module = sorted[key]; + text += '"' + key + '",'; + if (module.hasOwnProperty('licenses')) { + text += '"' + module['licenses'] + '"'; + } + else { + text += '""'; + } + text += ','; + if (module.hasOwnProperty('repository')) { + text += '"' + module['repository'] + '"'; + } + else { + text += '""'; + } + text += '\n'; + } + } + + return text; +}; \ No newline at end of file