repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

Avoid cmd flash for mockup browser open

43a2b234635679c797902408c7d9fbc2fe13b8e6

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

unsigned

Browse filesCompare with HEAD

Changed files (2)

extensions/super/__tests__/mockup.test.ts

Modified
3 3 import assert from "node:assert";
4 4 import { describe, it } from "vitest";
5 5 import {
6 6 MOCKUP_FULL_DOCUMENT_ERROR,
7 7 type MockupRequest,
8 + openerCandidates,
8 9 renderMockupPage,
9 10 startMockupServer,
10 11 validateMockupRequest,
153 +describe("mockup browser opener", () => {
154 + it("uses explorer directly on win32 to avoid cmd flashes", () => {
155 + assert.deepStrictEqual(
156 + openerCandidates("http://127.0.0.1:1", {
157 + platform: "win32",
158 + env: {},
159 + })[0],
161 + command: "explorer.exe",
162 + args: ["http://127.0.0.1:1"],
163 + windowsHide: true,
164 + },
165 + );
166 + });
167 +});
152 169 describe("mockup server", () => {
153 170 it("rejects multiple submitted selections unless multi is enabled", async () => {
154 171 const server = await startMockupServer(

extensions/super/mockup.ts

Modified
446 -function openerCandidates(
446 +export type OpenerCandidate = {
447 + command: string;
448 + args: string[];
449 + windowsHide?: boolean;
452 +export function openerCandidates(
447 453 url: string,
448 -): Array<{ command: string; args: string[] }> {
449 - if (process.env.TERMUX_VERSION)
450 - return [{ command: "termux-open-url", args: [url] }];
451 - if (process.platform === "darwin") return [{ command: "open", args: [url] }];
452 - if (process.platform === "win32")
453 - return [{ command: "cmd.exe", args: ["/c", "start", "", url] }];
454 + options: { platform?: NodeJS.Platform; env?: NodeJS.ProcessEnv } = {},
455 +): OpenerCandidate[] {
456 + const platform = options.platform ?? process.platform;
457 + const env = options.env ?? process.env;
458 + if (env.TERMUX_VERSION) return [{ command: "termux-open-url", args: [url] }];
459 + if (platform === "darwin") return [{ command: "open", args: [url] }];
460 + if (platform === "win32")
461 + return [{ command: "explorer.exe", args: [url], windowsHide: true }];
454 462 return [
455 463 { command: "xdg-open", args: [url] },
456 464 { command: "wslview", args: [url] },
461 469 function trySpawn(command: string, args: string[]): Promise<boolean> {
462 470 return new Promise((resolve) => {
463 - const child = spawn(command, args, { detached: true, stdio: "ignore" });
471 + const child = spawn(command, args, {
472 + detached: true,
473 + stdio: "ignore",
474 + windowsHide: true,
475 + });
464 476 let settled = false;
465 477 const done = (ok: boolean) => {
466 478 if (settled) return;