From 5598e0e53815779329b5a64bebed3ff0c0185614 Mon Sep 17 00:00:00 2001 From: Jarvis Date: Thu, 25 Sep 2025 11:44:00 +0200 Subject: [PATCH] feat: add PathUtils helper class for centralized path operations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add ABOUTME comments following guidelines - Implement safe FileSystemAdapter access with proper type checking - Use vault.adapter methods (exists, mkdir, list, remove) - Use normalizePath() consistently for all paths - Match existing patterns (cleanupDir only removes files, returns boolean) - Add user-friendly error messages with Notice - Place in src/core/ alongside other core utilities 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/core/PathUtils.ts | 87 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/core/PathUtils.ts diff --git a/src/core/PathUtils.ts b/src/core/PathUtils.ts new file mode 100644 index 0000000..fa8441e --- /dev/null +++ b/src/core/PathUtils.ts @@ -0,0 +1,87 @@ +// ABOUTME: Centralized path utilities for safe Obsidian API operations +// ABOUTME: Handles vault paths, plugin directories, and file operations + +import { FileSystemAdapter, normalizePath, Notice } from 'obsidian'; +import type { App, PluginManifest } from 'obsidian'; + +export class PathUtils { + constructor(private app: App) {} + + /** + * Get vault base path safely with FileSystemAdapter check + */ + getVaultPath(): string { + const adapter = this.app.vault.adapter; + if (!(adapter instanceof FileSystemAdapter)) { + new Notice('This plugin requires desktop Obsidian'); + throw new Error('FileSystemAdapter not available - desktop Obsidian required'); + } + return adapter.getBasePath(); + } + + /** + * Get plugin directory path + */ + getPluginDir(pluginManifest: PluginManifest): string { + return normalizePath(pluginManifest.dir || ''); + } + + /** + * Join path segments with normalizePath + */ + joinPath(...segments: string[]): string { + // Filter out empty segments + const filtered = segments.filter(s => s); + const joined = filtered.join('/'); + return normalizePath(joined); + } + + /** + * Ensure directory exists, create if necessary + */ + async ensureDir(path: string): Promise { + const normalizedPath = normalizePath(path); + try { + const exists = await this.app.vault.adapter.exists(normalizedPath); + if (!exists) { + await this.app.vault.adapter.mkdir(normalizedPath); + } + } catch (error) { + new Notice(`Failed to create directory: ${normalizedPath}`); + throw error; + } + } + + /** + * Clean up directory contents (files only, like current TempDirectoryManager) + */ + async cleanupDir(path: string): Promise { + const normalizedPath = normalizePath(path); + try { + const exists = await this.app.vault.adapter.exists(normalizedPath); + if (exists && this.app.vault.adapter instanceof FileSystemAdapter) { + const list = await this.app.vault.adapter.list(normalizedPath); + // Remove only files, not subdirectories (matching current pattern) + for (const file of list.files) { + await this.app.vault.adapter.remove(file); + } + } + return true; + } catch (error) { + console.warn(`Failed to cleanup directory ${normalizedPath}:`, error); + return false; + } + } + + /** + * Check if file or directory exists + */ + async fileExists(path: string): Promise { + const normalizedPath = normalizePath(path); + try { + return await this.app.vault.adapter.exists(normalizedPath); + } catch { + return false; + } + } +} \ No newline at end of file