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
57 changes: 57 additions & 0 deletions src/cli/operations/dev/__tests__/utils-open-browser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

const mockUnref = vi.fn();
const mockSpawn = vi.fn().mockReturnValue({ unref: mockUnref });

vi.mock('child_process', () => ({
spawn: (...args: unknown[]) => mockSpawn(...args),
}));

describe('openBrowser', () => {
const originalPlatform = process.platform;

beforeEach(() => {
mockSpawn.mockClear();
mockUnref.mockClear();
});

afterEach(() => {
Object.defineProperty(process, 'platform', { value: originalPlatform });
});

it('uses "open" on macOS', async () => {
Object.defineProperty(process, 'platform', { value: 'darwin' });
const { openBrowser } = await import('../utils');
openBrowser('http://localhost:3000');

expect(mockSpawn).toHaveBeenCalledWith('open', ['http://localhost:3000'], {
stdio: 'ignore',
detached: true,
});
expect(mockUnref).toHaveBeenCalled();
});

it('uses "cmd /c start" on Windows to avoid ENOENT', async () => {
Object.defineProperty(process, 'platform', { value: 'win32' });
const { openBrowser } = await import('../utils');
openBrowser('http://localhost:3000');

expect(mockSpawn).toHaveBeenCalledWith('cmd', ['/c', 'start', 'http://localhost:3000'], {
stdio: 'ignore',
detached: true,
});
expect(mockUnref).toHaveBeenCalled();
});

it('uses "xdg-open" on Linux', async () => {
Object.defineProperty(process, 'platform', { value: 'linux' });
const { openBrowser } = await import('../utils');
openBrowser('http://localhost:3000');

expect(mockSpawn).toHaveBeenCalledWith('xdg-open', ['http://localhost:3000'], {
stdio: 'ignore',
detached: true,
});
expect(mockUnref).toHaveBeenCalled();
});
});
8 changes: 8 additions & 0 deletions src/cli/operations/dev/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { spawn } from 'child_process';
import { createConnection, createServer } from 'net';

/** Check if a port is available on a specific host */
Expand Down Expand Up @@ -108,3 +109,10 @@ export function convertEntrypointToModule(entrypoint: string): string {
const path = entrypoint.replace(/\.py$/, '').replace(/\//g, '.');
return `${path}:app`;
}

export function openBrowser(url: string): void {
const isWindows = process.platform === 'win32';
const cmd = isWindows ? 'cmd' : process.platform === 'darwin' ? 'open' : 'xdg-open';
const args = isWindows ? ['/c', 'start', url] : [url];
spawn(cmd, args, { stdio: 'ignore', detached: true }).unref();
}
5 changes: 2 additions & 3 deletions src/cli/operations/dev/web-ui/run-web-ui.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ExecLogger } from '../../../logging';
import { findAvailablePort } from '../server';
import { openBrowser } from '../utils';
import { WEB_UI_DEFAULT_PORT } from './constants';
import { type WebUIOptions, WebUIServer } from './web-server';
import { spawn } from 'child_process';

export interface RunWebUIOptions {
/** Options to pass to WebUIServer (minus uiPort, which is resolved automatically) */
Expand Down Expand Up @@ -42,8 +42,7 @@ export async function runWebUI(opts: RunWebUIOptions): Promise<void> {
const chatUrl = url;
console.log(`\nChat UI: ${chatUrl}`);
console.log(`Press Ctrl+C to stop\n`);
const openCmd = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start' : 'xdg-open';
spawn(openCmd, [chatUrl], { stdio: 'ignore', detached: true }).unref();
openBrowser(chatUrl);
},
onLog,
});
Expand Down
Loading