Remove Electron functionality

This commit is contained in:
Alex Gleason committed 2026-01-01 16:36:35 -06:00
1 parent 16cb47832e
commit f4a3953d4d
17 files changed
+26 -2545

No files matched your search

+8 -61
View File
@@ -23,7 +23,6 @@ import { FSProvider } from '@/components/FSProvider';
import { ConsoleErrorProvider } from '@/components/ConsoleErrorProvider';
import { GitSyncProvider } from '@/components/GitSyncProvider';
import { LightningFSAdapter } from '@/lib/LightningFSAdapter';
import { ElectronFSAdapter } from '@/lib/ElectronFSAdapter';
import { cleanupTmpDirectory } from '@/lib/tmpCleanup';
import { DynamicFavicon } from '@/components/DynamicFavicon';
import { OfflineIndicator } from '@/components/OfflineIndicator';
@@ -48,55 +47,6 @@ const queryClient = new QueryClient({
},
});
// Get OS-specific default paths for Electron
function getElectronDefaultPaths(): { fsPathProjects: string; fsPathConfig: string; fsPathTmp: string; fsPathPlugins: string; fsPathTemplates: string } {
if (!globalThis.electron) {
// Browser defaults - use virtual filesystem paths
return {
fsPathProjects: "/projects",
fsPathConfig: "/config",
fsPathTmp: "/tmp",
fsPathPlugins: "/plugins",
fsPathTemplates: "/templates",
};
}
// 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",
fsPathPlugins: "~/AppData/Local/shakespeare/plugins",
fsPathTemplates: "~/AppData/Local/shakespeare/templates",
};
} else if (platform.includes('mac')) {
// macOS
return {
fsPathProjects: "~/Projects",
fsPathConfig: "~/Library/Application Support/shakespeare",
fsPathTmp: "/tmp/shakespeare",
fsPathPlugins: "~/Library/Application Support/shakespeare/plugins",
fsPathTemplates: "~/Library/Application Support/shakespeare/templates",
};
} else {
// Linux and other Unix-like systems
return {
fsPathProjects: "~/Projects",
fsPathConfig: "~/.config/shakespeare",
fsPathTmp: "/tmp/shakespeare",
fsPathPlugins: "~/.config/shakespeare/plugins",
fsPathTemplates: "~/.config/shakespeare/templates",
};
}
}
const electronPaths = getElectronDefaultPaths();
const defaultConfig: AppConfig = {
theme: "system",
relayMetadata: {
@@ -139,21 +89,18 @@ const defaultConfig: AppConfig = {
previewDomain: "local-shakespeare.dev",
showcaseEnabled: true,
showcaseModerator: "npub1jvnpg4c6ljadf5t6ry0w9q0rnm4mksde87kglkrc993z46c39axsgq89sc",
fsPathProjects: electronPaths.fsPathProjects,
fsPathConfig: electronPaths.fsPathConfig,
fsPathTmp: electronPaths.fsPathTmp,
fsPathPlugins: electronPaths.fsPathPlugins,
fsPathTemplates: electronPaths.fsPathTemplates,
fsPathProjects: "/projects",
fsPathConfig: "/config",
fsPathTmp: "/tmp",
fsPathPlugins: "/plugins",
fsPathTemplates: "/templates",
sentryDsn: import.meta.env.VITE_SENTRY_DSN || "",
sentryEnabled: true,
};
// Initialize filesystem adapter based on environment
// In Electron, use Electron filesystem at ~/shakespeare
// In browser, use LightningFS (IndexedDB-backed virtual filesystem)
const fs = globalThis.electron
? new ElectronFSAdapter()
: new LightningFSAdapter(new LightningFS('shakespeare-fs').promises);
// Initialize filesystem adapter
// Use LightningFS (IndexedDB-backed virtual filesystem)
const fs = new LightningFSAdapter(new LightningFS('shakespeare-fs').promises);
// Component to handle filesystem initialization and cleanup on startup
// Ensures tmp directory exists and removes files older than 1 hour
-21
View File
@@ -90,13 +90,6 @@ export function AppProvider(props: AppProviderProps) {
// 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);
config.fsPathPlugins = expandPath(config.fsPathPlugins);
config.fsPathTemplates = expandPath(config.fsPathTemplates);
const appContextValue: AppContextType = {
config,
defaultConfig,
@@ -164,18 +157,4 @@ 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;
}
}
+14 -42
View File
@@ -104,50 +104,22 @@ export function Terminal({ cwd, className }: TerminalProps) {
return;
}
// Check if running in Electron - use real shell
if (window.electron?.shell) {
// Use the provided cwd (Electron will expand ~ if present)
const result = await window.electron.shell.exec(command, cwd);
// Use virtual shell
if (!shellToolRef.current) return;
// Format output
let output = '';
if (result.stdout) {
output += result.stdout;
}
if (result.stderr) {
if (output) output += '\n';
output += result.stderr;
}
// Add exit code info for non-zero exits
if (result.exitCode !== 0) {
if (output) output += '\n';
output += `Exit code: ${result.exitCode}`;
}
if (output) {
addLine('output', output);
}
// Handle special help command
if (command.trim() === 'help') {
const availableCommands = shellToolRef.current.getAvailableCommands();
const helpText = `Available commands:\n\n${availableCommands.map(cmd =>
`${cmd.name.padEnd(12)} - ${cmd.description}`
).join('\n')}\n\nUse 'which <command>' for more details about a specific command.`;
addLine('output', helpText);
} else {
// Use virtual shell (browser mode)
if (!shellToolRef.current) return;
const result = await shellToolRef.current.execute({ command });
// Handle special help command
if (command.trim() === 'help') {
const availableCommands = shellToolRef.current.getAvailableCommands();
const helpText = `Available commands:\n\n${availableCommands.map(cmd =>
`${cmd.name.padEnd(12)} - ${cmd.description}`
).join('\n')}\n\nUse 'which <command>' for more details about a specific command.`;
addLine('output', helpText);
} else {
const result = await shellToolRef.current.execute({ command });
// Add output
if (result.content && result.content.trim()) {
addLine('output', result.content);
}
// Add output
if (result.content && result.content.trim()) {
addLine('output', result.content);
}
}
} catch (error) {
@@ -155,7 +127,7 @@ export function Terminal({ cwd, className }: TerminalProps) {
} finally {
setIsExecuting(false);
}
}, [addLine, commandHistory, cwd]);
}, [addLine, commandHistory]);
const handleSubmit = useCallback((e: React.FormEvent) => {
e.preventDefault();
-397
View File
@@ -1,397 +0,0 @@
import type { JSRuntimeFS, DirectoryEntry } from './JSRuntime';
/**
* Electron filesystem adapter that implements JSRuntimeFS interface
* This adapter communicates with Electron's main process via IPC to perform
* filesystem operations on the actual OS filesystem.
*
* Note: This adapter should only be instantiated when running in Electron.
*/
export class ElectronFSAdapter implements JSRuntimeFS {
// Cache for stat results to avoid repeated IPC calls
private statCache = new Map<string, { result: Awaited<ReturnType<typeof this.electron.fs.stat>>; timestamp: number }>();
private readonly STAT_CACHE_TTL = 1000; // 1 second TTL for stat cache
// Cache for ENOENT results to avoid repeated failed reads (especially for git objects)
private enoentCache = new Set<string>();
private readonly ENOENT_CACHE_MAX_SIZE = 5000;
private get electron() {
if (!window.electron) {
throw new Error('ElectronFSAdapter can only be used in Electron environment');
}
return window.electron;
}
/**
* Unwrap Electron IPC errors to preserve error codes.
* Electron wraps errors in "Error invoking remote method..." messages,
* but we need to preserve the original error code for isomorphic-git.
*/
private unwrapElectronError(error: unknown): Error {
if (!(error instanceof Error)) {
return error as Error;
}
// Extract error code from the error object if it exists
const code = (error as NodeJS.ErrnoException).code;
// If there's already a code, preserve it
if (code) {
const err = new Error(error.message);
(err as NodeJS.ErrnoException).code = code;
return err;
}
// If no code property, try to extract from the message
// Messages look like: "Error invoking remote method 'fs:stat': Error: Failed to stat /path: ENOENT: no such file or directory"
const match = error.message.match(/:\s*(E[A-Z]+):/);
if (match) {
const err = new Error(error.message);
(err as NodeJS.ErrnoException).code = match[1];
return err;
}
return error;
}
async readFile(path: string): Promise<Uint8Array<ArrayBufferLike>>;
async readFile(path: string, options: 'utf8'): Promise<string>;
async readFile(path: string, options: string): Promise<string>;
async readFile(path: string, options: { encoding: 'utf8' }): Promise<string>;
async readFile(path: string, options: { encoding: string }): Promise<string>;
async readFile(path: string, options?: string | { encoding?: string }): Promise<string | Uint8Array<ArrayBufferLike>>;
async readFile(path: string, options?: string | { encoding?: string }): Promise<string | Uint8Array<ArrayBufferLike>> {
try {
// Validate path is not empty (isomorphic-git sometimes passes empty strings during ref resolution)
if (!path || path.trim() === '') {
const err = new Error(`EINVAL: invalid path ''`);
(err as NodeJS.ErrnoException).code = 'EINVAL';
throw err;
}
// Check if we've already seen ENOENT for this path (common for git packed objects)
if (this.enoentCache.has(path)) {
const err = new Error(`ENOENT: no such file or directory, open '${path}'`);
(err as NodeJS.ErrnoException).code = 'ENOENT';
throw err;
}
const encoding = typeof options === 'string' ? options : options?.encoding;
const result = await this.electron.fs.readFile(path, encoding);
// If no encoding, convert array to Uint8Array
if (!encoding && Array.isArray(result)) {
return new Uint8Array(result);
}
return result as string;
} catch (error) {
const unwrapped = this.unwrapElectronError(error);
// Cache ENOENT errors to avoid repeated IPC calls for missing files
// This is especially important for git packed objects that isomorphic-git tries to read
if ((unwrapped as NodeJS.ErrnoException).code === 'ENOENT') {
this.enoentCache.add(path);
// Limit cache size to prevent memory leaks
if (this.enoentCache.size > this.ENOENT_CACHE_MAX_SIZE) {
// Clear oldest entries (first 1000)
const entries = Array.from(this.enoentCache);
entries.slice(0, 1000).forEach(entry => this.enoentCache.delete(entry));
}
}
throw unwrapped;
}
}
async writeFile(path: string, data: string | Uint8Array<ArrayBufferLike>, options?: string | { encoding?: string }): Promise<void> {
try {
const encoding = typeof options === 'string' ? options : options?.encoding;
// Convert Uint8Array to regular array for IPC serialization
const dataToWrite = data instanceof Uint8Array ? Array.from(data) : data;
const result = await this.electron.fs.writeFile(path, dataToWrite, encoding);
// Invalidate caches for this path and its parent directory
this.statCache.delete(path);
this.statCache.delete(`lstat:${path}`);
this.enoentCache.delete(path); // File now exists
const parentDir = path.substring(0, path.lastIndexOf('/'));
if (parentDir) {
this.statCache.delete(parentDir);
this.statCache.delete(`lstat:${parentDir}`);
}
return result;
} catch (error) {
throw this.unwrapElectronError(error);
}
}
async readdir(path: string): Promise<string[]>;
async readdir(path: string, options: { withFileTypes: true }): Promise<DirectoryEntry[]>;
async readdir(path: string, options?: { withFileTypes?: boolean }): Promise<string[] | DirectoryEntry[]>;
async readdir(path: string, options?: { withFileTypes?: boolean }): Promise<string[] | DirectoryEntry[]> {
try {
// Fast path: check if this is a file first (common case causing errors)
// Use cached stat if available to avoid extra IPC call
const cached = this.statCache.get(path);
if (cached && Date.now() - cached.timestamp < this.STAT_CACHE_TTL) {
if (cached.result.isFile) {
// This is a file, not a directory - throw ENOTDIR immediately
const err = new Error(`ENOTDIR: not a directory, scandir '${path}'`);
(err as NodeJS.ErrnoException).code = 'ENOTDIR';
throw err;
}
}
const result = await this.electron.fs.readdir(path, options?.withFileTypes);
if (options?.withFileTypes) {
// Convert plain objects to DirectoryEntry format
return (result as Array<{ name: string; isDirectory: boolean; isFile: boolean }>).map(entry => ({
name: entry.name,
isDirectory: () => entry.isDirectory,
isFile: () => entry.isFile,
}));
}
return result as string[];
} catch (error) {
throw this.unwrapElectronError(error);
}
}
async mkdir(path: string, options?: { recursive?: boolean }): Promise<void> {
try {
// Default to recursive: true for isomorphic-git compatibility
// isomorphic-git expects mkdir to behave like 'mkdir -p'
const recursive = options?.recursive !== false;
const result = await this.electron.fs.mkdir(path, recursive);
// Invalidate cache for this path and parent
this.statCache.delete(path);
this.statCache.delete(`lstat:${path}`);
const parentDir = path.substring(0, path.lastIndexOf('/'));
if (parentDir) {
this.statCache.delete(parentDir);
this.statCache.delete(`lstat:${parentDir}`);
}
return result;
} catch (error) {
throw this.unwrapElectronError(error);
}
}
async stat(path: string): Promise<{
isDirectory(): boolean;
isFile(): boolean;
isBlockDevice(): boolean;
isCharacterDevice(): boolean;
isSymbolicLink(): boolean;
isFIFO(): boolean;
isSocket(): boolean;
size: number;
mtimeMs: number;
ctimeMs: number;
atimeMs: number;
mtime: Date;
ctime: Date;
atime: Date;
}> {
try {
// Check cache first
const cached = this.statCache.get(path);
if (cached && Date.now() - cached.timestamp < this.STAT_CACHE_TTL) {
return cached.result;
}
const result = await this.electron.fs.stat(path);
const now = Date.now();
const statResult = {
isDirectory: () => result.isDirectory,
isFile: () => result.isFile,
isBlockDevice: () => result.isBlockDevice,
isCharacterDevice: () => result.isCharacterDevice,
isSymbolicLink: () => result.isSymbolicLink,
isFIFO: () => result.isFIFO,
isSocket: () => result.isSocket,
size: result.size ?? 0,
mtimeMs: result.mtimeMs ?? now,
ctimeMs: result.ctimeMs ?? now,
atimeMs: result.atimeMs ?? now,
// Convert timestamps back to Date objects
mtime: new Date(result.mtime ?? now),
ctime: new Date(result.ctime ?? now),
atime: new Date(result.atime ?? now),
};
// Cache the result
this.statCache.set(path, { result: statResult, timestamp: Date.now() });
// Limit cache size to prevent memory leaks
if (this.statCache.size > 10000) {
// Remove oldest entries (first 1000)
const entries = Array.from(this.statCache.entries());
entries.slice(0, 1000).forEach(([key]) => this.statCache.delete(key));
}
return statResult;
} catch (error) {
// Electron IPC wraps errors, extract the original error code
throw this.unwrapElectronError(error);
}
}
async lstat(path: string): Promise<{
isDirectory(): boolean;
isFile(): boolean;
isBlockDevice(): boolean;
isCharacterDevice(): boolean;
isSymbolicLink(): boolean;
isFIFO(): boolean;
isSocket(): boolean;
size: number;
mtimeMs: number;
ctimeMs: number;
atimeMs: number;
mtime: Date;
ctime: Date;
atime: Date;
}> {
try {
// Check cache first (use separate cache key for lstat)
const cacheKey = `lstat:${path}`;
const cached = this.statCache.get(cacheKey);
if (cached && Date.now() - cached.timestamp < this.STAT_CACHE_TTL) {
return cached.result;
}
const result = await this.electron.fs.lstat(path);
const now = Date.now();
const statResult = {
isDirectory: () => result.isDirectory,
isFile: () => result.isFile,
isBlockDevice: () => result.isBlockDevice,
isCharacterDevice: () => result.isCharacterDevice,
isSymbolicLink: () => result.isSymbolicLink,
isFIFO: () => result.isFIFO,
isSocket: () => result.isSocket,
size: result.size ?? 0,
mtimeMs: result.mtimeMs ?? now,
ctimeMs: result.ctimeMs ?? now,
atimeMs: result.atimeMs ?? now,
// Convert timestamps back to Date objects
mtime: new Date(result.mtime ?? now),
ctime: new Date(result.ctime ?? now),
atime: new Date(result.atime ?? now),
};
// Cache the result
this.statCache.set(cacheKey, { result: statResult, timestamp: Date.now() });
// Limit cache size to prevent memory leaks
if (this.statCache.size > 10000) {
// Remove oldest entries (first 1000)
const entries = Array.from(this.statCache.entries());
entries.slice(0, 1000).forEach(([key]) => this.statCache.delete(key));
}
return statResult;
} catch (error) {
throw this.unwrapElectronError(error);
}
}
async unlink(path: string): Promise<void> {
try {
const result = await this.electron.fs.unlink(path);
// Invalidate cache for this path and parent
this.statCache.delete(path);
this.statCache.delete(`lstat:${path}`);
const parentDir = path.substring(0, path.lastIndexOf('/'));
if (parentDir) {
this.statCache.delete(parentDir);
this.statCache.delete(`lstat:${parentDir}`);
}
return result;
} catch (error) {
throw this.unwrapElectronError(error);
}
}
async rmdir(path: string): Promise<void> {
try {
const result = await this.electron.fs.rmdir(path);
// Invalidate cache for this path and parent
this.statCache.delete(path);
this.statCache.delete(`lstat:${path}`);
const parentDir = path.substring(0, path.lastIndexOf('/'));
if (parentDir) {
this.statCache.delete(parentDir);
this.statCache.delete(`lstat:${parentDir}`);
}
return result;
} catch (error) {
throw this.unwrapElectronError(error);
}
}
async rename(oldPath: string, newPath: string): Promise<void> {
try {
const result = await this.electron.fs.rename(oldPath, newPath);
// Invalidate cache for both paths and their parents
this.statCache.delete(oldPath);
this.statCache.delete(`lstat:${oldPath}`);
this.statCache.delete(newPath);
this.statCache.delete(`lstat:${newPath}`);
const oldParent = oldPath.substring(0, oldPath.lastIndexOf('/'));
if (oldParent) {
this.statCache.delete(oldParent);
this.statCache.delete(`lstat:${oldParent}`);
}
const newParent = newPath.substring(0, newPath.lastIndexOf('/'));
if (newParent) {
this.statCache.delete(newParent);
this.statCache.delete(`lstat:${newParent}`);
}
return result;
} catch (error) {
throw this.unwrapElectronError(error);
}
}
async readlink(path: string): Promise<string> {
try {
return await this.electron.fs.readlink(path);
} catch (error) {
throw this.unwrapElectronError(error);
}
}
async symlink(target: string, path: string): Promise<void> {
try {
return await this.electron.fs.symlink(target, path);
} catch (error) {
throw this.unwrapElectronError(error);
}
}
/**
* Clear all caches. Useful when switching projects or after major filesystem changes.
*/
clearCaches(): void {
this.statCache.clear();
this.enoentCache.clear();
}
}
-49
View File
@@ -3,7 +3,6 @@ import type { Tool, ToolResult } from "./Tool";
import type { JSRuntimeFS } from "../JSRuntime";
import type { ShellCommand } from "../commands/ShellCommand";
import { Git } from "../git";
import { stripAnsiCodes } from "../ansiToHtml";
import type { NostrSigner } from "@nostrify/nostrify";
import {
CatCommand,
@@ -330,12 +329,6 @@ export class ShellTool implements Tool<ShellToolParams> {
return { content: "Error: Empty command" };
}
// Check if running in Electron - if so, use real shell execution
if (window.electron?.shell) {
const content = await this.executeRealShell(commandStr);
return { content };
}
// Parse compound commands
const commands = this.parseCompoundCommand(commandStr);
@@ -354,48 +347,6 @@ export class ShellTool implements Tool<ShellToolParams> {
return { content };
}
/**
* Execute command using real shell in Electron
*/
private async executeRealShell(commandStr: string): Promise<string> {
if (!window.electron?.shell) {
throw new Error('Real shell execution is only available in Electron');
}
try {
// In Electron, use the full cwd path (with ~ if present)
// The Electron main process will expand ~ to the home directory
const result = await window.electron.shell.exec(commandStr, this.cwd);
// Strip ANSI codes from output (color codes, cursor movements, etc.)
// This is important for AI consumption and consistent output
const cleanStdout = result.stdout ? stripAnsiCodes(result.stdout) : '';
const cleanStderr = result.stderr ? stripAnsiCodes(result.stderr) : '';
// Format output
let output = '';
if (cleanStdout) {
output += cleanStdout;
}
if (cleanStderr) {
if (output) output += '\n';
output += cleanStderr;
}
// Add exit code info for non-zero exits
if (result.exitCode !== 0) {
if (output) output += '\n';
output += `Exit code: ${result.exitCode}`;
}
return output || '(no output)';
} catch (error) {
return `Error: ${error instanceof Error ? error.message : 'Unknown error'}`;
}
}
/**
* Execute a simple command (no compound operators)
*/