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-09-02 08:26:45 +08:00
|
|
|
import { getCli } from '../config/cli-registry/registry.js';
|
|
|
|
|
import { expandHome } from './cli-resolver.js';
|
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 */
|
2026-09-02 08:26:45 +08:00
|
|
|
/**
|
|
|
|
|
* Directories probed after `which`, read from this CLI's registry entry so the spawn
|
|
|
|
|
* path, `codeman doctor` and this resolver cannot disagree about where to look.
|
|
|
|
|
* `~` is expanded by `expandHome`; nothing else is interpreted.
|
|
|
|
|
*/
|
|
|
|
|
const OPENCODE_SEARCH_DIRS = (): string[] => (getCli('opencode')?.discovery.searchDirs ?? []).map(expandHome);
|
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());
|
|
|
|
|
}
|