Something went wrong. Try again.
A local-first note taking app
Something went wrong. Try again.
2.3 kB · 78 lines
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879import type { FsDirent, FilesystemApi } from './types';
export type VaultIpcApi = { showOpenDialog: () => Promise<{ canceled: boolean; filePaths: string[] }>; readdir: (dirPath: string) => Promise<FsDirent[]>; readFile: (filePath: string) => Promise<string>; writeFile: (filePath: string, content: string) => Promise<void>; fileExists: (filePath: string) => Promise<boolean>; mkdir: (dirPath: string) => Promise<void>; rename: (oldPath: string, newPath: string) => Promise<void>; delete: (filePath: string) => Promise<void>; copyFile: (srcPath: string, destPath: string) => Promise<void>;};
export class VaultFilesystemApi implements FilesystemApi { constructor(private ipc: VaultIpcApi) {}
async readdir(dirPath: string): Promise<FsDirent[]> { return this.ipc.readdir(dirPath); }
async readFile(filePath: string): Promise<string> { return this.ipc.readFile(filePath); }
async writeFile(filePath: string, content: string): Promise<void> { return this.ipc.writeFile(filePath, content); }
async fileExists(filePath: string): Promise<boolean> { return this.ipc.fileExists(filePath); }
async mkdir(dirPath: string): Promise<void> { return this.ipc.mkdir(dirPath); }
async rename(oldPath: string, newPath: string): Promise<void> { return this.ipc.rename(oldPath, newPath); }
async deletePath(filePath: string): Promise<void> { return this.ipc.delete(filePath); }
async copyFile(srcPath: string, destPath: string): Promise<void> { return this.ipc.copyFile(srcPath, destPath); }}
/** * Get the basename of a path (last component). * Works with both / and \ separators. */export function basename(filePath: string): string { const parts = filePath.split(/[/\\]/); return parts[parts.length - 1] || '';}
/** * Get the directory name of a path (everything except the last component). * Works with both / and \ separators. */export function dirname(filePath: string): string { const parts = filePath.split(/[/\\]/); parts.pop(); return parts.join('/') || '/';}
/** * Join path segments. */export function join(...segments: string[]): string { return segments .map((s) => s.replace(/\\/g, '/').replace(/\/$/, '')) .filter(Boolean) .join('/');}