使用多个命令将命令行参数传递给 npm 'pre' 脚本和脚本 [英] Passing command line arguments to npm 'pre' script and script with multiple commands

查看:77
本文介绍了使用多个命令将命令行参数传递给 npm 'pre' 脚本和脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

console.log(process.argv);

这有效

使用 npm 脚本:

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

运行:

npm run sexyscript -- --foo=bar

参数按预期记录到控制台.

the arguments are logged to the console as expected.

'pre' 脚本 - 这不起作用

使用 npm 脚本:

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

运行:

npm run sexyscript -- --foo=bar

参数不会传递给 mySexyScript 并且不会被记录

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

多个命令 - 这也不起作用

使用 npm 脚本:

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

运行:

npm run sexyscript -- --foo=bar

参数不会传递给 mySexyScript 并且不会被记录

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

推荐答案

没有办法以您描述的方式传递参数.

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

假设一个 package.json:

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

运行:

npm run somescript -- --foo=bar

基本上只是运行

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

在默认的系统外壳上(通常是 bashcmd.exe).

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

npm 实际上对 shell 操作符(即 &&)一无所知,因此它无法将 args 传递给两个脚本.

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

这篇关于使用多个命令将命令行参数传递给 npm 'pre' 脚本和脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