From 65d5ab696dfff46b98a81a3fd2a4557eb72ee4dd Mon Sep 17 00:00:00 2001 From: Saschl <19493808+Saschl@users.noreply.github.com> Date: Thu, 24 Oct 2024 10:06:20 +0200 Subject: [PATCH 1/3] fix: add fallbacks for home folder determination --- apps/server/src/utilities/pathUtil.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/apps/server/src/utilities/pathUtil.ts b/apps/server/src/utilities/pathUtil.ts index 1b7cf5dd..5f170f19 100644 --- a/apps/server/src/utilities/pathUtil.ts +++ b/apps/server/src/utilities/pathUtil.ts @@ -1,7 +1,31 @@ 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"); + } + try { + const { stdout } = execFileSync("Powershell.exe", [ + "-Command", + `[System.Environment]::GetFolderPath('MyDocuments')`, + ]); + const path = stdout.trim(); + if(!path) { + throw new Error("Path is empty"); + } + return path; + } catch (error) { + Logger.warn("Could not get documents path via Powershell, trying to use %USERPROFILE% method"); + } + + return path.join(homedir(), "Documents"); +} //@ts-expect-error pkg only defined when running as exe export const getExecutablePath = () => (process.pkg ? path.dirname(process.argv[0]) : process.cwd()); From 07921fd8114d99086be28c991afbafbb47068f4e Mon Sep 17 00:00:00 2001 From: Sascha Date: Thu, 24 Oct 2024 11:24:48 +0200 Subject: [PATCH 2/3] format --- apps/server/src/utilities/pathUtil.ts | 33 ++++++++++++--------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/apps/server/src/utilities/pathUtil.ts b/apps/server/src/utilities/pathUtil.ts index 5f170f19..2283e5d6 100644 --- a/apps/server/src/utilities/pathUtil.ts +++ b/apps/server/src/utilities/pathUtil.ts @@ -2,30 +2,27 @@ import getPath from 'platform-folders'; import * as path from 'path'; import { Logger } from '@nestjs/common'; import { homedir } from 'os'; -import { execFileSync } from "child_process"; +import { execFileSync } from 'child_process'; 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"); - } - try { - const { stdout } = execFileSync("Powershell.exe", [ - "-Command", - `[System.Environment]::GetFolderPath('MyDocuments')`, - ]); - const path = stdout.trim(); - if(!path) { - throw new Error("Path is empty"); + try { + return path.join(getPath('documents'), 'FlyByWireSim', 'Simbridge'); + } catch (e) { + Logger.warn('Could not get documents path via WinAPI, trying alternate method'); + } + 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; + return path.join(documents, 'FlyByWireSim', 'Simbridge'); } catch (error) { - Logger.warn("Could not get documents path via Powershell, trying to use %USERPROFILE% method"); + Logger.warn('Could not get documents path via Powershell, trying to use %USERPROFILE% method'); } - return path.join(homedir(), "Documents"); -} + 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()); From a32530539516b36f68b3bc11889e004c7c66ba30 Mon Sep 17 00:00:00 2001 From: Saschl Date: Thu, 24 Oct 2024 13:13:41 +0200 Subject: [PATCH 3/3] log exception --- apps/server/src/utilities/pathUtil.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/server/src/utilities/pathUtil.ts b/apps/server/src/utilities/pathUtil.ts index 2283e5d6..f0877264 100644 --- a/apps/server/src/utilities/pathUtil.ts +++ b/apps/server/src/utilities/pathUtil.ts @@ -8,7 +8,7 @@ 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'); + Logger.warn('Could not get documents path via WinAPI, trying alternate method', e); } try { const output = execFileSync('Powershell.exe', ['-Command', `[System.Environment]::GetFolderPath('MyDocuments')`]); @@ -17,8 +17,8 @@ export const getSimbridgeDir = () => { throw new Error('Path is empty'); } return path.join(documents, 'FlyByWireSim', 'Simbridge'); - } catch (error) { - Logger.warn('Could not get documents path via Powershell, trying to use %USERPROFILE% method'); + } catch (e) { + Logger.warn('Could not get documents path via Powershell, trying to use %USERPROFILE% method', e); } return path.join(homedir(), 'Documents', 'FlyByWireSim', 'Simbridge');