UNPKG

firefox-location2

Version:

Approximates the current location of the Firefox browser across platforms.

423 lines (422 loc) 17.5 kB
"use strict"; var __webpack_require__ = {}; (()=>{ __webpack_require__.n = (module)=>{ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module; __webpack_require__.d(getter, { a: getter }); return getter; }; })(); (()=>{ __webpack_require__.d = (exports1, definition)=>{ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, { enumerable: true, get: definition[key] }); }; })(); (()=>{ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop); })(); (()=>{ __webpack_require__.r = (exports1)=>{ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, { value: 'Module' }); Object.defineProperty(exports1, '__esModule', { value: true }); }; })(); var __webpack_exports__ = {}; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { default: ()=>locateFirefox, getFirefoxVersion: ()=>getFirefoxVersion, getInstallGuidance: ()=>getInstallGuidance, locateFirefoxOrExplain: ()=>locateFirefoxOrExplain }); const external_fs_namespaceObject = require("fs"); var external_fs_default = /*#__PURE__*/ __webpack_require__.n(external_fs_namespaceObject); const external_path_namespaceObject = require("path"); var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject); const external_os_namespaceObject = require("os"); var external_os_default = /*#__PURE__*/ __webpack_require__.n(external_os_namespaceObject); const external_which_namespaceObject = require("which"); var external_which_default = /*#__PURE__*/ __webpack_require__.n(external_which_namespaceObject); const external_child_process_namespaceObject = require("child_process"); const external_node_fs_namespaceObject = require("node:fs"); var external_node_fs_default = /*#__PURE__*/ __webpack_require__.n(external_node_fs_namespaceObject); const external_node_path_namespaceObject = require("node:path"); var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject); function resolveFromPuppeteerCache(deps) { const f = (null == deps ? void 0 : deps.fs) ?? external_node_fs_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 = external_node_path_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(external_node_path_default().join(base, d, 'Firefox.app', 'Contents', 'MacOS', 'firefox')); candidates.push(external_node_path_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 = external_node_path_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(external_node_path_default().join(base, d, 'firefox.exe')); candidates.push(external_node_path_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 ? external_node_path_default().join(home, '.cache') : void 0); if (!cacheBase) return null; const base = external_node_path_default().join(cacheBase, 'puppeteer', 'firefox'); const dirs = listDirs(f, base).filter((d)=>d.startsWith('linux-')); const candidates = []; for (const d of dirs){ candidates.push(external_node_path_default().join(base, d, 'firefox')); candidates.push(external_node_path_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) ?? external_fs_default(); const w = (null == deps ? void 0 : deps.which) ?? external_which_default(); const o = (null == deps ? void 0 : deps.os) ?? external_os_default(); const p = (null == deps ? void 0 : deps.path) ?? external_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, external_child_process_namespaceObject.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 = external_path_default().dirname(external_path_default().dirname(bin)); const infoPlist = external_path_default().join(contentsDir, 'Info.plist'); if (external_fs_default().existsSync(infoPlist)) { const xml = external_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, external_child_process_namespaceObject.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, external_child_process_namespaceObject.execFileSync)(cmd, args, { encoding: 'utf8', stdio: [ 'ignore', 'pipe', 'ignore' ], timeout: 2000 }).trim(); if (out && external_fs_default().existsSync(out)) return out; } catch {} } catch {} return null; } exports["default"] = __webpack_exports__["default"]; exports.getFirefoxVersion = __webpack_exports__.getFirefoxVersion; exports.getInstallGuidance = __webpack_exports__.getInstallGuidance; exports.locateFirefoxOrExplain = __webpack_exports__.locateFirefoxOrExplain; for(var __webpack_i__ in __webpack_exports__)if (-1 === [ "default", "getFirefoxVersion", "getInstallGuidance", "locateFirefoxOrExplain" ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__]; Object.defineProperty(exports, '__esModule', { value: true });