前端工程化之用commintlint + husky实现git提交规范化

在咱们团队协做开发时,若是每一个人的git的commit提交规范都不同,最后的代码review或看git的log提交记录时就是一团乱,今天咱们用commit + husky实现git提交规范化,保证错误的commit信息不能提交成功。node

1、在项目中安装commitlint

  1. 安装依赖
npm install --save-dev @commitlint/{cli,config-conventional}
复制代码
  1. 生成配置文件
echo "module.exports = {extends: ['@commitlint/config-conventional']};" > commitlint.config.js
复制代码

commitlint是检测咱们提交的规范的,具体规范以下:git

type(scope?): subject
body?
footer?
复制代码

其中typesubject是必需的,其余是可选的,type 用于说明 commit 的类别,也能够本身在配置文件中更改或者扩展。subject是 commit 目的的简短描述,不能超过50个字符,且结尾不加英文句号。github

type的类型以下:npm

feat:新功能(feature)
fix:修补bug
docs:文档(documentation)
style: 格式方面的优化
refactor:重构
test:测试
chore:构建过程或辅助工具的变更
复制代码

2、在项目中安装husky

husky可以实现 git hooks ,就是在咱们使用 git 命令的时候,执行一些操做等,如这里就是在 git commit 时执行commitlint规范检测。json

  1. 安装依赖
npm install --save-dev husky
复制代码
  1. package.json 中配置
// package.json
{
  "husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }  
  }
}
复制代码

3、最后咱们来项目中测试下

$ git commit -m "add: first"
husky > commit-msg (node v12.16.2)
⧗   input: add: first
✖   type must be one of [build, chore, ci, docs, feat, fix, improvement, perf, refactor, revert, style, test] [type-enum]

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

husky > commit-msg hook failed (add --no-verify to bypass)
复制代码

执行了bash

git commit -m "add: first"
复制代码

但其实它并不符合咱们 commitlint 的规范,因此并无经过,报错了,到此 commitlint 的使用就完成了,看完的小伙伴记得动手操做一遍哦,有不懂得也能够留言哦!工具

相关文章
相关标签/搜索