Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for csvComponentPrefix #95

Merged
merged 2 commits into from
Apr 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Options
* `--onlyunknown` only list packages with unknown or guessed licenses.
* `--json` output in json format.
* `--csv` output in csv format.
* `--csvComponentPrefix` prefix column for compoment in csv format.
* `--out [filepath]` write the data to a specific file.
* `--customPath` to add a custom Format file in JSON
* `--exclude [list]` exclude modules which licenses are in the comma-separated list from the output
Expand Down
4 changes: 2 additions & 2 deletions bin/license-checker
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ if (args.help) {
' --onlyunknown only list packages with unknown or guessed licenses.',
' --json output in json format.',
' --csv output in csv format.',
' --csvComponentPrefix column prefix for components in csv file',
' --out [filepath] write the data to a specific file.',
' --customPath to add a custom Format file in JSON',
' --exclude [list] exclude modules which licenses are in the comma-separated list from the output',
Expand All @@ -54,11 +55,10 @@ checker.init(args, function(err, json) {
console.log('Found error');
console.log(err);
}

if (args.json) {
formattedOutput = JSON.stringify(json, null, 2);
} else if (args.csv) {
formattedOutput = checker.asCSV(json, args.customFormat);
formattedOutput = checker.asCSV(json, args.customFormat, args.csvComponentPrefix);
} else if (args.markdown){
formattedOutput = checker.asMarkDown(json, args.customFormat);
} else {
Expand Down
1 change: 1 addition & 0 deletions lib/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var nopt = require('nopt'),
development: Boolean,
json: Boolean,
csv: Boolean,
csvComponentPrefix: String,
markdown: Boolean,
out: require('path'),
unknown: Boolean,
Expand Down
23 changes: 19 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,18 +305,26 @@ exports.asTree = function(sorted) {
return treeify.asTree(sorted, true);
};

exports.asCSV = function(sorted, customFormat) {
exports.asCSV = function(sorted, customFormat, csvComponentPrefix) {
var text = [ ], textArr = [ ], lineArr = [ ];
var prefixName = '"component"';
var prefix = csvComponentPrefix;

if (customFormat && Object.keys(customFormat).length > 0) {
textArr = [ ];
if (csvComponentPrefix) { textArr.push(prefixName); }
textArr.push('"module name"');
Object.keys(customFormat).forEach(function forEachCallback(item) {
textArr.push('"' + item + '"');
});
text.push(textArr.join(','));
} else {
text.push(['"module name"','"license"','"repository"'].join(','));
textArr = [];
if (csvComponentPrefix) { textArr.push(prefixName); }
['"module name"','"license"','"repository"'].forEach(function(item) {
textArr.push(item);
});
text.push(textArr.join(','));
}

Object.keys(sorted).forEach(function(key) {
Expand All @@ -326,17 +334,24 @@ exports.asCSV = function(sorted, customFormat) {

//Grab the custom keys from the custom format
if (customFormat && Object.keys(customFormat).length > 0) {
if (csvComponentPrefix) {
lineArr.push('"'+prefix+'"');
}
lineArr.push('"' + key + '"');
Object.keys(customFormat).forEach(function forEachCallback(item) {
lineArr.push('"' + module[item] + '"');
});
line = lineArr.join(',');
} else {
line = [
if (csvComponentPrefix) {
lineArr.push('"'+prefix+'"');
}
lineArr.push([
'"' + key + '"',
'"' + (module.licenses || '') + '"',
'"' + (module.repository || '') + '"'
].join(',');
]);
line = lineArr.join(',');
}
text.push(line);
});
Expand Down
13 changes: 13 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ describe('main tests', function() {
assert.equal('"abbrev@1.0.9","abbrev","Like ruby\'s abbrev module, but in js","<<Should Never be set>>"', str.split('\n')[1]);
});

it('and convert to CSV with component prefix', function() {
var format = {
'name': '<<Default Name>>',
'description': '<<Default Description>>',
'pewpew': '<<Should Never be set>>'
};

var str = checker.asCSV(output, format, "main-module");
assert.equal('"component","module name","name","description","pewpew"', str.split('\n')[0]);
assert.equal('"main-module","abbrev@1.0.9","abbrev","Like ruby\'s abbrev module, but in js","<<Should Never be set>>"', str.split('\n')[1]);

});

it('and convert to MarkDown', function() {
var format = {
'name': '<<Default Name>>',
Expand Down