Skip to content

Commit 19fe5d7

Browse files
cdervclaude
andcommitted
Refactor chromeCb in check.ts to separate detection from formatting
Extract browser detection logic into `detectChromeForCheck()` helper, replacing the 4-branch if/else chain with a data object and single formatting block. Fixes redundant `safeExistsSync` call and normalizes JSON key from `QUARTO_CHROMIUM_invalid` to `warning`. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f8fece7 commit 19fe5d7

1 file changed

Lines changed: 94 additions & 56 deletions

File tree

src/command/check/check.ts

Lines changed: 94 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -442,67 +442,28 @@ async function checkInstall(conf: CheckConfiguration) {
442442
conf.jsonResult.chrome = chromeJson;
443443
}
444444
const chromeCb = async () => {
445-
const envPath = Deno.env.get("QUARTO_CHROMIUM");
446-
const chromeHsPath = chromeHeadlessShellExecutablePath();
447-
const chromeDetected = await findChrome();
448-
const chromiumTool = installableTool("chromium");
449-
const chromiumQuarto = chromiumTool && await chromiumTool.installed()
450-
? chromiumTool
451-
: undefined;
452-
if (envPath && !safeExistsSync(envPath)) {
453-
chromeHeadlessOutput.push(
454-
`${kIndent}NOTE: QUARTO_CHROMIUM is set to ${envPath} but the path does not exist.`,
455-
);
456-
chromeJson["QUARTO_CHROMIUM_invalid"] = envPath;
445+
const check = await detectChromeForCheck();
446+
447+
if (check.warning) {
448+
chromeHeadlessOutput.push(`${kIndent}NOTE: ${check.warning}`);
449+
chromeJson["warning"] = check.warning;
457450
}
458-
if (envPath && safeExistsSync(envPath)) {
459-
chromeHeadlessOutput.push(`${kIndent}Using: Chrome from QUARTO_CHROMIUM`);
460-
chromeHeadlessOutput.push(`${kIndent}Path: ${envPath}`);
461-
chromeJson["path"] = envPath;
462-
chromeJson["source"] = "QUARTO_CHROMIUM";
463-
} else if (chromeHsPath !== undefined) {
464-
const version = readInstalledVersion(chromeHeadlessShellInstallDir());
465-
chromeJson["source"] = "quarto-chrome-headless-shell";
466-
chromeHeadlessOutput.push(
467-
`${kIndent}Using: Chrome Headless Shell installed by Quarto`,
468-
);
469-
chromeHeadlessOutput.push(`${kIndent}Path: ${chromeHsPath}`);
470-
chromeJson["path"] = chromeHsPath;
451+
452+
if (check.detected) {
453+
const { label, path, source, displaySource, version } = check.detected;
454+
chromeHeadlessOutput.push(`${kIndent}Using: ${label}`);
455+
if (path) {
456+
chromeHeadlessOutput.push(`${kIndent}Path: ${path}`);
457+
chromeJson["path"] = path;
458+
}
459+
chromeJson["source"] = source;
460+
if (displaySource) {
461+
chromeHeadlessOutput.push(`${kIndent}Source: ${displaySource}`);
462+
}
471463
if (version) {
472464
chromeHeadlessOutput.push(`${kIndent}Version: ${version}`);
473465
chromeJson["version"] = version;
474466
}
475-
} else if (chromeDetected.path !== undefined) {
476-
chromeHeadlessOutput.push(`${kIndent}Using: Chrome found on system`);
477-
chromeHeadlessOutput.push(
478-
`${kIndent}Path: ${chromeDetected.path}`,
479-
);
480-
if (chromeDetected.source) {
481-
chromeHeadlessOutput.push(`${kIndent}Source: ${chromeDetected.source}`);
482-
}
483-
chromeJson["path"] = chromeDetected.path;
484-
chromeJson["source"] = chromeDetected.source;
485-
} else if (chromiumQuarto !== undefined) {
486-
chromeJson["source"] = "quarto";
487-
chromeHeadlessOutput.push(
488-
`${kIndent}Using: Chromium installed by Quarto`,
489-
);
490-
if (chromiumQuarto.binDir) {
491-
const binPath = await chromiumQuarto.binDir();
492-
if (binPath) {
493-
chromeHeadlessOutput.push(
494-
`${kIndent}Path: ${binPath}`,
495-
);
496-
chromeJson["path"] = binPath;
497-
}
498-
}
499-
const chromiumVersion = await chromiumQuarto.installedVersion();
500-
if (chromiumVersion) {
501-
chromeHeadlessOutput.push(
502-
`${kIndent}Version: ${chromiumVersion}`,
503-
);
504-
chromeJson["version"] = chromiumVersion;
505-
}
506467
} else {
507468
chromeHeadlessOutput.push(`${kIndent}Chrome: (not detected)`);
508469
chromeJson["installed"] = false;
@@ -562,3 +523,80 @@ title: "Title"
562523
}, markdownRenderCb);
563524
}
564525
}
526+
527+
interface ChromeDetectionResult {
528+
label: string;
529+
path?: string;
530+
source: string;
531+
version?: string;
532+
displaySource?: string;
533+
}
534+
535+
interface ChromeCheckInfo {
536+
warning?: string;
537+
detected?: ChromeDetectionResult;
538+
}
539+
540+
async function detectChromeForCheck(): Promise<ChromeCheckInfo> {
541+
const result: ChromeCheckInfo = {};
542+
543+
// 1. QUARTO_CHROMIUM environment variable
544+
const envPath = Deno.env.get("QUARTO_CHROMIUM");
545+
if (envPath) {
546+
if (safeExistsSync(envPath)) {
547+
result.detected = {
548+
label: "Chrome from QUARTO_CHROMIUM",
549+
path: envPath,
550+
source: "QUARTO_CHROMIUM",
551+
};
552+
return result;
553+
}
554+
result.warning =
555+
`QUARTO_CHROMIUM is set to ${envPath} but the path does not exist.`;
556+
}
557+
558+
// 2. Chrome headless shell installed by Quarto
559+
const chromeHsPath = chromeHeadlessShellExecutablePath();
560+
if (chromeHsPath !== undefined) {
561+
const version = readInstalledVersion(chromeHeadlessShellInstallDir());
562+
result.detected = {
563+
label: "Chrome Headless Shell installed by Quarto",
564+
path: chromeHsPath,
565+
source: "quarto-chrome-headless-shell",
566+
version,
567+
};
568+
return result;
569+
}
570+
571+
// 3. System Chrome
572+
const chromeDetected = await findChrome();
573+
if (chromeDetected.path !== undefined) {
574+
result.detected = {
575+
label: "Chrome found on system",
576+
path: chromeDetected.path,
577+
source: chromeDetected.source ?? "system",
578+
displaySource: chromeDetected.source,
579+
};
580+
return result;
581+
}
582+
583+
// 4. Legacy chromium installed by Quarto
584+
const chromiumTool = installableTool("chromium");
585+
if (chromiumTool && await chromiumTool.installed()) {
586+
let path: string | undefined;
587+
if (chromiumTool.binDir) {
588+
path = await chromiumTool.binDir();
589+
}
590+
const version = await chromiumTool.installedVersion();
591+
result.detected = {
592+
label: "Chromium installed by Quarto",
593+
path,
594+
source: "quarto",
595+
version,
596+
};
597+
return result;
598+
}
599+
600+
// 5. Not found
601+
return result;
602+
}

0 commit comments

Comments
 (0)