【问题标题】:cross-platform way to get a list of directories and run npm install in each of them跨平台获取目录列表并在每个目录中运行 npm install
【发布时间】:2015-08-06 12:11:12
【问题描述】:

我正在运行下一个命令来为模块目录中的每个模块执行 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'
}

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

【问题讨论】:

    标签: node.js powershell cmd


    【解决方案1】:

    这个脚本应该可以在 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

    【讨论】:

      猜你喜欢
      • 2021-02-12
      • 1970-01-01
      • 2010-10-25
      • 2014-09-20
      • 1970-01-01
      • 2016-02-09
      • 1970-01-01
      • 2011-11-08
      • 2011-11-16
      相关资源
      最近更新 更多