(url: string)
| 10 | * @throws Error if the browser cannot be opened |
| 11 | */ |
| 12 | export async function openInBrowser(url: string): Promise<void> { |
| 13 | const { command, args } = getOpenCommand(url) |
| 14 | debug(`Opening browser with command: ${command} ${args.join(' ')}`) |
| 15 | |
| 16 | return new Promise((resolve, reject) => { |
| 17 | const child = spawn(command, args, { stdio: 'ignore' }) |
| 18 | |
| 19 | child.on('error', error => { |
| 20 | reject(new Error(`Failed to open browser: ${error.message}`)) |
| 21 | }) |
| 22 | |
| 23 | child.on('close', code => { |
| 24 | if (code === 0) { |
| 25 | resolve() |
| 26 | } else { |
| 27 | reject(new Error(`Failed to open browser: process exited with code ${code}`)) |
| 28 | } |
| 29 | }) |
| 30 | }) |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Gets the platform-specific command and arguments to open a URL. |
no test coverage detected