Passing command line arguments to npm 'pre' script and script with multiple commands

六月ゝ 毕业季﹏ 提交于 2020-05-14 16:47:24

问题


Is there a way to pass command line arguments to an npm 'pre' script or to a script which runs multiple commands?

Assuming a simple script mySexyScript.js that just logs out the process.argv :

console.log(process.argv);

This works

With an npm script:

...
"scripts": {
    ....
    "sexyscript": "node mySexyScript.js"
    ....
}
...

running:

npm run sexyscript -- --foo=bar

the arguments are logged to the console as expected.

'pre' script - This doesn't work

With an npm script:

...
"scripts": {
    ....
    "presexyscript": "node mySexyScript.js"
    "sexyscript": "node mySuperSexyScript.js"
    ....
}
...

running:

npm run sexyscript -- --foo=bar

the arguments are not passed to mySexyScript and they are not logged

Multiple commands - This also doesn't work

With an npm script:

...
"scripts": {
    ....
    "sexyscript": "node mySexyScript.js && node mySuperSexyScript.js"
    ....
}
...

running:

npm run sexyscript -- --foo=bar

the arguments are not passed to mySexyScript and they are not logged


回答1:


There is no way to pass args in the way that you are describing.

Assuming a package.json:

...
"scripts": {
    ....
    "somescript": "node one.js && node two.js"
    ....
}
...

Running:

npm run somescript -- --foo=bar

basically just runs

node one.js && node two.js --foo=bar

on the default system shell (usually bash or cmd.exe).

npm doesn't actually know anything about shell operators (i.e. &&), so it can't pass args to both scripts.



来源:https://stackoverflow.com/questions/42491208/passing-command-line-arguments-to-npm-pre-script-and-script-with-multiple-comm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!