mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat(browser): add a method to get installed browsers (#10057)
This commit is contained in:
parent
704624eb20
commit
e16e2a9728
@ -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:
|
||||
*
|
||||
@ -65,4 +75,50 @@ 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 => {
|
||||
return item !== null;
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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};
|
||||
}
|
||||
|
@ -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';
|
||||
@ -97,16 +97,6 @@ export interface InstallOptions {
|
||||
unpack?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export type InstalledBrowser = {
|
||||
path: string;
|
||||
browser: Browser;
|
||||
buildId: string;
|
||||
platform: BrowserPlatform;
|
||||
};
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
|
@ -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,
|
||||
@ -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';
|
||||
|
@ -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 () {
|
||||
|
Loading…
Reference in New Issue
Block a user