in src/commands/testDependenciesCommands.ts [188:247]
async function downloadJar(
libFolder: string,
groupId: string,
artifactId: string,
version: string,
totalJars: number,
progress: Progress<{message?: string; increment?: number}>,
token: CancellationToken
): Promise<void> {
// tslint:disable-next-line: typedef
await new Promise<void>(async (resolve, reject) => {
progress.report({
message: `Downloading ${artifactId}-${version}.jar...`,
});
const tempFilePath: string = path.join(os.tmpdir(), `${artifactId}-${version}.jar`);
const writer: WriteStream = createWriteStream(tempFilePath);
const url: string = getDownloadLink(groupId, artifactId, version);
const totalSize: number = await getTotalBytes(url);
if (token.isCancellationRequested) {
writer.close();
return reject(new Error('User cancelled'));
}
const req: ClientRequest = https.get(url, (res: IncomingMessage) => {
res.pipe(writer);
res.on('data', (chunk: any) => {
progress.report({
message: `Downloading ${artifactId}-${version}.jar...`,
increment: chunk.length / totalSize / totalJars * 100,
});
});
});
token.onCancellationRequested(() => {
req.destroy();
writer.close();
fse.unlink(tempFilePath);
reject(new Error('User cancelled'));
});
req.on('error', (err: any) => {
writer.close();
fse.unlink(tempFilePath);
reject(err);
});
writer.on('finish', () => {
writer.close();
const filePath: string = path.join(libFolder, `${artifactId}-${version}.jar`);
fse.move(tempFilePath, filePath, { overwrite: false });
return resolve();
});
writer.on('error', () => {
writer.close();
fse.unlink(tempFilePath);
reject(new Error('Failed to write jar file.'));
});
});
}