// `archilyzer reports move-to-articles [--keep /,…] [--drafts] [--apply]` // — release 26 A7, ONE-OFF: a site's published reports become ARTICLES // (plans/reports-on-homepage.md, "A7 — move the data"). // // DRY RUN BY DEFAULT: it prints the plan and writes nothing. `--apply` does it. // // For every site (by id), every report its site.json lists, in the site's // order — except each `--keep /` (a page the site keeps, D5): // 1. its directory `sites//reports//` moves to `reports//` // whole — report.json, stills, sources, video, history-git/ and // notes.json with it (a rename; a copy then a removal across devices); // 2. report.json gets `audience: "private"` (D2), through the article // registry (lib/report/registry.ts patchArticle); // 3. it is published in reports/index.json, after what is there, sites in // id order, each site's reports in its order — through the registry // (configureArticle), which publishes only a sound article: one with // problems is moved as a DRAFT, and the plan says so; // 4. the site's list (`pages`) drops it (patchSite). // `--drafts` moves the sites' unlisted report directories too, as drafts. // // REFUSED BEFORE ANYTHING MOVES: an id two sites both use, an id that // `reports//` already holds, a `--keep` that names no listed report, a // site whose reports cannot be read. Nothing is half-done by a refusal. // // Not moved: the staging a site's prepare and export wrote // (`.export-index/sites//report-{media,exports}/`) — run // `archilyzer reports prepare --article ` for the articles afterwards. // // A SEARCH-OFF SITE LEFT WITH NO PAGES is retired: a save refuses a search-off // site with none (siteSearchProblem, D5), so the patch that drops its last // page turns its search on in the same write — it is saved as an empty site // (no channels, no pages), which validates, builds nothing worth deploying and // is never listed while private. The plan names it; delete it on /sites (or // keep it) once the move is checked. Nothing about it is deleted here. // // Stop the editor's report jobs first (a prepare reads these directories). // Exit 0 when the plan is clean (and applied, with --apply); 1 when it was // refused; 2 for usage. import { cp, mkdir, rename, rm, stat } from "node:fs/promises"; import path from "node:path"; import { getPaths, type Paths } from "../lib/paths"; import { getSite, listSiteIds, patchSite, type Site } from "../lib/site"; import { isCitedSite } from "../lib/siteSchema"; import { readJsonFile } from "../lib/jsonFile-server"; import { isReportId } from "../lib/report/schema"; import { parseReport } from "../lib/report/validate"; import { articleDir, configureArticle, patchArticle, readArticlesIndex } from "../lib/report/registry"; import { listReportDirs, siteReportDir } from "../publish/reportMedia"; import { parseArgv } from "./_parseFlags"; import { runIfEntryPoint } from "./_cli"; export type MoveItem = { site: string; id: string; // Listed by its site (published there), or a draft (--drafts). listed: boolean; from: string; to: string; // Whether it will be published in reports/index.json (a sound, listed report). publish: boolean; // Why it moves as a draft although its site listed it. draftBecause?: string; }; export type MovePlan = { moves: MoveItem[]; // Kept on their sites, as pages. kept: string[]; // Search-off sites left with no reports. emptied: string[]; refusals: string[]; }; // THE PLAN — read only. export async function planMove( paths: Paths, opts: { keep?: readonly string[]; drafts?: boolean } = {}, ): Promise { const keep = new Set(opts.keep ?? []); const plan: MovePlan = { moves: [], kept: [], emptied: [], refusals: [] }; try { await readArticlesIndex(paths); } catch (e) { plan.refusals.push((e as Error).message); return plan; } const seen = new Map(); const sites: Site[] = listSiteIds(paths).map((id) => getSite(id, paths)); const known = new Set(); for (const site of sites) { const listed = siteList(site); for (const id of listed) known.add(`${site.siteId}/${id}`); const drafts = opts.drafts ? (await listReportDirs(paths, site.siteId)).filter((d) => !listed.includes(d)) : []; let left = 0; for (const [id, isListed] of [...listed.map((id) => [id, true] as const), ...drafts.map((id) => [id, false] as const)]) { const key = `${site.siteId}/${id}`; if (keep.has(key)) { plan.kept.push(key); if (isListed) left++; continue; } if (!isReportId(id)) { plan.refusals.push(`${key}: "${id}" is not a report id`); continue; } const from = siteReportDir(paths, site.siteId, id); const to = articleDir(paths, id); if (seen.has(id)) plan.refusals.push(`${key}: "${id}" is also ${seen.get(id)} — two articles cannot share an id`); seen.set(id, key); if (await exists(to)) plan.refusals.push(`${key}: reports/${id}/ already exists`); const read = await readJsonFile(path.join(from, "report.json")); if (!read.ok) { plan.refusals.push(`${key}: its report.json is ${read.reason}`); continue; } const parsed = parseReport(read.value, { id }); if (!parsed.ok) { plan.refusals.push(`${key}: its report.json does not parse as a report (archilyzer reports check)`); continue; } const sound = parsed.problems.length === 0; plan.moves.push({ site: site.siteId, id, listed: isListed, from, to, publish: isListed && sound, ...(isListed && !sound ? { draftBecause: `${parsed.problems.length} problem(s) in report.json` } : {}), }); } if (isCitedSite(site) && listed.length > 0 && left === 0) plan.emptied.push(site.siteId); } for (const k of keep) if (!known.has(k)) plan.refusals.push(`--keep ${k}: no site lists that report`); return plan; } // APPLY a clean plan: move, mark private, publish, drop from the site. export async function applyMove(paths: Paths, plan: MovePlan, log: (line: string) => void = console.log): Promise { if (plan.refusals.length > 0) throw new Error("the plan has refusals; nothing is moved"); for (const m of plan.moves) { await moveDir(m.from, m.to); await patchArticle(m.id, { audience: "private" }, paths); if (m.publish) await configureArticle(m.id, { published: true }, paths); if (m.listed) { await patchSite( m.site, (cur) => { const pages = siteList(cur).filter((x) => x !== m.id); // Its last page gone, a search-off site is retired (see above). return pages.length === 0 && isCitedSite(cur) ? { pages, search: true } : { pages }; }, paths, ); } log(`moved ${m.site}/${m.id} → reports/${m.id}/${m.publish ? " (published)" : " (draft)"}`); } } export function formatPlan(plan: MovePlan): string[] { const out: string[] = []; for (const m of plan.moves) { out.push( `move ${m.site}/${m.id} → reports/${m.id}/ ${m.publish ? "published" : "draft"}${m.draftBecause ? ` (${m.draftBecause})` : ""}, audience private`, ); } for (const k of plan.kept) out.push(`keep ${k} (a page of its site)`); for (const s of plan.emptied) { out.push(`retire ${s}: search off and no pages left — saved with search on, as an empty site; delete it on /sites`); } for (const r of plan.refusals) out.push(`REFUSED ${r}`); out.push( `${plan.moves.length} to move (${plan.moves.filter((m) => m.publish).length} published), ${plan.kept.length} kept, ${plan.refusals.length} refusal(s).`, ); return out; } // A site's published list: site.json `pages` (a legacy `reports` reads as it). function siteList(site: Site): string[] { return site.pages ?? []; } const exists = async (p: string) => !!(await stat(p).catch(() => null)); async function moveDir(from: string, to: string): Promise { await mkdir(path.dirname(to), { recursive: true }); try { await rename(from, to); } catch (e) { if ((e as NodeJS.ErrnoException).code !== "EXDEV") throw e; await cp(from, to, { recursive: true, errorOnExist: true, force: false, preserveTimestamps: true }); await rm(from, { recursive: true, force: true }); } } const USAGE = "usage: archilyzer reports move-to-articles [--keep /,…] [--drafts] [--apply]"; export async function main( argv: string[], paths: Paths = getPaths(), out: { log: (line: string) => void } = console, ): Promise { const { positionals, flags } = parseArgv(argv, ["drafts", "apply", "help"]); if (flags.help || positionals.length > 0) { out.log(USAGE); return flags.help ? 0 : 2; } const keep = typeof flags.keep === "string" ? flags.keep.split(",").map((k) => k.trim()).filter(Boolean) : []; const plan = await planMove(paths, { keep, drafts: flags.drafts === true }); for (const line of formatPlan(plan)) out.log(line); if (plan.refusals.length > 0) return 1; if (flags.apply !== true) { out.log("dry run: nothing moved (--apply to move)."); return 0; } await applyMove(paths, plan, (l) => out.log(l)); out.log("done. Prepare and export the articles: archilyzer reports prepare --article ."); return 0; } runIfEntryPoint(import.meta.url, () => main(process.argv.slice(2)));