(vault: Vault, folderPath: string)
| 72 | * Ensures a folder and its parent folders exist |
| 73 | */ |
| 74 | export async function ensureFolderExists(vault: Vault, folderPath: string): Promise<void> { |
| 75 | try { |
| 76 | const normalizedFolderPath = normalizePath(folderPath); |
| 77 | const folders = normalizedFolderPath.split("/").filter((folder) => folder.length > 0); |
| 78 | let currentPath = ""; |
| 79 | |
| 80 | for (const folder of folders) { |
| 81 | currentPath = currentPath ? `${currentPath}/${folder}` : folder; |
| 82 | |
| 83 | // Check on-disk existence via adapter rather than the in-memory |
| 84 | // vault cache, which can be stale during startup or after external |
| 85 | // filesystem changes. |
| 86 | if (await vault.adapter.exists(currentPath)) { |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | try { |
| 91 | await vault.createFolder(currentPath); |
| 92 | } catch { |
| 93 | // Race condition: another call may have created the folder |
| 94 | // between our exists check and createFolder. Only re-throw |
| 95 | // if the folder genuinely doesn't exist. |
| 96 | if (!(await vault.adapter.exists(currentPath))) { |
| 97 | throw new Error(`Failed to create folder "${currentPath}"`); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } catch (error) { |
| 102 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 103 | const stack = error instanceof Error ? error.stack : undefined; |
| 104 | tasknotesLogger.error("Error creating folder structure:", { |
| 105 | category: "internal", |
| 106 | operation: "creating-folder-structure", |
| 107 | details: { stack, folderPath, normalizedPath: normalizePath(folderPath) }, |
| 108 | error: errorMessage, |
| 109 | }); |
| 110 | |
| 111 | // Create enhanced error with preserved context |
| 112 | const enhancedError = new Error(`Failed to create folder "${folderPath}": ${errorMessage}`); |
| 113 | if (stack) { |
| 114 | enhancedError.stack = stack; |
| 115 | } |
| 116 | throw enhancedError; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Calculate duration in minutes between two ISO timestamp strings |
no test coverage detected