// WHERE A RECURRING REPORT'S SCHEDULE LIVES, AND ITS READS (umtool R5). // // living transcripts/reports/
/schedule.json — beside the article // it keeps current: it goes where the article goes (a delete parks // the whole directory), and nothing that reads an article reads it // (compose copies named files, the site history commits // report.json, report.md and exports.json). A living report made // for a NEW article creates `
/iterations/` with it, so // the article is "in the making" (registry.ts listPendingArticles) // from the start — never a directory the registry would list as an // article with no report.json. // series transcripts/reports/.series//series.json — the schedule and // its editions listed in it. A dot-directory: the registry lists // only id-named directories (registry.ts idDirs), so a series is // never read as an article. Each edition is an ordinary article in // the store (`-`), the edition list naming it. // // The ids share one namespace: a series may not take a living schedule's id // (its article's) and a living schedule may not take a series'. // // Written ONLY by controller/reportSchedule.ts, under the article store's // lock (registry.ts withArticleStoreLock), atomically. Reads are lock-free: // a write replaces the file whole. // // SERVER-ONLY (node:fs). import { readdir } from "node:fs/promises"; import path from "node:path"; import type { Paths } from "../../paths"; import { readJsonFile } from "../../jsonFile-server"; import { isReportId } from "../schema"; import { parseSchedule, SCHEDULE_ID_RE, type Schedule, type ScheduleShape } from "./types"; export type SchedulePaths = Pick; export const SERIES_DIRNAME = ".series"; export const SERIES_FILENAME = "series.json"; export const LIVING_FILENAME = "schedule.json"; export const seriesDir = (paths: SchedulePaths, id: string) => path.join(paths.articlesDir, SERIES_DIRNAME, id); export const seriesFile = (paths: SchedulePaths, id: string) => path.join(seriesDir(paths, id), SERIES_FILENAME); export const livingFile = (paths: SchedulePaths, article: string) => path.join(paths.articlesDir, article, LIVING_FILENAME); export const scheduleFile = (paths: SchedulePaths, shape: ScheduleShape, id: string) => shape === "living" ? livingFile(paths, id) : seriesFile(paths, id); export type ScheduleRead = { ok: true; schedule: Schedule; file: string } | { ok: false; id: string; file: string; problem: string }; async function readAt(file: string, id: string): Promise { const r = await readJsonFile(file); if (!r.ok) return r.reason === "absent" ? null : { ok: false, id, file, problem: `${path.basename(file)} is ${r.reason}` }; const p = parseSchedule(r.value); if (!p.ok) return { ok: false, id, file, problem: p.problems.join("; ") }; if (p.value.id !== id) return { ok: false, id, file, problem: `it names "${p.value.id}", not "${id}"` }; return { ok: true, schedule: p.value, file }; } /** One schedule by id (a series first, then a living report's), or null. A file that does not read is a read with its problem. */ export async function readScheduleById(id: string, paths: SchedulePaths): Promise { if (!SCHEDULE_ID_RE.test(id) && !isReportId(id)) return null; return (await readAt(seriesFile(paths, id), id)) ?? (isReportId(id) ? await readAt(livingFile(paths, id), id) : null); } /** One schedule, or null (an unreadable one is null too). */ export async function readSchedule(id: string, paths: SchedulePaths): Promise { const r = await readScheduleById(id, paths); return r?.ok ? r.schedule : null; } /** A living report's schedule, by its article. */ export async function readLivingSchedule(article: string, paths: SchedulePaths): Promise { if (!isReportId(article)) return null; const r = await readAt(livingFile(paths, article), article); return r?.ok ? r.schedule : null; } async function dirNames(dir: string): Promise { try { return (await readdir(dir, { withFileTypes: true })).filter((e) => e.isDirectory()).map((e) => e.name); } catch { return []; } } /** Every schedule — the series, then the living reports — and the files that do not read. Sorted by id within each. */ export async function listScheduleReads(paths: SchedulePaths): Promise { const out: ScheduleRead[] = []; for (const id of (await dirNames(path.join(paths.articlesDir, SERIES_DIRNAME))).filter((n) => SCHEDULE_ID_RE.test(n)).sort()) { const r = await readAt(seriesFile(paths, id), id); if (r) out.push(r); } for (const id of (await dirNames(paths.articlesDir)).filter(isReportId).sort()) { const r = await readAt(livingFile(paths, id), id); if (r) out.push(r); } return out; } /** Every schedule that reads. */ export async function listSchedules(paths: SchedulePaths): Promise { return (await listScheduleReads(paths)).flatMap((r) => (r.ok ? [r.schedule] : [])); } /** Article id → the series it is an edition of (every series' editions). */ export async function editionIndex(paths: SchedulePaths): Promise> { const out = new Map(); for (const s of await listSchedules(paths)) { if (s.shape !== "series") continue; for (const e of s.editions ?? []) if (e.article) out.set(e.article, { series: s.id, n: e.n }); } return out; }