From 641f714fa417acfec2a168c24d92a93da5e05b2a Mon Sep 17 00:00:00 2001 From: Koen Vlaswinkel Date: Tue, 19 Dec 2023 16:49:28 +0100 Subject: [PATCH] Extract downloadWithProgress function in ensureCli --- .../ql-vscode/test/vscode-tests/ensureCli.ts | 58 +++++++++---------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/extensions/ql-vscode/test/vscode-tests/ensureCli.ts b/extensions/ql-vscode/test/vscode-tests/ensureCli.ts index e234e190d..7bee80de0 100644 --- a/extensions/ql-vscode/test/vscode-tests/ensureCli.ts +++ b/extensions/ql-vscode/test/vscode-tests/ensureCli.ts @@ -88,36 +88,7 @@ export async function ensureCli(useCli: boolean) { `CLI version ${CLI_VERSION} zip file not found. Downloading from '${url}' into '${downloadedFilePath}'.`, ); - 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(downloadedFilePath); - const body = assetStream.body; - await new Promise((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); - }); + await downloadWithProgress(url, downloadedFilePath); } else { console.log( `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((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 */