Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore: show small progress bar in downloader without tty (#13690)

Max Schmitt39525878ab00
diff --git a/packages/playwright-core/src/server/registry/download.ts b/packages/playwright-core/src/server/registry/download.ts
index 2584219b05..12f14301ff 100644
--- a/packages/playwright-core/src/server/registry/download.ts
+++ b/packages/playwright-core/src/server/registry/download.ts
@@ -26,19 +26,19 @@ type DownloadFileOptions = {
userAgent?: string
};
-function downloadFile(url: string, destinationPath: string, options: DownloadFileOptions = {}): Promise<{error: any}> {
+function downloadFile(url: string, destinationPath: string, options: DownloadFileOptions = {}): Promise<{ error: any }> {
const {
progressCallback,
- log = () => {},
+ log = () => { },
} = options;
log(`running download:`);
log(`-- from url: ${url}`);
log(`-- to location: ${destinationPath}`);
- let fulfill: ({ error }: {error: any}) => void = ({ error }) => {};
+ let fulfill: ({ error }: { error: any }) => void = ({ error }) => { };
let downloadedBytes = 0;
let totalBytes = 0;
- const promise: Promise<{error: any}> = new Promise(x => { fulfill = x; });
+ const promise: Promise<{ error: any }> = new Promise(x => { fulfill = x; });
httpRequest({
url,
@@ -83,7 +83,7 @@ export async function download(
destination: string,
options: DownloadOptions = {}
) {
- const { progressBarName = 'file', retryCount = 3, log = () => {}, userAgent } = options;
+ const { progressBarName = 'file', retryCount = 3, log = () => { }, userAgent } = options;
for (let attempt = 1; attempt <= retryCount; ++attempt) {
log(
`downloading ${progressBarName} - attempt #${attempt}`
@@ -115,12 +115,16 @@ export async function download(
}
function getDownloadProgress(progressBarName: string): OnProgressCallback {
+ if (process.stdout.isTTY)
+ return _getAnimatedDownloadProgress(progressBarName);
+ return _getBasicDownloadProgress(progressBarName);
+}
+
+function _getAnimatedDownloadProgress(progressBarName: string): OnProgressCallback {
let progressBar: ProgressBar;
let lastDownloadedBytes = 0;
return (downloadedBytes: number, totalBytes: number) => {
- if (!process.stderr.isTTY)
- return;
if (!progressBar) {
progressBar = new ProgressBar(
`Downloading ${progressBarName} - ${toMegabytes(
@@ -140,6 +144,24 @@ function getDownloadProgress(progressBarName: string): OnProgressCallback {
};
}
+function _getBasicDownloadProgress(progressBarName: string): OnProgressCallback {
+ // eslint-disable-next-line no-console
+ console.log(`Downloading ${progressBarName}...`);
+ const totalRows = 10;
+ const stepWidth = 8;
+ let lastRow = -1;
+ return (downloadedBytes: number, totalBytes: number) => {
+ const percentage = downloadedBytes / totalBytes;
+ const row = Math.floor(totalRows * percentage);
+ if (row > lastRow) {
+ lastRow = row;
+ const percentageString = String(percentage * 100 | 0).padStart(3);
+ // eslint-disable-next-line no-console
+ console.log(`|${'■'.repeat(row * stepWidth)}${' '.repeat((totalRows - row) * stepWidth)}| ${percentageString}% of ${toMegabytes(totalBytes)}`);
+ }
+ };
+}
+
function toMegabytes(bytes: number) {
const mb = bytes / 1024 / 1024;
return `${Math.round(mb * 10) / 10} Mb`;