Skip to content

Commit

Permalink
fix: if config doesn't exist, exe is not using cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
rejetto committed Dec 29, 2024
1 parent c6f036e commit a09dd44
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ console.log('started', formatTimestamp(HFS_STARTED), DEV)
console.log('version', VERSION||'-')
console.log('build', BUILD_TIMESTAMP||'-')
console.debug('arguments', argv)
const winExe = IS_WINDOWS && process.execPath.match(/(?<!node)\.exe$/i)
// still considering whether to use ".hfs" with Windows users, who may be less accustomed to it
const dir = argv.cwd || useHomeDir() && join(homedir(), '.hfs')
if (dir) {
Expand All @@ -68,7 +67,15 @@ console.log('platform', process.platform, process.arch, IS_BINARY ? 'binary' : b
console.log('pid', process.pid)

function useHomeDir() {
if (!winExe) return true
try { fs.accessSync(join(process.cwd(), CONFIG_FILE), fs.constants.W_OK) }
catch { return true }
if (!IS_WINDOWS || !IS_BINARY) return true
try { fs.accessSync(CONFIG_FILE, fs.constants.W_OK) }
catch(e: any) {
if (e.code !== 'ENOENT')
return true
try {
fs.writeFileSync(CONFIG_FILE, '') // not found, try to create
fs.unlinkSync(CONFIG_FILE)
}
catch { return true }
}
}

0 comments on commit a09dd44

Please sign in to comment.