index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import {promisify} from 'node:util';
  2. import process from 'node:process';
  3. import {execFile} from 'node:child_process';
  4. import defaultBrowserId from 'default-browser-id';
  5. import bundleName from 'bundle-name';
  6. import windows from './windows.js';
  7. export {_windowsBrowserProgIdMap} from './windows.js';
  8. const execFileAsync = promisify(execFile);
  9. // Inlined: https://github.com/sindresorhus/titleize/blob/main/index.js
  10. const titleize = string => string.toLowerCase().replaceAll(/(?:^|\s|-)\S/g, x => x.toUpperCase());
  11. export default async function defaultBrowser() {
  12. if (process.platform === 'darwin') {
  13. const id = await defaultBrowserId();
  14. const name = await bundleName(id);
  15. return {name, id};
  16. }
  17. if (process.platform === 'linux') {
  18. const {stdout} = await execFileAsync('xdg-mime', ['query', 'default', 'x-scheme-handler/http']);
  19. const id = stdout.trim();
  20. const name = titleize(id.replace(/.desktop$/, '').replace('-', ' '));
  21. return {name, id};
  22. }
  23. if (process.platform === 'win32') {
  24. return windows();
  25. }
  26. throw new Error('Only macOS, Linux, and Windows are supported');
  27. }