cloudaxe-opencode/packages/opencode/src/index.ts

125 lines
3.3 KiB
TypeScript
Raw Normal View History

2025-05-31 00:47:56 +00:00
import "zod-openapi/extend"
2025-06-05 15:50:54 +00:00
import yargs from "yargs"
import { hideBin } from "yargs/helpers"
import { RunCommand } from "./cli/cmd/run"
import { GenerateCommand } from "./cli/cmd/generate"
import { Log } from "./util/log"
2025-06-27 15:29:20 +00:00
import { AuthCommand } from "./cli/cmd/auth"
2025-06-15 02:05:41 +00:00
import { UpgradeCommand } from "./cli/cmd/upgrade"
import { ModelsCommand } from "./cli/cmd/models"
2025-06-10 19:43:14 +00:00
import { UI } from "./cli/ui"
2025-06-17 05:05:05 +00:00
import { Installation } from "./installation"
2025-06-20 04:57:28 +00:00
import { NamedError } from "./util/error"
import { FormatError } from "./cli/error"
2025-06-25 00:52:09 +00:00
import { ServeCommand } from "./cli/cmd/serve"
2025-06-27 15:29:20 +00:00
import { TuiCommand } from "./cli/cmd/tui"
import { DebugCommand } from "./cli/cmd/debug"
2025-07-11 21:37:41 +00:00
import { StatsCommand } from "./cli/cmd/stats"
2025-07-13 00:25:41 +00:00
import { McpCommand } from "./cli/cmd/mcp"
2025-07-16 06:58:32 +00:00
import { InstallGithubCommand } from "./cli/cmd/install-github"
import { Trace } from "./trace"
Trace.init()
2025-06-23 01:10:05 +00:00
const cancel = new AbortController()
2025-06-25 12:41:10 +00:00
process.on("unhandledRejection", (e) => {
Log.Default.error("rejection", {
e: e instanceof Error ? e.message : e,
})
})
process.on("uncaughtException", (e) => {
Log.Default.error("exception", {
e: e instanceof Error ? e.message : e,
})
})
const cli = yargs(hideBin(process.argv))
.scriptName("opencode")
2025-06-23 17:00:21 +00:00
.help("help", "show help")
.version("version", "show version number", Installation.VERSION)
.alias("version", "v")
.option("print-logs", {
2025-06-23 17:00:21 +00:00
describe: "print logs to stderr",
type: "boolean",
})
2025-07-19 17:35:42 +00:00
.option("log-level", {
describe: "log level",
type: "string",
choices: ["DEBUG", "INFO", "WARN", "ERROR"],
})
.middleware(async (opts) => {
await Log.init({
print: process.argv.includes("--print-logs"),
dev: Installation.isDev(),
level: (() => {
if (opts.logLevel) return opts.logLevel as Log.Level
if (Installation.isDev()) return "DEBUG"
return "INFO"
})(),
})
2025-07-09 15:00:03 +00:00
Log.Default.info("opencode", {
version: Installation.VERSION,
args: process.argv.slice(2),
})
})
2025-06-11 23:00:09 +00:00
.usage("\n" + UI.logo())
2025-07-13 00:25:41 +00:00
.command(McpCommand)
2025-06-27 15:29:20 +00:00
.command(TuiCommand)
.command(RunCommand)
.command(GenerateCommand)
.command(DebugCommand)
.command(AuthCommand)
2025-06-15 02:05:41 +00:00
.command(UpgradeCommand)
2025-06-25 00:52:09 +00:00
.command(ServeCommand)
.command(ModelsCommand)
2025-07-11 21:37:41 +00:00
.command(StatsCommand)
2025-07-16 06:58:32 +00:00
.command(InstallGithubCommand)
2025-06-18 17:40:21 +00:00
.fail((msg) => {
if (msg.startsWith("Unknown argument") || msg.startsWith("Not enough non-option arguments")) {
cli.showHelp("log")
}
})
.strict()
try {
await cli.parse()
} catch (e) {
let data: Record<string, any> = {}
2025-06-20 04:57:28 +00:00
if (e instanceof NamedError) {
const obj = e.toObject()
Object.assign(data, {
...obj.data,
})
}
2025-06-27 20:09:33 +00:00
2025-06-20 04:57:28 +00:00
if (e instanceof Error) {
Object.assign(data, {
name: e.name,
message: e.message,
cause: e.cause?.toString(),
})
2025-06-27 20:09:33 +00:00
}
if (e instanceof ResolveMessage) {
Object.assign(data, {
name: e.name,
message: e.message,
code: e.code,
specifier: e.specifier,
referrer: e.referrer,
position: e.position,
importKind: e.importKind,
2025-06-27 20:09:33 +00:00
})
2025-06-20 04:57:28 +00:00
}
Log.Default.error("fatal", data)
const formatted = FormatError(e)
if (formatted) UI.error(formatted)
if (formatted === undefined) UI.error("Unexpected error, check log file at " + Log.file() + " for more details")
2025-06-27 20:09:33 +00:00
process.exitCode = 1
}
2025-06-23 01:10:05 +00:00
cancel.abort()