refactor(browser): share chrome resolution helpers
This commit is contained in:
@@ -10,6 +10,7 @@ import {
|
|||||||
findChromeExecutableMac,
|
findChromeExecutableMac,
|
||||||
findChromeExecutableWindows,
|
findChromeExecutableWindows,
|
||||||
isChromeReachable,
|
isChromeReachable,
|
||||||
|
resolveBrowserExecutableForPlatform,
|
||||||
stopClawdChrome,
|
stopClawdChrome,
|
||||||
} from "./chrome.js";
|
} from "./chrome.js";
|
||||||
import {
|
import {
|
||||||
@@ -185,6 +186,29 @@ describe("browser chrome helpers", () => {
|
|||||||
exists.mockRestore();
|
exists.mockRestore();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("resolves Windows executables without LOCALAPPDATA", () => {
|
||||||
|
vi.stubEnv("LOCALAPPDATA", "");
|
||||||
|
vi.stubEnv("ProgramFiles", "C:\\Program Files");
|
||||||
|
vi.stubEnv("ProgramFiles(x86)", "C:\\Program Files (x86)");
|
||||||
|
const marker = path.win32.join(
|
||||||
|
"Program Files",
|
||||||
|
"Google",
|
||||||
|
"Chrome",
|
||||||
|
"Application",
|
||||||
|
"chrome.exe",
|
||||||
|
);
|
||||||
|
const exists = vi
|
||||||
|
.spyOn(fs, "existsSync")
|
||||||
|
.mockImplementation((p) => String(p).includes(marker));
|
||||||
|
const exe = resolveBrowserExecutableForPlatform(
|
||||||
|
{} as Parameters<typeof resolveBrowserExecutableForPlatform>[0],
|
||||||
|
"win32",
|
||||||
|
);
|
||||||
|
expect(exe?.kind).toBe("chrome");
|
||||||
|
expect(exe?.path).toMatch(/chrome\.exe$/);
|
||||||
|
exists.mockRestore();
|
||||||
|
});
|
||||||
|
|
||||||
it("reports reachability based on /json/version", async () => {
|
it("reports reachability based on /json/version", async () => {
|
||||||
vi.stubGlobal(
|
vi.stubGlobal(
|
||||||
"fetch",
|
"fetch",
|
||||||
|
|||||||
@@ -41,6 +41,16 @@ function exists(filePath: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function findFirstExecutable(
|
||||||
|
candidates: Array<BrowserExecutable>,
|
||||||
|
): BrowserExecutable | null {
|
||||||
|
for (const candidate of candidates) {
|
||||||
|
if (exists(candidate.path)) return candidate;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
export function findChromeExecutableMac(): BrowserExecutable | null {
|
export function findChromeExecutableMac(): BrowserExecutable | null {
|
||||||
const candidates: Array<BrowserExecutable> = [
|
const candidates: Array<BrowserExecutable> = [
|
||||||
{
|
{
|
||||||
@@ -78,11 +88,7 @@ export function findChromeExecutableMac(): BrowserExecutable | null {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const candidate of candidates) {
|
return findFirstExecutable(candidates);
|
||||||
if (exists(candidate.path)) return candidate;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findChromeExecutableLinux(): BrowserExecutable | null {
|
export function findChromeExecutableLinux(): BrowserExecutable | null {
|
||||||
@@ -95,11 +101,7 @@ export function findChromeExecutableLinux(): BrowserExecutable | null {
|
|||||||
{ kind: "chrome", path: "/usr/bin/chrome" },
|
{ kind: "chrome", path: "/usr/bin/chrome" },
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const candidate of candidates) {
|
return findFirstExecutable(candidates);
|
||||||
if (exists(candidate.path)) return candidate;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findChromeExecutableWindows(): BrowserExecutable | null {
|
export function findChromeExecutableWindows(): BrowserExecutable | null {
|
||||||
@@ -165,15 +167,12 @@ export function findChromeExecutableWindows(): BrowserExecutable | null {
|
|||||||
),
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const candidate of candidates) {
|
return findFirstExecutable(candidates);
|
||||||
if (exists(candidate.path)) return candidate;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveBrowserExecutable(
|
export function resolveBrowserExecutableForPlatform(
|
||||||
resolved: ResolvedBrowserConfig,
|
resolved: ResolvedBrowserConfig,
|
||||||
|
platform: NodeJS.Platform,
|
||||||
): BrowserExecutable | null {
|
): BrowserExecutable | null {
|
||||||
if (resolved.executablePath) {
|
if (resolved.executablePath) {
|
||||||
if (!exists(resolved.executablePath)) {
|
if (!exists(resolved.executablePath)) {
|
||||||
@@ -184,12 +183,18 @@ function resolveBrowserExecutable(
|
|||||||
return { kind: "custom", path: resolved.executablePath };
|
return { kind: "custom", path: resolved.executablePath };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (process.platform === "darwin") return findChromeExecutableMac();
|
if (platform === "darwin") return findChromeExecutableMac();
|
||||||
if (process.platform === "linux") return findChromeExecutableLinux();
|
if (platform === "linux") return findChromeExecutableLinux();
|
||||||
if (process.platform === "win32") return findChromeExecutableWindows();
|
if (platform === "win32") return findChromeExecutableWindows();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function resolveBrowserExecutable(
|
||||||
|
resolved: ResolvedBrowserConfig,
|
||||||
|
): BrowserExecutable | null {
|
||||||
|
return resolveBrowserExecutableForPlatform(resolved, process.platform);
|
||||||
|
}
|
||||||
|
|
||||||
export function resolveClawdUserDataDir(
|
export function resolveClawdUserDataDir(
|
||||||
profileName = DEFAULT_CLAWD_BROWSER_PROFILE_NAME,
|
profileName = DEFAULT_CLAWD_BROWSER_PROFILE_NAME,
|
||||||
) {
|
) {
|
||||||
|
|||||||
Reference in New Issue
Block a user