fix: harden Base manifest recovery

This commit is contained in:
golem
2026-08-21 16:16:12 -06:00
parent 90b0a11b8a
commit 6dee960d15
7 changed files with 119 additions and 32 deletions
+12 -8
View File
@@ -1,4 +1,4 @@
import { access, readFile, rename } from "node:fs/promises";
import { link, readFile, rm } from "node:fs/promises";
import { join, resolve } from "node:path";
import { pathToFileURL } from "node:url";
@@ -14,7 +14,7 @@ export async function selectManifest({ root = process.cwd(), network }) {
return { source, active };
}
export async function archiveManifest({ root = process.cwd(), network, now = new Date() }) {
export async function archiveManifest({ root = process.cwd(), network, now = new Date(), io = {} }) {
if (network !== "baseSepolia") throw new Error("only baseSepolia may be archived");
const spec = networkSpec(network);
const source = join(root, "deployments", spec.canonical);
@@ -23,16 +23,20 @@ export async function archiveManifest({ root = process.cwd(), network, now = new
const timestamp = now.toISOString().replace(/[-:.]/g, "");
if (!/^\d{8}T\d{9}Z$/.test(timestamp)) throw new Error("archive timestamp is invalid");
const target = join(root, "deployments", `base-sepolia.${timestamp}.json`);
const operations = { link, rm, ...io };
try {
await access(target);
await operations.link(source, target);
} catch (error) {
if (error.code === "ENOENT") {
await rename(source, target);
return { source, path: target };
}
if (error.code === "EEXIST") throw new Error("refusing to overwrite an existing Base Sepolia archive");
throw error;
}
throw new Error("refusing to overwrite an existing Base Sepolia archive");
try {
await operations.rm(source);
} catch (error) {
await operations.rm(target, { force: true }).catch(() => {});
throw error;
}
return { source, path: target };
}
export async function runSelectCli(argv, { root = process.cwd(), log = console.log } = {}) {