mirror of
https://github.com/ankidroid/Anki-Android.git
synced 2024-09-20 03:52:15 +02:00
c15cc962aa
* Fix prettier throwing errors * Don't run prettier if there is not JS modified files Now pre-commit can be faster and throw less errors
78 lines
2.3 KiB
Bash
Executable File
78 lines
2.3 KiB
Bash
Executable File
#!/bin/sh
|
|
set -e
|
|
|
|
runKtlint () {
|
|
# Following code was generated automatically by running ./gradlew addKtlintFormatGitPreCommitHook
|
|
# for reference, see https://github.com/JLLeitschuh/ktlint-gradle#additional-helper-tasks
|
|
# and for the implementation, https://github.com/JLLeitschuh/ktlint-gradle/blob/master/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt
|
|
# The only change made was replacing "exit" for "return", as this is running inside a function
|
|
######## KTLINT-GRADLE HOOK START ########
|
|
|
|
CHANGED_FILES="$(git --no-pager diff --name-status --no-color --cached | awk '$1 != "D" && $NF ~ /\.kts?$/ { print $NF }')"
|
|
|
|
if [ -z "$CHANGED_FILES" ]; then
|
|
echo "No Kotlin staged files."
|
|
return 0
|
|
fi;
|
|
|
|
echo "Running ktlint over these files:"
|
|
echo "$CHANGED_FILES"
|
|
|
|
diff=.git/unstaged-ktlint-git-hook.diff
|
|
git diff --color=never > $diff
|
|
if [ -s $diff ]; then
|
|
git apply -R $diff
|
|
fi
|
|
|
|
./gradlew --quiet ktlintFormat -PinternalKtlintGitFilter="$CHANGED_FILES"
|
|
gradle_command_exit_code=$?
|
|
|
|
echo "Completed ktlint run."
|
|
|
|
echo "$CHANGED_FILES" | while read -r file; do
|
|
if [ -f $file ]; then
|
|
git add $file
|
|
fi
|
|
done
|
|
|
|
|
|
if [ -s $diff ]; then
|
|
git apply --ignore-whitespace $diff
|
|
fi
|
|
rm $diff
|
|
unset diff
|
|
|
|
echo "Completed ktlint hook."
|
|
exit $gradle_command_exit_code
|
|
######## KTLINT-GRADLE HOOK END ########
|
|
}
|
|
|
|
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
|
|
|
|
CHANGED_JS_FILES="$(git --no-pager diff --name-status --no-color --cached | awk '$1 != "D" && $2 ~ /\.js/ { print $2 }')"
|
|
if [ -z "$CHANGED_JS_FILES" ]; then
|
|
echo "No Javascript staged files. Hence, skipping pre-commit Prettier run."
|
|
return 0
|
|
fi;
|
|
|
|
# Prettify changed files
|
|
echo "$CHANGED_JS_FILES" | xargs npx prettier --ignore-unknown --write
|
|
|
|
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 || :; # || to avoid cancelling the commit if there is an error with Prettier
|
|
echo "Completed the pre-commit hook."
|