Propagate arguments to two commands combined with &&

Viewed 50

I have two commands in package.json combined with '&&':

  "scripts": {
    "someAction": "node dist/scripts/actionOne && node -r dist/scripts/actionTwo"
  },

Is it possible to call this script from cli, passing arguments to both 'actionOne' and 'actionTwo' ?

When calling npm run someAction -- firstArg, secondArg args are passed only to 'actionOne' script.

*Number of args expected by actionOne and actionTwo are identical.

2 Answers

If there is a real answer, I wanna know. But also, you can make a script like this

some-action.sh

set -e
dist/scripts/ActionOne $@
dist/scripts/ActionTwo $@

and then put this in your package.json

"scripts": {
  "someAction": "bash some-action.sh"
},

After looking at the docs, it looks like npm-run-all would work with argument placeholders.

We can use placeholders to give the arguments preceded by -- to scripts.

$ npm-run-all build "start-server -- --port {1}" -- 8080

This is useful to pass through arguments from npm run command.

{
    "scripts": {
        "start": "npm-run-all build \"start-server -- --port {1}\" --"
    }
}

$ npm run start 8080

> example@0.0.0 start /path/to/package.json
> npm-run-all build "start-server -- --port {1}" -- "8080"

So you could do something like this:

{
    "scripts": {
        "start": "npm-run-all dist/scripts/actionOne -- --arg {1} && dist/scripts/actionTwo -- --arg2 {2}"
    }
}

Then:

npm run start arg1 arg2
Related