Skip to content

Instantly share code, notes, and snippets.

@aztack
Forked from linhmtran168/pre-commit-eslint
Last active December 2, 2018 10:03
Show Gist options
  • Select an option

  • Save aztack/b95c198a0022d6d0f4bebbe01925cb8e to your computer and use it in GitHub Desktop.

Select an option

Save aztack/b95c198a0022d6d0f4bebbe01925cb8e to your computer and use it in GitHub Desktop.
Pre-commit hook to check for Javascript using ESLint
#!/bin/sh
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".jsx\{0,1\}$")
ESLINT="$(git rev-parse --show-toplevel)/node_modules/.bin/eslint"
if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
PASS=true
printf "\n检查JavaScript代码中...:\n"
# Check for eslint
if [[ ! -x "$ESLINT" ]]; then
printf "\t\033[41m请先安装ESlint: npm i --save-dev eslint"
exit 1
fi
for FILE in $STAGED_FILES
do
"$ESLINT" "$FILE"
if [[ "$?" == 0 ]]; then
printf "\t\033[32mESLint通过: $FILE\033[0m"
else
printf "\t\033[41mESLint失败: $FILE\033[0m"
PASS=false
fi
done
printf "\nDone!\n"
if ! $PASS; then
echo "\033[41m提交失败:\033[0m 请修复ESLint提示的问题再提交.\n"
exit 1
else
echo "\033[42m提交成功\033[0m\n"
fi
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment