Files
webref/scripts/install-hooks.sh
Danilo Reyes b55ac51fe2 feat: add unified linting scripts and git hooks for code quality enforcement
- Introduced `lint` and `lint-fix` applications in `flake.nix` for unified linting of backend (Python) and frontend (TypeScript/Svelte) code.
- Added `scripts/lint.sh` for manual linting execution.
- Created `scripts/install-hooks.sh` to set up git hooks for automatic linting before commits and optional tests before pushes.
- Updated `README.md` with instructions for using the new linting features and git hooks.
2025-11-02 00:08:37 -06:00

103 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Install git hooks for the project
set -e
HOOKS_DIR=".git/hooks"
SCRIPTS_DIR="scripts"
echo "Installing git hooks..."
echo ""
# Create hooks directory if it doesn't exist
mkdir -p "$HOOKS_DIR"
# Pre-commit hook
cat > "$HOOKS_DIR/pre-commit" << 'EOF'
#!/usr/bin/env bash
# Git pre-commit hook - runs linting before commit
echo "🔍 Running pre-commit linting..."
echo ""
# Try to use nix run if available, otherwise use script directly
if command -v nix &> /dev/null && [ -f "flake.nix" ]; then
# Use nix run for consistent environment
if ! nix run .#lint; then
echo ""
echo "❌ Linting failed. Fix errors or use --no-verify to skip."
echo " Auto-fix: nix run .#lint-fix"
exit 1
fi
else
# Fallback to script
if ! ./scripts/lint.sh; then
echo ""
echo "❌ Linting failed. Fix errors or use --no-verify to skip."
echo " Auto-fix: ./scripts/lint.sh --fix"
exit 1
fi
fi
echo ""
echo "✅ Pre-commit checks passed!"
EOF
chmod +x "$HOOKS_DIR/pre-commit"
echo "✓ Installed pre-commit hook"
# Pre-push hook (optional - runs tests)
cat > "$HOOKS_DIR/pre-push" << 'EOF'
#!/usr/bin/env bash
# Git pre-push hook - runs tests before push (optional)
# Comment out or remove if you want to push without running tests
echo "🧪 Running tests before push..."
echo ""
# Backend tests (if pytest is available)
if [ -d "backend" ] && command -v pytest &> /dev/null; then
cd backend
if ! pytest -xvs --tb=short; then
echo ""
echo "❌ Backend tests failed. Fix tests or use --no-verify to skip."
exit 1
fi
cd ..
fi
# Frontend tests (if npm test is available)
if [ -d "frontend/node_modules" ]; then
cd frontend
if ! npm test -- --run; then
echo ""
echo "❌ Frontend tests failed. Fix tests or use --no-verify to skip."
exit 1
fi
cd ..
fi
echo ""
echo "✅ All tests passed!"
EOF
chmod +x "$HOOKS_DIR/pre-push"
echo "✓ Installed pre-push hook (optional - runs tests)"
echo ""
echo "========================================="
echo "✅ Git hooks installed successfully!"
echo "========================================="
echo ""
echo "Hooks installed:"
echo " • pre-commit - Runs linting before commit"
echo " • pre-push - Runs tests before push (optional)"
echo ""
echo "To skip hooks when committing:"
echo " git commit --no-verify"
echo ""
echo "To uninstall:"
echo " rm .git/hooks/pre-commit .git/hooks/pre-push"
echo ""