Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search in Path for chrome.exe #218

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions src/chrome-finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -187,23 +184,37 @@ export function win32() {
const installations: Array<string> = [];
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();
if (customChromePath) {
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);
}
}));

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;
}

Expand Down