1
0

pre-commit 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/bin/sh
  2. # Pre-commit hook: runs Ruff on staged Python files, frontend tests and build on staged TS/TSX/CSS files
  3. #
  4. # Install: cp scripts/pre-commit .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit
  5. set -e
  6. # Colors
  7. RED='\033[0;31m'
  8. GREEN='\033[0;32m'
  9. YELLOW='\033[0;33m'
  10. NC='\033[0m'
  11. # --- Ruff lint on staged Python files ---
  12. STAGED_PY=$(git diff --cached --name-only --diff-filter=ACM -- '*.py')
  13. if [ -n "$STAGED_PY" ]; then
  14. printf "${YELLOW}Running Ruff on staged Python files...${NC}\n"
  15. if command -v ruff >/dev/null 2>&1; then
  16. if ! echo "$STAGED_PY" | xargs ruff check; then
  17. printf "${RED}Ruff found issues. Fix them or run 'ruff check --fix' then re-stage.${NC}\n"
  18. exit 1
  19. fi
  20. printf "${GREEN}Ruff passed.${NC}\n"
  21. else
  22. printf "${YELLOW}Ruff not installed, skipping Python lint. Install with: pip install ruff${NC}\n"
  23. fi
  24. fi
  25. # --- Frontend checks on staged UI files ---
  26. STAGED_FE=$(git diff --cached --name-only --diff-filter=ACM -- 'frontend/src/**/*.ts' 'frontend/src/**/*.tsx' 'frontend/src/**/*.css' 'frontend/index.html' 'frontend/vite.config.ts' 'frontend/tsconfig*.json' 'frontend/tailwind.config.*' 'frontend/components.json')
  27. if [ -n "$STAGED_FE" ]; then
  28. if [ -d "frontend/node_modules" ]; then
  29. # Run tests first
  30. printf "${YELLOW}Running frontend tests...${NC}\n"
  31. if ! (cd frontend && npx vitest run --reporter=dot 2>&1); then
  32. printf "${RED}Frontend tests failed. Fix them before committing.${NC}\n"
  33. exit 1
  34. fi
  35. printf "${GREEN}Frontend tests passed.${NC}\n"
  36. # Build production bundle
  37. printf "${YELLOW}Building frontend...${NC}\n"
  38. if ! (cd frontend && npm run build 2>&1); then
  39. printf "${RED}Frontend build failed. Fix errors before committing.${NC}\n"
  40. exit 1
  41. fi
  42. printf "${GREEN}Frontend build succeeded.${NC}\n"
  43. # Stage the updated build output so it's included in this commit
  44. git add static/dist/
  45. else
  46. printf "${YELLOW}frontend/node_modules not found, skipping tests and build. Run: cd frontend && npm install${NC}\n"
  47. fi
  48. fi