opencode_offline/packages/opencode/src/index.ts

83 lines
2.4 KiB
TypeScript
Raw Normal View History

2025-05-31 08:47:56 +08:00
import "zod-openapi/extend"
import { App } from "./app/app"
import { Server } from "./server/server"
import fs from "fs/promises"
import path from "path"
2025-06-05 23:50:54 +08:00
2025-05-31 08:47:56 +08:00
import { Share } from "./share/share"
2025-06-05 23:50:54 +08:00
2025-05-31 09:26:53 +08:00
import { Global } from "./global"
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"
2025-06-06 11:42:04 +08:00
import { VERSION } from "./cli/version"
import { ScrapCommand } from "./cli/cmd/scrap"
import { Log } from "./util/log"
2025-06-11 03:43:14 +08:00
import { ProviderAddCommand, ProviderCommand } from "./cli/cmd/provider"
import { Provider } from "./provider/provider"
import { UI } from "./cli/ui"
2025-06-10 03:00:48 +08:00
await Log.init({ print: process.argv.includes("--print-logs") })
2025-05-19 10:30:41 +08:00
2025-06-05 23:50:54 +08:00
yargs(hideBin(process.argv))
.scriptName("opencode")
2025-06-06 11:42:04 +08:00
.version(VERSION)
2025-06-05 23:50:54 +08:00
.command({
command: "$0",
describe: "Start OpenCode TUI",
2025-06-07 11:58:09 +08:00
builder: (yargs) =>
yargs.option("print-logs", {
type: "boolean",
}),
handler: async (args) => {
2025-06-11 06:10:30 +08:00
UI.logo()
await App.provide({ cwd: process.cwd(), version: VERSION }, async () => {
2025-06-11 03:43:14 +08:00
const providers = await Provider.list()
if (Object.keys(providers).length === 0) {
await ProviderAddCommand.handler(args)
return
}
await Share.init()
const server = Server.listen()
2025-06-05 23:50:54 +08:00
let cmd = ["go", "run", "./main.go"]
let cwd = new URL("../../tui/cmd/opencode", import.meta.url).pathname
if (Bun.embeddedFiles.length > 0) {
const blob = Bun.embeddedFiles[0] as File
const binary = path.join(Global.Path.cache, "tui", blob.name)
const file = Bun.file(binary)
if (!(await file.exists())) {
await Bun.write(file, blob, { mode: 0o755 })
await fs.chmod(binary, 0o755)
2025-06-05 08:49:28 +08:00
}
cwd = process.cwd()
cmd = [binary]
}
const proc = Bun.spawn({
cmd,
cwd,
stdout: "inherit",
stderr: "inherit",
stdin: "inherit",
env: {
...process.env,
OPENCODE_SERVER: server.url.toString(),
},
onExit: () => {
server.stop()
},
})
await proc.exited
await server.stop()
})
2025-06-05 23:50:54 +08:00
},
2025-05-31 08:47:56 +08:00
})
2025-06-05 23:50:54 +08:00
.command(RunCommand)
.command(GenerateCommand)
2025-06-06 11:42:04 +08:00
.command(ScrapCommand)
2025-06-11 01:30:08 +08:00
.command(ProviderCommand)
2025-06-05 23:50:54 +08:00
.parse()