跳到主要内容
版本:1.0

基本配置

信息

在 git commit 之前进行代码风格统一、代码质量检测

安装

npm i -D husky lint-staged pretty-quick

配置

package 添加:

"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"app/**/*.{jsx,txs,ts,js,json,css,md}": [
"prettier --write",
"eslint --fix",
"git add"
]
}

初始化

git init //必须先git初始化
npx husky install
npm set-script prepare "husky install"
npx husky add .husky/pre-commit "npx lint-staged"
git add .husky/pre-commit

更多配置

1. 增加 commit 提示:

npm install commitizen -g
npm i cz-customizable -D

package.json 配置:

"config": {
"commitizen": {
"path":"node_modules/cz-customizable"
}
}

根目录增加文件:.cz-config.js

.cz-config.js
module.exports = {
// 可选类型
types: [
{ value: "feat", name: "feat: 新功能" },
{ value: "fix", name: "fix: 修复" },
{ value: "docs", name: "docs: 文档变更" },
{ value: "style", name: "style: 代码格式(不影响代码运行的变动)" },
{
value: "refactor",
name: "refactor: 重构(既不是增加feature),也不是修复bug",
},
{ value: "pref", name: "pref: 性能优化" },
{ value: "test", name: "test: 增加测试" },
{ value: "chore", name: "chore: 构建过程或辅助工具的变动" },
{ value: "revert", name: "revert: 回退" },
{ value: "build", name: "build: 打包" },
],

// 步骤
messages: {
type: "请选择提交的类型;",
customScope: "请输入修改的范围(可选)",
subject: "请简要描述提交(必填)",
body: "请输入详细描述(可选)",
footer: "请选择要关闭的issue(可选)",
confirmCommit: "确认要使用以上信息提交?(y/n)",
},

// 跳过步骤
skip: ["body", "footer"],

// 默认长度
subjectLimit: 72,
};

2.commitlint 检测 commit 提交是否符合规范:

npm install --save-dev @commitlint/config-conventional @commitlint/cli

根目录创建

commitlint.config.js
module.exports = {
extends: ["@commitlint/config-conventional"],
// 定义规则类型
rules: {
// type 类型定义,表示 git 提交的 type 必须在以下类型范围内
"type-enum": [
2,
"always",
[
"feat", // 新功能
"fix", // 修复
"docs", // 文档变更
"style", // 代码格式(不影响代码运行的变动)
"refactor", // 重构(既不是增加feature),也不是修复bug
"pref", // 性能优化
"test", // 增加测试
"chore", // 构建过程或辅助工具的变动
"revert", // 回退
"build", // 打包
],
],
// subject 大小写不做校验
"subject-case": [0],
},
};

执行 husky

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