index.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import yargs from "yargs"
  2. import { hideBin } from "yargs/helpers"
  3. import { RunCommand } from "./cli/cmd/run"
  4. import { GenerateCommand } from "./cli/cmd/generate"
  5. import { Log } from "./util/log"
  6. import { AuthCommand } from "./cli/cmd/auth"
  7. import { AgentCommand } from "./cli/cmd/agent"
  8. import { UpgradeCommand } from "./cli/cmd/upgrade"
  9. import { ModelsCommand } from "./cli/cmd/models"
  10. import { UI } from "./cli/ui"
  11. import { Installation } from "./installation"
  12. import { NamedError } from "./util/error"
  13. import { FormatError } from "./cli/error"
  14. import { ServeCommand } from "./cli/cmd/serve"
  15. import { TuiCommand } from "./cli/cmd/tui"
  16. import { DebugCommand } from "./cli/cmd/debug"
  17. import { StatsCommand } from "./cli/cmd/stats"
  18. import { McpCommand } from "./cli/cmd/mcp"
  19. import { GithubCommand } from "./cli/cmd/github"
  20. import { ExportCommand } from "./cli/cmd/export"
  21. import { AttachCommand } from "./cli/cmd/attach"
  22. const cancel = new AbortController()
  23. process.on("unhandledRejection", (e) => {
  24. Log.Default.error("rejection", {
  25. e: e instanceof Error ? e.message : e,
  26. })
  27. })
  28. process.on("uncaughtException", (e) => {
  29. Log.Default.error("exception", {
  30. e: e instanceof Error ? e.message : e,
  31. })
  32. })
  33. const cli = yargs(hideBin(process.argv))
  34. .scriptName("opencode")
  35. .help("help", "show help")
  36. .version("version", "show version number", Installation.VERSION)
  37. .alias("version", "v")
  38. .option("print-logs", {
  39. describe: "print logs to stderr",
  40. type: "boolean",
  41. })
  42. .option("log-level", {
  43. describe: "log level",
  44. type: "string",
  45. choices: ["DEBUG", "INFO", "WARN", "ERROR"],
  46. })
  47. .middleware(async (opts) => {
  48. await Log.init({
  49. print: process.argv.includes("--print-logs"),
  50. dev: Installation.isLocal(),
  51. level: (() => {
  52. if (opts.logLevel) return opts.logLevel as Log.Level
  53. if (Installation.isLocal()) return "DEBUG"
  54. return "INFO"
  55. })(),
  56. })
  57. process.env["OPENCODE"] = "1"
  58. Log.Default.info("opencode", {
  59. version: Installation.VERSION,
  60. args: process.argv.slice(2),
  61. })
  62. })
  63. .usage("\n" + UI.logo())
  64. .command(McpCommand)
  65. .command(TuiCommand)
  66. .command(AttachCommand)
  67. .command(RunCommand)
  68. .command(GenerateCommand)
  69. .command(DebugCommand)
  70. .command(AuthCommand)
  71. .command(AgentCommand)
  72. .command(UpgradeCommand)
  73. .command(ServeCommand)
  74. .command(ModelsCommand)
  75. .command(StatsCommand)
  76. .command(ExportCommand)
  77. .command(GithubCommand)
  78. .fail((msg) => {
  79. if (
  80. msg.startsWith("Unknown argument") ||
  81. msg.startsWith("Not enough non-option arguments") ||
  82. msg.startsWith("Invalid values:")
  83. ) {
  84. cli.showHelp("log")
  85. }
  86. process.exit(1)
  87. })
  88. .strict()
  89. try {
  90. await cli.parse()
  91. } catch (e) {
  92. let data: Record<string, any> = {}
  93. if (e instanceof NamedError) {
  94. const obj = e.toObject()
  95. Object.assign(data, {
  96. ...obj.data,
  97. })
  98. }
  99. if (e instanceof Error) {
  100. Object.assign(data, {
  101. name: e.name,
  102. message: e.message,
  103. cause: e.cause?.toString(),
  104. stack: e.stack,
  105. })
  106. }
  107. if (e instanceof ResolveMessage) {
  108. Object.assign(data, {
  109. name: e.name,
  110. message: e.message,
  111. code: e.code,
  112. specifier: e.specifier,
  113. referrer: e.referrer,
  114. position: e.position,
  115. importKind: e.importKind,
  116. })
  117. }
  118. Log.Default.error("fatal", data)
  119. const formatted = FormatError(e)
  120. if (formatted) UI.error(formatted)
  121. if (formatted === undefined) UI.error("Unexpected error, check log file at " + Log.file() + " for more details")
  122. process.exitCode = 1
  123. }
  124. cancel.abort()