Skip to content

Instantly share code, notes, and snippets.

@fujimura
Created December 6, 2017 10:39
Show Gist options
  • Save fujimura/4dd362ca7b3d30de088dc92c5f710677 to your computer and use it in GitHub Desktop.
Save fujimura/4dd362ca7b3d30de088dc92c5f710677 to your computer and use it in GitHub Desktop.
pre-commit hook to run prettier
#!/bin/sh
# Taken from https://prettier.io/docs/en/precommit.html#option-3-bash-script and added (s)css as target
jsfiles=$(git diff --cached --name-only --diff-filter=ACM "*.js" "*.jsx" "*.css" "*.scss" | tr '\n' ' ')
[ -z "$jsfiles" ] && exit 0
# Prettify all staged .js files
echo "$jsfiles" | xargs yarn run prettier --write
# Add back the modified/prettified files to staging
echo "$jsfiles" | xargs git add
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment