Last active
August 29, 2016 02:43
-
-
Save anotheredward/b910bd01a1e2190a0e6d29479dc7f775 to your computer and use it in GitHub Desktop.
get license, name, homepage of all direct npm dependencies script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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