// `umtool rulings` (release 34 N1): the clip deck's rulings from a terminal -- // the same writer the deck's buttons use (lib/report/montageRulings.mjs), so a // ruling typed here and one swiped there are the same file. // // umtool rulings [--json] every montage clip, its ruling; the next render // umtool rulings import [--dry-run] [--json] the earlier rulings in the seed's edits.json // umtool rulings set [--reason "…"] [--match "…"] // umtool rulings clear import { REPORTS_ROOT, resolveProject } from "../projects/core.mjs"; import { readManifest } from "../projects/report.mjs"; import { hasMontageClips, importEarlierRulings, momentKeysOf, montageClipsOf, readRulingsState, renderPlan, resolveRulingKey, setMontageRuling, } from "./montageRulings.mjs"; export const RULINGS_USAGE = [ "usage: umtool rulings [--json] each montage clip and its ruling; the next render", " umtool rulings import [--dry-run] [--json] the earlier rulings in the seed's edits.json", " umtool rulings set keep|cut|not-him|read-aloud [--reason \"…\"] [--match \"…\"]", " umtool rulings clear ", "", "The clip deck's rulings (/montage-rulings.json, keyed by the source moment): a render leaves", "out every clip ruled cut, not-him or read-aloud, cuts to a ruled --match, and numbers the kept 1…N.", ].join("\n"); class CliError extends Error {} /** * @param {string[]} args the words after `rulings` * @param {{ root?: string, out?: (s: string) => void, err?: (s: string) => void, now?: Date }} [opts] * @returns {Promise} the exit code */ export async function rulingsCommand(args, { root = REPORTS_ROOT, out = (s) => console.log(s), err = (s) => console.error(s), now = new Date() } = {}) { const json = args.includes("--json"); const dryRun = args.includes("--dry-run"); const valued = new Set(["--reason", "--match"]); const flags = new Map(); const words = []; for (let i = 0; i < args.length; i += 1) { const a = args[i]; if (valued.has(a)) { flags.set(a, args[i + 1] ?? ""); i += 1; } else if (!a.startsWith("--")) words.push(a); } try { const [projectArg, verb = "show", target, verdict] = words; if (!projectArg || args.includes("--help")) { err(RULINGS_USAGE); return 2; } const r = await resolveProject(projectArg, root); if (r.ambiguous) throw new CliError(`"${projectArg}" is the name of ${r.ambiguous.length} projects — pass its id`); if (!r.project) throw new CliError(`no project matches "${projectArg}"`); const project = r.project; const manifest = await readManifest(project.dir); if (!manifest) throw new CliError(`${project.id} has no manifest`); if (!hasMontageClips(manifest)) throw new CliError(`${project.id} has no montage clips (clips carrying \`montage\`)`); if (verb === "show") { const state = await readRulingsState(project.dir); if (state.error) throw new CliError(state.error); const keys = momentKeysOf(manifest); const rows = montageClipsOf(manifest).map((e) => ({ id: e.id, key: keys.get(e.id) ?? null, ruling: state.doc.rulings[keys.get(e.id) ?? ""] ?? null })); const plan = renderPlan(manifest, state.doc); if (json) out(JSON.stringify({ project: project.id, clips: rows, plan }, null, 2)); else { for (const row of rows) { const v = row.ruling; out(`${row.id.padEnd(5)} ${(v?.verdict ?? "—").padEnd(10)} ${row.key ?? "(no moment)"}${v?.match ? ` match "${v.match}"` : ""}${v?.reason ? ` — ${v.reason}` : ""}`); } out(`${rows.filter((x) => x.ruling).length} of ${rows.length} ruled; the next render: ${plan.kept.length} clip(s)`); for (const line of plan.lines) out(line); } return 0; } if (verb === "import") { const done = await importEarlierRulings(project.dir, manifest, { now, dryRun }); if (json) out(JSON.stringify({ project: project.id, dryRun, offered: done.rulings, written: done.written, unmapped: done.unmapped }, null, 2)); else { if (!done.exists) out(`${project.id}: no edits.json — nothing to import`); for (const x of done.rulings) { out(`${dryRun ? "would write" : done.written.includes(x.key) ? "wrote" : "kept"} ${x.key}${x.clip ? ` (${x.clip})` : " (not in the manifest)"}: ${x.ruling.verdict}${x.ruling.match ? ` match "${x.ruling.match}"` : ""}${x.ruling.reason ? ` — ${x.ruling.reason}` : ""}`); } for (const u of done.unmapped) out(`no moment for ${u.videoId} near ${u.near}${u.why ? ` (${u.why})` : ""}`); out(`${dryRun ? "would import" : "imported"} ${dryRun ? done.rulings.length : done.written.length} earlier ruling(s)`); } return 0; } if (verb === "set" || verb === "clear") { if (!target) throw new CliError(`${verb} needs a clip id or a moment key`); const key = resolveRulingKey(manifest, target); if (!key) throw new CliError(`no montage clip "${target}" in ${project.id}`); if (verb === "set" && !verdict) throw new CliError("set needs a verdict: keep, cut, not-him or read-aloud"); const ruling = verb === "clear" ? null : { verdict, reason: flags.get("--reason") ?? "", ...(flags.has("--match") ? { match: flags.get("--match") } : {}) }; const done = await setMontageRuling(project.dir, key, ruling, { now }); if (json) out(JSON.stringify({ key, ruling: done.ruling }, null, 2)); else out(done.ruling ? `${key}: ${done.ruling.verdict}${done.ruling.match ? ` match "${done.ruling.match}"` : ""}` : `${key}: cleared`); return 0; } throw new CliError(`unknown: rulings ${verb} — show, import, set or clear`); } catch (e) { err(e instanceof CliError ? `umtool rulings: ${e.message}` : e instanceof Error ? `umtool rulings: ${e.message}` : String(e)); return 1; } }