Simplify file download code

This commit is contained in:
Laurențiu Nicola 2021-01-25 19:12:54 +02:00
parent 2c735ed734
commit 03a1da9d46
2 changed files with 1 additions and 10 deletions

View File

@ -208,7 +208,6 @@ async function bootstrapExtension(config: Config, state: PersistentState): Promi
url: artifact.browser_download_url,
dest,
progressTitle: "Downloading rust-analyzer extension",
overwrite: true,
});
});
@ -340,7 +339,6 @@ async function getServer(config: Config, state: PersistentState): Promise<string
progressTitle: "Downloading rust-analyzer server",
gunzip: true,
mode: 0o755,
overwrite: true,
});
});

View File

@ -73,23 +73,16 @@ interface DownloadOpts {
dest: string;
mode?: number;
gunzip?: boolean;
overwrite?: boolean;
}
export async function download(opts: DownloadOpts) {
// Put artifact into a temporary file (in the same dir for simplicity)
// to prevent partially downloaded files when user kills vscode
// This also avoids overwriting running executables
const dest = path.parse(opts.dest);
const randomHex = crypto.randomBytes(5).toString("hex");
const tempFile = path.join(dest.dir, `${dest.name}${randomHex}`);
if (opts.overwrite) {
// Unlinking the exe file before moving new one on its place should prevent ETXTBSY error.
await fs.promises.unlink(opts.dest).catch(err => {
if (err.code !== "ENOENT") throw err;
});
}
await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,