// Writing the runtime-plugin ledger. The edits work on the RAW parsed value rather than on // `parseLedger`'s output: an entry the parser skips (hand-written, missing a field) is still the // user's, and an install must not delete it by rewriting the file from the entries it understood. // The one exception is a value carrying the name being installed or removed: the name gets exactly // one entry, so such a value is replaced or removed even when the parser would have skipped it. import { readFile } from "node:fs/promises"; import { isRecord } from "@mulmoclaude/common"; import { writeJsonAtomic } from "../files/json.js"; import type { LedgerEntry } from "./ledger.js"; const isEntryNamed = (value: unknown, name: string): boolean => isRecord(value) && Object.hasOwn(value, "name") && value.name === name; /** The ledger with `entry` in it, exactly once: it takes the place of the first entry of the same * name, any further ones are dropped (each would load another copy), and with none it is appended. */ export function withLedgerEntry(raw: readonly unknown[], entry: LedgerEntry): unknown[] { const first = raw.findIndex((value) => isEntryNamed(value, entry.name)); if (first === -1) return [...raw, entry]; return raw.flatMap((value, index) => { if (index === first) return [entry]; return isEntryNamed(value, entry.name) ? [] : [value]; }); } /** The ledger without any entry named `name`; everything else is kept as it was. */ export const withoutLedgerEntry = (raw: readonly unknown[], name: string): unknown[] => raw.filter((value) => !isEntryNamed(value, name)); export type ReadLedgerResult = { ok: true; raw: unknown[] } | { ok: false; error: string }; async function readLedgerText(ledgerPath: string): Promise<{ ok: true; text: string | null } | { ok: false; error: string }> { try { return { ok: true, text: await readFile(ledgerPath, "utf-8") }; } catch (err) { if (isRecord(err) && err.code === "ENOENT") return { ok: true, text: null }; return { ok: false, error: `cannot read the plugin ledger ${ledgerPath}: ${String(err)}` }; } } function parseLedgerText(ledgerPath: string, text: string): ReadLedgerResult { try { const parsed: unknown = JSON.parse(text); return Array.isArray(parsed) ? { ok: true, raw: parsed } : { ok: false, error: `the plugin ledger ${ledgerPath} is not a JSON array; fix it before installing` }; } catch (err) { return { ok: false, error: `the plugin ledger ${ledgerPath} is not valid JSON; fix it before installing: ${String(err)}` }; } } /** The ledger as an array to edit: a missing file is an empty ledger; a file that is not a JSON * array is refused rather than overwritten, since writing over it would lose what the user wrote. */ export async function readLedgerForWrite(ledgerPath: string): Promise { const read = await readLedgerText(ledgerPath); if (!read.ok) return read; return read.text === null ? { ok: true, raw: [] } : parseLedgerText(ledgerPath, read.text); } export const writeLedger = (ledgerPath: string, raw: readonly unknown[]): Promise => writeJsonAtomic(ledgerPath, raw);