|
| 1 | +// The declarative JSON template-spec the CLI ports (C#/Python) consume, and TS |
| 2 | +// can spread into `generators`. The JSON shape is the cross-port contract |
| 3 | +// (SP-1 §4); a JSON Schema (template-spec.schema.json) sits beside it. |
| 4 | +import type { RenderFormat } from "@metaobjectsdev/render"; |
| 5 | +import type { Generator } from "../generator.js"; |
| 6 | +import { templateGenerator, type TemplateScope } from "../generators/template-generator.js"; |
| 7 | + |
| 8 | +const SCOPES = ["perEntity", "perPackage", "perModel"] as const satisfies readonly TemplateScope[]; |
| 9 | + |
| 10 | +export interface TemplateSpecEntry { |
| 11 | + name: string; |
| 12 | + template: string; |
| 13 | + scope: TemplateScope; |
| 14 | + outputPattern: string; |
| 15 | + format?: RenderFormat; |
| 16 | + target?: string; |
| 17 | +} |
| 18 | +export interface TemplateSpecFile { generators: TemplateSpecEntry[]; } |
| 19 | + |
| 20 | +function isRecord(v: unknown): v is Record<string, unknown> { |
| 21 | + return typeof v === "object" && v !== null && !Array.isArray(v); |
| 22 | +} |
| 23 | + |
| 24 | +/** Validate + narrow an untyped JSON value into a TemplateSpecFile. Throws on |
| 25 | + * any shape violation (missing/empty required string, bad scope, non-object). */ |
| 26 | +export function parseTemplateSpec(json: unknown): TemplateSpecFile { |
| 27 | + if (!isRecord(json) || !Array.isArray(json.generators)) { |
| 28 | + throw new Error("template-spec: expected an object with a `generators` array"); |
| 29 | + } |
| 30 | + const generators = json.generators.map((raw, i): TemplateSpecEntry => { |
| 31 | + if (!isRecord(raw)) throw new Error(`template-spec generators[${i}]: expected an object`); |
| 32 | + for (const key of ["name", "template", "scope", "outputPattern"] as const) { |
| 33 | + if (typeof raw[key] !== "string" || raw[key] === "") { |
| 34 | + throw new Error(`template-spec generators[${i}]: missing or empty required string '${key}'`); |
| 35 | + } |
| 36 | + } |
| 37 | + if (!SCOPES.includes(raw.scope as TemplateScope)) { |
| 38 | + throw new Error( |
| 39 | + `template-spec generators[${i}]: scope must be one of ${SCOPES.join(" | ")}, got '${String(raw.scope)}'`, |
| 40 | + ); |
| 41 | + } |
| 42 | + const entry: TemplateSpecEntry = { |
| 43 | + name: raw.name as string, |
| 44 | + template: raw.template as string, |
| 45 | + scope: raw.scope as TemplateScope, |
| 46 | + outputPattern: raw.outputPattern as string, |
| 47 | + }; |
| 48 | + if (typeof raw.format === "string") entry.format = raw.format as RenderFormat; |
| 49 | + if (typeof raw.target === "string") entry.target = raw.target; |
| 50 | + return entry; |
| 51 | + }); |
| 52 | + return { generators }; |
| 53 | +} |
| 54 | + |
| 55 | +/** Map a parsed spec into runnable Generators (one templateGenerator per entry). */ |
| 56 | +export function templateSpecToGenerators(spec: TemplateSpecFile): Generator[] { |
| 57 | + return spec.generators.map((e) => |
| 58 | + templateGenerator({ |
| 59 | + name: e.name, |
| 60 | + template: e.template, |
| 61 | + scope: e.scope, |
| 62 | + outputPattern: e.outputPattern, |
| 63 | + ...(e.format !== undefined ? { format: e.format } : {}), |
| 64 | + ...(e.target !== undefined ? { target: e.target } : {}), |
| 65 | + }), |
| 66 | + ); |
| 67 | +} |
0 commit comments