Skip to content
Open
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ Show a CLI message for the listening URL.

Open the URL in the browser. Silently ignores errors.

### `browser`

- Default: `process.env.BROWSER` (and use the system default browser if not specified).

Open the URL in the specified browser.

### `clipboard`

- Default: `false` (force disabled on test and production environments)
Expand Down
55 changes: 35 additions & 20 deletions src/lib/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type OpenOptions = {
background?: boolean;
newInstance?: boolean;
allowNonzeroExitCode?: boolean;
browser?: string;
};

export async function open(target: string, options: OpenOptions = {}) {
Expand All @@ -36,6 +37,10 @@ export async function open(target: string, options: OpenOptions = {}) {
if (options.newInstance) {
cliArguments.push("--new");
}

if (options.browser) {
cliArguments.push("-a", options.browser);
}
} else if (process.platform === "win32" || (isWsl() && !isDocker())) {
// --- Windows or WSL ---
command = isWsl()
Expand All @@ -60,6 +65,12 @@ export async function open(target: string, options: OpenOptions = {}) {
encodedArguments.push("-Wait");
}

if (options.browser) {
// Double quote with double quotes to ensure the inner quotes are passed through.
// Inner quotes are delimited for PowerShell interpretation with backticks.
encodedArguments.push(`"\`"${options.browser}\`""`);
}

encodedArguments.push(target);

// Using Base64-encoded command, accepted by PowerShell, to allow special characters.
Expand All @@ -68,29 +79,33 @@ export async function open(target: string, options: OpenOptions = {}) {
);
} else {
// --- Linux ---
command = "xdg-open";
const useSystemXdgOpen =
process.versions.electron || process.platform === "android";
if (!useSystemXdgOpen) {
command = join(os.tmpdir(), "xdg-open");
if (!fs.existsSync(command)) {
try {
fs.writeFileSync(
join(os.tmpdir(), "xdg-open"),
await import("./xdg-open").then((r) => r.xdgOpenScript()),
"utf8",
);
fs.chmodSync(command, 0o755 /* rwx r-x r-x */);
} catch {
command = "xdg-open";
if (options.browser) {
command = options.browser;
} else {
command = "xdg-open";
const useSystemXdgOpen =
process.versions.electron || process.platform === "android";
if (!useSystemXdgOpen) {
command = join(os.tmpdir(), "xdg-open");
if (!fs.existsSync(command)) {
try {
fs.writeFileSync(
join(os.tmpdir(), "xdg-open"),
await import("./xdg-open").then((r) => r.xdgOpenScript()),
"utf8",
);
fs.chmodSync(command, 0o755 /* rwx r-x r-x */);
} catch {
command = "xdg-open";
}
}
}
}

if (!options.wait) {
// `xdg-open` will block the process unless stdio is ignored and it's detached from the parent even if it's unref'd.
childProcessOptions.stdio = "ignore";
childProcessOptions.detached = true;
if (!options.wait) {
// `xdg-open` will block the process unless stdio is ignored and it's detached from the parent even if it's unref'd.
childProcessOptions.stdio = "ignore";
childProcessOptions.detached = true;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/listen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export async function listen(
}

const _open = async () => {
await open(getURL()).catch(() => {});
await open(getURL(), { browser: process.env.BROWSER }).catch(() => {});
};
if (listhenOptions.open) {
await _open();
Expand Down