( rootPath: string, options: MergedFolderStructureOptions, )
| 59 | // --- Helper Functions --- |
| 60 | |
| 61 | async function readFullStructure( |
| 62 | rootPath: string, |
| 63 | options: MergedFolderStructureOptions, |
| 64 | ): Promise<FullFolderInfo | null> { |
| 65 | const rootName = path.basename(rootPath); |
| 66 | const rootNode: FullFolderInfo = { |
| 67 | name: rootName, |
| 68 | path: rootPath, |
| 69 | files: [], |
| 70 | subFolders: [], |
| 71 | totalChildren: 0, |
| 72 | totalFiles: 0, |
| 73 | }; |
| 74 | |
| 75 | const queue: Array<{ folderInfo: FullFolderInfo; currentPath: string }> = [ |
| 76 | { folderInfo: rootNode, currentPath: rootPath }, |
| 77 | ]; |
| 78 | let currentItemCount = 0; |
| 79 | // Count the root node itself as one item if we are not just listing its content |
| 80 | |
| 81 | const processedPaths = new Set<string>(); // To avoid processing same path if symlinks create loops |
| 82 | |
| 83 | while (queue.length > 0) { |
| 84 | const { folderInfo, currentPath } = queue.shift()!; |
| 85 | |
| 86 | if (processedPaths.has(currentPath)) { |
| 87 | continue; |
| 88 | } |
| 89 | processedPaths.add(currentPath); |
| 90 | |
| 91 | if (currentItemCount >= options.maxItems) { |
| 92 | // If the root itself caused us to exceed, we can't really show anything. |
| 93 | // Otherwise, this folder won't be processed further. |
| 94 | // The parent that queued this would have set its own hasMoreSubfolders flag. |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | let entries: Dirent[]; |
| 99 | try { |
| 100 | const rawEntries = await fs.readdir(currentPath, { withFileTypes: true }); |
| 101 | // Sort entries alphabetically by name for consistent processing order |
| 102 | entries = rawEntries.sort((a, b) => a.name.localeCompare(b.name)); |
| 103 | } catch (error: unknown) { |
| 104 | if ( |
| 105 | isNodeError(error) && |
| 106 | (error.code === 'EACCES' || error.code === 'ENOENT') |
| 107 | ) { |
| 108 | console.warn( |
| 109 | `Warning: Could not read directory ${currentPath}: ${error.message}`, |
| 110 | ); |
| 111 | if (currentPath === rootPath && error.code === 'ENOENT') { |
| 112 | return null; // Root directory itself not found |
| 113 | } |
| 114 | // For other EACCES/ENOENT on subdirectories, just skip them. |
| 115 | continue; |
| 116 | } |
| 117 | throw error; |
| 118 | } |
no test coverage detected