Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add replay record command to launch and record #286

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 });
Copy link
Contributor

@callingmedic911 callingmedic911 Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong but this is a breaking change. Do you know if users rely on this command and if we need to have any migrate path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a breaking change. I don't think anyone outside of us and QA is using it though.

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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does detached do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jazzdan added it so the CLI process wouldn't exit until the the browser exited. Not sure why (but not really relevant to this PR either)

env: {
RECORD_ALL_CONTENT: record ? "1" : "",
...process.env,
RECORD_ALL_CONTENT: "1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you still need RECORD_ALL_CONTENT for the record option, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It moved up two lines

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
Loading