Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 41 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
"smol-toml": "^1.7.0",
"specialist": "^2.0.0",
"tiny-editorconfig": "^1.0.2",
"tiny-readdir": "^2.7.4",
"tiny-readdir-glob": "^1.23.2",
"tiny-readdir": "^3.1.1",
"tiny-readdir-glob": "^2.0.0",
"tiny-spinner": "^2.0.5",
"worktank": "^3.0.2",
"zeptomatch": "^2.1.0",
Expand Down
52 changes: 48 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Copy link
Copy Markdown
Collaborator Author

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 Dirent type


function castArray<T>(value: T | T[]): T[] {
return isArray(value) ? value : [value];
}
Expand Down Expand Up @@ -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"));

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabiospampinato FYI too, it seems the switch to zeptomatch means !./foo/*.ts, ./foo/*.ts, etc used to work and no longer do.

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> {
Expand Down Expand Up @@ -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);
Expand Down
Loading