( dirPath: string, includeHidden = false, )
| 166 | * Uses LRU cache to avoid repeated filesystem calls |
| 167 | */ |
| 168 | export async function scanDirectoryForPaths( |
| 169 | dirPath: string, |
| 170 | includeHidden = false, |
| 171 | ): Promise<PathEntry[]> { |
| 172 | const cacheKey = `${dirPath}:${includeHidden}` |
| 173 | const cached = pathCache.get(cacheKey) |
| 174 | if (cached) { |
| 175 | return cached |
| 176 | } |
| 177 | |
| 178 | try { |
| 179 | const fs = getFsImplementation() |
| 180 | const entries = await fs.readdir(dirPath) |
| 181 | |
| 182 | const paths = entries |
| 183 | .filter(entry => includeHidden || !entry.name.startsWith('.')) |
| 184 | .map(entry => ({ |
| 185 | name: entry.name, |
| 186 | path: join(dirPath, entry.name), |
| 187 | type: entry.isDirectory() ? ('directory' as const) : ('file' as const), |
| 188 | })) |
| 189 | .sort((a, b) => { |
| 190 | // Sort directories first, then alphabetically |
| 191 | if (a.type === 'directory' && b.type !== 'directory') return -1 |
| 192 | if (a.type !== 'directory' && b.type === 'directory') return 1 |
| 193 | return a.name.localeCompare(b.name) |
| 194 | }) |
| 195 | .slice(0, 100) |
| 196 | |
| 197 | pathCache.set(cacheKey, paths) |
| 198 | return paths |
| 199 | } catch (error) { |
| 200 | logError(error) |
| 201 | return [] |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Get path completion suggestions for files and directories |
no test coverage detected