node.js 自动更新 package.json 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13059991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
update package.json version automatically
提问by tUrG0n
Before I do a small release and tag it, I'd like to update the package.json to reflect the new version of the program.
在我做一个小版本并标记它之前,我想更新 package.json 以反映程序的新版本。
Is there a way to edit the file package.jsonautomatically?
有没有办法package.json自动编辑文件?
Would using a git pre-release hookhelp?
会使用git pre-release hook帮助吗?
采纳答案by zemirco
npm versionis probably the correct answer. Just to give an alternative I recommend grunt-bump. It is maintained by one of the guys from angular.js.
npm version可能是正确的答案。只是为了提供一个替代方案,我推荐grunt-bump。它由 angular.js 的一名人员维护。
Usage:
用法:
grunt bump
>> Version bumped to 0.0.2
grunt bump:patch
>> Version bumped to 0.0.3
grunt bump:minor
>> Version bumped to 0.1.0
grunt bump
>> Version bumped to 0.1.1
grunt bump:major
>> Version bumped to 1.0.0
If you're using grunt anyway it might be the simplest solution.
如果您无论如何都在使用 grunt,它可能是最简单的解决方案。
回答by gustavotkg
Right answer
正确答案
To do so, just npm version patch=)
为此,只需npm version patch=)
My old answer
我的旧答案
There is no pre-releasehook originally in git. At least, man githooksdoes not show it.
中pre-release最初没有钩子git。至少,man githooks没有表现出来。
If you're using git-extra(https://github.com/visionmedia/git-extras), for instance, you can use a pre-releasehook which is implemented by it, as you can see at https://github.com/visionmedia/git-extras/blob/master/bin/git-release. It is needed only a .git/hook/pre-release.shexecutable file which edits your package.jsonfile. Committing, pushing and tagging will be done by the git releasecommand.
例如,如果您使用git-extra(https://github.com/visionmedia/git-extras),您可以使用pre-release由它实现的钩子,如您所见:https://github.com/visionmedia/ git-extras/blob/master/bin/git-release。只需要一个.git/hook/pre-release.sh可以编辑package.json文件的可执行文件。提交、推送和标记将由git release命令完成。
If you're not using any extension for git, you can write a shell script (I'll name it git-release.sh) and than you can alias it to git releasewith something like:
如果您没有为 使用任何扩展名git,您可以编写一个 shell 脚本(我将其命名为git-release.sh),然后您可以将其别名为git release:
git config --global alias.release '!sh path/to/pre-release.sh $1'
git config --global alias.release '!sh path/to/pre-release.sh $1'
You can, than, use git release 0.4which will execute path/to/pre-release.sh 0.4. Your script can edit package.json, create the tag and push it to the server.
您可以使用git release 0.4which 将执行path/to/pre-release.sh 0.4. 您的脚本可以编辑package.json、创建标签并将其推送到服务器。
回答by Merc
This is what I normally do with my projects:
这是我通常对我的项目所做的:
npm version patch
git add *;
git commit -m "Commit message"
git push
npm publish
The first line, npm version patch, will increase the patch version by 1 (x.x.1 to x.x.2) in package.json. Then you add all files -- including package.jsonwhich at that point has been modified.
Then, the usual git commitand git push, and finally npm publishto publish the module.
第一行,npm version patch将补丁版本增加 1(xx1 到 xx2)package.json。然后添加所有文件——包括package.json当时已修改的文件。然后,通常的git commitand git push,最后npm publish发布模块。
I hope this makes sense...
我希望这是有道理的...
Merc.
梅尔。
回答by Jonatas Walker
To give a more up-to-date approach.
提供一个更新的方法。
package.json
package.json
"scripts": {
"eslint": "eslint index.js",
"pretest": "npm install",
"test": "npm run eslint",
"preversion": "npm run test",
"version": "",
"postversion": "git push && git push --tags && npm publish"
}
Then you run it:
然后你运行它:
npm version minor --force -m "Some message to commit"
Which will:
这将:
... run tests ...
change your
package.jsonto a next minor version (e.g: 1.8.1 to 1.9.0)push your changes
create a new git tag release and
publish your npm package.
... 运行测试 ...
将您更改
package.json为下一个次要版本(例如:1.8.1 到 1.9.0)推动你的改变
创建一个新的 git 标签版本和
发布你的 npm 包。
--forceis to show who is the boss! Jokes aside see https://github.com/npm/npm/issues/8620
--force就是显示谁是老大!除了笑话,请参阅https://github.com/npm/npm/issues/8620
回答by Tieme
As an addition to npm versionyou can use the --no-git-tag-versionflag if you want a version bump but no tag or a new commit:
作为补充,如果您想要版本提升但没有标签或新提交,则npm version可以使用该--no-git-tag-version标志:
npm --no-git-tag-version version patch
回答by Eric Kim
If you are using yarn you can use
如果您使用纱线,您可以使用
yarn version --patch
This will increment package.jsonversion by patch (0.0.x), commit, and tag it with format v0.0.0
这将package.json通过补丁(0.0.x)、提交和使用格式标记它来增加版本v0.0.0
Likewise you can bump minor or major version by using --minoror --major
同样,您可以通过使用--minor或--major
When pushing to git ensure you also push the tags with --follow-tags
推送到 git 时,请确保您也推送标签 --follow-tags
git push --follow-tags
You can also create a script for it
你也可以为它创建一个脚本
"release-it": "yarn version --patch && git push --follow-tags"
Simply run it by typing yarn release-it
只需键入即可运行它 yarn release-it
回答by Anima-t3d
I am using huskyand git-branch-is:
我正在使用husky和git-branch-is:
As of husky v1+:
从 husky v1+ 开始:
// package.json
{
"husky": {
"hooks": {
"post-merge": "(git-branch-is master && npm version minor ||
(git-branch-is dev && npm --no-git-tag-version version patch)",
}
}
}
Prior to husky V1:
在哈士奇 V1 之前:
"scripts": {
...
"postmerge": "(git-branch-is master && npm version minor ||
(git-branch-is dev && npm --no-git-tag-version version patch)",
...
},
Read more about npm version
阅读有关npm 版本的更多信息
Webpack or Vue.js
Webpack 或 Vue.js
If you are using webpack or Vue.js, you can display this in the UI using Auto inject version - Webpack plugin
如果您使用的是 webpack 或 Vue.js,您可以使用Auto injection version - Webpack plugin在 UI 中显示它
NUXT
NUXT
In nuxt.config.js:
在nuxt.config.js:
var WebpackAutoInject = require('webpack-auto-inject-version');
module.exports = {
build: {
plugins: [
new WebpackAutoInject({
// options
// example:
components: {
InjectAsComment: false
},
}),
]
},
}
Inside your templatefor example in the footer:
在您template的例如页脚中:
<p> All rights reserved ? 2018 [v[AIV]{version}[/AIV]]</p>
回答by Alejandro Vales
I want to add some clarity to the answers this question got.
我想为这个问题得到的答案增加一些清晰度。
Even thought there are some answers here that are tackling properly the problem and providing a solution, they are not the correct ones. The correct answer to this question is to use npm version
即使认为这里有一些答案可以正确解决问题并提供解决方案,但它们并不是正确的。这个问题的正确答案是使用npm version
Is there a way to edit the file package.json automatically?
有没有办法自动编辑文件 package.json ?
Yes, what you can do to make this happen is to run the npm versioncommand when needed, you can read more about it here npm version, but the base usage would be npm version patchand it would add the 3rd digit order on your package.jsonversion (1.0.X)
是的,您可以做的是npm version在需要时运行该命令,您可以在此处阅读有关它的更多信息npm version,但基本用法是npm version patch,它会在您的package.json版本 (1.0. X)上添加第 3 位数字顺序
Would using a git pre-release hook help?
使用 git pre-release hook 会有帮助吗?
You could configure to run the npm versioncommand on the pre-release hook, as you need, but that depends if that is what you need or not in your CD/CI pipe, but without the npm versioncommand a git pre-releasehook can't do anything "easily" with the package.json
您可以根据需要配置npm version在预发布挂钩上运行该命令,但这取决于您在 CD/CI 管道中是否需要该npm version命令,但是如果没有该命令,git pre-release挂钩将无法“轻松”执行任何操作与package.json
The reason why npm versionis the correct answer is the following:
npm version正确答案的原因如下:
- If the user is using a folder structure in which he has a
package.jsonhe is usingnpmif he is usingnpmhe has access to thenpm scripts. - If he has access to
npm scriptshe has access to thenpm versioncommand. - Using this command he doesn't need to install anything more in his computer or CD/CI pipe which on the long term will reduce the maintainability effort for the project, and will help with the setup
- 如果用户正在使用的文件夹结构,其中他有
package.json他用npm,如果他是用npm他访问npm scripts。 - 如果他有权访问,
npm scripts他就可以访问npm version命令。 - 使用这个命令,他不需要在他的计算机或 CD/CI 管道中安装任何东西,从长远来看,这将减少项目的可维护性工作,并有助于设置
The other answers in which other tools are proposed are incorrect.
提出其他工具的其他答案是不正确的。
gulp-bumpworks but requires another extra package which could create issues in the long term (point 3 of my answer)
gulp-bump有效,但需要另一个额外的包,从长远来看可能会产生问题(我的答案的第 3 点)
grunt-bumpworks but requires another extra package which could create issues in the long term (point 3 of my answer)
grunt-bump有效,但需要另一个额外的包,从长远来看可能会产生问题(我的答案的第 3 点)
回答by Mnemo
First, you need to understand the rules for upgrading the versioning number. You can read more about the semantic versionhere.
首先,您需要了解升级版本号的规则。您可以在此处阅读有关语义版本的更多信息。
Each version will have x.y.z version where it defines for different purpose as shown below.
每个版本都有 xyz 版本,它为不同的目的定义,如下所示。
- x - major, up this when you have major changes and it is huge discrepancy of changes occurred.
- y - minor, up this when you have new functionality or enhancement occurred.
- z - patch, up this when you have bugs fixed or revert changes on earlier version.
- x - 主要,当您发生重大变化并且发生巨大的变化差异时。
- y - 次要,当您有新功能或增强功能时增加。
- z - 修补程序,当您修复了错误或恢复了早期版本的更改时进行了修补。
To run the scripts, you can define it in your package.json.
要运行脚本,您可以在 package.json 中定义它。
"script": {
"buildmajor": "npm version major && ng build --prod",
"buildminor": "npm version minor && ng build --prod",
"buildpatch": "npm version patch && ng build --prod"
}
In your terminal, you just need to npm run accordingly to your needs like
在您的终端中,您只需要根据您的需要运行 npm,例如
npm run buildpatch
If run it in git repo, the default git-tag-version is true and if you do not wish to do so, you can add below command into your scripts:
如果在 git repo 中运行它,默认的 git-tag-version 为 true,如果您不想这样做,您可以将以下命令添加到您的脚本中:
--no-git-tag-version
for eg: "npm --no-git-tag-version version major && ng build --prod"
例如: "npm --no-git-tag-version version major && ng build --prod"
回答by Daniel Eagle
I have created a toolthat can accomplish automatic semantic versioning based on the tags in commit messages, known as change types. This closely follows the Angular Commit Message Convention along with the Semantic Versioning Specification.
我创建了一个工具,可以根据提交消息中的标签(称为更改类型)完成自动语义版本控制。这与 Angular Commit Message 约定以及语义版本规范密切相关。
You could use this tool to automatically change the version in the package.json using the npm CLI (this is described here).
您可以使用此工具使用 npm CLI 自动更改 package.json 中的版本(此处描述)。
In addition, it can create a changelog from these commits and also has a menu (with a spell checker for commit messages) for creating commits based on the change type. I highly recommend checking it out and reading to docs to see everything that can be accomplished with it.
此外,它可以从这些提交创建变更日志,并且还有一个菜单(带有提交消息的拼写检查器)用于根据变更类型创建提交。我强烈建议检查它并阅读文档以查看可以使用它完成的所有内容。
I wrote the tool because I couldn't find anything that suited my needs for my CICD Pipeline to automate semantic versioning. I'd rather focus on what the actual changes are than what the version should be and that's where my tool saves the day.
我编写这个工具是因为我找不到任何适合我的 CICD Pipeline 自动化语义版本控制的需求。我宁愿关注实际的变化是什么,而不是版本应该是什么,这就是我的工具可以节省一天的地方。
For more information on the rationale for the tool, please see this.
有关该工具的基本原理的更多信息,请参阅此。

