chore: expose additional methods for Puppeteer (#9900)

This commit is contained in:
Alex Rudenko 2023-03-22 10:04:41 +01:00 committed by GitHub
parent 69b77ded4d
commit 00abcdca7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 30 deletions

View File

@ -155,7 +155,7 @@ export class CLI {
buildId: args.browser.buildId,
platform: args.platform,
cacheDir: args.path ?? this.#cachePath,
downloadProgressCallback: this.#makeProgressCallback(
downloadProgressCallback: makeProgressCallback(
args.browser.name,
args.browser.buildId
),
@ -265,32 +265,35 @@ export class CLI {
#parseBuildId(version: string): string {
return version.split('@').pop() ?? 'latest';
}
#toMegabytes(bytes: number) {
const mb = bytes / 1000 / 1000;
return `${Math.round(mb * 10) / 10} MB`;
}
#makeProgressCallback(browser: Browser, buildId: string) {
let progressBar: ProgressBar;
let lastDownloadedBytes = 0;
return (downloadedBytes: number, totalBytes: number) => {
if (!progressBar) {
progressBar = new ProgressBar(
`Downloading ${browser} r${buildId} - ${this.#toMegabytes(
totalBytes
)} [:bar] :percent :etas `,
{
complete: '=',
incomplete: ' ',
width: 20,
total: totalBytes,
}
);
}
const delta = downloadedBytes - lastDownloadedBytes;
lastDownloadedBytes = downloadedBytes;
progressBar.tick(delta);
};
}
}
export function makeProgressCallback(
browser: Browser,
buildId: string
): (downloadedBytes: number, totalBytes: number) => void {
let progressBar: ProgressBar;
let lastDownloadedBytes = 0;
return (downloadedBytes: number, totalBytes: number) => {
if (!progressBar) {
progressBar = new ProgressBar(
`Downloading ${browser} r${buildId} - ${toMegabytes(
totalBytes
)} [:bar] :percent :etas `,
{
complete: '=',
incomplete: ' ',
width: 20,
total: totalBytes,
}
);
}
const delta = downloadedBytes - lastDownloadedBytes;
lastDownloadedBytes = downloadedBytes;
progressBar.tick(delta);
};
}
function toMegabytes(bytes: number) {
const mb = bytes / 1000 / 1000;
return `${Math.round(mb * 10) / 10} MB`;
}

View File

@ -24,9 +24,10 @@ export {
export {fetch, canFetch} from './fetch.js';
export {detectBrowserPlatform} from './detectPlatform.js';
export {
resolveBuildId,
Browser,
BrowserPlatform,
ChromeReleaseChannel,
} from './browser-data/browser-data.js';
export {CLI} from './CLI.js';
export {CLI, makeProgressCallback} from './CLI.js';
export {Cache} from './Cache.js';