|
1 | | -export * as Ide from "./ide" |
| 1 | +import { BusEvent } from "@/bus/bus-event" |
| 2 | +import z from "zod" |
| 3 | +import { NamedError } from "@opencode-ai/shared/util/error" |
| 4 | +import { Log } from "../util" |
| 5 | +import { Process } from "@/util" |
| 6 | + |
| 7 | +const SUPPORTED_IDES = [ |
| 8 | + { name: "Windsurf" as const, cmd: "windsurf" }, |
| 9 | + { name: "Visual Studio Code - Insiders" as const, cmd: "code-insiders" }, |
| 10 | + { name: "Visual Studio Code" as const, cmd: "code" }, |
| 11 | + { name: "Cursor" as const, cmd: "cursor" }, |
| 12 | + { name: "VSCodium" as const, cmd: "codium" }, |
| 13 | +] |
| 14 | + |
| 15 | +const log = Log.create({ service: "ide" }) |
| 16 | + |
| 17 | +export const Event = { |
| 18 | + Installed: BusEvent.define( |
| 19 | + "ide.installed", |
| 20 | + z.object({ |
| 21 | + ide: z.string(), |
| 22 | + }), |
| 23 | + ), |
| 24 | +} |
| 25 | + |
| 26 | +export const AlreadyInstalledError = NamedError.create("AlreadyInstalledError", z.object({})) |
| 27 | + |
| 28 | +export const InstallFailedError = NamedError.create( |
| 29 | + "InstallFailedError", |
| 30 | + z.object({ |
| 31 | + stderr: z.string(), |
| 32 | + }), |
| 33 | +) |
| 34 | + |
| 35 | +export function ide() { |
| 36 | + if (process.env["TERM_PROGRAM"] === "vscode") { |
| 37 | + const v = process.env["GIT_ASKPASS"] |
| 38 | + for (const ide of SUPPORTED_IDES) { |
| 39 | + if (v?.includes(ide.name)) return ide.name |
| 40 | + } |
| 41 | + } |
| 42 | + return "unknown" |
| 43 | +} |
| 44 | + |
| 45 | +export function alreadyInstalled() { |
| 46 | + return process.env["OPENCODE_CALLER"] === "vscode" || process.env["OPENCODE_CALLER"] === "vscode-insiders" |
| 47 | +} |
| 48 | + |
| 49 | +export async function install(ide: (typeof SUPPORTED_IDES)[number]["name"]) { |
| 50 | + const cmd = SUPPORTED_IDES.find((i) => i.name === ide)?.cmd |
| 51 | + if (!cmd) throw new Error(`Unknown IDE: ${ide}`) |
| 52 | + |
| 53 | + const p = await Process.run([cmd, "--install-extension", "sst-dev.opencode"], { |
| 54 | + nothrow: true, |
| 55 | + }) |
| 56 | + const stdout = p.stdout.toString() |
| 57 | + const stderr = p.stderr.toString() |
| 58 | + |
| 59 | + log.info("installed", { |
| 60 | + ide, |
| 61 | + stdout, |
| 62 | + stderr, |
| 63 | + }) |
| 64 | + |
| 65 | + if (p.code !== 0) { |
| 66 | + throw new InstallFailedError({ stderr }) |
| 67 | + } |
| 68 | + if (stdout.includes("already installed")) { |
| 69 | + throw new AlreadyInstalledError({}) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +export * as Ide from "." |
0 commit comments