import fs from "node:fs/promises"; import path from "node:path"; export const SCHEMA_PATH = path.join(process.cwd(), "app.schema.json"); export type Schema = Record; const FALLBACK: Schema = { "/": { root: { props: { title: "Untitled" } }, content: [] }, }; export async function readSchema(): Promise { try { const raw = await fs.readFile(SCHEMA_PATH, "utf8"); const parsed = JSON.parse(raw); if (parsed && typeof parsed === "object") return parsed as Schema; return FALLBACK; } catch { return FALLBACK; } } export async function writeSchema(schema: Schema): Promise { await fs.writeFile( SCHEMA_PATH, JSON.stringify(schema, null, 2) + "\n", "utf8", ); } export async function patchRoute( route: string, data: { root: any; content: any[] }, ): Promise { const current = await readSchema(); const next: Schema = { ...current, [route]: data }; await writeSchema(next); return next; }