如何在预提交钩子运行时从eslint获得错误代码?



我有这个在我的.git/hooks/pre-commit:

npx eslint --max-warnings 1 --exit-on-fatal-error $(git diff --name-only HEAD **/*.ts | xargs)
status=$?
if [ $status -eq 0 ]
then
echo "Good"
exit 2
fi
if [ $status -eq 1 ]
then
echo "Bad"
exit 2
fi
if [ $status -eq 2 ]
then
echo "Worse"
exit 2
fi

它总是打印&;good &;,即使我有一个有效的ESLint错误。当我直接在CLI中运行ESLint时,我可以告诉它错误,它输出如下内容:

$ npx eslint --max-warnings 0 --exit-on-fatal-error make/link/fold/index.ts
./make/link/fold/index.ts
325:14  error  Expected a default case  default-case
✖ 1 problem (1 error, 0 warnings)

如果有错误,我如何从eslint捕获错误并防止提交发生?

我以前从来没有真正这样做过,总是在其他人的项目中使用它,现在尝试配置。试了一会儿,没有运气。

我会怎么做:

while read -r ts; do
npx eslint --max-warnings 1 --exit-on-fatal-error "$ts"
status=$?
case $status in
0)
echo "Good"
exit 0
;;
1)
echo "Bad"
exit 1
;;
1)
echo "Worse"
exit 2
;;
*)
echo "WTF ?!"
exit 3
;;
esac
done < <(git diff --name-only HEAD **/*.ts)

case语句允许您根据几个模式匹配一个单词,并根据匹配的模式执行命令。参见http://mywiki.wooledge.org/BashGuide/TestsAndConditionals#Choices和http://wiki.bash-hackers.org/syntax/ccmd/case以及man bash中的'case word in'。

最新更新