Skip to content

Instantly share code, notes, and snippets.

@maxbeatty
Created March 16, 2014 23:41
Show Gist options
  • Save maxbeatty/9591512 to your computer and use it in GitHub Desktop.
Save maxbeatty/9591512 to your computer and use it in GitHub Desktop.
slow npm install
#!/usr/bin/env node
// This script will attempt to install your dependencies one by one
// I wrote it while debugging repeated failed attempts to `npm install`
// In the end, `npm cache clean` solved things for me
var json = require(__dirname + '/../package.json'),
depNames = Object.keys(json.dependencies),
devDepNames = Object.keys(json.devDependencies),
spawn = require('child_process').spawn,
fs = require('fs');
var install = function(module, version) {
var path = 'node_modules/' + module;
if (fs.existsSync(path)) {
console.log(module + ' already installed. skipping.');
next();
} else {
console.log('installing ' + module + '@' + version);
var child = spawn('npm', ['install', module + '@' + version], { stdio: 'inherit' });
child.on('close', function(code) {
console.log(module + ' exited w/ code ' + code);
if (code === 0) {
next();
} else {
console.log('aborting');
var rm = spawn('rm', ['-r', path]);
rm.on('error', function() {
console.log('error removing bad install module', arguments);
});
}
});
}
};
var next = function() {
var d;
if (depNames.length === 0) {
if (devDepNames.length === 0) {
console.log('all done!');
} else {
d = devDepNames.pop();
install(d, json.devDependencies[d]);
}
} else {
d = depNames.pop();
install(d, json.dependencies[d]);
}
};
next();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment