|
| 1 | +import { defineCommand } from 'citty'; |
| 2 | + |
| 3 | +import { cliToConfig } from './adapter'; |
| 4 | +import type { RunCliOptions } from './types'; |
| 5 | + |
| 6 | +export function createCommand(options: RunCliOptions) { |
| 7 | + return defineCommand({ |
| 8 | + args: { |
| 9 | + client: { |
| 10 | + alias: 'c', |
| 11 | + description: 'HTTP client to generate', |
| 12 | + type: 'string', |
| 13 | + }, |
| 14 | + debug: { |
| 15 | + alias: 'd', |
| 16 | + description: 'Enable debug logging', |
| 17 | + type: 'boolean', |
| 18 | + }, |
| 19 | + 'dry-run': { |
| 20 | + description: 'Skip writing files', |
| 21 | + type: 'boolean', |
| 22 | + }, |
| 23 | + file: { |
| 24 | + alias: 'f', |
| 25 | + description: 'Path to config file', |
| 26 | + type: 'string', |
| 27 | + }, |
| 28 | + input: { |
| 29 | + alias: 'i', |
| 30 | + description: 'OpenAPI specification(s), comma-separated (path, URL, or string)', |
| 31 | + type: 'string', |
| 32 | + valueHint: 'paths', |
| 33 | + }, |
| 34 | + 'log-file': { |
| 35 | + default: true, |
| 36 | + negativeDescription: 'Disable log file output', |
| 37 | + type: 'boolean', |
| 38 | + }, |
| 39 | + logs: { |
| 40 | + alias: 'l', |
| 41 | + description: 'Logs folder path', |
| 42 | + type: 'string', |
| 43 | + }, |
| 44 | + output: { |
| 45 | + alias: 'o', |
| 46 | + description: 'Output folder(s), comma-separated', |
| 47 | + type: 'string', |
| 48 | + valueHint: 'paths', |
| 49 | + }, |
| 50 | + plugins: { |
| 51 | + alias: 'p', |
| 52 | + description: 'Plugins to use (comma-separated)', |
| 53 | + type: 'string', |
| 54 | + valueHint: 'names', |
| 55 | + }, |
| 56 | + silent: { |
| 57 | + alias: 's', |
| 58 | + description: 'Suppress all output', |
| 59 | + type: 'boolean', |
| 60 | + }, |
| 61 | + watch: { |
| 62 | + alias: 'w', |
| 63 | + description: 'Watch for changes (optional interval in ms)', |
| 64 | + type: 'string', |
| 65 | + valueHint: 'ms', |
| 66 | + }, |
| 67 | + }, |
| 68 | + meta: { |
| 69 | + description: options.meta.description, |
| 70 | + name: options.meta.name, |
| 71 | + version: |
| 72 | + process.env.HEYAPI_CODEGEN_ENV === 'development' ? '[DEVELOPMENT]' : options.meta.version, |
| 73 | + }, |
| 74 | + async run({ args }) { |
| 75 | + const config = cliToConfig(args); |
| 76 | + console.log('[args]', args); |
| 77 | + console.log('[config]', config); |
| 78 | + const context = await options.createClient(config); |
| 79 | + const hasActiveWatch = context[0]?.config.input.some((input) => input.watch?.enabled); |
| 80 | + if (!hasActiveWatch) { |
| 81 | + process.exit(0); |
| 82 | + } |
| 83 | + }, |
| 84 | + }); |
| 85 | +} |
0 commit comments