foodotfiles/.husky/pre-commit

51 lines
1.5 KiB
Plaintext
Raw Normal View History

2022-09-16 16:21:54 +00:00
#!/bin/sh
# Source husky
# shellcheck disable=SC1091
. "$(dirname -- "$0")/_/husky.sh"
echo "FunctionalHacker's dotfile pre-commit hook start"
printf "Using shell: %s \n" "$(readlink /proc/$$/exe)"
# All staged files
staged_files=$(git diff --name-only --cached --diff-filter=d)
2022-09-16 16:21:54 +00:00
# Run taplo on staged TOML files
staged_toml=$(echo "$staged_files" | grep '.toml$' || true)
num_staged_toml=$(echo "$staged_toml" | grep -vce '^$' || true)
if [ "$num_staged_toml" -gt 0 ]; then
2022-09-16 16:21:54 +00:00
printf '\nFormatting %s staged TOML files with taplo\n' "$num_staged_toml"
taplo format "$staged_toml"
printf "Re-staging\n"
git add $staged_toml
2022-09-16 16:21:54 +00:00
fi
# Run lua-format on staged Lua files
staged_lua=$(echo "$staged_files" | grep '.lua$' || true)
num_staged_lua=$(echo "$staged_lua" | grep -vce '^$' || true)
if [ "$num_staged_lua" -gt 0 ]; then
printf '\nFormatting %s staged Lua files with stylua\n' "$num_staged_lua"
2022-10-26 11:09:50 +00:00
for file in $staged_lua; do
stylua "$file"
done
2022-09-16 16:21:54 +00:00
printf "Re-staging\n\n"
git add $staged_lua
2022-09-16 16:21:54 +00:00
fi
# Run shfmt on staged shell scripts
2022-09-16 17:08:42 +00:00
staged_sh=$(echo "$staged_files" | grep -E '(.sh$|pre-commit)' || true)
num_staged_sh=$(echo "$staged_sh" | grep -vce '^$' || true)
if [ "$num_staged_sh" -gt 0 ]; then
printf '\nFormatting %s staged shell scripts with shfmt\n' "$num_staged_sh"
shfmt -w $staged_sh
2022-09-16 17:08:42 +00:00
printf "Re-staging\n\n"
git add $staged_sh
2022-09-16 17:08:42 +00:00
fi
2022-09-16 16:21:54 +00:00
# Run prettier on all other staged files
# that are supported
printf "\nRunning prettier on all supported files\n"
2022-09-16 16:21:54 +00:00
npx pretty-quick --staged