@samitouri / QOSami-HFS / commits / a09dd448

fix: if config doesn't exist, exe is not using cwd

Massimo Melina committed Dec 27, 2024 at 19:17 UTC a09dd448419bfc9bdcdd54fcfb9a19d49933eeb1
1 file changed +11 -4
src/const.ts
+11 -4
@@ -47,7 +47,6 @@ console.log('started', formatTimestamp(HFS_STARTED), DEV)
47 console.log('version', VERSION||'-')
48 console.log('build', BUILD_TIMESTAMP||'-')
49 console.debug('arguments', argv)
50 -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) {
@@ -68,7 +67,15 @@ console.log('platform', process.platform, process.arch, IS_BINARY ? 'binary' : b
67 console.log('pid', process.pid)
68
69 function useHomeDir() {
71 - if (!winExe) return true
72 - try { fs.accessSync(join(process.cwd(), CONFIG_FILE), fs.constants.W_OK) }
73 - catch { return true }
70 + if (!IS_WINDOWS || !IS_BINARY) return true
71 + try { fs.accessSync(CONFIG_FILE, fs.constants.W_OK) }
72 + catch(e: any) {
73 + if (e.code !== 'ENOENT')
74 + return true
75 + try {
76 + fs.writeFileSync(CONFIG_FILE, '') // not found, try to create
77 + fs.unlinkSync(CONFIG_FILE)
78 + }
79 + catch { return true }
80 + }
81 }
\ No newline at end of file