编写优雅的 commit message 并自动生成 changelog

时间:2022-07-24
本文章向大家介绍编写优雅的 commit message 并自动生成 changelog,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

commit message 规范

http://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html

自动校验 commit message

参考:https://github.com/conventional-changelog/commitlint

  1. 安装依赖npm install -D @commitlint/{config-conventional,cli} husky
  2. 在 package.json 添加 commitlint 配置和 git hook
"husky": {
  "hooks": {
    "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
  }
},
"commitlint": {
  "extends": [
    "@commitlint/config-conventional"
  ]
}
  1. 提交 commit 触发校验

通过交互界面生成符合规范的 commit message

  1. 安装依赖npm install -D commitizen cz-conventional-changelog
  2. 在 package.json 中新增一条 script
"scripts": {
  ...
  "commit": "git-cz"
}
  1. 在 package.json 添加 commitizen 配置
"config": {
  "commitizen": {
    "path": "node_modules/cz-conventional-changelog"
  }
}
  1. 执行git add .npm run commit

image

自动生成 changelog

参考:https://github.com/CookPete/auto-changelog

  1. 安装依赖npm install -D auto-changelog
  2. 创建配置文件 .auto-changelog
{
  "template": "CHANGELOG.template",
  "unreleased": true,
  "commitLimit": false
}
  1. 创建自定义 changelog 模板 CHANGELOG.template.**注意替换组名和仓库名**
# Changelog

{{#each releases}}
  ## [{{title}}]

  {{#commit-list commits heading='### Breaking Change' message='[break]'}}
    - {{subject}} [{{shorthash}}]({{href}})
  {{/commit-list}}

  {{#commit-list commits heading='### New Feature' message='feat: ' exclude='[break]'}}
    - {{subject}} [{{shorthash}}]({{href}})
  {{/commit-list}}

  {{#commit-list commits heading='### Bug Fix' message='fix: ' exclude='[break]'}}
    - {{subject}} [{{shorthash}}]({{href}})
  {{/commit-list}}
{{/each}}


  1. 在 package.json 中新增 script
"scripts": {
  ...
  "changelog": "auto-changelog",
  "version": "auto-changelog -p && git add CHANGELOG.md"
}
  1. 执行npm run changelog

执行npm version时会自动生成带所发布版本号的 changelog