|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +echo "=== Pre-commit checks ===" |
| 5 | + |
| 6 | +# ── Stage 0: lint-staged (format staged files) ────────────────────── |
| 7 | +echo "[0/8] Formatting staged files..." |
| 8 | +npx lint-staged |
| 9 | + |
| 10 | +# ── Stage 1: Lockfile sync ────────────────────────────────────────── |
| 11 | +echo "[1/8] Checking lockfile sync..." |
| 12 | +pnpm install --frozen-lockfile --ignore-scripts 2>/dev/null || { |
| 13 | + echo "❌ pnpm-lock.yaml is out of sync. Run 'pnpm install' and stage the lockfile." |
| 14 | + exit 1 |
| 15 | +} |
| 16 | + |
| 17 | +# ── Stage 2: Type-check (affected packages) ───────────────────────── |
| 18 | +echo "[2/8] Type-checking..." |
| 19 | +npx turbo typecheck |
| 20 | + |
| 21 | +# ── Stage 3: Lint (affected packages) ─────────────────────────────── |
| 22 | +echo "[3/8] Linting..." |
| 23 | +npx turbo lint |
| 24 | + |
| 25 | +# ── Stage 4: Build ────────────────────────────────────────────────── |
| 26 | +echo "[4/8] Building..." |
| 27 | +npx turbo build |
| 28 | + |
| 29 | +# ── Stage 5: Tests ────────────────────────────────────────────────── |
| 30 | +echo "[5/8] Running tests..." |
| 31 | +npx turbo test |
| 32 | + |
| 33 | +# ── Stage 6: Hardcoded secrets check ──────────────────────────────── |
| 34 | +echo "[6/8] Checking for hardcoded secrets..." |
| 35 | +SECRETS_FOUND=0 |
| 36 | +git diff --cached --name-only --diff-filter=d | while read -r file; do |
| 37 | + if [[ "$file" =~ \.(ts|tsx|js|jsx|mjs|cjs)$ ]]; then |
| 38 | + if git show ":$file" 2>/dev/null | grep -inE '(api[_-]?key|secret[_-]?key|password|token)\s*[:=]\s*["\x27][A-Za-z0-9+/=]{8,}' | grep -v '// eslint-disable' | grep -v 'process\.env' | grep -v 'import' >/dev/null 2>&1; then |
| 39 | + echo "⚠️ Possible hardcoded secret in: $file" |
| 40 | + SECRETS_FOUND=1 |
| 41 | + fi |
| 42 | + fi |
| 43 | +done |
| 44 | +if [ "$SECRETS_FOUND" -eq 1 ]; then |
| 45 | + echo "❌ Potential secrets detected. Use environment variables instead." |
| 46 | + exit 1 |
| 47 | +fi |
| 48 | + |
| 49 | +# ── Stage 7: Console.log warnings ─────────────────────────────────── |
| 50 | +echo "[7/8] Checking for console.log statements..." |
| 51 | +CONSOLE_FOUND=0 |
| 52 | +git diff --cached --name-only --diff-filter=d | while read -r file; do |
| 53 | + if [[ "$file" =~ \.(ts|tsx)$ ]] && [[ ! "$file" =~ \.test\. ]] && [[ ! "$file" =~ \.spec\. ]]; then |
| 54 | + if git show ":$file" 2>/dev/null | grep -n 'console\.log\b' >/dev/null 2>&1; then |
| 55 | + echo "⚠️ console.log found in: $file" |
| 56 | + CONSOLE_FOUND=1 |
| 57 | + fi |
| 58 | + fi |
| 59 | +done |
| 60 | +if [ "$CONSOLE_FOUND" -eq 1 ]; then |
| 61 | + echo "⚠️ Warning: console.log statements found (non-blocking)" |
| 62 | +fi |
| 63 | + |
| 64 | +# ── Stage 8: Changeset requirement ────────────────────────────────── |
| 65 | +# Check if any publishable package files changed without a changeset |
| 66 | +CHANGED_PACKAGES=$(git diff --cached --name-only | grep -E '^packages/(parser|cli|eslint-plugin)/src/' || true) |
| 67 | +if [ -n "$CHANGED_PACKAGES" ]; then |
| 68 | + CHANGESETS=$(git diff --cached --name-only | grep -E '^\.changeset/.*\.md$' || true) |
| 69 | + if [ -z "$CHANGESETS" ]; then |
| 70 | + echo "⚠️ Warning: Source files changed in publishable packages but no changeset found." |
| 71 | + echo " Run 'pnpm changeset' to create one before merging to main." |
| 72 | + fi |
| 73 | +fi |
| 74 | + |
| 75 | +echo "✅ All pre-commit checks passed" |
0 commit comments