index.js 807 B

12345678910111213141516171819202122232425
  1. import {promisify} from 'node:util';
  2. import process from 'node:process';
  3. import {execFile} from 'node:child_process';
  4. const execFileAsync = promisify(execFile);
  5. export default async function defaultBrowserId() {
  6. if (process.platform !== 'darwin') {
  7. throw new Error('macOS only');
  8. }
  9. const {stdout} = await execFileAsync('defaults', ['read', 'com.apple.LaunchServices/com.apple.launchservices.secure', 'LSHandlers']);
  10. // `(?!-)` is to prevent matching `LSHandlerRoleAll = "-";`.
  11. const match = /LSHandlerRoleAll = "(?!-)(?<id>[^"]+?)";\s+?LSHandlerURLScheme = (?:http|https);/.exec(stdout);
  12. const browserId = match?.groups.id ?? 'com.apple.Safari';
  13. // Correct the case for Safari's bundle identifier
  14. if (browserId === 'com.apple.safari') {
  15. return 'com.apple.Safari';
  16. }
  17. return browserId;
  18. }