"""Run project pre-commit checks for backend and frontend code.""" from __future__ import annotations import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent FRONTEND_DIR = ROOT / "frontend" BACKEND_DIR = ROOT / "backend" def run_step(command: list[str], *, cwd: Path | None = None) -> None: display_cmd = " ".join(command) step_cwd = cwd or ROOT print(f"\n>>> {display_cmd} (cwd={step_cwd})") result = subprocess.run(command, cwd=step_cwd, check=False) if result.returncode != 0: raise RuntimeError(f"Command failed: {display_cmd}") def main() -> int: steps: list[tuple[list[str], Path | None]] = [ ([sys.executable, "-m", "black", "--check", str(BACKEND_DIR)], None), ([sys.executable, "-m", "isort", "--check-only", str(BACKEND_DIR)], None), ([sys.executable, "-m", "pytest", str(BACKEND_DIR / "tests")], None), ] if (FRONTEND_DIR / "node_modules").exists(): steps.append( ( [ "npm", "--prefix", str(FRONTEND_DIR), "run", "lint", ], None, ) ) else: print("\n>>> Skipping frontend lint (frontend/node_modules not installed)") try: for command, cwd in steps: run_step(command, cwd=cwd) except RuntimeError as exc: print(f"\nPre-commit checks failed: {exc}") return 1 print("\nAll pre-commit checks passed.") return 0 if __name__ == "__main__": raise SystemExit(main())