|
1 | 1 | import { exec, ExecOptions } from 'child_process'; |
2 | 2 | import * as vscode from 'vscode'; |
| 3 | +import * as os from 'os'; |
| 4 | +import * as fs from 'fs'; |
3 | 5 | import { Urls } from '../constants'; |
4 | 6 | import { |
5 | 7 | HomebrewPackageIdentifier, |
@@ -125,3 +127,99 @@ export async function upgradeDevProxyWithPackageManager( |
125 | 127 | export function openUpgradeDocumentation(): void { |
126 | 128 | vscode.env.openExternal(vscode.Uri.parse(Urls.upgradeDoc)); |
127 | 129 | } |
| 130 | + |
| 131 | +/** |
| 132 | + * Common installation paths for Dev Proxy on different platforms. |
| 133 | + */ |
| 134 | +const COMMON_PATHS: Record<string, string[]> = { |
| 135 | + darwin: ['/opt/homebrew/bin', '/usr/local/bin'], |
| 136 | + linux: ['/usr/local/bin', '/home/linuxbrew/.linuxbrew/bin'], |
| 137 | + win32: [], // Windows typically has correct PATH from installer |
| 138 | +}; |
| 139 | + |
| 140 | +/** |
| 141 | + * Resolve the Dev Proxy executable path. |
| 142 | + * |
| 143 | + * Priority: |
| 144 | + * 1. Custom path from settings (if set and non-empty) |
| 145 | + * 2. Auto-detection: |
| 146 | + * a. Try bare command (devproxy/devproxy-beta) |
| 147 | + * b. Try via login shell (macOS/Linux only) |
| 148 | + * c. Try common installation paths |
| 149 | + * |
| 150 | + * @returns The resolved executable path, or the bare command name if not found |
| 151 | + */ |
| 152 | +export async function resolveDevProxyExecutable( |
| 153 | + exeName: string, |
| 154 | + customPath?: string |
| 155 | +): Promise<string> { |
| 156 | + // 1. Use custom path if provided |
| 157 | + if (customPath && customPath.trim() !== '') { |
| 158 | + return customPath.trim(); |
| 159 | + } |
| 160 | + |
| 161 | + // 2. Try bare command first |
| 162 | + if (await canExecute(exeName)) { |
| 163 | + return exeName; |
| 164 | + } |
| 165 | + |
| 166 | + const platform = os.platform(); |
| 167 | + |
| 168 | + // 3. Try via login shell (macOS/Linux only) |
| 169 | + if (platform !== 'win32') { |
| 170 | + const loginShellPath = await tryLoginShell(exeName); |
| 171 | + if (loginShellPath) { |
| 172 | + return loginShellPath; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + // 4. Try common installation paths |
| 177 | + const commonPaths = COMMON_PATHS[platform] || []; |
| 178 | + for (const dir of commonPaths) { |
| 179 | + const fullPath = `${dir}/${exeName}`; |
| 180 | + if (fs.existsSync(fullPath)) { |
| 181 | + return fullPath; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + // Fallback to bare command (will fail gracefully in getVersion) |
| 186 | + return exeName; |
| 187 | +} |
| 188 | + |
| 189 | +/** |
| 190 | + * Check if a command can be executed successfully. |
| 191 | + */ |
| 192 | +async function canExecute(cmd: string): Promise<boolean> { |
| 193 | + try { |
| 194 | + await executeCommand(`${cmd} --version`); |
| 195 | + return true; |
| 196 | + } catch { |
| 197 | + return false; |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +/** |
| 202 | + * Try to find the executable using a login shell. |
| 203 | + * This sources the user's shell profile which may include custom PATH entries. |
| 204 | + */ |
| 205 | +async function tryLoginShell(exeName: string): Promise<string | undefined> { |
| 206 | + const shells = ['/bin/zsh', '/bin/bash']; |
| 207 | + |
| 208 | + for (const shell of shells) { |
| 209 | + if (!fs.existsSync(shell)) { |
| 210 | + continue; |
| 211 | + } |
| 212 | + |
| 213 | + try { |
| 214 | + const result = await executeCommand(`${shell} -l -c "which ${exeName}"`); |
| 215 | + const path = result.trim(); |
| 216 | + if (path && !path.includes('not found')) { |
| 217 | + return path; |
| 218 | + } |
| 219 | + } catch { |
| 220 | + // Shell failed, try next |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + return undefined; |
| 225 | +} |
0 commit comments