【问题标题】:npm package.json OS specific scriptnpm package.json 操作系统特定脚本
【发布时间】:2017-12-18 08:31:53
【问题描述】:

我想创建一个package.json 构建脚本,当从 Windows、Linux、Mac 运行时,它执行的命令集略有不同。

问题是我找不到将它放入package.json 文件的方法,该文件将在每个系统上都运行而不会出现问题。

这是我想要的一个例子:

"scripts" : {
    "build.windows" : "echo do windows specific stuff",
    "build.linux" : "echo do linux specific stuff",
    "build.mac" : "echo do mac specific stuff",
    "build" : "??????????????" <- what to put here to execute script designed for OS
                                  on which npm is running
}

【问题讨论】:

    标签: node.js npm package.json npm-scripts


    【解决方案1】:

    有一个名为 run-script-os ( NPM | GitHub ) 的 NPM 包不需要您编写任何额外的文件,如果您尝试做的事情非常简单,这会很方便。例如,在您的 package.json 中,您可能有类似的内容:

    "scripts": {
        "test": "run-script-os",
        "test:darwin:linux": "export NODE_ENV=test && mocha",
        "test:win32": "SET NODE_ENV=test&& mocha"
    }
    

    然后,您可以在 Windows、Mac 或 Linux 上运行 npm test,并在每一个上获得相似(或不同!)的结果。

    【讨论】:

    • 将“test:unit”与“run-script-os”一起使用的等效语法是什么?
    • @BerndPrager 你能找到上面的等价物吗?
    • 我使用了 "prettier-check": "run-script-os" 并且成功了!
    【解决方案2】:

    您可以使用带有 node run-script 命令的脚本。 npm run 是它的快捷方式。

    包json:

    "scripts" : {
        "build-windows" : "node build-windows.js",
        "build-linux" : "node build-linux.js",
        "build-mac" : "node build-mac.js",
        "build" : "node build.js"
    }
    

    命令行:

    npm run build-windows
    

    如果你不喜欢,你可以在 node.js 中使用命令。

    包json:

    "scripts" : {
        "build" : "node build.js"
    }
    

    构建.js

    var sys = require('sys');
    var exec = require('child_process').exec;
    var os = require('os');
    
    function puts(error, stdout, stderr) { sys.puts(stdout) }
    
    // Run command depending on the OS
    if (os.type() === 'Linux') 
       exec("node build-linux.js", puts); 
    else if (os.type() === 'Darwin') 
       exec("node build-mac.js", puts); 
    else if (os.type() === 'Windows_NT') 
       exec("node build-windows.js", puts);
    else
       throw new Error("Unsupported OS found: " + os.type());
    

    【讨论】:

    • 我认为exec("node build-windows.js", puts); 应该依赖于os
    • @gawi 你是对的。我写了“控制操作系统,然后运行命令”。
    • 我会输入答案:if (os.type() === 'Linux') exec("node build-linux.js", puts); else if (os.type() === 'Darwin') exec("node build-mac.js", puts); else if (os.type() === 'Windows_NT') exec("node build-windows.js", puts);
    • 您可以使用process.env.OS 而不是需要os 模块。只是想提一下。
    • @hurricane 如果我正在使用 exec('build-windows.ps1', puts) 执行 ps1 文件,它不会执行该文件,而只是打开它。知道如何解决这个问题
    【解决方案3】:

    这完全取决于您在脚本中尝试执行的操作,但您可能可以使用 npm cli packages 有效地将跨平台命令添加到任何 shell。

    例如,如果你想删除一个目录,你可以为 windows 和 linux 使用不同的语法:

    rm -rf _site     # bash
    rd /s /q _site   # cmd
    

    或者,您可以使用跨平台工作的 npm 包 rimraf

    npx rimraf _site
    

    以上面 Dave P 的示例为例,您可以使用 cross-env 设置环境变量,如下所示:

    "scripts": {
        "test": "npx cross-env NODE_ENV=test mocha",
    }
    

    如果您不想使用npx 实时安装脚本,您可以像这样提前全局安装它们:

    npm i cross-env -g
    

    这是我在making NPM scripts work cross platform 上写的一篇文章,探讨了其中一些选项

    【讨论】:

      【解决方案4】:

      绝对不是最可靠的方法来做到这一点,但从技术上讲,您可以在一个 npm 脚本中完成这一切:

      {
          "scripts": {
              "build": "( Write-Output 'Powershell' && ./tools/build-ps.ps1 ) || 
                        ( CALL ./tools/build-cmd.bat ) || 
                        ( bash -c 'uname -a | grep -q -i Linux' && bash -c ./tools/build-linux.sh ) ||
                        ( bash -c 'uname -a | grep -q -i Darwin' && bash -c ./tools/build-mac.sh )"
              }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-22
        • 2018-09-04
        • 2012-04-29
        • 1970-01-01
        • 2014-06-26
        相关资源
        最近更新 更多