Skip to content

Commit

Permalink
WIP: Add new option 'plainVertical'
Browse files Browse the repository at this point in the history
  • Loading branch information
RSeidelsohn committed Apr 7, 2021
1 parent 3dc7f67 commit 8619690
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
7 changes: 6 additions & 1 deletion bin/license-checker-rseidelsohn
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ if (args.help) {
' --includePackages [list] restrict output to the packages (either "package@fullversion" or "package@majorversion" or only "package") in the semicolon-seperated list',
' --excludePackages [list] restrict output to the packages (either "package@fullversion" or "package@majorversion" or only "package") not in the semicolon-seperated list',
' --excludePrivatePackages restrict output to not include any package marked as private',
' --plainVertical output in plain vertical format like [Angular CLI does](https://angular.io/3rdpartylicenses.txt)',
'',
' --version The current version',
' --help The text you are reading right now :)',
Expand Down Expand Up @@ -90,7 +91,7 @@ licenseChecker.init(args, function(err, json) {
});

function shouldColorizeOutput(args) {
return args.color && !args.out && !args.files && !(args.csv || args.json || args.markdown);
return args.color && !args.out && !args.files && !(args.csv || args.json || args.markdown || args.plainVertical);
}

function colorizeOutput(json) {
Expand Down Expand Up @@ -120,5 +121,9 @@ function getFormattedOutput(json, args) {
return licenseChecker.asSummary(json);
}

if (args.plainVertical) {
return licenseChecker.asPlainVertical(json);
}

return licenseChecker.asTree(json);
}
57 changes: 57 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,63 @@ exports.asMarkDown = function asMarkDown(sorted, customFormat) {
return text;
};

/**
* Output data in plain vertical format like Angular CLI does: https://angular.io/3rdpartylicenses.txt
*/
exports.asPlainVertical = function asPlainVertical(sorted) {
return Object.entries(sorted)
.map(([moduleName, moduleData]) => {
let licenseText = moduleName.substring(0, moduleName.lastIndexOf('@')) + '\n';

if (Array.isArray(moduleData.licenses) && moduleData.licenses.length > 0) {
licenseText += moduleData.licenses.map((moduleLicense) => {
/*istanbul ignore else*/
if (typeof moduleLicense === 'object') {
/*istanbul ignore next*/
return moduleLicense.type || moduleLicense.name;
}

if (typeof moduleLicense === 'string') {
return moduleLicense;
}
});
} else if (
typeof moduleData.licenses === 'object' &&
(moduleData.licenses.type || moduleData.licenses.name)
) {
licenseText += license(moduleData.licenses.type || moduleData.licenses.name);
} else if (typeof moduleData.licenses === 'string') {
licenseText += license(moduleData.licenses);
}

licenseText += '\n';

if (Array.isArray(moduleData.licenseFile) && moduleData.licenseFile.length > 0) {
licenseText += moduleData.licenseFile.map((moduleLicense) => {
/*istanbul ignore else*/
if (typeof moduleLicense === 'object') {
/*istanbul ignore next*/
return moduleLicense.type || moduleLicense.name;
}

if (typeof moduleLicense === 'string') {
return moduleLicense;
}
});
} else if (
typeof moduleData.licenseFile === 'object' &&
(moduleData.licenseFile.type || moduleData.licenseFile.name)
) {
licenseText += moduleData.licenseFile.type || moduleData.licenseFile.name;
} else if (typeof moduleData.licenseFile === 'string') {
licenseText += fs.readFileSync(moduleData.licenseFile, { encoding: 'utf8' });
}

return `${licenseText}`;
})
.join('\n\n');
};

exports.parseJson = function parseJson(jsonPath) {
if (typeof jsonPath !== 'string') {
return new Error('The path was not specified for the JSON file to parse.');
Expand Down

0 comments on commit 8619690

Please sign in to comment.