为npm测试运行多个命令
我正在使用gulp任务来测试一个项目。这使用以下工具运行任务:为npm测试运行多个命令
- 噶(异步)
- 量角器(衍生进程)
- ESlint(使用
gulp-eslint
) - HTMLHint(使用
gulp-htmlhint
) - Stylelint(使用
gulp-postcss
)
如果其中任何任务失败,任务将失败。
所有这些工具都有非常好的cli接口。所以我决定使用npm测试脚本来运行这些工具。
简单地说,让我们说所有的工具只需在没有任何标志的情况下调用它们即可运行。那么这是可以做到用:
{
...
"scripts": {
"test": "karma && protractor && eslint && htmlhint && stylelint"
},
...
}
但是,这意味着如果karma
失败,没有其他工具将运行。
是否可以创建所有这些工具都可以运行的设置,但如果任何命令失败,npm test
将会失败?
"scripts": {
"test": "karma ; protractor ; eslint ; htmlhint ; stylelint"
},
将运行所有的命令,如果你有一个UNIX/OSX壳:
npm-run-all也可以处理这个问题很好
您可以运行多个NPM并发命令,如下错误继续:
npm-run-all --parallel --continue-on-error karma protractor 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).
在package.json
脚本标签是由你的shell,你希望shell都跑,这样你就可以运行该命令。
为了能够像指定一样保留exit_code,您需要有一个单独的脚本来运行这些命令。也许是这样的:
#!/bin/bash
EXIT_STATUS=0
function check_command {
"[email protected]"
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
这将运行所有的命令,但它会与最后一个命令的代码存在退出,忽略任何先前的退出代码。所以在这种情况下,任何失败的测试都将被忽略。 –
我已经添加了一个例子。 – bolav
请注意,第一个示例中的'test'脚本不支持Windows,';'语法在PowerShell中无效。 –
concurrently是可以处理这个问题一个很好的库。它可以从npm安装。
npm install --save-dev concurrently
当每一个部分进行排序作为一个单独的脚本中,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'",
},
...
}
''''测试':'同时'npm运行karma''npm运行量角器''npm运行eslint''npm运行htmlhint''npm运行stylelint'“'''应该'''''测试”:“同时\“npm run karma \”\ npm运行量角器\“\”npm运行eslint \“\”npm运行htmlhint \“\”npm运行stylelint \“”'''在windows上。 – lfree
我现在更喜欢这个。 –