配置代码提交规范

831 阅读2分钟

安装npm包

yarn add commitizen -D

yarn add cz-customizable -D

新建 .cz-config.js 配置文件

在根目录下新建,注意,必须是 .js 文件,不能是 .ts

// .cz-config.js

/*
 * @Description: commit 提交配置
 * @Author: kivet
 * @Date: 2022-01-07 13:21:57
 * @LastEditTime: 2022-01-26 10:38:24
 */

module.exports = {
  types: [
    {
      value: 'fix',
      name: 'fix:      优化,或修复一个bug',
    },
    {
      value: 'feat',
      name: 'feat:     新增一个功能',
    },
    {
      value: 'update',
      name: 'update:   代码打包提交',
    },
    {
      value: 'build',
      name: 'build:    变更项目构建或外部依赖(如scopes: webpack、npm等)',
    },
    {
      value: 'perf',
      name: 'perf:     性能优化',
    },
    {
      value: 'docs',
      name: 'docs:     文档变更',
    },
    {
      value: 'revert',
      name: 'revert:   代码回退',
    },
    {
      value: 'refactor',
      name: 'refactor: 代码重构',
    },
    {
      value: 'test',
      name: 'test:     添加一个测试',
    },
  ],
  messages: {
    type: '选择一种你期望的提交类型(type):',
    // scope: '选择一个更改的范围(scope) (可选):',
    // used if allowCustomScopes is true
    // customScope: 'Denote the SCOPE of this change:',
    subject: '输入本次commit记录说明:\n',
    // body: '长说明,使用"|"换行(可选):\n',
    // breaking: '非兼容性说明 (可选):\n',
    // footer: '关联关闭的issue,例如:#31, #34(可选):\n',
    confirmCommit: '确定提交说明?',
  },
  // ? 设置更改的范围
  // scopes: [
  //   { name: 'api' },
  //   { name: 'bug' },
  //   { name: 'optimization' },
  //   { name: '添加其它' }
  // ],
  skipQuestions: ['scope', 'body', 'breaking', 'footer'],
  allowBreakingChanges: [
    'fix',
    'feat',
    'update',
    'refactor',
    'perf',
    'build',
    'revert',
  ],
  subjectLimit: 100,
};

上面是个简单的配置,实际项目中,按项目开发需要自定义配置成自己项目所需要的样子即可。

配置命令

package.json 文件中进行配置 commit 命令。

{
  "scripts": {
    "commit": "git add . && git-cz",
  },
  "config": {
    "commitizen": {
      "path": "node_modules/cz-customizable"
    }
  },
  "devDependencies": {
    "commitizen": "^4.2.4",
    "cz-customizable": "^6.3.0",
  }
}

测试

配置完成后,命令行终端输入命令 yarn commit 进行代码提交测试,如果能成功跑通下面流程,表示已经配置成功。

注意:

命令行只是简化了 git add .git commit -m 'xxx' 两步,代码 push 依然需要自行 git push


返回目录文档