Skip to content

Instantly share code, notes, and snippets.

@mcavage
Created January 5, 2012 01:06
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 mcavage/1563151 to your computer and use it in GitHub Desktop.
Save mcavage/1563151 to your computer and use it in GitHub Desktop.
npmfreeze
#!/usr/bin/env node
// -*- mode: js -*-
var fs = require('fs');
var semver = require('semver');
var spawn = require('child_process').spawn;
///--- Globals
var deps = {};
///--- Helpers
function done() {
console.log(JSON.stringify(deps, null, 2));
}
function waitForDone() {
process.nextTick(function() {
if (wait === 0)
return done();
return waitForDone();
});
}
///--- Main
process.argv.slice(2).forEach(function(fname) {
var pkg = JSON.parse(fs.readFileSync(fname, 'utf8'));
if (!pkg.dependencies)
return;
var tmp = pkg.dependencies;
Object.keys(tmp).forEach(function(dep) {
if (!deps[dep] || semver.gt(tmp[dep], deps[dep]))
deps[dep] = semver.clean(tmp[dep]) || '*';
});
});
// Make a pass and clean up all the '*'
var wait = 0;
Object.keys(deps).forEach(function(k) {
if (deps[k] !== '*')
return;
wait++;
var npm = spawn('npm', ['info', k]);
var json = '';
npm.stdout.on('data', function(data) {
if (data)
json += data;
});
npm.stdout.on('end', function(code) {
if (code) {
console.error('npm info %s exited: %d', k, code);
process.exit(code);
}
var val;
eval('val = ' + json);
deps[k] = val['dist-tags'].latest;
wait--;
});
});
return (wait === 0 ? done() : waitForDone());
@mcavage
Copy link
Author

mcavage commented Jan 5, 2012

bluesnoop:cloud-api mark$ find node_modules -name package.json | xargs ../npmfreeze/bin/npmfreeze
{
"asn1": "0.1.9",
"ctype": "0.3.1",
"sprintf": "0.1.1",
"mimelib-noiconv": "0.1.5",
"abbrev": "1.0.3",
"sax": "0.3.5",
"formidable": "1.0.8",
"httpu": "0.0.1",
"node-uuid": "1.2.0",
"retry": "0.4.0",
"semver": "1.0.12",
"xml2js": "0.1.10",
"inherits": "1.0.0",
"yamlish": "0.0.2",
"slide": "1.1.3"
}
bluesnoop:cloud-api mark$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment