Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing argument to the middle of an npm script

Tags:

npm

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?

like image 338
Ryan Taylor Avatar asked Jun 16 '16 21:06

Ryan Taylor


2 Answers

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
like image 100
Ryan Taylor Avatar answered Sep 28 '22 09:09

Ryan Taylor


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

like image 42
William Avatar answered Sep 28 '22 07:09

William