将命令行参数传递给package.json中的npm脚本 [英] Pass command line args to npm scripts in package.json
问题描述
我的package.json中包含以下脚本:
"scripts": {
"vumper": "node node_modules/vumper/index.js",
"format": "prettier --single-quote -width=80 --write package.json"
},
'vumper'软件包采用命令行参数(例如'dv').我想做的是有一个连续运行这两个命令的命令.
基本上,我希望能够运行:
npm run vumber dv
然后
npm run format
但是在一个命令中,类似于
npm run my-build dv
可以运行上述两个命令,正确接受命令行参数'dv'并将其传递给第一个npm run vumper.这可能吗?
简短答案:
本质上,您想要的是具有这样的npm脚本,从而通过CLI提供<arg-here>
;
...
"scripts": {
"my-build": "npm run vumper <arg-here> && npm run format",
...
},
...
但是,不幸的是,npm没有内置功能来实现此目的.
特殊的npm选项--
(有关此选项的更多信息,请参阅下面的解决方案1 的末尾),只能用于将参数传递给 END 的脚本,但不在中间.因此,如果两个命令的顺序相反,则可以像这样使用--
选项:
...
"scripts": {
"my-build": "npm run format && npm run vumper --",
...
},
...
要克服没有内置功能将参数传递到脚本中间的局限性,请考虑以下解决方案:
-
有关仅限Bash的解决方案,请参见解决方案1" 部分.
-
如果需要跨平台支持,请遵循解决方案2" 部分中所述的解决方案.
解决方案1- Bash(MacOS/Linux/等.):
在 package.json 的 说明: 名为 这两个 运行 要通过您的CLI调用 注意: 在这种情况下,结尾的 必须在参数前指定特殊选项 ... package.json 配置您的 运行 根据解决方案1 ,要通过CLI调用 说明: run.js 使用 run.js 还利用 I have the below scripts in my package.json: The 'vumper' package takes in a command line argument (such as 'dv'). What I would like to be able to do is have a command that runs both of these in succession. Essentially, I would like to be able to run: and then but in one command, something like which would run both of the above commands, correctly accepting the command line argument 'dv' and passing it to the first npm run vumper. Is this possible? Essentially, what you're wanting is to have an npm-script something like this, whereby However, unfortunately npm does not have a built-in feature to achieve this. The special npm option To overcome the limitation of there being no built-in feature to pass an argument into the MIDDLE of a script consider the following solutions: For a Bash only solution refer to the "Solution 1" section. If cross platform support is required then follow the solution described in the "Solution 2" section.
Configure your package.json Explanation: The Bash function named These two Running To invoke Note: In this instance the trailing The special option ... The special option
For a cross-platform solution, (one which works successfully with Bash, Windows Command Prompt / cmd.exe, and PowerShell etc..), you'll need to utilize a nodejs helper script as follows. run.js Let's name the nodejs script run.js and save it in the projects root directory, at the same level as package.json. package.json Configure your Running As per Solution 1, to invoke Explanation: run.js utilizes run.js also utilizes 这篇关于将命令行参数传递给package.json中的npm脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!scripts
部分中配置my-build
脚本,以调用Bash ...
"scripts": {
"my-build": "func() { npm run vumper \"$1\" && npm run format; }; func",
"vumper": "node node_modules/vumper/index.js",
"format": "prettier --single-quote -width=80 --write package.json"
},
...
func
的Bash函数执行以下操作:
npm run vumper <arg>
.其中,<arg>
是通过CLI传递的shell参数.在脚本中使用$1
进行引用(即第一个 positional参数/参数).npm run format
运行名为format
的脚本.npm run
命令是使用&&
运算符链接在一起的,因此,第二个npm run format
命令将仅在初始npm run vumper <arg>
命令成功完成(即返回0
退出代码)时运行. >
my-build
脚本: my-build
,您需要运行:
npm run my-build -- dv
dv
部分是将传递给您的vumper
脚本的参数.--
. 文档将--
选项描述为:
const execSync = require('child_process').execSync;
const arg = process.argv[2] || 'dv'; // Default value `dv` if no args provided via CLI.
execSync('npm run vumper ' + arg, {stdio:[0, 1, 2]});
execSync('npm run format', {stdio:[0, 1, 2]});
my-build
脚本以如下方式调用 run.js :
...
"scripts": {
"my-build": "node run",
"vumper": "node node_modules/vumper/index.js",
"format": "prettier --single-quote -width=80 --write package.json"
},
...
my-build
脚本: my-build
,您需要运行:
npm run my-build -- dv
dv
).如果在运行npm run my-build
时未提供任何参数,则默认值(即dv
)将传递给vumper
npm-script.child_process.execSync(...)
npm run
命令."scripts": {
"vumper": "node node_modules/vumper/index.js",
"format": "prettier --single-quote -width=80 --write package.json"
},
npm run vumber dv
npm run format
npm run my-build dv
Short Answer:
<arg-here>
is provide via the CLI;...
"scripts": {
"my-build": "npm run vumper <arg-here> && npm run format",
...
},
...
--
, (refer to the end of Solution 1 below for further info about this option), can only be used to pass an argument to the END of a script but NOT into the MIDDLE. So, if your two commands were in the opposite order, the --
option could be used like this:...
"scripts": {
"my-build": "npm run format && npm run vumper --",
...
},
...
Solution 1 - Bash (MacOS/Linux/ etc..):
my-build
script in the scripts
section of package.json to invoke a Bash shell function, as shown below:...
"scripts": {
"my-build": "func() { npm run vumper \"$1\" && npm run format; }; func",
"vumper": "node node_modules/vumper/index.js",
"format": "prettier --single-quote -width=80 --write package.json"
},
...
func
does the following:
npm run vumper <arg>
. Whereby <arg>
will be the shell argument passed via the CLI. It is referenced in the script using $1
(i.e. the first positional parameter/argument).format
via the command npm run format
.npm run
commands are chained using the &&
operator, so the second npm run format
command will only run if the initial npm run vumper <arg>
command completes successfully (i.e. returns a 0
exit code).my-build
script:my-build
via your CLI you'll need to run:npm run my-build -- dv
dv
part is the argument that will be passed to your vumper
script.--
must be specified before the argument. The docs describe the --
option as:
--
is used by getopt
to delimit the end of the options. npm will pass all the arguments after the --
directly to your script: ... The arguments will only be passed to the script specified after npm run
and not to any pre or post script.
Solution 2 - Cross-platform:
const execSync = require('child_process').execSync;
const arg = process.argv[2] || 'dv'; // Default value `dv` if no args provided via CLI.
execSync('npm run vumper ' + arg, {stdio:[0, 1, 2]});
execSync('npm run format', {stdio:[0, 1, 2]});
my-build
script to invoke run.js as follows:...
"scripts": {
"my-build": "node run",
"vumper": "node node_modules/vumper/index.js",
"format": "prettier --single-quote -width=80 --write package.json"
},
...
my-build
script:my-build
via your CLI you'll need to run:npm run my-build -- dv
process.argv
to obtain the argument passed via the CLI (e.g. dv
). If no argument is provided when running npm run my-build
the default value, (i.e. dv
), is passed to the vumper
npm-script.child_process.execSync(...)
to shell-out/invoke the two npm run
commands.