将随机数 (UUID) 值分配给 npm 脚本中的环境变量
Assign a random number (UUID) value to a environment variable in npm script
我想在调用我的 npm 脚本时添加一个 UUID 参数。每次调用脚本时,我都想生成一个新号码。它应该看起来像:
"build": "cross-env UUID=unique_number ng build"
我唯一需要的就是在此处生成 unique_number
。我尝试使用 uuid 包,但我不知道如何在脚本中触发它并将数字作为参数传递。
tl;dr 由于您的问题显示了 cross-var
的使用,我假设需要一个跨平台解决方案。在这种情况下,请参考 解决方案 A。但是,如果我的假设不正确,请参考 解决方案 B 或 C。
解决方案 A:跨平台(Windows/Linux/macOS...)
Fo 跨平台解决方案(即 运行 在 Windows、Linux 和 macOS 上成功...),您需要利用 nodejs 来达到你的要求。有几种不同的方法可以解决这个问题,如以下两个小节所述:
- 使用外部 nodejs (.js) 文件
- 在 package.json 中内联您的 JavaScript。
注意两种方法实际上是相同的
使用外部 nodejs (.js) 文件
创建一个 nodejs 实用程序脚本。让我们将文件命名为 运行-ng-build.js 并将其保存在项目目录的根目录中,即与 package.json 目前居住:
运行-ng-build.js
const uuid = require('uuid/v1');
const execSync = require('child_process').execSync;
process.env.UUID = uuid();
execSync('ng build', { stdio: [0, 1, 2]} );
在 package.json 的 scripts
部分用以下内容替换当前的 build
脚本:
package.json
"scripts": {
"build": "node run-ng-build"
}
解释:
- 在运行-ng-build.js中我们需要uuid package and the nodejs built-in
execSync()
.
- 要创建名为
UUID
的环境变量,我们使用 nodejs 内置 process.env
,并通过调用 uuid()
为其分配 uuid 值].
- 然后我们使用
execSync
调用ng build
命令。
options.stdio
选项配置父进程和子进程之间的管道 - [0, 1, 2]
有效地继承了 stdin
、stdout
和 stderr
。
在 package.json.
中内联您的 JavaScript
或者,您可以在 package.json.
的 scripts
部分内联 nodejs/JavaScript 代码
在 package.json 的 scripts
部分中,将当前的 build
脚本替换为以下内容:
package.json
"scripts": {
"build": "node -e \"process.env.UUID = require('uuid/v1')(); require('child_process').execSync('ng build', { stdio: [0, 1, 2]} );\""
}
解释:
- 这实际上与上述使用单独
.js
文件的解决方案相同,但是现在使用单独的 nodejs script/file 是多余的。
- nodejs 命令行选项
-e
用于评估内联JavaScript。
重要 cross-env 包使用上述两种解决方案中的任何一种都是多余的。要卸载它 运行:npm un -D cross-env
通过 CLI。
解决方案 B:*仅限 Nix 平台(Linux/MacOS...)
仅适用于 *nix 平台,它变得非常简洁,您可以在 package.json 中定义您的 build
脚本,如下所示:
package.json
"scripts": {
"build": "cross-env UUID=$(uuid) ng build"
}
这使用了一种称为 command substitution, i.e. $(uuid)
. However, if *nix is the only platform you need to support, then cross-env
is really not necessary. Use the built-in export 的 Bash 功能。例如:
package.json
"scripts": {
"build": "export UUID=$(uuid) && ng build"
}
解决方案 C:仅 Windows (cmd.exe)
On Windows(仅)运行通过命令提示符或 PowerShell,您可以执行以下操作:
package.json
"scripts": {
"build": "FOR /F %U IN ('uuid') DO cross-env UUID=%~U node -e \"process.env.UUID = require('uuid/v1')(); require('child_process').execSync('ng buuld', { stdio: [0, 1, 2] });\""
}
这类似于 解决方案 B 中显示的第一个示例,但是实现命令替换的方式 (非常) 与 cmd.exe。请参阅此 answer 以获得进一步的解释。
我想在调用我的 npm 脚本时添加一个 UUID 参数。每次调用脚本时,我都想生成一个新号码。它应该看起来像:
"build": "cross-env UUID=unique_number ng build"
我唯一需要的就是在此处生成 unique_number
。我尝试使用 uuid 包,但我不知道如何在脚本中触发它并将数字作为参数传递。
tl;dr 由于您的问题显示了 cross-var
的使用,我假设需要一个跨平台解决方案。在这种情况下,请参考 解决方案 A。但是,如果我的假设不正确,请参考 解决方案 B 或 C。
解决方案 A:跨平台(Windows/Linux/macOS...)
Fo 跨平台解决方案(即 运行 在 Windows、Linux 和 macOS 上成功...),您需要利用 nodejs 来达到你的要求。有几种不同的方法可以解决这个问题,如以下两个小节所述:
- 使用外部 nodejs (.js) 文件
- 在 package.json 中内联您的 JavaScript。
注意两种方法实际上是相同的
使用外部 nodejs (.js) 文件
创建一个 nodejs 实用程序脚本。让我们将文件命名为 运行-ng-build.js 并将其保存在项目目录的根目录中,即与 package.json 目前居住:
运行-ng-build.js
const uuid = require('uuid/v1'); const execSync = require('child_process').execSync; process.env.UUID = uuid(); execSync('ng build', { stdio: [0, 1, 2]} );
在 package.json 的
scripts
部分用以下内容替换当前的build
脚本:package.json
"scripts": { "build": "node run-ng-build" }
解释:
- 在运行-ng-build.js中我们需要uuid package and the nodejs built-in
execSync()
. - 要创建名为
UUID
的环境变量,我们使用 nodejs 内置process.env
,并通过调用uuid()
为其分配 uuid 值]. - 然后我们使用
execSync
调用ng build
命令。 options.stdio
选项配置父进程和子进程之间的管道 -[0, 1, 2]
有效地继承了stdin
、stdout
和stderr
。
在 package.json.
中内联您的 JavaScript或者,您可以在 package.json.
的scripts
部分内联 nodejs/JavaScript 代码
在 package.json 的
scripts
部分中,将当前的build
脚本替换为以下内容:package.json
"scripts": { "build": "node -e \"process.env.UUID = require('uuid/v1')(); require('child_process').execSync('ng build', { stdio: [0, 1, 2]} );\"" }
解释:
- 这实际上与上述使用单独
.js
文件的解决方案相同,但是现在使用单独的 nodejs script/file 是多余的。 - nodejs 命令行选项
-e
用于评估内联JavaScript。
重要 cross-env 包使用上述两种解决方案中的任何一种都是多余的。要卸载它 运行:npm un -D cross-env
通过 CLI。
解决方案 B:*仅限 Nix 平台(Linux/MacOS...)
仅适用于 *nix 平台,它变得非常简洁,您可以在 package.json 中定义您的 build
脚本,如下所示:
package.json
"scripts": {
"build": "cross-env UUID=$(uuid) ng build"
}
这使用了一种称为 command substitution, i.e. $(uuid)
. However, if *nix is the only platform you need to support, then cross-env
is really not necessary. Use the built-in export 的 Bash 功能。例如:
package.json
"scripts": {
"build": "export UUID=$(uuid) && ng build"
}
解决方案 C:仅 Windows (cmd.exe)
On Windows(仅)运行通过命令提示符或 PowerShell,您可以执行以下操作:
package.json
"scripts": {
"build": "FOR /F %U IN ('uuid') DO cross-env UUID=%~U node -e \"process.env.UUID = require('uuid/v1')(); require('child_process').execSync('ng buuld', { stdio: [0, 1, 2] });\""
}
这类似于 解决方案 B 中显示的第一个示例,但是实现命令替换的方式 (非常) 与 cmd.exe。请参阅此 answer 以获得进一步的解释。