edge-location
Version:
Approximates the current location of the Edge browser across platforms.
274 lines (273 loc) • 12.2 kB
JavaScript
import * as __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__ from "node:fs";
import * as __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__ from "node:path";
import * as __WEBPACK_EXTERNAL_MODULE_node_child_process_27f17141__ from "node:child_process";
import * as __WEBPACK_EXTERNAL_MODULE_node_os_74b4b876__ from "node:os";
import * as __WEBPACK_EXTERNAL_MODULE_which__ from "which";
function scanOsxPath(allowFallback = false, deps) {
const f = (null == deps ? void 0 : deps.fs) ?? __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"];
const uh = (null == deps ? void 0 : deps.userhome) ?? ((p)=>__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(__WEBPACK_EXTERNAL_MODULE_node_os_74b4b876__["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) ?? __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__;
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 = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join(prefix, suffix);
if (f.existsSync(exe)) return exe;
}
return null;
}
function scanUnknownPlatform(allowFallback = false, deps) {
const w = (null == deps ? void 0 : deps.which) ?? __WEBPACK_EXTERNAL_MODULE_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) ?? __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', 'ms-playwright');
const dirs = listDirs(f, base).filter((d)=>/^msedge(-|$)/i.test(d));
const candidates = [];
for (const d of dirs){
candidates.push(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(base, d, 'Microsoft Edge.app', 'Contents', 'MacOS', 'Microsoft Edge'));
candidates.push(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(base, d, 'msedge.app', 'Contents', 'MacOS', 'msedge'));
candidates.push(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(base, d, 'msedge-mac', 'Microsoft Edge.app', 'Contents', 'MacOS', 'Microsoft Edge'));
candidates.push(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["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 = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["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(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(base, d, 'msedge.exe'));
candidates.push(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(base, d, 'msedge', 'msedge.exe'));
candidates.push(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(base, d, 'msedge-win64', 'msedge.exe'));
candidates.push(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["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 ? __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, 'ms-playwright');
const dirs = listDirs(f, base).filter((d)=>/^msedge(-|$)/i.test(d));
const candidates = [];
for (const d of dirs){
candidates.push(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(base, d, 'msedge'));
candidates.push(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(base, d, 'msedge', 'msedge'));
candidates.push(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(base, d, 'msedge-linux', 'msedge'));
candidates.push(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["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) ?? __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["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, __WEBPACK_EXTERNAL_MODULE_node_child_process_27f17141__.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 = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].dirname(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].dirname(bin));
const infoPlist = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(contentsDir, 'Info.plist');
if (__WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].existsSync(infoPlist)) {
const xml = __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["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_node_child_process_27f17141__.execFileSync)(bin, args, {
encoding: 'utf8',
stdio: [
'ignore',
'pipe',
'ignore'
]
}).trim();
} catch {
return null;
}
}
export { locateEdge as default, getEdgeVersion, getInstallGuidance, locateEdgeOrExplain };