fix: "C:\program files portable" is confused with "C:\program files", causing settings to be saved to "users" folder

Massimo Melina committed Oct 1, 2024 at 23:38 UTC 220d9be77c29080a3603ca458d3bab31829af05d
2 files changed +11 -7
src/config.ts
+3 -5
@@ -1,6 +1,6 @@
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 { argv, ORIGINAL_CWD, VERSION } from './const'
3 +import { argv, ORIGINAL_CWD, VERSION, CONFIG_FILE } from './const'
4 import { watchLoad } from './watchLoad'
5 import yaml from 'yaml'
6 import _ from 'lodash'
@@ -10,8 +10,6 @@ import { join, resolve } from 'path'
10 import events from './events'
11 import { copyFile, stat } from 'fs/promises'
12
13 -const FILE = 'config.yaml'
14 -
13 // keep definition of config properties
14 const configProps: Record<string, { defaultValue?: unknown }> = {}
15
@@ -19,11 +17,11 @@ let started = false // this will tell the difference for subscribeConfig()s that
17 let state: Record<string, any> = {} // current state of config properties
18 const filePath = with_(argv.config || process.env.HFS_CONFIG, p => {
19 if (!p)
22 - return FILE
20 + return CONFIG_FILE
21 p = resolve(ORIGINAL_CWD, p)
22 try {
23 if (statSync(p).isDirectory()) // try to detect if path points to a folder, in which case we add the standard filename
26 - return join(p, FILE)
24 + return join(p, CONFIG_FILE)
25 }
26 catch {}
27 return p
src/const.ts
+8 -2
@@ -26,6 +26,7 @@ export const IS_MAC = process.platform === 'darwin'
26 export const IS_BINARY = !/node|bun/.test(basename(process.execPath)) // this won't be node if pkg was used
27 export const APP_PATH = dirname(IS_BINARY ? process.execPath : __dirname) // __dirname's parent can be compared with cwd
28 export const MIME_AUTO = 'auto'
29 +export const CONFIG_FILE = 'config.yaml'
30
31 // we want this to be the first stuff to be printed, then we print it in this module, that is executed at the beginning
32 if (DEV) console.clear()
@@ -37,8 +38,7 @@ console.log('version', VERSION||'-')
38 console.log('build', BUILD_TIMESTAMP||'-')
39 const winExe = IS_WINDOWS && process.execPath.match(/(?<!node)\.exe$/i)
40 // still considering whether to use ".hfs" with Windows users, who may be less accustomed to it
40 -const useHomeDir = !winExe || process.execPath.includes(process.env.ProgramFiles||'|') // you can't write in program-files
41 -const dir = argv.cwd || useHomeDir && join(homedir(), '.hfs')
41 +const dir = argv.cwd || useHomeDir() && join(homedir(), '.hfs')
42 if (dir) {
43 try { mkdirSync(dir) }
44 catch(e: any) {
@@ -55,3 +55,9 @@ const bun = (globalThis as any).Bun
55 if (bun) console.log('bun', bun.version)
56 console.log('platform', process.platform, IS_BINARY ? 'binary' : basename(process.execPath))
57 console.log('pid', process.pid)
58 +
59 +function useHomeDir() {
60 + if (!winExe) return true
61 + try { fs.accessSync(join(process.cwd(), CONFIG_FILE), fs.constants.W_OK) }
62 + catch { return true }
63 +}
\ No newline at end of file