import { spawn } from 'node:child_process'; import { setting } from './config.ts'; // Opening a URL in the user's browser, when there plausibly is one. export function canOpenBrowser(): boolean { if (setting('browser') === 'never') return false; if (process.env['SSH_CONNECTION'] || process.env['SSH_TTY']) return false; if (process.platform === 'darwin' || process.platform === 'win32') return true; return Boolean(process.env['DISPLAY'] || process.env['WAYLAND_DISPLAY']); } // Best effort: the URL has been printed as well. Windows goes through the URL // protocol handler rather than `cmd /c start`, which would read every `&` in a // query string as a command separator. export function openBrowser(url: string): void { const [cmd, ...args] = process.platform === 'darwin' ? ['open', url] : process.platform === 'win32' ? ['rundll32', 'url.dll,FileProtocolHandler', url] : ['xdg-open', url]; try { spawn(cmd!, args, { stdio: 'ignore', detached: true }).on('error', () => {}).unref(); } catch { // The URL is on screen. } }