使用多个命令将命令行参数传递给npm'pre'脚本和脚本

时间:2022-04-29 23:19:34

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

有没有办法将命令行参数传递给npm'pre'脚本或运行多个命令的脚本?

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

假设一个简单的脚本mySexyScript.js只是注销process.argv:

console.log(process.argv);

This works

With an npm script:

使用npm脚本:

...
"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

'pre'脚本 - 这不起作用

With an npm script:

使用npm脚本:

...
"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

参数不会传递给mySexyScript,也不会记录它们

Multiple commands - This also doesn't work

多个命令 - 这也不起作用

With an npm script:

使用npm脚本:

...
"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

参数不会传递给mySexyScript,也不会记录它们

1 个解决方案

#1


1  

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

没有办法按照你描述的方式传递args。

Assuming a package.json:

假设一个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).

在默认的系统shell(通常是bash或cmd.exe)上。

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

npm实际上并不了解shell运算符(即&&),因此它无法将args传递给两个脚本。

#1


1  

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

没有办法按照你描述的方式传递args。

Assuming a package.json:

假设一个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).

在默认的系统shell(通常是bash或cmd.exe)上。

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

npm实际上并不了解shell运算符(即&&),因此它无法将args传递给两个脚本。