Initial commit
This commit is contained in:
39
lib/schema.server.ts
Normal file
39
lib/schema.server.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
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<string, { root: any; content: any[] }>;
|
||||
|
||||
const FALLBACK: Schema = {
|
||||
"/": { root: { props: { title: "Untitled" } }, content: [] },
|
||||
};
|
||||
|
||||
export async function readSchema(): Promise<Schema> {
|
||||
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<void> {
|
||||
await fs.writeFile(
|
||||
SCHEMA_PATH,
|
||||
JSON.stringify(schema, null, 2) + "\n",
|
||||
"utf8",
|
||||
);
|
||||
}
|
||||
|
||||
export async function patchRoute(
|
||||
route: string,
|
||||
data: { root: any; content: any[] },
|
||||
): Promise<Schema> {
|
||||
const current = await readSchema();
|
||||
const next: Schema = { ...current, [route]: data };
|
||||
await writeSchema(next);
|
||||
return next;
|
||||
}
|
||||
Reference in New Issue
Block a user