chore: 提交和推送前增加前端 TypeScript 检查

与 Docker 构建的 tsc -b 对齐,避免类型错误在服务器构建才暴露。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
yml2213
2026-09-10 11:45:35 +08:00
co-authored by Cursor
parent c1f4e7c7ae
commit 874a95dd6f
7 changed files with 92 additions and 6 deletions
+37
View File
@@ -0,0 +1,37 @@
#!/usr/bin/env bash
# 提交前做轻量检查:Python 格式 + 前端 TypeScript。
# 全量 pytest 放在 pre-push,避免每次 commit 都跑一遍。
set -euo pipefail
cd "$(git rev-parse --show-toplevel)"
if [ "${SKIP_TESTS:-0}" = "1" ]; then
echo "[pre-commit] SKIP_TESTS=1,跳过检查"
exit 0
fi
staged="$(git diff --cached --name-only --diff-filter=ACMR)"
python_files=()
frontend_changed=0
while IFS= read -r path; do
[ -z "$path" ] && continue
case "$path" in
*.py)
[ -f "$path" ] && python_files+=("$path")
;;
web/frontend/*)
frontend_changed=1
;;
esac
done <<< "$staged"
if [ "${#python_files[@]}" -gt 0 ]; then
echo "[pre-commit] 检查 Python 格式..."
uv run --group dev ruff format --check "${python_files[@]}"
echo "[pre-commit] Python 格式通过"
fi
if [ "$frontend_changed" -eq 1 ]; then
.githooks/run-frontend-tsc.sh
fi
+16 -3
View File
@@ -17,18 +17,27 @@ if [ "${SKIP_TESTS:-0}" = "1" ]; then
fi
python_files=()
frontend_changed=0
while read -r local_ref local_sha remote_ref remote_sha; do
[ -z "$local_sha" ] && continue
if [ "$local_sha" = "0000000000000000000000000000000000000000" ]; then
continue
fi
if [ "$remote_sha" = "0000000000000000000000000000000000000000" ]; then
changed_paths="$(git diff-tree --no-commit-id --name-only -r --root "$local_sha" -- '*.py')"
changed_paths="$(git diff-tree --no-commit-id --name-only -r --root "$local_sha")"
else
changed_paths="$(git diff --name-only "$remote_sha..$local_sha" -- '*.py')"
changed_paths="$(git diff --name-only "$remote_sha..$local_sha")"
fi
while IFS= read -r path; do
[ -n "$path" ] && [ -f "$path" ] && python_files+=("$path")
[ -z "$path" ] && continue
case "$path" in
*.py)
[ -f "$path" ] && python_files+=("$path")
;;
web/frontend/*)
frontend_changed=1
;;
esac
done <<< "$changed_paths"
done <<< "$push_lines"
@@ -38,6 +47,10 @@ if [ "${#python_files[@]}" -gt 0 ]; then
echo "[pre-push] Python 格式通过"
fi
if [ "$frontend_changed" -eq 1 ]; then
.githooks/run-frontend-tsc.sh
fi
if [ "${TYPECHECK:-0}" = "1" ]; then
echo "[pre-push] 运行 Pyright 类型检查..."
uv run --group dev pyright
+22
View File
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
# 与 Docker 构建第一步对齐:tsc -b。失败则阻止提交/推送。
set -euo pipefail
ROOT="$(git rev-parse --show-toplevel)"
FRONTEND="$ROOT/web/frontend"
if ! command -v npm >/dev/null 2>&1; then
echo "[git-hook] 未找到 npm,无法做前端 TypeScript 检查。"
echo "请安装 Node.js,或紧急用 git push --no-verify 绕过。"
exit 1
fi
cd "$FRONTEND"
if [ ! -x node_modules/.bin/tsc ]; then
echo "[git-hook] 前端依赖缺失,正在 npm ci..."
npm ci
fi
echo "[git-hook] 检查前端 TypeScript..."
npm exec -- tsc -b --pretty false
echo "[git-hook] TypeScript 通过"