前端开发规范06-使用 husky + commitlint 检查提交描述是否符合规范要求

我们了解了 git hooks 的概念,那么接下来我们就使用 git hooks 来去校验我们的提交信息。

要完成这么个目标,那么我们需要使用两个工具:

  1. commitlint:用于检查提交信息

  2. husky:是git hooks工具

注意:npm 需要在 7.x 以上版本!!!!!

那么下面我们分别来去安装一下这两个工具:

commitlint

  1. 安装依赖:

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

    echo "module.exports = {extends: ['@commitlint/config-conventional']}" > 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', // 回退
            'build' // 打包
          ]
        ],
        // subject 大小写不做校验
        'subject-case': [0]
      }
    }
    
    

注意:确保保存为 UTF-8 的编码格式,否则可能报错。

husky

  1. 安装依赖:

    npm install husky@7.0.1 --save-dev
    
  2. 启动 hooks , 生成 .husky 文件夹

    npx husky install
    

在这里插入图片描述

  1. 在 package.json 中生成 prepare 指令( 需要 npm > 7.0 版本 )
npm set-script prepare "husky install"

在这里插入图片描述

  1. 执行 prepare 指令
npm run prepare
  1. 执行成功,提示
    在这里插入图片描述
  2. 添加 commitlinthookhusky中,并指令在 commit-msghooks 下执行 npx --no-install commitlint --edit "$1" 指令
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'

执行命令后会在commit-msg文件中添加一行脚本:npx --no-install commitlint --edit $1

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

# npx 是npm的一个免安装工具,
# 本质就是可以临时download执行某个二进制
# 如果想让 npx 强制使用本地模块,不下载远程模块,可以使用–no-install参数。如果本地不存在该模块,就会报错。
# 这个是采用线上的
# npx --no-install commitlint --edit $1

#这个是采用 依赖包里面的 node_modules
# ./wescrm/node_modules/.bin/commitlint --edit


# 使用yarn
yarn commitlint --edit $1


# # 提交记录检查
# yarn commitlint --edit $1
# # 格式化检查
# yarn format:check
# # eslint检查
# yarn lint:check
  1. 此时的 .husky 的文件结构
    在这里插入图片描述
    至此, 不符合规范的 commit 将不再可提交:
PS C:\Users\95645\Desktop\code\vue\wescrm> git commit -m "测试"
yarn run v1.22.18
warning ..\..\..\..\package.json: No license field
$ C:\Users\95645\Desktop\code\vue\wescrm\node_modules\.bin\commitlint --edit .git/COMMIT_EDITMSG
⧗   input: 测试
✖   subject may not be empty [subject-empty]type may not be empty [type-empty]

✖   found 2 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
husky - commit-msg hook exited with code 1 (error)

如果执行报错:
在这里插入图片描述
可以将commit-config里面的
npx --no-install commitlint --edit $1
改为
yarn commitlint --edit $1
至此,就已经可以处理好了 强制规范化的提交要求,到现在 不符合规范的提交信息,将不可在被提交!
那么到这里我们的 规范化目标 就完成了吗?
当然没有!
现在我们还缺少一个 规范化的处理 ,那就是 代码格式提交规范处理!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值