diff options
Diffstat (limited to 'editors/code/src/net.ts')
-rw-r--r-- | editors/code/src/net.ts | 25 |
1 files changed, 10 insertions, 15 deletions
diff --git a/editors/code/src/net.ts b/editors/code/src/net.ts index 1ab21e726..d39dc1baf 100644 --- a/editors/code/src/net.ts +++ b/editors/code/src/net.ts | |||
@@ -73,23 +73,16 @@ interface DownloadOpts { | |||
73 | dest: string; | 73 | dest: string; |
74 | mode?: number; | 74 | mode?: number; |
75 | gunzip?: boolean; | 75 | gunzip?: boolean; |
76 | overwrite?: boolean; | ||
77 | } | 76 | } |
78 | 77 | ||
79 | export async function download(opts: DownloadOpts) { | 78 | export async function download(opts: DownloadOpts) { |
80 | // Put artifact into a temporary file (in the same dir for simplicity) | 79 | // Put artifact into a temporary file (in the same dir for simplicity) |
81 | // to prevent partially downloaded files when user kills vscode | 80 | // to prevent partially downloaded files when user kills vscode |
81 | // This also avoids overwriting running executables | ||
82 | const dest = path.parse(opts.dest); | 82 | const dest = path.parse(opts.dest); |
83 | const randomHex = crypto.randomBytes(5).toString("hex"); | 83 | const randomHex = crypto.randomBytes(5).toString("hex"); |
84 | const tempFile = path.join(dest.dir, `${dest.name}${randomHex}`); | 84 | const tempFile = path.join(dest.dir, `${dest.name}${randomHex}`); |
85 | 85 | ||
86 | if (opts.overwrite) { | ||
87 | // Unlinking the exe file before moving new one on its place should prevent ETXTBSY error. | ||
88 | await fs.promises.unlink(opts.dest).catch(err => { | ||
89 | if (err.code !== "ENOENT") throw err; | ||
90 | }); | ||
91 | } | ||
92 | |||
93 | await vscode.window.withProgress( | 86 | await vscode.window.withProgress( |
94 | { | 87 | { |
95 | location: vscode.ProgressLocation.Notification, | 88 | location: vscode.ProgressLocation.Notification, |
@@ -99,13 +92,15 @@ export async function download(opts: DownloadOpts) { | |||
99 | async (progress, _cancellationToken) => { | 92 | async (progress, _cancellationToken) => { |
100 | let lastPercentage = 0; | 93 | let lastPercentage = 0; |
101 | await downloadFile(opts.url, tempFile, opts.mode, !!opts.gunzip, (readBytes, totalBytes) => { | 94 | await downloadFile(opts.url, tempFile, opts.mode, !!opts.gunzip, (readBytes, totalBytes) => { |
102 | const newPercentage = (readBytes / totalBytes) * 100; | 95 | const newPercentage = Math.round((readBytes / totalBytes) * 100); |
103 | progress.report({ | 96 | if (newPercentage !== lastPercentage) { |
104 | message: newPercentage.toFixed(0) + "%", | 97 | progress.report({ |
105 | increment: newPercentage - lastPercentage | 98 | message: `${newPercentage.toFixed(0)}%`, |
106 | }); | 99 | increment: newPercentage - lastPercentage |
107 | 100 | }); | |
108 | lastPercentage = newPercentage; | 101 | |
102 | lastPercentage = newPercentage; | ||
103 | } | ||
109 | }); | 104 | }); |
110 | } | 105 | } |
111 | ); | 106 | ); |