// `archilyzer reports respell --heard --reads [--bracket] // [--note ] [--iteration ] [--apply] [--json]` — correct a transcript's // mis-hearing across an article's quotes (lib/report/respellArticle.ts). // // DRY RUN BY DEFAULT: it prints every change, file by file, as before → after, and // writes nothing. `--apply` writes (each file backed up first to its project's // revisions/). Exit 0 when the plan is clean (and applied); 1 when it changes nothing // anywhere; 2 for usage, or no such article or iteration. import type { Paths } from "../lib/paths"; import { ArticleRegistryError } from "../lib/report/registry"; import { applyRespell, formatRespellPlan, planRespell } from "../lib/report/respellArticle"; import { projectsRootOf } from "./reports-delete"; type Out = { log: (s: string) => void; error: (s: string) => void }; export type RespellFlags = { heard?: string; reads?: string; bracket?: boolean; note?: string; iteration?: string; apply?: boolean; json?: boolean; }; export async function respellMain( article: string, flags: RespellFlags, env: { projectsRoot: string; paths: Pick }, out: Out = console, ): Promise { const heard = (flags.heard ?? "").split(",").map((h) => h.trim()).filter(Boolean); const reads = (flags.reads ?? "").trim(); if (!heard.length || !reads) { out.error('reports respell: give --heard "
[,…]" and --reads ""'); return 2; } let iteration: number | undefined; if (flags.iteration !== undefined) { iteration = Number(flags.iteration); if (!Number.isInteger(iteration) || iteration < 1) { out.error(`reports respell: --iteration takes an iteration number (1, 2, …), not "${flags.iteration}"`); return 2; } } const respellings = [{ heard, reads, ...(flags.bracket ? { bracket: true } : {}), ...(flags.note ? { note: flags.note } : {}) }]; const opts = { ...(iteration !== undefined ? { iteration } : {}), ...env }; try { if (!flags.apply) { const plan = await planRespell(article, respellings, opts); if (flags.json) out.log(JSON.stringify({ applied: false, plan }, null, 2)); else { for (const l of formatRespellPlan(plan)) out.log(l); out.log("dry run: nothing written — run again with --apply to write it"); } return plan.total ? 0 : 1; } const { plan, backups } = await applyRespell(article, respellings, opts); if (flags.json) out.log(JSON.stringify({ applied: true, plan, backups }, null, 2)); else { for (const l of formatRespellPlan(plan)) out.log(l); for (const b of backups) out.log(`backup ${b}`); out.log(`applied: ${plan.total} change(s); the old files are the backups above`); } return plan.total ? 0 : 1; } catch (err) { if (err instanceof ArticleRegistryError) { out.error(`reports respell: ${err.message}`); for (const p of (err as { problems?: string[] }).problems ?? []) out.error(` ${p}`); return 2; } throw err; } } export async function respellRow(article: string, flags: RespellFlags): Promise { const { getPaths } = await import("../lib/paths"); const { getSettings } = await import("../lib/settings"); return respellMain(article, flags, { projectsRoot: projectsRootOf(getSettings().generation), paths: getPaths() }); }