Extract downloadWithProgress function in ensureCli
This commit is contained in:
@@ -88,36 +88,7 @@ export async function ensureCli(useCli: boolean) {
|
|||||||
`CLI version ${CLI_VERSION} zip file not found. Downloading from '${url}' into '${downloadedFilePath}'.`,
|
`CLI version ${CLI_VERSION} zip file not found. Downloading from '${url}' into '${downloadedFilePath}'.`,
|
||||||
);
|
);
|
||||||
|
|
||||||
const assetStream = await fetch(url);
|
await downloadWithProgress(url, downloadedFilePath);
|
||||||
const contentLength = Number(
|
|
||||||
assetStream.headers.get("content-length") || 0,
|
|
||||||
);
|
|
||||||
console.log("Total content size", Math.round(contentLength / _1MB), "MB");
|
|
||||||
const archiveFile = createWriteStream(downloadedFilePath);
|
|
||||||
const body = assetStream.body;
|
|
||||||
await new Promise<void>((resolve, reject) => {
|
|
||||||
let numBytesDownloaded = 0;
|
|
||||||
let lastMessage = 0;
|
|
||||||
body.on("data", (data) => {
|
|
||||||
numBytesDownloaded += data.length;
|
|
||||||
if (numBytesDownloaded - lastMessage > _10MB) {
|
|
||||||
console.log(
|
|
||||||
"Downloaded",
|
|
||||||
Math.round(numBytesDownloaded / _1MB),
|
|
||||||
"MB",
|
|
||||||
);
|
|
||||||
lastMessage = numBytesDownloaded;
|
|
||||||
}
|
|
||||||
archiveFile.write(data);
|
|
||||||
});
|
|
||||||
body.on("finish", () => {
|
|
||||||
archiveFile.end(() => {
|
|
||||||
console.log("Finished download into", downloadedFilePath);
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
body.on("error", reject);
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
console.log(
|
console.log(
|
||||||
`CLI version ${CLI_VERSION} zip file found at '${downloadedFilePath}'.`,
|
`CLI version ${CLI_VERSION} zip file found at '${downloadedFilePath}'.`,
|
||||||
@@ -135,6 +106,33 @@ export async function ensureCli(useCli: boolean) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function downloadWithProgress(url: string, filePath: string) {
|
||||||
|
const assetStream = await fetch(url);
|
||||||
|
const contentLength = Number(assetStream.headers.get("content-length") || 0);
|
||||||
|
console.log("Total content size", Math.round(contentLength / _1MB), "MB");
|
||||||
|
const archiveFile = createWriteStream(filePath);
|
||||||
|
const body = assetStream.body;
|
||||||
|
await new Promise<void>((resolve, reject) => {
|
||||||
|
let numBytesDownloaded = 0;
|
||||||
|
let lastMessage = 0;
|
||||||
|
body.on("data", (data) => {
|
||||||
|
numBytesDownloaded += data.length;
|
||||||
|
if (numBytesDownloaded - lastMessage > _10MB) {
|
||||||
|
console.log("Downloaded", Math.round(numBytesDownloaded / _1MB), "MB");
|
||||||
|
lastMessage = numBytesDownloaded;
|
||||||
|
}
|
||||||
|
archiveFile.write(data);
|
||||||
|
});
|
||||||
|
body.on("finish", () => {
|
||||||
|
archiveFile.end(() => {
|
||||||
|
console.log("Finished download into", filePath);
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
body.on("error", reject);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Url to download from
|
* Url to download from
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user