Skip to content

Commit

Permalink
feat: Use licenseFile paths with --files option
Browse files Browse the repository at this point in the history
  • Loading branch information
RSeidelsohn committed Feb 1, 2022
1 parent 26333fe commit 9cedc1e
Showing 1 changed file with 45 additions and 12 deletions.
57 changes: 45 additions & 12 deletions bin/license-checker-rseidelsohn
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const mkdirp = require('mkdirp');
const path = require('path');
const chalk = require('chalk');
const fs = require('fs');
const cloneDeep = require('lodash.clonedeep');
const hasFailingArg = parsedArgs.failOn || parsedArgs.onlyAllow;

const usageMessage = [
Expand Down Expand Up @@ -47,15 +48,19 @@ const usageMessage = [
'',
' --version The current version',
' --help The text you are reading right now :)',
''
'',
].join('\n');

const kownOptions = Object.keys(args.knownOpts);
const unknownArgs = Object.keys(parsedArgs).filter((arg) => !kownOptions.includes(arg));

if (unknownArgs.length) {
console.error(`license-checker-rseidelsohn@${require('../package.json').version}`, '\n');
console.error(`Error: Unknown option${unknownArgs.length > 1 ? 's' : ''}: ${unknownArgs.map((unknownArg) => `'${unknownArg}'`).join(', ')}`);
console.error(
`Error: Unknown option${unknownArgs.length > 1 ? 's' : ''}: ${unknownArgs
.map((unknownArg) => `'${unknownArg}'`)
.join(', ')}`,
);
console.error(` Possibly a typo? Currently known options are:`);
console.error(usageMessage, '\n');
process.exit(1);
Expand All @@ -79,7 +84,9 @@ if (parsedArgs.failOn && parsedArgs.onlyAllow) {

if (hasFailingArg && hasFailingArg.indexOf(',') >= 0) {
const argName = parsedArgs.failOn ? 'failOn' : 'onlyAllow';
console.warn(`Warning: As of v17 the --${argName} argument takes semicolons as delimeters instead of commas (some license names can contain commas)`);
console.warn(
`Warning: As of v17 the --${argName} argument takes semicolons as delimeters instead of commas (some license names can contain commas)`,
);
}

licenseChecker.init(parsedArgs, function (err, json) {
Expand All @@ -105,7 +112,9 @@ licenseChecker.init(parsedArgs, function (err, json) {
mkdirp.sync(dir);
fs.writeFileSync(parsedArgs.out, formattedOutput, 'utf8');
}
} else {
}

if (!parsedArgs.out) {
console.log(formattedOutput);
}
});
Expand All @@ -117,33 +126,57 @@ function shouldColorizeOutput(args) {
function colorizeOutput(json) {
Object.keys(json).forEach((key) => {
const index = key.lastIndexOf('@');
const colorizedKey = chalk.white.bgKeyword('darkslategrey')(key.substr(0, index)) + chalk.dim('@') + chalk.white.bgKeyword('green')(key.substr(index + 1));
const colorizedKey =
chalk.white.bgKeyword('darkslategrey')(key.substr(0, index)) +
chalk.dim('@') +
chalk.white.bgKeyword('green')(key.substr(index + 1));
json[colorizedKey] = json[key];

delete json[key];
});
}

function getFormattedOutput(json, args) {
const jsonCopy = cloneDeep(json);

if (args.files) {
Object.keys(jsonCopy).forEach((moduleName) => {
const outPath = path.join(args.files, `${moduleName}-LICENSE.txt`);
const originalLicenseFile = jsonCopy[moduleName].licenseFile;

if (originalLicenseFile && fs.existsSync(originalLicenseFile)) {
if (args.relativeLicensePath) {
if (args.out) {
jsonCopy[moduleName].licenseFile = path.relative(args.out, outPath);
} else {
jsonCopy[moduleName].licenseFile = path.relative(process.cwd(), outPath);
}
} else {
jsonCopy[moduleName].licenseFile = outPath;
}
}
});
}

if (args.json) {
return JSON.stringify(json, null, 4) + '\n';
return JSON.stringify(jsonCopy, null, 4) + '\n';
}

if (args.csv) {
return licenseChecker.asCSV(json, args.customFormat, args.csvComponentPrefix);
return licenseChecker.asCSV(jsonCopy, args.customFormat, args.csvComponentPrefix);
}

if (args.markdown){
return licenseChecker.asMarkDown(json, args.customFormat) + "\n";
if (args.markdown) {
return licenseChecker.asMarkDown(jsonCopy, args.customFormat) + '\n';
}

if (args.summary) {
return licenseChecker.asSummary(json);
return licenseChecker.asSummary(jsonCopy);
}

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

return licenseChecker.asTree(json);
return licenseChecker.asTree(jsonCopy);
}

0 comments on commit 9cedc1e

Please sign in to comment.