( options: CrawlFileSystemOptions<T>, )
| 75 | fileTransform?: (filePath: string) => Promise<T> | T; |
| 76 | }; |
| 77 | export async function crawlFileSystem<T = string>( |
| 78 | options: CrawlFileSystemOptions<T>, |
| 79 | ): Promise<T[]> { |
| 80 | const { |
| 81 | directory, |
| 82 | pattern, |
| 83 | fileTransform = (filePath: string) => filePath as T, |
| 84 | } = options; |
| 85 | |
| 86 | const files = await readdir(directory); |
| 87 | const promises = files.map(async (file): Promise<T | T[]> => { |
| 88 | const filePath = path.join(directory, file); |
| 89 | const stats = await stat(filePath); |
| 90 | |
| 91 | if (stats.isDirectory()) { |
| 92 | return crawlFileSystem({ directory: filePath, pattern, fileTransform }); |
| 93 | } |
| 94 | if (stats.isFile() && (!pattern || new RegExp(pattern).test(file))) { |
| 95 | return fileTransform(filePath); |
| 96 | } |
| 97 | return []; |
| 98 | }); |
| 99 | |
| 100 | const resultsNestedArray = await Promise.all(promises); |
| 101 | return resultsNestedArray.flat() as T[]; |
| 102 | } |
| 103 | |
| 104 | export async function findNearestFile( |
| 105 | fileNames: string[], |
no outgoing calls
no test coverage detected