Add support for shfmt in git pre-commit hook
This commit is contained in:
parent
2a9a3e5a2a
commit
895cf7706b
|
@ -1,8 +1,5 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
# Exit on error
|
|
||||||
set -e
|
|
||||||
|
|
||||||
# Source husky
|
# Source husky
|
||||||
# shellcheck disable=SC1091
|
# shellcheck disable=SC1091
|
||||||
. "$(dirname -- "$0")/_/husky.sh"
|
. "$(dirname -- "$0")/_/husky.sh"
|
||||||
|
@ -14,9 +11,9 @@ printf "Using shell: %s \n" "$(readlink /proc/$$/exe)"
|
||||||
staged_files=$(git diff --name-only --cached)
|
staged_files=$(git diff --name-only --cached)
|
||||||
|
|
||||||
# Run taplo on staged TOML files
|
# Run taplo on staged TOML files
|
||||||
staged_toml=$(echo "$staged_files" | grep ".toml$" || true)
|
staged_toml=$(echo "$staged_files" | grep '.toml$' || true)
|
||||||
num_staged_toml=$(echo "$staged_toml" | grep -vce '^$')
|
num_staged_toml=$(echo "$staged_toml" | grep -vce '^$' || true)
|
||||||
if [ "$num_staged_toml" -ne "0" ]; then
|
if [ "$num_staged_toml" -gt 0 ]; then
|
||||||
printf '\nFormatting %s staged TOML files with taplo\n' "$num_staged_toml"
|
printf '\nFormatting %s staged TOML files with taplo\n' "$num_staged_toml"
|
||||||
taplo format "$staged_toml"
|
taplo format "$staged_toml"
|
||||||
printf "Re-staging\n"
|
printf "Re-staging\n"
|
||||||
|
@ -24,16 +21,26 @@ if [ "$num_staged_toml" -ne "0" ]; then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Run lua-format on staged Lua files
|
# Run lua-format on staged Lua files
|
||||||
staged_lua=$(echo "$staged_files" | grep ".lua$" || true)
|
staged_lua=$(echo "$staged_files" | grep '.lua$' || true)
|
||||||
num_staged_lua=$(echo "$staged_lua" | grep -vce '^$')
|
num_staged_lua=$(echo "$staged_lua" | grep -vce '^$' || true)
|
||||||
if [ "$num_staged_lua" -ne "0" ]; then
|
if [ "$num_staged_lua" -gt 0 ]; then
|
||||||
printf '\nFormatting %s staged Lua files with lua-format\n' "$num_staged_lua"
|
printf '\nFormatting %s staged Lua files with lua-format\n' "$num_staged_lua"
|
||||||
lua-format -i "$staged_lua"
|
lua-format -i "$staged_lua"
|
||||||
printf "Re-staging\n\n"
|
printf "Re-staging\n\n"
|
||||||
git add "$staged_lua"
|
git add "$staged_lua"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Run shfmt on staged shell scripts
|
||||||
|
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"
|
||||||
|
printf "Re-staging\n\n"
|
||||||
|
git add "$staged_sh"
|
||||||
|
fi
|
||||||
|
|
||||||
# Run prettier on all other staged files
|
# Run prettier on all other staged files
|
||||||
# that are supported
|
# that are supported
|
||||||
echo "Running prettier on all supported files"
|
printf "\nRunning prettier on all supported files\n"
|
||||||
npx pretty-quick --staged
|
npx pretty-quick --staged
|
||||||
|
|
Loading…
Reference in a new issue