Ryan Taylor
Ryan Taylor

Reputation: 8892

Passing argument to the middle of an npm script

Title says it all. I want to be able to pass the argument to the middle of an npm script so that I may do the following.

$ npm run deploy -- <destination-path>

In package.json

"scripts": {
    "deploy": "robocopy dist <destination-path> /E /NP"
 }

Is this possible without using environment variables or npm's configuration variables?

Upvotes: 7

Views: 5575

Answers (5)

Mouneer
Mouneer

Reputation: 13489

Levereging npm_config_* variables.

package.json

{
  "scripts": {
    "echoMyParam": "echo 'your param value is' $npm_config_foo"
  }
}

Run

npm run echoMyParam --foo=bar

Result

your param value is bar

It's important to check the docs for other cases: https://docs.npmjs.com/using-npm/config

Upvotes: 2

sarimarton
sarimarton

Reputation: 198

You can make use of the arg structure of "sh -c". In my example below, I have to echo-feed the npm arg into a language parser. The argument for npm run foma <some word> will be in place of the $0:

"sayhello": "bash -c 'echo hello $0!"

A cross-platform solution I use is:

"arg-helper": "node -e \"process.stdout.write(require('child_process').execSync(process.argv[1].replace('$', process.argv[2] || '')))\"",
"sayhello": "npm run arg-helper \"echo hello $!\"

...
>npm run sayhello world

Upvotes: 2

Mina Luke
Mina Luke

Reputation: 2171

I have a different way to do that via shell.

If your npm command is:

"deploy": "robocopy dist ${1} /E /NP"

Where ${1} is the parameter you want to substitute.

Then wrap it in a function as follow:

"deploy": "func() { robocopy dist ${1} /E /NP";}; func"

then you can run a positional parameter substitution in shell as follow:

npm run deploy -- xyz

which would run

robocopy dist xyz /E /NP

And since this is a shell script, you can use default parameters as well:

"deploy": "func() { robocopy dist ${1:-xyz} /E /NP";}; func"

And you can use it as follows:

npm run deploy <==> robocopy dist xyz /E /NP
npm run deploy -- abc <==> robocopy dist abc /E /NP

Upvotes: 4

Ryan Taylor
Ryan Taylor

Reputation: 8892

Per Passing args into run-scripts #5518 it would appear that is it not possible to pass arguments to the middle of the script.

We are not going to support passing args into the middle of the script, sorry. If you really need this, write your test command using literally any of the command line parsers that anyone uses. (Minimist, dashdash, nopt, and commander all support this just fine.)

However, an alternative to this using the npm configuration block has been documented here. My implementation then looks like this:

"name": "foo"
"config": { "destination" : "deploy" },
"scripts": { "deploy": "robocopy dist %npm_package_config_destination% /E /NP" }

I can then override this on the command line and on my build server with:

npm run deploy --foo:destination=C:\path\to\deploy\dir

Upvotes: 8

William
William

Reputation: 1527

You can use an environment variable to set the destination path.

PATH=/path/to/file npm run deploy -- $PATH

or

export PATH=/path/to/file

npm run deploy -- $PATH

Upvotes: 2

Related Questions