1、安装husky
yarn add --dev husky
2、配置husky
在 package.json 中的 script 中添加一条prepare命令:
-
"scripts": {
-
"serve":
"vue-cli-service serve",
-
"build":
"vue-cli-service build",
-
"lint":
"vue-cli-service lint",
-
"prepare":
"husky install"
-
},
执行以下命令进行husky初始化,自动生成.husky文件夹
yarn prepare
3、代码规范配置
- 安装 @commitlint/config-conventional 和 @commitlint/cli
yarn add @commitlint/config-conventional @commitlint/cli -D
- 配置 commitlint,在项目根目录下新建 commitlint.config.js 文件
-
module.
exports = {
-
ignores: [
(commit) => commit.
includes(
'init')],
-
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 生成 commit-msg 文件,验证提交信息
npx husky add .husky/commit-msg "npx --no-install commitlint --edit "$1""
commit-msg文件内容如下:
-
#!
/usr/bin/env sh
-
.
"$(dirname -- "$0
")/_/husky.sh"
-
-
npx --no-install commitlint --edit
"$1"