commit-msg配置相关

398 阅读2分钟

提交Commit-msg梳理

相关工具:husky(钩子)、commitizen(替换git commit),commitlint(commit-msg规范)

具体原理及路径:

Commitizen

  1. 全局安装
npm install -g commitizen.  // 全局安装commitizen
  1. 配置cz-customizable作为commitizen的插件
"config": {
  "commitizen": {
    "path": "node_modules/cz-customizable"
  }
}
  1. 配置自定义git-commit-msg

.cz-config.js

'use strict'
// https://github.com/leoforfree/cz-customizable
const scopes = ['ALL', 'ROOT', 'POS', 'SYS', 'BASE', 'PARTNER'];
const types = [
  {
    value: 'feat',
    name: '✨  feat: 一个新的功能',
  },
  {
    value: 'fix',
    name: '🐞  fix: 修复Bug',
  },
  {
    value: 'style',
    // prettier-ignore
    name: '💅  style: 不影响功能的代码格式改动(消除空格,prettier格式化,文件目录改动等)'
  },
  {
    value: 'docs',
    name: '📚  docs: 只有文档变更',
  },
  {
    value: 'refactor',
    name: '🛠   refactor: 代码重构,不包括 bug 修复、功能新增',
  },
  {
    value: 'perf',
    name: '💪  perf: 性能优化'
  },
  {
    value: 'test',
    name: '🏁  test: 添加、修改测试用例',
  },   
  {
    value: 'chore',
    // prettier-ignore
    name: '🗯   chore: 对构建过程或辅助工具和库的更改(构建、脚手架工具等)'
  },
  {
    value: 'revert',
    name: '⏪  revert: 代码回退',
  },
  {
    value: 'WIP',
    name: '💪  WIP: 开发过程的工作',
  },
];

exports.scopes = scopes;
exports.types = types;

module.exports = {
  types,
  scopes,
  // override the messages, defaults are as follows
  messages: {
    type: '\n选择你要提交的类型:',
    scope: '\n表明修改的范围 (必填):',
    // used if allowCustomScopes is true
    customScope: '请输入自定义的范围:',
    subject: '填写提交节点的描述(必填):\n',
    body: '提供改动的详细描述 (可选). 使用 "|" 换行:\n',
    breaking: '罗列破坏性更新事项 (可选):\n',
    footer: '罗列改动涉及到的issue相关 (可选). E.g.: #31, #34:\n',
    confirmCommit: '确认提交,回车默认yes',
  },
  allowCustomScopes: false,
  allowBreakingChanges: ['feat', 'fix'],
  skipQuestions: ['body', 'breaking', 'footer']
}

Commitlint

commitlint // 校验commit 信息

  • 安装commitlint
npm install --save-dev @commitlint/{cli,config-conventional,prompt-cli}
echo "module.exports = {extends: ['@commitlint/config-conventional']};" > commitlint.config.js
  • 配置commitlint.config.js
'use strict'
// https://commitlint.js.org/#/reference-rules

var czConfig = require("./.cz-configrc");
module.exports = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    'type-enum': () => {
      const typeList = czConfig.types.map(type => type.value);
      return [2, 'always', typeList];
    },
    'scope-enum':[2, 'always', czConfig.scopes],
    'type-case': [1, 'always', ['lower-case', 'upper-case']],
    'scope-case': [2, 'always', ['upper-case']],
    'subject-case': [0, 'never'],
    'scope-empty': [2, 'never'],
  },
}

Husky

yarn add husky --dev

写入钩子

npx husky install
npx husky add .husky/commit-msg 'npx commitlint --edit $1'

至此,commit-msg检验完成

相关参考