如何在npm脚本中引用软件包版本? [英] How can I reference package version in npm script?

查看:141
本文介绍了如何在npm脚本中引用软件包版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在npm脚本中引用我的软件包版本,以便可以在应用程序中显示当前版本. 像

I want to reference my package version in a npm script so I can show current version in the app. Something like

{
  "name": "bla",
  "version": "1.0.0",
  "author": "bla bla",
  "scripts": {
    "build": "node VERSION=<<package.version>> build/build.js"
  }
}

有没有办法做到这一点?

Is there a way to do this?

推荐答案

1)在npm-scripts中引用程序包版本.

npm-script中,可以使用变量npm_package_version引用version.例如:

1) Referencing package version in npm-scripts.

In npm-script's you can reference the version using the variable npm_package_version. For example:

  • 使用bash shell(例如Linux,macOS):

{
  ...
  "version": "1.0.0",
  "scripts": {
    "build": "echo $npm_package_version"
  }
}

注意$前缀

使用Windows(例如cmd.exe,Powershell):

{
  ...
  "version": "1.0.0",
  "scripts": {
    "build": "echo %npm_package_version%"
  }
}

注意%前缀和后缀

Note the % prefix and suffix

跨平台

要使用一种语法跨平台,请检查软件包 cross-var

To utilize one syntax cross-platform check out the package cross-var

还可以在您的应用程序/节点脚本(即build.js)中引用程序包版本,如下所示:

The package version can also be referenced in your a app/node script (i.e. build.js) as follows:

const VERSION = process.env.npm_package_version;
console.log(VERSION); // --> 1.0.0


3)用程序包版本替换.js文件中的占位符字符串.

另一种实现此目的的方法是在JavaScript文件中指定一个占位符文本字符串.假设我们有一个名为build.js的文件,并且在该文件中有一个名为VERSION的变量,声明如下:


3) Replacing a placeholder string in a .js file with package version.

Another way to achieve this is to specify a placeholder text string within your JavaScript file. Lets say we have a file named build.js and within that file we have a variable named VERSION declared as follows:

// build.js
const VERSION = '@VERSION@'

如您所见,占位符文本字符串为@VERSION@.

As you can see, the placeholder text string is @VERSION@.

然后,您可以在替换的软件包. /docs.npmjs.com/misc/scripts"rel =" noreferrer> npm-script 如下:

You can then install and utilize the package called replace in an npm-script as follows:

{
  ...
  "version": "1.0.0",
  "scripts": {
    "add-version":  "replace -s \"@VERSION@\" $npm_package_version build/build.js"
  }
}

运行npm run add-version会将@VERSION@的实例替换为名为build.js的文件中的软件包版本(即1.0.0).此解决方案会将npm软件包版本硬编码到结果文件中.

Running npm run add-version will replace the instance of @VERSION@ with the package version (i.e. 1.0.0), in the file named build.js. This solution will hard-code the npm package version into the resultant file.

注意:add-version脚本(如上)中的 to 字符串当前使用$前缀(即$npm_package_version)访问变量,因此只能在bash外壳.但是,对于跨平台的使用,您将需要使用cross-var,如上文第一节所述.在这种情况下,可以如下定义add-version脚本:

Note: The to string in the add-version script (above) currently uses the $ prefix (i.e. $npm_package_version) to access the variable, so this will only run successfully on a bash shell. However, for cross-platform usage you'll need to use cross-var as explained in section one (above). In which case the add-version script can be defined as follows:

{
  ...
  "version": "1.0.0",
  "scripts": {
    "add-version":  "cross-var replace -s \"@VERSION@\" $npm_package_version build/build.js"
  }
}

这篇关于如何在npm脚本中引用软件包版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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