Skip to content

Instantly share code, notes, and snippets.

@anotheredward
Last active August 29, 2016 02:43
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 anotheredward/b910bd01a1e2190a0e6d29479dc7f775 to your computer and use it in GitHub Desktop.
Save anotheredward/b910bd01a1e2190a0e6d29479dc7f775 to your computer and use it in GitHub Desktop.
get license, name, homepage of all direct npm dependencies script
'use strict'
// Prints the license, name and homepage of every top-level npm package
const fs = require('fs')
const folders = fs.readdirSync('node_modules')
const paths = folders.map(folder => 'node_modules/' + folder)
const directoryPaths = paths.filter(path => fs.statSync(path).isDirectory())
const packagePaths = directoryPaths.map(path => `${path}/package.json`)
const existingPackagePaths = packagePaths.filter(path => fs.existsSync(path))
const packageFiles = existingPackagePaths.map(path => fs.readFileSync(path))
const packages = packageFiles.map(JSON.parse)
const extracts = packages.map(p => ({name: p.name, homepage: p.homepage, license: expandLicense(p.license) || expandLicenses(p.licenses)}))
const formattedExtracts = extracts.map(e => `${e.license}, ${e.name}, ${e.homepage}`)
for (let e of formattedExtracts) console.log(e)
function expandLicense (license) {
if (!license) return undefined
if (typeof license === 'string') return license
if (license.type) return license.type
return license.name
}
function expandLicenses (licenses) {
if (!licenses || !licenses.length) return undefined
return licenses.map(expandLicense).join(' ')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment