windows.js (1348B)
1 import {execa} from 'execa'; 2 3 // Windows doesn't have browser IDs in the same way macOS/Linux does so we give fake 4 // ones that look real and match the macOS/Linux versions for cross-platform apps. 5 const windowsBrowserProgIds = { 6 AppXq0fevzme2pys62n3e0fbqa7peapykr8v: {name: 'Edge', id: 'com.microsoft.edge.old'}, 7 MSEdgeDHTML: {name: 'Edge', id: 'com.microsoft.edge'}, // On macOS, it's "com.microsoft.edgemac" 8 MSEdgeHTM: {name: 'Edge', id: 'com.microsoft.edge'}, // Newer Edge/Win10 releases 9 'IE.HTTP': {name: 'Internet Explorer', id: 'com.microsoft.ie'}, 10 FirefoxURL: {name: 'Firefox', id: 'org.mozilla.firefox'}, 11 ChromeHTML: {name: 'Chrome', id: 'com.google.chrome'}, 12 }; 13 14 export class UnknownBrowserError extends Error {} 15 16 export default async function defaultBrowser(_execa = execa) { 17 const result = await _execa('reg', [ 18 'QUERY', 19 ' HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice', 20 '/v', 21 'ProgId', 22 ]); 23 24 const match = /ProgId\s*REG_SZ\s*(?<id>\S+)/.exec(result.stdout); 25 if (!match) { 26 throw new UnknownBrowserError(`Cannot find Windows browser in stdout: ${JSON.stringify(result.stdout)}`); 27 } 28 29 const {id} = match.groups; 30 31 const browser = windowsBrowserProgIds[id]; 32 if (!browser) { 33 throw new UnknownBrowserError(`Unknown browser ID: ${id}`); 34 } 35 36 return browser; 37 }