named parameter to npm run script

2019-07-21 08:25发布

I would like to pass a named parameter to an npm run script so I can do something like the following:

"scripts":{
    "say-hello":"echo $greeting && ls"
 }

 npm run hello --greeting=hello

I'd like it to then put 'hello' in place of the $greeting variable, echo the command and then do the ls (this is obviously just a simple example of a chained command)

2条回答
Fickle 薄情
2楼-- · 2019-07-21 08:52

Another way is to simply add the following '--' followed by your desired param(s). This would be for named parameters that '=' a value. Please note the underlying process in this example, a protractor call, expects a Url arg.

npm run e2e -- --Url="https://yoururlhere.com"
查看更多
smile是对你的礼貌
3楼-- · 2019-07-21 09:19

Just found out that this works:

"scripts":{
  "say-hello" : "echo $npm_config_greeting && ls"
}

Edit:

Any environment variables that start with npm_config_ will be interpreted as a configuration parameter. For example, putting npm_config_foo=bar in your environment will set the foo configuration parameter to bar.

npm docs

查看更多
登录 后发表回答