-
Notifications
You must be signed in to change notification settings - Fork 0
/
precommit
executable file
·76 lines (53 loc) · 1.06 KB
/
precommit
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env bash
###
### To disable precommit hook: "git config precommit.disable 1"
###
### Lib ###
disable=`grep -Pzo '\[precommit\]\s+disable\s+=\s+1' .git/config &> /dev/null || false`
if [ "$disable" ]; then
exit 0
fi
diff() {
git diff-index --name-only HEAD --diff-filter ACMRTUXB
}
grepTypes() {
res=`diff | grep -Ec "\.($1)$"`
[[ $res == 0 ]] && return 1 || return 0
}
print() {
printf "$1" "${@:2}"
}
println() {
print "$1\n" "${@:2}"
}
msg() {
print "[precommit] $1: $2" "${@:3}"
}
infom() {
msg "info" "${@:1}"
}
info() {
infom "$1\n" "${@:2}"
}
### Main ###
# Lint
if grepTypes "ts|tsx|js|jsx";
then
infom "Linting..."
./node_modules/.bin/eslint -c ./.eslintrc --quiet `diff | grep -P "\.(ts|tsx|js|jsx)$"` || exit 1
println "done."
fi
# Dry compile
if grepTypes "ts|tsx";
then
infom "Dry compiling..."
./node_modules/.bin/tsc -p ./tsconfig.json --noEmit || exit 1
println "done."
fi
# Test
if grepTypes "ts|tsx";
then
infom "Testing..."
./node_modules/.bin/jest -c ./jest.config.js -o || exit 1
println "done."
fi