|
| 1 | +import { Global } from "../../global" |
| 2 | +import { cmd } from "./cmd" |
| 3 | +import path from "path" |
| 4 | +import fs from "fs/promises" |
| 5 | +import { Log } from "../../util/log" |
| 6 | + |
| 7 | +import { $ } from "bun" |
| 8 | + |
| 9 | +export const AttachCommand = cmd({ |
| 10 | + command: "attach <server>", |
| 11 | + describe: "attach to a running opencode server", |
| 12 | + builder: (yargs) => |
| 13 | + yargs.positional("server", { |
| 14 | + type: "string", |
| 15 | + describe: "http://localhost:4096", |
| 16 | + }), |
| 17 | + handler: async (args) => { |
| 18 | + let cmd = [] as string[] |
| 19 | + const tui = Bun.embeddedFiles.find((item) => (item as File).name.includes("tui")) as File |
| 20 | + if (tui) { |
| 21 | + let binaryName = tui.name |
| 22 | + if (process.platform === "win32" && !binaryName.endsWith(".exe")) { |
| 23 | + binaryName += ".exe" |
| 24 | + } |
| 25 | + const binary = path.join(Global.Path.cache, "tui", binaryName) |
| 26 | + const file = Bun.file(binary) |
| 27 | + if (!(await file.exists())) { |
| 28 | + await Bun.write(file, tui, { mode: 0o755 }) |
| 29 | + if (process.platform !== "win32") await fs.chmod(binary, 0o755) |
| 30 | + } |
| 31 | + cmd = [binary] |
| 32 | + } |
| 33 | + if (!tui) { |
| 34 | + const dir = Bun.fileURLToPath(new URL("../../../../tui/cmd/opencode", import.meta.url)) |
| 35 | + let binaryName = `./dist/tui${process.platform === "win32" ? ".exe" : ""}` |
| 36 | + await $`go build -o ${binaryName} ./main.go`.cwd(dir) |
| 37 | + cmd = [path.join(dir, binaryName)] |
| 38 | + } |
| 39 | + Log.Default.info("tui", { |
| 40 | + cmd, |
| 41 | + }) |
| 42 | + const proc = Bun.spawn({ |
| 43 | + cmd, |
| 44 | + stdout: "inherit", |
| 45 | + stderr: "inherit", |
| 46 | + stdin: "inherit", |
| 47 | + env: { |
| 48 | + ...process.env, |
| 49 | + CGO_ENABLED: "0", |
| 50 | + OPENCODE_SERVER: args.server, |
| 51 | + }, |
| 52 | + }) |
| 53 | + |
| 54 | + await proc.exited |
| 55 | + }, |
| 56 | +}) |
0 commit comments