Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions packages/cli/src/open.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { spawn } from 'node:child_process';

/** The command that opens a file in the default browser on `platform`. */
export function browserCommand(file: string, platform: NodeJS.Platform = process.platform): { cmd: string; args: string[] } {
/** The command that opens a file in the default browser on `platform`, or why it can't be done safely. */
export function browserCommand(
file: string,
platform: NodeJS.Platform = process.platform,
): { cmd: string; args: string[] } | { refused: string } {
if (platform === 'darwin') return { cmd: 'open', args: [file] };
// `start` is a cmd builtin, and its first quoted argument is the window title.
if (platform === 'win32') return { cmd: 'cmd', args: ['/c', 'start', '""', file] };
// Quoted, cmd takes & | < > ^ ( ) literally; it still expands %VAR% inside quotes, and " would end the quoting.
if (platform === 'win32')
return /["%]/.test(file)
? { refused: 'the path has a " or % that cmd would interpret' }
: { cmd: 'cmd', args: ['/c', 'start', '""', `"${file}"`] };
return { cmd: 'xdg-open', args: [file] };
}

Expand All @@ -13,7 +20,9 @@ export function browserCommand(file: string, platform: NodeJS.Platform = process
* opener installed, a headless machine). An opener that is still running after a moment is taken as success.
*/
export function openInBrowser(file: string): Promise<string | null> {
const { cmd, args } = browserCommand(file);
const command = browserCommand(file);
if ('refused' in command) return Promise.resolve(command.refused);
const { cmd, args } = command;
return new Promise((resolve) => {
const child = spawn(cmd, args, { stdio: 'ignore', detached: true, windowsVerbatimArguments: cmd === 'cmd' });
const timer = setTimeout(() => resolve(null), 3000);
Expand Down
19 changes: 17 additions & 2 deletions packages/cli/test/open.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@ import { describe, expect, it } from 'vitest';
import { browserCommand } from '../src/open';

describe('browserCommand', () => {
it("uses each platform's own opener", () => {
it('uses the native opener on darwin and linux', () => {
expect(browserCommand('/tmp/a b.html', 'darwin')).toEqual({ cmd: 'open', args: ['/tmp/a b.html'] });
expect(browserCommand('C:\\d\\a.html', 'win32')).toEqual({ cmd: 'cmd', args: ['/c', 'start', '""', 'C:\\d\\a.html'] });
expect(browserCommand('/tmp/a.html', 'linux')).toEqual({ cmd: 'xdg-open', args: ['/tmp/a.html'] });
});

it('quotes the path for cmd on win32', () => {
expect(browserCommand('C:\\d\\a.html', 'win32')).toEqual({ cmd: 'cmd', args: ['/c', 'start', '""', '"C:\\d\\a.html"'] });
});

it('keeps spaces and cmd metacharacters inside the quotes', () => {
const file = 'C:\\Users\\First Last\\a&b (1).html';
expect(browserCommand(file, 'win32')).toEqual({ cmd: 'cmd', args: ['/c', 'start', '""', `"${file}"`] });
});

it('refuses win32 paths with a % or " that cmd would interpret', () => {
for (const file of ['C:\\d\\100%.html', 'C:\\d\\a"b.html']) {
const result = browserCommand(file, 'win32');
expect('refused' in result && typeof result.refused).toBe('string');
}
});
});
Loading