// 설명 문서: docs/specs/bada-code-quality.md (v1.0.0) // CLI와 GUI의 실행 파일 탐색을 공유하며 셸 보간 없이 조회한다. const fs = require('fs'); const path = require('path'); const os = require('os'); const { execFileSync } = require('child_process'); function findOnPath(command) { try { const locator = process.platform === 'win32' ? 'where.exe' : 'which'; const output = execFileSync(locator, [command], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], timeout: 3000, windowsHide: true }); const lines = output.split(/\r?\n/).map(line => line.trim()).filter(Boolean); if (process.platform === 'win32') { // Windows npm 글로벌 설치 경로(%APPDATA%\npm\opencode)의 확장자 없는 POSIX 스크립트보다 .exe/.cmd/.bat를 우선 선택한다. const winExec = lines.find(line => /\.(exe|cmd|bat|com)$/i.test(line) && fs.existsSync(line)); if (winExec) return winExec; } const first = lines[0]; return first && fs.existsSync(first) ? first : null; } catch { // 선택 실행 파일이 없으면 다음 후보를 사용한다. 자격이나 환경은 출력하지 않는다. return null; } } function resolveNodeBin() { const home = os.homedir(); const candidates = [ '/opt/homebrew/bin/node', '/usr/local/bin/node', '/usr/bin/node', path.join(home, '.nvm/current/bin/node'), 'C:\\Program Files\\nodejs\\node.exe' ]; return candidates.find(candidate => fs.existsSync(candidate)) || findOnPath('node') || findOnPath('bun') || (!process.versions.electron ? process.execPath : 'node'); } function resolveOpencodeBin() { const override = process.env.OPENWORK_OPENCODE_BIN; if (override && fs.existsSync(override)) return override; const home = os.homedir(); const candidates = process.platform === 'win32' ? [ path.join(home, '.opencode', 'bin', 'opencode.exe'), path.join(home, '.opencode', 'bin', 'opencode.cmd'), path.join(home, 'AppData', 'Local', 'Microsoft', 'WinGet', 'Links', 'opencode.exe'), path.join(home, 'scoop', 'shims', 'opencode.exe'), path.join(home, '.bun', 'bin', 'opencode.exe'), path.join(home, 'AppData', 'Roaming', 'npm', 'opencode.cmd'), path.join(home, '.codeagent', 'bin', 'opencode.exe') ] : [ path.join(home, '.opencode', 'bin', 'opencode'), path.join(home, '.codeagent', 'bin', 'opencode'), '/opt/homebrew/bin/opencode', '/usr/local/bin/opencode' ]; return candidates.find(candidate => fs.existsSync(candidate)) || findOnPath('opencode') || 'opencode'; } module.exports = { resolveNodeBin, resolveOpencodeBin };