import { readFile, rm } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { spawn } from "node:child_process"; const webRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); const runtimePath = path.join(webRoot, "e2e", ".runtime.json"); const pidPath = path.join(webRoot, "e2e", ".server.pid"); const binDir = path.join(webRoot, "e2e", ".bin"); async function killPid(pid: number) { if (!pid) return; if (process.platform === "win32") { await new Promise((resolve) => { const child = spawn("taskkill", ["/PID", String(pid), "/T", "/F"], { shell: true }); child.on("close", () => resolve()); child.on("error", () => resolve()); }); return; } try { process.kill(pid, "SIGKILL"); } catch { /* ignore */ } } export default async function globalTeardown() { let dataDir = ""; let servePid = 0; try { const info = JSON.parse(await readFile(runtimePath, "utf8")) as { dataDir?: string; servePid?: number; }; dataDir = info.dataDir ?? ""; servePid = info.servePid ?? 0; } catch { /* ignore */ } try { const pidRaw = (await readFile(pidPath, "utf8")).trim(); const pid = Number(pidRaw); if (Number.isFinite(pid) && pid > 0) servePid = pid; } catch { /* ignore */ } await killPid(servePid); if (dataDir) { await rm(dataDir, { recursive: true, force: true }); } await rm(runtimePath, { force: true }); await rm(pidPath, { force: true }); await rm(binDir, { recursive: true, force: true }); }