generate-sdk-types.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * Generates src/entrypoints/sdk/coreTypes.generated.ts from the Zod schemas
  3. * in coreSchemas.ts. Derives TypeScript types via z.infer.
  4. */
  5. import { readFileSync, writeFileSync } from 'fs';
  6. import { join } from 'path';
  7. const schemasPath = join(import.meta.dir, '../src/entrypoints/sdk/coreSchemas.ts');
  8. const outputPath = join(import.meta.dir, '../src/entrypoints/sdk/coreTypes.generated.ts');
  9. const content = readFileSync(schemasPath, 'utf-8');
  10. // Extract all exported schema names
  11. const schemaNames: string[] = [];
  12. const regex = /^export const (\w+Schema)\b/gm;
  13. let match: RegExpExecArray | null;
  14. while ((match = regex.exec(content)) !== null) {
  15. schemaNames.push(match[1]!);
  16. }
  17. // Generate type exports
  18. const lines = [
  19. '// AUTO-GENERATED from coreSchemas.ts — do not edit manually',
  20. "import { z } from 'zod/v4'",
  21. "import * as schemas from './coreSchemas.js'",
  22. '',
  23. ];
  24. for (const schemaName of schemaNames) {
  25. const typeName = schemaName.replace(/Schema$/, '');
  26. lines.push(`export type ${typeName} = z.infer<ReturnType<typeof schemas.${schemaName}>>;`);
  27. }
  28. writeFileSync(outputPath, lines.join('\n') + '\n');
  29. console.log(`Generated ${schemaNames.length} types to ${outputPath}`);