新版husky配合commitlint,规范我们的git的提交记

2022-05-30  本文已影响0人  励志前端小黑哥

网上介绍新版husky的文章太少了,这里记录一下

安装

最简单的,只需要这两个东西就行了

npm install --save-dev husky @commitlint/cli

网上其他文章,把很多杂七杂八的依赖包搅在一起,很烦

配置

配置也很简单,网上其他文章长篇大论的,整得很复杂一样

第一步:初始化husky

npx husky install

第二步:添加git提交的hook

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

第三步:在项目根目录新建commitlint的配置文件commitlint.config.js

module.exports = {
    extends: ['@commitlint/config-conventional'],
    rules: {
      'type-case': [2, 'always', ['lower-case', 'upper-case']],
      'type-enum': [2, 'always',['feat', 'fix', 'docs','style','refactor','perf','test', 'chore', 'revert']]
    }
  }

至此,所有的配置就完成了

验证一下

输入

git commit -m "错误测试"

git返回

✖   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

husky - commit-msg hook exited with code 1 (error)

可以看见,规范已经生效了,这个"错误测试"没提供typesubject


最后再补充两个附加信息,提交信息和配置文件的解释,用的时候来我这篇文章查一查就行了哦~

附1:git commit完整的规范

提交格式:

<type>(<scope>): <subject>
1. type 为必填项,用于指定 commit 的类型
2. scope 为非必填项,用于描述改动的影响范围

scope可以是文件名,也可以是模块名,由自己定

3. subject 是必填项,这次提交的日志信息

附2:commitlint.config.js配置文件配置项解释

commitlint的规则由名称和配置数组组成,格式为:
"rule-name": [Level,Applicable,Value]

可用的rule-name包括:

body-full-stop

body-leading-blank

body-empty

body-max-length

body-max-line-length

body-min-length

body-case

[
  'lower-case', // default
  'upper-case', // UPPERCASE
  'camel-case', // camelCase
  'kebab-case', // kebab-case
  'pascal-case', // PascalCase
  'sentence-case', // Sentence case
  'snake-case', // snake_case
  'start-case' // Start Case
]

footer-leading-blank

footer-empty

footer-max-length

footer-max-line-length

footer-min-length

header-case

header-full-stop

header-max-length

header-min-length

references-empty

scope-enum

scope-case

scope-empty

scope-max-length

scope-min-length

subject-case

subject-empty

subject-full-stop

subject-max-length

subject-min-length

subject-exclamation-mark

type-enum

type-case

type-empty

type-max-length

type-min-length

signed-off-by

trailer-exists

commitlint配置文件原文(英文的):https://github.com/conventional-changelog/commitlint/blob/master/docs/reference-rules.md

上一篇 下一篇

猜你喜欢

热点阅读