Created
May 21, 2018 15:33
-
-
Save DevinClark/8247c7d01881aefa928b985c15c2e9ff to your computer and use it in GitHub Desktop.
given a list of dependencies, grab their version, license information, and dependencies, then grab version and license information for the dependencies. Outputs an html list.
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
const fs = require('fs'); | |
const getLatestVersion = require('latest-semver'); | |
const semver = require('semver'); | |
require('isomorphic-fetch'); | |
const NPM = 'https://registry.npmjs.com'; | |
function getPackage(name) { | |
return fetch(`${NPM}/${name}`) | |
.then(function (response) { | |
return response.json(); | |
}); | |
} | |
function getDependency(name, version) { | |
return getPackage(name) | |
.then(function (package) { | |
let latestVersion = semver.maxSatisfying(Object.keys(package.versions), version); | |
let requestedVersion = package.versions[latestVersion]; | |
return { | |
name: requestedVersion.name, | |
url: `https://npmjs.com/package/${requestedVersion.name}`, | |
description: requestedVersion.description, | |
version: latestVersion, | |
license: requestedVersion.license | |
} | |
}) | |
} | |
function renderPackage(data) { | |
let template = `<li><a href="${data.url}">${data.name} (${data.version})</a>, ${data.license}: ${data.description} | |
<ul>${(data.dependencies || []).map(function (dep) { | |
return `<li><a href="${dep.url}">${dep.name} (${dep.version})</a>, ${dep.license}: ${dep.description}</li>`; | |
}).join('')}</ul> | |
</li> | |
`; | |
return template; | |
} | |
const PACKAGES = [ | |
'documentation', | |
'gulp-documentation' | |
]; | |
var promise; | |
if (Array.isArray(PACKAGES)) { | |
promise = Promise.all(PACKAGES.map(function (p) { | |
return getPackage(p); | |
})) | |
} else { | |
promise = [getPackage(PACKAGES)]; | |
} | |
promise | |
.then(function (packages) { | |
return Promise.all(packages.map(function(package) { | |
let latestVersion = getLatestVersion(Object.keys(package.versions)); | |
let requestedVersion = package.versions[latestVersion]; | |
let dependencies = requestedVersion.dependencies; | |
let depList = []; | |
Object.keys(dependencies).forEach(function (name) { | |
depList.push(getDependency(name, dependencies[name])); | |
}, []); | |
return Promise.all(depList).then(function (deps) { | |
let originalPackage = { | |
name: requestedVersion.name, | |
url: `https://npmjs.com/package/${requestedVersion.name}`, | |
description: requestedVersion.description, | |
version: latestVersion, | |
license: requestedVersion.license, | |
dependencies: deps | |
} | |
return originalPackage; | |
}) | |
})) | |
}) | |
.then(function (data) { | |
return `<ul>${data.map(function (d) { | |
return `${renderPackage(d)}` | |
}).join('')}</ul>`; | |
}) | |
.then(function (html) { | |
fs.writeFileSync('./ossr.html', html); | |
}) | |
.catch(function (ex) { | |
console.log(ex); | |
}) |
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
{ | |
"name": "ossr", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"isomorphic-fetch": "^2.2.1", | |
"latest-semver": "^1.0.0", | |
"semver": "^5.4.1", | |
"semver-extra": "^2.0.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment