import { NextResponse } from "next/server"; import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { listSiteIds } from "yt-dlp-transcript-common/lib/site"; import { enqueueAsks, lastJobOf, refusalsOf } from "../_publish"; import { OpsInputError, ops, optBool, reqSiteIds } from "../_lib"; export const dynamic = "force-dynamic"; // AN ALIAS (release 18): each site's build, forced, the index first when it is stale. The body and the answer are the // route's old ones; the work is publish stages on the publish queue // (../_publish.ts), as `POST /api/ops/publish` {"verb": "build"} runs them. // // `skipData` is accepted and ignored (the data is the index stage, shared by // every site); `all` builds every site. → { ok, jobs: [{siteId, jobId}], // skipped: [{siteId, reason}] } (+ `jobId` when one site), as before. `all` // used to answer ONE job (build-all); it answers the per-site list now, and // its top-level `jobId` is the run's LAST job (`--wait` follows `jobs` anyway). export async function POST(request: Request) { return ops( request, ["siteId", "siteIds", "all", "skipData", "skipArchives"], async (body) => { const skipArchives = optBool(body, "skipArchives"); optBool(body, "skipData"); let ids: string[]; if (optBool(body, "all")) { if (body.siteIds !== undefined || body.siteId !== undefined) { throw new OpsInputError('send either "siteId"/"siteIds" or "all", not both'); } ids = listSiteIds(getPaths()); if (ids.length === 0) throw new OpsInputError("No sites configured."); } else ids = reqSiteIds(body); const run = await enqueueAsks(ids.map((target) => ({ target, build: { force: true, skipArchives } }))); const jobs = ids.flatMap((siteId) => { const j = lastJobOf(run, siteId); return j ? [{ siteId, jobId: j.jobId }] : []; }); const skipped = refusalsOf(run).map((r) => ({ siteId: r.target, reason: r.error })); const all = optBool(body, "all") === true; const top = jobs.length === 1 ? jobs[0].jobId : all ? run.jobs.at(-1)?.jobId : undefined; return NextResponse.json({ ok: true, runId: run.runId, jobs, skipped, ...(top ? { jobId: top } : {}), }); }, ); }