fix: automatic updating was not retaining --cwd #760
Massimo Melina committed
Oct 3, 2024 at 19:53 UTC
992a1e45f199dda3828c0a14ac101aef9e8892cb
2 files changed
+25
-9
src/const.ts
+13
-3
@@ -3,7 +3,7 @@
3
import minimist from 'minimist'
4
import * as fs from 'fs'
5
import { homedir } from 'os'
6
-import { mkdirSync } from 'fs'
6
+import _ from 'lodash'
7
import { basename, dirname, join } from 'path'
8
export * from './cross-const'
9
@@ -12,6 +12,16 @@ export const COMPATIBLE_API_VERSION = 1 // while changes in the api are not brea
12
export const HFS_REPO = 'rejetto/hfs'
13
14
export const argv = minimist(process.argv.slice(2))
15
+// you can add arguments with this file, currently used for the update process on mac/linux
16
+export const ARGS_FILE = join(homedir(), 'hfs-args')
17
+try {
18
+ const s = fs.readFileSync(ARGS_FILE, 'utf-8')
19
+ console.log('additional arguments', s)
20
+ _.defaults(argv, minimist(JSON.parse(s)))
21
+ fs.unlinkSync(ARGS_FILE)
22
+}
23
+catch {}
24
+
25
export const DEV = process.env.DEV || argv.dev ? 'DEV' : ''
26
export const ORIGINAL_CWD = process.cwd()
27
export const HFS_STARTED = new Date()
@@ -40,14 +50,14 @@ const winExe = IS_WINDOWS && process.execPath.match(/(?<!node)\.exe$/i)
50
// still considering whether to use ".hfs" with Windows users, who may be less accustomed to it
51
const dir = argv.cwd || useHomeDir() && join(homedir(), '.hfs')
52
if (dir) {
43
- try { mkdirSync(dir) }
53
+ try { fs.mkdirSync(dir) }
54
catch(e: any) {
55
if (e.code !== 'EEXIST')
56
console.error(e)
57
}
58
process.chdir(dir)
59
}
50
-console.log('working directory', process.cwd())
60
+console.log('working directory (cwd)', process.cwd())
61
if (APP_PATH !== process.cwd())
62
console.log('app', APP_PATH)
63
console.log('node', process.version)
src/update.ts
+12
-6
@@ -1,11 +1,11 @@
1
// This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
2
3
import { getProjectInfo, getRepoInfo } from './github'
4
-import { argv, HFS_REPO, IS_BINARY, IS_WINDOWS, RUNNING_BETA } from './const'
4
+import { ARGS_FILE, argv, HFS_REPO, IS_BINARY, IS_WINDOWS, RUNNING_BETA } from './const'
5
import { dirname, join } from 'path'
6
import { spawn, spawnSync } from 'child_process'
7
import { DAY, exists, debounceAsync, httpStream, unzip, prefix, xlate, HOUR } from './misc'
8
-import { createReadStream, renameSync, unlinkSync } from 'fs'
8
+import { createReadStream, renameSync, unlinkSync, writeFileSync } from 'fs'
9
import { pluginsWatcher } from './plugins'
10
import { chmod, stat } from 'fs/promises'
11
import { Readable } from 'stream'
@@ -153,7 +153,7 @@ export async function update(tagOrUrl: string='') {
153
if (doingLocal)
154
try { renameSync(doingLocal, 'old-' + doingLocal) }
155
catch(e) { console.warn(e) }
156
- launch(newBin, ['--updating', binFile], { sync: true }) // sync necessary to work on Mac by double-click
156
+ launch(newBin, ['--updating', binFile, '--cwd .'], { sync: true }) // sync necessary to work on Mac by double-click
157
})
158
console.log("quitting")
159
setTimeout(() => process.exit()) // give time to return (and caller to complete, eg: rest api to reply)
@@ -177,9 +177,15 @@ if (argv.updating) { // we were launched with a temporary name, restore original
177
// be sure to test launching both double-clicking and in a terminal
178
if (IS_WINDOWS) // this method on Mac works only once, and without console
179
onProcessExit(() =>
180
- launch(dest, ['--updated']) ) // launch+sync here would cause old process to stay open, locking ports
181
- else
180
+ launch(dest, ['--updated', '--cwd .']) ) // launch+sync here would cause old process to stay open, locking ports
181
+ else {
182
+ /* open() is the only consistent way that i could find working on Mac that preserved console input/output over relaunching,
183
+ * but I couldn't find a way to pass parameters, at least on Linux. The workaround I'm using is to write them to a temp file, that's read and deleted at restart.
184
+ * For the record, on mac you can: write "./hfs arg1 arg2" to /tmp/tmp.sh with 0o700, and then spawn "open -a Terminal /tmp/tmp.sh"
185
+ */
186
+ try { writeFileSync(ARGS_FILE, JSON.stringify(['--updated', '--cwd', process.cwd().replaceAll(' ', '\\ ')])) }
187
+ catch {}
188
void open(dest)
183
-
189
+ }
190
process.exit()
191
}