-
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
(#52361)
This pull request replaces `readdir` with `opendir` and simplifies the implementation. Co-authored-by: Steven <[email protected]> Co-authored-by: Tim Neutkens <[email protected]>
- Loading branch information
1 parent
0aeda3a
commit 0f94563
Showing
1 changed file
with
16 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,23 @@ | ||
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) { | ||
let shouldOmit = | ||
part.isDirectory() || !includes.some((include) => include.test(part.name)) | ||
|
||
return absolutePath | ||
}) | ||
) | ||
if (part.isSymbolicLink()) { | ||
const stats = await fs.stat(join(dir, part.name)) | ||
shouldOmit = stats.isDirectory() | ||
} | ||
|
||
return result.filter(nonNullable) | ||
if (!shouldOmit) { | ||
result.push(join(dir, part.name)) | ||
} | ||
} | ||
|
||
return result | ||
} |