From 1a0693e72a47531a538deb2d9a46a42459c2b1af Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 25 Oct 2025 00:34:50 -0500 Subject: [PATCH] Use platform-specific default paths --- electron/main.js | 55 +++++++++++++++++---------------------------- electron/preload.js | 2 +- src/App.tsx | 47 +++++++++++++++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 39 deletions(-) diff --git a/electron/main.js b/electron/main.js index 9dd7a801..bcada125 100644 --- a/electron/main.js +++ b/electron/main.js @@ -9,9 +9,6 @@ import os from 'os'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); -// Shakespeare filesystem root directory -const SHAKESPEARE_ROOT = path.join(os.homedir(), 'shakespeare'); - let mainWindow; function createWindow() { @@ -79,16 +76,6 @@ function createWindow() { }); } -// Helper function to ensure Shakespeare root directory exists -async function ensureShakespeareRoot() { - try { - await fs.mkdir(SHAKESPEARE_ROOT, { recursive: true }); - console.log(`Shakespeare filesystem root: ${SHAKESPEARE_ROOT}`); - } catch (error) { - console.error('Failed to create Shakespeare root directory:', error); - } -} - // Register custom protocol scheme before app is ready (required for privileges) if (!process.env.ELECTRON_DEV) { protocol.registerSchemesAsPrivileged([ @@ -135,9 +122,6 @@ app.whenReady().then(async () => { }); } - // Ensure Shakespeare root directory exists before creating window - await ensureShakespeareRoot(); - createWindow(); app.on('activate', () => { @@ -193,11 +177,12 @@ ipcMain.handle('get-platform', () => { return process.platform; }); -// Helper function to resolve paths relative to Shakespeare root -function resolvePath(relativePath) { - // Normalize the path and remove leading slash if present - const normalized = path.normalize(relativePath).replace(/^[/\\]+/, ''); - return path.join(SHAKESPEARE_ROOT, normalized); +// Helper function to expand tilde (~) in paths +function expandTilde(filepath) { + if (filepath.startsWith('~/') || filepath === '~') { + return path.join(os.homedir(), filepath.slice(1)); + } + return filepath; } // Filesystem IPC handlers @@ -208,7 +193,7 @@ ipcMain.handle('fs:readFile', async (event, filePath, encoding) => { throw err; } try { - const fullPath = resolvePath(filePath); + const fullPath = expandTilde(filePath); const data = await fs.readFile(fullPath, encoding ? { encoding } : undefined); // If no encoding, convert Buffer to array for IPC serialization return encoding ? data : Array.from(data); @@ -236,7 +221,7 @@ ipcMain.handle('fs:writeFile', async (event, filePath, data, encoding) => { throw err; } try { - const fullPath = resolvePath(filePath); + const fullPath = expandTilde(filePath); // Ensure parent directory exists await fs.mkdir(path.dirname(fullPath), { recursive: true }); // Convert array back to Buffer if needed @@ -258,7 +243,7 @@ ipcMain.handle('fs:writeFile', async (event, filePath, data, encoding) => { ipcMain.handle('fs:readdir', async (event, dirPath, withFileTypes) => { try { - const fullPath = resolvePath(dirPath); + const fullPath = expandTilde(dirPath); const entries = await fs.readdir(fullPath, { withFileTypes: !!withFileTypes }); if (withFileTypes) { @@ -288,7 +273,7 @@ ipcMain.handle('fs:readdir', async (event, dirPath, withFileTypes) => { ipcMain.handle('fs:mkdir', async (event, dirPath, recursive) => { try { - const fullPath = resolvePath(dirPath); + const fullPath = expandTilde(dirPath); await fs.mkdir(fullPath, { recursive: !!recursive }); } catch (error) { const err = new Error(`Failed to create directory ${dirPath}: ${error.message}`); @@ -306,7 +291,7 @@ ipcMain.handle('fs:mkdir', async (event, dirPath, recursive) => { ipcMain.handle('fs:stat', async (event, filePath) => { try { - const fullPath = resolvePath(filePath); + const fullPath = expandTilde(filePath); const stats = await fs.stat(fullPath); return { isDirectory: stats.isDirectory(), @@ -342,7 +327,7 @@ ipcMain.handle('fs:stat', async (event, filePath) => { ipcMain.handle('fs:lstat', async (event, filePath) => { try { - const fullPath = resolvePath(filePath); + const fullPath = expandTilde(filePath); const stats = await fs.lstat(fullPath); return { isDirectory: stats.isDirectory(), @@ -377,7 +362,7 @@ ipcMain.handle('fs:lstat', async (event, filePath) => { ipcMain.handle('fs:unlink', async (event, filePath) => { try { - const fullPath = resolvePath(filePath); + const fullPath = expandTilde(filePath); await fs.unlink(fullPath); } catch (error) { const err = new Error(`Failed to unlink ${filePath}: ${error.message}`); @@ -395,7 +380,7 @@ ipcMain.handle('fs:unlink', async (event, filePath) => { ipcMain.handle('fs:rmdir', async (event, dirPath) => { try { - const fullPath = resolvePath(dirPath); + const fullPath = expandTilde(dirPath); await fs.rmdir(fullPath); } catch (error) { // Re-throw with original error code preserved for ENOENT handling @@ -414,8 +399,8 @@ ipcMain.handle('fs:rmdir', async (event, dirPath) => { ipcMain.handle('fs:rename', async (event, oldPath, newPath) => { try { - const fullOldPath = resolvePath(oldPath); - const fullNewPath = resolvePath(newPath); + const fullOldPath = expandTilde(oldPath); + const fullNewPath = expandTilde(newPath); // Ensure parent directory of new path exists await fs.mkdir(path.dirname(fullNewPath), { recursive: true }); await fs.rename(fullOldPath, fullNewPath); @@ -435,7 +420,7 @@ ipcMain.handle('fs:rename', async (event, oldPath, newPath) => { ipcMain.handle('fs:readlink', async (event, filePath) => { try { - const fullPath = resolvePath(filePath); + const fullPath = expandTilde(filePath); return await fs.readlink(fullPath); } catch (error) { const err = new Error(`Failed to read link ${filePath}: ${error.message}`); @@ -453,7 +438,7 @@ ipcMain.handle('fs:readlink', async (event, filePath) => { ipcMain.handle('fs:symlink', async (event, target, filePath) => { try { - const fullPath = resolvePath(filePath); + const fullPath = expandTilde(filePath); // Ensure parent directory exists await fs.mkdir(path.dirname(fullPath), { recursive: true }); await fs.symlink(target, fullPath); @@ -480,8 +465,8 @@ ipcMain.handle('shell:exec', async (event, command, cwd) => { const shellCmd = isWindows ? 'cmd.exe' : '/bin/sh'; const shellArgs = isWindows ? ['/c', command] : ['-c', command]; - // Resolve the working directory relative to Shakespeare root - const workingDir = cwd ? resolvePath(cwd) : SHAKESPEARE_ROOT; + // Expand tilde in working directory if provided, otherwise use home directory + const workingDir = cwd ? expandTilde(cwd) : os.homedir(); // Ensure the working directory exists before spawning try { diff --git a/electron/preload.js b/electron/preload.js index 0178d2dd..5bd6d124 100644 --- a/electron/preload.js +++ b/electron/preload.js @@ -12,7 +12,7 @@ contextBridge.exposeInMainWorld('electron', { // Flag to indicate we're running in Electron isElectron: true, - // Filesystem API - all paths are relative to ~/shakespeare + // Filesystem API - paths support tilde (~) expansion for home directory fs: { readFile: (path, encoding) => ipcRenderer.invoke('fs:readFile', path, encoding), writeFile: (path, data, encoding) => ipcRenderer.invoke('fs:writeFile', path, data, encoding), diff --git a/src/App.tsx b/src/App.tsx index 9299f814..64bc24d0 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -44,6 +44,47 @@ const queryClient = new QueryClient({ }, }); +// Get OS-specific default paths for Electron +function getElectronDefaultPaths(): { fsPathProjects: string; fsPathConfig: string; fsPathTmp: string } { + if (!window.electron?.isElectron) { + // Browser defaults - use virtual filesystem paths + return { + fsPathProjects: "/projects", + fsPathConfig: "/config", + fsPathTmp: "/tmp", + }; + } + + // Electron defaults - use OS-specific paths + // Note: The ~ will be expanded by the Electron main process + const platform = navigator.platform.toLowerCase(); + + if (platform.includes('win')) { + // Windows + return { + fsPathProjects: "~/Documents/Projects", + fsPathConfig: "~/AppData/Local/shakespeare", + fsPathTmp: "~/AppData/Local/Temp/shakespeare", + }; + } else if (platform.includes('mac')) { + // macOS + return { + fsPathProjects: "~/Projects", + fsPathConfig: "~/Library/Application Support/shakespeare", + fsPathTmp: "/tmp/shakespeare", + }; + } else { + // Linux and other Unix-like systems + return { + fsPathProjects: "~/Projects", + fsPathConfig: "~/.config/shakespeare", + fsPathTmp: "/tmp/shakespeare", + }; + } +} + +const electronPaths = getElectronDefaultPaths(); + const defaultConfig: AppConfig = { theme: "system", relayUrl: "wss://relay.ditto.pub", @@ -55,9 +96,9 @@ const defaultConfig: AppConfig = { showcaseEnabled: true, showcaseModerator: "npub1jvnpg4c6ljadf5t6ry0w9q0rnm4mksde87kglkrc993z46c39axsgq89sc", ngitServers: ["git.shakespeare.diy", "relay.ngit.dev"], - fsPathProjects: "/projects", - fsPathConfig: "/config", - fsPathTmp: "/tmp", + fsPathProjects: electronPaths.fsPathProjects, + fsPathConfig: electronPaths.fsPathConfig, + fsPathTmp: electronPaths.fsPathTmp, }; const presetRelays = [