index.ts 3.3 KB

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