Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running multiple commands for npm test

Tags:

node.js

npm

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

  • Karma (async)
  • Protractor (spawned process)
  • ESlint (using gulp-eslint)
  • HTMLHint (using gulp-htmlhint)
  • Stylelint (using 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?

like image 295
Remco Haszing Avatar asked Feb 26 '16 10:02

Remco Haszing


People also ask

How do I run multiple npm commands?

Approach 1(npm-run all package): We can use the” npm-run all” package to run different scripts at the same time. First, we have to install the package itself by using the command. After installation of the package we have to navigate to the package.

How do I run multiple npm commands in sequentially?

Sequentially. If you have commands that need to run in order, then using a double ampersand - && - in between those commands will make it so that the preceding command must finish before the next can start.

Can I run npm run build multiple times?

Nope, Anything you change over code, build will rerun for that change.


1 Answers

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
like image 189
bolav Avatar answered Sep 22 '22 19:09

bolav