Skip to content

Instantly share code, notes, and snippets.

@lukepearson
Created December 22, 2022 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukepearson/9632c0d8f0ef6076de5ec949b08209d3 to your computer and use it in GitHub Desktop.
Save lukepearson/9632c0d8f0ef6076de5ec949b08209d3 to your computer and use it in GitHub Desktop.
Generate a csv containing a list of all of the licences used in nodejs projects in a subdirectories
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const startingDir = __dirname
const walkSync = (dir) => {
fs.readdirSync(dir).forEach(file => {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
if (file === 'node_modules') return
walkSync(path.join(dir, file))
}
if (file === 'package.json') {
console.log(`checking packages in ${dir}`)
const outFile = `${startingDir}/licenses/${dir.replace(/\//g, '-')}.csv`
const cmd = `cd ${__dirname}/${dir} && npx license-checker --summary --csv --out ${outFile}`
// Run license-checker
execSync(cmd)
// Remove header row
execSync(`sed -i '' '1d' ${outFile}`)
// Add newline to end of file
execSync(`echo '' >> ${outFile}`)
}
});
};
// Recursively walk through all directories
walkSync('.')
// Concat all the csv files into one
execSync(`cat ${startingDir}/licenses/*.csv > ${startingDir}/licenses.csv`)
execSync('open licenses.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment