配置git提交的校验钩子
- husky:git 提交时触发hooks
- commitlint: 对提交的内容做规范校验husky,主要对pre-commit和commit-msg钩子做校验
# 安装husky npm install husky -D # 初始化husky配置,在根目录新增.husky配置文件。初始化配置pre-commit npx husky-init #另外新增一个hooks,commit-log npx husky add .husky/commit-msg
目录结构这样的
在 commit-msg文件中添加 npm run commitlint
在pre-commit 文件中有个npm run test我们先注释掉,不然会报错
安装commitlint
npm install -D @commitlint/config-conventional @commitlint/cli
配置scripts
因为我们要运行npm run commitlint,所以在package.json文件中添加
"scripts": { "commitlint": "commitlint --config commitlint.config.js -e -V" },
在commit-msg中添加npm run commitlint
然后我们在根目录新建commitlint.config.js
module.exports = { ignores: [(commit) => commit.includes("build")], extends: ["@commitlint/config-conventional"], rules: { "body-leading-blank": [2, "always"], "footer-leading-blank": [1, "always"], "header-max-length": [2, "always", 108], "subject-empty": [2, "never"], "type-empty": [2, "never"], "type-enum": [2, "always", ["merge", "feat", "fix", "perf", "style", "docs", "test", "refactor", "build", "ci", "chore", "revert", "wip", "workflow", "types", "release", "build"]] } };
以上简单的husky配置完成了,我们简单测试下
再次添加前缀,commit成功
标签:npm,Husky,校验,husky,commit,message,config,commitlint From: https://www.cnblogs.com/chailuG/p/17335050.html