UNPKG

firefox-location2

Version:

Approximates the current location of the Firefox browser across platforms.

365 lines (364 loc) 15.6 kB
import * as __WEBPACK_EXTERNAL_MODULE_fs__ from "fs"; import * as __WEBPACK_EXTERNAL_MODULE_path__ from "path"; import * as __WEBPACK_EXTERNAL_MODULE_os__ from "os"; import * as __WEBPACK_EXTERNAL_MODULE_which__ from "which"; import * as __WEBPACK_EXTERNAL_MODULE_child_process__ from "child_process"; import * as __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__ from "node:fs"; import * as __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__ from "node:path"; function resolveFromPuppeteerCache(deps) { const f = (null == deps ? void 0 : deps.fs) ?? __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"]; const env = (null == deps ? void 0 : deps.env) ?? process.env; const platform = (null == deps ? void 0 : deps.platform) ?? process.platform; try { if ('darwin' === platform) { const home = (null == deps ? void 0 : deps.homeDir) ?? env.HOME ?? ''; if (!home) return null; const base = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(home, 'Library', 'Caches', 'puppeteer', 'firefox'); const dirs = listDirs(f, base).filter((d)=>d.startsWith('mac-') || d.startsWith('mac_arm-')); const candidates = []; for (const d of dirs){ candidates.push(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(base, d, 'Firefox.app', 'Contents', 'MacOS', 'firefox')); candidates.push(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(base, d, 'Firefox Nightly.app', 'Contents', 'MacOS', 'firefox')); } return firstExisting(f, candidates); } if ('win32' === platform) { const lad = (null == deps ? void 0 : deps.localAppData) ?? env.LOCALAPPDATA; if (!lad) return null; const base = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(lad, 'puppeteer', 'firefox'); const dirs = listDirs(f, base); const preferred = [ ...dirs.filter((d)=>d.startsWith('win64-')), ...dirs.filter((d)=>d.startsWith('win32-')) ]; const candidates = []; for (const d of preferred){ candidates.push(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(base, d, 'firefox.exe')); candidates.push(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(base, d, 'firefox', 'firefox.exe')); } return firstExisting(f, candidates); } const xdg = env.XDG_CACHE_HOME; const home = (null == deps ? void 0 : deps.homeDir) ?? env.HOME ?? ''; const cacheBase = xdg || (home ? __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(home, '.cache') : void 0); if (!cacheBase) return null; const base = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(cacheBase, 'puppeteer', 'firefox'); const dirs = listDirs(f, base).filter((d)=>d.startsWith('linux-')); const candidates = []; for (const d of dirs){ candidates.push(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(base, d, 'firefox')); candidates.push(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(base, d, 'firefox', 'firefox')); } return firstExisting(f, candidates); } catch { return null; } } function listDirs(f, dir) { try { return f.readdirSync(dir, { withFileTypes: true }).filter((e)=>{ if (!e) return false; const v = e.isDirectory; return 'function' == typeof v ? v.call(e) : Boolean(v); }).map((e)=>e.name || String(e)); } catch { return []; } } function firstExisting(f, candidates) { for (const c of candidates)try { if (c && f.existsSync(c)) return c; } catch {} return null; } function locateFirefox(allowFallbackOrDeps, depsMaybe) { const isBoolean = 'boolean' == typeof allowFallbackOrDeps; const allowFallback = isBoolean ? allowFallbackOrDeps : false; const deps = isBoolean ? depsMaybe : allowFallbackOrDeps; const f = (null == deps ? void 0 : deps.fs) ?? __WEBPACK_EXTERNAL_MODULE_fs__["default"]; const w = (null == deps ? void 0 : deps.which) ?? __WEBPACK_EXTERNAL_MODULE_which__["default"]; const o = (null == deps ? void 0 : deps.os) ?? __WEBPACK_EXTERNAL_MODULE_os__["default"]; const p = (null == deps ? void 0 : deps.path) ?? __WEBPACK_EXTERNAL_MODULE_path__["default"]; const env = (null == deps ? void 0 : deps.env) ?? process.env; const platform = (null == deps ? void 0 : deps.platform) ?? process.platform; const override = env.FIREFOX_BINARY; if (override && f.existsSync(override)) return override; const osx = 'darwin' === platform; const win = 'win32' === platform; const other = !osx && !win; if (other) { var _process_env, _process_env1; const stable = [ 'firefox' ]; const fallbacks = [ 'firefox-esr', 'firefox-developer-edition', 'firefox-devedition', 'firefox-nightly' ]; const candidates = allowFallback ? [ ...stable, ...fallbacks ] : stable; for (const cmd of candidates)try { const resolved = w.sync(cmd); if (resolved) return resolved; } catch (_) {} if (allowFallback) { const linuxPaths = [ '/usr/bin/firefox', '/usr/local/bin/firefox', '/usr/lib/firefox/firefox', '/snap/bin/firefox', '/opt/firefox/firefox', '/usr/local/firefox/firefox', p.join(o.homedir(), 'bin', 'firefox'), p.join(o.homedir(), 'Downloads', 'firefox', 'firefox'), p.join(o.homedir(), '.local', 'share', 'flatpak', 'exports', 'bin', 'org.mozilla.firefox'), '/var/lib/flatpak/exports/bin/org.mozilla.firefox' ]; for (const linuxPath of linuxPaths)if (f.existsSync(linuxPath)) return linuxPath; } if (!deps) { const viaCache = resolveFromPuppeteerCache(); if (viaCache) return viaCache; } const isTestEnv = 'test' === process.env.NODE_ENV || void 0 !== (null == (_process_env = process.env) ? void 0 : _process_env.VITEST) || void 0 !== (null == (_process_env1 = process.env) ? void 0 : _process_env1.JEST_WORKER_ID); const skipCliProbe = isTestEnv && 'darwin' === process.platform; if (allowFallback && !deps && !skipCliProbe) { const viaCLI = resolveFromPuppeteerBrowsersCLI(); if (viaCLI) return viaCLI; } return null; } if (osx) { var _process_env2, _process_env3; const appsAll = [ { app: 'Firefox.app', exec: 'firefox' }, { app: 'Firefox ESR.app', exec: 'firefox' }, { app: 'Firefox Developer Edition.app', exec: 'firefox' }, { app: 'Firefox Nightly.app', exec: 'firefox' } ]; const apps = allowFallback ? appsAll : [ appsAll[0] ]; const systemBase = '/Applications'; const userBase = p.join(o.homedir(), 'Applications'); for (const { app, exec } of apps){ const systemPath = `${systemBase}/${app}/Contents/MacOS/${exec}`; if (f.existsSync(systemPath)) return systemPath; const userPath = `${userBase}/${app}/Contents/MacOS/${exec}`; if (f.existsSync(userPath)) return userPath; } if (!deps) { const viaCache = resolveFromPuppeteerCache(); if (viaCache) return viaCache; } const isTestEnv = 'test' === process.env.NODE_ENV || void 0 !== (null == (_process_env2 = process.env) ? void 0 : _process_env2.VITEST) || void 0 !== (null == (_process_env3 = process.env) ? void 0 : _process_env3.JEST_WORKER_ID); const skipCliProbe = isTestEnv && 'darwin' === process.platform; if (allowFallback && !deps && !skipCliProbe) { const viaCLI = resolveFromPuppeteerBrowsersCLI(); if (viaCLI) return viaCLI; } return null; } { var _process_env4, _process_env5; const prefixes = [ env.LOCALAPPDATA, env.PROGRAMFILES, env['PROGRAMFILES(X86)'] ].filter(Boolean); const suffixesAll = [ p.join('Mozilla Firefox', 'firefox.exe'), p.join('Mozilla Firefox ESR', 'firefox.exe'), p.join('Mozilla Firefox Developer Edition', 'firefox.exe'), p.join('Firefox Nightly', 'firefox.exe') ]; const suffixes = allowFallback ? suffixesAll : [ suffixesAll[0] ]; for (const prefix of prefixes)for (const suffix of suffixes){ const exePath = p.join(prefix, suffix); if (f.existsSync(exePath)) return exePath; } const defaultPathsAll = [ 'C:\\Program Files\\Mozilla Firefox\\firefox.exe', 'C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe', 'C:\\Program Files\\Mozilla Firefox ESR\\firefox.exe', 'C:\\Program Files (x86)\\Mozilla Firefox ESR\\firefox.exe', 'C:\\Program Files\\Mozilla Firefox Developer Edition\\firefox.exe', 'C:\\Program Files (x86)\\Mozilla Firefox Developer Edition\\firefox.exe', 'C:\\Program Files\\Firefox Nightly\\firefox.exe', 'C:\\Program Files (x86)\\Firefox Nightly\\firefox.exe' ]; const defaultPaths = allowFallback ? defaultPathsAll : defaultPathsAll.slice(0, 2); for (const defaultPath of defaultPaths)if (f.existsSync(defaultPath)) return defaultPath; if (!deps) { const viaCache = resolveFromPuppeteerCache(); if (viaCache) return viaCache; } const isTestEnv = 'test' === process.env.NODE_ENV || void 0 !== (null == (_process_env4 = process.env) ? void 0 : _process_env4.VITEST) || void 0 !== (null == (_process_env5 = process.env) ? void 0 : _process_env5.JEST_WORKER_ID); const skipCliProbe = isTestEnv && 'darwin' === process.platform; if (allowFallback && !deps && !skipCliProbe) { const viaCLI = resolveFromPuppeteerBrowsersCLI(); if (viaCLI) return viaCLI; } return null; } } function getInstallGuidance() { return "We couldn't find a Firefox browser on this machine.\n\nHere's the fastest way to get set up:\n\n1) Install Firefox via Puppeteer Browsers (recommended for CI/dev)\n npx @puppeteer/browsers install firefox@stable\n\nThen re-run your command — we will detect it automatically.\n\nAlternatively, install Firefox using your OS package manager and re-run."; } function locateFirefoxOrExplain(options) { const allowFallback = 'boolean' == typeof options ? options : Boolean(null == options ? void 0 : options.allowFallback); const found = locateFirefox(allowFallback) || locateFirefox(true); if ('string' == typeof found && found) return found; throw new Error(getInstallGuidance()); } function getFirefoxVersion(bin, opts) { if ('win32' === process.platform) { try { const psPath = bin.replace(/'/g, "''"); const pv = (0, __WEBPACK_EXTERNAL_MODULE_child_process__.execFileSync)('powershell.exe', [ '-NoProfile', '-Command', `(Get-Item -LiteralPath '${psPath}').VersionInfo.ProductVersion` ], { encoding: 'utf8', stdio: [ 'ignore', 'pipe', 'ignore' ] }).trim(); return normalizeVersion(pv); } catch {} if (null == opts ? void 0 : opts.allowExec) { const v = tryExec(bin, [ '--version' ]); return normalizeVersion(v); } return null; } if ('darwin' === process.platform) { try { const contentsDir = __WEBPACK_EXTERNAL_MODULE_path__["default"].dirname(__WEBPACK_EXTERNAL_MODULE_path__["default"].dirname(bin)); const infoPlist = __WEBPACK_EXTERNAL_MODULE_path__["default"].join(contentsDir, 'Info.plist'); if (__WEBPACK_EXTERNAL_MODULE_fs__["default"].existsSync(infoPlist)) { const xml = __WEBPACK_EXTERNAL_MODULE_fs__["default"].readFileSync(infoPlist, 'utf8'); const v = parsePlistString(xml, 'CFBundleShortVersionString') || parsePlistString(xml, 'CFBundleVersion') || ''; return normalizeVersion(v); } } catch {} if (null == opts ? void 0 : opts.allowExec) { const v = tryExec(bin, [ '--version' ]); return normalizeVersion(v); } return null; } if (null == opts ? void 0 : opts.allowExec) { const v = tryExec(bin, [ '--version' ]); return normalizeVersion(v); } return null; } function normalizeVersion(s) { if (!s) return null; const m = String(s).match(/(\d+(?:\.\d+){1,3})/); return m ? m[1] : null; } function parsePlistString(xml, key) { const re = new RegExp(`<key>${key}<\\/key>\\s*<string>([^<]+)<\\/string>`); const m = xml.match(re); return m ? m[1].trim() : null; } function tryExec(bin, args) { try { return (0, __WEBPACK_EXTERNAL_MODULE_child_process__.execFileSync)(bin, args, { encoding: 'utf8', stdio: [ 'ignore', 'pipe', 'ignore' ] }).trim(); } catch { return null; } } function resolveFromPuppeteerBrowsersCLI() { try { const attempts = [ { cmd: 'npx', args: [ '-y', '@puppeteer/browsers', 'path', 'firefox@stable' ] }, { cmd: 'pnpm', args: [ 'dlx', '@puppeteer/browsers', 'path', 'firefox@stable' ] }, { cmd: 'yarn', args: [ 'dlx', '@puppeteer/browsers', 'path', 'firefox@stable' ] }, { cmd: 'bunx', args: [ '@puppeteer/browsers', 'path', 'firefox@stable' ] } ]; for (const { cmd, args } of attempts)try { const out = (0, __WEBPACK_EXTERNAL_MODULE_child_process__.execFileSync)(cmd, args, { encoding: 'utf8', stdio: [ 'ignore', 'pipe', 'ignore' ], timeout: 2000 }).trim(); if (out && __WEBPACK_EXTERNAL_MODULE_fs__["default"].existsSync(out)) return out; } catch {} } catch {} return null; } export { locateFirefox as default, getFirefoxVersion, getInstallGuidance, locateFirefoxOrExplain };