本文為譯文,原為為:Christian Ing Sunardi: Automate JavaScript project versioning with commitizen and standard-version
其實(shí)手動(dòng)更新版本號(hào),changelog, 創(chuàng)建 git tags
還是比較麻煩的。有沒(méi)有更好的方法?別怕,使用 standard-version ,只需要一行命令,就會(huì)幫你搞定一切!為實(shí)現(xiàn)自動(dòng)化版本控制,有一些基礎(chǔ)配置需要做。
安裝 commitizen
在使用 standard-version
之前,需要遵循 Conventional Commit Specifications 來(lái)進(jìn)行標(biāo)準(zhǔn)化的 commit message 編寫(xiě)。這是因?yàn)?standard-version
是基于 commit 類(lèi)型來(lái)更新版本號(hào)的(feature 會(huì)更新 minor, bug fix 會(huì)更新 patch, BREAKING CHANGES 會(huì)更新 major)。commitizen 可以幫助我們提交符合 Conventional Commit Specifications 的 commit message。
- 在你的 JavaScript 項(xiàng)目中安裝 commitizen:
npm install -D commitizen
- 設(shè)置 changelog adapter:
commitizen init cz-conventional-changelog --save-dev --save-exact
- 在 package.json,中增加如下腳本:
"scripts": {
"commit" : "git-cz"
}
首先我們 git add .
文件,然后 npm run commit
,此時(shí) commitizen 會(huì)通過(guò) CLI 對(duì)我們進(jìn)行詢問(wèn):
選擇提交類(lèi)型后,會(huì)需要我們填寫(xiě)詳細(xì)信息:
至此,我們的修改已經(jīng)被成功提交,可以開(kāi)始配置 standard-version 了。
安裝 standard-version
當(dāng)我們使用 commitizen 進(jìn)行標(biāo)準(zhǔn)化提交之后,我們就可以使用 standard-version 進(jìn)行版本管理自動(dòng)化了,包括更新 CHANGELOG.md,以及使用 git tag
。
- 安裝 standard-version:
npm install -D standard-version
- 在 package.json 中編寫(xiě)響應(yīng)的腳本:
"scripts": {
"commit" : "git-cz",
"release": "standard-version"
}
為了合理的使用 standard-version,我們首先需要 git add .
文件,然后執(zhí)行 npm run commit
,最后執(zhí)行 npm run release
。
CHANGELOG.md 配置
默認(rèn)情況下,standard-version 只會(huì)在 CHANGELOG.md 中記錄 feat
和 fix
類(lèi)型的提交。如果想記錄其他類(lèi)型的提交,需要如下步驟:
- 在項(xiàng)目的根目錄下創(chuàng)建一個(gè)名為
.versionrc
的文件,并粘貼復(fù)制一下內(nèi)容:
// .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}
]
}
-
"type"
commit 類(lèi)型 -
"section"
不同的 commit 類(lèi)型所在 CHANGELOG.md 中的區(qū)域 -
"hidden"
是否在 CHANGELOG.md 中顯示
開(kāi)發(fā)工作流
截至目前,所有的配置已經(jīng)設(shè)置完畢。讓我們用一個(gè)真實(shí)的開(kāi)發(fā)工作流來(lái)梳理一下 *commitizen * 與 standard-version 的使用。
- [feature-branch] stage 更改的文件:
git add .
- [feature-branch] 用
git-cz
來(lái)提交文件:
npm run commit
- 選擇提交類(lèi)型(feat, refactor, fix, 等等)
- 提供簡(jiǎn)短描述
- (可選)提供長(zhǎng)描述
- 確定本次提交是否為 BREAKING CHANGES
- (可選)提供 JIRA issue
- [feature-branch] 推送遠(yuǎn)端
git push origin <feature-branch>
- [Bitbucket] 向
master
分支提交 Pull Request - [master] 成功合并后:
- 執(zhí)行命令
npm run release
(自動(dòng)更新版本號(hào),自動(dòng)更新 CHANGELOG.md, 自動(dòng)創(chuàng)建git tag
) - 將更改和
git tags
推送至遠(yuǎn)端:
git push --follow-tags origin master
- 執(zhí)行命令
- Relax and enjoy ??
總結(jié)
使用以上流程進(jìn)行版本發(fā)布是一種非常好的最佳實(shí)踐。因?yàn)槲覀兛梢苑浅]p松的追溯雖有的更新。并且一切都是自動(dòng)化的。
常見(jiàn)坑??
- 如果當(dāng)前版本沒(méi)有主版本號(hào),則
BREAKING CHANGES
commit 不會(huì)更新主版本號(hào)!!!比如當(dāng)前版本號(hào)為0.1.2
,我們運(yùn)行npm run release
則只會(huì)更新minor
和patch
版本號(hào)(參考:https://github.com/conventional-changelog/standard-version/issues/429
)。 - 為了正確的更新主版本號(hào),我們需要顯式的進(jìn)行:
standard-version -- --release-as major
注意: standard-version
后的 雙橫線 --
是必須的。在 --release-as
和 major
之間是有空格的。
- 此后,我們?cè)俅伟l(fā)布包含
BREAKING CHANGES
類(lèi)型的提交,則能夠自動(dòng)更新主版本號(hào)????。 - 如果有多次提交,則版本號(hào)更新是有優(yōu)先級(jí)的:
- major 或者
BREAKING CHANGES
高于minor
和patch
-
minor
高于patch
- major 或者