mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-14 13:27:13 +00:00
* feat(cli): add browser launch options to preview and play * fix(cli): add spawn error listener to prevent ENOENT crash --------- Co-authored-by: AnoKno <122017492+AnoKno@users.noreply.github.com>
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { spawn } from "node:child_process";
|
|
|
|
export interface OpenBrowserOptions {
|
|
browserPath?: string;
|
|
userDataDir?: string;
|
|
}
|
|
|
|
/**
|
|
* Build the argument list for spawning a browser process.
|
|
*
|
|
* Pure function — easy to unit-test without mocking `spawn` or `import("open")`.
|
|
*/
|
|
export function buildBrowserArgs(url: string, options: OpenBrowserOptions): string[] {
|
|
const args: string[] = [];
|
|
if (options.userDataDir) {
|
|
args.push(`--user-data-dir=${options.userDataDir}`);
|
|
}
|
|
args.push(url);
|
|
return args;
|
|
}
|
|
|
|
/**
|
|
* Open a URL in the browser with the given options.
|
|
*
|
|
* - browserPath: spawn the given binary directly (enables Chromium flags)
|
|
* - userDataDir: passed as --user-data-dir (requires browserPath)
|
|
* - otherwise: fall back to the `open` package (default browser)
|
|
*/
|
|
export function openBrowser(url: string, options: OpenBrowserOptions = {}): void {
|
|
if (options.browserPath) {
|
|
const args = buildBrowserArgs(url, options);
|
|
const child = spawn(options.browserPath, args, {
|
|
detached: true,
|
|
stdio: "ignore",
|
|
});
|
|
child.on("error", () => {});
|
|
child.unref();
|
|
return;
|
|
}
|
|
|
|
import("open").then((mod) => mod.default(url)).catch(() => {});
|
|
}
|