|
| 1 | +import { existsSync } from 'node:fs' |
| 2 | +import type { BrowserDiscovery, BrowserDiscoveryEntry, BrowserDiscoveryKind } from './types.ts' |
| 3 | + |
| 4 | +type CandidateDefinition = { |
| 5 | + id: string |
| 6 | + name: string |
| 7 | + kind: BrowserDiscoveryKind |
| 8 | + darwin: string[] |
| 9 | + win32: string[] |
| 10 | + linux: string[] |
| 11 | +} |
| 12 | + |
| 13 | +const CANDIDATES: CandidateDefinition[] = [ |
| 14 | + { |
| 15 | + id: 'chrome', |
| 16 | + name: 'Google Chrome', |
| 17 | + kind: 'chrome', |
| 18 | + darwin: ['/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'], |
| 19 | + win32: [ |
| 20 | + '%LOCALAPPDATA%\\Google\\Chrome\\Application\\chrome.exe', |
| 21 | + '%ProgramFiles%\\Google\\Chrome\\Application\\chrome.exe', |
| 22 | + '%ProgramFiles(x86)%\\Google\\Chrome\\Application\\chrome.exe', |
| 23 | + ], |
| 24 | + linux: ['/usr/bin/google-chrome', '/usr/bin/google-chrome-stable'], |
| 25 | + }, |
| 26 | + { |
| 27 | + id: 'edge', |
| 28 | + name: 'Microsoft Edge', |
| 29 | + kind: 'edge', |
| 30 | + darwin: ['/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge'], |
| 31 | + win32: [ |
| 32 | + '%LOCALAPPDATA%\\Microsoft\\Edge\\Application\\msedge.exe', |
| 33 | + '%ProgramFiles%\\Microsoft\\Edge\\Application\\msedge.exe', |
| 34 | + '%ProgramFiles(x86)%\\Microsoft\\Edge\\Application\\msedge.exe', |
| 35 | + ], |
| 36 | + linux: ['/usr/bin/microsoft-edge', '/usr/bin/microsoft-edge-stable'], |
| 37 | + }, |
| 38 | + { |
| 39 | + id: 'brave', |
| 40 | + name: 'Brave', |
| 41 | + kind: 'brave', |
| 42 | + darwin: ['/Applications/Brave Browser.app/Contents/MacOS/Brave Browser'], |
| 43 | + win32: [ |
| 44 | + '%LOCALAPPDATA%\\BraveSoftware\\Brave-Browser\\Application\\brave.exe', |
| 45 | + '%ProgramFiles%\\BraveSoftware\\Brave-Browser\\Application\\brave.exe', |
| 46 | + '%ProgramFiles(x86)%\\BraveSoftware\\Brave-Browser\\Application\\brave.exe', |
| 47 | + ], |
| 48 | + linux: ['/usr/bin/brave-browser', '/usr/bin/brave-browser-stable'], |
| 49 | + }, |
| 50 | + { |
| 51 | + id: 'chromium', |
| 52 | + name: 'Chromium', |
| 53 | + kind: 'chromium', |
| 54 | + darwin: ['/Applications/Chromium.app/Contents/MacOS/Chromium'], |
| 55 | + win32: [ |
| 56 | + '%LOCALAPPDATA%\\Chromium\\Application\\chrome.exe', |
| 57 | + '%ProgramFiles%\\Chromium\\Application\\chrome.exe', |
| 58 | + '%ProgramFiles(x86)%\\Chromium\\Application\\chrome.exe', |
| 59 | + ], |
| 60 | + linux: ['/usr/bin/chromium', '/usr/bin/chromium-browser', '/snap/bin/chromium'], |
| 61 | + }, |
| 62 | + { |
| 63 | + id: 'vivaldi', |
| 64 | + name: 'Vivaldi', |
| 65 | + kind: 'vivaldi', |
| 66 | + darwin: ['/Applications/Vivaldi.app/Contents/MacOS/Vivaldi'], |
| 67 | + win32: [ |
| 68 | + '%LOCALAPPDATA%\\Vivaldi\\Application\\vivaldi.exe', |
| 69 | + '%ProgramFiles%\\Vivaldi\\Application\\vivaldi.exe', |
| 70 | + '%ProgramFiles(x86)%\\Vivaldi\\Application\\vivaldi.exe', |
| 71 | + ], |
| 72 | + linux: ['/usr/bin/vivaldi', '/usr/bin/vivaldi-stable'], |
| 73 | + }, |
| 74 | + { |
| 75 | + id: 'arc', |
| 76 | + name: 'Arc', |
| 77 | + kind: 'arc', |
| 78 | + darwin: ['/Applications/Arc.app/Contents/MacOS/Arc'], |
| 79 | + win32: [], |
| 80 | + linux: [], |
| 81 | + }, |
| 82 | +] |
| 83 | + |
| 84 | +function expandWindowsPath(rawPath: string, env: NodeJS.ProcessEnv): string { |
| 85 | + return rawPath.replace(/%([^%]+)%/g, (_, key: string) => env[key] ?? '') |
| 86 | +} |
| 87 | + |
| 88 | +function getCandidatePaths( |
| 89 | + candidate: CandidateDefinition, |
| 90 | + platform: NodeJS.Platform, |
| 91 | + env: NodeJS.ProcessEnv, |
| 92 | +): string[] { |
| 93 | + switch (platform) { |
| 94 | + case 'darwin': |
| 95 | + return candidate.darwin |
| 96 | + case 'win32': |
| 97 | + return candidate.win32.map((entry) => expandWindowsPath(entry, env)) |
| 98 | + default: |
| 99 | + return candidate.linux |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +function guessRecommendedBrowserId( |
| 104 | + browsers: BrowserDiscoveryEntry[], |
| 105 | + env: NodeJS.ProcessEnv, |
| 106 | +): { recommendedBrowserId: string | null; recommendationSource: BrowserDiscovery['recommendationSource'] } { |
| 107 | + if (browsers.length === 0) { |
| 108 | + return { recommendedBrowserId: null, recommendationSource: 'none' } |
| 109 | + } |
| 110 | + |
| 111 | + const browserHint = (env.BROWSER ?? '').toLowerCase() |
| 112 | + if (browserHint) { |
| 113 | + const hinted = browsers.find((browser) => |
| 114 | + browserHint.includes(browser.id) || |
| 115 | + browserHint.includes(browser.kind) || |
| 116 | + browserHint.includes(browser.name.toLowerCase()), |
| 117 | + ) |
| 118 | + if (hinted) { |
| 119 | + return { recommendedBrowserId: hinted.id, recommendationSource: 'env' } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return { recommendedBrowserId: browsers[0]!.id, recommendationSource: 'priority' } |
| 124 | +} |
| 125 | + |
| 126 | +export function detectInstalledBrowsers(params?: { |
| 127 | + platform?: NodeJS.Platform |
| 128 | + env?: NodeJS.ProcessEnv |
| 129 | + exists?: (path: string) => boolean |
| 130 | +}): BrowserDiscovery { |
| 131 | + const platform = params?.platform ?? process.platform |
| 132 | + const env = params?.env ?? process.env |
| 133 | + const exists = params?.exists ?? existsSync |
| 134 | + |
| 135 | + const browsers: BrowserDiscoveryEntry[] = [] |
| 136 | + |
| 137 | + for (const candidate of CANDIDATES) { |
| 138 | + const executablePath = getCandidatePaths(candidate, platform, env).find((entry) => entry && exists(entry)) |
| 139 | + if (!executablePath) continue |
| 140 | + |
| 141 | + browsers.push({ |
| 142 | + id: candidate.id, |
| 143 | + name: candidate.name, |
| 144 | + kind: candidate.kind, |
| 145 | + executablePath, |
| 146 | + isRecommended: false, |
| 147 | + }) |
| 148 | + } |
| 149 | + |
| 150 | + const recommendation = guessRecommendedBrowserId(browsers, env) |
| 151 | + const nextBrowsers = browsers.map((browser) => ({ |
| 152 | + ...browser, |
| 153 | + isRecommended: browser.id === recommendation.recommendedBrowserId, |
| 154 | + })) |
| 155 | + |
| 156 | + return { |
| 157 | + browsers: nextBrowsers, |
| 158 | + recommendedBrowserId: recommendation.recommendedBrowserId, |
| 159 | + recommendationSource: recommendation.recommendationSource, |
| 160 | + } |
| 161 | +} |
0 commit comments