Created
August 1, 2016 02:32
-
-
Save andymac4182/d116544222ecabf567697e3eb1cadd54 to your computer and use it in GitHub Desktop.
NpmInstallRecursive
This file contains 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
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