-
Notifications
You must be signed in to change notification settings - Fork 7
/
pre-commit.sh
executable file
·42 lines (36 loc) · 1.12 KB
/
pre-commit.sh
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
41
42
#!/bin/bash
TEST_FILE_PATTERN="test[a-zA-Z0-9]+\.(js)$"
FILE_PATTERN="\.(js)$"
FORBIDDEN_WORDS=('.only')
FOUND_FORBIDDEN_WORDS=''
CHANGED_TEST_FILES=`git diff --cached --name-only | grep -E $TEST_FILE_PATTERN`
CHANGED_FILES=`git diff --cached --name-only | grep -E $FILE_PATTERN`
# Check for forbidden words
if [[ "$CHANGED_TEST_FILES" != "" ]]; then
for i in "$CHANGED_TEST_FILES"; do
for j in "${FORBIDDEN_WORDS[@]}"; do
if echo `git show :$i` | grep -q "$j"; then
FOUND_FORBIDDEN_WORDS+=" $i contains $j references\n"
fi
done
done
if [[ ! -z "$FOUND_FORBIDDEN_WORDS" ]]; then
echo "Commit rejected"
echo -e "${FOUND_FORBIDDEN_WORDS%'\n'}"
echo "Please remove them before committing"
exit 1
fi
fi
# Run ESLint
if [[ "$CHANGED_FILES" != "" ]]; then
for i in "$CHANGED_FILES"; do
"node_modules/.bin/eslint" $i
if [[ "$?" != 0 ]]; then
echo "Commit rejected"
echo "ESLint failed"
echo "Please fix the errors before committing"
exit 1
fi
done
fi
exit 0