-
-
Notifications
You must be signed in to change notification settings - Fork 10
feat: update readdir/readdir-glob #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,13 +11,16 @@ import url from "node:url"; | |
| import resolveTimeout from "promise-resolve-timeout"; | ||
| import { exit } from "specialist"; | ||
| import readdir from "tiny-readdir"; | ||
| import type { Dirent } from "tiny-readdir"; | ||
| import readdirGlob from "tiny-readdir-glob"; | ||
| import zeptomatch from "zeptomatch"; | ||
| import zeptomatchEscape from "zeptomatch-escape"; | ||
| import zeptomatchIsStatic from "zeptomatch-is-static"; | ||
| import type { ContextOptions, FormatOptions, FunctionMaybe, Key, LogLevel, Options, PrettierConfigWithOverrides, PrettierPlugin } from "./types.js"; | ||
| import type { PluginsOptions } from "./types.js"; | ||
|
|
||
| type DirentWithParentPath = Dirent & { parentPath: string }; | ||
|
|
||
| function castArray<T>(value: T | T[]): T[] { | ||
| return isArray(value) ? value : [value]; | ||
| } | ||
|
|
@@ -106,12 +109,53 @@ async function getFoldersChildrenPaths(foldersPaths: string[]): Promise<string[] | |
| return childrenPaths; | ||
| } | ||
|
|
||
| function getGlobPaths(rootPath: string, globs: string[], withNodeModules: boolean) { | ||
| return readdirGlob(globs, { | ||
| async function getGlobPaths(rootPath: string, globs: string[], withNodeModules: boolean) { | ||
| const ignoreGlob = `**/{.git,.sl,.svn,.hg,.DS_Store,Thumbs.db${withNodeModules ? "" : ",node_modules"}}`; | ||
| // we compile this so we can reuse it for `onDirents` and `ignore` | ||
| const ignoreRe = zeptomatch.compile(ignoreGlob); | ||
| const ignore = (targetPath: string): boolean => { | ||
| return ignoreRe.test(path.relative(rootPath, targetPath)); | ||
| }; | ||
|
|
||
| // These are the files and directories that were found during glob traversal. | ||
| // They haven't yet been filtered by the `ignore` function so may not | ||
| // equal the result files. | ||
| const filesFound: string[] = []; | ||
| const filesFoundNames = new Set<string>(); | ||
| const filesFoundNamesToPaths: Record<string, string[]> = {}; | ||
| const directoriesFound: string[] = []; | ||
|
|
||
| const onDirents = (dirents: Dirent[]): undefined => { | ||
| for (const dirent of dirents) { | ||
| const direntName = dirent.name; | ||
| // TODO (jg): remove this cast once tiny-readdir knows about | ||
| // the `parentPath` property on Dirent objects | ||
| const direntPath = fastJoinedPath((dirent as DirentWithParentPath).parentPath, direntName); | ||
| if (ignore(direntPath)) continue; | ||
| if (dirent.isFile()) { | ||
| filesFound.push(direntPath); | ||
| filesFoundNames.add(direntName); | ||
| if (!Object.hasOwn(filesFoundNamesToPaths, direntName)) { | ||
| filesFoundNamesToPaths[direntName] = []; | ||
| } | ||
| filesFoundNamesToPaths[direntName].push(direntPath); | ||
| } else if (dirent.isDirectory()) { | ||
| directoriesFound.push(direntPath); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // Globs are matched against paths relative to the root, which are never prefixed with "./" | ||
| const globsNormalized = globs.map((glob) => glob.replace(/^(!*)(?:\.\/)+/, "$1")); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fabiospampinato FYI too, it seems the switch to zeptomatch means that may be the right thing to do, just flagging. we can work with it here by stripping |
||
|
|
||
| const result = await readdirGlob(globsNormalized, { | ||
| cwd: rootPath, | ||
| followSymlinks: false, | ||
| ignore: `**/{.git,.sl,.svn,.hg,.DS_Store,Thumbs.db${withNodeModules ? "" : ",node_modules"}}`, | ||
| ignore, | ||
| onDirents, | ||
| }); | ||
|
|
||
| return { ...result, filesFound, filesFoundNames, filesFoundNamesToPaths, directoriesFound }; | ||
| } | ||
|
|
||
| async function getModule<T = unknown>(modulePath: string): Promise<T> { | ||
|
|
@@ -263,7 +307,7 @@ async function getTargetsPaths( | |
|
|
||
| const directoriesResults = await Promise.all(targetDirectories.map((targetPath) => getDirectoryPaths(targetPath, withNodeModules))); | ||
| const directoriesResultsFiles = directoriesResults.map((result) => result.files); | ||
| const directoriesResultsFilesFoundNames = directoriesResults.map((result) => [...result.filesNames]); | ||
| const directoriesResultsFilesFoundNames = directoriesResults.map((result) => uniq(result.files.map((filePath) => path.basename(filePath)))); | ||
|
|
||
| const foundFiles = uniqChunks(globResultFiles, ...directoriesResultsFiles); | ||
| const foundFilesNames = uniqChunks(globResultFilesFoundNames, ...directoriesResultsFilesFoundNames); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI @fabiospampinato in case you want to expose this on your
Direnttype