[note] git conventional commit

過去曾經整理過如何透過 semantic-release 這套工具整合 CI/CD 來達到自動化更新套件版號、產生 CHANGELOG 檔,並發佈到 npm 的流程。
當時雖然有提到 conventional commit 這個撰寫 commit message 的規範(convention),但還沒有實際透過一些好用的工具來建立 commit message 和針對 commit message 進行檢查。
在這篇文章中就來介紹幾套用來建立符合 conventional commit 的好用工具:
- 透過 commitlint 進行 commit message 的檢查(lint)
- 搭配 husky 在建立 commit message 前就自動執行 commitlint
- 透過 commitizen 方便開發者建立符合 conventional commit 的 commit message
- 使用 conventional-changelog 根據 commit message 來產生 CHANGELOG 檔
- 使用 standard version 來同時更新版本號和產生 CHANGELOG 檔
如果是對於 CI/CD 中如何自動更新版號的部分,則可以參考過去寫的系列文章:發佈 npm 套件 - 從手動到自動。
commitlint:檢查 commit message
commitlint 這套工具是用來作為 git commit 的 linter,並且可以搭配不同的 convention。
這裡選擇 config-conventional,也就是需要依據 conventional commit 的規範來寫 commit message:
@commitlint/cli是用來執行 commitlint 的工具@commitlint/config-conventional是根據 conventional commit 所建立的規範
初次安裝
# 安裝 commitlint-cli 和 config-conventional
npm install --save-dev @commitlint/{config-conventional,cli}
# 會在專案中建立 commitlint.config.js 並放入設定
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js