Skip to content

Commit

Permalink
Added csv output option similar to the existing JSON output option. R…
Browse files Browse the repository at this point in the history
…equires an output filename.
  • Loading branch information
Holger Knust committed Jul 7, 2014
1 parent 1ce6d42 commit 54e2390
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ CVS/
*~
.com.apple.timemachine.supported
coverage/
.idea/
12 changes: 10 additions & 2 deletions bin/license-checker
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
4 changes: 2 additions & 2 deletions lib/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
29 changes: 28 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
};

0 comments on commit 54e2390

Please sign in to comment.