首页 > 技术文章 > husky+commitlint配置步骤

wangpenghui522 2021-11-13 14:53 原文

husky v7+ 及 commitlint v13+ 配置方法都有很大改变,  原有的配置方法已经不再使用

安装git-hook工具

yarn add  husky -D

在当前目录下生成.husky文件夹,并删除里面的.gitignore忽略文件,否则无法将.husky下面的文件提交到git仓库

yarn husky install

在.husky文件夹下创建commit-msg文件,注意不要修改commit-msg这个文件的名称,否则husky功能会失效

npx husky add .husky/commit-msg

在.husky/commit-msg文件中写入

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
# 提交记录检查
yarn commitlint --edit $1
# 格式化检查
yarn format:check
# eslint检查
yarn lint:check

将.husky/commit-msg添加到代码仓库

git add .husky/commit-msg

安装git 提交信息规范配置文件

yarn  add -D @commitlint/{cli,config-conventional} 

安装changelog自动化生成工具

yarn add -D conventional-changelog-cli

package.json的script中添加

{
    "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
    "changelog:init": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
    "changelog:help": "conventional-changelog --help",
},
  • 参数-p指定提交信息的规范,有以下选择:angular, atom, codemirror, ember, eslint, express, jquery, jscs or jshint
  • 参数-s 表示将新生成的CHANGELOG输出到-i指定的文件中
  • 参数-i 指定输出CHANGELOG内容的文件
  • 参数-r默认为1,设为0将重新生成所有版本的变更信息

推荐阅读