Просмотр исходного кода

fix input format affected by installing vscode extension

Frank 7 месяцев назад
Родитель
Сommit
9741a6703c
2 измененных файлов с 33 добавлено и 37 удалено
  1. 3 7
      packages/opencode/src/cli/cmd/tui.ts
  2. 30 30
      packages/opencode/src/ide/index.ts

+ 3 - 7
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(() => {})
         })()
 

+ 30 - 30
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<ReturnType<typeof ide>>
-
   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({})
+    }
   }
 }