From 9afdc6300b80f01091dc4cb42d4ebe952c7d60f0 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Mon, 7 Feb 2022 12:59:44 +0100 Subject: [PATCH] fix: make projectRoot optional in Puppeteer and launchers (#7967) --- src/initialize-node.ts | 4 ---- src/node/Launcher.ts | 20 +++++++++++++++----- src/node/Puppeteer.ts | 9 +++++++-- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/initialize-node.ts b/src/initialize-node.ts index de1c7fc43a162..e5336654034d4 100644 --- a/src/initialize-node.ts +++ b/src/initialize-node.ts @@ -34,10 +34,6 @@ export const initializePuppeteerNode = (packageName: string): PuppeteerNode => { if (!isPuppeteerCore && productName === 'firefox') preferredRevision = PUPPETEER_REVISIONS.firefox; - if (!puppeteerRootDirectory) { - throw new Error('puppeteerRootDirectory is not found.'); - } - return new PuppeteerNode({ projectRoot: puppeteerRootDirectory, preferredRevision, diff --git a/src/node/Launcher.ts b/src/node/Launcher.ts index 2f76dbf6bb1de..4c87f0dbd43e9 100644 --- a/src/node/Launcher.ts +++ b/src/node/Launcher.ts @@ -52,12 +52,12 @@ export interface ProductLauncher { * @internal */ class ChromeLauncher implements ProductLauncher { - _projectRoot: string; + _projectRoot: string | undefined; _preferredRevision: string; _isPuppeteerCore: boolean; constructor( - projectRoot: string, + projectRoot: string | undefined, preferredRevision: string, isPuppeteerCore: boolean ) { @@ -276,12 +276,12 @@ class ChromeLauncher implements ProductLauncher { * @internal */ class FirefoxLauncher implements ProductLauncher { - _projectRoot: string; + _projectRoot: string | undefined; _preferredRevision: string; _isPuppeteerCore: boolean; constructor( - projectRoot: string, + projectRoot: string | undefined, preferredRevision: string, isPuppeteerCore: boolean ) { @@ -428,6 +428,11 @@ class FirefoxLauncher implements ProductLauncher { async _updateRevision(): Promise { // replace 'latest' placeholder with actual downloaded revision if (this._preferredRevision === 'latest') { + if (!this._projectRoot) { + throw new Error( + '_projectRoot is undefined. Unable to create a BrowserFetcher.' + ); + } const browserFetcher = new BrowserFetcher(this._projectRoot, { product: this.product, }); @@ -813,6 +818,11 @@ function resolveExecutablePath(launcher: ChromeLauncher | FirefoxLauncher): { process.env.npm_config_puppeteer_download_path || process.env.npm_package_config_puppeteer_download_path; } + if (!launcher._projectRoot) { + throw new Error( + '_projectRoot is undefined. Unable to create a BrowserFetcher.' + ); + } const browserFetcher = new BrowserFetcher(launcher._projectRoot, { product: launcher.product, path: downloadPath, @@ -845,7 +855,7 @@ function resolveExecutablePath(launcher: ChromeLauncher | FirefoxLauncher): { * @internal */ export default function Launcher( - projectRoot: string, + projectRoot: string | undefined, preferredRevision: string, isPuppeteerCore: boolean, product?: string diff --git a/src/node/Puppeteer.ts b/src/node/Puppeteer.ts index 38799f20967b2..13aa85ee8bc8d 100644 --- a/src/node/Puppeteer.ts +++ b/src/node/Puppeteer.ts @@ -67,7 +67,7 @@ import { Product } from '../common/Product.js'; */ export class PuppeteerNode extends Puppeteer { private _lazyLauncher?: ProductLauncher; - private _projectRoot: string; + private _projectRoot?: string; private __productName?: Product; /** * @internal @@ -79,7 +79,7 @@ export class PuppeteerNode extends Puppeteer { */ constructor( settings: { - projectRoot: string; + projectRoot?: string; preferredRevision: string; productName?: Product; } & CommonPuppeteerSettings @@ -224,6 +224,11 @@ export class PuppeteerNode extends Puppeteer { * @returns A new BrowserFetcher instance. */ createBrowserFetcher(options: BrowserFetcherOptions): BrowserFetcher { + if (!this._projectRoot) { + throw new Error( + '_projectRoot is undefined. Unable to create a BrowserFetcher.' + ); + } return new BrowserFetcher(this._projectRoot, options); } }