UNPKG

@vscode/js-debug-browsers

Version:

Browser launch and discovery logic used in VS Code's JavaScript Debugger

122 lines 5.45 kB
"use strict"; /*--------------------------------------------------------- * Copyright (C) Microsoft Corporation. All rights reserved. *--------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); exports.LinuxChromeBrowserFinder = void 0; const path_1 = require("path"); const util_1 = require("./util"); const child_process_1 = require("child_process"); const os_1 = require("os"); const fs_1 = require("fs"); const newLineRegex = /\r?\n/; /** * Finds the Chrome browser on Windows. */ class LinuxChromeBrowserFinder { constructor(env = process.env, fs = fs_1.promises) { this.env = env; this.fs = fs; this.pathEnvironmentVar = 'CHROME_PATH'; this.priorities = [ { regex: /chrome-wrapper$/, weight: 54, quality: "custom" /* Custom */ }, { regex: /google-chrome-dev$/, weight: 53, quality: "dev" /* Dev */ }, { regex: /google-chrome-canary$/, weight: 52, quality: "canary" /* Canary */ }, { regex: /google-chrome-unstable$/, weight: 51, quality: "canary" /* Canary */ }, { regex: /google-chrome-canary$/, weight: 51, quality: "canary" /* Canary */ }, { regex: /google-chrome-stable$/, weight: 50, quality: "stable" /* Stable */ }, { regex: /(google-)?chrome$/, weight: 49, quality: "stable" /* Stable */ }, { regex: /chromium-browser$/, weight: 48, quality: "custom" /* Custom */ }, { regex: /chromium$/, weight: 47, quality: "custom" /* Custom */ }, ]; this.executablesOnPath = [ 'google-chrome-unstable', 'google-chrome-dev', 'google-chrome-beta', 'google-chrome-canary', 'google-chrome-stable', 'google-chrome', 'chromium-browser', 'chromium', ]; } async findWhere(predicate) { return (await this.findAll()).find(predicate); } async findAll() { const installations = new Set(); // 1. Look into CHROME_PATH env variable const envPath = this.env[this.pathEnvironmentVar]; const customChromePath = envPath && (await (0, util_1.canAccess)(this.fs, envPath)); if (customChromePath) { installations.add(envPath); } // 2. Look into the directories where .desktop are saved on gnome based distro's const desktopInstallationFolders = [ path_1.posix.join((0, os_1.homedir)(), '.local/share/applications/'), '/usr/share/applications/', '/usr/bin', '/opt/google', // Chromebook location, vscode #198192 ]; desktopInstallationFolders.forEach((folder) => { for (const bin in this.findChromeExecutables(folder)) { installations.add(bin); } }); // 3. Look for google-chrome & chromium executables by using the which command await Promise.all(this.executablesOnPath.map(async (executable) => { try { const chromePath = (0, child_process_1.execFileSync)('which', [executable], { stdio: 'pipe' }) .toString() .split(newLineRegex)[0]; if (await (0, util_1.canAccess)(this.fs, chromePath)) { installations.add(chromePath); } } catch (e) { // Not installed. } })); const priorities = envPath ? [ { regex: new RegExp((0, util_1.escapeRegexSpecialChars)(envPath)), weight: 101, quality: "custom" /* Custom */, }, ].concat(this.priorities) : this.priorities; return (0, util_1.sort)(installations, priorities); } async findChromeExecutables(folder) { const argumentsRegex = /(^[^ ]+).*/; // Take everything up to the first space const chromeExecRegex = `^Exec=/.*/(${this.executablesOnPath.join('|')})-.*`; const installations = []; if (await (0, util_1.canAccess)(this.fs, folder)) { // Output of the grep & print looks like: // /opt/google/chrome/google-chrome --profile-directory // /home/user/Downloads/chrome-linux/chrome-wrapper %U // Some systems do not support grep -R so fallback to -r. // See https://github.com/GoogleChrome/chrome-launcher/issues/46 for more context. let execResult; try { execResult = (0, child_process_1.execSync)(`grep -ERI "${chromeExecRegex}" ${folder} | awk -F '=' '{print $2}'`); } catch (e) { execResult = (0, child_process_1.execSync)(`grep -Er "${chromeExecRegex}" ${folder} | awk -F '=' '{print $2}'`); } const execPaths = execResult .toString() .split(newLineRegex) .map((execPath) => execPath.replace(argumentsRegex, '$1')); await Promise.all(execPaths.map(async (execPath) => { if (await (0, util_1.canAccess)(this.fs, execPath)) { installations.push(execPath); } })); } return installations; } } exports.LinuxChromeBrowserFinder = LinuxChromeBrowserFinder; //# sourceMappingURL=linuxChrome.js.map