-
Notifications
You must be signed in to change notification settings - Fork 2
59 lines (49 loc) · 1.7 KB
/
prettier.yml
File metadata and controls
59 lines (49 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
name: Auto-format with Prettier
on:
pull_request:
permissions:
contents: write
jobs:
prettier:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # important so git diff works
- uses: actions/setup-node@v4
- name: Install deps
run: yarn
- name: Run prettier on changed files
run: |
CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD \
| grep -E '\.(ts|tsx|js|jsx|json|md|yaml|yml|css|scss)$' || true)
if [ -n "$CHANGED_FILES" ]; then
FILES_TO_FORMAT=""
while IFS= read -r file; do
if [ -f "$file" ]; then
FILES_TO_FORMAT="$FILES_TO_FORMAT\n$file"
else
echo "Skipping missing file: $file"
fi
done <<< "$CHANGED_FILES"
if [ -n "$FILES_TO_FORMAT" ]; then
printf "%b\n" "$FILES_TO_FORMAT" | sed '/^$/d' | xargs yarn prettier --write
else
echo "No existing files to format"
fi
else
echo "No files to format"
fi
- name: Commit formatted changes
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
if [ -n "$(git status --porcelain)" ]; then
# Check out the PR branch to avoid detached HEAD
git checkout ${{ github.head_ref }}
git add --all
git commit -m "chore: Apply Prettier formatting"
git push origin ${{ github.head_ref }}
else
echo "No formatting changes to commit"
fi