import { cp, mkdtemp, rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { spawnSync } from "node:child_process"; /** * 在临时目录中验证发布包的运行时依赖闭包。 * 冻结安装只能证明锁文件一致,不能证明打包时没有遗漏 workspace package。 */ export async function verifyRuntimePreflight(releaseDirectory) { const temporary = await mkdtemp(path.join(os.tmpdir(), "personal-brand-runtime-")); const apiDirectory = path.join(temporary, "apps/api"); try { await cp(releaseDirectory, temporary, { recursive: true }); run( "pnpm", [ "--filter", "@personal-brand/api...", "--filter", "@personal-brand/taro...", "--filter", "@personal-brand/template-acheng-campus-food", "--filter", "@personal-brand/template-linjing-editorial", "--filter", "@personal-brand/template-atelier-solopreneur", "install", "--frozen-lockfile", "--prefer-offline", "--ignore-scripts", ], temporary, ); run("pnpm", ["--filter", "@personal-brand/api", "exec", "prisma", "generate"], temporary); run( process.execPath, ["--input-type=module", "-e", "await import('./dist/main.js')"], apiDirectory, { NODE_ENV: "test" }, ); } finally { await rm(temporary, { recursive: true, force: true }); } } function run(command, args, cwd, extraEnvironment = {}) { const result = spawnSync(command, args, { cwd, stdio: "inherit", env: { ...process.env, ...extraEnvironment }, }); if (result.status !== 0) throw new Error( `${command} failed during release runtime preflight with exit ${result.status}`, ); }