Skip to content

Commit 5cb063a

Browse files
author
infiniteregrets
committed
Background binary download with progress
1 parent b1ad596 commit 5cb063a

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

src/binaryManager.ts

+32-21
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function getExtensionMirrordPath(): Uri {
2121

2222
/**
2323
* Tries to find local mirrord in path or in extension storage.
24-
* @param version If specified, then the version of the binary is checked and matched path is returned if it matches.
24+
* @param version If specified, then the version of the binary is checked and matched path is returned.
2525
* @returns Path to mirrord binary or null if not found
2626
*/
2727
export async function getLocalMirrordBinary(version?: string): Promise<string | null> {
@@ -47,7 +47,7 @@ export async function getLocalMirrordBinary(version?: string): Promise<string |
4747
const api = new MirrordAPI(mirrordPath);
4848
const installedVersion = await api.getBinaryVersion();
4949

50-
// we use semver.get here because installedVersion can be greater than the latest version
50+
// we use semver.gte here because installedVersion can be greater than the latest version
5151
// if we are running on the release CI.
5252
if (installedVersion && semver.gte(installedVersion, version)) {
5353
return mirrordPath;
@@ -145,6 +145,7 @@ export async function getMirrordBinary(): Promise<string> {
145145
if (localMirrordBinary) {
146146
return localMirrordBinary;
147147
}
148+
// donot block and download binary, instead download in background
148149
await downloadMirrordBinary(getExtensionMirrordPath(), autoUpdateConfigured);
149150
return extensionPath;
150151
} else {
@@ -218,25 +219,35 @@ function getMirrordDownloadUrl(version: string): string {
218219
* @param destPath Path to download the binary to
219220
*/
220221
async function downloadMirrordBinary(destPath: Uri, version: string): Promise<void> {
221-
fs.mkdirSync(Utils.dirname(destPath).fsPath, { recursive: true });
222-
const response: AxiosResponse = await window.withProgress({
223-
location: ProgressLocation.Notification,
222+
await window.withProgress({
223+
location: ProgressLocation.Window,
224224
title: "mirrord",
225225
cancellable: false
226-
}, (progress, _) => {
227-
progress.report({ increment: 0, "message": "Downloading mirrord binary..." });
228-
const p = axios.get(
229-
getMirrordDownloadUrl(version),
230-
{
231-
onDownloadProgress: function (progressEvent) {
232-
progress.report({ increment: progressEvent.progress, "message": "Downloading mirrord binary..." });
233-
},
234-
responseType: 'arraybuffer',
235-
});
236-
237-
return p;
238-
}
239-
);
240-
fs.writeFileSync(destPath.fsPath, response.data);
241-
fs.chmodSync(destPath.fsPath, 0o755);
226+
}, async (progress) => {
227+
return new Promise<void>(async (resolve, reject) => {
228+
fs.mkdirSync(Utils.dirname(destPath).fsPath, { recursive: true });
229+
try {
230+
const response: AxiosResponse = await axios.get(
231+
getMirrordDownloadUrl(version),
232+
{
233+
onDownloadProgress: function (progressEvent) {
234+
progress.report({ increment: progressEvent.progress, "message": "Downloading mirrord binary..." });
235+
},
236+
responseType: 'arraybuffer',
237+
});
238+
239+
fs.writeFileSync(destPath.fsPath, response.data);
240+
fs.chmodSync(destPath.fsPath, 0o755);
241+
new NotificationBuilder()
242+
.withMessage(`Downloaded mirrord binary version ${version}`)
243+
.info();
244+
resolve();
245+
} catch (error) {
246+
new NotificationBuilder()
247+
.withMessage(`Error downloading mirrord binary: ${error}`)
248+
.error();
249+
reject(error);
250+
}
251+
});
252+
});
242253
}

0 commit comments

Comments
 (0)