UNPKG

@paroicms/cli

Version:
51 lines 2.15 kB
import { rename, writeFile } from "node:fs/promises"; import { join } from "node:path"; export async function migrateFrom40To5(directory, oldSchema) { const newSchema = { ...oldSchema, }; newSchema.version = "5"; if (oldSchema.nodeTypes) { newSchema.nodeTypes = oldSchema.nodeTypes.map((nodeType) => { if (nodeType.kind !== "document") return nodeType; const { kind, typeName, ...rest } = nodeType; return { typeName, kind, documentKind: typeof nodeType.route === "string" && nodeType.route.startsWith(":") ? "regular" : "routing", ...rest, }; }); const map = new Map(newSchema.nodeTypes.map((nodeType) => [nodeType.typeName, nodeType])); for (const nodeType of newSchema.nodeTypes) { if (nodeType.kind !== "document") continue; if (!nodeType.children) continue; const children = []; const routingChildren = []; for (const typeName of nodeType.children) { const child = map.get(typeName); if (!child) throw new Error(`Unknown document child type '${typeName}'`); if (child.kind !== "document") throw new Error(`Invalid document child type '${typeName}'`); if (child.documentKind === "routing") { routingChildren.push(typeName); } else { children.push(typeName); } } nodeType.children = children.length > 0 ? children : undefined; nodeType.routingChildren = routingChildren.length > 0 ? routingChildren : undefined; } } await rename(join(directory, "site-schema.json"), join(directory, "site-schema-v4.0.json")); await writeFile(join(directory, "site-schema.json"), JSON.stringify(newSchema, undefined, 2)); return newSchema; } //# sourceMappingURL=site-schema-migration-4.0-to-5.js.map