Skip to content

Commit

Permalink
Ensuring in most cases (no --ignore-path option used) ignored folders…
Browse files Browse the repository at this point in the history
… are never even entered
  • Loading branch information
fabiospampinato committed Apr 19, 2024
1 parent 6478420 commit 22c8fb6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
24 changes: 23 additions & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"specialist": "^1.4.0",
"tiny-editorconfig": "^1.0.0",
"tiny-jsonc": "^1.0.1",
"tiny-readdir-glob": "^1.22.24",
"tiny-readdir-glob-gitignore": "^1.0.2",
"tiny-spinner": "^2.0.3",
"worktank": "^2.6.1",
"zeptomatch": "^2.0.0",
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ async function runGlobs(options: Options, pluginsOptions: PluginsOptions): Promi

const rootPath = process.cwd();
const projectPath = getProjectPath(rootPath);
const [filesPaths, filesNames, filesNamesToPaths, filesFoundPaths, foldersFoundPaths] = await getTargetsPaths(rootPath, options.globs, options.withNodeModules); // prettier-ignore
const ignoreFiles = options.ignorePath ? [] : [".gitignore", ".prettierignore"];
const [filesPaths, filesNames, filesNamesToPaths, filesFoundPaths, foldersFoundPaths] = await getTargetsPaths(rootPath, options.globs, ignoreFiles, options.withNodeModules); // prettier-ignore
const filesPathsTargets = filesPaths.filter(negate(isBinaryPath)).sort();
const [foldersPathsTargets, foldersExtraPaths] = getExpandedFoldersPaths(foldersFoundPaths, projectPath);
const filesExtraPaths = await getFoldersChildrenPaths([rootPath, ...foldersExtraPaths]);
Expand Down Expand Up @@ -102,7 +103,7 @@ async function runGlobs(options: Options, pluginsOptions: PluginsOptions): Promi
//TODO: Maybe do work in chunks here, as keeping too many formatted files in memory can be a problem
const filesResults = await Promise.allSettled(
filesPathsTargets.map(async (filePath) => {
const isIgnored = () => (ignoreManual ? ignoreManual(filePath) : getIgnoreResolved(filePath, ignoreNames));
const isIgnored = () => !ignoreFiles.length && (ignoreManual ? ignoreManual(filePath) : getIgnoreResolved(filePath, ignoreNames));
const isCacheable = () => cache?.has(filePath, isIgnored);
const ignored = cache ? !(await isCacheable()) : await isIgnored();
if (ignored) return;
Expand Down
9 changes: 6 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { text as stream2text } from "node:stream/consumers";
import url from "node:url";
import resolveTimeout from "promise-resolve-timeout";
import { exit } from "specialist";
import readdir from "tiny-readdir-glob";
import readdir from "tiny-readdir-glob-gitignore";
import zeptomatchEscape from "zeptomatch-escape";
import zeptomatchIsStatic from "zeptomatch-is-static";
import type { ContextOptions, FormatOptions, FunctionMaybe, Key, LogLevel, Options, PrettierConfigWithOverrides, PrettierPlugin } from "./types.js";
Expand Down Expand Up @@ -87,11 +87,13 @@ async function getFoldersChildrenPaths(foldersPaths: string[]): Promise<string[]
return childrenPaths;
}

function getGlobPaths(rootPath: string, globs: string[], withNodeModules: boolean) {
function getGlobPaths(rootPath: string, globs: string[], ignoreFiles: string[], withNodeModules: boolean) {
return readdir(globs, {
cwd: rootPath,
followSymlinks: false,
ignore: `**/{.git,.sl,.svn,.hg,.DS_Store,Thumbs.db${withNodeModules ? "" : ",node_modules"}}`,
ignoreFiles,
ignoreFilesFindAbove: false,
});
}

Expand Down Expand Up @@ -184,6 +186,7 @@ const getStdin = once(async (): Promise<string | undefined> => {
async function getTargetsPaths(
rootPath: string,
globs: string[],
ignoreFiles: string[],
withNodeModules: boolean,
): Promise<[string[], string[], Record<string, string[]>, string[], string[]]> {
const targetFiles: string[] = [];
Expand All @@ -204,7 +207,7 @@ async function getTargetsPaths(
}
}

const result = await getGlobPaths(rootPath, targetGlobs, withNodeModules);
const result = await getGlobPaths(rootPath, targetGlobs, ignoreFiles, withNodeModules);
const filesPaths = [...targetFiles, ...result.files];
const filesNames = [...targetFilesNames, ...result.filesFoundNames];
const filesNamesToPaths = result.filesFoundNamesToPaths;
Expand Down

0 comments on commit 22c8fb6

Please sign in to comment.