|
| 1 | +import { mkdir } from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import puppeteer from "puppeteer"; |
| 4 | +import os from "node:os"; |
| 5 | +import { existsSync } from "node:fs"; |
| 6 | +import { |
| 7 | + Browser, |
| 8 | + BrowserPlatform, |
| 9 | + BrowserTag, |
| 10 | + Cache, |
| 11 | + computeExecutablePath, |
| 12 | + install, |
| 13 | + resolveBuildId, |
| 14 | +} from "@puppeteer/browsers"; |
| 15 | +import { writeJSONToStdout } from "./stdin"; |
| 16 | + |
| 17 | +/** |
| 18 | + * Gets the executable path for the installed Chrome browser. |
| 19 | + * @param cacheDir - The cache directory where browsers are stored |
| 20 | + * @returns The executable path, or null if not found |
| 21 | + */ |
| 22 | +const getInstalledBrowserPath = (cacheDir: string): string | null => { |
| 23 | + try { |
| 24 | + const platformMap: Record<string, BrowserPlatform> = { |
| 25 | + win32: BrowserPlatform.WIN32, |
| 26 | + darwin: BrowserPlatform.MAC, |
| 27 | + linux: BrowserPlatform.LINUX, |
| 28 | + }; |
| 29 | + |
| 30 | + const currentPlatform = process.platform; |
| 31 | + const browserPlatform = |
| 32 | + platformMap[currentPlatform] || BrowserPlatform.LINUX; |
| 33 | + |
| 34 | + // Try to get the latest installed browser |
| 35 | + const cache = new Cache(cacheDir); |
| 36 | + const installedBrowsers = cache.getInstalledBrowsers(); |
| 37 | + const chromeBrowser = installedBrowsers.find( |
| 38 | + (b) => b.browser === Browser.CHROME && b.platform === browserPlatform, |
| 39 | + ); |
| 40 | + |
| 41 | + if (chromeBrowser) { |
| 42 | + return computeExecutablePath({ |
| 43 | + cacheDir: cacheDir, |
| 44 | + browser: Browser.CHROME, |
| 45 | + platform: browserPlatform, |
| 46 | + buildId: chromeBrowser.buildId, |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + return null; |
| 51 | + } catch (/* eslint-disable-line */ error) { |
| 52 | + return null; |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +/** |
| 57 | + * Sets up Puppeteer cache directory and ensures browser is installed. |
| 58 | + * This is necessary when running in a bundled binary where the default |
| 59 | + * cache path might not be accessible. |
| 60 | + * Uses platform-specific default cache directories: |
| 61 | + * - Windows: %LOCALAPPDATA%\puppeteer |
| 62 | + * - macOS: ~/Library/Caches/puppeteer |
| 63 | + * - Linux: ~/.cache/puppeteer |
| 64 | + * @returns The executable path of the installed browser, or null if not found |
| 65 | + */ |
| 66 | +export const setupPuppeteer = async (): Promise<string | null> => { |
| 67 | + // Use platform-specific default cache directory |
| 68 | + const cacheDir = path.join(os.homedir(), ".cache", "puppeteer"); |
| 69 | + |
| 70 | + // Ensure the cache directory exists |
| 71 | + await mkdir(cacheDir, { recursive: true }); |
| 72 | + |
| 73 | + // Check if browser is installed |
| 74 | + let executablePath: string | null = null; |
| 75 | + let needsInstall = false; |
| 76 | + |
| 77 | + // First, try to get the executable path from installed browsers |
| 78 | + executablePath = getInstalledBrowserPath(cacheDir); |
| 79 | + |
| 80 | + // If not found, try Puppeteer's method |
| 81 | + if (!executablePath) { |
| 82 | + try { |
| 83 | + executablePath = puppeteer.executablePath(); |
| 84 | + // Check if the executable actually exists |
| 85 | + if (!executablePath || !existsSync(executablePath)) { |
| 86 | + needsInstall = true; |
| 87 | + } |
| 88 | + } catch (/* eslint-disable-line */ error: unknown) { |
| 89 | + // Browser is not installed, we'll try to install it below |
| 90 | + needsInstall = true; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if (needsInstall) { |
| 95 | + try { |
| 96 | + const platformMap: Record<string, BrowserPlatform> = { |
| 97 | + win32: BrowserPlatform.WIN32, |
| 98 | + darwin: BrowserPlatform.MAC, |
| 99 | + linux: BrowserPlatform.LINUX, |
| 100 | + }; |
| 101 | + |
| 102 | + const currentPlatform = process.platform; |
| 103 | + const browserPlatform = |
| 104 | + platformMap[currentPlatform] || BrowserPlatform.LINUX; |
| 105 | + |
| 106 | + const buildId = await resolveBuildId( |
| 107 | + Browser.CHROME, |
| 108 | + browserPlatform, |
| 109 | + BrowserTag.LATEST, |
| 110 | + ); |
| 111 | + |
| 112 | + // Install the browser with the correct buildId |
| 113 | + // This may take 30-60 seconds, so ensure timeout is set appropriately |
| 114 | + await install({ |
| 115 | + browser: Browser.CHROME, |
| 116 | + buildId: buildId, |
| 117 | + cacheDir: cacheDir, |
| 118 | + }); |
| 119 | + |
| 120 | + // After installation, get the executable path |
| 121 | + executablePath = computeExecutablePath({ |
| 122 | + cacheDir: cacheDir, |
| 123 | + browser: Browser.CHROME, |
| 124 | + platform: browserPlatform, |
| 125 | + buildId: buildId, |
| 126 | + }); |
| 127 | + } catch (installError) { |
| 128 | + writeJSONToStdout({ |
| 129 | + success: false, |
| 130 | + error: "Error installing Chrome browser", |
| 131 | + context: |
| 132 | + installError instanceof Error |
| 133 | + ? installError.message |
| 134 | + : String(installError), |
| 135 | + }); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return executablePath; |
| 140 | +}; |
0 commit comments