美文网首页
husky + commitlint 配置git代码提交规范

husky + commitlint 配置git代码提交规范

作者: chenjundi | 来源:发表于2023-01-05 15:56 被阅读0次

安装本地依赖

npm i husky -D
npm i @commitlint/config-conventional @commitlint/cli -D

package.json配置prepare脚本

"scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "prepare": "husky install"
}

项目根目录下新建commitlint.config.js文件

module.exports = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    'body-leading-blank': [2, 'always'],        // body 开始于空白行
    'footer-leading-blank': [1, 'always'],
    'header-max-length': [2, 'always', 108],    // header 字符最大长度为 108
    'subject-empty': [2, 'never'],              // subject 不为空 
    'type-empty': [2, 'never'],                 // type 不为空
    'subject-case': [0],
    'type-enum': [
      2,
      'always',
      [
        'feat',       // 增加新功能
        'fix',        // 修复问题/BUG
        'perf',       // 优化/性能提升
        'style',      // 代码风格相关无影响运行结果的
        'docs',       // 文档/注释
        'test',       // 测试相关
        'refactor',   // 重构
        'build',      // 对构建系统或者外部依赖项进行了修改
        'ci',         // 对 CI 配置文件或脚本进行了修改
        'chore',      // 依赖更新/脚手架配置修改等
        'revert',     // 撤销修改
        'wip',        // 开发中
        'workflow',   // 工作流改进
        'types',      // 类型修改
        'release',
      ],
    ],
  },
};

控制台输入命令,根目录生成.husky文件夹

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

修改.husky文件夹下commit-msg文件内容

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx --no-install commitlint --edit "$1"

此时提交代码会显示报错

git commit -m "commitlint style"
image.png

必须以commitlint配置格式提交代码

git commit -m "fix: commitlint style"

也可以使用--no-verify命令强制跳过验证提交代码

git commit -m "commitlint style" --no-verify

相关文章

网友评论

      本文标题:husky + commitlint 配置git代码提交规范

      本文链接:https://www.haomeiwen.com/subject/qtswsrtx.html