|
1 | 1 | import { execFile } from 'node:child_process'; |
2 | 2 |
|
| 3 | +export interface OpenUrlCommand { |
| 4 | + readonly command: string; |
| 5 | + readonly args: readonly string[]; |
| 6 | +} |
| 7 | + |
| 8 | +/** |
| 9 | + * Windows uses `rundll32` rather than `cmd /c start` because `cmd` re-parses |
| 10 | + * its arguments and cuts a URL at the first `&`, which strips every OAuth |
| 11 | + * query parameter after `client_id`. |
| 12 | + */ |
| 13 | +export function openUrlCommandFor( |
| 14 | + url: string, |
| 15 | + platform: NodeJS.Platform = process.platform, |
| 16 | +): OpenUrlCommand { |
| 17 | + switch (platform) { |
| 18 | + case 'darwin': |
| 19 | + return { command: 'open', args: [url] }; |
| 20 | + case 'win32': |
| 21 | + return { command: 'rundll32', args: ['url.dll,FileProtocolHandler', url] }; |
| 22 | + default: |
| 23 | + return { command: 'xdg-open', args: [url] }; |
| 24 | + } |
| 25 | +} |
| 26 | + |
3 | 27 | export function openUrl(url: string): void { |
4 | | - const command: [string, string[]] = |
5 | | - process.platform === 'darwin' |
6 | | - ? ['open', [url]] |
7 | | - : process.platform === 'win32' |
8 | | - ? ['cmd', ['/c', 'start', '', url]] |
9 | | - : ['xdg-open', [url]]; |
10 | | - execFile(command[0], command[1], () => {}); |
| 28 | + const { command, args } = openUrlCommandFor(url); |
| 29 | + execFile(command, [...args], () => {}); |
11 | 30 | } |
0 commit comments