将命令行参数传递给 package.json 中的 npm 脚本 [英] Pass command line args to npm scripts in package.json

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

问题描述

我的 package.json 中有以下脚本:

I have the below scripts in my package.json:

"scripts": {
    "vumper": "node node_modules/vumper/index.js",
    "format": "prettier --single-quote -width=80 --write package.json"
 },

vumper"包接受命令行参数(例如dv").我希望能够做的是有一个命令可以连续运行这两个命令.

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:

npm run vumber dv

然后

npm run format

但是在一个命令中,类似于

but in one command, something like

npm run my-build dv

这将运行上述两个命令,正确接受命令行参数dv"并将其传递给第一个 npm run vumper.这可能吗?

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?

推荐答案

简答:

本质上,你想要的是一个类似这样的 npm-script,其中 是通过 CLI 提供的;

Short Answer:

Essentially, what you're wanting is to have an npm-script something like this, whereby <arg-here> is provide via the CLI;

...
"scripts": {
  "my-build": "npm run vumper <arg-here> && npm run format",
  ...
},
...

然而,不幸的是 npm 没有内置功能来实现这一点.

However, unfortunately npm does not have a built-in feature to achieve this.

特殊的 npm 选项 --,(请参阅下面解决方案 1 的结尾以获取有关此选项的更多信息),只能用于将参数传递给脚本的 END 但不进入 MIDDLE.因此,如果您的两个命令的顺序相反,则可以像这样使用 -- 选项:

The special npm option --, (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 --",
  ...
},
...

要克服没有内置功能将参数传递到脚本的 MIDDLE 的限制,请考虑以下解决方案:

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:

  1. 有关仅 Bash 的解决方案,请参阅解决方案 1" 部分.

如果需要跨平台支持,请遵循解决方案 2" 部分中描述的解决方案.

If cross platform support is required then follow the solution described in the "Solution 2" section.


解决方案 1 - Bash(MacOS/Linux/等):

package.jsonscripts 部分配置您的 my-build 脚本以调用 Bash shell函数,如下图:


Solution 1 - Bash (MacOS/Linux/ etc..):

Configure your my-build script in the scripts section of package.json to invoke a Bash shell function, as shown below:

package.json

...
"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 函数执行以下操作:

The Bash function named func does the following:

  1. 首先运行 npm run vumper .其中 将是通过 CLI 传递的 shell 参数.它在脚本中使用 $1 引用(即第一个 位置参数/argument).
  2. 随后它通过命令 npm run format 运行名为 format 的脚本.
  1. Firstly runs 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).
  2. Subsequently it runs the script named format via the command npm run format.

这两个 npm run 命令使用 && 操作符链接起来,所以第二个 npm run format 命令只会在以下情况下运行初始 npm run vumper 命令成功完成(即返回 0 退出代码).

These two 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 脚本:

Running my-build script:

要通过 CLI 调用 my-build,您需要运行:

To invoke my-build via your CLI you'll need to run:

npm run my-build -- dv

注意:

  1. 在这种情况下,尾随 dv 部分是将传递给您的 vumper 脚本的参数.

  1. In this instance the trailing dv part is the argument that will be passed to your vumper script.

特殊选项 -- 必须在参数之前指定.docs-- 选项描述为:

The special option -- must be specified before the argument. The docs describe the -- option as:

... 特殊选项 --getopt 来分隔选项的结尾.npm 会将 -- 之后的所有参数直接传递给您的脚本:...这些参数只会传递给 npm run 之后指定的脚本,而不传递给任何 pre或发布脚本.

... The special option -- 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.


解决方案 2 - 跨平台:

对于跨平台解决方案(一个可以成功与 Bash、Windows 命令提示符/cmd.exe 和 PowerShell 等配合使用的解决方案),您需要使用如下的 nodejs 帮助程序脚本.


Solution 2 - Cross-platform:

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

让我们将 nodejs 脚本命名为 run.js 并将其保存在项目根目录中,与 package.json 处于同一级别.

Let's name the nodejs script run.js and save it in the projects root directory, at the same level as package.json.

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]});

package.json

配置您的 my-build 脚本以调用 run.js,如下所示:

Configure your 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 脚本:

Running my-build script:

根据解决方案 1,要通过 CLI 调用 my-build,您需要运行:

As per Solution 1, to invoke my-build via your CLI you'll need to run:

npm run my-build -- dv

说明:

  • run.js 使用 process.argv 获取通过 CLI 传递的参数(例如 dv).如果在运行 npm run my-build 时没有提供参数,则默认值(即 dv)将传递给 vumper npm-script.

  • run.js utilizes 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.

run.js 还利用了 child_process.execSync(...)shell-out/invoke 两个 npm run 命令.

run.js also utilizes child_process.execSync(...) to shell-out/invoke the two npm run commands.

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

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