repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

Add ui browser opener

06ec65d7c6fcb1e6346e5e267b15e9ac4c588b07

Oliver Krylow <o.krylow@isp-insoft.de> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

extensions/ui/__tests__/runtime.test.ts

Added
new file mode 100644
1 +import { describe, expect, it } from "vitest";
2 +import { openerCandidates } from "../open";
4 +describe("ui browser opener", () => {
5 + it("uses platform openers", () => {
6 + expect(openerCandidates("http://x", { platform: "win32", env: {} })[0]).toEqual(
7 + {
8 + command: "cmd.exe",
9 + args: ["/c", "start", "", "http://x"],
10 + },
11 + );
12 + expect(
13 + openerCandidates("http://x", { platform: "darwin", env: {} })[0],
14 + ).toEqual({
15 + command: "open",
16 + args: ["http://x"],
17 + });
18 + expect(openerCandidates("http://x", { platform: "linux", env: {} })[0]).toEqual(
19 + {
20 + command: "xdg-open",
21 + args: ["http://x"],
22 + },
23 + );
24 + });
26 + it("uses termux opener when available", () => {
27 + expect(
28 + openerCandidates("http://x", {
29 + platform: "linux",
30 + env: { TERMUX_VERSION: "1" },
31 + })[0],
32 + ).toEqual({
33 + command: "termux-open-url",
34 + args: ["http://x"],
35 + });
36 + });
37 +});

extensions/ui/open.ts

Added
new file mode 100644
1 +import { spawn } from "node:child_process";
2 +import { uiError } from "./errors";
4 +export function openerCandidates(
5 + url: string,
6 + options: { platform?: NodeJS.Platform; env?: NodeJS.ProcessEnv } = {},
7 +): Array<{ command: string; args: string[] }> {
8 + const platform = options.platform ?? process.platform;
9 + const env = options.env ?? process.env;
10 + if (env.TERMUX_VERSION) return [{ command: "termux-open-url", args: [url] }];
11 + if (platform === "darwin") return [{ command: "open", args: [url] }];
12 + if (platform === "win32") {
13 + return [{ command: "cmd.exe", args: ["/c", "start", "", url] }];
14 + }
15 + return [
16 + { command: "xdg-open", args: [url] },
17 + { command: "wslview", args: [url] },
18 + { command: "gio", args: ["open", url] },
19 + ];
22 +export async function openBrowser(url: string): Promise<void> {
23 + if (process.env.NODE_ENV === "test" || process.env.VITEST) return;
24 + for (const candidate of openerCandidates(url)) {
25 + if (await trySpawn(candidate.command, candidate.args)) return;
26 + }
27 + throw uiError("open_failed", "Could not open browser", {
28 + action:
29 + "Open the /ui link manually from the terminal widget or scan the QR code.",
30 + retryable: true,
31 + });
34 +function trySpawn(command: string, args: string[]): Promise<boolean> {
35 + return new Promise((resolve) => {
36 + const child = spawn(command, args, { detached: true, stdio: "ignore" });
37 + let settled = false;
38 + const done = (ok: boolean) => {
39 + if (settled) return;
40 + settled = true;
41 + resolve(ok);
42 + };
43 + child.once("error", () => done(false));
44 + child.once("spawn", () => {
45 + child.unref();
46 + done(true);
47 + });
48 + setTimeout(() => done(false), 1500).unref();
49 + });