Skip to content

Instantly share code, notes, and snippets.

@llimllib llimllib/pre-commit.sh
Last active Mar 8, 2017

Embed
What would you like to do?
#!/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
You can’t perform that action at this time.