-
Notifications
You must be signed in to change notification settings - Fork 2
/
pre-commit.sh
executable file
·57 lines (46 loc) · 1.03 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
unit_test_result=0
eslint_result=0
check_unit_test () {
npm test
return $?
}
check_eslint () {
files=$(git diff --cached --name-only | grep '\.js$')
# --cached 对比的是缓存区和当前 HEAD 之间差异
# 否则对比的就是工作区和当前 HEAD 之间的差异
# Prevent ESLint help message if no files matched
if [[ $files = "" ]] ; then
return 0
fi
for file in ${files}; do
if [[ -f $file ]] ; then
git show :$file | ./node_modules/.bin/eslint $file
if [[ $? != 0 ]] ; then
eslint_result=1
fi
fi
done;
return $eslint_result
}
check_unit_test
unit_test_result=$?
check_eslint
eslint_result=$?
if [[ $eslint_result != 0 ]] ; then
echo "🚫 ESLint failed!"
else
echo "👍 ESlint success!"
fi
if [[ $unit_test_result != 0 ]] ; then
echo "🚫 UnitTest failed!"
else
echo "👍 UnitTest success!"
fi
if [[ $(($eslint_result+$unit_test_result)) != 0 ]] ; then
echo "🚫 git commit denied!"
exit 1
else
echo "👍 Commit success!"
exit 0
fi