Skip to content

Instantly share code, notes, and snippets.

@llimllib
Last active March 8, 2017 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save llimllib/acca10ef730384ee519631843e4c31b1 to your computer and use it in GitHub Desktop.
Save llimllib/acca10ef730384ee519631843e4c31b1 to your computer and use it in GitHub Desktop.
#!/bin/sh
# don't allow the commit if it contains any files that are not correctly
# formatted by prettier (https://github.com/jlongster/prettier/).
# Directly inspired by the gofmt precommit hook:
# https://golang.org/misc/git/pre-commit
#
# This code is BSD licensed.
#
# To use, store as .git/hooks/pre-commit inside your repository and make sure
# it has execute permissions. It will not handle files with spaces in their
# name.
jsfiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\.js$')
[ -z "$jsfiles" ] && exit 0
diffs=$(node_modules/.bin/prettier -l $jsfiles | tr '\n' ' ')
[ -z "$diffs" ] && exit 0
echo >&2 "Javascript files must be formatted with prettier. Please run:"
echo >&2 "prettier --write $(echo $jsfiles | tr '\n' ' ')"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment