From 8219658fcac4ccecdce031ecb298539aa9212e03 Mon Sep 17 00:00:00 2001 From: Mitchell Skaggs Date: Sat, 31 Oct 2020 05:47:22 -0500 Subject: [PATCH 1/3] Search in Path for chrome.exe Allows finding Scoop installations, among others. --- src/chrome-finder.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/chrome-finder.ts b/src/chrome-finder.ts index 7f0862835..c625c34e8 100644 --- a/src/chrome-finder.ts +++ b/src/chrome-finder.ts @@ -197,12 +197,22 @@ export function win32() { installations.push(customChromePath); } + // Search common locations for chrome.exe prefixes.forEach(prefix => suffixes.forEach(suffix => { const chromePath = path.join(prefix, suffix); if (canAccess(chromePath)) { installations.push(chromePath); } })); + + // Search %PATH% for chrome.exe + process.env.Path?.split(';')?.forEach(prefix => { + const chromePath = path.join(prefix, 'chrome.exe') + if (canAccess(chromePath)) { + installations.push(chromePath) + } + }); + return installations; } From 5b9a17e52963fa9a3ead98750471235651420fde Mon Sep 17 00:00:00 2001 From: Mitchell Skaggs Date: Mon, 14 Mar 2022 21:31:50 -0500 Subject: [PATCH 2/3] Incorporate `PATH` search into prefix and suffix list --- src/chrome-finder.ts | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/chrome-finder.ts b/src/chrome-finder.ts index acfa8cff8..e92ebbbb0 100644 --- a/src/chrome-finder.ts +++ b/src/chrome-finder.ts @@ -187,10 +187,11 @@ export function win32() { const installations: Array = []; const suffixes = [ `${path.sep}Google${path.sep}Chrome SxS${path.sep}Application${path.sep}chrome.exe`, - `${path.sep}Google${path.sep}Chrome${path.sep}Application${path.sep}chrome.exe` + `${path.sep}Google${path.sep}Chrome${path.sep}Application${path.sep}chrome.exe`, + `${path.sep}chrome.exe` ]; const prefixes = [ - process.env.LOCALAPPDATA, process.env.PROGRAMFILES, process.env['PROGRAMFILES(X86)'] + process.env.LOCALAPPDATA, process.env.PROGRAMFILES, process.env['PROGRAMFILES(X86)'], ...process.env.PATH?.split(path.delimiter) ?? [] ].filter(Boolean) as string[]; const customChromePath = resolveChromePath(); @@ -206,14 +207,6 @@ export function win32() { } })); - // Search %PATH% for chrome.exe - process.env.Path?.split(';')?.forEach(prefix => { - const chromePath = path.join(prefix, 'chrome.exe') - if (canAccess(chromePath)) { - installations.push(chromePath) - } - }); - return installations; } From 0324b89fa921ca66c441ab43137c959378bae5b1 Mon Sep 17 00:00:00 2001 From: Mitchell Skaggs Date: Tue, 15 Mar 2022 16:01:59 -0500 Subject: [PATCH 3/3] Use `which -a` on Linux and `where` on Windows to find more binaries --- src/chrome-finder.ts | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/chrome-finder.ts b/src/chrome-finder.ts index e92ebbbb0..d94183d74 100644 --- a/src/chrome-finder.ts +++ b/src/chrome-finder.ts @@ -7,12 +7,12 @@ import fs = require('fs'); import path = require('path'); -import {homedir} from 'os'; -import {execSync, execFileSync} from 'child_process'; import escapeRegExp = require('escape-string-regexp'); -const log = require('lighthouse-logger'); +import {homedir} from 'os'; +import {execFileSync, execSync} from 'child_process'; +import {ChromePathNotSetError, getWSLLocalAppDataPath, toWSLPath} from './utils'; -import {getWSLLocalAppDataPath, toWSLPath, ChromePathNotSetError} from './utils'; +const log = require('lighthouse-logger'); const newLineRegex = /\r?\n/; @@ -136,18 +136,15 @@ export function linux() { 'chromium-browser', 'chromium', ]; - executables.forEach((executable: string) => { - try { - const chromePath = - execFileSync('which', [executable], {stdio: 'pipe'}).toString().split(newLineRegex)[0]; - - if (canAccess(chromePath)) { - installations.push(chromePath); - } - } catch (e) { - // Not installed. - } - }); + try { + execFileSync('which', ['-a', ...executables], {stdio: 'pipe'}) + .toString() + .split(newLineRegex) + .filter(canAccess) + .forEach((chromePath: string) => installations.push(chromePath)); + } catch (e) { + // Not installed. + } if (!installations.length) { throw new ChromePathNotSetError(); @@ -191,7 +188,8 @@ export function win32() { `${path.sep}chrome.exe` ]; const prefixes = [ - process.env.LOCALAPPDATA, process.env.PROGRAMFILES, process.env['PROGRAMFILES(X86)'], ...process.env.PATH?.split(path.delimiter) ?? [] + process.env.LOCALAPPDATA, process.env.PROGRAMFILES, process.env['PROGRAMFILES(X86)'], + ...process.env.PATH?.split(path.delimiter) ?? [] ].filter(Boolean) as string[]; const customChromePath = resolveChromePath(); @@ -207,6 +205,16 @@ export function win32() { } })); + try { + execFileSync('where.exe', ['chrome.exe'], {stdio: 'pipe'}) + .toString() + .split(newLineRegex) + .filter((path: string) => !path.startsWith('INFO: ') && canAccess(path)) + .forEach((chromePath: string) => installations.push(chromePath)); + } catch (e) { + // Not installed. + } + return installations; }