| 50 | |
| 51 | // Helper to find the project root (looks for .git directory) |
| 52 | async function findProjectRoot(startDir: string): Promise<string> { |
| 53 | let currentDir = path.resolve(startDir); |
| 54 | while (true) { |
| 55 | const gitPath = path.join(currentDir, '.git'); |
| 56 | try { |
| 57 | const stats = await fs.lstat(gitPath); |
| 58 | if (stats.isDirectory()) { |
| 59 | return currentDir; |
| 60 | } |
| 61 | } catch { |
| 62 | // .git not found, continue to parent |
| 63 | } |
| 64 | const parentDir = path.dirname(currentDir); |
| 65 | if (parentDir === currentDir) { |
| 66 | // Reached filesystem root |
| 67 | break; |
| 68 | } |
| 69 | currentDir = parentDir; |
| 70 | } |
| 71 | // Fallback to startDir if .git not found |
| 72 | return path.resolve(startDir); |
| 73 | } |
| 74 | |
| 75 | // Add a type guard for error objects |
| 76 | function hasMessage(err: unknown): err is { message: string } { |