npm install pre-commit --save-dev
npm instal husky --save-dev
"husky": {
"hooks": {
"pre-commit": "npm run prettier && ./check-keyword.sh"
}
},
check-keyword.sh
#!/bin/bash
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
echo "${green}start checking keyword${reset}"
checkList='TODO debugger console <Table rgb #' #配置检测条件
passfileList='.sh .json' #配置后缀
isError=0
for FILE in `git diff --ignore-space-at-eol --name-only --staged`; do
isPass=0
#排除文件
for passFile in $passfileList;do
if [[ $FILE == *"$passFile"* ]] ; then
echo $FILE '不检测此类型文件'
isPass=1;
continue
fi
done
`git diff --staged $FILE >a.txt`
`grep -v '-' a.txt>b.txt`
if [ $isPass -eq 0 ]; then
#调试
for checkItem in $checkList;do
grep $checkItem b.txt 2>&1 >/dev/null
if [ $? -eq 0 ]; then
echo $FILE '包含'$checkItem'请删除后再提交'
isError=1
fi
done
fi
done
if [ $isError -eq 0 ]; then
exit
fi
if [ $isError -eq 1 ]; then
exit 1
fi