项目中使用commitizen+commitlint规范提交

安装相关依赖

npm install commitizen cz-customizable @commitlint/cli @commitlint/config-conventional -D

版本号如下:

{
    "@commitlint/cli": "^17.5.0",
    "@commitlint/config-conventional": "^17.4.4",
    "commitizen": "^4.3.0",
    "cz-conventional-changelog": "^3.3.0",
    "cz-customizable": "^7.0.0",
}

cz-conventional-changelog和cz-customizable安装其中一个即可,当前是安装cz-customizable来自定义提交信息
在package.json中配置commitizen的目录

{
   "config": {
    "commitizen": {
      "path": "./node_modules/cz-customizable"
    }
  },

}

新建.cz-config.js文件

module.exports = {
  types: [//描述修改的性质是什么,是bugfix还是feat,在这里进行定义。
    { value: 'feat', name: 'feat:新功能' },
    { value: 'fix', name: 'fix: 修复Bug' },
    { value: 'docs', name: 'docs: 文档更新' },
    {
      value: 'style',
      name: 'style: 样式更新',
    },
    {
      value: 'refactor',
      name: 'refactor:重构了',
    },
    { value: 'test', name: 'test:测试代码' },
  ],
  //定义之后,我们就可以通过上下键去选择 scope
  // scopes: [{ name: 'accounts' }, { name: 'admin' }, { name: 'exampleScope' }, { name: 'changeMe' }],

  usePreparedCommit: false, // to re-use commit from ./.git/COMMIT_EDITMSG
  allowTicketNumber: false,
  isTicketNumberRequired: false,
  ticketNumberPrefix: 'TICKET-',
  ticketNumberRegExp: '\\d{1,5}',

  // it needs to match the value for field type. Eg.: 'fix'
  /*针对每一个type去定义scope

  scopeOverrides: {
    fix: [
      {name: 'merge'},
      {name: 'style'},
      {name: 'e2eTest'},
      {name: 'unitTest'}
    ]
  },
  */
  // override the messages, defaults are as follows
  messages: {
    type: "选择你要提交的类型",
    // scope: '\nDenote the SCOPE of this change (optional):',
    // used if allowCustomScopes is true
    // customScope: 'Denote the SCOPE of this change:',
     subject: 'Write a SHORT, IMPERATIVE tense description of the change:\n',
    // body: 'Provide a LONGER description of the change (optional). Use "|" to break new line:\n',
    // breaking: 'List any BREAKING CHANGES (optional):\n',
    // footer: 'List any ISSUES CLOSED by this change (optional). E.g.: #31, #34:\n',
    confirmCommit: 'Are you sure you want to proceed with the commit above?',
  },
  // 设置为 true,在 scope 选择的时候,会有 empty 和 custom 可以选择,顾名思义,选择 empty 表示 scope 缺省,如果选择 custom,则可以自己输入信息
  allowCustomScopes: true,
  //如上设置为 ['feat', 'fix'],只有我们type选择了 feat 或者是 fix,才会询问我们 breaking message.
  allowBreakingChanges: ['feat', 'fix'],
  // 指定跳过哪些步骤,例如跳过我们刚刚说的详细描述,设置其为 scope: ['body'],假设我们的项目也不会涉及到关联 issue,我们可以设置其为 scope: ['body', 'footer']
  skipQuestions: ['scope','customScope','breaking','footer','body'],

  // 描述的长度限制
  subjectLimit: 100,
  // breaklineChar: '|', // It is supported for fields body and footer.
  // footerPrefix : 'ISSUES CLOSED:'
  // askForBreakingChangeFirst : true, // default is false
};

新建commitlint.config.js

module.exports = {
  extends: ["@commitlint/config-conventional"],
  rules: {
    "type-enum": [
      2,
      "always",
      [
        "WIP", // 开发中
        "feat", // 新特性
        "improvement", // 加强现有特性
        "fix", // 修补bug
        "refactor", // 重构
        "docs", // 文档
        "test", // 单元测试
        "config", // 配置文件
        "style", // 格式需改
        "perf", // 性能提升
        "ci", // ci
        "revert", // 版本回退
        "chore", // 其他修改
      ],
    ],
    "type-empty": [2, "never"], // type不能为空
    "type-case": [0, "always", "lower-case"], // type不限制大小写
    "subject-empty": [2, "never"], // subject(简短得描述)不能为空
    "subject-max-length": [1, "always", 50], // subject最大长度,超出只会警告,不阻止提交
    "body-leading-blank": [1, "always"],
    "footer-leading-blank": [1, "always"],
    "header-max-length": [2, "always", 72],
  },
};

安装husky,安装了可忽略

npm install husky -D

新建commit-msg文件并写入hook执行命令

npx husky add .husky/commit-msg "npx --no -- commitlint --edit $1"

以上完成在git commit 发起提交前执行commitlint检查是否符合规范。可使用git cz 命令代替git commit提交代码

你可能感兴趣的:(项目中使用commitizen+commitlint规范提交)