print progress while downloading an update, in case of slow connection
Massimo Melina committed
Apr 21, 2026 at 00:07 UTC
e44dc5efadb7c05e983d1553f55d90df6c7dcf75
1 file changed
+13
-4
src/update.ts
+13
-4
@@ -4,10 +4,12 @@ import { apiGithubPaginated, getProjectInfo, getRepoInfo } from './github'
4
import { ARGS_FILE, HFS_REPO, IS_BINARY, IS_WINDOWS, IS_MAC, PREVIOUS_TAG, RUNNING_BETA } from './const'
5
import { dirname, join } from 'path'
6
import { spawn, spawnSync } from 'child_process'
7
-import { DAY, exists, unzip, prefix, xlate, HOUR, httpStream, statWithTimeout, repeat, debounceAsync } from './misc'
8
-import { createReadStream, existsSync, renameSync, unlinkSync, writeFileSync } from 'fs'
7
+import {
8
+ DAY, exists, unzip, prefix, xlate, HOUR, httpStream, statWithTimeout, repeat, debounceAsync, formatPerc
9
+} from './misc'
10
+import { createReadStream, createWriteStream, existsSync, renameSync, unlinkSync, writeFileSync } from 'fs'
11
import { pluginsWatcher } from './plugins'
10
-import { chmod, rename, writeFile, rm } from 'fs/promises'
12
+import { chmod, rename, rm } from 'fs/promises'
13
import open from 'open'
14
import { configReady, currentVersion, defineConfig, versionToScalar } from './config'
15
import { cmdEscape, runningAsWindowsService } from './util-os'
@@ -15,6 +17,7 @@ import { onProcessExit, quit } from './first'
17
import { storedMap } from './persistence'
18
import _ from 'lodash'
19
import { argv } from './argv'
20
+import { pipeline } from 'stream/promises'
21
22
const updateToBeta = defineConfig('update_to_beta', false)
23
const autoCheckUpdate = defineConfig('auto_check_update', true)
@@ -143,7 +146,13 @@ export async function update(tagOrUrl: string='') {
146
const temp = LOCAL_UPDATE + '-temp'
147
await rm(temp, { force: true })
148
try {
146
- await writeFile(temp, await httpStream(url))
149
+ const stream = await httpStream(url)
150
+ const total = Number(stream.headers['content-length']) || 0
151
+ let downloadedSize = 0
152
+ const progress = total && setInterval(() => console.log("Download progress", formatPerc(downloadedSize / total)), 5_000)
153
+ stream.on('data', chunk => downloadedSize += chunk.length)
154
+ await pipeline(stream, createWriteStream(temp))
155
+ .finally(() => clearInterval(progress))
156
}
157
catch(e: any) {
158
await rm(temp).catch(() => {}) // no leftovers