opencode_offline/packages/opencode/src/index.ts

108 lines
2.7 KiB
TypeScript
Raw Normal View History

2025-05-31 08:47:56 +08:00
import "zod-openapi/extend"
2025-06-05 23:50:54 +08: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 23:29:20 +08:00
import { AuthCommand } from "./cli/cmd/auth"
2025-06-15 10:05:41 +08:00
import { UpgradeCommand } from "./cli/cmd/upgrade"
import { ModelsCommand } from "./cli/cmd/models"
2025-06-11 03:43:14 +08:00
import { UI } from "./cli/ui"
2025-06-17 13:05:05 +08:00
import { Installation } from "./installation"
2025-06-20 12:57:28 +08:00
import { NamedError } from "./util/error"
import { FormatError } from "./cli/error"
2025-06-25 08:52:09 +08:00
import { ServeCommand } from "./cli/cmd/serve"
2025-06-27 23:29:20 +08:00
import { TuiCommand } from "./cli/cmd/tui"
import { DebugCommand } from "./cli/cmd/debug"
2025-06-23 09:10:05 +08:00
const cancel = new AbortController()
2025-06-25 20:41:10 +08: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-24 01:00:21 +08:00
.help("help", "show help")
.version("version", "show version number", Installation.VERSION)
.alias("version", "v")
.option("print-logs", {
2025-06-24 01:00:21 +08:00
describe: "print logs to stderr",
type: "boolean",
})
.middleware(async () => {
await Log.init({ print: process.argv.includes("--print-logs") })
Log.Default.info("opencode", {
version: Installation.VERSION,
args: process.argv.slice(2),
})
})
2025-06-12 07:00:09 +08:00
.usage("\n" + UI.logo())
2025-06-27 23:29:20 +08:00
.command(TuiCommand)
.command(RunCommand)
.command(GenerateCommand)
.command(DebugCommand)
.command(AuthCommand)
2025-06-15 10:05:41 +08:00
.command(UpgradeCommand)
2025-06-25 08:52:09 +08:00
.command(ServeCommand)
.command(ModelsCommand)
2025-06-19 01:40:21 +08: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 12:57:28 +08:00
if (e instanceof NamedError) {
const obj = e.toObject()
Object.assign(data, {
...obj.data,
})
}
2025-06-28 04:09:33 +08:00
2025-06-20 12:57:28 +08:00
if (e instanceof Error) {
Object.assign(data, {
name: e.name,
message: e.message,
cause: e.cause?.toString(),
})
2025-06-28 04:09:33 +08: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-28 04:09:33 +08:00
})
2025-06-20 12:57:28 +08:00
}
Log.Default.error("fatal", data)
const formatted = FormatError(e)
if (formatted) UI.error(formatted)
if (formatted === undefined)
2025-06-20 12:57:28 +08:00
UI.error(
"Unexpected error, check log file at " + Log.file() + " for more details",
)
2025-06-28 04:09:33 +08:00
process.exitCode = 1
}
2025-06-23 09:10:05 +08:00
cancel.abort()