refactor: remove import cycles in browsers (#10974)

This commit is contained in:
Alex Rudenko 2023-09-21 11:39:02 +02:00 committed by GitHub
parent 7bcdfcb7e9
commit 3238b93a79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 36 deletions

View File

@ -15,10 +15,15 @@
*/ */
import fs from 'fs'; import fs from 'fs';
import os from 'os';
import path from 'path'; import path from 'path';
import {Browser, type BrowserPlatform} from './browser-data/browser-data.js'; import {
import {computeExecutablePath} from './launch.js'; Browser,
type BrowserPlatform,
executablePathByBrowser,
} from './browser-data/browser-data.js';
import {detectBrowserPlatform} from './detectPlatform.js';
/** /**
* @public * @public
@ -27,6 +32,7 @@ export class InstalledBrowser {
browser: Browser; browser: Browser;
buildId: string; buildId: string;
platform: BrowserPlatform; platform: BrowserPlatform;
readonly executablePath: string;
#cache: Cache; #cache: Cache;
@ -43,6 +49,11 @@ export class InstalledBrowser {
this.browser = browser; this.browser = browser;
this.buildId = buildId; this.buildId = buildId;
this.platform = platform; this.platform = platform;
this.executablePath = cache.computeExecutablePath({
browser,
buildId,
platform,
});
} }
/** /**
@ -56,15 +67,27 @@ export class InstalledBrowser {
this.buildId this.buildId
); );
} }
get executablePath(): string {
return computeExecutablePath({
cacheDir: this.#cache.rootDir,
platform: this.platform,
browser: this.browser,
buildId: this.buildId,
});
} }
/**
* @internal
*/
export interface ComputeExecutablePathOptions {
/**
* Determines which platform the browser will be suited for.
*
* @defaultValue **Auto-detected.**
*/
platform?: BrowserPlatform;
/**
* Determines which browser to launch.
*/
browser: Browser;
/**
* Determines which buildId to download. BuildId should uniquely identify
* binaries and they are used for caching.
*/
buildId: string;
} }
/** /**
@ -159,6 +182,27 @@ export class Cache {
}); });
}); });
} }
computeExecutablePath(options: ComputeExecutablePathOptions): string {
options.platform ??= detectBrowserPlatform();
if (!options.platform) {
throw new Error(
`Cannot download a binary for the provided platform: ${os.platform()} (${os.arch()})`
);
}
const installationDir = this.installationDir(
options.browser,
options.platform,
options.buildId
);
return path.join(
installationDir,
executablePathByBrowser[options.browser](
options.platform,
options.buildId
)
);
}
} }
function parseFolderPath( function parseFolderPath(

View File

@ -14,9 +14,6 @@
* limitations under the License. * limitations under the License.
*/ */
import * as chrome from './chrome.js';
import * as firefox from './firefox.js';
/** /**
* Supported browsers. * Supported browsers.
* *
@ -44,12 +41,6 @@ export enum BrowserPlatform {
WIN64 = 'win64', WIN64 = 'win64',
} }
export const downloadUrls = {
[Browser.CHROME]: chrome.resolveDownloadUrl,
[Browser.CHROMIUM]: chrome.resolveDownloadUrl,
[Browser.FIREFOX]: firefox.resolveDownloadUrl,
};
/** /**
* @public * @public
*/ */

View File

@ -17,13 +17,11 @@
import childProcess from 'child_process'; import childProcess from 'child_process';
import {accessSync} from 'fs'; import {accessSync} from 'fs';
import os from 'os'; import os from 'os';
import path from 'path';
import readline from 'readline'; import readline from 'readline';
import { import {
type Browser, type Browser,
type BrowserPlatform, type BrowserPlatform,
executablePathByBrowser,
resolveSystemExecutablePath, resolveSystemExecutablePath,
type ChromeReleaseChannel, type ChromeReleaseChannel,
} from './browser-data/browser-data.js'; } from './browser-data/browser-data.js';
@ -64,21 +62,7 @@ export interface ComputeExecutablePathOptions {
export function computeExecutablePath( export function computeExecutablePath(
options: ComputeExecutablePathOptions options: ComputeExecutablePathOptions
): string { ): string {
options.platform ??= detectBrowserPlatform(); return new Cache(options.cacheDir).computeExecutablePath(options);
if (!options.platform) {
throw new Error(
`Cannot download a binary for the provided platform: ${os.platform()} (${os.arch()})`
);
}
const installationDir = new Cache(options.cacheDir).installationDir(
options.browser,
options.platform,
options.buildId
);
return path.join(
installationDir,
executablePathByBrowser[options.browser](options.platform, options.buildId)
);
} }
/** /**