UNPKG

@spfn/core

Version:

SPFN Framework Core - File-based routing, transactions, repository pattern

102 lines (98 loc) 3.28 kB
/** * Generator Interface * * Defines the contract for code generators that can be orchestrated by the codegen system. */ /** * Generator execution trigger types */ type GeneratorTrigger = 'watch' | 'manual' | 'build' | 'start'; interface GeneratorOptions { /** Project root directory */ cwd: string; /** Enable debug logging */ debug?: boolean; /** Execution trigger information */ trigger?: { /** How the generator was triggered */ type: GeneratorTrigger; /** Changed file information (only for 'watch' trigger) */ changedFile?: { path: string; event: 'add' | 'change' | 'unlink'; }; }; /** Custom configuration options */ [key: string]: any; } interface Generator { /** Unique generator name */ name: string; /** File patterns to watch (glob patterns) */ watchPatterns: string[]; /** * When this generator should run * * @default ['watch', 'manual', 'build'] * * Examples: * - ['watch', 'build']: Run during development and build (e.g., admin-nav-generator) * - ['build', 'start']: Run during build and server start (e.g., db-migration) * - ['watch', 'manual']: Run during development and manual CLI (e.g., contract-generator) * - ['start']: Run only on server start (e.g., runtime config generator) */ runOn?: GeneratorTrigger[]; /** * Generate code * * Generator can implement incremental updates by checking `options.trigger.changedFile`. * If incremental update is not possible, do full regeneration. * * @param options - Generator options with trigger context * * @example * ```typescript * async generate(options: GeneratorOptions): Promise<void> * { * // Check if incremental update is possible * if (options.trigger?.changedFile) * { * const { path, event } = options.trigger.changedFile; * * if (canDoIncrementalUpdate(path, event)) * { * await updateSingleFile(path); * return; * } * } * * // Fallback: full regeneration * await fullRegenerate(); * } * ``` */ generate(options: GeneratorOptions): Promise<void>; } /** * Contract Generator * * Generates type-safe API client from contract definitions * * Features: * - Automatic scanning of contract files * - Type-safe client generation with InferContract * - Split output by resource for better code organization * - Incremental updates when single files change (smart regeneration) */ interface ContractGeneratorConfig { /** Contracts directory (default: src/lib/contracts) */ contractsDir?: string; /** Output directory (default: src/lib/api) */ outputPath?: string; /** Base URL for API client */ baseUrl?: string; /** When to run this generator (default: ['watch', 'manual', 'build']) */ runOn?: GeneratorTrigger[]; } declare function createContractGenerator(config?: ContractGeneratorConfig): Generator; export { type ContractGeneratorConfig as C, type Generator as G, type GeneratorTrigger as a, type GeneratorOptions as b, createContractGenerator as c };