前端项目参与人多的时候,实现代码风格统一,删除 console/debug 等开发代码是比较麻烦的。
代码风格统一可以通过配置 eslint-+ prettier 实现通过自定义规则后再编译,
但是 console/debug 等规则可以随 Git 一起推送到远程仓库。甚至打包生产出现 console.log 这种代码
开始
-
npm i husky pretty-quick -D
-
新增
GitHooks.sh
#!/bin/bash
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
echo "${green}--------- git 提交预检测 ---------${reset}"
for FILE in `git diff --name-only --cached`
do
if [[ $FILE == *".sh"* ]] || [[ $FILE == *"iconfont.js"* || $FILE == *"ExamplePage"* || $FILE == *"min.js"* || $FILE == *"vendor/*"* ]]
then
continue
fi
grep 'debugger\|console.log' $FILE 2>&1 >/dev/null
if [ $? -eq 0 ]
then
echo $red $FILE '包含 TODO/debugger/console/alert 请完全删除后提交(注释也不可以哦😯)'
exit 1
fi
done
exit
修改 package.json
npm 脚本中 && 是先执行前者完成后再执行后者
& 是同时执行无先后次序
"scripts": {
"pre-commit": "bash ./GitHooks.sh && pretty-quick --staged"
},
"husky": {
"hooks": {
"pre-commit": "npm run pre-commit"
}
},