#!/usr/bin/env bash # Stop hook: runs quick quality checks and outputs JSON result. # 轻量检查(<10s) # 残留扫描由 check-completeness.sh(PostToolUse)按文件处理,此处不重复。 set -euo pipefail cd "${AGENT_PROJECT_DIR:-$(pwd)}" # Suppress during brainstorming / exploratory sessions if [ -f ".trae/.suppress-hooks" ]; then echo '{"status":"ok","mode":"suppressed"}' exit 0 fi HAS_ERRORS=0 # All diagnostics go to stderr { echo '=== [STOP HOOK QUALITY GATE] ===' # ---- 1/3: TypeScript type check ---- echo '[1/3] TypeScript 类型检查...' APP_DIR="everything-is-suitable-uniapp" if [ -f "${APP_DIR}/node_modules/.bin/tsc" ]; then if (cd "${APP_DIR}" && npx tsc --noEmit --pretty false 2>&1); then echo ' tsc: PASS' else echo ' tsc: FAIL' HAS_ERRORS=1 fi else echo ' tsc: SKIP(未安装依赖,运行 npm install)' fi # ---- 2/3: TODO/FIXME 残留扫描 ---- echo '[2/3] 代码残留扫描(TODO/FIXME/skeleton)...' FOUND=$(grep -rn 'TODO:\|FIXME:\|not implemented.*throw' "${APP_DIR}/src" --include='*.ts' --include='*.vue' --include='*.js' 2>/dev/null | grep -v 'node_modules' | grep -v '.test.' | head -10 || true) if [ -z "$FOUND" ]; then echo ' 残留扫描: PASS' else echo ' 残留扫描: 发现以下残留标记:' echo "$FOUND" HAS_ERRORS=1 fi # ---- 3/3: 测试文件完整性检查 ---- echo '[3/3] 测试文件一致性检查...' # 检查 src 中每个 .ts/.vue 文件是否有关联测试文件 ORPHANED=0 while IFS= read -r src_file; do [ -z "$src_file" ] && continue base=$(basename "$src_file" .ts) base=$(basename "$base" .vue) dir=$(dirname "$src_file") test_file="${dir}/__tests__/${base}.test.ts" if [ ! -f "$test_file" ]; then # 跳过 index.vue 等特殊文件 case "$base" in index|main|App) continue ;; esac # 跳过类型定义文件 case "$src_file" in */types/*) continue ;; esac ORPHANED=$((ORPHANED + 1)) fi done < <(find "${APP_DIR}/src" -name '*.ts' -o -name '*.vue' 2>/dev/null | head -100) if [ "$ORPHANED" -eq 0 ]; then echo ' 测试文件一致性: PASS' else echo " 测试文件一致性: ${ORPHANED} 个源文件缺少关联测试(仅供参考,非强制)" fi # ---- Summary ---- if [ "$HAS_ERRORS" -eq 0 ]; then echo '=== [STOP HOOK: ALL PASS] ===' else echo '=== [STOP HOOK: ERRORS FOUND] ===' fi } >&2 # Output final status as JSON if [ "$HAS_ERRORS" -eq 0 ]; then echo '{"status":"ok"}' else echo '{"status":"error"}' fi