Reputation: 7829
I'm currently using a gulp task to test a project. This runs tasks using the following tools:
gulp-eslint
)gulp-htmlhint
)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.
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 karma
fails, none of the other tools will run.
Is it possible to create a setup where all of these tools will run, but npm test
will fail if any of the commands failed?
Upvotes: 19
Views: 35353
Reputation: 2806
npm-run-all can also handle this well
You can run multiple npm commands concurrently, continuing on error as follows:
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).
Upvotes: 11
Reputation: 7829
concurrently is a nice library that can handle this. It can be installed from npm.
npm install --save-dev concurrently
When each section is sorted as a separate script, the scripts section of package.json
looks a bit like this:
{
...
"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'",
},
...
}
Upvotes: 7
Reputation: 6998
The scripts tags in package.json
are run by your shell, so you can run the command that you want the shell to run:
"scripts": {
"test": "karma ; protractor ; eslint ; htmlhint ; stylelint"
},
Will run all commands if you have a 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:
#!/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
Upvotes: 14