Skip to content

Commit

Permalink
Improve Prettier hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ayukmr committed Oct 28, 2024
1 parent 4f2370d commit 6d583d6
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions .git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,54 +1,59 @@
#!/bin/zsh

# format files
function format {
echo "$1" | xargs npx prettier --ignore-unknown --write
}
function format_files {
action 'running formatter'

echo "$files" | xargs npx prettier --ignore-unknown --write

if [[ $status == 0 ]]; then
info 'formatter succeeded'

# add files to git
function add_files {
echo "$1" | xargs git add
# stage formatted files
echo "$files" | xargs git add
else
return 1
fi
}

# run `npm install`
function npm_install {
print -P '%B%F{yellow}action%f%b: running `npm install`'
action 'installing node packages'

npm install

if [[ $status == 0 ]]; then
print -P '%B%F{blue}info%f%b: `npm install` succeeded'
print -P '%B%F{yellow}action%f%b: running formatter'

# run formatter
format "$1"
add_files "$1"
info 'installing succeeded'
format_files
else
print -P '%B%F{red}error%f%b: `npm install` failed'
error 'installing failed'
info 'try running `npm install` manually'
fi
}

# changed files
# logging
function action { print -P "%B%F{yellow}action%f%b: $1" }
function info { print -P "%B%F{blue}info%f%b: $1" }
function error { print -P "%B%F{red}error%f%b: $1" }

# staged java files
local files="$(git diff --cached --name-only --diff-filter=ACMR | grep -E '\.java$')"

# no changed files
if [ -z "$files" ]; then
if [[ -z "$files" ]]; then
# no staged files
exit 0
fi

format "$files"
format_files "$files"

if [[ $status == 0 ]]; then
# add files to git
add_files "$1"
else
print -P "%B%F{red}error%f%b: prettier exited $status"
if [[ $status != 0 ]]; then
error 'formatter failed'

if which npx &> /dev/null; then
# try `npm install`
npm_install "$files"
npm_install
else
print -P '%B%F{red}error%f%b: `npx` is not accessible'
print -P '%B%F{blue}info%f%b: install npm and verify it is in $PATH'
error '`npx` is not accessible'
info 'install npm and verify it is in $PATH'
fi
fi

0 comments on commit 6d583d6

Please sign in to comment.