From 0f7984422040e5059c6d734c161383c7f912667d Mon Sep 17 00:00:00 2001 From: Ryan Duffy Date: Mon, 4 Dec 2023 12:56:42 -0800 Subject: [PATCH] Add replay record command to launch and record (#286) * Add replay record command to launch and record * conditionally set launch env variables --- packages/replay/src/bin.ts | 39 ++++++++++++++++++++++++++++++------ packages/replay/src/main.ts | 28 +++++++++++++++++--------- packages/replay/src/types.ts | 5 +++++ 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/packages/replay/src/bin.ts b/packages/replay/src/bin.ts index b2f7cba2..c3b255ce 100644 --- a/packages/replay/src/bin.ts +++ b/packages/replay/src/bin.ts @@ -19,6 +19,7 @@ import { import { BrowserName, FilterOptions, + LaunchOptions, MetadataOptions, Options, SourcemapUploadOptions, @@ -75,6 +76,17 @@ commandWithGlobalOptions("launch [url]") .allowUnknownOption() .action(commandLaunchBrowser); +commandWithGlobalOptions("record [url]") + .description("Launch the replay browser and start recording") + .option("-b, --browser ", "Browser to launch", "chromium") + .option( + "--attach ", + "Whether to attach to the browser process after launching", + false + ) + .allowUnknownOption() + .action(commandLaunchBrowserAndRecord); + commandWithGlobalOptions("process ") .description("Upload a recording to the remote server and process it.") .option("--api-key ", "Authentication API Key") @@ -203,10 +215,7 @@ async function commandUploadRecording(id: string, opts: CommandLineOptions) { async function commandLaunchBrowser( url: string | undefined, - opts: Pick & { - browser: string | undefined; - attach: boolean | undefined; - } + opts: Pick & LaunchOptions ) { try { debug("Options", opts); @@ -214,9 +223,27 @@ async function commandLaunchBrowser( const browser = fuzzyBrowserName(opts.browser) || "chromium"; assertValidBrowserName(browser); - const attach = opts.attach || false; + await launchBrowser(browser, [url || "about:blank"], false, { ...opts, verbose: true }); + process.exit(0); + } catch (e) { + console.error("Failed to launch browser"); + debug("launchBrowser error %o", e); + + process.exit(opts.warn ? 0 : 1); + } +} + +async function commandLaunchBrowserAndRecord( + url: string | undefined, + opts: Pick & LaunchOptions +) { + try { + debug("Options", opts); + + const browser = fuzzyBrowserName(opts.browser) || "chromium"; + assertValidBrowserName(browser); - await launchBrowser(browser, [url || "about:blank"], attach, { ...opts, verbose: true }); + await launchBrowser(browser, [url || "about:blank"], true, { ...opts, verbose: true }); process.exit(0); } catch (e) { console.error("Failed to launch browser"); diff --git a/packages/replay/src/main.ts b/packages/replay/src/main.ts index 0302586a..4c3fefc4 100644 --- a/packages/replay/src/main.ts +++ b/packages/replay/src/main.ts @@ -24,6 +24,7 @@ import { BrowserName, ExternalRecordingEntry, FilterOptions, + LaunchOptions, ListOptions, MetadataOptions, Options, @@ -806,9 +807,10 @@ async function updateMetadata({ async function launchBrowser( browserName: BrowserName, args: string[] = [], - attach: boolean = false, - opts?: Options + record: boolean = false, + opts?: Options & LaunchOptions ) { + debug("launchBrowser: %s %o %s %o", browserName, args, record, opts); const execPath = getExecutablePath(browserName, opts); if (!execPath) { throw new Error(`${browserName} not supported on the current platform`); @@ -831,16 +833,24 @@ async function launchBrowser( firefox: ["-foreground", ...args], }; + const env = { + ...process.env, + }; + + if (record) { + env.RECORD_ALL_CONTENT = "1"; + } + + if (opts?.directory) { + env.RECORD_REPLAY_DIRECTORY = opts?.directory; + } + const proc = spawn(execPath, browserArgs[browserName], { - detached: !attach, - env: { - ...process.env, - RECORD_ALL_CONTENT: "1", - RECORD_REPLAY_DIRECTORY: opts?.directory, - }, + detached: !opts?.attach, + env, stdio: "inherit", }); - if (!attach) { + if (!opts?.attach) { proc.unref(); } else { // Wait for the browser process to finish. diff --git a/packages/replay/src/types.ts b/packages/replay/src/types.ts index 5bea8870..f35c4979 100644 --- a/packages/replay/src/types.ts +++ b/packages/replay/src/types.ts @@ -50,6 +50,11 @@ export interface FilterOptions { includeCrashes?: boolean; } +export interface LaunchOptions { + browser?: string; + attach?: boolean; +} + export interface ListOptions extends FilterOptions { all?: boolean; }