使用husky和commitlint检查提交说明

119 阅读1分钟

husky和comminlint

  1. commitlint:用于检查提交说明
  2. husky:git hooks 工具

开始

commitlint

  1. 安装相关依赖
  npm install @commitlint/config-conventional@12.1.4 @commitlint/cli@12.1.4 -D
  1. 根目录下新增commitlint.config.js文件,并添加配置
module.exports = {
  // 继承的规则
  extends: ['@commitlint/config-conventional'],
  // 定义规则类型
  rules: {
    // type 类型定义,表示 git 提交的 type 必须在一下类型范围内
    'type-enum': [
      2,
      'always',
      [
        'feat', // 新功能 feature
        'fix', // 修复 bug
        'docs', // 文档注释
        'style', // 代码格式
        'refactor', // 重构
        'perf', // 性能优化
        'test', // 增加测试
        'chore', // 构建过程或辅助工具的变动
        'revert', // 回退
        'build' // 打包
      ]
    ],
    // subject 大小写不做校验
    'subject-case': [0]
  }
}

husky

  1. 安装依赖
  npm install husky@7.0.1 -D
  1. 启动 hooks,生成 .husky 文件夹
  npx husky install
  1. package.json中生成 prepare 指令
  // npm 版本需要 > 7.0
  npm set-script prepare "husky install"
  1. 执行 prepare 指令
  npm run prepare
  1. 执行成功,提示

image.png

  1. 添加 commitlint 的 hook 到 husky 中,并指令在 commit-msg 的 hooks 下执行 npx --no-install commitlint --edit "$1" 指令
  npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
  1. 此时 .husky 文件结构如下

image.png