From 3238b93a79b9e6891c00a32ca009057fb7425a68 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Thu, 21 Sep 2023 11:39:02 +0200 Subject: [PATCH] refactor: remove import cycles in browsers (#10974) --- packages/browsers/src/Cache.ts | 64 +++++++++++++++++---- packages/browsers/src/browser-data/types.ts | 9 --- packages/browsers/src/launch.ts | 18 +----- 3 files changed, 55 insertions(+), 36 deletions(-) diff --git a/packages/browsers/src/Cache.ts b/packages/browsers/src/Cache.ts index ac77d6ad..babb4b9c 100644 --- a/packages/browsers/src/Cache.ts +++ b/packages/browsers/src/Cache.ts @@ -15,10 +15,15 @@ */ import fs from 'fs'; +import os from 'os'; import path from 'path'; -import {Browser, type BrowserPlatform} from './browser-data/browser-data.js'; -import {computeExecutablePath} from './launch.js'; +import { + Browser, + type BrowserPlatform, + executablePathByBrowser, +} from './browser-data/browser-data.js'; +import {detectBrowserPlatform} from './detectPlatform.js'; /** * @public @@ -27,6 +32,7 @@ export class InstalledBrowser { browser: Browser; buildId: string; platform: BrowserPlatform; + readonly executablePath: string; #cache: Cache; @@ -43,6 +49,11 @@ export class InstalledBrowser { this.browser = browser; this.buildId = buildId; this.platform = platform; + this.executablePath = cache.computeExecutablePath({ + browser, + buildId, + platform, + }); } /** @@ -56,15 +67,27 @@ export class InstalledBrowser { 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( diff --git a/packages/browsers/src/browser-data/types.ts b/packages/browsers/src/browser-data/types.ts index 2f818e09..b7bf7fd7 100644 --- a/packages/browsers/src/browser-data/types.ts +++ b/packages/browsers/src/browser-data/types.ts @@ -14,9 +14,6 @@ * limitations under the License. */ -import * as chrome from './chrome.js'; -import * as firefox from './firefox.js'; - /** * Supported browsers. * @@ -44,12 +41,6 @@ export enum BrowserPlatform { WIN64 = 'win64', } -export const downloadUrls = { - [Browser.CHROME]: chrome.resolveDownloadUrl, - [Browser.CHROMIUM]: chrome.resolveDownloadUrl, - [Browser.FIREFOX]: firefox.resolveDownloadUrl, -}; - /** * @public */ diff --git a/packages/browsers/src/launch.ts b/packages/browsers/src/launch.ts index 696af288..f4ed06d7 100644 --- a/packages/browsers/src/launch.ts +++ b/packages/browsers/src/launch.ts @@ -17,13 +17,11 @@ import childProcess from 'child_process'; import {accessSync} from 'fs'; import os from 'os'; -import path from 'path'; import readline from 'readline'; import { type Browser, type BrowserPlatform, - executablePathByBrowser, resolveSystemExecutablePath, type ChromeReleaseChannel, } from './browser-data/browser-data.js'; @@ -64,21 +62,7 @@ export interface ComputeExecutablePathOptions { export function 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 = new Cache(options.cacheDir).installationDir( - options.browser, - options.platform, - options.buildId - ); - return path.join( - installationDir, - executablePathByBrowser[options.browser](options.platform, options.buildId) - ); + return new Cache(options.cacheDir).computeExecutablePath(options); } /**