node.js 为 npm test 运行多个命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35649551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:58:36  来源:igfitidea点击:

Running multiple commands for npm test

node.jsnpm

提问by Remco Haszing

I'm currently using a gulp task to test a project. This runs tasks using the following tools:

我目前正在使用 gulp 任务来测试项目。这使用以下工具运行任务:

  • Karma (async)
  • Protractor (spawned process)
  • ESlint (using gulp-eslint)
  • HTMLHint (using gulp-htmlhint)
  • Stylelint (using gulp-postcss)
  • 业力(异步)
  • 量角器(生成过程)
  • ESlint(使用gulp-eslint
  • HTMLHint(使用gulp-htmlhint
  • Stylelint(使用gulp-postcss

The task fails if any of these tasks failed.

如果这些任务中的任何一个失败,则任务失败。

All of these tools have perfectly fine cli interfaces. So I decided I'd like to run these tools using an npm test script instead.

所有这些工具都有完美的 cli 界面。所以我决定使用 npm 测试脚本来运行这些工具。

For simplicitly let's say all tools run by simply invoking them without any flags. Then this can be done using:

简单地说,让我们说所有工具都通过简单地调用它们而没有任何标志来运行。然后这可以使用:

{
  ...
  "scripts": {
    "test": "karma && protractor && eslint && htmlhint && stylelint"
  },
  ...
}

However, this means that if karmafails, none of the other tools will run.

但是,这意味着如果karma失败,其他工具都不会运行。

Is it possible to create a setup where all of these tools will run, but npm testwill fail if any of the commands failed?

是否可以创建一个设置,所有这些工具都可以运行,但npm test如果任何命令失败都会失败?

采纳答案by Chris

npm-run-allcan also handle this well

npm-run-all也可以很好地处理这个问题

You can run multiple npm commands concurrently, continuing on error as follows:

你可以同时运行多个 npm 命令,继续报错如下:

npm-run-all --parallel --continue-on-error karma protractor eslint htmlhint stylelint

npm-run-all --parallel --continue-on-error karma protractor eslint htmlhint stylelint

Options as written in the documentation:

文档中所写的选项:

-p, --parallel <tasks>   - Run a group of tasks in parallel.
                           e.g. 'npm-run-all -p foo bar' is similar to
                                'npm run foo & npm run bar'.
-c, --continue-on-error  - Set the flag to continue executing other tasks
                           even if a task threw an error. 'run-p' itself
                           will exit with non-zero code if one or more tasks
                           threw error(s).

回答by bolav

The scripts tags in package.jsonare run by your shell, so you can run the command that you want the shell to run:

中的脚本标记package.json由您的 shell 运行,因此您可以运行希望 shell 运行的命令:

"scripts": {
  "test": "karma ; protractor ; eslint ; htmlhint ; stylelint"
},

Will run all commands if you have a unix/OSX shell.

如果您有 unix/OSX shell,将运行所有命令。

To be able to retain the exit_code like you specify you need to have a separate script to run the commands. Maybe something like this:

为了能够像您指定的那样保留 exit_code,您需要有一个单独的脚本来运行命令。也许是这样的:

#!/bin/bash

EXIT_STATUS=0

function check_command {
    "$@"
    local STATUS=$?
    if [ $STATUS -ne 0 ]; then
        echo "error with  ($STATUS)" >&2
        EXIT_STATUS=$STATUS
    fi
}

check_command karma
check_command protractor
check_command eslint
check_command htmlhint
check_command stylelint
exit $EXIT_STATUS

回答by Remco Haszing

concurrentlyis a nice library that can handle this. It can be installed from npm.

concurrently是一个很好的库,可以处理这个问题。它可以从 npm 安装。

npm install --save-dev concurrently

When each section is sorted as a separate script, the scripts section of package.jsonlooks a bit like this:

当每个部分都作为一个单独的脚本进行排序时,脚本部分package.json看起来有点像这样:

{
  ...
  "scripts": {
    "test": "concurrently 'npm run karma' 'npm run protractor' 'npm run eslint' 'npm run htmlhint' 'npm run stylelint'",
    "karma": "karma start --single-run",
    "protractor": "protractor",
    "eslint": "eslint .",
    "htmlhint": "htmlhint 'app/**/*.html'",
    "stylelint": "stylelint 'app/**/*.css'",
  },
  ...
}

回答by Bas Heerschop

You can use a tool like script-launcherto extend the features of you package.jsonfile.

您可以使用脚本启动器之类的工具来扩展package.json文件的功能。

With script-launcheryou can use arrays as scripts and run script concurrently, and many more.

使用脚本启动器,您可以将数组用作脚本并同时运行脚本等等。

Example using script arrays and concurrency

使用脚本数组和并发的示例

{
  "scripts": {
    "lint": [
      [
        "karma",
        "protractor",
        "eslint",
        "htmlhint",
        "stylelint"
      ],
      "echo All test were successful."
    ],
    "karma": "echo karma start --single-run",
    "protractor": "echo protractor",
    "eslint": "echo eslint .",
    "htmlhint": "echo htmlhint 'app/**/*.html'",
    "stylelint": "echo stylelint 'app/**/*.css'"
  }
}

This will also work on Windows. script-launcherwill handle the OS related stuff like globbing patterns and environment variables incompatibility etc..

这也适用于 Windows。脚本启动器将处理操作系统相关的东西,如通配模式和环境变量不兼容等。

Use the examples from the Table of Contentson how to implement this.

使用目录中的示例了解如何实现这一点。