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

This commit is contained in:
Max Schmitt 2022-04-22 19:55:43 +02:00 committed by GitHub
parent 317b649f78
commit 39525878ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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`;