mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chore: show small progress bar in downloader without tty (#13690)
This commit is contained in:
parent
317b649f78
commit
39525878ab
@ -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`;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user