2026-02-26 10:07:43 +01:00
|
|
|
/**
|
|
|
|
|
* @fileoverview Resolve the OpenCode CLI binary across common install paths.
|
|
|
|
|
*
|
|
|
|
|
* Mirrors claude-cli-resolver.ts pattern. Finds the `opencode` binary
|
|
|
|
|
* and provides an augmented PATH string for tmux sessions.
|
|
|
|
|
*
|
|
|
|
|
* @module utils/opencode-cli-resolver
|
|
|
|
|
*/
|
|
|
|
|
|
2026-08-20 12:47:42 -04:00
|
|
|
import { join } from 'node:path';
|
2026-02-26 10:07:43 +01:00
|
|
|
import { homedir } from 'node:os';
|
2026-08-20 12:47:42 -04:00
|
|
|
import { createCliExecutableResolver, formatCliNotFoundMessage } from './cli-executable-resolver.js';
|
2026-02-26 10:07:43 +01:00
|
|
|
|
|
|
|
|
/** Common directories where the OpenCode CLI binary may be installed */
|
|
|
|
|
const OPENCODE_SEARCH_DIRS = [
|
2026-02-27 13:16:23 +01:00
|
|
|
join(homedir(), '.opencode', 'bin'), // Default install location
|
|
|
|
|
join(homedir(), '.local', 'bin'), // Alternative install location
|
|
|
|
|
'/usr/local/bin', // Homebrew / system
|
|
|
|
|
join(homedir(), 'go', 'bin'), // Go install
|
|
|
|
|
join(homedir(), '.bun', 'bin'), // Bun global
|
|
|
|
|
join(homedir(), '.npm-global', 'bin'), // npm global
|
|
|
|
|
join(homedir(), 'bin'), // User bin
|
2026-02-26 10:07:43 +01:00
|
|
|
];
|
|
|
|
|
|
2026-08-20 12:47:42 -04:00
|
|
|
const openCodeResolver = createCliExecutableResolver({ binary: 'opencode', searchDirs: OPENCODE_SEARCH_DIRS });
|
|
|
|
|
const OPENCODE_NOT_FOUND = 'OpenCode CLI not found. Install with: curl -fsSL https://opencode.ai/install | bash';
|
2026-02-26 10:07:43 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Finds the directory containing the `opencode` binary.
|
|
|
|
|
* Checks `which opencode` first, then falls back to common install locations.
|
|
|
|
|
* Result is cached for subsequent calls.
|
|
|
|
|
*
|
|
|
|
|
* @returns Directory path, or null if not found
|
|
|
|
|
*/
|
|
|
|
|
export function resolveOpenCodeDir(): string | null {
|
2026-08-20 12:47:42 -04:00
|
|
|
return openCodeResolver.resolve()?.directory ?? null;
|
2026-02-26 10:07:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if OpenCode CLI is available on the system.
|
|
|
|
|
*/
|
|
|
|
|
export function isOpenCodeAvailable(): boolean {
|
|
|
|
|
return resolveOpenCodeDir() !== null;
|
|
|
|
|
}
|
2026-08-20 12:47:42 -04:00
|
|
|
|
|
|
|
|
export function getOpenCodeNotFoundMessage(): string {
|
|
|
|
|
return formatCliNotFoundMessage(OPENCODE_NOT_FOUND, openCodeResolver.diagnostics());
|
|
|
|
|
}
|