Skip to content

Instantly share code, notes, and snippets.

@Y--
Last active September 22, 2015 07:58
Show Gist options
  • Save Y--/23caf59ba81c80803b7f to your computer and use it in GitHub Desktop.
Save Y--/23caf59ba81c80803b7f to your computer and use it in GitHub Desktop.
'use strict';
const cp = require('child_process');
function readDependencies(done) {
const compileCP = cp.spawn('npm', ['ls', '--json', '--long', '--prod']);
const errors = [];
const chunks = [];
compileCP.stderr.on('data', errors.push.bind(errors));
compileCP.stdout.on('data', chunks.push.bind(chunks));
compileCP.on('close', function (code) {
if (code || errors.length) {
return done(new Error(Buffer.concat(errors).toString()));
}
return done(null, Buffer.concat(chunks).toString());
});
}
readDependencies(function(err, data) {
if (err) { throw err; }
const allDeps = {};
const jsonData = JSON.parse(data);
walkDependencies(jsonData, allDeps);
console.log(['name', 'tb', 'description', 'repository', 'license'].join(';'));
const sk = Object.keys(allDeps).sort();
for (let keys of sk) {
let d = allDeps[keys];
console.log([d.name, d.tb, d.description, d.repository, d.license].join(';'));
}
});
function walkDependencies(o, allDeps) {
for (let k of Object.keys(o.dependencies)) {
const oo = o.dependencies[k];
if (!allDeps[k]) {
allDeps[k] = {
name : oo.name,
tb : oo.dist && oo.dist.tarball,
description : oo.description,
repository : oo.repository && oo.repository.url,
license : oo.license
};
} else {
allDeps[k].name = allDeps[k].name || oo.name;
allDeps[k].tb = allDeps[k].tb || oo.dist && oo.dist.tarball;
allDeps[k].description = allDeps[k].description || oo.description;
allDeps[k].repository = allDeps[k].repository || oo.repository && oo.repository.url;
allDeps[k].license = allDeps[k].license || oo.license;
}
walkDependencies(o.dependencies[k], allDeps);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment