|
| 1 | +/** |
| 2 | + * Platform-specific bash path resolution |
| 3 | + * |
| 4 | + * On Unix/Linux/macOS, bash is in PATH by default. |
| 5 | + * On Windows, bash comes from Git Bash and needs to be located. |
| 6 | + */ |
| 7 | + |
| 8 | +import { execSync } from "child_process"; |
| 9 | +import { existsSync } from "fs"; |
| 10 | +import path from "path"; |
| 11 | + |
| 12 | +let cachedBashPath: string | null = null; |
| 13 | + |
| 14 | +/** |
| 15 | + * Find bash executable path on Windows |
| 16 | + * Checks common Git Bash installation locations |
| 17 | + */ |
| 18 | +function findWindowsBash(): string | null { |
| 19 | + // Common Git Bash installation paths |
| 20 | + const commonPaths = [ |
| 21 | + // Git for Windows default paths |
| 22 | + "C:\\Program Files\\Git\\bin\\bash.exe", |
| 23 | + "C:\\Program Files (x86)\\Git\\bin\\bash.exe", |
| 24 | + // User-local Git installation |
| 25 | + path.join(process.env.LOCALAPPDATA || "", "Programs", "Git", "bin", "bash.exe"), |
| 26 | + // Portable Git |
| 27 | + path.join(process.env.USERPROFILE || "", "scoop", "apps", "git", "current", "bin", "bash.exe"), |
| 28 | + // Chocolatey installation |
| 29 | + "C:\\tools\\git\\bin\\bash.exe", |
| 30 | + ]; |
| 31 | + |
| 32 | + // Check if bash is in PATH first |
| 33 | + try { |
| 34 | + const result = execSync("where bash", { encoding: "utf8", stdio: ["pipe", "pipe", "ignore"] }); |
| 35 | + const firstPath = result.split("\n")[0].trim(); |
| 36 | + if (firstPath && existsSync(firstPath)) { |
| 37 | + return firstPath; |
| 38 | + } |
| 39 | + } catch { |
| 40 | + // Not in PATH, continue to check common locations |
| 41 | + } |
| 42 | + |
| 43 | + // Check common installation paths |
| 44 | + for (const bashPath of commonPaths) { |
| 45 | + if (existsSync(bashPath)) { |
| 46 | + return bashPath; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Also check if Git is in PATH and derive bash path from it |
| 51 | + try { |
| 52 | + const gitPath = execSync("where git", { encoding: "utf8", stdio: ["pipe", "pipe", "ignore"] }); |
| 53 | + const firstGitPath = gitPath.split("\n")[0].trim(); |
| 54 | + if (firstGitPath) { |
| 55 | + // Git is usually in Git/cmd/git.exe, bash is in Git/bin/bash.exe |
| 56 | + const gitDir = path.dirname(path.dirname(firstGitPath)); |
| 57 | + const bashPath = path.join(gitDir, "bin", "bash.exe"); |
| 58 | + if (existsSync(bashPath)) { |
| 59 | + return bashPath; |
| 60 | + } |
| 61 | + // Also try usr/bin/bash.exe (newer Git for Windows structure) |
| 62 | + const usrBashPath = path.join(gitDir, "usr", "bin", "bash.exe"); |
| 63 | + if (existsSync(usrBashPath)) { |
| 64 | + return usrBashPath; |
| 65 | + } |
| 66 | + } |
| 67 | + } catch { |
| 68 | + // Git not in PATH |
| 69 | + } |
| 70 | + |
| 71 | + return null; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Get the bash executable path for the current platform |
| 76 | + * |
| 77 | + * @returns Path to bash executable. On Unix/macOS returns "bash", |
| 78 | + * on Windows returns full path to bash.exe if found. |
| 79 | + * @throws Error if bash cannot be found on Windows |
| 80 | + */ |
| 81 | +export function getBashPath(): string { |
| 82 | + // On Unix/Linux/macOS, bash is in PATH |
| 83 | + if (process.platform !== "win32") { |
| 84 | + return "bash"; |
| 85 | + } |
| 86 | + |
| 87 | + // Use cached path if available |
| 88 | + if (cachedBashPath !== null) { |
| 89 | + return cachedBashPath; |
| 90 | + } |
| 91 | + |
| 92 | + // Find bash on Windows |
| 93 | + const bashPath = findWindowsBash(); |
| 94 | + if (!bashPath) { |
| 95 | + throw new Error( |
| 96 | + "Git Bash not found. Please install Git for Windows from https://git-scm.com/download/win" |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + cachedBashPath = bashPath; |
| 101 | + return bashPath; |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Check if bash is available on the system |
| 106 | + * |
| 107 | + * @returns true if bash is available, false otherwise |
| 108 | + */ |
| 109 | +export function isBashAvailable(): boolean { |
| 110 | + try { |
| 111 | + getBashPath(); |
| 112 | + return true; |
| 113 | + } catch { |
| 114 | + return false; |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | + |
0 commit comments