Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass an argument to an npm run script comprising multiple commands

I'd like to pass an argument into the first of two commands in my npm script:

"scripts": {
  "myscript": "a && b"
}

When I run npm run myscript -- --somearg=somevalue, the argument is passed to command b but not command a. Is there some way to instead ensure that the first command receives the argument?

like image 366
Keir Avatar asked Feb 16 '16 09:02

Keir


2 Answers

"scripts": {
  "myscript": "a",
  "postmyscript": "b"
}
like image 157
4thex Avatar answered Nov 14 '22 21:11

4thex


I found the following solution after searching a lot from this discussion:

https://github.com/npm/npm/issues/9627#issuecomment-152607809

and

https://github.com/npm/npm/issues/9627#issuecomment-178576802

Solution 1. So in your package json add a config object like so

{
  "name": "packageName",
  "config" : { "variable1" : "value1" }
}

then you can access the variable like so on windows

    {
      "name": "packageName",
      "config" : { "variable1" : "value1" },
      "scripts": {
              "myscript": "a -c %npm_package_config_variable1%  && b -c %npm_package_config_variable1%"
      }
    }

in mac/linux I am assuming (not a 100%)

{
  "name": "packageName",
  "config" : { "variable1" : "value1" },
  "scripts": {
          "myscript": "a -c $npm_package_config_variable1  && b -c $npm_package_config_variable1"
  }
}

then you can override the variable by calling the script like so:

npm run myscript --packageName:variable1=value2

Solution 2.

no need for the config entry in the package.json

{
  "name": "packageName",
  "scripts": {
          "myscript": "a -c %npm_config_variable1%  && b -c %npm_config_variable1%"
  }
}

and call it like so

npm run myscript --variable1=value2

reason to pick one over the other:

https://github.com/npm/npm/issues/9627#issuecomment-178813965

like image 33
abann sunny Avatar answered Nov 14 '22 21:11

abann sunny