import { z } from 'zod' import { defineFlags, type FlagMapping } from '@chkit/core' // ───── Plugin config schema (what codegen({...}) accepts) ───── const PluginConfigSchema = z.object({ outFile: z.string().min(1).optional(), emitZod: z.boolean().optional(), tableNameStyle: z.enum(['pascal', 'camel', 'raw']).optional(), bigintMode: z.enum(['string', 'bigint']).optional(), includeViews: z.boolean().optional(), runOnGenerate: z.boolean().optional(), failOnUnsupportedType: z.boolean().optional(), emitIngest: z.boolean().optional(), ingestOutFile: z.string().min(1).optional(), emitMigrations: z.boolean().optional(), migrationsOutFile: z.string().min(1).optional(), }) export type PluginConfig = z.input // ───── Codegen command schema ───── export const CodegenSchema = z.object({ outFile: z.string().min(1).default('./src/generated/chkit-types.ts'), emitZod: z.boolean().default(false), tableNameStyle: z.enum(['pascal', 'camel', 'raw']).default('pascal'), bigintMode: z.enum(['string', 'bigint']).default('string'), includeViews: z.boolean().default(false), runOnGenerate: z.boolean().default(true), failOnUnsupportedType: z.boolean().default(true), emitIngest: z.boolean().default(false), ingestOutFile: z.string().min(1).default('./src/generated/chkit-ingest.ts'), emitMigrations: z.boolean().default(false), migrationsOutFile: z.string().min(1).default('./src/generated/chkit-migrations.ts'), }) export type CodegenOptions = z.infer // ───── CLI flag definitions ───── export const CODEGEN_FLAGS = defineFlags([ { name: '--check', type: 'boolean', description: 'Check if generated output is up-to-date' }, { name: '--out-file', type: 'string', description: 'Output file path', placeholder: '' }, { name: '--emit-zod', type: 'boolean', description: 'Emit Zod schemas alongside TypeScript types', negation: true }, { name: '--emit-ingest', type: 'boolean', description: 'Emit ingest helper functions', negation: true }, { name: '--ingest-out-file', type: 'string', description: 'Ingest output file path', placeholder: '' }, { name: '--bigint-mode', type: 'string', description: 'How to represent large integers (string or bigint)', placeholder: '' }, { name: '--include-views', type: 'boolean', description: 'Include views in generated output' }, { name: '--emit-migrations', type: 'boolean', description: 'Emit runtime migration module', negation: true }, { name: '--migrations-out-file', type: 'string', description: 'Migration module output file path', placeholder: '' }, ] as const) // ───── Flag mapping ───── export const CODEGEN_FLAG_MAP: FlagMapping = { '--out-file': { key: 'outFile' }, '--emit-zod': { key: 'emitZod' }, '--emit-ingest': { key: 'emitIngest' }, '--ingest-out-file': { key: 'ingestOutFile' }, '--bigint-mode': { key: 'bigintMode' }, '--include-views': { key: 'includeViews' }, '--emit-migrations': { key: 'emitMigrations' }, '--migrations-out-file': { key: 'migrationsOutFile' }, } /** Fill defaults for partial options. Used by generators. */ export function normalizeCodegenOptions(options: PluginConfig = {}): CodegenOptions { return CodegenSchema.parse(options) }