Skip to content

Commit

Permalink
fix: add fallbacks for getting documents folder path (#127)
Browse files Browse the repository at this point in the history
Co-authored-by: Sascha <sascha@dev.com>
Co-authored-by: Saschl <noreply@saschl.com>
  • Loading branch information
3 people authored Oct 24, 2024
1 parent 2138d38 commit 1ffe6b5
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion apps/server/src/utilities/pathUtil.ts
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());

0 comments on commit 1ffe6b5

Please sign in to comment.