Skip to content

Commit 8920566

Browse files
committed
fix(cli): hide detached Windows spawns
1 parent a562946 commit 8920566

6 files changed

Lines changed: 34 additions & 3 deletions

File tree

packages/cli/src/commands/previewLifecycle.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,11 @@ describe("background preview lifecycle", () => {
355355

356356
expect(result).toMatchObject({ type: "started", port: 3210, pid: 4321 });
357357
expect(unref).toHaveBeenCalledOnce();
358+
expect(spawn).toHaveBeenCalledWith(
359+
"/usr/bin/node",
360+
expect.any(Array),
361+
expect.objectContaining({ detached: true, windowsHide: true }),
362+
);
358363
expect(existsSync(previewSessionPath(projectDir, stateHome))).toBe(true);
359364
});
360365

packages/cli/src/commands/previewLifecycle.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type SpawnPreview = (
3333
detached: boolean;
3434
stdio: ["ignore", number, number];
3535
env: NodeJS.ProcessEnv;
36+
windowsHide: boolean;
3637
},
3738
) => SpawnResult;
3839

@@ -219,6 +220,7 @@ function spawnDetachedPreview(
219220
detached: true,
220221
stdio: ["ignore", logFd, logFd],
221222
env: process.env,
223+
windowsHide: true,
222224
},
223225
);
224226
} finally {

packages/cli/src/telemetry/client.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,14 @@ describe("telemetry queue delivery", () => {
140140
const [execPath, args, opts] = spawnMock.mock.calls[0] as unknown as [
141141
string,
142142
string[],
143-
{ detached: boolean },
143+
{ detached: boolean; windowsHide?: boolean },
144144
];
145145
expect(execPath).toBe(process.execPath);
146146
expect(args[0]).toBe("-e");
147147
expect(args[1]).toContain("render_complete");
148148
expect(args[1]).toMatch(/[0-9a-f-]{36}/); // event uuid rides along
149149
expect(opts.detached).toBe(true);
150+
expect(opts.windowsHide).toBe(true);
150151

151152
// Queue handed to the child — nothing left for a regular flush.
152153
const fetchMock = vi.fn(() => Promise.resolve(new Response("")));

packages/cli/src/telemetry/transport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export function flushSync(): void {
155155
"-e",
156156
`fetch(${JSON.stringify(`${POSTHOG_HOST}/batch/`)},{method:"POST",headers:{"Content-Type":"application/json"},body:${JSON.stringify(payload)},signal:AbortSignal.timeout(${FLUSH_TIMEOUT_MS})}).catch(()=>{})`,
157157
],
158-
{ detached: true, stdio: "ignore" },
158+
{ detached: true, stdio: "ignore", windowsHide: true },
159159
);
160160
// Let the parent exit without waiting for the child
161161
child.unref();

packages/cli/src/utils/openBrowser.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
1-
import { describe, it, expect } from "vitest";
1+
import { describe, it, expect, vi } from "vitest";
2+
3+
const spawnMock = vi.hoisted(() =>
4+
vi.fn(() => ({
5+
on: vi.fn(),
6+
unref: vi.fn(),
7+
})),
8+
);
9+
vi.mock("node:child_process", () => ({ spawn: spawnMock }));
10+
211
import {
312
buildBrowserArgs,
13+
openBrowser,
414
parseRemoteDebuggingPort,
515
validateRemoteDebuggingPortDeps,
616
} from "./openBrowser.js";
717

18+
describe("openBrowser", () => {
19+
it("hides the console window when spawning a detached browser", () => {
20+
openBrowser("http://localhost:3002", { browserPath: "C:\\Browser\\browser.exe" });
21+
22+
expect(spawnMock).toHaveBeenCalledWith(
23+
"C:\\Browser\\browser.exe",
24+
["http://localhost:3002"],
25+
expect.objectContaining({ detached: true, windowsHide: true }),
26+
);
27+
});
28+
});
29+
830
describe("buildBrowserArgs", () => {
931
it("returns only the URL when no options are given", () => {
1032
expect(buildBrowserArgs("http://localhost:3002", {})).toEqual(["http://localhost:3002"]);

packages/cli/src/utils/openBrowser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export function openBrowser(url: string, options: OpenBrowserOptions = {}): void
8181
const child = spawn(options.browserPath, args, {
8282
detached: true,
8383
stdio: "ignore",
84+
windowsHide: true,
8485
});
8586
child.on("error", () => {});
8687
child.unref();

0 commit comments

Comments
 (0)