Skip to content

Commit

Permalink
Merge pull request #1699 from guardian/run-prettier-in-pre-commit-hook
Browse files Browse the repository at this point in the history
Add pre-commit hook that runs prettier
  • Loading branch information
emdash-ie authored Oct 23, 2024
2 parents 8af7e64 + 8274d9b commit 4804b2a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
53 changes: 53 additions & 0 deletions scripts/git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bash

if command -v fronts-client/node_modules/.bin/prettier > /dev/null; then
echo "Checking file formatting with prettier…"
echo ""
# For files which have staged changes and no unstaged changes, let prettier format them
filesWithOnlyStagedChanges=$(
comm -1 -3 \
<(git diff --name-only --diff-filter=ACMR | grep "\.tsx\?\$" | sort) \
<(git diff --cached --name-only --diff-filter=ACMR | grep "\.tsx\?\$" | sort))
prettierFormattedOutput=$(
comm -1 -3 \
<(git diff --name-only --diff-filter=ACMR | grep "\.tsx\?\$" | sort) \
<(git diff --cached --name-only --diff-filter=ACMR | grep "\.tsx\?\$" | sort) \
| tr "\n" "\0" \
| xargs -0 fronts-client/node_modules/.bin/prettier --write --list-different)
# For files which have both staged and unstaged changes, check them with prettier but don’t modify them
prettierCheckedOutput=$(
comm -1 -2 \
<(git diff --name-only --diff-filter=ACMR | grep "\.tsx\?\$" | sort) \
<(git diff --cached --name-only --diff-filter=ACMR | grep "\.tsx\?\$" | sort) \
| tr "\n" "\0" \
| xargs -0 fronts-client/node_modules/.bin/prettier --list-different)
if test -z "$prettierFormattedOutput" -a -z "$prettierCheckedOutput" ; then
echo "All good"
exit 0
else
if test -n "$prettierFormattedOutput" ; then
echo "The following staged files needed formatting by prettier! Formatting has been
applied but the files have not been re-staged: please verify the changes, stage,
and commit again."
echo ""
filesWhichHaveBeenFormatted=$(
comm -1 -2 \
<(git diff --name-only --diff-filter=ACMR | grep "\.tsx\?\$" | sort) \
<(echo "$filesWithOnlyStagedChanges"))
echo "$filesWhichHaveBeenFormatted"
echo ""
fi
if test -n "$prettierCheckedOutput" ; then
echo "The following staged files with additional unstaged changes were reported by
prettier as needing formatting: these files have not been formatted, to avoid
overwriting unstaged changes. Please format them yourself or bypass this hook with
--no-verify."
echo ""
echo "$prettierCheckedOutput"
echo ""
fi
exit 1
fi
else
echo "Note: fronts-client/node_modules/.bin/prettier not found, so skipping formatting check"
fi
1 change: 1 addition & 0 deletions scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ fetch_config(){
configure_git() {
echo "Configuring git…"
git config --local blame.ignoreRevsFile .git-blame-ignore-revs
git config --local core.hooksPath scripts/git-hooks
}

main() {
Expand Down

0 comments on commit 4804b2a

Please sign in to comment.