// The drafts review's view of a project (release 28, M0): every drafted field // of the manifest (every cut's) and every narrated card, with who drafted it, // its value as words, its review state (report-to-video's drafts-review.mjs), // the alternatives the draft tasks kept for it (`-draft.json`), and what // a final build of the default cut would say. What the review page and the // badges draw, and what the route answers after each action -- one shape. // // Plain ESM: `node --test` runs it with no TypeScript. import { readdir, readFile } from "node:fs/promises"; import path from "node:path"; import { DEFAULT_VARIANT, selectVariant } from "umtool-report-to-video/build-video"; import { DRAFTS_REVIEW_FILE, FIELD_LABELS, draftsReviewProblems, draftsReviewRows, judgedText, readDraftsReview, } from "umtool-report-to-video/drafts-review"; import { fieldSignature, reviewAddress, stableStringify } from "yt-dlp-transcript-common/lib/report/drafted.mjs"; /** A task's draft file beside the manifest: `-draft.json`. */ export const DRAFT_FILE_RE = /^[a-z0-9][a-z0-9-]{0,62}-draft\.json$/; /** * Every alternative the draft tasks kept in this project, in file order: * `{ id: "#", file, index, address, entry, field, value, provenance, reason }`. * A file that does not parse is skipped (its task wrote it; it is not this * view's to repair). * * @param {string} projectDir */ export async function readAlternatives(projectDir) { let names = []; try { names = (await readdir(projectDir)).filter((n) => DRAFT_FILE_RE.test(n)).sort(); } catch { return []; } const out = []; for (const file of names) { let doc; try { doc = JSON.parse(await readFile(path.join(projectDir, file), "utf8")); } catch { continue; } const list = Array.isArray(doc?.alternatives) ? doc.alternatives : []; list.forEach((a, index) => { const t = a?.target; if (!t || typeof t.field !== "string" || a.value === undefined) return; const entry = typeof t.entry === "string" && t.entry ? t.entry : null; out.push({ id: `${file}#${index}`, file, index, address: reviewAddress({ entry, field: t.field }), entry, field: t.field, value: a.value, provenance: a.provenance ?? null, reason: typeof a.reason === "string" ? a.reason : "operator", }); }); } return out; } /** One alternative by its id, or null. */ export async function alternativeById(projectDir, id) { return (await readAlternatives(projectDir)).find((a) => a.id === id) ?? null; } /** * @param {string} projectDir * @param {any} manifest the whole manifest, as written */ export async function draftsView(projectDir, manifest) { const review = await readDraftsReview(projectDir); const m = manifest && typeof manifest === "object" ? manifest : { timeline: [] }; const alternatives = (await readAlternatives(projectDir)).map((a) => { const current = fieldSignature(m, { entry: a.entry, field: a.field }); // "in use": the field holds this value now (somebody took it). const proposed = a.field === "timeline" ? null : a.value; return { ...a, label: FIELD_LABELS[a.field] ?? a.field, text: judgedText(a.field, a.value), inUse: proposed !== null && stableStringify(current) === stableStringify(proposed) }; }); const rows = draftsReviewRows(m, review).map((r) => ({ address: r.address, entry: r.entry, field: r.field, label: FIELD_LABELS[r.field] ?? r.field, drafted: r.provenance, text: r.text, key: r.key, state: r.state, verdict: r.row?.verdict ?? null, note: r.row?.note ?? "", judgedText: r.row?.text ?? "", judgedAt: r.row?.at ?? "", })); const counts = { approved: 0, rejected: 0, edited: 0, unreviewed: 0 }; for (const r of rows) if (r.drafted) counts[r.state] += 1; // What a final build of the default cut would say of the review. let gate = { errors: [], warnings: [] }; try { const cut = selectVariant(m, DEFAULT_VARIANT); const fin = draftsReviewProblems(cut, review, { final: true }); gate = { errors: fin.errors, warnings: fin.warnings }; } catch (e) { gate = { errors: [e instanceof Error ? e.message : String(e)], warnings: [] }; } return { file: DRAFTS_REVIEW_FILE, exists: review.exists, error: review.error ?? null, rows, alternatives, counts, drafted: rows.filter((r) => r.drafted).length, gate, }; }