* Open a URL in the user's default browser. * Handles macOS (open), Linux (xdg-open), and headless environments.
(url: string)
| 243 | * Handles macOS (open), Linux (xdg-open), and headless environments. |
| 244 | */ |
| 245 | function openBrowser(url: string): void { |
| 246 | const platform = process.platform; |
| 247 | let cmd: string; |
| 248 | |
| 249 | if (platform === "darwin") { |
| 250 | cmd = "open"; |
| 251 | } else if (platform === "linux") { |
| 252 | cmd = "xdg-open"; |
| 253 | } else { |
| 254 | // Windows or unknown — just print the URL |
| 255 | console.error(`SERVE_BROWSER_MANUAL: url=${url}`); |
| 256 | console.error(`Open this URL in your browser: ${url}`); |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | try { |
| 261 | const child = spawn(cmd, [url], { |
| 262 | stdio: "ignore", |
| 263 | detached: true, |
| 264 | }); |
| 265 | child.unref(); |
| 266 | console.error(`SERVE_BROWSER_OPENED: url=${url}`); |
| 267 | } catch { |
| 268 | // open/xdg-open not available (headless CI environment) |
| 269 | console.error(`SERVE_BROWSER_MANUAL: url=${url}`); |
| 270 | console.error(`Open this URL in your browser: ${url}`); |
| 271 | } |
| 272 | } |