Skip to content

Commit

Permalink
Add "replayio open" command to launch browser in non-recording mode (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn authored Jun 12, 2024
1 parent 7628818 commit b7113e9
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 24 deletions.
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",
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

0 comments on commit b7113e9

Please sign in to comment.