Expand fs paths with homedir

This commit is contained in:
Alex Gleason committed 2025-10-30 18:05:03 -05:00
1 parent 1bd21f2625
commit b0c0b5c9a4
3 files changed
+29

No files matched your search

+5
View File
@@ -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'
+4
View File
@@ -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'),
+20
View File
@@ -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;
}
}