import { readFileSync, writeFileSync, mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { execFileSync } from "node:child_process"; const doc = JSON.parse(readFileSync(process.argv[2], "utf8")); if ( doc.date !== "2026-11-15" || doc.sections?.length !== 6 || doc.sections.some((s) => !Array.isArray(s.stories)) ) throw Error( "Expected checked first edition for 2026-11-15 with six sections.", ); const esc = (s) => "'" + s.replaceAll("'", "''") + "'"; const temp = mkdtempSync(join(tmpdir(), "digest-seed-")); try { const file = join(temp, "reviewed.sql"); const text = JSON.stringify(doc), chunks = Array.from(text.matchAll(/[\s\S]{1,12000}/gu), (x) => x[0]); const staging = "staging-" + crypto.randomUUID(); const queries = [ "INSERT INTO reviewed(issue_date,content) VALUES (" + esc(staging) + ", '');", ...chunks.map( (chunk) => "UPDATE reviewed SET content=content || " + esc(chunk) + " WHERE issue_date=" + esc(staging) + ";", ), "INSERT INTO reviewed(issue_date,content) SELECT " + esc(doc.date) + ",content FROM reviewed WHERE issue_date=" + esc(staging) + " ON CONFLICT(issue_date) DO UPDATE SET content=excluded.content WHERE NOT EXISTS (SELECT 1 FROM editions WHERE issue_date=excluded.issue_date);", "DELETE FROM reviewed WHERE issue_date=" + esc(staging) + ";", ]; writeFileSync(file, queries.join("\n"), { mode: 0o600 }); execFileSync( "wrangler", ["d1", "execute", "alexander-morning-digest", "--remote", "--file", file], { stdio: "inherit" }, ); } finally { rmSync(temp, { recursive: true, force: true }); }