Skip to content

Instantly share code, notes, and snippets.

@andymac4182
Created August 1, 2016 02:32
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 andymac4182/d116544222ecabf567697e3eb1cadd54 to your computer and use it in GitHub Desktop.
Save andymac4182/d116544222ecabf567697e3eb1cadd54 to your computer and use it in GitHub Desktop.
NpmInstallRecursive
var fs = require('fs');
var path = require('path');
var exec = require('child_process').exec;
var walk = function(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
file = path.resolve(dir, file);
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
var fileName = path.basename(file)
var currentDir = path.dirname(file)
if(fileName === 'package.json' && file.indexOf('node_modules') === -1) {
results.push(currentDir);
}
if (!--pending) done(null, results);
}
});
});
});
};
console.log('Installing node modules under ' + __dirname)
walk(__dirname, function (err, files) {
if(!err) {
installNpm(files)
} else {
console.error(err)
}
})
var installNpm = function(pathsToInstall) {
if (pathsToInstall.length > 0) {
pathsToInstall.forEach(function (path) {
cmd = 'Installing packages in ' + path
console.log(cmd)
exec('npm install', {cwd: path}, function (error, stdout, stderr) {
});
})
} else {
console.error('No package.json found')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment