UNPKG

@vscode/js-debug-browsers

Version:

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

112 lines 4.12 kB
"use strict"; /*--------------------------------------------------------- * Copyright (C) Microsoft Corporation. All rights reserved. *--------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); exports.DarwinFinderBase = void 0; const path_1 = require("path"); const fs_1 = require("fs"); const util_1 = require("./util"); const pathSuffixRe = /( \(0x[a-f0-9]+\))/; /** * Base class providing utilities for the Darwin browser finders. */ class DarwinFinderBase { constructor(env = process.env, fs = fs_1.promises, execa = execa) { this.env = env; this.fs = fs; this.execa = execa; this.lsRegisterCommand = '/System/Library/Frameworks/CoreServices.framework' + '/Versions/A/Frameworks/LaunchServices.framework' + '/Versions/A/Support/lsregister -dump'; /** * Well-known paths to browsers on Chrome. This is used to make finding fast * in the common case, avoiding sometimes-slow launch services. * @see https://github.com/microsoft/vscode-js-debug/issues/570 */ this.wellKnownPaths = []; } /** * @inheritdoc */ async findWhere(predicate) { for (const test of this.wellKnownPaths) { if (predicate(test) && (await (0, util_1.canAccess)(this.fs, test.path))) { return test; } } return (await this.findAll()).find(predicate); } /** * @inheritdoc */ findAll() { var _a; (_a = this.foundAll) !== null && _a !== void 0 ? _a : (this.foundAll = this.findAllInner()); return this.foundAll; } /** * Finds apps matching the given pattern in the launch service register. */ async findLaunchRegisteredApps(pattern, defaultPaths, suffixes) { const { stdout } = await this.execa.command(`${this.lsRegisterCommand} | awk 'tolower($0) ~ /${pattern.toLowerCase()}${pathSuffixRe.source}?$/ { $1=""; print $0 }'`, { shell: true, stdio: 'pipe' }); const paths = [ ...defaultPaths, ...stdout.split('\n').map((l) => l.trim().replace(pathSuffixRe, '')), ].filter((l) => !!l); const preferred = this.getPreferredPath(); if (preferred) { paths.push(preferred); } const installations = new Set(); for (const inst of paths) { for (const suffix of suffixes) { const execPath = path_1.posix.join(inst.trim(), suffix); try { await this.fs.access(execPath); installations.add(execPath); } catch (e) { // no access => ignored } } } return installations; } /** * Creates priorities for the {@link sort} function that places browsers * in proper order based on their installed location./ */ createPriorities(priorities) { const home = this.env.HOME && (0, util_1.escapeRegexSpecialChars)(this.env.HOME); const preferred = this.getPreferredPath(); const mapped = priorities.reduce((acc, p) => [ ...acc, { regex: new RegExp(`^/Applications/.*${p.name}`), weight: p.weight + 100, quality: p.quality, }, { regex: new RegExp(`^${home}/Applications/.*${p.name}`), weight: p.weight, quality: p.quality, }, { regex: new RegExp(`^/Volumes/.*${p.name}`), weight: p.weight - 100, quality: p.quality, }, ], []); if (preferred) { mapped.unshift({ regex: new RegExp((0, util_1.escapeRegexSpecialChars)(preferred)), weight: 151, quality: "custom" /* Custom */, }); } return mapped; } } exports.DarwinFinderBase = DarwinFinderBase; //# sourceMappingURL=darwinFinderBase.js.map