跨平台的方式获取目录列表,并在其中运行npm install

我正在运行下一个命令为模块目录中的每个模块执行npm install

if (process.platform === 'win32') { return 'powershell -noprofile -command "Get-ChildItem ../modules | ? { $_.PSIsContainer } | % { Push-Location $_.FullName; npm install; Pop-Location }"'; } else { return 'for dir in ../modules/*; do (cd $dir && pwd && npm install); done' } 

有没有更好的方法来做到这一点? 应该是跨平台的

这个脚本应该在Windows和Linux系统上工作:

 var fs = require('fs'); var path = require('path'); var child_process = require('child_process'); fs.readdirSync(path.join(__dirname, 'modules') .filter(function(dir) { return fs.statSync(path.join(__dirname, 'modules', dir)).isDirectory(); }) .forEach(function(dir) { child_process.spawnSync('npm', ['install'], { cwd: path.join(__dirname, 'modules', dir)) }); }); 

该脚本列出目录内容,过滤掉非目录,然后在每个目录中执行npm install