UNPKG

puppeteer-finder

Version:

Find a executable Chrome / Edge / Firefox in your system

99 lines 4.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const child_process_1 = require("child_process"); const path_1 = require("path"); const os_1 = require("os"); const fs_1 = require("fs"); const utils_1 = require("./utils"); function findChromeExecutablesForLinuxDesktop(folder) { const argumentsRegex = /(^[^ ]+).*/; // Take everything up to the first space const chromeExecRegex = '^Exec=\/.*\/(google|chrome|chromium)-.*'; let installations = []; if ((0, utils_1.canAccess)(folder)) { // Output of the grep & print looks like: // /opt/google/chrome/google-chrome --profile-directory // /home/user/Downloads/chrome-linux/chrome-wrapper %U let execPaths; execPaths = (0, child_process_1.execSync)(`find "${folder}" -type f -exec grep -E "${chromeExecRegex}" "{}" \\; | awk -F '=' '{print $2}'`); execPaths = execPaths .toString() .split(utils_1.newLineRegex) .map((execPath) => execPath.replace(argumentsRegex, '$1')); execPaths.forEach((execPath) => (0, utils_1.canAccess)(execPath) && installations.push(execPath)); } return installations; } /** * Look for linux executables in 2 ways * 1. Look into the directories where .desktop are saved on gnome based distro's * 2. Look for google-chrome-stable & google-chrome executables by using the which command */ function linux() { let installations = []; // 2. Look into the directories where .desktop are saved on gnome based distro's const desktopInstallationFolders = [ (0, path_1.join)((0, os_1.homedir)(), '.local/share/applications/'), '/usr/share/applications/', ]; desktopInstallationFolders.forEach(folder => { installations = installations.concat(findChromeExecutablesForLinuxDesktop(folder)); }); // Look for google-chrome-stable & google-chrome executables by using the which command const executables = [ 'google-chrome-stable', 'google-chrome', 'chromium', 'chromium-browser', 'chromium/chrome', // on toradex machines "chromium" is a directory. seen on Angstrom v2016.12 ]; executables.forEach((executable) => { // see http://tldp.org/LDP/Linux-Filesystem-Hierarchy/html/ const validChromePaths = [ '/usr/bin', '/usr/local/bin', '/usr/sbin', '/usr/local/sbin', '/opt/bin', '/usr/bin/X11', '/usr/X11R6/bin' ].map((possiblePath) => { try { const chromePathToTest = possiblePath + '/' + executable; if ((0, fs_1.existsSync)(chromePathToTest) && (0, utils_1.canAccess)(chromePathToTest) && (0, utils_1.isExecutable)(chromePathToTest)) { installations.push(chromePathToTest); return chromePathToTest; } } catch (err) { // not installed on this path or inaccessible } return undefined; }).filter((foundChromePath) => foundChromePath); // skip asking "which" command if the binary was found by searching the known paths. if (validChromePaths && validChromePaths.length > 0) { return; } try { const chromePath = (0, child_process_1.execFileSync)('which', [executable], { stdio: [null, 'pipe', null] }).toString().split(utils_1.newLineRegex)[0]; if ((0, utils_1.canAccess)(chromePath)) { installations.push(chromePath); } } catch (err) { // cmd which not installed. } }); const priorities = [ { regex: /chromium$/, weight: 52 }, { regex: /chrome-wrapper$/, weight: 51 }, { regex: /google-chrome-stable$/, weight: 50 }, { regex: /google-chrome$/, weight: 49 }, { regex: /chromium-browser$/, weight: 48 }, { regex: /chrome$/, weight: 47 }, ]; return (0, utils_1.sort)(Array.from(new Set(installations.filter(Boolean))), priorities); } exports.default = linux; //# sourceMappingURL=linux.js.map