Skip to content

Commit

Permalink
replay: add attach flag to launch command (#246)
Browse files Browse the repository at this point in the history
* replay: add detached flag to launch command

Fixes BAC-3976

* s/detach/attach

* fix formatting

* swap order of arguments

* swap invocation too

---------

Co-authored-by: Ryan Duffy <ryan@replay.io>
  • Loading branch information
jazzdan and ryanjduffy authored Sep 14, 2023
1 parent bcce968 commit d90ceb5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
14 changes: 12 additions & 2 deletions packages/replay/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ commandWithGlobalOptions("upload <id>")
commandWithGlobalOptions("launch [url]")
.description("Launch the replay browser")
.option("-b, --browser <browser>", "Browser to launch", "chromium")
.option(
"--attach <true|false>",
"Whether to attach to the browser process after launching",
false
)
.allowUnknownOption()
.action(commandLaunchBrowser);

Expand Down Expand Up @@ -188,15 +193,20 @@ async function commandUploadRecording(id: string, opts: CommandLineOptions) {

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

const browser = fuzzyBrowserName(opts.browser) || "chromium";
assertValidBrowserName(browser);

await launchBrowser(browser, [url || "about:blank"]);
const attach = opts.attach || false;

await launchBrowser(browser, [url || "about:blank"], attach);
process.exit(0);
} catch (e) {
console.error("Failed to launch browser");
Expand Down
8 changes: 6 additions & 2 deletions packages/replay/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,11 @@ async function updateMetadata({
}
}

async function launchBrowser(browserName: BrowserName, args: string[] = []) {
async function launchBrowser(
browserName: BrowserName,
args: string[] = [],
attach: boolean = false
) {
const execPath = getExecutablePath(browserName);
if (!execPath) {
throw new Error(`${browserName} not supported on the current platform`);
Expand All @@ -750,7 +754,7 @@ async function launchBrowser(browserName: BrowserName, args: string[] = []) {
firefox: ["-foreground", ...args],
};

const proc = spawn(execPath, browserArgs[browserName], { detached: true });
const proc = spawn(execPath, browserArgs[browserName], { detached: !attach });
proc.unref();

return proc;
Expand Down

0 comments on commit d90ceb5

Please sign in to comment.