(filePath: string)
| 9 | * @returns A promise that resolves to an array of newly created directories. |
| 10 | */ |
| 11 | export 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. |
no test coverage detected