Merge pull request #13 from CarlosGtrz/refactor/bash-windows-detection

Cleaner Windows Git Bash detection
This commit is contained in:
Mario Zechner 2025-11-13 23:22:17 +01:00 committed by GitHub
commit b22dfcd2be

View file

@ -3,51 +3,31 @@ import { Type } from "@sinclair/typebox";
import { spawn } from "child_process"; import { spawn } from "child_process";
import { existsSync } from "fs"; import { existsSync } from "fs";
/**
* Find Git Bash installation on Windows
* Searches common installation paths
*/
function findGitBash(): string | null {
if (process.platform !== "win32") {
return null;
}
const paths = ["C:\\Program Files\\Git\\bin\\bash.exe", "C:\\Program Files (x86)\\Git\\bin\\bash.exe"];
// Also check ProgramFiles environment variables
const programFiles = process.env.ProgramFiles;
if (programFiles) {
paths.push(`${programFiles}\\Git\\bin\\bash.exe`);
}
const programFilesX86 = process.env["ProgramFiles(x86)"];
if (programFilesX86) {
paths.push(`${programFilesX86}\\Git\\bin\\bash.exe`);
}
for (const path of paths) {
if (existsSync(path)) {
return path;
}
}
return null;
}
/** /**
* Get shell configuration based on platform * Get shell configuration based on platform
*/ */
function getShellConfig(): { shell: string; args: string[] } { function getShellConfig(): { shell: string; args: string[] } {
if (process.platform === "win32") { if (process.platform === "win32") {
const gitBash = findGitBash(); const paths: string[] = [];
if (!gitBash) { const programFiles = process.env.ProgramFiles;
const paths = ["C:\\Program Files\\Git\\bin\\bash.exe", "C:\\Program Files (x86)\\Git\\bin\\bash.exe"]; if (programFiles) {
const pathList = paths.map((p) => ` - ${p}`).join("\n"); paths.push(`${programFiles}\\Git\\bin\\bash.exe`);
throw new Error(
`Git Bash not found in standard installation locations:\n${pathList}\n\nPlease install Git for Windows from https://git-scm.com/download/win using the default installation path.`,
);
} }
return { shell: gitBash, args: ["-c"] }; const programFilesX86 = process.env["ProgramFiles(x86)"];
if (programFilesX86) {
paths.push(`${programFilesX86}\\Git\\bin\\bash.exe`);
}
for (const path of paths) {
if (existsSync(path)) {
return { shell: path, args: ["-c"] };
}
}
throw new Error(
`Git Bash not found. Please install Git for Windows from https://git-scm.com/download/win\n` +
`Searched in:\n${paths.map((p) => ` ${p}`).join("\n")}`,
);
} }
return { shell: "sh", args: ["-c"] }; return { shell: "sh", args: ["-c"] };
} }