Created
March 18, 2020 10:18
-
-
Save michchan/5d84b82c97523396a43e626a87a401b5 to your computer and use it in GitHub Desktop.
Script to run npm install for all nested folders
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
/** | |
* Script to run npm install for all nested folders | |
* | |
* Reference: https://stackoverflow.com/questions/31773546/the-best-way-to-run-npm-install-for-nested-folders | |
*/ | |
var fs = require('fs') | |
var resolve = require('path').resolve | |
var join = require('path').join | |
var cp = require('child_process') | |
var os = require('os') | |
// get library path | |
var lib = resolve(__dirname, './') | |
fs.readdirSync(lib) | |
.forEach(function (mod) { | |
var modPath = join(lib, mod) | |
// ensure path has package.json | |
if (!fs.existsSync(join(modPath, 'package.json'))) return | |
// npm binary based on OS | |
var npmCmd = os.platform().startsWith('win') ? 'npm.cmd' : 'npm' | |
// install folder | |
cp.spawn(npmCmd, ['i'], { env: process.env, cwd: modPath, stdio: 'inherit' }) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment