Skip to content

Commit

Permalink
Add Prettier pre-push hook (#9283)
Browse files Browse the repository at this point in the history
* test pre-push hook

* test pre-push hook

* add prettier pre-push hook to .github/hooks

* add git hooks section to readme

* actually add the changed files to git

---------

Co-authored-by: Rachel <[email protected]>
  • Loading branch information
jazzsequence and rachelwhitton authored Oct 29, 2024
1 parent f8f2c3c commit 265cef1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/hooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh
git fetch origin main
# Check for changed JavaScript files.
CHANGED_FILES=$(git diff --name-only origin/main...HEAD | grep -E '\.js$|\.jsx$|\.ts$|\.tsx$' || echo "")
# If JavaScript files have changed, run Prettier.
if [ -n "$CHANGED_FILES" ]; then
echo "Fixing the following files:"
echo "$CHANGED_FILES"

# Run Prettier on the changed files.
echo "$CHANGED_FILES" | xargs npx prettier --write
echo "Adding Prettier changes to commit"
git add .
git commit -m "Apply Prettier formatting"
else
echo "No files to format"
fi
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ nvm install 18
npm install -g gatsby-cli
```

### Using Git Hooks
Git hooks have been added to `[.github/hooks](.github/hooks)` to help automate some of the tasks that make it easier to work locally with the repository. The following hooks are available:

- `pre-commit`: Runs Prettier to check for code styling issues on updated/changed javascript files. If Prettier made any changes, those changes are automatically committed back to the PR. This mirrors the functionality of the [Prettier GitHub Action](.github/workflows/prettier.yml).

To use the git hooks, you must configure your local environment to use `.github/hooks` as the hooks path (normally `.git/hooks`, but this folder is ignored by Git). This can be done by running the following command from the root of the repository:

```bash
git config core.hooksPath .github/hooks
```

It's recommended (but not required) to use the provided git hooks if you are working with any javascript files in the repository. This will help ensure that your changes are formatted correctly before being committed and ensure that your local environment and the remote branch are the same.
### Get the Code
Fork and clone this repository:
Expand Down

0 comments on commit 265cef1

Please sign in to comment.