| 12 | } |
| 13 | |
| 14 | export async function runGenerate(entry: string, options: GenerateOptions): Promise<void> { |
| 15 | const t0 = Date.now(); |
| 16 | telemetry.capture("cli_generate_started", { |
| 17 | json_schema: !!options.jsonSchema, |
| 18 | out_to_file: !!options.out, |
| 19 | }); |
| 20 | const entryPath = path.resolve(process.cwd(), entry); |
| 21 | |
| 22 | if (!fs.existsSync(entryPath)) { |
| 23 | throw new CreateError("generate_entry_missing", `File not found: ${entryPath}`); |
| 24 | } |
| 25 | |
| 26 | const workerPath = path.join(__dirname, "generate-worker.js"); |
| 27 | |
| 28 | const workerArgs = [workerPath, entryPath]; |
| 29 | if (options.export) workerArgs.push(options.export); |
| 30 | if (options.jsonSchema) workerArgs.push("--json-schema"); |
| 31 | if (options.promptOptions) workerArgs.push("--prompt-options", options.promptOptions); |
| 32 | |
| 33 | let output: string; |
| 34 | try { |
| 35 | output = execFileSync(process.execPath, workerArgs, { |
| 36 | encoding: "utf-8", |
| 37 | cwd: process.cwd(), |
| 38 | stdio: ["inherit", "pipe", "inherit"], |
| 39 | }); |
| 40 | } catch (err) { |
| 41 | throw new CreateError("generate_worker", err instanceof Error ? err.message : String(err)); |
| 42 | } |
| 43 | |
| 44 | if (options.out) { |
| 45 | const outPath = path.resolve(process.cwd(), options.out); |
| 46 | fs.mkdirSync(path.dirname(outPath), { recursive: true }); |
| 47 | fs.writeFileSync(outPath, output + "\n"); |
| 48 | console.info(`Written to ${outPath}`); |
| 49 | } else { |
| 50 | process.stdout.write(output + "\n"); |
| 51 | } |
| 52 | |
| 53 | telemetry.capture("cli_generate_succeeded", { |
| 54 | json_schema: !!options.jsonSchema, |
| 55 | out_to_file: !!options.out, |
| 56 | duration_ms: Date.now() - t0, |
| 57 | }); |
| 58 | } |