Skip to content

Commit 62e5791

Browse files
rachelwhittonstevectorPantheon Bot
authored
Re-add Prettier GitHub workflow #9163 (#9255)
* Revert "Revert "Add Prettier GitHub workflow (#9163)" (#9254)" This reverts commit eb86890. * trying renaming of config * Revert "trying renaming of config" This reverts commit f823a05. * removing type declaration in package.json * removing eslint * removing eslint * readme fix * trying https://github.com/gatsbyjs/gatsby/issues/9038\#issuecomment-432342005 * Apply Prettier formatting * only update prettier * changing gatsby-node back --------- Co-authored-by: Steve Persch <steve.persch@pantheon.io> Co-authored-by: Pantheon Bot <bot@getpantheon.com>
1 parent 8fe7860 commit 62e5791

File tree

7 files changed

+106
-24
lines changed

7 files changed

+106
-24
lines changed

.eslintrc

Lines changed: 0 additions & 7 deletions
This file was deleted.

.github/workflows/prettier.yml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
name: Prettier
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- 'src/**/*.js'
7+
- '.github/workflows/prettier.yml'
8+
types:
9+
- opened
10+
- synchronize
11+
- reopened
12+
- ready_for_review
13+
workflow_dispatch:
14+
15+
permissions:
16+
contents: write
17+
pull-requests: write
18+
19+
jobs:
20+
lint:
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: actions/checkout@v4
24+
with:
25+
fetch-depth: 0
26+
- uses: actions/setup-node@v4
27+
with:
28+
node-version: 'latest'
29+
- name: Install Node dependencies
30+
run: npm ci
31+
- name: Run Prettier on changed files
32+
env:
33+
GH_TOKEN: ${{ github.token }}
34+
run: |
35+
git fetch origin main
36+
# Check for changed JavaScript files.
37+
CHANGED_FILES=$(git diff --name-only origin/main...HEAD | grep -E '\.js$|\.jsx$|\.ts$|\.tsx$' || echo "")
38+
# If JavaScript files have changed, run Prettier.
39+
if [ -n "$CHANGED_FILES" ]; then
40+
echo "Fixing the following files:"
41+
echo "$CHANGED_FILES"
42+
43+
# Run Prettier on the changed files.
44+
echo "$CHANGED_FILES" | xargs npx prettier --write
45+
46+
# Join the filenames into a single line with space separation.
47+
JOINED_FILES=$(echo $CHANGED_FILES | tr '\n' ' ')
48+
echo "CHANGED_FILES=$JOINED_FILES" >> $GITHUB_ENV
49+
else
50+
echo "No files to format"
51+
fi
52+
- name: Commit and push Prettier changes
53+
env:
54+
GH_TOKEN: ${{ github.token }}
55+
run: |
56+
# Did we make changes when we ran Prettier?
57+
if [ -n "$(git status --porcelain)" ]; then
58+
git config user.name "Pantheon Bot"
59+
git config user.email "bot@getpantheon.com"
60+
git add .
61+
git commit -m "Apply Prettier formatting"
62+
git push origin HEAD:${{ github.head_ref }}
63+
64+
DIFF_OUTPUT=$(git diff HEAD~1 HEAD)
65+
echo "DIFF_OUTPUT<<EOF" >> $GITHUB_ENV
66+
echo "$DIFF_OUTPUT" >> $GITHUB_ENV
67+
echo "EOF" >> $GITHUB_ENV
68+
else
69+
echo "No changes to commit"
70+
fi
71+
- name: Set Prettier diff output
72+
env:
73+
GH_TOKEN: ${{ github.token }}
74+
run: |
75+
if [ -n "${{ env.DIFF_OUTPUT }}" ]; then
76+
CURRENT_COMMIT=$(git rev-parse --short HEAD)
77+
gh pr comment ${{ github.event.pull_request.number }} --body "$(echo -e "Hi from your friendly robot! 🤖\n\nI've applied Prettier formatting to the following files in $CURRENT_COMMIT:\n\n\`${{ env.CHANGED_FILES }}\`\n\nThe full diff is below. Please review the changes.\n\n<details>\n<summary>Click to expand</summary>\n\n\`\`\`diff\n$DIFF_OUTPUT\n\`\`\`\n</details>")"
78+
else
79+
echo "No Prettier changes"
80+
fi

.prettierrc

Lines changed: 0 additions & 7 deletions
This file was deleted.

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ lando start
139139
140140
You can view the local environment at `localhost:8000/`. Updates to docs are automatically refreshed in the browser.
141141
142+
## Code Formatting
143+
We use Prettier to enforce code style on changed files. On each pull request to the repository, if any `.js`, `.jsx`, `.ts` or `.tsx` files are modified in the `/src` directory, We run Prettier to check for code styling issues on the updated/changed files. If Prettier made any changes, those changes are automatically committed back to the PR (see [example PR](https://github.com/pantheon-systems/documentation/pull/9180#issuecomment-2292403319)).
144+
145+
146+
To automatically fix formatting issues across the entire `/src` directory, run:
147+
```bash
148+
npm run format
149+
```
150+
Be cautious when running this command, as it will automatically fix any formatting issues it can.
151+
142152
## Testing
143153
144154
We include several tools to test that new content doesn't break the documentation. Most of these tests are performed automatically by our continuous integration service, but pull requests created from external contributors aren't included in CI tests. If you want to manually test your branch, you can execute the following tests within the Docker container.

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"@octokit/rest": "^16.43.2",
6868
"date-fns": "^2.29.3",
6969
"mark.js": "^8.11.1",
70-
"prettier": "^2.8.3",
70+
"prettier": "3.3.3",
7171
"react-date-range": "^1.4.0"
7272
},
7373
"homepage": "https://github.com/gatsbyjs/gatsby-starter-blog#readme",

prettier.config.cjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
"endOfLine": "lf",
3+
"singleQuote": true,
4+
"tabWidth": 2,
5+
"trailingComma": "all"
6+
}

0 commit comments

Comments
 (0)