forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit
executable file
·56 lines (46 loc) · 1.66 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
#!/bin/sh
set -e
runKtlint () {
CHANGED_KT_FILES="$(git --no-pager diff --name-status --no-color --cached | awk '$1 != "D" && $2 ~ /\.kts|\.kt/ { print $2 }')"
if [ -z "$CHANGED_KT_FILES" ]; then
echo "No Kotlin staged files. Hence, skipping pre-commit Ktlint run."
return 0
fi;
echo "Running Ktlint over these files:"
echo "$CHANGED_KT_FILES"
# -q removes noise from the output if it fails.
# TODO: -w is better, but https://github.com/JLLeitschuh/ktlint-gradle/issues/457 adds noise
# -w should display: "CriticalExceptionTest.kt:19:1 Wildcard import (cannot be auto-corrected)"
# Cleaning and retrying only if the ktlint fails
if ! ./gradlew -q ktlintFormat ; then
echo "Cleaning the project and retrying."
# Clean is required because of https://github.com/ankidroid/Anki-Android/issues/9521
./gradlew clean
./gradlew -q ktlintFormat
fi;
echo "Completed ./gradlew ktlintFormat run."
echo "$CHANGED_KT_FILES" | while read -r file; do
if [ -f "$file" ]; then
git add "$file"
fi
done
}
runPrettier () {
# check if npx is installed, else skip runPrettier
if ! command -v npx &> /dev/null
then
echo "npx could not be found. Hence, skipping pre-commit Prettier run."
return 0
fi
# Run prettier over all Javascript files
npx --yes prettier --ignore-unknown --write AnkiDroid/**/*.js
echo "Completed npx prettier run."
echo "$CHANGED_JS_FILES" | while read -r file; do
if [ -f "$file" ]; then
git add "$file"
fi
done
}
runKtlint
runPrettier
echo "Completed the pre-commit hook."