Skip to content

Instantly share code, notes, and snippets.

@matallo
Created December 12, 2017 10:54
Show Gist options
  • Save matallo/0ceac0039772855566f16143e98cf783 to your computer and use it in GitHub Desktop.
Save matallo/0ceac0039772855566f16143e98cf783 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const licenses = (dirpath, acc, depth = -1) => {
const hasPackage = fs.existsSync(`${dirpath}/package.json`);
if (!hasPackage) {
return;
}
const pkg = require(`${dirpath}/package.json`);
let license = (pkg.licenses && pkg.licenses) ? pkg.licenses : pkg.license;
acc[pkg.name] = `${pkg.description}\t${JSON.stringify(license)}`;
const checkSubmodules = (depth === -1) || (depth-- > 0);
const hasSubmodules = fs.existsSync(`${dirpath}/node_modules`);
if (checkSubmodules && hasSubmodules) {
const submodules = fs.readdirSync(`${dirpath}/node_modules`);
submodules.forEach(submodule => {
licenses(`${dirpath}/node_modules/${submodule}`, acc, depth);
});
}
return acc;
};
if (process.argv.indexOf('--help') > -1) {
console.log(`Usage: ${process.argv0} ${__filename} [dirpath]`);
process.exit(1);
}
const dirpath = process.argv[2] || '.';
const results = licenses(dirpath, {});
const rootPkg = require(`${dirpath}/package.json`);
Object.keys(rootPkg.dependencies).forEach(depName => {
console.log(`${depName}\t${results[depName]}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment