MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / createDirectoriesForFile

Function createDirectoriesForFile

src/utils/fs.ts:11–32  ·  view source on GitHub ↗
(filePath: string)

Source from the content-addressed store, hash-verified

9 * @returns A promise that resolves to an array of newly created directories.
10 */
11export async function createDirectoriesForFile(filePath: string): Promise<string[]> {
12 const newDirectories: string[] = []
13 const normalizedFilePath = path.normalize(filePath) // Normalize path for cross-platform compatibility
14 const directoryPath = path.dirname(normalizedFilePath)
15
16 let currentPath = directoryPath
17 const dirsToCreate: string[] = []
18
19 // Traverse up the directory tree and collect missing directories
20 while (!(await fileExistsAtPath(currentPath))) {
21 dirsToCreate.push(currentPath)
22 currentPath = path.dirname(currentPath)
23 }
24
25 // Create directories from the topmost missing one down to the target directory
26 for (let i = dirsToCreate.length - 1; i >= 0; i--) {
27 await fs.mkdir(dirsToCreate[i])
28 newDirectories.push(dirsToCreate[i])
29 }
30
31 return newDirectories
32}
33
34/**
35 * Helper function to check if a path exists.

Callers 4

executeMethod · 0.90
handlePartialMethod · 0.90
openMethod · 0.90
saveDirectlyMethod · 0.90

Calls 1

fileExistsAtPathFunction · 0.85

Tested by

no test coverage detected