modify framework
This commit is contained in:
@@ -1,14 +1,10 @@
|
||||
import json
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
CHECKS = ("lint", "build", "test")
|
||||
|
||||
|
||||
def _repo_root(cwd: Path) -> Path:
|
||||
try:
|
||||
root = subprocess.check_output(
|
||||
@@ -22,25 +18,11 @@ def _repo_root(cwd: Path) -> Path:
|
||||
return Path(root)
|
||||
|
||||
|
||||
def _load_scripts(root: Path) -> dict[str, str]:
|
||||
package_json = root / "package.json"
|
||||
if not package_json.exists():
|
||||
return {}
|
||||
|
||||
try:
|
||||
package = json.loads(package_json.read_text(encoding="utf-8"))
|
||||
except json.JSONDecodeError as exc:
|
||||
_deny(f"Invalid package.json: {exc}")
|
||||
raise SystemExit(0) from exc
|
||||
|
||||
scripts = package.get("scripts", {})
|
||||
if not isinstance(scripts, dict):
|
||||
return {}
|
||||
return {str(name): str(command) for name, command in scripts.items()}
|
||||
|
||||
|
||||
def _is_git_commit(command: str) -> bool:
|
||||
return re.search(r"\bgit(?:\s+(?:-[A-Za-z]\s+\S+|--[A-Za-z0-9-]+(?:=\S+)?))*\s+commit\b", command) is not None
|
||||
return re.search(
|
||||
r"^\s*git(?:\s+(?:-[A-Za-z]\s+\S+|--[A-Za-z0-9-]+(?:=\S+)?))*\s+commit\b",
|
||||
command,
|
||||
) is not None
|
||||
|
||||
|
||||
def _deny(reason: str) -> None:
|
||||
@@ -64,26 +46,22 @@ def _tail(text: str, limit: int = 1200) -> str:
|
||||
return text[-limit:]
|
||||
|
||||
|
||||
def _run_checks(root: Path, scripts: dict[str, str]) -> str | None:
|
||||
npm = shutil.which("npm") or shutil.which("npm.cmd")
|
||||
if npm is None:
|
||||
return "npm was not found, so pre-commit checks could not run."
|
||||
def _build_pre_commit_commands(root: Path) -> list[list[str]]:
|
||||
return [
|
||||
[sys.executable, "-m", "unittest", "discover", "-s", "scripts", "-p", "test_*.py"],
|
||||
[sys.executable, "scripts/validate_workspace.py"],
|
||||
]
|
||||
|
||||
for check in CHECKS:
|
||||
if check not in scripts:
|
||||
continue
|
||||
result = subprocess.run(
|
||||
[npm, "run", check],
|
||||
cwd=root,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
|
||||
def _run_checks(root: Path) -> str | None:
|
||||
for command in _build_pre_commit_commands(root):
|
||||
result = subprocess.run(command, cwd=root, capture_output=True, text=True)
|
||||
if result.returncode != 0:
|
||||
details = _tail(result.stdout + "\n" + result.stderr)
|
||||
label = " ".join(command)
|
||||
if details:
|
||||
return f"npm run {check} failed:\n{details}"
|
||||
return f"npm run {check} failed with exit code {result.returncode}."
|
||||
return f"{label} failed:\n{details}"
|
||||
return f"{label} failed with exit code {result.returncode}."
|
||||
|
||||
return None
|
||||
|
||||
@@ -100,7 +78,7 @@ def main() -> int:
|
||||
|
||||
cwd = Path(payload.get("cwd") or Path.cwd())
|
||||
root = _repo_root(cwd)
|
||||
failure = _run_checks(root, _load_scripts(root))
|
||||
failure = _run_checks(root)
|
||||
if failure:
|
||||
_deny(f"PRE-COMMIT CHECKS: {failure}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user