入门
安装
shell
pnpm add -D husky初始化
shell
pnpm exec husky init该指令可以被拆分为三个步骤:
在
package.json中修改prepare脚本json"scripts": { "prepare": "husky" }在
.husky文件夹中创建pre-commit文件,默认内容为npm test创建
.husky/_文件夹,写入内部脚本,用于钩子执行时的环境设置。
DANGER
init 命令会强制覆盖 prepare 脚本,所以如果你原来有 prepare 脚本,你需要手动合并一下。
验证
shell
git commit -m "Keep calm and commit"如果正常 husky 会触发 test 脚本
注意事项
如果需要获取暂存区的文件,请搭配
lint-staged实现如果想要临时禁用
Husky的钩子,可以设置环境变量HUSKY=0shellHUSKY=0 git commit -m "test"