Skip to content

Commit

Permalink
docs: Order options alphabetically and refactor some minor functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Seidelsohn committed May 30, 2021
1 parent 4930f10 commit 9405cfb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 70 deletions.
61 changes: 28 additions & 33 deletions bin/license-checker-rseidelsohn
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,49 @@ const chalk = require('chalk');
const fs = require('fs');
const hasFailingArg = args.failOn || args.onlyAllow;

const usageEntries = [
const usageMessage = [
'All options in alphabetical order:',
'',
' --production only show production dependencies.',
' --development only show development dependencies.',
' --unknown report guessed licenses as unknown licenses.',
' --start [filepath] path of the initial json to look for',
' --onlyunknown only list packages with unknown or guessed licenses.',
' --markdown output in markdown format.',
' --json output in json format.',
' --angularCli is just a synonym for --plainVertical',
' --csv output in csv format.',
' --csvComponentPrefix column prefix for components in csv file',
' --out [filepath] write the data to a specific file.',
' --files [path] copy all license files to path and rename them to `module-name`@`version`-LICENSE.txt.',
' --customPath to add a custom Format file in JSON',
' --development only show development dependencies.',
' --direct look for direct dependencies only',
' --excludeLicenses [list] exclude modules which licenses are in the comma-separated list from the output',
' --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',
' --failOn [list] fail (exit with code 1) on the first occurrence of the licenses of the semicolon-separated list',
' --files [path] copy all license files to path and rename them to `module-name`@`version`-LICENSE.txt.',
' --includeLicenses [list] include only modules which licenses are in the comma-separated list from the output',
' --includePackages [list] restrict output to the packages (either "package@fullversion" or "package@majorversion" or only "package") in the semicolon-seperated list',
' --json output in json format.',
' --markdown output in markdown format.',
' --onlyAllow [list] fail (exit with code 1) on the first occurrence of the licenses not in the semicolon-seperated list',
' --onlyunknown only list packages with unknown or guessed licenses.',
' --out [filepath] write the data to a specific file.',
' --plainVertical output in plain vertical format like [Angular CLI does](https://angular.io/3rdpartylicenses.txt)',
' --production only show production dependencies.',
' --relativeLicensePath output the location of the license files as relative paths',
' --relativeModulePath output the location of the module files as relative paths',
' --start [filepath] path of the initial json to look for',
' --summary output a summary of the license usage',
' --failOn [list] fail (exit with code 1) on the first occurrence of the licenses of the semicolon-separated list',
' --onlyAllow [list] fail (exit with code 1) on the first occurrence of the licenses not in the semicolon-seperated list',
' --direct look for direct dependencies only',
' --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)',
' --angularCli is just a synonym for --plainVertical',
' --unknown report guessed licenses as unknown licenses.',
'',
' --version The current version',
' --help The text you are reading right now :)',
' --help The text you are reading right now :)',
''
];

const kownOptions = usageEntries
.map((entry) => { return entry.trim().replace('--', ''); })
.filter((entry) => { return entry.length > 0; })
.map((entry) => { return entry.split(' ').shift(); })
.concat(['color']);
const usageMessage = usageEntries.join('\n');
const unknownArgs = Object.keys(args).filter((arg) => { return !kownOptions.includes(arg); });
// console.log('args: ', args);
].join('\n');

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

if (unknownArgs.length) {
console.error(`license-checker-rseidelsohn@${require('../package.json').version}`, '\n');
console.error(`Warning: Unknown option${unknownArgs.length > 1 ? 's' : ''}: ${unknownArgs.map((unknownArg) => { return `'${unknownArg}'`; }).join(', ')}`);
console.error(`Warning: 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);

if (!unknownArgs.length && args.help) {
console.error(`license-checker-rseidelsohn@${require('../package.json').version}`);
Expand All @@ -75,7 +70,7 @@ if (args.version) {
}

if (args.failOn && args.onlyAllow) {
console.error('--failOn and --onlyAllow can not be used at the same time. Choose one or the other.');
console.error('Error: --failOn and --onlyAllow can not be used at the same time. Choose one or the other.');
process.exit(1);
}

Expand All @@ -84,7 +79,7 @@ if (hasFailingArg && hasFailingArg.indexOf(',') >= 0) {
console.warn(`Warning: As of v17 the --${argName} argument takes semicolons as delimeters instead of commas (some license names can contain commas)`);
}

licenseChecker.init(args, function(err, json) {
licenseChecker.init(args, function (err, json) {
if (err) {
console.error('An error has occurred:');
console.error(err);
Expand Down
64 changes: 27 additions & 37 deletions lib/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ http://yuilibrary.com/license/

const nopt = require('nopt');
const chalk = require('chalk');
const known = {
const knownOpts = {
angularCli: Boolean,
color: Boolean,
csv: Boolean,
csvComponentPrefix: String,
Expand All @@ -21,56 +22,46 @@ const known = {
files: require('path'),
help: Boolean,
includeLicenses: String,
includePackages: String,
json: Boolean,
markdown: Boolean,
onlyAllow: String,
onlyunknown: Boolean,
onlyunknownOpts: Boolean,
out: require('path'),
includePackages: String,
plainVertical: Boolean,
production: Boolean,
relativeLicensePath: Boolean,
relativeModulePath: Boolean,
start: String,
summary: Boolean,
unknown: Boolean,
unknownOpts: Boolean,
version: Boolean,
};
const shorts = {
v: ['--version'],
const shortHands = {
h: ['--help'],
v: ['--version'],
};

const getParsedArgs = function getParsedArgs(args) {
return nopt(known, shorts, args || process.argv);
};

/*istanbul ignore next */
const has = function has(a) {
const cooked = getParsedArgs().argv.cooked;
let ret = false;

cooked.forEach(function(o) {
if (o === `--${a}` || o === `--no-${a}`) {
ret = true;
}
});

return ret;
};

const getCleanParsedArgs = function getCleanParsedArgs(args) {
const parsed = getParsedArgs(args);
// nopt returns an object looking like this, if you pass the params '--one --two here there -- --three --four':
//
// parsed {
// one: true,
// two: true,
// argv: {
// remain: [ 'here', 'there', '--three', '--four' ],
// cooked: [ '--one', '--two', 'here', 'there', '--', '--three', '--four' ], // contains the expanded shorthand options
// original: [ '--one', '--two', 'here', 'there', '--', '--three', '--four' ]
// }
// }
const parsed = nopt(knownOpts, shortHands, args || process.argv);

delete parsed.argv;

return parsed;
};

const setDefaults = function setDefaults(parsed) {
if (parsed == null) {
parsed = getCleanParsedArgs();
}

const setDefaults = function setDefaults(parsed = {}) {
/*istanbul ignore else*/
if (parsed.color == null) {
parsed.color = chalk.supportsColor;
Expand All @@ -94,12 +85,11 @@ const setDefaults = function setDefaults(parsed) {
};

const parse = function parse(args) {
return setDefaults(getCleanParsedArgs(args));
return setDefaults(getParsedArgs(args));
};

exports.defaults = setDefaults;
exports.has = has;
exports.getParsedArgs = getParsedArgs;
exports.parse = parse;
exports.shorts = shorts;
exports.known = known;
module.exports = {
knownOpts: knownOpts,
parse: parse,
setDefaults: setDefaults,
};

0 comments on commit 9405cfb

Please sign in to comment.