| 1 | import { cpSync, mkdirSync, writeFileSync } from "node:fs"; |
| 2 | import { execFileSync } from "node:child_process"; |
| 3 | import path from "node:path"; |
| 4 | import { fileURLToPath } from "node:url"; |
| 5 | import { buildIdentity } from "./app-memory-evidence.mjs"; |
| 6 | import { memoryProtocol, verifyIdentity } from "./app-memory-shards.mjs"; |
| 7 | const frontend = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); |
| 8 | const target = process.argv[2]; |
| 9 | const executionId = `${process.env.GITHUB_RUN_ID}:${process.env.GITHUB_RUN_ATTEMPT}`; |
| 10 | const protocol = memoryProtocol(process.env.REASONIX_APP_MEMORY_PROFILE ?? "full"); |
| 11 | if (!target || !process.env.GITHUB_RUN_ID || !process.env.GITHUB_RUN_ATTEMPT) throw new Error("prepare requires an output directory and workflow identity"); |
| 12 | // Vite removes this tracked placeholder; restore it before hashing the clean build. |
| 13 | const prefix = execFileSync("git", ["rev-parse", "--show-prefix"], { cwd: frontend, encoding: "utf8" }).trim(); |
| 14 | writeFileSync(path.join(frontend, "dist/.gitkeep"), execFileSync("git", ["show", `HEAD:${prefix}dist/.gitkeep`], { cwd: frontend })); |
| 15 | const identity = buildIdentity(frontend); |
| 16 | verifyIdentity(identity, identity); |
| 17 | if (identity.sourceSHA !== process.env.EXPECTED_SOURCE_SHA) throw new Error("prepared build is not the requested commit"); |
| 18 | mkdirSync(target, { recursive: true }); |
| 19 | cpSync(path.join(frontend, "dist"), path.join(target, "dist"), { recursive: true }); |
| 20 | writeFileSync(path.join(target, "identity.json"), JSON.stringify({ identity, executionId, protocol }, null, 2)); |
| 21 |