even

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Commitizen助你规范化提交代码

约定式提交规范 ,我们知道如果严格安装 约定式提交规范, 来手动进行代码提交的话,那么是一件非常痛苦的事情,但是 git 提交规范的处理 又势在必行,那么怎么办呢?

你遇到的问题,也是其他人所遇到的!

经过了很多人的冥思苦想,就出现了一种叫做 git 提交规范化工具 的东西,而我们要学习的 commitizen 就是其中的佼佼者!

commitizen 仓库名为 cz-cli ,它提供了一个 git cz 的指令用于代替 git commit,简单一句话介绍它:

当你使用 commitizen 进行代码提交(git commit)时,commitizen 会提交你在提交时填写所有必需的提交字段!

这句话怎么解释呢?不用着急,下面我们就来安装并且使用一下 commitizen ,使用完成之后你自然就明白了这句话的意思!

  1. 全局安装Commitizen

    npm install -g commitizen
    
  2. 安装并配置 cz-customizable 插件

    1. 使用 npm 下载 cz-customizable

      npm i cz-customizable --save-dev
      
    2. 添加以下配置到 package.json

      ...
        "config": {
          "commitizen": {
            "path": "node_modules/cz-customizable"
          },
          "cz-customizable": {
            "config": "cz-config.js"
          }
        }
      
  3. 项目根目录下创建 .cz-config.js 自定义提示文件

     'use strict'
     module.exports = {
       types: [
         { value: 'feat', name: '新增:    新的内容' },
         { value: 'fix', name: '修复:    修复一个Bug' },
         { value: 'docs', name: '文档:    变更的只有文档' },
         { value: 'style', name: '格式:    空格, 分号等格式修复' },
         { value: 'refactor', name: '重构:    代码重构,注意和特性、修复区分开' },
         { value: 'perf', name: '性能:    提升性能' },
         { value: 'test', name: '测试:    添加一个测试' },
         { value: 'chore', name: '工具:    开发工具变动(构建、脚手架工具等)' },
         { value: 'revert', name: '回滚:    代码回退' },
       ],
       messages: {
         type: '选择一种你的提交类型:',
         customScope: '请输入修改范围(可选):',
         subject: '请简要描述提交(必填):\n',
         body: '请输入详细描述(可选):\n',
         breaking: '非兼容性说明 (可选):\n',
         footer: '关联关闭的issue,例如:#31, #34(可选):\n',
         confirmCommit: '确定提交说明?(y/n/e/h)',
       },
       // 跳过问题
       skipQuestions: ['customScope', 'breaking'],
       // subject文字长度默认是72
       subjectLimit: 100,
     }
    
  4. 使用 git cz 代替 git commit
    使用 git cz 代替 git commit,即可看到提示内容

那么到这里我们就已经可以使用git cz 来代替了 git commit 实现了规范化的提交诉求了,但是当前依然存在着一个问题,那就是我们必须要通过 git cz 指令才可以完成规范化提交!

使用 husky + commitlint 检查提交描述是否符合规范要求

commitlint

  1. 安装依赖:

    npm install --save-dev @commitlint/config-conventional @commitlint/cli
    
  2. 创建 commitlint.config.js 文件

  3. 打开 commitlint.config.js , 增加配置项( config-conventional 默认配置点击可查看 ):

    module.exports = {
      // 继承的规则
      extends: ['@commitlint/config-conventional'],
      // 定义规则类型
      rules: {
        // type 类型定义,表示 git 提交的 type 必须在以下类型范围内
        'type-enum': [
          2,
          'always',
          [
            'feat', // 新功能 feature
            'fix', // 修复 bug
            'docs', // 文档注释
            'style', // 代码格式(不影响代码运行的变动)
            'refactor', // 重构(既不增加新功能,也不是修复bug)
            'perf', // 性能优化
            'test', // 增加测试
            'chore', // 构建过程或辅助工具的变动
            'revert', // 回退
          ]
        ],
        // subject 大小写不做校验
        'subject-case': [0]
      }
    }
    
    

**注意:确保保存为 UTF-8

安装husky,可以参看vite关于的代码提交前格式化,章节,再安装后husky后添加以下指令

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

这样,经过面配置后,通过git commit也需要符合规范才能提交,可以使用git cz进行提示性提交

posted on 2021-12-27 23:18  even_blogs  阅读(259)  评论(0)    收藏  举报