-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add fallbacks for getting documents folder path (#127)
Co-authored-by: Sascha <sascha@dev.com> Co-authored-by: Saschl <noreply@saschl.com>
- Loading branch information
1 parent
2138d38
commit 1ffe6b5
Showing
1 changed file
with
22 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,28 @@ | ||
import getPath from 'platform-folders'; | ||
import * as path from 'path'; | ||
import { Logger } from '@nestjs/common'; | ||
import { homedir } from 'os'; | ||
import { execFileSync } from 'child_process'; | ||
|
||
export const getSimbridgeDir = () => path.join(getPath('documents'), 'FlyByWireSim', 'Simbridge'); | ||
export const getSimbridgeDir = () => { | ||
try { | ||
return path.join(getPath('documents'), 'FlyByWireSim', 'Simbridge'); | ||
} catch (e) { | ||
Logger.warn('Could not get documents path via WinAPI, trying alternate method', e); | ||
} | ||
try { | ||
const output = execFileSync('Powershell.exe', ['-Command', `[System.Environment]::GetFolderPath('MyDocuments')`]); | ||
const documents = output.toString().trim(); | ||
if (!documents) { | ||
throw new Error('Path is empty'); | ||
} | ||
return path.join(documents, 'FlyByWireSim', 'Simbridge'); | ||
} catch (e) { | ||
Logger.warn('Could not get documents path via Powershell, trying to use %USERPROFILE% method', e); | ||
} | ||
|
||
return path.join(homedir(), 'Documents', 'FlyByWireSim', 'Simbridge'); | ||
}; | ||
|
||
//@ts-expect-error pkg only defined when running as exe | ||
export const getExecutablePath = () => (process.pkg ? path.dirname(process.argv[0]) : process.cwd()); |