Husky 与 Commit 规范
Husky
Husky 让 Git Hooks 配置变得简单。
安装
bash
npm install -D husky
npx husky init
# 生成 .husky/ 目录和 pre-commit 钩子npm install -D husky
npx husky init
# 生成 .husky/ 目录和 pre-commit 钩子常用 Hooks
bash
# .husky/pre-commit — 提交前检查
npx lint-staged
# .husky/commit-msg — 提交信息规范
npx --no -- commitlint --edit $1
# .husky/pre-push — 推送前运行测试
npm run test# .husky/pre-commit — 提交前检查
npx lint-staged
# .husky/commit-msg — 提交信息规范
npx --no -- commitlint --edit $1
# .husky/pre-push — 推送前运行测试
npm run testVS Code 中 Husky 不生效
bash
# 确保 .husky 目录中的文件有可执行权限
chmod +x .husky/pre-commit
chmod +x .husky/commit-msg# 确保 .husky 目录中的文件有可执行权限
chmod +x .husky/pre-commit
chmod +x .husky/commit-msglint-staged
只对暂存区的文件运行 lint,避免全量检查。
js
// .lintstagedrc.js 或 package.json
export default {
'*.{js,jsx,ts,tsx,vue}': ['eslint --fix', 'prettier --write'],
'*.{css,scss,json,md}': ['prettier --write'],
'*.vue': ['eslint --fix', 'prettier --write'],
}// .lintstagedrc.js 或 package.json
export default {
'*.{js,jsx,ts,tsx,vue}': ['eslint --fix', 'prettier --write'],
'*.{css,scss,json,md}': ['prettier --write'],
'*.vue': ['eslint --fix', 'prettier --write'],
}commitlint
安装
bash
npm install -D @commitlint/cli @commitlint/config-conventionalnpm install -D @commitlint/cli @commitlint/config-conventional配置
bash
echo "export default { extends: ['@commitlint/config-conventional'] }" > commitlint.config.jsecho "export default { extends: ['@commitlint/config-conventional'] }" > commitlint.config.jsConventional Commits 规范
<type>(<scope>): <description>
[body]
[footer]<type>(<scope>): <description>
[body]
[footer]type 类型
| type | 含义 | 语义化版本 |
|---|---|---|
feat | 新功能 | minor |
fix | 修复 Bug | patch |
docs | 文档变更 | - |
style | 代码格式调整 | - |
refactor | 重构 | - |
perf | 性能优化 | patch |
test | 测试相关 | - |
chore | 构建/工具变更 | - |
ci | CI/CD 变更 | - |
revert | 回退 | - |
types | 类型定义变更 | - |
示例
feat(auth): add login API
fix(button): resolve click event not firing in Safari
docs(readme): update installation guide
refactor(utils): split large helper into smaller functions
perf(render): reduce unnecessary re-renders by 50%feat(auth): add login API
fix(button): resolve click event not firing in Safari
docs(readme): update installation guide
refactor(utils): split large helper into smaller functions
perf(render): reduce unnecessary re-renders by 50%完整配置流程
bash
# 1. 安装依赖
npm install -D husky lint-staged @commitlint/cli @commitlint/config-conventional
# 2. 初始化 Husky
npx husky init
# 3. 配置 pre-commit hook
echo "npx lint-staged" > .husky/pre-commit
# 4. 配置 commit-msg hook
echo "npx --no -- commitlint --edit \$1" > .husky/commit-msg
# 5. 配置 lint-staged(package.json)# 1. 安装依赖
npm install -D husky lint-staged @commitlint/cli @commitlint/config-conventional
# 2. 初始化 Husky
npx husky init
# 3. 配置 pre-commit hook
echo "npx lint-staged" > .husky/pre-commit
# 4. 配置 commit-msg hook
echo "npx --no -- commitlint --edit \$1" > .husky/commit-msg
# 5. 配置 lint-staged(package.json)json
// package.json
{
"lint-staged": {
"*.{ts,tsx,vue}": ["eslint --fix", "prettier --write"],
"*.{css,scss,json,md}": ["prettier --write"]
}
}// package.json
{
"lint-staged": {
"*.{ts,tsx,vue}": ["eslint --fix", "prettier --write"],
"*.{css,scss,json,md}": ["prettier --write"]
}
}commitizen(交互式提交)
bash
npm install -D commitizen cz-conventional-changelognpm install -D commitizen cz-conventional-changelogjson
// package.json
{
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
},
"scripts": {
"commit": "cz"
}
}// package.json
{
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
},
"scripts": {
"commit": "cz"
}
}然后使用 npm run commit 代替 git commit 交互式填写。
Cao Note