- 安装
commitlint
依赖
pnpm add -D @commitlint/cli @commitlint/config-conventional
- 项目根目录创建
commitlint.config.js
文件, 编码UTF-8
// commitlint.config.js
module.exports = {
extends: ['@commitlint/config-conventional']
}
- 安装
husky
依赖
pnpm add -D husky
## 执行后会自动添加脚本 package.json -> script -> { "prepare": "husky" }
pnpm husky init
- 设置
commitlint
脚本
npm pkg set scripts.commitlint="commitlint --edit"
- 创建
.husky/commit-msg
文件, 编码UTF-8
如果 .husky
文件夹不存在,执行以下代码
pnpm prepare
创建 .husky/commit-msg
执行脚本
pnpm commitlint
- 测试
git add .
git commit -m "foo: bar"
如果安装正常,应该会出现以下报错
⧗ input: foo: first
✖ type must be one of [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test] [type-enum]
✖ found 1 problems, 0 warnings
ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
ELIFECYCLE Command failed with exit code 1.
husky - commit-msg script failed (code 1)
将默认格式化 Beautify -> perttier