fix input format affected by installing vscode extension

This commit is contained in:
Frank
2025-07-27 11:56:18 -04:00
parent 27a079d9cb
commit 9741a6703c
2 changed files with 33 additions and 37 deletions

View File

@@ -123,19 +123,15 @@ export const TuiCommand = cmd({
const method = await Installation.method() const method = await Installation.method()
if (method === "unknown") return if (method === "unknown") return
await Installation.upgrade(method, latest) await Installation.upgrade(method, latest)
.then(() => { .then(() => Bus.publish(Installation.Event.Updated, { version: latest }))
Bus.publish(Installation.Event.Updated, { version: latest })
})
.catch(() => {}) .catch(() => {})
})() })()
;(async () => { ;(async () => {
if (Ide.alreadyInstalled()) return if (Ide.alreadyInstalled()) return
const ide = await Ide.ide() const ide = Ide.ide()
if (ide === "unknown") return if (ide === "unknown") return
await Ide.install(ide) await Ide.install(ide)
.then(() => { .then(() => Bus.publish(Ide.Event.Installed, { ide }))
Bus.publish(Ide.Event.Installed, { ide })
})
.catch(() => {}) .catch(() => {})
})() })()

View File

@@ -1,10 +1,15 @@
import { $ } from "bun" import { spawn } from "bun"
import { z } from "zod" import { z } from "zod"
import { NamedError } from "../util/error" import { NamedError } from "../util/error"
import { Log } from "../util/log" import { Log } from "../util/log"
import { Bus } from "../bus" 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 { export namespace Ide {
const log = Log.create({ service: "ide" }) const log = Log.create({ service: "ide" })
@@ -18,8 +23,6 @@ export namespace Ide {
), ),
} }
export type Ide = Awaited<ReturnType<typeof ide>>
export const AlreadyInstalledError = NamedError.create("AlreadyInstalledError", z.object({})) export const AlreadyInstalledError = NamedError.create("AlreadyInstalledError", z.object({}))
export const InstallFailedError = NamedError.create( 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") { if (process.env["TERM_PROGRAM"] === "vscode") {
const v = process.env["GIT_ASKPASS"] const v = process.env["GIT_ASKPASS"]
for (const ide of SUPPORTED_IDES) { for (const ide of SUPPORTED_IDES) {
if (v?.includes(ide)) return ide if (v?.includes(ide.name)) return ide.name
} }
} }
return "unknown" return "unknown"
@@ -43,32 +46,29 @@ export namespace Ide {
return process.env["OPENCODE_CALLER"] === "vscode" return process.env["OPENCODE_CALLER"] === "vscode"
} }
export async function install(ide: Ide) { export async function install(ide: (typeof SUPPORTED_IDES)[number]["name"]) {
const cmd = (() => { const cmd = SUPPORTED_IDES.find((i) => i.name === ide)?.cmd
switch (ide) { if (!cmd) throw new Error(`Unknown IDE: ${ide}`)
case "Windsurf":
return $`windsurf --install-extension sst-dev.opencode` const p = spawn([cmd, "--install-extension", "sst-dev.opencode"], {
case "Visual Studio Code": stdout: "pipe",
return $`code --install-extension sst-dev.opencode` stderr: "pipe",
case "Cursor": })
return $`cursor --install-extension sst-dev.opencode` await p.exited
case "VSCodium": const stdout = await new Response(p.stdout).text()
return $`codium --install-extension sst-dev.opencode` const stderr = await new Response(p.stderr).text()
default:
throw new Error(`Unknown IDE: ${ide}`)
}
})()
// TODO: check OPENCODE_CALLER
const result = await cmd.quiet().throws(false)
log.info("installed", { log.info("installed", {
ide, ide,
stdout: result.stdout.toString(), stdout,
stderr: result.stderr.toString(), stderr,
}) })
if (result.exitCode !== 0)
throw new InstallFailedError({ if (p.exitCode !== 0) {
stderr: result.stderr.toString("utf8"), throw new InstallFailedError({ stderr })
}) }
if (result.stdout.toString().includes("already installed")) throw new AlreadyInstalledError({}) if (stdout.includes("already installed")) {
throw new AlreadyInstalledError({})
}
} }
} }