UNPKG

edge-location

Version:

Approximates the current location of the Edge browser across platforms.

330 lines (329 loc) 13.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: ()=>locateEdge, getEdgeVersion: ()=>getEdgeVersion, locateEdgeOrExplain: ()=>locateEdgeOrExplain, getInstallGuidance: ()=>getInstallGuidance }); 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); const external_node_child_process_namespaceObject = require("node:child_process"); const external_node_os_namespaceObject = require("node:os"); var external_node_os_default = /*#__PURE__*/ __webpack_require__.n(external_node_os_namespaceObject); function scanOsxPath(allowFallback = false, deps) { const f = (null == deps ? void 0 : deps.fs) ?? external_node_fs_default(); const uh = (null == deps ? void 0 : deps.userhome) ?? ((p)=>external_node_path_default().join(external_node_os_default().homedir(), String(p))); const apps = [ { app: 'Microsoft Edge.app', exec: 'Microsoft Edge' }, { app: 'Microsoft Edge Beta.app', exec: 'Microsoft Edge Beta' }, { app: 'Microsoft Edge Dev.app', exec: 'Microsoft Edge Dev' }, { app: 'Microsoft Edge Canary.app', exec: 'Microsoft Edge Canary' } ]; const systemBase = '/Applications'; const userBase = uh('Applications'); const channels = allowFallback ? apps : [ apps[0] ]; for (const { app, exec } of channels){ 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; } return null; } function scanWindowsPath(allowFallback = false, deps) { const f = (null == deps ? void 0 : deps.fs) ?? external_node_fs_namespaceObject; const e = (null == deps ? void 0 : deps.env) ?? process.env; const prefixes = [ e.LOCALAPPDATA, e.PROGRAMFILES, e['PROGRAMFILES(X86)'] ].filter(Boolean); const suffixesAll = [ '\\Microsoft\\Edge\\Application\\msedge.exe', '\\Microsoft\\Edge Beta\\Application\\msedge.exe', '\\Microsoft\\Edge Dev\\Application\\msedge.exe', '\\Microsoft\\Edge SxS\\Application\\msedge.exe' ]; const suffixes = allowFallback ? suffixesAll : [ suffixesAll[0] ]; for (const prefix of prefixes)for (const suffix of suffixes){ const exe = external_node_path_namespaceObject.join(prefix, suffix); if (f.existsSync(exe)) return exe; } return null; } const external_which_namespaceObject = require("which"); var external_which_default = /*#__PURE__*/ __webpack_require__.n(external_which_namespaceObject); function scanUnknownPlatform(allowFallback = false, deps) { const w = (null == deps ? void 0 : deps.which) ?? external_which_default(); const candidatesAll = [ 'microsoft-edge', 'microsoft-edge-beta', 'microsoft-edge-dev', 'microsoft-edge-canary' ]; const candidates = allowFallback ? candidatesAll : [ candidatesAll[0] ]; for (const cmd of candidates)try { const resolved = w.sync(cmd); if (resolved) return resolved; } catch (_) {} return null; } function resolveFromPlaywrightCache(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', 'ms-playwright'); const dirs = listDirs(f, base).filter((d)=>/^msedge(-|$)/i.test(d)); const candidates = []; for (const d of dirs){ candidates.push(external_node_path_default().join(base, d, 'Microsoft Edge.app', 'Contents', 'MacOS', 'Microsoft Edge')); candidates.push(external_node_path_default().join(base, d, 'msedge.app', 'Contents', 'MacOS', 'msedge')); candidates.push(external_node_path_default().join(base, d, 'msedge-mac', 'Microsoft Edge.app', 'Contents', 'MacOS', 'Microsoft Edge')); candidates.push(external_node_path_default().join(base, d, 'msedge-mac-arm64', 'Microsoft Edge.app', 'Contents', 'MacOS', 'Microsoft Edge')); } 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, 'ms-playwright'); const dirs = listDirs(f, base).filter((d)=>/^msedge(-|$)/i.test(d)); const candidates = []; for (const d of dirs){ candidates.push(external_node_path_default().join(base, d, 'msedge.exe')); candidates.push(external_node_path_default().join(base, d, 'msedge', 'msedge.exe')); candidates.push(external_node_path_default().join(base, d, 'msedge-win64', 'msedge.exe')); candidates.push(external_node_path_default().join(base, d, 'msedge-win32', 'msedge.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, 'ms-playwright'); const dirs = listDirs(f, base).filter((d)=>/^msedge(-|$)/i.test(d)); const candidates = []; for (const d of dirs){ candidates.push(external_node_path_default().join(base, d, 'msedge')); candidates.push(external_node_path_default().join(base, d, 'msedge', 'msedge')); candidates.push(external_node_path_default().join(base, d, 'msedge-linux', 'msedge')); candidates.push(external_node_path_default().join(base, d, 'msedge-linux-64', 'msedge')); } 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 locateEdge(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_node_fs_default(); const e = (null == deps ? void 0 : deps.env) ?? process.env; const platform = (null == deps ? void 0 : deps.platform) ?? process.platform; const envPath = null == e ? void 0 : e.EDGE_BINARY; if (envPath && f.existsSync(envPath)) return envPath; let found = null; switch(platform){ case 'darwin': found = scanOsxPath(allowFallback, { fs: f, userhome: null == deps ? void 0 : deps.userhome }); break; case 'win32': found = scanWindowsPath(allowFallback, { fs: f, env: e }); break; default: found = scanUnknownPlatform(allowFallback, { which: null == deps ? void 0 : deps.which }); break; } if (!found) found = resolveFromPlaywrightCache({ fs: f, env: e, platform }); return found; } function getInstallGuidance() { return "We couldn't find a Microsoft Edge browser on this machine.\n\nHere's the fastest way to get set up:\n\n1) Install Edge via Playwright (recommended for CI/dev)\n npx playwright install msedge\n\nThen re-run your command — we'll detect it automatically.\n\nAlternatively, install Microsoft Edge from the official site and re-run."; } function locateEdgeOrExplain(options) { const allowFallback = 'boolean' == typeof options ? options : Boolean(null == options ? void 0 : options.allowFallback); const found = locateEdge(allowFallback) || locateEdge(true); if ('string' == typeof found && found) return found; throw new Error(getInstallGuidance()); } function getEdgeVersion(bin, opts) { if ('win32' === process.platform) { try { const psPath = bin.replace(/'/g, "''"); const pv = (0, external_node_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, [ '--product-version' ]) || tryExec(bin, [ '--version' ]); return normalizeVersion(v); } return null; } if ('darwin' === process.platform) { try { const contentsDir = external_node_path_default().dirname(external_node_path_default().dirname(bin)); const infoPlist = external_node_path_default().join(contentsDir, 'Info.plist'); if (external_node_fs_default().existsSync(infoPlist)) { const xml = external_node_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_node_child_process_namespaceObject.execFileSync)(bin, args, { encoding: 'utf8', stdio: [ 'ignore', 'pipe', 'ignore' ] }).trim(); } catch { return null; } } exports["default"] = __webpack_exports__["default"]; exports.getEdgeVersion = __webpack_exports__.getEdgeVersion; exports.getInstallGuidance = __webpack_exports__.getInstallGuidance; exports.locateEdgeOrExplain = __webpack_exports__.locateEdgeOrExplain; for(var __webpack_i__ in __webpack_exports__)if (-1 === [ "default", "getEdgeVersion", "getInstallGuidance", "locateEdgeOrExplain" ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__]; Object.defineProperty(exports, '__esModule', { value: true });