{"version":3,"file":"file-emitter.mjs","names":[],"sources":["../../src/codegen/file-emitter.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'node:fs';\nimport { dirname, join, resolve } from 'node:path';\nimport type { CodeBuildResult, EmitResult, FileEmitter, FileEmitterOptions } from './types.ts';\n\ninterface QueuedFile {\n  path: string;\n  content: string;\n}\n\nclass FileEmitterImpl implements FileEmitter {\n  private files: QueuedFile[] = [];\n  private options: FileEmitterOptions;\n\n  constructor(options: FileEmitterOptions) {\n    this.options = options;\n  }\n\n  addFile(path: string, content: string | CodeBuildResult): void {\n    const text = typeof content === 'string' ? content : content.text;\n    const fullContent = this.options.header ? `${this.options.header}\\n\\n${text}` : text;\n\n    this.files.push({ path, content: fullContent });\n  }\n\n  async emit(): Promise<EmitResult> {\n    const result: EmitResult = {\n      written: [],\n      skipped: [],\n      errors: [],\n    };\n\n    const outDir = resolve(this.options.outDir);\n\n    for (const file of this.files) {\n      const fullPath = join(outDir, file.path);\n\n      try {\n        // Check if file already exists\n        if (existsSync(fullPath) && !this.options.overwrite) {\n          result.skipped.push(file.path);\n          continue;\n        }\n\n        if (this.options.dryRun) {\n          result.written.push(file.path);\n          continue;\n        }\n\n        // Ensure directory exists\n        const dir = dirname(fullPath);\n        mkdirSync(dir, { recursive: true });\n\n        // Write the file\n        writeFileSync(fullPath, file.content, 'utf-8');\n        result.written.push(file.path);\n      } catch (err) {\n        result.errors.push({\n          file: file.path,\n          error: err instanceof Error ? err : new Error(String(err)),\n        });\n      }\n    }\n\n    return result;\n  }\n}\n\n/**\n * Create a FileEmitter for writing multiple generated files to disk.\n */\nexport function createFileEmitter(options: FileEmitterOptions): FileEmitter {\n  return new FileEmitterImpl(options);\n}\n"],"mappings":";;;AASA,IAAM,kBAAN,MAA6C;CAC3C,QAA8B,CAAC;CAC/B;CAEA,YAAY,SAA6B;EACvC,KAAK,UAAU;CACjB;CAEA,QAAQ,MAAc,SAAyC;EAC7D,MAAM,OAAO,OAAO,YAAY,WAAW,UAAU,QAAQ;EAC7D,MAAM,cAAc,KAAK,QAAQ,SAAS,GAAG,KAAK,QAAQ,OAAO,MAAM,SAAS;EAEhF,KAAK,MAAM,KAAK;GAAE;GAAM,SAAS;EAAY,CAAC;CAChD;CAEA,MAAM,OAA4B;EAChC,MAAM,SAAqB;GACzB,SAAS,CAAC;GACV,SAAS,CAAC;GACV,QAAQ,CAAC;EACX;EAEA,MAAM,SAAS,QAAQ,KAAK,QAAQ,MAAM;EAE1C,KAAK,MAAM,QAAQ,KAAK,OAAO;GAC7B,MAAM,WAAW,KAAK,QAAQ,KAAK,IAAI;GAEvC,IAAI;IAEF,IAAI,WAAW,QAAQ,KAAK,CAAC,KAAK,QAAQ,WAAW;KACnD,OAAO,QAAQ,KAAK,KAAK,IAAI;KAC7B;IACF;IAEA,IAAI,KAAK,QAAQ,QAAQ;KACvB,OAAO,QAAQ,KAAK,KAAK,IAAI;KAC7B;IACF;IAIA,UADY,QAAQ,QACR,GAAG,EAAE,WAAW,KAAK,CAAC;IAGlC,cAAc,UAAU,KAAK,SAAS,OAAO;IAC7C,OAAO,QAAQ,KAAK,KAAK,IAAI;GAC/B,SAAS,KAAK;IACZ,OAAO,OAAO,KAAK;KACjB,MAAM,KAAK;KACX,OAAO,eAAe,QAAQ,MAAM,IAAI,MAAM,OAAO,GAAG,CAAC;IAC3D,CAAC;GACH;EACF;EAEA,OAAO;CACT;AACF;;;;AAKA,SAAgB,kBAAkB,SAA0C;CAC1E,OAAO,IAAI,gBAAgB,OAAO;AACpC"}