-
Notifications
You must be signed in to change notification settings - Fork 27.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: optimize flat-readdir to use
fs.opendir
- Loading branch information
Showing
1 changed file
with
13 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,20 @@ | ||
import { join } from 'path' | ||
import { nonNullable } from './non-nullable' | ||
import { promises } from 'fs' | ||
import fs from 'fs/promises' | ||
|
||
export async function flatReaddir(dir: string, includes: RegExp[]) { | ||
const dirents = await promises.readdir(dir, { withFileTypes: true }) | ||
const result = await Promise.all( | ||
dirents.map(async (part) => { | ||
const absolutePath = join(dir, part.name) | ||
if (part.isSymbolicLink()) { | ||
const stats = await promises.stat(absolutePath) | ||
if (stats.isDirectory()) { | ||
return null | ||
} | ||
} | ||
const dirents = await fs.opendir(dir) | ||
const result = [] | ||
|
||
if ( | ||
part.isDirectory() || | ||
!includes.some((include) => include.test(part.name)) | ||
) { | ||
return null | ||
} | ||
for await (const part of dirents) { | ||
const shouldOmit = | ||
part.isDirectory() || | ||
part.isSymbolicLink() || | ||
!includes.some((include) => include.test(part.name)) | ||
|
||
return absolutePath | ||
}) | ||
) | ||
if (!shouldOmit) { | ||
result.push(join(dir, part.name)) | ||
} | ||
} | ||
|
||
return result.filter(nonNullable) | ||
return result | ||
} |