简体   繁体   English

如何在 NPM 脚本中传递 package.json 脚本中的命令行变量?

[英]How to pass command-line variable in package.json script, in NPM?

Pass command line args to npm scripts in package.json is almost what I'm looking for. 将命令行参数传递给 package.json 中的 npm 脚本几乎就是我要找的。

I use Gulp to do our NPM builds.我使用 Gulp 进行 NPM 构建。 I am able to do this, using the yargs plugin我能够做到这一点,使用yargs 插件

$ gulp build --gitTag 1.0.0

and it produces dist/packageName-1.0.0.zip file.它产生dist/packageName-1.0.0.zip文件。 However, I need to be able to do this但是,我需要能够做到这一点

$ npm run build --gitTag=1.0.0

I tried this,我试过这个,

  "scripts": {
    "build": "npm ci && gulp build --gitTag %npm_config_gitTag%"
  }

...and this ...和这个

  "scripts": {
    "build": "export GIT_TAG=%npm_config_gitTag% && npm ci && gulp build"
  }

However, the %npm_config_gitTag% is not substituted with my passed-in gitTag argument, meaning the resulting artifact is packageName-%npm_config_gitTag%.zip但是, %npm_config_gitTag%没有被我传入的gitTag参数替换,这意味着生成的工件是packageName-%npm_config_gitTag%.zip

What should my build script look like in my package.json file?我的package.json文件中的build脚本应该是什么样子?

I found the magic recipe我找到了神奇的食谱

I simply have this in my package.json我只是在我的package.json

  "scripts": {
    "build": "npm ci && gulp build"
  }

and use the following command line并使用以下命令行

$ npm run build -- --gitTag 1.0.0

and get the wanted packageName-1.0.0.zip artifact.并获得想要的packageName-1.0.0.zip工件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 package.json,用于最小的命令行实用程序 - package.json for minimal command-line utility 使用bash脚本将参数从npm命令行传递到package.json - Issues passing arguments from npm command line to package.json with a bash script 将命令行参数或标志传递给 NPM package.json 脚本 - Passing a command line argument or flag to NPM package.json scripts 在没有 yarn/npm 命令的情况下使用 package.json 脚本 - Use package.json script without yarn/npm command 运行npm脚本命令时覆盖package.json键 - Overriding package.json keys while running npm script command 如何从package.json在Windows命令行(bash,babun)中运行2个npm命令? - How to run 2 npm commands in Windows command line (bash, babun) from package.json? 如何在 Nodejs package.json 中配置“npm run-script build”命令 - how to configure “npm run-script build” command in Nodejs package.json npm 构建如何更改 package.json - 通过变量或脚本的主页值 - npm build how to change package.json - homepage value by variable or script 将命令行变量传递给npm脚本? - Pass command line variable into npm script? 将多个变量传递给 package.json 脚本 - Pass more than one variable to a package.json script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM