提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

18 lines
650 B

  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. }