Husky 的安装
Husky 是一个 Git 钩子工具,可以在 Git 提交时运行预定义的脚本。
npm install husky --save-dev
生成 husky 文件夹
npx husky install
添加 pre-commit 脚本
npx husky add .husky/pre-commit
在根目录下创建 pre-commit 时使用的 ./eslint-config.js 规则文件。
module.exports = {
// ...
rules: {
// ...
'no-console': ['error', { allow: ['error'] }],
// 其他规则
},
// ...
};
编写脚本
#!/bin/bash
# 执行前 eslint 校验
# Run ESLint check for 'no-console' rule
if git diff --cached --name-only --diff-filter=ACM | grep -E '\.(js|vue)$' | xargs -r eslint --max-warnings=0 --config ./eslint-config.js; then
exit 0
else
echo "ESLint check failed: no-console rule violation found. Please fix the errors and try again."
exit 1
fi
# 可以配置 lint-stant 校验