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 "replayio open" command to launch browser in non-recording mode #526

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/grumpy-icons-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"replayio": patch
---

Add "open" command to open Replay browser in non-recording mode
1 change: 1 addition & 0 deletions packages/replayio/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "./commands/info";
import "./commands/list";
import "./commands/login";
import "./commands/logout";
import "./commands/open";
import "./commands/record";
import "./commands/remove";
import "./commands/update";
Expand Down
18 changes: 18 additions & 0 deletions packages/replayio/src/commands/open.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { killBrowserIfRunning } from "../utils/browser/killBrowserIfRunning";
import { launchBrowser } from "../utils/browser/launchBrowser";
import { registerCommand } from "../utils/commander/registerCommand";
import { exitProcess } from "../utils/exitProcess";

registerCommand("open", { checkForRuntimeUpdate: true, requireAuthentication: true })
.argument("[url]", `URL to open (default: "about:blank")`)
.description("Open the replay browser with recording disabled")
.action(open)
.allowUnknownOption();

async function open(url: string = "about:blank") {
await killBrowserIfRunning();

await launchBrowser(url, { record: false });

await exitProcess(0);
}
20 changes: 2 additions & 18 deletions packages/replayio/src/commands/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import debug from "debug";
import { v4 as uuid } from "uuid";
import { ProcessError } from "../utils/ProcessError";
import { logAsyncOperation } from "../utils/async/logAsyncOperation";
import { getRunningProcess } from "../utils/browser/getRunningProcess";
import { killBrowserIfRunning } from "../utils/browser/killBrowserIfRunning";
import { launchBrowser } from "../utils/browser/launchBrowser";
import { reportBrowserCrash } from "../utils/browser/reportBrowserCrash";
import { registerCommand } from "../utils/commander/registerCommand";
import { confirm } from "../utils/confirm";
import { exitProcess } from "../utils/exitProcess";
import { killProcess } from "../utils/killProcess";
import { trackEvent } from "../utils/mixpanel/trackEvent";
import { canUpload } from "../utils/recordings/canUpload";
import { getRecordings } from "../utils/recordings/getRecordings";
Expand All @@ -34,22 +33,7 @@ async function record(url: string = "about:blank") {
const processGroupId = uuid();

try {
const process = await getRunningProcess();
if (process) {
const confirmed = await confirm(
"The replay browser is already running. You'll need to close it before starting a new recording.\n\nWould you like to close it now?",
true
);
if (confirmed) {
const killResult = await killProcess(process.pid);
if (!killResult) {
console.log("Something went wrong trying to close the replay browser. Please try again.");
await exitProcess(1);
}
} else {
await exitProcess(0);
}
}
await killBrowserIfRunning();

await launchBrowser(url, { processGroupId });
} catch (error) {
Expand Down
24 changes: 24 additions & 0 deletions packages/replayio/src/utils/browser/killBrowserIfRunning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { confirm } from "../confirm";
import { exitProcess } from "../exitProcess";
import { killProcess } from "../killProcess";
import { getRunningProcess } from "./getRunningProcess";

export async function killBrowserIfRunning() {
const process = await getRunningProcess();
if (process) {
const confirmed = await confirm(
"The replay browser is already running. You'll need to close it before running this command.\n\nWould you like to close it now?",
true
);
if (confirmed) {
const killResult = await killProcess(process.pid);
if (!killResult) {
console.log("Something went wrong trying to close the replay browser. Please try again.");

await exitProcess(1);
}
} else {
await exitProcess(0);
}
}
}
22 changes: 16 additions & 6 deletions packages/replayio/src/utils/browser/launchBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import { getBrowserPath } from "./getBrowserPath";
export async function launchBrowser(
url: string,
options: {
processGroupId: string;
processGroupId?: string;
record?: boolean;
}
) {
const { processGroupId } = options;
const { processGroupId, record = true } = options;

const profileDir = join(runtimePath, "profiles", runtimeMetadata.runtime);
ensureDirSync(profileDir);
Expand All @@ -28,9 +29,10 @@ export async function launchBrowser(
];
const processOptions = {
env: {
RECORD_ALL_CONTENT: "1",
RECORD_ALL_CONTENT: record ? "1" : undefined,
RECORD_REPLAY_DONT_RECORD: record ? undefined : "1",
Copy link
Member

Choose a reason for hiding this comment

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

TIL, what is this? test plugins dont set this at all (IIRC)

Copy link
Member

Choose a reason for hiding this comment

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

sideways: do u know what RECORD_REPLAY_DRIVER is for? test plugins use it but we don't use it here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TIL too. Jason mentioned it in Discord. All I know about it is that if it's set, it tells the runtime not to record anything.

Copy link
Contributor Author

@bvaughn bvaughn Jun 12, 2024

Choose a reason for hiding this comment

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

sideways: do u know what RECORD_REPLAY_DRIVER is for? test plugins use it but we don't use it here

No. Not the right person to answer that 😆

RECORD_REPLAY_DIRECTORY: getReplayPath(),
RECORD_REPLAY_METADATA: JSON.stringify({ processGroupId }),
RECORD_REPLAY_METADATA: processGroupId ? JSON.stringify({ processGroupId }) : undefined,
RECORD_REPLAY_VERBOSE: "1",
},
stdio: undefined,
Expand All @@ -55,7 +57,11 @@ export async function launchBrowser(
const spawnDeferred = spawnProcess(browserExecutablePath, args, processOptions, {
onSpawn: () => {
if (process.stdin.isTTY) {
console.log(`Recording... ${dim("(press any key to stop recording)")}`);
if (record) {
console.log(`Recording... ${dim("(press any key to stop recording)")}`);
} else {
console.log("Press any key to close the browser");
}

prompt({
signal: abortControllerForPrompt.signal,
Expand All @@ -66,7 +72,11 @@ export async function launchBrowser(
spawnDeferred.data.kill();
});
} else {
console.log(`Recording... ${dim("(quit the Replay Browser to stop recording)")}`);
if (record) {
console.log(`Recording... ${dim("(quit the Replay Browser to stop recording)")}`);
} else {
console.log("Quit the Replay browser when you're finished");
}
}
},
printStderr: (text: string) => {
Expand Down