Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions packages/flow-schema/scripts/generate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// is committed; CI runs `gen` and fails on any diff (drift guard).

import { compileFromFile } from 'json-schema-to-typescript';
import { mkdir, writeFile, copyFile } from 'node:fs/promises';
import { mkdir, readFile, writeFile, copyFile } from 'node:fs/promises';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

Expand All @@ -22,11 +22,24 @@ await mkdir(genDir, { recursive: true });
// consumers don't reach outside the package at runtime.
await copyFile(schemaPath, resolve(genDir, 'flow.v1.schema.json'));

// 2. Emit the model types from the schema.
const banner =
'// GENERATED from schema/flow.v1.schema.json by scripts/generate.mjs — do not edit.\n' +
'// Run `pnpm gen` after changing the schema; CI fails on drift.\n';

// 2. Emit the schema as a TS module. The runtime imports this instead of
// the bundled JSON file: a JSON import needs `with { type: 'json' }`,
// which older consumer bundlers (e.g. wrangler 3's pinned esbuild)
// cannot parse in the published dist.
const schemaSource = JSON.parse(await readFile(schemaPath, 'utf8'));
const schemaTs =
banner +
'\nconst schema = ' +
JSON.stringify(schemaSource, null, 2) +
' as const;\n\nexport default schema;\n';
await writeFile(resolve(genDir, 'schema.ts'), schemaTs);

// 3. Emit the model types from the schema.

const ts = await compileFromFile(schemaPath, {
bannerComment: banner,
additionalProperties: true, // unknown fields are accepted (matches both twins)
Expand All @@ -35,4 +48,4 @@ const ts = await compileFromFile(schemaPath, {

await writeFile(resolve(genDir, 'model.ts'), ts);

console.log('generated src/generated/model.ts and bundled flow.v1.schema.json');
console.log('generated src/generated/{model,schema}.ts and bundled flow.v1.schema.json');
Loading
Loading