From 9741a6703cbd1abd4ff292b8d254e6b9100d2a2d Mon Sep 17 00:00:00 2001 From: Frank Date: Sun, 27 Jul 2025 11:56:18 -0400 Subject: [PATCH] fix input format affected by installing vscode extension --- packages/opencode/src/cli/cmd/tui.ts | 10 ++--- packages/opencode/src/ide/index.ts | 60 ++++++++++++++-------------- 2 files changed, 33 insertions(+), 37 deletions(-) diff --git a/packages/opencode/src/cli/cmd/tui.ts b/packages/opencode/src/cli/cmd/tui.ts index 791faadd..37317329 100644 --- a/packages/opencode/src/cli/cmd/tui.ts +++ b/packages/opencode/src/cli/cmd/tui.ts @@ -123,19 +123,15 @@ export const TuiCommand = cmd({ const method = await Installation.method() if (method === "unknown") return await Installation.upgrade(method, latest) - .then(() => { - Bus.publish(Installation.Event.Updated, { version: latest }) - }) + .then(() => Bus.publish(Installation.Event.Updated, { version: latest })) .catch(() => {}) })() ;(async () => { if (Ide.alreadyInstalled()) return - const ide = await Ide.ide() + const ide = Ide.ide() if (ide === "unknown") return await Ide.install(ide) - .then(() => { - Bus.publish(Ide.Event.Installed, { ide }) - }) + .then(() => Bus.publish(Ide.Event.Installed, { ide })) .catch(() => {}) })() diff --git a/packages/opencode/src/ide/index.ts b/packages/opencode/src/ide/index.ts index 78094713..300aa9f5 100644 --- a/packages/opencode/src/ide/index.ts +++ b/packages/opencode/src/ide/index.ts @@ -1,10 +1,15 @@ -import { $ } from "bun" +import { spawn } from "bun" import { z } from "zod" import { NamedError } from "../util/error" import { Log } from "../util/log" import { Bus } from "../bus" -const SUPPORTED_IDES = ["Windsurf", "Visual Studio Code", "Cursor", "VSCodium"] as const +const SUPPORTED_IDES = [ + { name: "Windsurf" as const, cmd: "windsurf" }, + { name: "Visual Studio Code" as const, cmd: "code" }, + { name: "Cursor" as const, cmd: "cursor" }, + { name: "VSCodium" as const, cmd: "codium" }, +] export namespace Ide { const log = Log.create({ service: "ide" }) @@ -18,8 +23,6 @@ export namespace Ide { ), } - export type Ide = Awaited> - export const AlreadyInstalledError = NamedError.create("AlreadyInstalledError", z.object({})) export const InstallFailedError = NamedError.create( @@ -29,11 +32,11 @@ export namespace Ide { }), ) - export async function ide() { + export function ide() { if (process.env["TERM_PROGRAM"] === "vscode") { const v = process.env["GIT_ASKPASS"] for (const ide of SUPPORTED_IDES) { - if (v?.includes(ide)) return ide + if (v?.includes(ide.name)) return ide.name } } return "unknown" @@ -43,32 +46,29 @@ export namespace Ide { return process.env["OPENCODE_CALLER"] === "vscode" } - export async function install(ide: Ide) { - const cmd = (() => { - switch (ide) { - case "Windsurf": - return $`windsurf --install-extension sst-dev.opencode` - case "Visual Studio Code": - return $`code --install-extension sst-dev.opencode` - case "Cursor": - return $`cursor --install-extension sst-dev.opencode` - case "VSCodium": - return $`codium --install-extension sst-dev.opencode` - default: - throw new Error(`Unknown IDE: ${ide}`) - } - })() - // TODO: check OPENCODE_CALLER - const result = await cmd.quiet().throws(false) + export async function install(ide: (typeof SUPPORTED_IDES)[number]["name"]) { + const cmd = SUPPORTED_IDES.find((i) => i.name === ide)?.cmd + if (!cmd) throw new Error(`Unknown IDE: ${ide}`) + + const p = spawn([cmd, "--install-extension", "sst-dev.opencode"], { + stdout: "pipe", + stderr: "pipe", + }) + await p.exited + const stdout = await new Response(p.stdout).text() + const stderr = await new Response(p.stderr).text() + log.info("installed", { ide, - stdout: result.stdout.toString(), - stderr: result.stderr.toString(), + stdout, + stderr, }) - if (result.exitCode !== 0) - throw new InstallFailedError({ - stderr: result.stderr.toString("utf8"), - }) - if (result.stdout.toString().includes("already installed")) throw new AlreadyInstalledError({}) + + if (p.exitCode !== 0) { + throw new InstallFailedError({ stderr }) + } + if (stdout.includes("already installed")) { + throw new AlreadyInstalledError({}) + } } }