Skip to content

Commit

Permalink
feat(browser): add an API to get installed browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Apr 21, 2023
1 parent 704624e commit ff8bfb9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
54 changes: 54 additions & 0 deletions packages/browsers/src/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ import path from 'path';

import {Browser, BrowserPlatform} from './browser-data/browser-data.js';

/**
* @public
*/
export type InstalledBrowser = {
path: string;
browser: Browser;
buildId: string;
platform: BrowserPlatform;
};

/**
* The cache used by Puppeteer relies on the following structure:
*
Expand Down Expand Up @@ -65,4 +75,48 @@ export class Cache {
retryDelay: 500,
});
}

getInstalledBrowsers(): InstalledBrowser[] {
if (!fs.existsSync(this.#rootDir)) {
return [];
}
const types = fs.readdirSync(this.#rootDir);
const browsers = types.filter((t): t is Browser => {
return (Object.values(Browser) as string[]).includes(t);
});
return browsers.flatMap(browser => {
const files = fs.readdirSync(this.browserRoot(browser));
return files
.map(file => {
const result = parseFolderPath(
path.join(this.browserRoot(browser), file)
);
if (!result) {
return null;
}
return {
path: path.join(this.browserRoot(browser), file),
browser,
platform: result.platform,
buildId: result.buildId,
};
})
.filter((item): item is InstalledBrowser => item !== null);

Check failure on line 104 in packages/browsers/src/Cache.ts

View workflow job for this annotation

GitHub Actions / [Required] Inspect code

Expected block statement surrounding arrow body
});
}
}

function parseFolderPath(
folderPath: string
): {platform: string; buildId: string} | undefined {
const name = path.basename(folderPath);
const splits = name.split('-');
if (splits.length !== 2) {
return;
}
const [platform, buildId] = splits;
if (!buildId || !platform) {
return;
}
return {platform, buildId};
}
12 changes: 1 addition & 11 deletions packages/browsers/src/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
BrowserPlatform,
downloadUrls,
} from './browser-data/browser-data.js';
import {Cache} from './Cache.js';
import {Cache, InstalledBrowser} from './Cache.js';
import {debug} from './debug.js';
import {detectBrowserPlatform} from './detectPlatform.js';
import {unpackArchive} from './fileUtil.js';
Expand Down Expand Up @@ -97,16 +97,6 @@ export interface InstallOptions {
unpack?: boolean;
}

/**
* @public
*/
export type InstalledBrowser = {
path: string;
browser: Browser;
buildId: string;
platform: BrowserPlatform;
};

/**
* @public
*/
Expand Down
9 changes: 2 additions & 7 deletions packages/browsers/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ export {
WEBDRIVER_BIDI_WEBSOCKET_ENDPOINT_REGEX,
Process,
} from './launch.js';
export {
install,
canDownload,
InstallOptions,
InstalledBrowser,
} from './install.js';
export {install, canDownload, InstallOptions} from './install.js';
export {detectBrowserPlatform} from './detectPlatform.js';
export {
resolveBuildId,
Expand All @@ -42,4 +37,4 @@ export {
ProfileOptions,
} from './browser-data/browser-data.js';
export {CLI, makeProgressCallback} from './CLI.js';
export {Cache} from './Cache.js';
export {Cache, InstalledBrowser} from './Cache.js';
4 changes: 4 additions & 0 deletions packages/browsers/test/src/chrome/install.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ describe('Chrome install', () => {
});
assert.strictEqual(browser.path, expectedOutputPath);
assert.ok(fs.existsSync(expectedOutputPath));
// Should discover installed browsers.
const cache = new Cache(tmpDir);
const installed = cache.getInstalledBrowsers();
assert.deepStrictEqual(browser, installed[0]);
});

it('throws on invalid URL', async function () {
Expand Down

0 comments on commit ff8bfb9

Please sign in to comment.