pre-commit 727 B

123456789101112131415161718192021222324252627282930
  1. #!/bin/sh
  2. # Pre-commit hook: auto-formats staged files and ensures tests pass before committing
  3. echo "Running pre-commit checks..."
  4. # 1. Auto-format staged files
  5. STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(ts|tsx|js|jsx|json|md)$')
  6. if [ -n "$STAGED_FILES" ]; then
  7. echo "Auto-formatting..."
  8. echo "$STAGED_FILES" | xargs deno fmt
  9. if [ $? -ne 0 ]; then
  10. echo ""
  11. echo "Auto-format failed. Fix the issues manually and try again."
  12. exit 1
  13. fi
  14. echo "$STAGED_FILES" | xargs git add
  15. fi
  16. # 2. Run all tests
  17. echo "Running tests..."
  18. deno task test
  19. if [ $? -ne 0 ]; then
  20. echo ""
  21. echo "Tests failed. Fix the failing tests before committing."
  22. exit 1
  23. fi
  24. echo "All checks passed."