Skip to content

Commit

Permalink
feat: add ability to uninstall a browser (#10179)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed May 15, 2023
1 parent 2ba96c6 commit d388a6e
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 20 deletions.
25 changes: 25 additions & 0 deletions docs/browsers-api/browsers.getinstalledbrowsers.md
Original file line number Diff line number Diff line change
@@ -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;
17 changes: 17 additions & 0 deletions docs/browsers-api/browsers.getinstalledbrowsersoptions.md
Original file line number Diff line number Diff line change
@@ -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. | |
2 changes: 1 addition & 1 deletion docs/browsers-api/browsers.installoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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> |
| 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. | |
| 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.** |
Expand Down
21 changes: 21 additions & 0 deletions docs/browsers-api/browsers.uninstall.md
Original file line number Diff line number Diff line change
@@ -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;
20 changes: 20 additions & 0 deletions docs/browsers-api/browsers.uninstalloptions.md
Original file line number Diff line number Diff line change
@@ -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.** |
38 changes: 21 additions & 17 deletions docs/browsers-api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,30 @@ The programmatic API allows installing and launching browsers from your code. Se

## Functions

| Function | Description |
| --------------------------------------------------------------------------------- | ----------- |
| [canDownload(options)](./browsers.candownload.md) | |
| [computeExecutablePath(options)](./browsers.computeexecutablepath.md) | |
| [computeSystemExecutablePath(options)](./browsers.computesystemexecutablepath.md) | |
| [createProfile(browser, opts)](./browsers.createprofile.md) | |
| [detectBrowserPlatform()](./browsers.detectbrowserplatform.md) | |
| [install(options)](./browsers.install.md) | |
| [launch(opts)](./browsers.launch.md) | |
| [makeProgressCallback(browser, buildId)](./browsers.makeprogresscallback.md) | |
| [resolveBuildId(browser, platform, tag)](./browsers.resolvebuildid.md) | |
| Function | Description |
| --------------------------------------------------------------------------------- | ----------------------------------------------------------------- |
| [canDownload(options)](./browsers.candownload.md) | |
| [computeExecutablePath(options)](./browsers.computeexecutablepath.md) | |
| [computeSystemExecutablePath(options)](./browsers.computesystemexecutablepath.md) | |
| [createProfile(browser, opts)](./browsers.createprofile.md) | |
| [detectBrowserPlatform()](./browsers.detectbrowserplatform.md) | |
| [getInstalledBrowsers(options)](./browsers.getinstalledbrowsers.md) | Returns metadata about browsers installed in the cache directory. |
| [install(options)](./browsers.install.md) | |
| [launch(opts)](./browsers.launch.md) | |
| [makeProgressCallback(browser, buildId)](./browsers.makeprogresscallback.md) | |
| [resolveBuildId(browser, platform, tag)](./browsers.resolvebuildid.md) | |
| [uninstall(options)](./browsers.uninstall.md) | |

## Interfaces

| Interface | Description |
| ---------------------------------------------- | ----------- |
| [InstallOptions](./browsers.installoptions.md) | |
| [Options](./browsers.options.md) | |
| [ProfileOptions](./browsers.profileoptions.md) | |
| [SystemOptions](./browsers.systemoptions.md) | |
| Interface | Description |
| ------------------------------------------------------------------------ | ----------- |
| [GetInstalledBrowsersOptions](./browsers.getinstalledbrowsersoptions.md) | |
| [InstallOptions](./browsers.installoptions.md) | |
| [Options](./browsers.options.md) | |
| [ProfileOptions](./browsers.profileoptions.md) | |
| [SystemOptions](./browsers.systemoptions.md) | |
| [UninstallOptions](./browsers.uninstalloptions.md) | |

## Variables

Expand Down
13 changes: 13 additions & 0 deletions packages/browsers/src/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
if (!fs.existsSync(this.#rootDir)) {
return [];
Expand Down
60 changes: 59 additions & 1 deletion packages/browsers/src/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export interface InstallOptions {
*/
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.
*/
buildId: string;
Expand Down Expand Up @@ -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
*/
Expand Down
10 changes: 9 additions & 1 deletion packages/browsers/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ export {
WEBDRIVER_BIDI_WEBSOCKET_ENDPOINT_REGEX,
Process,
} 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 {
resolveBuildId,
Expand Down
73 changes: 73 additions & 0 deletions packages/browsers/test/src/uninstall.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit d388a6e

Please sign in to comment.