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
|
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 {
|
const {
|
||||||
progressCallback,
|
progressCallback,
|
||||||
log = () => {},
|
log = () => { },
|
||||||
} = options;
|
} = options;
|
||||||
log(`running download:`);
|
log(`running download:`);
|
||||||
log(`-- from url: ${url}`);
|
log(`-- from url: ${url}`);
|
||||||
log(`-- to location: ${destinationPath}`);
|
log(`-- to location: ${destinationPath}`);
|
||||||
let fulfill: ({ error }: {error: any}) => void = ({ error }) => {};
|
let fulfill: ({ error }: { error: any }) => void = ({ error }) => { };
|
||||||
let downloadedBytes = 0;
|
let downloadedBytes = 0;
|
||||||
let totalBytes = 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({
|
httpRequest({
|
||||||
url,
|
url,
|
||||||
|
|
@ -83,7 +83,7 @@ export async function download(
|
||||||
destination: string,
|
destination: string,
|
||||||
options: DownloadOptions = {}
|
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) {
|
for (let attempt = 1; attempt <= retryCount; ++attempt) {
|
||||||
log(
|
log(
|
||||||
`downloading ${progressBarName} - attempt #${attempt}`
|
`downloading ${progressBarName} - attempt #${attempt}`
|
||||||
|
|
@ -115,12 +115,16 @@ export async function download(
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDownloadProgress(progressBarName: string): OnProgressCallback {
|
function getDownloadProgress(progressBarName: string): OnProgressCallback {
|
||||||
|
if (process.stdout.isTTY)
|
||||||
|
return _getAnimatedDownloadProgress(progressBarName);
|
||||||
|
return _getBasicDownloadProgress(progressBarName);
|
||||||
|
}
|
||||||
|
|
||||||
|
function _getAnimatedDownloadProgress(progressBarName: string): OnProgressCallback {
|
||||||
let progressBar: ProgressBar;
|
let progressBar: ProgressBar;
|
||||||
let lastDownloadedBytes = 0;
|
let lastDownloadedBytes = 0;
|
||||||
|
|
||||||
return (downloadedBytes: number, totalBytes: number) => {
|
return (downloadedBytes: number, totalBytes: number) => {
|
||||||
if (!process.stderr.isTTY)
|
|
||||||
return;
|
|
||||||
if (!progressBar) {
|
if (!progressBar) {
|
||||||
progressBar = new ProgressBar(
|
progressBar = new ProgressBar(
|
||||||
`Downloading ${progressBarName} - ${toMegabytes(
|
`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) {
|
function toMegabytes(bytes: number) {
|
||||||
const mb = bytes / 1024 / 1024;
|
const mb = bytes / 1024 / 1024;
|
||||||
return `${Math.round(mb * 10) / 10} Mb`;
|
return `${Math.round(mb * 10) / 10} Mb`;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue