chore: add eslint/prettier gates and fix windows test failures

This commit is contained in:
wdawdwa committed 2026-08-22 15:16:47 +08:00
1 parent b366e16087
commit b47302e2ff
42 files changed
+3460 -843

No files matched your search

+41
View File
@@ -0,0 +1,41 @@
/**
* Git pre-commit 钩子(Node 版,Windows/Linux/macOS 通用)。
* 执行:磁盘上语法/完整性检查 + 生成物提交防护。
*/
"use strict";
const { execSync } = require("node:child_process");
function run(cmd) {
try {
return execSync(cmd, { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }).trim();
} catch {
return "";
}
}
// 1) 暂存区生成物防护:graft/、.codegraph/、.code-review-graph/、dist/ 等不允许进仓库。
const staged = run("git diff --cached --name-only").split("\n").filter(Boolean);
const blockedPrefixes = ["graft/", ".codegraph/", ".code-review-graph/", "dist/", ".ghbot-tmp/"];
const blocked = staged.filter((file) => blockedPrefixes.some((p) => file === p.slice(0, -1) || file.startsWith(p)));
if (blocked.length > 0) {
console.error("[pre-commit] BLOCKED:以下生成物/索引文件被暂存,已拒绝提交:");
blocked.forEach((f) => console.error(` - ${f}`));
console.error("请先 git rm --cached -r 它们。");
process.exit(1);
}
// 2) 磁盘上被修改过的 TS 文件跑一轮 tsc(增量,仅当本机装了 typescript)。
const changedTs = staged
.filter((f) => f.endsWith(".ts") || f.endsWith(".tsx"))
.map((f) => f.replace(/\\/g, "/"));
if (changedTs.length > 0) {
const tsc = run("npm run typecheck");
if (tsc && /error TS\d+/.test(tsc)) {
console.error("[pre-commit] TypeScript 类型错误,已阻止提交:");
console.error(tsc.split("\n").filter((l) => /error TS/.test(l)).slice(0, 20).join("\n"));
process.exit(1);
}
}
process.exit(0);
+32
View File
@@ -0,0 +1,32 @@
#!/usr/bin/env node
/**
* Windows 兼容的 husky 安装器(不使用 .sh 脚本)。
* 用本仓库自带的 Node 脚本把 husky 的 prepare 钩子写到 .git/hooks/pre-commit。
*/
"use strict";
const fs = require("node:fs");
const path = require("node:path");
const os = require("node:os");
const root = path.resolve(__dirname, "..");
const gitDir = path.join(root, ".git");
const hooksDir = path.join(gitDir, "hooks");
const hookPath = path.join(hooksDir, "pre-commit");
if (!fs.existsSync(gitDir)) {
console.warn("[install-husky] 未找到 .git 目录(例如在 CI 中),跳过 pre-commit 钩子安装。");
process.exit(0);
}
fs.mkdirSync(hooksDir, { recursive: true });
const separator = os.platform() === "win32" ? "\\" : "/";
const nodeBinary = process.execPath;
const hookScript = [
"#!/bin/sh",
`node "${nodeBinary.replace(/\\/g, "/")}" "${path.join(root, "scripts", "husky-pre-commit.cjs").replace(/\\/g, "/")}"`,
].join("\n");
fs.writeFileSync(hookPath, `${hookScript}\n`, { mode: 0o755 });
console.log(`[install-husky] 已写入 git pre-commit 钩子: ${hookPath}`);