Add OpenCode install method detection and improve process termination handling

This commit is contained in:
Chris Scott committed 2026-02-03 16:04:32 -05:00
1 parent bdd0408346
commit e3cf6482ef
2 files changed
+41 -6

No files matched your search

+29 -4
View File
@@ -1,6 +1,8 @@
import { Hono } from 'hono'
import { z } from 'zod'
import { execSync } from 'child_process'
import { existsSync } from 'fs'
import { resolve, dirname } from 'path'
import type { Database } from 'bun:sqlite'
import { SettingsService } from '../services/settings'
import { writeFileContent, readFileContent, fileExists } from '../services/file-operations'
@@ -27,6 +29,27 @@ function compareVersions(v1: string, v2: string): number {
return 0
}
function getOpenCodeInstallMethod(): string {
const homePath = process.env.HOME || ''
const opencodePath = process.env.OPENCOD_PATH || resolve(homePath, '.opencode', 'bin', 'opencode')
if (!existsSync(opencodePath)) return 'curl'
try {
const opencodeDir = dirname(opencodePath)
if (opencodeDir.includes('.opencode')) return 'curl'
if (opencodePath.includes('/homebrew/') || opencodePath.includes('/HOMEBREW/')) return 'brew'
if (opencodePath.includes('/.npm/') || opencodePath.includes('/node_modules/')) return 'npm'
if (opencodePath.includes('/.pnpm/')) return 'pnpm'
if (opencodePath.includes('/.bun/')) return 'bun'
} catch {
return 'curl'
}
return 'curl'
}
function execWithTimeout(command: string, timeoutMs: number): { output: string; timedOut: boolean } {
try {
const output = execSync(command, {
@@ -396,8 +419,9 @@ export function createSettingsRoutes(db: Database) {
logger.info(`Current OpenCode version: ${oldVersion}`)
try {
logger.info('Running opencode upgrade with 90s timeout...')
const { output: upgradeOutput, timedOut } = execWithTimeout('opencode upgrade 2>&1', 90000)
const installMethod = getOpenCodeInstallMethod()
logger.info(`Running opencode upgrade --method ${installMethod} with 90s timeout...`)
const { output: upgradeOutput, timedOut } = execWithTimeout(`opencode upgrade --method ${installMethod} 2>&1`, 90000)
logger.info(`Upgrade output: ${upgradeOutput}`)
if (timedOut) {
@@ -546,9 +570,10 @@ export function createSettingsRoutes(db: Database) {
logger.info(`Installing OpenCode version: ${version}`)
const versionArg = version.startsWith('v') ? version : `v${version}`
logger.info(`Running opencode upgrade ${versionArg} with 90s timeout...`)
const installMethod = getOpenCodeInstallMethod()
logger.info(`Running opencode upgrade ${versionArg} --method ${installMethod} with 90s timeout...`)
const { output: upgradeOutput, timedOut } = execWithTimeout(`opencode upgrade ${versionArg} 2>&1`, 90000)
const { output: upgradeOutput, timedOut } = execWithTimeout(`opencode upgrade ${versionArg} --method ${installMethod} 2>&1`, 90000)
logger.info(`Upgrade output: ${upgradeOutput}`)
if (timedOut) {
+12 -2
View File
@@ -185,7 +185,12 @@ class OpenCodeServerManager {
try {
process.kill(this.serverPid, 'SIGTERM')
} catch (error) {
logger.warn(`Failed to send SIGTERM to ${this.serverPid}:`, error)
const errorCode = error && typeof error === 'object' && 'code' in error ? (error as { code: string }).code : ''
if (errorCode === 'ESRCH') {
logger.debug(`Process ${this.serverPid} already stopped`)
} else {
logger.warn(`Failed to send SIGTERM to ${this.serverPid}:`, error)
}
}
await new Promise(r => setTimeout(r, 2000))
@@ -193,7 +198,12 @@ class OpenCodeServerManager {
try {
process.kill(this.serverPid, 'SIGKILL')
} catch (error) {
logger.warn(`Failed to send SIGKILL to ${this.serverPid}:`, error)
const errorCode = error && typeof error === 'object' && 'code' in error ? (error as { code: string }).code : ''
if (errorCode === 'ESRCH') {
logger.debug(`Process ${this.serverPid} already stopped`)
} else {
logger.warn(`Failed to send SIGKILL to ${this.serverPid}:`, error)
}
}
this.serverPid = null