Skip to content

Instantly share code, notes, and snippets.

@williamhaley
Created January 24, 2020 01:42
Show Gist options
  • Save williamhaley/fa8f2ce14655a8893435ef1e4507f09e to your computer and use it in GitHub Desktop.
Save williamhaley/fa8f2ce14655a8893435ef1e4507f09e to your computer and use it in GitHub Desktop.
Get the top-level licenses of all node packages/libraries/modules and print them out in CSV
/**
* Run this script from the root of the repo you want to inspect. It's assumed
* that there's a `./node_modules` and `./package.json` where this script runs.
*/
const packageJSON = require('./package.json');
// Get all the top-level dependencies for the current project.
const dependencies = {
...packageJSON.dependencies,
...packageJSON.devDependencies,
};
// Get the names of every dependency.
const dependencyNames = Object.keys(dependencies);
// Create a key => value of depenencyName => { license, whateverElse };
const licenses = dependencyNames.reduce((memo, dependencyName) => {
const dependencyDirectory = `./node_modules/${dependencyName}`;
const dependencyPackageJSON = require(`${dependencyDirectory}/package.json`);
const license = dependencyPackageJSON.license;
return {
...memo,
[dependencyName]: {
dependencyName,
license,
}
};
}, {});
// Print out all the dependencies and their license info.
dependencyNames.sort().forEach((dependencyName) => {
console.log(`"${dependencyName}","${licenses[dependencyName].license}"`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment