Skip to content

Instantly share code, notes, and snippets.

@john-humi
Last active March 6, 2020 20:54
Show Gist options
  • Select an option

  • Save john-humi/0a2d52020e5c695a471d6f062e5ba6e5 to your computer and use it in GitHub Desktop.

Select an option

Save john-humi/0a2d52020e5c695a471d6f062e5ba6e5 to your computer and use it in GitHub Desktop.
Execute Prettier pre-commit
#!/bin/sh
# husky...
FILES=$(git diff --cached --name-only --diff-filter=ACMR "*.js" "*.ts" | sed 's| |\\ |g')
[ -z "$FILES" ] && exit 0
# Prettify all selected filesi
LINES=$(wc -l <<< $FILES | tr -d '[:space:]')
echo "Running prettier on $LINES files"
echo "$FILES" | xargs prettier --write --config ./package.json
# Add back the modified/prettified files to staging
echo "$FILES" | xargs git add
exit 0
@john-humi
Copy link
Author

Requires husky.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment