Skip to content

Commit 641f714

Browse files
committed
Extract downloadWithProgress function in ensureCli
1 parent 216f10d commit 641f714

1 file changed

Lines changed: 28 additions & 30 deletions

File tree

extensions/ql-vscode/test/vscode-tests/ensureCli.ts

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -88,36 +88,7 @@ export async function ensureCli(useCli: boolean) {
8888
`CLI version ${CLI_VERSION} zip file not found. Downloading from '${url}' into '${downloadedFilePath}'.`,
8989
);
9090

91-
const assetStream = await fetch(url);
92-
const contentLength = Number(
93-
assetStream.headers.get("content-length") || 0,
94-
);
95-
console.log("Total content size", Math.round(contentLength / _1MB), "MB");
96-
const archiveFile = createWriteStream(downloadedFilePath);
97-
const body = assetStream.body;
98-
await new Promise<void>((resolve, reject) => {
99-
let numBytesDownloaded = 0;
100-
let lastMessage = 0;
101-
body.on("data", (data) => {
102-
numBytesDownloaded += data.length;
103-
if (numBytesDownloaded - lastMessage > _10MB) {
104-
console.log(
105-
"Downloaded",
106-
Math.round(numBytesDownloaded / _1MB),
107-
"MB",
108-
);
109-
lastMessage = numBytesDownloaded;
110-
}
111-
archiveFile.write(data);
112-
});
113-
body.on("finish", () => {
114-
archiveFile.end(() => {
115-
console.log("Finished download into", downloadedFilePath);
116-
resolve();
117-
});
118-
});
119-
body.on("error", reject);
120-
});
91+
await downloadWithProgress(url, downloadedFilePath);
12192
} else {
12293
console.log(
12394
`CLI version ${CLI_VERSION} zip file found at '${downloadedFilePath}'.`,
@@ -135,6 +106,33 @@ export async function ensureCli(useCli: boolean) {
135106
}
136107
}
137108

109+
async function downloadWithProgress(url: string, filePath: string) {
110+
const assetStream = await fetch(url);
111+
const contentLength = Number(assetStream.headers.get("content-length") || 0);
112+
console.log("Total content size", Math.round(contentLength / _1MB), "MB");
113+
const archiveFile = createWriteStream(filePath);
114+
const body = assetStream.body;
115+
await new Promise<void>((resolve, reject) => {
116+
let numBytesDownloaded = 0;
117+
let lastMessage = 0;
118+
body.on("data", (data) => {
119+
numBytesDownloaded += data.length;
120+
if (numBytesDownloaded - lastMessage > _10MB) {
121+
console.log("Downloaded", Math.round(numBytesDownloaded / _1MB), "MB");
122+
lastMessage = numBytesDownloaded;
123+
}
124+
archiveFile.write(data);
125+
});
126+
body.on("finish", () => {
127+
archiveFile.end(() => {
128+
console.log("Finished download into", filePath);
129+
resolve();
130+
});
131+
});
132+
body.on("error", reject);
133+
});
134+
}
135+
138136
/**
139137
* Url to download from
140138
*/

0 commit comments

Comments
 (0)