feat: add ability to uninstall a browser (#10179)

This commit is contained in:
Alex Rudenko 2023-05-15 11:51:42 +02:00 committed by GitHub
parent 2ba96c6557
commit d388a6edfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 259 additions and 20 deletions

View File

@ -0,0 +1,25 @@
---
sidebar_label: getInstalledBrowsers
---
# getInstalledBrowsers() function
Returns metadata about browsers installed in the cache directory.
#### Signature:
```typescript
export declare function getInstalledBrowsers(
options: GetInstalledBrowsersOptions
): Promise<InstalledBrowser[]>;
```
## Parameters
| Parameter | Type | Description |
| --------- | ------------------------------------------------------------------------ | ----------- |
| options | [GetInstalledBrowsersOptions](./browsers.getinstalledbrowsersoptions.md) | |
**Returns:**
Promise&lt;[InstalledBrowser](./browsers.installedbrowser.md)\[\]&gt;

View File

@ -0,0 +1,17 @@
---
sidebar_label: GetInstalledBrowsersOptions
---
# GetInstalledBrowsersOptions interface
#### Signature:
```typescript
export interface GetInstalledBrowsersOptions
```
## Properties
| Property | Modifiers | Type | Description | Default |
| -------- | --------- | ------ | -------------------------------------------- | ------- |
| cacheDir | | string | The path to the root of the cache directory. | |

View File

@ -16,7 +16,7 @@ export interface InstallOptions
| ------------------------ | --------------------- | -------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ------------------------ | --------------------- | -------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| baseUrl | <code>optional</code> | string | Determines the host that will be used for downloading. | <p>Either</p><p>- https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing or - https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central</p> | | baseUrl | <code>optional</code> | string | Determines the host that will be used for downloading. | <p>Either</p><p>- https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing or - https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central</p> |
| browser | | [Browser](./browsers.browser.md) | Determines which browser to install. | | | browser | | [Browser](./browsers.browser.md) | Determines which browser to install. | |
| buildId | | string | Determines which buildId to dowloand. BuildId should uniquely identify binaries and they are used for caching. | | | buildId | | string | Determines which buildId to download. BuildId should uniquely identify binaries and they are used for caching. | |
| cacheDir | | string | Determines the path to download browsers to. | | | cacheDir | | string | Determines the path to download browsers to. | |
| downloadProgressCallback | <code>optional</code> | (downloadedBytes: number, totalBytes: number) =&gt; void | Provides information about the progress of the download. | | | downloadProgressCallback | <code>optional</code> | (downloadedBytes: number, totalBytes: number) =&gt; void | Provides information about the progress of the download. | |
| platform | <code>optional</code> | [BrowserPlatform](./browsers.browserplatform.md) | Determines which platform the browser will be suited for. | **Auto-detected.** | | platform | <code>optional</code> | [BrowserPlatform](./browsers.browserplatform.md) | Determines which platform the browser will be suited for. | **Auto-detected.** |

View File

@ -0,0 +1,21 @@
---
sidebar_label: uninstall
---
# uninstall() function
#### Signature:
```typescript
export declare function uninstall(options: UninstallOptions): Promise<void>;
```
## Parameters
| Parameter | Type | Description |
| --------- | -------------------------------------------------- | ----------- |
| options | [UninstallOptions](./browsers.uninstalloptions.md) | |
**Returns:**
Promise&lt;void&gt;

View File

@ -0,0 +1,20 @@
---
sidebar_label: UninstallOptions
---
# UninstallOptions interface
#### Signature:
```typescript
export interface UninstallOptions
```
## Properties
| Property | Modifiers | Type | Description | Default |
| -------- | --------------------- | ------------------------------------------------ | ----------------------------------------------- | ------------------ |
| browser | | [Browser](./browsers.browser.md) | Determines which browser to uninstall. | |
| buildId | | string | The browser build to uninstall | |
| cacheDir | | string | The path to the root of the cache directory. | |
| platform | <code>optional</code> | [BrowserPlatform](./browsers.browserplatform.md) | Determines the platform for the browser binary. | **Auto-detected.** |

View File

@ -49,26 +49,30 @@ The programmatic API allows installing and launching browsers from your code. Se
## Functions ## Functions
| Function | Description | | Function | Description |
| --------------------------------------------------------------------------------- | ----------- | | --------------------------------------------------------------------------------- | ----------------------------------------------------------------- |
| [canDownload(options)](./browsers.candownload.md) | | | [canDownload(options)](./browsers.candownload.md) | |
| [computeExecutablePath(options)](./browsers.computeexecutablepath.md) | | | [computeExecutablePath(options)](./browsers.computeexecutablepath.md) | |
| [computeSystemExecutablePath(options)](./browsers.computesystemexecutablepath.md) | | | [computeSystemExecutablePath(options)](./browsers.computesystemexecutablepath.md) | |
| [createProfile(browser, opts)](./browsers.createprofile.md) | | | [createProfile(browser, opts)](./browsers.createprofile.md) | |
| [detectBrowserPlatform()](./browsers.detectbrowserplatform.md) | | | [detectBrowserPlatform()](./browsers.detectbrowserplatform.md) | |
| [install(options)](./browsers.install.md) | | | [getInstalledBrowsers(options)](./browsers.getinstalledbrowsers.md) | Returns metadata about browsers installed in the cache directory. |
| [launch(opts)](./browsers.launch.md) | | | [install(options)](./browsers.install.md) | |
| [makeProgressCallback(browser, buildId)](./browsers.makeprogresscallback.md) | | | [launch(opts)](./browsers.launch.md) | |
| [resolveBuildId(browser, platform, tag)](./browsers.resolvebuildid.md) | | | [makeProgressCallback(browser, buildId)](./browsers.makeprogresscallback.md) | |
| [resolveBuildId(browser, platform, tag)](./browsers.resolvebuildid.md) | |
| [uninstall(options)](./browsers.uninstall.md) | |
## Interfaces ## Interfaces
| Interface | Description | | Interface | Description |
| ---------------------------------------------- | ----------- | | ------------------------------------------------------------------------ | ----------- |
| [InstallOptions](./browsers.installoptions.md) | | | [GetInstalledBrowsersOptions](./browsers.getinstalledbrowsersoptions.md) | |
| [Options](./browsers.options.md) | | | [InstallOptions](./browsers.installoptions.md) | |
| [ProfileOptions](./browsers.profileoptions.md) | | | [Options](./browsers.options.md) | |
| [SystemOptions](./browsers.systemoptions.md) | | | [ProfileOptions](./browsers.profileoptions.md) | |
| [SystemOptions](./browsers.systemoptions.md) | |
| [UninstallOptions](./browsers.uninstalloptions.md) | |
## Variables ## Variables

View File

@ -71,6 +71,19 @@ export class Cache {
}); });
} }
uninstall(
browser: Browser,
platform: BrowserPlatform,
buildId: string
): void {
fs.rmSync(this.installationDir(browser, platform, buildId), {
force: true,
recursive: true,
maxRetries: 10,
retryDelay: 500,
});
}
getInstalledBrowsers(): InstalledBrowser[] { getInstalledBrowsers(): InstalledBrowser[] {
if (!fs.existsSync(this.#rootDir)) { if (!fs.existsSync(this.#rootDir)) {
return []; return [];

View File

@ -68,7 +68,7 @@ export interface InstallOptions {
*/ */
browser: Browser; browser: Browser;
/** /**
* Determines which buildId to dowloand. BuildId should uniquely identify * Determines which buildId to download. BuildId should uniquely identify
* binaries and they are used for caching. * binaries and they are used for caching.
*/ */
buildId: string; buildId: string;
@ -188,6 +188,64 @@ export async function install(
}; };
} }
export interface UninstallOptions {
/**
* Determines the platform for the browser binary.
*
* @defaultValue **Auto-detected.**
*/
platform?: BrowserPlatform;
/**
* The path to the root of the cache directory.
*/
cacheDir: string;
/**
* Determines which browser to uninstall.
*/
browser: Browser;
/**
* The browser build to uninstall
*/
buildId: string;
}
/**
*
* @public
*/
export async function uninstall(options: UninstallOptions): Promise<void> {
options.platform ??= detectBrowserPlatform();
if (!options.platform) {
throw new Error(
`Cannot detect the browser platform for: ${os.platform()} (${os.arch()})`
);
}
new Cache(options.cacheDir).uninstall(
options.browser,
options.platform,
options.buildId
);
}
export interface GetInstalledBrowsersOptions {
/**
* The path to the root of the cache directory.
*/
cacheDir: string;
}
/**
* Returns metadata about browsers installed in the cache directory.
*
* @public
*/
export async function getInstalledBrowsers(
options: GetInstalledBrowsersOptions
): Promise<InstalledBrowser[]> {
return new Cache(options.cacheDir).getInstalledBrowsers();
}
/** /**
* @public * @public
*/ */

View File

@ -26,7 +26,15 @@ export {
WEBDRIVER_BIDI_WEBSOCKET_ENDPOINT_REGEX, WEBDRIVER_BIDI_WEBSOCKET_ENDPOINT_REGEX,
Process, Process,
} from './launch.js'; } from './launch.js';
export {install, canDownload, InstallOptions} from './install.js'; export {
install,
getInstalledBrowsers,
canDownload,
uninstall,
InstallOptions,
GetInstalledBrowsersOptions,
UninstallOptions,
} from './install.js';
export {detectBrowserPlatform} from './detectPlatform.js'; export {detectBrowserPlatform} from './detectPlatform.js';
export { export {
resolveBuildId, resolveBuildId,

View File

@ -0,0 +1,73 @@
/**
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import assert from 'assert';
import fs from 'fs';
import os from 'os';
import path from 'path';
import {
install,
uninstall,
Browser,
BrowserPlatform,
Cache,
} from '../../lib/cjs/main.js';
import {getServerUrl, setupTestServer} from './utils.js';
import {testChromeBuildId} from './versions.js';
describe('common', () => {
setupTestServer();
let tmpDir = '/tmp/puppeteer-browsers-test';
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'puppeteer-browsers-test'));
});
afterEach(() => {
new Cache(tmpDir).clear();
});
it('should uninstall a browser', async function () {
this.timeout(60000);
const expectedOutputPath = path.join(
tmpDir,
'chrome',
`${BrowserPlatform.LINUX}-${testChromeBuildId}`
);
assert.strictEqual(fs.existsSync(expectedOutputPath), false);
const browser = await install({
cacheDir: tmpDir,
browser: Browser.CHROME,
platform: BrowserPlatform.LINUX,
buildId: testChromeBuildId,
baseUrl: getServerUrl(),
});
assert.strictEqual(browser.path, expectedOutputPath);
assert.ok(fs.existsSync(expectedOutputPath));
await uninstall({
cacheDir: tmpDir,
browser: Browser.CHROME,
platform: BrowserPlatform.LINUX,
buildId: testChromeBuildId,
});
assert.strictEqual(browser.path, expectedOutputPath);
assert.strictEqual(fs.existsSync(expectedOutputPath), false);
});
});