2026-05-10 15:32:36 +09:00
|
|
|
import { extractSemverFromOutput } from "../../shared/extract-semver"
|
2026-02-08 15:01:42 +09:00
|
|
|
import type { OpenCodeBinaryType } from "../../shared/opencode-config-dir-types"
|
2026-02-26 21:00:24 +09:00
|
|
|
import { spawnWithWindowsHide } from "../../shared/spawn-with-windows-hide"
|
2026-02-08 15:01:42 +09:00
|
|
|
import { initConfigContext } from "./config-context"
|
|
|
|
|
|
|
|
|
|
const OPENCODE_BINARIES = ["opencode", "opencode-desktop"] as const
|
2026-05-10 15:07:23 +09:00
|
|
|
const OPENCODE_VERSION_CHECK_TIMEOUT_MS = 1500
|
|
|
|
|
const OPENCODE_VERSION_KILL_GRACE_MS = 200
|
2026-05-11 09:24:42 +09:00
|
|
|
const OPENCODE_OUTPUT_WAIT_TIMEOUT_MS = 200
|
2026-02-08 15:01:42 +09:00
|
|
|
|
|
|
|
|
interface OpenCodeBinaryResult {
|
|
|
|
|
binary: OpenCodeBinaryType
|
|
|
|
|
version: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function findOpenCodeBinaryWithVersion(): Promise<OpenCodeBinaryResult | null> {
|
|
|
|
|
for (const binary of OPENCODE_BINARIES) {
|
|
|
|
|
try {
|
2026-02-26 21:00:24 +09:00
|
|
|
const proc = spawnWithWindowsHide([binary, "--version"], {
|
2026-02-08 15:01:42 +09:00
|
|
|
stdout: "pipe",
|
|
|
|
|
stderr: "pipe",
|
|
|
|
|
})
|
2026-05-10 15:07:23 +09:00
|
|
|
|
|
|
|
|
const outputPromise = new Response(proc.stdout).text()
|
|
|
|
|
let killTimer: ReturnType<typeof setTimeout> | null = null
|
|
|
|
|
const timedExitCode = await Promise.race([
|
|
|
|
|
proc.exited,
|
|
|
|
|
new Promise<number>((resolve) => {
|
|
|
|
|
killTimer = setTimeout(() => {
|
|
|
|
|
proc.kill("SIGTERM")
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
proc.kill("SIGKILL")
|
|
|
|
|
}, OPENCODE_VERSION_KILL_GRACE_MS)
|
|
|
|
|
resolve(1)
|
|
|
|
|
}, OPENCODE_VERSION_CHECK_TIMEOUT_MS)
|
|
|
|
|
}),
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
if (killTimer) {
|
|
|
|
|
clearTimeout(killTimer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const output = await Promise.race([
|
|
|
|
|
outputPromise,
|
|
|
|
|
new Promise<string>((resolve) => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
resolve("")
|
2026-05-11 09:24:42 +09:00
|
|
|
}, OPENCODE_OUTPUT_WAIT_TIMEOUT_MS)
|
2026-05-10 15:07:23 +09:00
|
|
|
}),
|
2026-05-11 09:24:42 +09:00
|
|
|
]).catch(() => "")
|
2026-05-10 15:07:23 +09:00
|
|
|
|
|
|
|
|
if (timedExitCode === 0 && proc.exitCode === 0) {
|
2026-05-10 15:32:36 +09:00
|
|
|
const version = extractSemverFromOutput(output) ?? output.trim()
|
2026-02-08 15:01:42 +09:00
|
|
|
initConfigContext(binary, version)
|
|
|
|
|
return { binary, version }
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function isOpenCodeInstalled(): Promise<boolean> {
|
|
|
|
|
const result = await findOpenCodeBinaryWithVersion()
|
|
|
|
|
return result !== null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getOpenCodeVersion(): Promise<string | null> {
|
|
|
|
|
const result = await findOpenCodeBinaryWithVersion()
|
|
|
|
|
return result?.version ?? null
|
|
|
|
|
}
|