Skip to content

Commit

Permalink
Add replay record command to launch and record
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjduffy committed Dec 2, 2023
1 parent dfb88cb commit c9550a3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
39 changes: 33 additions & 6 deletions packages/replay/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import {
BrowserName,
FilterOptions,
LaunchOptions,
MetadataOptions,
Options,
SourcemapUploadOptions,
Expand Down Expand Up @@ -74,6 +75,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 @@ -197,20 +209,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
12 changes: 7 additions & 5 deletions packages/replay/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
BrowserName,
ExternalRecordingEntry,
FilterOptions,
LaunchOptions,
ListOptions,
MetadataOptions,
Options,
Expand Down Expand Up @@ -804,9 +805,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 @@ -830,15 +832,15 @@ async function launchBrowser(
};

const proc = spawn(execPath, browserArgs[browserName], {
detached: !attach,
detached: !opts?.attach,
env: {
RECORD_ALL_CONTENT: record ? "1" : "",
...process.env,
RECORD_ALL_CONTENT: "1",
RECORD_REPLAY_DIRECTORY: opts?.directory,
},
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 c9550a3

Please sign in to comment.