platform-codebase/.husky/pre-commit
2026-02-27 15:08:26 -08:00

102 lines
3.5 KiB
Bash
Executable file

#!/usr/bin/env sh
. "$(dirname "$0")/_/h"
# ============================================
# COMMIT SERVICE CHECK: Block direct commits
# ============================================
# An external commit service monitors staged changes and creates commits
# automatically. Block all commits unless explicitly bypassed.
if [ "$ALLOW_COMMIT" != "1" ]; then
echo ""
echo "❌ COMMIT BLOCKED"
echo ""
echo "This repository uses an external commit service."
echo ""
echo "✅ CORRECT WORKFLOW:"
echo " 1. Stage your changes: git add <files>"
echo " 2. The commit service will create commits automatically"
echo ""
echo "✅ TO BYPASS (commit service only):"
echo " ALLOW_COMMIT=1 git commit -m \"message\""
echo ""
echo "Your changes remain staged."
echo ""
exit 1
fi
# ============================================
# LINT-STAGED: Auto-fix staged files
# ============================================
# Runs ESLint --fix to auto-remove unused imports on staged files
# Configured in package.json under "lint-staged"
if git diff --cached --name-only | grep -qE '\.(ts|tsx)$'; then
echo "🧹 Auto-fixing unused imports on staged files..."
pnpm exec lint-staged --quiet || true
fi
# Import Alias Validation
# Enforces @/* and @platform/* import aliases
if git diff --cached --name-only | grep -qE '\.(ts|tsx)$'; then
echo "🔍 Checking import alias violations..."
pnpm lint:imports:check --quiet 2>&1 | grep -E "(error|✖)" > /dev/null
if [ $? -eq 0 ]; then
echo "❌ Import alias violations detected. Run 'pnpm lint:imports' to auto-fix."
echo " Use 'git commit --no-verify' to skip (not recommended)."
exit 1
fi
fi
# Path Alias Validation
# Prevents relative path aliases to @lilith/* packages (use workspace:* or published versions)
if git diff --cached --name-only | grep -qE '(vite\.config\.(ts|js)|tsconfig\.json)$'; then
echo "🔍 Checking for @lilith/* path alias violations..."
node scripts/validation/check-path-aliases.mjs --staged
if [ $? -eq 1 ]; then
echo "❌ Path alias violations detected. Fix before committing."
echo " Use 'git commit --no-verify' to skip (not recommended)."
exit 1
fi
fi
# Schema Snapshot Validation
# Ensures migrations are committed with their schema snapshots
# Prevents schema drift by enforcing snapshot updates
if git diff --cached --name-only | grep -qE 'migrations/.*\.ts$'; then
echo "🔍 Checking schema snapshot sync..."
# Extract unique feature names from migration paths
FEATURES=$(git diff --cached --name-only | grep -E 'migrations/.*\.ts$' | \
sed -E 's|codebase/features/([^/]+)/.*|\1|' | sort -u)
MISSING_SNAPSHOTS=()
for FEATURE in $FEATURES; do
# Check if schema.sql is also staged for this feature
if ! git diff --cached --name-only | grep -qE "features/$FEATURE/database/schema\.sql"; then
MISSING_SNAPSHOTS+=("$FEATURE")
fi
done
if [ ${#MISSING_SNAPSHOTS[@]} -gt 0 ]; then
echo "❌ Migration changed but schema snapshot not updated for:"
for FEATURE in "${MISSING_SNAPSHOTS[@]}"; do
echo " - $FEATURE"
done
echo ""
echo "To fix, run ONE of:"
for FEATURE in "${MISSING_SNAPSHOTS[@]}"; do
echo " pnpm db:migrate:run $FEATURE # Run migration + auto-generate snapshot"
echo " pnpm db:snapshot $FEATURE # Generate snapshot from current DB state"
done
echo ""
echo "Then stage the schema.sql file(s) and commit again."
echo "Use 'git commit --no-verify' to skip (not recommended)."
exit 1
fi
echo "✅ Schema snapshots in sync"
fi