index.js 650 B

123456789101112131415161718
  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. return match?.groups.id ?? 'com.apple.Safari';
  13. }