index.js 1.0 KB

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