11import { Hono } from 'hono'
22import { z } from 'zod'
33import { execSync } from 'child_process'
4+ import { existsSync } from 'fs'
5+ import { resolve , dirname } from 'path'
46import type { Database } from 'bun:sqlite'
57import { SettingsService } from '../services/settings'
68import { writeFileContent , readFileContent , fileExists } from '../services/file-operations'
@@ -27,6 +29,27 @@ function compareVersions(v1: string, v2: string): number {
2729 return 0
2830}
2931
32+ function getOpenCodeInstallMethod ( ) : string {
33+ const homePath = process . env . HOME || ''
34+ const opencodePath = process . env . OPENCOD_PATH || resolve ( homePath , '.opencode' , 'bin' , 'opencode' )
35+
36+ if ( ! existsSync ( opencodePath ) ) return 'curl'
37+
38+ try {
39+ const opencodeDir = dirname ( opencodePath )
40+ if ( opencodeDir . includes ( '.opencode' ) ) return 'curl'
41+
42+ if ( opencodePath . includes ( '/homebrew/' ) || opencodePath . includes ( '/HOMEBREW/' ) ) return 'brew'
43+ if ( opencodePath . includes ( '/.npm/' ) || opencodePath . includes ( '/node_modules/' ) ) return 'npm'
44+ if ( opencodePath . includes ( '/.pnpm/' ) ) return 'pnpm'
45+ if ( opencodePath . includes ( '/.bun/' ) ) return 'bun'
46+ } catch {
47+ return 'curl'
48+ }
49+
50+ return 'curl'
51+ }
52+
3053function execWithTimeout ( command : string , timeoutMs : number ) : { output : string ; timedOut : boolean } {
3154 try {
3255 const output = execSync ( command , {
@@ -396,8 +419,9 @@ export function createSettingsRoutes(db: Database) {
396419 logger . info ( `Current OpenCode version: ${ oldVersion } ` )
397420
398421 try {
399- logger . info ( 'Running opencode upgrade with 90s timeout...' )
400- const { output : upgradeOutput , timedOut } = execWithTimeout ( 'opencode upgrade 2>&1' , 90000 )
422+ const installMethod = getOpenCodeInstallMethod ( )
423+ logger . info ( `Running opencode upgrade --method ${ installMethod } with 90s timeout...` )
424+ const { output : upgradeOutput , timedOut } = execWithTimeout ( `opencode upgrade --method ${ installMethod } 2>&1` , 90000 )
401425 logger . info ( `Upgrade output: ${ upgradeOutput } ` )
402426
403427 if ( timedOut ) {
@@ -546,9 +570,10 @@ export function createSettingsRoutes(db: Database) {
546570
547571 logger . info ( `Installing OpenCode version: ${ version } ` )
548572 const versionArg = version . startsWith ( 'v' ) ? version : `v${ version } `
549- logger . info ( `Running opencode upgrade ${ versionArg } with 90s timeout...` )
573+ const installMethod = getOpenCodeInstallMethod ( )
574+ logger . info ( `Running opencode upgrade ${ versionArg } --method ${ installMethod } with 90s timeout...` )
550575
551- const { output : upgradeOutput , timedOut } = execWithTimeout ( `opencode upgrade ${ versionArg } 2>&1` , 90000 )
576+ const { output : upgradeOutput , timedOut } = execWithTimeout ( `opencode upgrade ${ versionArg } --method ${ installMethod } 2>&1` , 90000 )
552577 logger . info ( `Upgrade output: ${ upgradeOutput } ` )
553578
554579 if ( timedOut ) {
0 commit comments