Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hook for prettier #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions pre-commit.d/prettier.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

# Run prettier on the files in the diff.
#
# Tests if prettier exists

formatter=${PRETTIER_BIN:-./node_modules/.bin/prettier}

if [ ! -f "$formatter" ]; then
exit 0
fi

STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM -- '*.js' '*.scss' | sed 's| |\\ |g')

CHANGED_UNSTAGED_FILES=$(git diff --name-only)

if [ ! -z "$STAGED_FILES" ]; then
if [ ! -z "$CHANGED_UNSTAGED_FILES" ]; then
# if there's changed stuff that isn't staged, we dont want to format the unstaged stuff too
git stash --keep-index
fi

# Format all selected files
echo "$STAGED_FILES" | xargs "$formatter" --write

# Check for the exit code
if [ $? -ne 0 ]; then
exit 1
fi

# Add back the modified/prettified files to staging
echo "$STAGED_FILES" | xargs git add -u


if [ ! -z "$CHANGED_UNSTAGED_FILES" ]; then
# put everything back in its place, hopefully without any merge conflicts...
git stash pop
fi
fi