index.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env node
  2. import { Command } from 'commander'
  3. import { readCliVersion } from './lib/version.js'
  4. const program = new Command()
  5. program
  6. .name('oac')
  7. .description('OpenAgents Control — install, manage, and update AI agents and context files')
  8. .version(readCliVersion(), '-v, --version', 'Print version and exit')
  9. // Lazy-load command modules in parallel — keeps startup < 100ms
  10. async function main(): Promise<void> {
  11. // Fast path: --version only — --help needs all commands registered first
  12. const args = process.argv.slice(2)
  13. const isFastPath =
  14. args.includes('--version') || args.includes('-v')
  15. if (isFastPath) {
  16. await program.parseAsync(process.argv)
  17. return
  18. }
  19. const [
  20. { registerInitCommand },
  21. { registerUpdateCommand },
  22. { registerAddCommand },
  23. { registerApplyCommand },
  24. { registerDoctorCommand },
  25. { registerListCommand },
  26. { registerStatusCommand },
  27. ] = await Promise.all([
  28. import('./commands/init.js'),
  29. import('./commands/update.js'),
  30. import('./commands/add.js'),
  31. import('./commands/apply.js'),
  32. import('./commands/doctor.js'),
  33. import('./commands/list.js'),
  34. import('./commands/status.js'),
  35. ])
  36. registerInitCommand(program)
  37. registerUpdateCommand(program)
  38. registerAddCommand(program) // also registers `remove`
  39. registerApplyCommand(program)
  40. registerDoctorCommand(program)
  41. registerListCommand(program)
  42. registerStatusCommand(program)
  43. // Unknown commands: print a helpful error and exit 1
  44. program.on('command:*', (operands: string[]) => {
  45. console.error(`error: unknown command '${operands[0]}'\n`)
  46. console.error(`Run 'oac --help' to see available commands.`)
  47. process.exitCode = 1
  48. })
  49. await program.parseAsync(process.argv)
  50. // Print help when no command is given
  51. if (args.length === 0) {
  52. program.help()
  53. }
  54. }
  55. main().catch((err: unknown) => {
  56. console.error('Fatal error:', err instanceof Error ? err.message : String(err))
  57. process.exitCode = 1
  58. })