Skip to content

Instantly share code, notes, and snippets.

@dmueller39
Created March 29, 2017 21:47
Show Gist options
  • Save dmueller39/43bf58436927188845a81d437e87c785 to your computer and use it in GitHub Desktop.
Save dmueller39/43bf58436927188845a81d437e87c785 to your computer and use it in GitHub Desktop.
A simple shell script for validating prettier has been used on the files that were changed according to git
command -v prettier >/dev/null 2>&1 || { npm install -g prettier; }
usage() {
echo "usage: git-prettier.sh (format|validate) [<commit>]"
}
must_provide_commit_error () {
echo "Must provide a known revision to compare against"
usage
exit 1
}
must_provide_command_error () {
echo "Must provide either 'format' or 'validate' as a command"
usage
exit 1
}
if [[ "$1" != "validate" && "$1" != "format" ]]
then
must_provide_command_error
fi
commit_hash=$(git log --pretty=format:'%h' -n 1 $2 2>&1)
if [[ $commit_hash == *"unknown revision"* ]]
then
must_provide_commit_error
fi
git diff --name-only $2 | grep \.js$ | xargs prettier --single-quote --trailing-comma es5 --write "$1"
if [[ $1 == "validate" ]]
then
changed_files = $(git diff --name-only | grep "\.js$")
changed_filecount=$(echo $changed_filecount | wc -l)
if (( changed_filecount > 0 )); then
echo "You have changes, but they aren't prettier.
usage: git-prettier.sh $1
Files that are not prettier:"
echo changed_files
exit 1
fi
echo "Your changes are prettier."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment