版本标签
语意化版本标签按照下面的格式进行规范
MAJOR.MINOR.PATCH
其中
MAJOR
表示主版本号,通常当提交了BREAKING CHANGE
时会更新MINOR
表示次版本号,通常当提交了feat
时会更新PATCH
表示补丁版本号,通常当提交了fix
时会更新
standard-version
standard-version 是一个遵循语义化版本规则(Semantic Versioning)的npm包工具,能够自动管理版本号并生成清晰的版本日志。
-
目的:帮助开发者和用户更好地了解软件版本之间的差异和兼容性。这种规范为软件版本赋予了明确的意义,使得版本升级和依赖管理变得更加简单和清晰
-
下载
npm i --save-dev standard-version
或者
npm install -g standard-version
说明:standard-version
是依据conventional commit
,因此需要确保提交的说明符合规范
- 配置和使用
- 在一个新建的库中,我们需要在
package.json
中添加一段脚本
"scripts": {
"release": "standard-version"
}
然后尝试提交
git add .
npm run commit
这之后会提供一些选项进行选择和填入,然后推送到仓库,合并后执行npm run release
即可生成版本号和git tag
将更改和tag推送到远端:git push --follow-tags origin master
新建一个文件为.versionrc
,然后填入
// .versionrc
{
"types": [
{ "type": "chore", "section": "Others", "hidden": false },
{ "type": "revert", "section": "Reverts", "hidden": false },
{ "type": "feat", "section": "Features", "hidden": false },
{ "type": "fix", "section": "Bug Fixes", "hidden": false },
{ "type": "improvement", "section": "Feature Improvements", "hidden": false },
{ "type": "docs", "section": "Docs", "hidden": false },
{ "type": "style", "section": "Styling", "hidden": false },
{ "type": "refactor", "section": "Code Refactoring", "hidden": false },
{ "type": "perf", "section": "Performance Improvements", "hidden": false },
{ "type": "test", "section": "Tests", "hidden": false },
{ "type": "build", "section": "Build System", "hidden": false },
{ "type": "ci", "section": "CI", "hidden": false }
]
}
以上为示例,实际情况要根据自己的需求来看,或者当你clone了一个项目后,查看项目的设置来遵循项目维护者的规范
例如,在项目的package.json
的脚本中看到了format
,这意味着你可以通过执行npm run format
对你的代码/文档进行格式化
Warning
要慎用,要遵循单次PR尽量小改动的原则
对于clone的项目,修改了项目的代码/文档后,在本地进行提交,然后运行standard-version
,会弹出如下说明
✔ bumping version in package.json from 0.1.0 to 0.1.1
✔ bumping version in package-lock.json from 0.1.0 to 0.1.1
✔ outputting changes to CHANGELOG.md
✔ committing package-lock.json and package.json and CHANGELOG.md
✔ tagging release v0.1.1
然后可以运行git push --follow-tags origin your_branch
推送到远程仓库
发布版本
除了基本的基于fix
、feat
、BREAKING CHANGE
的版本外,还有预发布版本、特定类型的预发布版本
npm run release -- --prerelease # 预发布版本
npm run release -- --prerelease beta #特定类型的预发布版本
Warning
跳过某些步骤,跳过的选项为
--skip
,如跳过提交和版本日志生成npm run release -- --skip.commit --skip.changelog
注意事项
-
standard-version
的两种下载方式是不同的,npm install -g standard-version
为全局下载,会将命令添加到环境变量,而npm i --save-dev standard-version
只安装到当前项目 -
.versionrc
不是必要的,可以根据自身需求选择不同的配置文件 -
版本迭代并不是完全取决于commit自动生成,即可以手动配置,例如:手动升级
major
的命令为standard-version --release-as major
,或者强制打一个静态版本号standard-version --release-as 6.6.6