工具
- husky
操作 git 钩子的工具 - commitlint
commit 信息校验工具 - commitizen
commit 自动化提示工具,简称 cz - cz-customizable
可自定义的 cz 适配器 - commitlint-config-git-commit-emoji
emoji 插件 
安装
安装 husky 工具
安装 husky 插件
yarn add husky -D
或者
npm i husky -D
在package.json中添加脚本
yarn set-script prepare "husky install"
或者
npm set-script prepare "husky install"
根目录创建.husky文件夹,将 git hooks 钩子交由 husky 执行
yarn prepare
或者
npm run prepare
添加 commit-msg 钩子,执行信息校验
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
安装 commitlint
安装 commit 校验工具
yarn add commitlint @commitlint/config-conventional -D
或者
npm i commitlint @commitlint/config-conventional -D
- @commitlint/config-conventional 这是一个规范配置,标识采用什么规范来执行消息校验, 这个默认是 Angular 的提交规范
 
创建 commitlint.config.js 配置文件
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
安装 commitizen
安装自动化提示工具
yarn add commitizen cz-conventional-changelog -D
或者
npm i commitizen cz-conventional-changelog -D
package.json 中添加 commit 指令, 执行 git-cz 指令
yarn set-script commit "git-cz"
或者
npm set-script commit "git-cz"
初始化命令行的选项信息
npx commitizen init cz-conventional-changelog --save-dev --save-exact
安装可自定义的 cz 适配器
因为 commitizen 只支持英文,如果我想要支持中文指令和 emoji,那么就必须安装可自定义的 cz 适配器了
安装 cz 适配器
yarn -D commitlint-config-cz  cz-customizable
或者
npm i -D commitlint-config-cz  cz-customizable
在根目录创建 .cz-config.js文件
echo > .cz-config.js
将以下配置内容复制到.cz-config.js文件中
module.exports = {
  types: [
    {
      value: ":sparkles: feat",
      name: "✨ feat:     新功能",
    },
    {
      value: ":bug: fix",
      name: "🐛 fix:      修复bug",
    },
    {
      value: ":package: build",
      name: "📦️ build:    打包",
    },
    {
      value: ":zap: perf",
      name: "⚡️ perf:     性能优化",
    },
    {
      value: ":tada: release",
      name: "🎉 release:  发布正式版",
    },
    {
      value: ":lipstick: style",
      name: "💄 style:    代码的样式美化",
    },
    {
      value: ":recycle: refactor",
      name: "♻️  refactor: 重构",
    },
    {
      value: ":pencil2: docs",
      name: "✏️  docs:     文档变更",
    },
    {
      value: ":white_check_mark: test",
      name: "✅ test:     测试",
    },
    {
      value: ":rewind: revert",
      name: "⏪️ revert:   回退",
    },
    {
      value: ":rocket: chore",
      name: "🚀 chore:    构建/工程依赖/工具",
    },
    {
      value: ":construction_worker: ci",
      name: "👷 ci:       CI related changes",
    },
  ],
  messages: {
    type: "请选择提交类型(必填)",
    customScope: "请输入文件修改范围(可选)",
    subject: "请简要描述提交(必填)",
    body: "请输入详细描述(可选)",
    breaking: "列出任何BREAKING CHANGES(可选)",
    footer: "请输入要关闭的issue(可选)",
    confirmCommit: "确定提交此说明吗?",
  },
  allowCustomScopes: true,
  // 跳过问题
  skipQuestions: ["body", "footer"],
  subjectLimit: 72,
};
将 cz-customizable 脚本添加到您的 package.json
yarn set-script commit "git add . && cz-customizable"
或者
npm set-script commit "git add . && cz-customizable"
安装 git-commit-emoji
安装 emoji 插件
yarn add -D  commitlint-config-git-commit-emoji
或者
npm i -D  commitlint-config-git-commit-emoji
更新 commitlint.config.js
移除 extends 中原来的 @commitlint/config-conventional,加入'git-commit-emoji', 'cz'
module.exports = {
  extends: ["git-commit-emoji", "cz"],
};
含 emoji 的自动化提示选项列表
使用 npm run commit或者yarn commit 代替 git commit
在命令行中输入 npm run commit或者yarn commit ,即可通过键盘上下键选择需要要的 commit type了。
其他
根据 commit message 自动生成 changelog
安装 conventional-changelog
yarn add conventional-changelog conventional-changelog-cli --save-dev
或者
npm install conventional-changelog conventional-changelog-cli --save-dev
将 changelog 脚本添加到您的 package.json
yarn set-script changelog "conventional-changelog -p cz-config.js -i CHANGELOG.md -s -r 0"
或者
npm set-script changelog "conventional-changelog -p cz-config.js -i CHANGELOG.md -s -r 0"
添加成功:
{
  "scripts": {
    "changelog": "conventional-changelog -p cz-config.js -i CHANGELOG.md -s -r 0"
  }
}
| 指令 | 功能 | 
|---|---|
| -p | 指定风格* | 
| -i | CHANGELOG.md 指定输出的文件名称 | 
| -s | 输出到 infile,这样就不需要指定与 outfile 相同的文件 | 
| -r | 从最新的版本生成多少个版本。如果为 0,则整个更改日志将被重新生成,输出文件将被覆盖。默认值:1 | 
| -n | ./changelog-option.js 指定自定义配置 | 
运行命令生成最新CHANGELOG
yarn changelog
或者
npm run changelog
使用自动化工具后,还可以用 git commit - m ""提交代码吗?
可以。
配置自动化工具后,不仅支持npm run commit或者yarn commit自动化提示选择提交,也可以继续使用 git commit - m ""提交,只是写type的时候前面还需要加上emoji的字符串。