Skip to content

Commit 4ef99da

Browse files
Update prettier-code-format-check.yml
Signed-off-by: LUIZ HAMILTON ROBERTO DA SILVA <[email protected]>
1 parent 78c6da7 commit 4ef99da

1 file changed

Lines changed: 67 additions & 18 deletions

File tree

.github/workflows/prettier-code-format-check.yml

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ on:
1919
- "package-lock.json"
2020
- "pnpm-lock.yaml"
2121
- "yarn.lock"
22+
- ".github/workflows/prettier-code-format-check.yml"
2223
pull_request:
2324
branches: [main, develop]
2425
paths:
@@ -37,6 +38,7 @@ on:
3738
- "package-lock.json"
3839
- "pnpm-lock.yaml"
3940
- "yarn.lock"
41+
- ".github/workflows/prettier-code-format-check.yml"
4042
workflow_dispatch:
4143

4244
concurrency:
@@ -58,7 +60,6 @@ jobs:
5860
ALLOW_WARNINGS: "true"
5961

6062
# If Prettier is missing from devDependencies, CI can install a pinned version temporarily.
61-
# Recommended long-term: add prettier to devDependencies.
6263
BOOTSTRAP_PRETTIER_IF_MISSING: "true"
6364
PRETTIER_VERSION: "3.3.3"
6465

@@ -70,30 +71,37 @@ jobs:
7071
- name: 📦 Checkout Repository
7172
uses: actions/checkout@v4
7273

