Node.js 为npm测试运行多个命令

Node.js 为npm测试运行多个命令,node.js,npm,Node.js,Npm,我目前正在使用一个吞咽任务来测试一个项目。这将使用以下工具运行任务: 因果报应(异步) 量角器(衍生进程) ESlint(使用吞咽ESlint) HTMLHint(使用吞下HTMLHint) Stylelint(使用gulp postsss) 如果其中任何任务失败,则任务将失败 所有这些工具都有完美的cli接口。所以我决定使用npm测试脚本来运行这些工具 简单地说,所有工具都是通过调用它们而运行的,没有任何标志。然后,可以使用以下方法完成此操作: { ... "scripts": {

我目前正在使用一个吞咽任务来测试一个项目。这将使用以下工具运行任务:

  • 因果报应(异步)
  • 量角器(衍生进程)
  • ESlint(使用
    吞咽ESlint
  • HTMLHint(使用
    吞下HTMLHint
  • Stylelint(使用
    gulp postsss
如果其中任何任务失败,则任务将失败

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

简单地说,所有工具都是通过调用它们而运行的,没有任何标志。然后,可以使用以下方法完成此操作:

{
  ...
  "scripts": {
    "test": "karma && protractor && eslint && htmlhint && stylelint"
  },
  ...
}
然而,这意味着如果
karma
失败,其他工具将无法运行


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

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

"scripts": {
  "test": "karma ; protractor ; eslint ; htmlhint ; stylelint"
},
如果您有unix/OSX shell,将运行所有命令

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

#!/bin/bash

EXIT_STATUS=0

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

check_command karma
check_command protractor
check_command eslint
check_command htmlhint
check_command stylelint
exit $EXIT_STATUS
{
  ...
  "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'",
  },
  ...
}
是一个很好的库,可以处理这个问题。它可以从npm安装

npm install --save-dev concurrently
当每个部分作为单独的脚本排序时,
package.json
的脚本部分看起来有点像这样:

#!/bin/bash

EXIT_STATUS=0

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

check_command karma
check_command protractor
check_command eslint
check_command htmlhint
check_command stylelint
exit $EXIT_STATUS
{
  ...
  "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'",
  },
  ...
}
我也能处理好这件事

您可以同时运行多个npm命令,在出现错误时继续执行,如下所示:

npm运行全部--并行--继续执行错误业力量角器eslint htmlhint stylelint

文档中所述的选项:

-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).
-p,--parallel-并行运行一组任务。
e、 g.“npm运行所有-p foo bar”类似于
“npm运行foo和npm运行栏”。
-c、 --出错时继续-设置标志以继续执行其他任务
即使任务抛出错误。”run-p本身
如果有一个或多个任务,将以非零代码退出
抛出错误。

这将运行所有命令,但它将以最后一个命令的exist代码退出,忽略以前的任何退出代码。因此,在本例中,任何失败的测试都将被忽略。我现在添加了一个这样的示例。请注意,第一个示例中的
测试
脚本不支持Windows,
语法在PowerShell中无效。
sh:1:语法错误:“;”意外
,这太糟糕了;\uuu;在powershell下,使用
&
而不是
&
的“测试”:“并发”npm运行karma“npm运行量角器”npm运行eslint“npm运行htmlhint”npm运行htmlhint“npm运行stylelint”应该是
“测试”:“并发”npm运行karma“\”npm运行量角器“\”npm运行eslint“\”npm运行htmlhint“\”npm运行stylelint”
在windows上。事实上,我现在更喜欢这个。