Skip to content

Commit

Permalink
Add replay record command to launch and record (#286)
Browse files Browse the repository at this point in the history
* Add replay record command to launch and record

* conditionally set launch env variables
  • Loading branch information
ryanjduffy authored Dec 4, 2023
1 parent aa02a94 commit 0f79844
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
39 changes: 33 additions & 6 deletions packages/replay/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import {
BrowserName,
FilterOptions,
LaunchOptions,
MetadataOptions,
Options,
SourcemapUploadOptions,
Expand Down Expand Up @@ -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>", "Browser to launch", "chromium")
.option(
"--attach <true|false>",
"Whether to attach to the browser process after launching",
false
)
.allowUnknownOption()
.action(commandLaunchBrowserAndRecord);

commandWithGlobalOptions("process <id>")
.description("Upload a recording to the remote server and process it.")
.option("--api-key <key>", "Authentication API Key")
Expand Down Expand Up @@ -203,20 +215,35 @@ async function commandUploadRecording(id: string, opts: CommandLineOptions) {

async function commandLaunchBrowser(
url: string | undefined,
opts: Pick<CommandLineOptions, "warn" | "directory"> & {
browser: string | undefined;
attach: boolean | undefined;
}
opts: Pick<CommandLineOptions, "warn" | "directory"> & LaunchOptions
) {
try {
debug("Options", opts);

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<CommandLineOptions, "warn" | "directory"> & 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");
Expand Down
28 changes: 19 additions & 9 deletions packages/replay/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
BrowserName,
ExternalRecordingEntry,
FilterOptions,
LaunchOptions,
ListOptions,
MetadataOptions,
Options,
Expand Down Expand Up @@ -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`);
Expand All @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions packages/replay/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export interface FilterOptions {
includeCrashes?: boolean;
}

export interface LaunchOptions {
browser?: string;
attach?: boolean;
}

export interface ListOptions extends FilterOptions {
all?: boolean;
}
Expand Down

0 comments on commit 0f79844

Please sign in to comment.