-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit
40 lines (30 loc) · 853 Bytes
/
pre-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/bash
cd "$(git rev-parse --show-toplevel)"
ESLINT="node_modules/.bin/eslint"
FILE1=".eslintrc"
FILE2=".eslintrc.js"
FILE3=".eslintrc.json"
pwd
if [ ! -f $FILE1 ] && [ ! -f $FILE2 ] && [ ! -f $FILE3 ]; then
exit 0;
fi
if [[ ! -x "$ESLINT" ]]; then
printf "\t\033[41mPlease install ESlint\033[0m (npm install eslint)\n"
exit 1
fi
STAGED_FILES=($(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(vue|js|html)$'))
echo "ESLint'ing ${#STAGED_FILES[@]} files"
if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
$ESLINT "${STAGED_FILES[@]}" --fix
ESLINT_EXIT="$?"
# Re-add files since they may have been fixed
git add "${STAGED_FILES[@]}"
if [[ "${ESLINT_EXIT}" == 0 ]]; then
printf "\n\033[42mCOMMIT SUCCEEDED\033[0m\n"
else
printf "\n\033[41mCOMMIT FAILED:\033[0m Fix eslint errors and try again\n"
exit 1
fi
exit $?