Repository navigation
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·61 lines (54 loc) · 2.72 KB
/
Copy pathpre-commit
File metadata and controls
executable file
·61 lines (54 loc) · 2.72 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
60
61
#!/usr/bin/env bash
#
# Pre-commit hook: auto-format, auto-fix lint, then run fast quality checks.
# Heavier build+test gate is in .githooks/pre-push (mise run ci).
# Full CI (docs, e2e, browser smoke) runs in the configured CI provider, not here.
# Install: git config core.hooksPath .githooks
#
# Philosophy: formatting and safe lint nits are fixed automatically so agents
# and humans only need to manually fix what the tooling cannot (real type
# errors, non-auto-fixable lint, failing tests).
#
set -euo pipefail
# Snapshot the list of originally-staged files so we can re-stage them after
# auto-fix. Files not in this list but modified by biome stay unstaged — we
# only want the commit to carry fixes to files the caller intended to commit.
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR)
# Lockfile-sync gate: if any package.json was staged, run `bun install` so
# bun.lock stays in sync with package.json in the SAME commit. Without this,
# a partial commit (package.json without bun.lock) can land with stale lock,
# and CI's --frozen-lockfile path catches the divergence only after the push
# — late signal we want to short-circuit at commit time.
PKG_JSON_CHANGED=$(echo "$STAGED_FILES" | grep -E '(^|/)package\.json$' || true)
if [ -n "$PKG_JSON_CHANGED" ]; then
echo "🔄 Pre-commit lockfile sync: package.json changed, running bun install..."
bun install >/dev/null
# Always append bun.lock to STAGED_FILES — if bun install didn't change it,
# `git add bun.lock` is a no-op; if it did, we capture the sync in the
# commit. The auto-stage loop below picks it up uniformly.
STAGED_FILES="$STAGED_FILES"$'\n'"bun.lock"
fi
echo "🔧 Pre-commit auto-fix: bun run format..."
bun run format >/dev/null
# biome check --write applies the subset of lint rules that biome knows how to
# fix safely (unused imports, simple organization, etc.). Unfixable lint is
# passed through to the final `bun run check` gate below, which will fail
# commits with real issues.
echo "🔧 Pre-commit auto-fix: biome check --write (safe lint fixes)..."
bunx biome check --write . >/dev/null || true
# Re-stage any originally-staged files that the auto-fix touched so the
# commit carries the fixed version. Files outside this list that biome
# happened to modify stay in the worktree, unstaged.
if [ -n "$STAGED_FILES" ]; then
while IFS= read -r f; do
if [ -f "$f" ]; then
# `-f` lets us re-stage intentionally force-added paths (e.g. files
# under gitignored trees like `_dot/` that the caller explicitly
# staged). Without `-f`, those re-adds fail and break the commit.
git add -f "$f"
fi
done <<< "$STAGED_FILES"
fi
echo "🔍 Running pre-commit check (bun run check)..."
bun run check
echo "✅ All checks passed."