-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit
executable file
·85 lines (64 loc) · 2.49 KB
/
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
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
77
78
79
80
81
82
83
84
85
#!/bin/sh
# Runs Prettier on .ts, .tsx, .js, .jsx, .md, .yml, .yaml, .css and .json files, and also runs ESLint and tsc on .ts,
# .tsx, .js, .jsx files. Prettier and ESLint only check the committed files, whereas tsc checks all project files, since
# it's not possible to run tsc on specific files while obeying the tsconfig.json options (see
# https://github.com/microsoft/TypeScript/issues/27379 and https://www.npmjs.com/package/tsc-files).
# This hook always commits, even if errors are found. And it does not modify the files (ie it does not do `prettier
# write`), it only checks if there is any issue and reports it.
# Inspired by https://prettier.io/docs/en/precommit.html#option-6-shell-script
# Validate that pre-commit and .git/hooks/pre-commit have the same content.
# If content is not the same, do not commit.
diff pre-commit .git/hooks/pre-commit > /dev/null
if [ $? = 1 ]
then
echo "Error: pre-commit and .git/hooks/pre-commit have different content."
echo "Commit aborted."
echo "To fix this run: cp pre-commit .git/hooks"
exit 1
fi
get_staged_files() {
ALL_FILES=$(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g' | awk '/\.ts$|\.tsx$|\.js$|\.jsx$|\.md$|\.yml$|\.yaml$|\.css$|\.json$/')
ALL_CODE_FILES=$(echo "$ALL_FILES" | awk '/\.ts$|\.tsx$|\.js$|\.jsx$/')
CODE_FILES_SERVER=$(echo "$ALL_CODE_FILES" | awk '/server\//' | sed 's/server\///')
CODE_FILES_WEB=$(echo "$ALL_CODE_FILES" | awk '/web\//' | sed 's/web\///')
}
print_title() {
printf "\n\n🔍 %s\n\n" "$1"
}
print_result() {
ERROR_CODE=$?
if [ $ERROR_CODE -eq 0 ]; then
printf "\n✅ %s Success\n\n" "$1"
else
printf "\n❌ %s Failure\n\n" "$1"
fi
}
get_staged_files
# Prettier on both server and web
printf "📁 Files\n\n%s" "$ALL_FILES"
[ -z "$ALL_FILES" ] && exit 0
print_title "Prettier"
echo "$ALL_FILES" | xargs ./node_modules/.bin/prettier --ignore-unknown --check
print_result "Prettier"
# ESLint and tsc on server
cd server || exit 1
printf "📁 Files server\n\n%s" "$CODE_FILES_SERVER"
print_title "ESLint server"
echo "$CODE_FILES_SERVER" | xargs ./node_modules/.bin/eslint
print_result "ESLint server"
print_title "tsc server"
npx tsc --noEmit
print_result "tsc server"
# ESLint and tsc on web
cd ..
cd web || exit 1
printf "📁 Files web\n\n%s" "$CODE_FILES_WEB"
[ -z "$CODE_FILES_WEB" ] && exit 0
print_title "ESLint web"
echo "$CODE_FILES_WEB" | xargs ./node_modules/.bin/eslint
print_result "ESLint web"
print_title "tsc web"
npx tsc --noEmit
print_result "tsc web"
cd ..
exit 0