Skip to content

Commit 8203828

Browse files
author
Veetaha
committed
vscode-prerefactor: merge two files into downloads.ts
1 parent fab40db commit 8203828

File tree

2 files changed

+46
-50
lines changed

2 files changed

+46
-50
lines changed

editors/code/src/installation/download_artifact.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.

editors/code/src/installation/download_file.ts renamed to editors/code/src/installation/downloads.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import fetch from "node-fetch";
2+
import * as vscode from "vscode";
3+
import * as path from "path";
24
import * as fs from "fs";
35
import * as stream from "stream";
46
import * as util from "util";
57
import { log, assert } from "../util";
8+
import { ArtifactReleaseInfo } from "./interfaces";
69

710
const pipeline = util.promisify(stream.pipeline);
811

@@ -49,3 +52,46 @@ export async function downloadFile(
4952
// Issue at nodejs repo: https://github.com/nodejs/node/issues/31776
5053
});
5154
}
55+
56+
/**
57+
* Downloads artifact from given `downloadUrl`.
58+
* Creates `installationDir` if it is not yet created and puts the artifact under
59+
* `artifactFileName`.
60+
* Displays info about the download progress in an info message printing the name
61+
* of the artifact as `displayName`.
62+
*/
63+
export async function downloadArtifactWithProgressUi(
64+
{ downloadUrl, releaseName }: ArtifactReleaseInfo,
65+
artifactFileName: string,
66+
installationDir: string,
67+
displayName: string,
68+
) {
69+
await fs.promises.mkdir(installationDir).catch(err => assert(
70+
err?.code === "EEXIST",
71+
`Couldn't create directory "${installationDir}" to download ` +
72+
`${artifactFileName} artifact: ${err?.message}`
73+
));
74+
75+
const installationPath = path.join(installationDir, artifactFileName);
76+
77+
await vscode.window.withProgress(
78+
{
79+
location: vscode.ProgressLocation.Notification,
80+
cancellable: false, // FIXME: add support for canceling download?
81+
title: `Downloading rust-analyzer ${displayName} (${releaseName})`
82+
},
83+
async (progress, _cancellationToken) => {
84+
let lastPrecentage = 0;
85+
const filePermissions = 0o755; // (rwx, r_x, r_x)
86+
await downloadFile(downloadUrl, installationPath, filePermissions, (readBytes, totalBytes) => {
87+
const newPercentage = (readBytes / totalBytes) * 100;
88+
progress.report({
89+
message: newPercentage.toFixed(0) + "%",
90+
increment: newPercentage - lastPrecentage
91+
});
92+
93+
lastPrecentage = newPercentage;
94+
});
95+
}
96+
);
97+
}

0 commit comments

Comments
 (0)