Node.js 通过跨平台的方式获取目录列表,并在每个目录中运行npm安装
我正在运行下一个命令来为modules目录中的每个模块安装npmNode.js 通过跨平台的方式获取目录列表,并在每个目录中运行npm安装,node.js,powershell,cmd,Node.js,Powershell,Cmd,我正在运行下一个命令来为modules目录中的每个模块安装npm if (process.platform === 'win32') { return 'powershell -noprofile -command "Get-ChildItem ../modules | ? { $_.PSIsContainer } | % { Push-Location $_.FullName; npm install; Pop-Location }"'; } else { return '
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