Skip to content
Merged
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.8.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
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions,
const cache = shouldCache ? new Cache(cacheVersion, projectPath, getCacheRootPath(rootPath), options, stdout) : undefined;
const prettier = await makePrettier(options, cache);

const concurrency = options.parallel ? (options.parallelWorkers ?? Math.max(1, (os.availableParallelism?.() ?? os.cpus().length) - 1)) : 1;
const concurrency = options.parallel
? options.parallelWorkers > 0
? options.parallelWorkers
: Math.max(1, (os.availableParallelism?.() ?? os.cpus().length) - 1)
: 1;
const filesResults = await mapSettledWithConcurrency(filesPathsTargets, concurrency, async (filePath) => {
const isIgnored = () => (ignoreManual ? ignoreManual(filePath) : getIgnoreResolved(filePath, ignoreNames));
const isCacheable = () => cache?.has(filePath, isIgnored);
Expand Down
59 changes: 53 additions & 6 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,54 @@ async function getFoldersChildrenPaths(foldersPaths: string[]): Promise<string[]
return childrenPaths;
}

function getGlobPaths(rootPath: string, globs: string[], withNodeModules: boolean) {
return readdirGlob(globs, {
const defaultIgnoreGlob = "**/{.git,.sl,.svn,.hg,.DS_Store,Thumbs.db,node_modules}";
const defaultIgnoreGlobIncludingNodeModules = "**/{.git,.sl,.svn,.hg,.DS_Store,Thumbs.db}";

async function getGlobPaths(rootPath: string, globs: string[], withNodeModules: boolean) {
const ignoreGlob = withNodeModules ? defaultIgnoreGlobIncludingNodeModules : defaultIgnoreGlob;
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 user globs so may not
// equal the result files.
const filesFound: string[] = [];
const filesFoundNamesToPaths: Record<string, string[]> = Object.create(null);
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);
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 "./"
// TODO (jg): handle '../' in globs
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: ignoreGlob,
onDirents,
});

return { files: result.files, filesFound, filesFoundNamesToPaths, directoriesFound };
}

async function getModule<T = unknown>(modulePath: string): Promise<T> {
Expand Down Expand Up @@ -248,7 +293,9 @@ async function getTargetsPaths(
const fileName = path.basename(filePath);
targetFiles.push(filePath);
targetFilesNames.push(fileName);
targetFilesNamesToPaths.propertyIsEnumerable(fileName) || (targetFilesNamesToPaths[fileName] = []);
if (!Object.hasOwn(targetFilesNamesToPaths, fileName)) {
targetFilesNamesToPaths[fileName] = [];
}
targetFilesNamesToPaths[fileName].push(filePath);
} else if (fileStats?.isDirectory()) {
targetDirectories.push(filePath);
Expand All @@ -259,11 +306,11 @@ async function getTargetsPaths(

const globResult = await getGlobPaths(rootPath, targetGlobs, withNodeModules);
const globResultFiles = globResult.files;
const globResultFilesFoundNames = [...globResult.filesFoundNames];
const globResultFilesFoundNames = Object.keys(globResult.filesFoundNamesToPaths);

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
35 changes: 35 additions & 0 deletions test/__tests__/parallel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { runCli } from "../utils";

describe("files are processed when parallelism is left at its defaults", () => {
runCli("write", [
"--list-different",
"formatted.js",
"unformatted.js",
"unformatted2.js",
], {
parallel: true,
}).test({
stdout: "unformatted.js\nunformatted2.js",
stderr: "",
status: 1,
write: [],
});
});

describe("files are processed when an explicit worker count is given", () => {
runCli("write", [
"--parallel-workers",
"2",
"--list-different",
"formatted.js",
"unformatted.js",
"unformatted2.js",
], {
parallel: true,
}).test({
stdout: "unformatted.js\nunformatted2.js",
stderr: "",
status: 1,
write: [],
});
});
3 changes: 1 addition & 2 deletions test/__tests__/patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ describe("multiple patterns with a negated pattern, ignores node_modules by defa
});
});

// TODO: Handle leading `./` and `../` in patterns.
describe.skip("multiple patterns with a negated pattern and leading `./`, ignores node_modules by default", () => {
describe("multiple patterns with a negated pattern and leading `./`, ignores node_modules by default", () => {
runCli("patterns", [
"./**/*.js",
"!./directory/**",
Expand Down
2 changes: 1 addition & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ async function runCommand(dir, args, options) {
const fixtures = dir ? await getIsolatedFixtures(dir) : undefined;
const archive = fixtures ? await getArchive(fixtures.path) : undefined;
const cwd = fixtures ? fixtures.path : TESTS_PATH;
const argsForTesting = ["--no-parallel"];
const argsForTesting = options.parallel ? [] : ["--no-parallel"];
const argsWithReplacements = args.map((arg) => arg.replaceAll("$CWD", cwd));
const result = exec("node", [BIN_PATH, ...argsForTesting, ...argsWithReplacements], { cwd, stdio: "pipe" });

Expand Down
Loading