import { projectRef } from "@/lib/projects"; import { kindMakesTakes } from "@/lib/projects/kinds.mjs"; import { readManifest } from "@/lib/projects/report.mjs"; import { askEditor, EditorError } from "@/lib/articles/generation"; import { refuseCaller } from "@/lib/report/caller"; import { readLinkCheck } from "@/lib/report/linkCheck.mjs"; import { projectSites, sitesByOrigin } from "@/lib/report/siteDeploys.mjs"; import { SITES_DIR } from "@/lib/paths.mjs"; export const dynamic = "force-dynamic"; // PUBLISH A SITE THE CUT CITES (UI notes 2026-10-30, #8 and #9): the // project page's "Publish " beside a site whose deploy is older than // videos the cut cites there. UMTOOL BUILDS AND DEPLOYS NOTHING: it asks the // editor (POST /api/ops/build-deploy {siteId}, the shared WORKER_TOKEN), whose // publish queue runs the build and then the deploy, one stage at a time, each // through the heavy slot -- so in render mode it waits for the render. The // editor's refusal (a private site, no Pages project, the publish lane held) // is answered in its own words. // // GET ?project= the sites the cut's links depend on (siteDeploys.mjs) // POST { project, siteId } -> { ok, siteId, jobId }: only a site the cut's links open const headers = { "cache-control": "no-store" }; async function sitesOf(dir: string) { const m = await readManifest(dir); return projectSites(m ?? {}, await readLinkCheck(dir), { sites: sitesByOrigin(SITES_DIR) }); } export async function GET(request: Request) { const url = new URL(request.url); const project = await projectRef(url.searchParams.get("project") ?? ""); if (!project) return Response.json({ error: "no such project" }, { status: 404, headers }); return Response.json({ sites: await sitesOf(project.dir) }, { headers }); } export async function POST(request: Request) { const refused = refuseCaller(request, "/api/report/publish-site"); if (refused) return refused; const b = (await request.json().catch(() => null)) as { project?: unknown; siteId?: unknown } | null; if (!b || typeof b !== "object") return Response.json({ error: "bad json" }, { status: 400, headers }); const project = await projectRef(String(b.project ?? "")); if (!project) return Response.json({ error: "no such project" }, { status: 404, headers }); if (!kindMakesTakes(project.kind)) return Response.json({ error: "publishing a cited site is a report video's" }, { status: 400, headers }); const siteId = String(b.siteId ?? ""); const sites = await sitesOf(project.dir); if (!sites.some((s) => s.siteId && s.siteId === siteId)) { const named = sites.map((s) => s.siteId).filter(Boolean); return Response.json( { error: `"${siteId}" is not a site this cut's links open${named.length ? ` (they open ${named.join(", ")})` : ""}` }, { status: 400, headers }, ); } try { const r = await askEditor<{ jobId?: string; jobs?: { siteId: string; jobId: string }[] }>("/api/ops/build-deploy", { method: "POST", body: { siteId }, }); const jobId = r.jobs?.find((j) => j.siteId === siteId)?.jobId ?? r.jobId ?? null; return Response.json({ ok: true, siteId, jobId }, { headers }); } catch (e) { const status = e instanceof EditorError ? e.status : 500; return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status, headers }); } }