Skip to content

Commit cfaead5

Browse files
authored
perf(fmt): use tiny-readdir context (#206)
1 parent 0fa610f commit cfaead5

1 file changed

Lines changed: 6 additions & 15 deletions

File tree

packages/rstack/src/fmt/discoverPaths.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'node:path';
33
import ignore from 'ignore';
44
import isBinaryPath from 'is-binary-path';
55
import micromatch from 'micromatch';
6-
import readdir, { type Dirent } from 'tiny-readdir';
6+
import readdir, { type Dirent, type DirentLike } from 'tiny-readdir';
77

88
const defaultIgnoredDirNames = new Set(['.git', '.sl', '.svn', '.hg', '.jj', 'node_modules']);
99

@@ -45,10 +45,6 @@ const toPosixPath = (filePath: string): string =>
4545
const getDirentParentPath = (dirent: Dirent): string =>
4646
(dirent as Dirent & { parentPath?: string }).parentPath ?? dirent.path;
4747

48-
/** Mirrors the path passed to tiny-readdir's ignore callback. */
49-
const getDirentPath = (dirent: Dirent, parentPath: string): string =>
50-
`${parentPath}${parentPath === path.sep ? '' : path.sep}${dirent.name}`;
51-
5248
const hasBuiltInIgnoredSegment = (
5349
cwd: string,
5450
filePath: string,
@@ -218,18 +214,16 @@ const createTraversalOptions = (
218214
isIncluded?: (filePath: string) => boolean,
219215
isDirectoryIgnored?: (directoryPath: string) => boolean,
220216
) => {
221-
// tiny-readdir passes only a path to `ignore`, so retain the dirent type briefly.
222-
const directories = new Set<string>();
223-
224217
return {
225218
followSymlinks: false,
226-
ignore: (targetPath: string) => {
227-
const isDirectory = directories.delete(targetPath);
228-
if (ignoredDirNames.has(path.basename(targetPath))) {
219+
ignore: (targetPath: string, targetContext: DirentLike) => {
220+
// With symlink following disabled, tiny-readdir always provides a Dirent here.
221+
const dirent = targetContext as Dirent;
222+
if (ignoredDirNames.has(dirent.name)) {
229223
return true;
230224
}
231225

232-
if (isDirectory) {
226+
if (dirent.isDirectory()) {
233227
return gitIgnore.isIgnored(targetPath, true) || isDirectoryIgnored?.(targetPath) === true;
234228
}
235229

@@ -244,9 +238,6 @@ const createTraversalOptions = (
244238
let hasGitIgnore = false;
245239

246240
for (const dirent of dirents) {
247-
if (dirent.isDirectory()) {
248-
directories.add(getDirentPath(dirent, parentPath));
249-
}
250241
if (dirent.name === '.gitignore') {
251242
hasGitIgnore = true;
252243
}

0 commit comments

Comments
 (0)