npm 运行:如何在脚本调用中传递参数并用参数替换占位符
npm run: how to pass parameter and replace placeholder by param in script call
我想在我的 package.json 中定义一个 scripts
条目以简化多个环境的构建。
在脚本执行中,我需要用我将传递给 npm run build-script
的参数替换 </code>(或占位符所需的任何语法),例如 <code>--env=prod
甚至更简单,--prod
。我怎样才能做到这一点?我在这里找到的其他问题和答案并没有帮助我解决问题。
"scripts": {
"build-for": "ng build --output-path=../../web/admin-v2 --env= --base-href=\"/admin-v2/\""
}
我经常为这种情况创建一个实用程序节点脚本,并通过 package.json
的 scripts
部分调用它。
构建-for.js
var nodeCLI = require('shelljs-nodecli');
var env = '--env=foo'; // <-- Default env flag/value when no arg provided.
if (process.argv.indexOf('--prod') !== -1) {
env = '--env=prod';
}
// Check for other possible env values
if (process.argv.indexOf('--quux') !== -1) {
env = '--env=quux';
}
// Run the ng build command
nodeCLI.exec('ng', 'build', '--output-path=../../web/admin-v2', env, '--base-href=\"/admin-v2/\"');
build-for.js
利用节点 process.argv to ascertain the argument/flag passed via the CLI and then invokes the ng
command (the one currently defined in your package.json
) using shelljs-nodecli.
npm i -D shelljs-nodecli
假设 build-for.js
保存在项目根目录中名为 .scripts
的隐藏文件夹中;那么 package.json
的 scripts
部分将定义如下:
package.json
{
...
"scripts": {
"build-for": "node ./.scripts/build-for"
},
...
}
运行脚本
通过运行调用npm脚本:
npm run build-for -- --prod
请注意参数 --prod
之前的特殊 --
必须按照说明包含在内 here
As of npm@2.0.0, you can use custom arguments when executing scripts. 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:
鉴于 build-for.js
中当前的逻辑 - 当没有传递任何参数时,例如:
npm run build-for
...env
标志将设置为 --env=foo
运行 以下:
npm run build-for -- --quux
...将导致 env
标志将设置为 --env=quux
警告
我还没有完全测试build-for.js
,所以你可能会发现你不需要转义下面命令'--base-href=\"/admin-v2/\"'
这部分的双引号 (nodeCLI
可以为您处理。):
// Run the ng build command
nodeCLI.exec('ng', 'build', '--output-path=../../web/admin-v2', env, '--base-href=\"/admin-v2/\"');
不完全是您要查找的内容,但您可以使用环境变量并内联提供它们:
package.json 脚本:
"<script>": "echo ${ENV1} ${ENV2}"
运行 像这样:
ENV1=a ENV2=b npm run <script>
$ npm run <script>
a b
我想在我的 package.json 中定义一个 scripts
条目以简化多个环境的构建。
在脚本执行中,我需要用我将传递给 npm run build-script
的参数替换 </code>(或占位符所需的任何语法),例如 <code>--env=prod
甚至更简单,--prod
。我怎样才能做到这一点?我在这里找到的其他问题和答案并没有帮助我解决问题。
"scripts": {
"build-for": "ng build --output-path=../../web/admin-v2 --env= --base-href=\"/admin-v2/\""
}
我经常为这种情况创建一个实用程序节点脚本,并通过 package.json
的 scripts
部分调用它。
构建-for.js
var nodeCLI = require('shelljs-nodecli');
var env = '--env=foo'; // <-- Default env flag/value when no arg provided.
if (process.argv.indexOf('--prod') !== -1) {
env = '--env=prod';
}
// Check for other possible env values
if (process.argv.indexOf('--quux') !== -1) {
env = '--env=quux';
}
// Run the ng build command
nodeCLI.exec('ng', 'build', '--output-path=../../web/admin-v2', env, '--base-href=\"/admin-v2/\"');
build-for.js
利用节点 process.argv to ascertain the argument/flag passed via the CLI and then invokes the ng
command (the one currently defined in your package.json
) using shelljs-nodecli.
npm i -D shelljs-nodecli
假设 build-for.js
保存在项目根目录中名为 .scripts
的隐藏文件夹中;那么 package.json
的 scripts
部分将定义如下:
package.json
{
...
"scripts": {
"build-for": "node ./.scripts/build-for"
},
...
}
运行脚本
通过运行调用npm脚本:
npm run build-for -- --prod
请注意参数 --prod
之前的特殊 --
必须按照说明包含在内 here
As of npm@2.0.0, you can use custom arguments when executing scripts. 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:
鉴于 build-for.js
中当前的逻辑 - 当没有传递任何参数时,例如:
npm run build-for
...env
标志将设置为 --env=foo
运行 以下:
npm run build-for -- --quux
...将导致 env
标志将设置为 --env=quux
警告
我还没有完全测试build-for.js
,所以你可能会发现你不需要转义下面命令'--base-href=\"/admin-v2/\"'
这部分的双引号 (nodeCLI
可以为您处理。):
// Run the ng build command
nodeCLI.exec('ng', 'build', '--output-path=../../web/admin-v2', env, '--base-href=\"/admin-v2/\"');
不完全是您要查找的内容,但您可以使用环境变量并内联提供它们:
package.json 脚本:
"<script>": "echo ${ENV1} ${ENV2}"
运行 像这样:
ENV1=a ENV2=b npm run <script>
$ npm run <script>
a b