|
| 1 | +import { Command } from 'commander'; |
| 2 | +import { registerAssetCommands } from './commands/assets'; |
| 3 | +import { registerBuildCommands } from './commands/build'; |
| 4 | +import { registerChunkCommands } from './commands/chunks'; |
| 5 | +import { registerErrorCommands } from './commands/errors'; |
| 6 | +import { registerLoaderCommands } from './commands/loaders'; |
| 7 | +import { registerModuleCommands } from './commands/modules'; |
| 8 | +import { registerPackageCommands } from './commands/packages'; |
| 9 | +import { registerRuleCommands } from './commands/rules'; |
| 10 | +import { registerServerCommands } from './commands/server'; |
| 11 | +import { closeAllSockets } from './socket'; |
| 12 | +import { printResult } from './utils/cli-utils'; |
| 13 | + |
| 14 | +export const program = new Command(); |
| 15 | +program |
| 16 | + .name('rsdoctor-skill') |
| 17 | + .description('Rsdoctor skill CLI') |
| 18 | + .option('--data-file <path>', 'Path to rsdoctor-data.json file (required)') |
| 19 | + .option('--compact', 'Compact JSON output') |
| 20 | + .showHelpAfterError() |
| 21 | + .showSuggestionAfterError(); |
| 22 | + |
| 23 | +export const execute = async ( |
| 24 | + handler: () => Promise<unknown>, |
| 25 | +): Promise<void> => { |
| 26 | + // Parse compact option once at the beginning |
| 27 | + const opts = program.opts<{ compact?: boolean | string }>(); |
| 28 | + const compact = opts.compact === true || opts.compact === 'true'; |
| 29 | + const spacing = compact ? 0 : 2; |
| 30 | + |
| 31 | + try { |
| 32 | + const result = await handler(); |
| 33 | + // Format result similar to old format |
| 34 | + if (result && typeof result === 'object' && 'ok' in result) { |
| 35 | + console.log(JSON.stringify(result, null, spacing)); |
| 36 | + if (!(result as { ok: boolean }).ok) { |
| 37 | + process.exit(1); |
| 38 | + } |
| 39 | + } else { |
| 40 | + printResult(result, compact); |
| 41 | + } |
| 42 | + } catch (error) { |
| 43 | + const message = error instanceof Error ? error.message : String(error); |
| 44 | + console.log( |
| 45 | + JSON.stringify( |
| 46 | + { |
| 47 | + ok: false, |
| 48 | + error: message, |
| 49 | + }, |
| 50 | + null, |
| 51 | + spacing, |
| 52 | + ), |
| 53 | + ); |
| 54 | + process.exit(1); |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +// Register all command groups |
| 59 | +registerChunkCommands(program, execute); |
| 60 | +registerModuleCommands(program, execute); |
| 61 | +registerPackageCommands(program, execute); |
| 62 | +registerRuleCommands(program, execute); |
| 63 | +registerAssetCommands(program, execute); |
| 64 | +registerLoaderCommands(program, execute); |
| 65 | +registerBuildCommands(program, execute); |
| 66 | +registerErrorCommands(program, execute); |
| 67 | +registerServerCommands(program, execute); |
| 68 | + |
| 69 | +export async function run(): Promise<void> { |
| 70 | + if (process.argv.length <= 2) { |
| 71 | + program.help({ error: true }); |
| 72 | + } |
| 73 | + await program.parseAsync(process.argv); |
| 74 | + |
| 75 | + // Cleanup |
| 76 | + closeAllSockets(); |
| 77 | +} |
0 commit comments