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.
This commit is contained in:
Danilo Reyes
2025-11-02 00:08:37 -06:00
parent 4c94793aba
commit b55ac51fe2
32 changed files with 470 additions and 171 deletions

102
scripts/install-hooks.sh Executable file
View File

@@ -0,0 +1,102 @@
#!/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 ""

131
scripts/lint.sh Executable file
View File

@@ -0,0 +1,131 @@
#!/usr/bin/env bash
# Unified linting script for all project code
# Can be run manually or via git hooks
set -e
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
FAILED=0
# Detect if we're in nix shell
if ! command -v ruff &> /dev/null && command -v nix &> /dev/null; then
echo "🔄 Entering nix development environment..."
exec nix develop -c bash "$0" "$@"
fi
echo "========================================="
echo "🔍 Reference Board Viewer - Code Linting"
echo "========================================="
echo ""
# Backend Python linting
echo -e "${BLUE}📦 Backend (Python)${NC}"
echo "-----------------------------------"
if [ -d "backend" ]; then
cd backend
# Ruff check
echo -n " Ruff check... "
if ruff check app/ 2>&1 | grep -q "All checks passed!"; then
echo -e "${GREEN}${NC}"
else
echo -e "${RED}${NC}"
echo ""
ruff check app/
FAILED=1
fi
# Ruff format check
echo -n " Ruff format... "
if ruff format --check app/ > /dev/null 2>&1; then
echo -e "${GREEN}${NC}"
else
echo -e "${RED}${NC}"
echo ""
echo "Run: cd backend && ruff format app/"
FAILED=1
fi
cd ..
else
echo -e "${YELLOW} ⚠ Backend directory not found, skipping${NC}"
fi
echo ""
# Frontend linting
echo -e "${BLUE}🎨 Frontend (TypeScript/Svelte)${NC}"
echo "-----------------------------------"
if [ -d "frontend" ] && [ -f "frontend/package.json" ]; then
cd frontend
# Check if node_modules exists
if [ ! -d "node_modules" ]; then
echo -e "${YELLOW} ⚠ node_modules not found, run 'npm install' first${NC}"
cd ..
else
# ESLint
echo -n " ESLint... "
if npm run lint > /dev/null 2>&1; then
echo -e "${GREEN}${NC}"
else
echo -e "${RED}${NC}"
echo ""
npm run lint
FAILED=1
fi
# Prettier check
if [ -f ".prettierrc" ]; then
echo -n " Prettier... "
if npx prettier --check src/ > /dev/null 2>&1; then
echo -e "${GREEN}${NC}"
else
echo -e "${RED}${NC}"
echo ""
echo "Run: cd frontend && npx prettier --write src/"
FAILED=1
fi
fi
# TypeScript check
echo -n " TypeScript... "
if npm run check > /dev/null 2>&1; then
echo -e "${GREEN}${NC}"
else
echo -e "${RED}${NC}"
echo ""
npm run check
FAILED=1
fi
cd ..
fi
else
echo -e "${YELLOW} ⚠ Frontend directory not found, skipping${NC}"
fi
echo ""
echo "========================================="
if [ $FAILED -eq 0 ]; then
echo -e "${GREEN}✅ All linting checks passed!${NC}"
echo "========================================="
exit 0
else
echo -e "${RED}❌ Some linting checks failed${NC}"
echo "========================================="
echo ""
echo "To auto-fix issues:"
echo " Backend: cd backend && ruff check --fix app/ && ruff format app/"
echo " Frontend: cd frontend && npx prettier --write src/"
exit 1
fi