{"version":3,"file":"command-tree.mjs","names":[],"sources":["../../../src/codegen/generators/command-tree.ts"],"sourcesContent":["import { fieldMetaToCode } from '../schema-to-code.ts';\nimport type { CommandMeta, GeneratorContext } from '../types.ts';\nimport type { CommandFileOptions } from './command-file.ts';\nimport { generateCommandFile, toCommandFunctionName } from './command-file.ts';\n\nexport interface CommandTreeOptions {\n  /** When set, generates .wrap() calls instead of .action(). */\n  wrap?: {\n    /** The external command being wrapped (e.g. 'gh'). */\n    command: string;\n  };\n}\n\n/**\n * Walk a CommandMeta tree and emit one file per command plus a root program file.\n * Maps nested subcommands to a directory structure.\n */\nexport function generateCommandTree(root: CommandMeta, ctx: GeneratorContext, options?: CommandTreeOptions): void {\n  const rootImports: { name: string; varName: string; path: string; aliases?: string[] }[] = [];\n\n  // Recursively generate command files (depth-first so children exist before parents)\n  function walkCommands(cmd: CommandMeta, dirPath: string, parentArgs: string[]): void {\n    if (cmd === root) {\n      for (const sub of cmd.subcommands || []) {\n        walkCommands(sub, 'commands', []);\n      }\n      return;\n    }\n\n    const filePath = `${dirPath}/${cmd.name}.ts`;\n\n    // Recurse into subcommands first so we can reference them\n    const childRefs: { name: string; varName: string; importPath: string; aliases?: string[] }[] = [];\n    if (cmd.subcommands && cmd.subcommands.length > 0) {\n      for (const sub of cmd.subcommands) {\n        walkCommands(sub, `${dirPath}/${cmd.name}`, [...parentArgs, cmd.name]);\n        childRefs.push({\n          name: sub.name,\n          varName: toCommandFunctionName(sub.name),\n          importPath: `./${cmd.name}/${sub.name}.ts`,\n          aliases: sub.aliases,\n        });\n      }\n    }\n\n    const fileOptions: CommandFileOptions = {};\n    if (options?.wrap) {\n      fileOptions.wrap = { command: options.wrap.command, args: [...parentArgs, cmd.name] };\n    }\n    if (childRefs.length > 0) {\n      fileOptions.subcommands = childRefs;\n    }\n\n    const code = generateCommandFile(cmd, ctx, Object.keys(fileOptions).length > 0 ? fileOptions : undefined);\n    ctx.emitter.addFile(filePath, code.build());\n\n    rootImports.push({\n      name: cmd.name,\n      varName: toCommandFunctionName(cmd.name),\n      path: `./${filePath.replace(/\\.ts$/, '.ts')}`,\n      aliases: cmd.aliases,\n    });\n  }\n\n  walkCommands(root, '', []);\n\n  // Generate root program.ts\n  const program = ctx.createCodeBuilder();\n\n  const rootHasArgs = (root.arguments && root.arguments.length > 0) || (root.positionals && root.positionals.length > 0);\n  if (rootHasArgs) {\n    program.import('z', 'zod/v4');\n  }\n  program.import(['createPadrone'], 'padrone');\n\n  // Only import direct children of root\n  const directChildren = rootImports.filter((imp) => imp.path.split('/').length <= 3);\n  for (const imp of directChildren) {\n    program.import([imp.varName], imp.path);\n  }\n\n  program.line();\n  program.line(`const program = createPadrone(${JSON.stringify(root.name)})`);\n\n  // .configure()\n  const configParts: string[] = [];\n  if (root.description) {\n    configParts.push(`description: ${JSON.stringify(root.description)}`);\n  }\n  if (configParts.length > 0) {\n    program.line(`  .configure({ ${configParts.join(', ')} })`);\n  }\n\n  // Root arguments (for programs that have options at the root level)\n  if (rootHasArgs) {\n    const allFields = [...(root.arguments || []), ...(root.positionals || [])];\n    const schemaCode = fieldMetaToCode(allFields);\n    program.line(`  .arguments(${schemaCode.code})`);\n  }\n\n  // Chain .command() calls for direct children\n  for (const imp of directChildren) {\n    const nameArg =\n      imp.aliases && imp.aliases.length > 0\n        ? `[${JSON.stringify(imp.name)}, ${imp.aliases.map((a) => JSON.stringify(a)).join(', ')}]`\n        : JSON.stringify(imp.name);\n    program.line(`  .command(${nameArg}, ${imp.varName})`);\n  }\n\n  // If root has no subcommands, add .wrap() or .action()\n  if (directChildren.length === 0 && options?.wrap) {\n    program.line(`  .wrap({ command: ${JSON.stringify(options.wrap.command)} })`);\n  }\n\n  program.line();\n  program.line(`export default program`);\n\n  ctx.emitter.addFile('program.ts', program.build());\n\n  // Generate index.ts\n  const index = ctx.createCodeBuilder();\n  index.line(`export { default } from './program.ts'`);\n  ctx.emitter.addFile('index.ts', index.build());\n}\n"],"mappings":";;;;;;;AAiBA,SAAgB,o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