73-
- name: ⚙️ Setup Node.js (LTS)
74-
uses: actions/setup-node@v4
75-
with:
76-
node-version: "20"
77-
cache: "npm"
78-
79-
- name: 🧾 Detect Package Manager
74+
- name: 🧾 Detect Package Manager + Tooling
8075
id: pm
8176
shell: bash
8277
run: |
8378
set -euo pipefail
79+
8480
if [[ -f pnpm-lock.yaml ]]; then
8581
echo "manager=pnpm" >> "$GITHUB_OUTPUT"
8682
elif [[ -f yarn.lock ]]; then
8783
echo "manager=yarn" >> "$GITHUB_OUTPUT"
8884
elif [[ -f package-lock.json ]]; then
8985
echo "manager=npm" >> "$GITHUB_OUTPUT"
9086
else
91-
# No lockfile -> still attempt npm install, but warn in summary.
9287
echo "manager=npm" >> "$GITHUB_OUTPUT"
9388
fi
9489
95-
- name: 📥 Install Dependencies (deterministic when possible)
90+
if [[ -f package.json ]]; then
91+
echo "has_package_json=true" >> "$GITHUB_OUTPUT"
92+
else
93+
echo "has_package_json=false" >> "$GITHUB_OUTPUT"
94+
fi
95+
96+
- name: ⚙️ Setup Node.js (LTS) + Cache
97+
uses: actions/setup-node@v4
98+
with:
99+
node-version: "20"
100+
cache: ${{ steps.pm.outputs.manager }}
101+
102+
- name: 📥 Install Dependencies (only if package.json exists)
96103
id: install
104+
if: ${{ steps.pm.outputs.has_package_json == 'true' }}
97105
shell: bash
98106
run: |
99107
set -euo pipefail
@@ -106,13 +114,12 @@ jobs:
106114
elif [[ "$mgr" == "yarn" ]]; then
107115
corepack enable
108116
yarn --version
109-
# Yarn Berry uses --immutable; Yarn classic ignores it but won't break
110117
yarn install --immutable || yarn install
111118
else
112119
if [[ -f package-lock.json ]]; then
113120
npm ci
114121
else
115-
echo "::warning::No lockfile found (package-lock.json). Using 'npm install' (non-deterministic)."
122+
echo "::warning::No lockfile found. Using 'npm install' (non-deterministic)."
116123
npm install
117124
fi
118125
fi
@@ -136,7 +143,6 @@ jobs:
136143
fi
137144
138145
echo "::notice::Prettier not found in node_modules. Bootstrapping Prettier v${PRETTIER_VERSION} for CI-only run."
139-
# CI-only install. This will NOT commit anything (runner workspace only).
140146
npm install --no-save "prettier@${PRETTIER_VERSION}"
141147
142148
if [[ -x "node_modules/.bin/prettier" ]]; then
@@ -147,7 +153,32 @@ jobs:
147153
echo "prettier_source=bootstrap-failed" >> "$GITHUB_OUTPUT"
148154
fi
149155
150-
- name: 🔍 Run Prettier Check
156+
- name: 🧾 Build file list (tracked files only)
157+
id: files
158+
shell: bash
159+
run: |
160+
set -euo pipefail
161+
162+
# Only tracked files; avoids scanning node_modules or generated artifacts.
163+
# Keep in sync with your "paths" filters.
164+
git ls-files -z \
165+
'*.js' '*.ts' '*.jsx' '*.tsx' \
166+
'*.css' '*.json' '*.md' \
167+
'*.yml' '*.yaml' \
168+
> prettier-files.txt || true
169+
170+
count="$(python3 - <<'PY'
171+
import os
172+
p='prettier-files.txt'
173+
if not os.path.exists(p):
174+
print(0); raise SystemExit
175+
data=open(p,'rb').read()
176+
print(0 if not data else data.count(b'\x00'))
177+
PY
178+
)"
179+
echo "count=${count}" >> "$GITHUB_OUTPUT"
180+
181+
- name: 🔍 Run Prettier Check (tracked files)
151182
id: run_prettier
152183
shell: bash
153184
run: |
@@ -157,15 +188,24 @@ jobs:
157188
: > "$report"
158189
159190
prettier_present="${{ steps.ensure_prettier.outputs.prettier_present }}"
191+
files_count="${{ steps.files.outputs.count }}"
192+
160193
if [[ "${prettier_present}" != "true" ]]; then
161194
echo "Prettier not available in this run." > "$report"
162195
echo "exit_code=2" >> "$GITHUB_OUTPUT"
163196
exit 0
164197
fi
165198
166-
# Run local prettier directly (avoid npx ambiguity)
199+
if [[ "${files_count}" == "0" ]]; then
200+
echo "No tracked files matched the Prettier file patterns." > "$report"
201+
echo "exit_code=0" >> "$GITHUB_OUTPUT"
202+
exit 0
203+
fi
204+
167205
set +e
168-
node_modules/.bin/prettier --check . > "$report" 2>&1
206+
# Use --ignore-unknown to avoid errors on edge cases if file list expands later.
207+
# Use xargs -0 to preserve spaces/newlines in paths.
208+
cat prettier-files.txt | xargs -0 node_modules/.bin/prettier --ignore-unknown --check > "$report" 2>&1
169209
ec="$?"
170210
set -e
171211
@@ -194,17 +234,25 @@ jobs:
194234
prettier_present="${{ steps.ensure_prettier.outputs.prettier_present }}"
195235
prettier_source="${{ steps.ensure_prettier.outputs.prettier_source }}"
196236
mgr="${{ steps.pm.outputs.manager }}"
237+
has_pkg="${{ steps.pm.outputs.has_package_json }}"
238+
files_count="${{ steps.files.outputs.count }}"
239+
240+
# Approx count of reported issues/files from report (best-effort).
241+
findings="$(grep -E '^\[warn\]|^\[error\]' "${PRETTIER_REPORT}" 2>/dev/null | wc -l | tr -d ' ' || echo 0)"
197242
198243
{
199244
echo "### 💅 Prettier Format Summary"
200245
echo
201246
echo "- **Ref:** \`${GITHUB_REF}\`"
202247
echo "- **Commit:** \`${GITHUB_SHA}\`"
203248
echo "- **Package manager:** \`${mgr}\`"
249+
echo "- **package.json:** \`${has_pkg}\`"
250+
echo "- **Files checked (tracked):** \`${files_count}\`"
204251
echo "- **Exit code:** \`${exit_code}\`"
205252
echo "- **ALLOW_WARNINGS:** \`${ALLOW_WARNINGS}\`"
206253
echo "- **Prettier available:** \`${prettier_present}\`"
207254
echo "- **Prettier source:** \`${prettier_source}\`"
255+
echo "- **Reported lines (warn/error):** \`${findings}\`"
208256
echo
209257
210258
if [[ "${prettier_present}" != "true" ]]; then
@@ -218,10 +266,10 @@ jobs:
218266
elif [[ "$exit_code" != "0" ]]; then
219267
echo "⚠️ Formatting issues detected."
220268
echo
221-
echo "**Top 80 lines:**"
269+
echo "**Top 120 lines:**"
222270
echo
223271
echo '```text'
224-
head -n 80 "${PRETTIER_REPORT}" || true
272+
head -n 120 "${PRETTIER_REPORT}" || true
225273
echo '```'
226274
echo
227275
echo "_Output truncated. Download artifacts for the full report._"
@@ -245,6 +293,7 @@ jobs:
245293
${{ env.PRETTIER_REPORT }}
246294
${{ env.PRETTIER_SUMMARY }}
247295
${{ env.NPM_DEBUG_LOG }}
296+
prettier-files.txt
248297
retention-days: 30
249298

250299
- name: ✅ Policy Gate

0 commit comments

Comments
 (0)