// `umtool intro [key=value …] [--off] [--take --off|--on] [--json]`: THE INTRO from a terminal // (umtool/docs/knobs.md, "The intro"), the twin of the Cut tab's intro editor -- the same reading (introEdit.mjs // introState) and the same writers: the project's render.intro through the look writer, a version's "no intro" // through the take's own (its patch's `render.intro: null`). // // Plain ESM: the CLI runs under bare node. import { REPORTS_ROOT, resolveProject } from "../projects/core.mjs"; import { INTRO_KEYS, INTRO_STYLES } from "yt-dlp-transcript-common/lib/report/intro.mjs"; import { introPatch, introState, setVersionIntro, writeIntro } from "./introEdit.mjs"; import { withEditNotes } from "./edit-guard.mjs"; export const INTRO_USAGE = [ "usage: umtool intro [key=value …] [--off] [--json]", " umtool intro --take --off|--on [--json]", "", "THE INTRO the cut opens on (render.intro): with no key, prints it -- as written, what the cut opens with (its", "title-card words resolved) and each version's on/off. key=value writes it (a new intro starts as the brand", "sting, boot-dark); `key=` clears a key; --off takes the intro off the cut. Keys:", ` ${INTRO_KEYS.join(" ")}`, ` style: ${INTRO_STYLES.join(" ")} · seconds 3.5–8 · mark none|draw|chime · sound=|false`, " title= · lines='29 October 2026|Part one' (≤ 3, or a JSON list) · site=auto||none", "--take --off: that version opens on no intro (its patch's render.intro: null; the cut's untouched);", "--on: the key comes off its patch -- it opens on the intro it was saved with (a legacy take: the draft's). Play,", "Present and the Cut tab show the intro as the build draws it.", ].join("\n"); class CliError extends Error {} /** One line per thing the intro is: what the CLI prints. */ function describe(state) { const lines = []; const w = state.intro; if (w === null || w === undefined) lines.push("intro: none (the cut opens on its first entry)"); else lines.push(`intro: ${JSON.stringify(w)}`); for (const e of state.errors) lines.push(` REFUSED: ${e}`); if (state.entryError) lines.push(` does not resolve: ${state.entryError}`); const e = state.entry; if (e) { const site = e.site ? ` · site ${e.site.name}` : ""; const words = [e.title ? `"${e.title}"` : null, ...(e.lines ?? []).map((l) => `"${l}"`)].filter(Boolean).join(" / "); lines.push(` opens with: ${e.style} (${e.ground}, mark ${e.mark}) ${e.seconds} s${site}${words ? ` · ${words}` : ""}${e.sound === false ? " · silent" : ""}`); } if (state.versions.length) { lines.push("versions:"); for (const v of state.versions) lines.push(` ${v.id.padEnd(16)} ${v.intro === "off" ? "no intro" : v.intro === "own" ? "its own intro" : "the cut's intro"}${v.draft ? " (draft)" : ""}`); } return lines; } /** * @param {string[]} args the words after `intro` * @param {{ root?: string, out?: (s: string) => void, err?: (s: string) => void }} [opts] * @returns {Promise} */ export async function introCommand(args, { root = REPORTS_ROOT, out = (s) => console.log(s), err = (s) => console.error(s) } = {}) { try { const words = []; const flags = new Map(); for (let i = 0; i < args.length; i += 1) { const a = args[i]; if (a === "--take") { if (i + 1 >= args.length) throw new CliError("--take needs a take id"); flags.set(a, args[++i]); } else if (a.startsWith("--")) flags.set(a, true); else words.push(a); } if (!words[0] || flags.has("--help")) { (flags.has("--help") ? out : err)(INTRO_USAGE); return flags.has("--help") ? 0 : 2; } for (const f of flags.keys()) if (!["--take", "--off", "--on", "--json"].includes(f)) throw new CliError(`unknown flag ${f}\n\n${INTRO_USAGE}`); const r = await resolveProject(words[0], root); if (r.ambiguous) throw new CliError(`"${words[0]}" is the name of ${r.ambiguous.length} projects — pass its id`); if (!r.project) throw new CliError(`no project matches "${words[0]}"`); const project = r.project; const json = flags.has("--json"); const pairs = words.slice(1).map((w) => { const at = w.indexOf("="); if (at <= 0) throw new CliError(`"${w}" is not key=value\n\n${INTRO_USAGE}`); return /** @type {[string, string]} */ ([w.slice(0, at), w.slice(at + 1)]); }); const done = []; if (flags.has("--take")) { const take = String(flags.get("--take")); if (pairs.length) throw new CliError("--take sets a version's on/off only (--off or --on); its own knobs are a take's patch (umtool take … --set render.intro.=…)"); if (flags.has("--off") === flags.has("--on")) throw new CliError("--take needs --off or --on"); const res = await setVersionIntro(project.dir, take, flags.has("--on")); done.push(`${take}: ${flags.has("--on") ? "its intro again" : "no intro"}${res.unchanged ? " (already)" : res.revision ? ` · undo point ${res.revision.rel}` : ""}`); } else if (flags.has("--off")) { if (pairs.length) throw new CliError("--off takes the intro off; key=value writes one -- one or the other"); const { editNotes } = await withEditNotes(project, () => writeIntro(project.dir, null)); done.push(`intro: off${editNotes?.length ? ` · ${editNotes.length} edit note(s)` : ""}`); } else if (pairs.length) { const before = (await introState(project)).intro; let value; try { value = introPatch(before, pairs); } catch (e) { throw new CliError(e instanceof Error ? e.message : String(e)); } const { editNotes } = await withEditNotes(project, () => writeIntro(project.dir, value)); done.push(`intro: ${JSON.stringify(value)}${editNotes?.length ? ` · ${editNotes.length} edit note(s)` : ""}`); } const state = await introState(project); if (json) out(JSON.stringify({ ok: true, ...state }, null, 2)); else { for (const d of done) out(d); if (!done.length) for (const l of describe(state)) out(l); } return 0; } catch (e) { if (e instanceof CliError) { err(`umtool intro: ${e.message}`); return 1; } err(`umtool intro: ${e instanceof Error ? (/** @type {any} */ (e).errors ? /** @type {any} */ (e).errors.join("\n") : e.message) : String(e)}`); return 1; } }