local-mode update
Massimo Melina committed
May 3, 2023 at 00:18 UTC
2ddb095db02d173cf2869c4e8aeff0db921067d3
1 file changed
+19
-12
src/update.ts
+19
-12
@@ -5,9 +5,10 @@ import { argv, HFS_REPO, IS_BINARY, IS_WINDOWS, VERSION } from './const'
5
import { basename, dirname, join } from 'path'
6
import { spawn } from 'child_process'
7
import { httpsStream, onProcessExit, unzip } from './misc'
8
-import { renameSync, unlinkSync } from 'fs'
8
+import { createReadStream, renameSync, unlinkSync } from 'fs'
9
import { pluginsWatcher } from './plugins'
10
-import { chmod, stat } from 'fs/promises'
10
+import { access, chmod, stat } from 'fs/promises'
11
+import { Readable } from 'stream'
12
13
export async function getUpdate() {
14
const [latest] = await getRepoInfo(HFS_REPO + '/releases?per_page=1')
@@ -19,22 +20,28 @@ export async function getUpdate() {
20
export async function update() {
21
if (!IS_BINARY)
22
throw "only binary versions are supported for now"
22
- const update = await getUpdate()
23
- const assetSearch = ({ win32: 'windows', darwin: 'mac', linux: 'linux' } as any)[process.platform]
24
- if (!assetSearch)
25
- throw "this feature doesn't support your platform: " + process.platform
26
- const asset = update.assets.find((x: any) => x.name.endsWith('.zip') && x.name.includes(assetSearch))
27
- if (!asset)
28
- throw "asset not found"
29
- const url = asset.browser_download_url
30
- console.log("downloading", url)
23
+ const ZIP = 'hfs-update.zip' // update from file takes precedence over net
24
+ let updateSource: Readable | undefined = await access(ZIP).then(() => createReadStream(ZIP), () => undefined)
25
+ if (!updateSource) {
26
+ const update = await getUpdate()
27
+ const assetSearch = ({ win32: 'windows', darwin: 'mac', linux: 'linux' } as any)[process.platform]
28
+ if (!assetSearch)
29
+ throw "this feature doesn't support your platform: " + process.platform
30
+ const asset = update.assets.find((x: any) => x.name.endsWith('.zip') && x.name.includes(assetSearch))
31
+ if (!asset)
32
+ throw "asset not found"
33
+ const url = asset.browser_download_url
34
+ console.log("downloading", url)
35
+ updateSource = await httpsStream(url)
36
+ }
37
+
38
const bin = process.execPath
39
const binPath = dirname(bin)
40
const binFile = basename(bin)
41
const newBinFile = 'new-' + binFile
42
pluginsWatcher.pause()
43
try {
37
- await unzip(await httpsStream(url), path =>
44
+ await unzip(updateSource, path =>
45
join(binPath, path === binFile ? newBinFile : path))
46
const newBin = join(binPath, newBinFile)
47
if (!IS_WINDOWS) {