From b0c0b5c9a4287c6f63fcfc0958778d283955ab08 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 30 Oct 2025 18:05:03 -0500 Subject: [PATCH] Expand fs paths with homedir --- electron/electron.d.ts | 5 +++++ electron/preload.js | 4 ++++ src/components/AppProvider.tsx | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/electron/electron.d.ts b/electron/electron.d.ts index ef41da91..dbfa44c7 100644 --- a/electron/electron.d.ts +++ b/electron/electron.d.ts @@ -60,6 +60,11 @@ interface ElectronShellAPI { } interface ElectronAPI { + /** + * Get the user's home directory path. + */ + homedir: string; + /** * Get the current platform. * @returns 'darwin' | 'win32' | 'linux' diff --git a/electron/preload.js b/electron/preload.js index dd554c3c..7c32b7fa 100644 --- a/electron/preload.js +++ b/electron/preload.js @@ -1,8 +1,12 @@ import { contextBridge, ipcRenderer } from 'electron'; +import os from 'node:os'; // Expose protected methods that allow the renderer process to use // ipcRenderer without exposing the entire object contextBridge.exposeInMainWorld('electron', { + // Get the home directory + homedir: os.homedir(), + // Platform information platform: () => ipcRenderer.invoke('get-platform'), diff --git a/src/components/AppProvider.tsx b/src/components/AppProvider.tsx index 0e4b3126..275bdb8e 100644 --- a/src/components/AppProvider.tsx +++ b/src/components/AppProvider.tsx @@ -58,8 +58,14 @@ export function AppProvider(props: AppProviderProps) { setConfig(updater); }; + // Merge default config with stored config const config = { ...defaultConfig, ...rawConfig }; + // Expand filesystem paths + config.fsPathConfig = expandPath(config.fsPathConfig); + config.fsPathProjects = expandPath(config.fsPathProjects); + config.fsPathTmp = expandPath(config.fsPathTmp); + const appContextValue: AppContextType = { config, updateConfig, @@ -127,4 +133,18 @@ function useApplyLanguage(language?: string): void { useEffect(() => { i18n.changeLanguage(language ?? navigator.language); }, [language]); +} + +/** Expand FS paths with the user's home directory, if available. */ +function expandPath(path: string): string { + const homedir = window.electron?.homedir; + if (!homedir) { + return path; + } else if (path.startsWith('~/')) { + return homedir + path.slice(1); + } else if (path === '~') { + return homedir; + } else { + return path; + } } \ No newline at end of file