Skip to content
Draft
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
13 changes: 5 additions & 8 deletions __tests__/fdir.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { fdir } from "../src/index";
import fs from "fs";
import fs from "node:fs";
import path, { sep } from "node:path";
import mock from "mock-fs";
import { test, beforeEach, TestContext, vi } from "vitest";
import path, { sep } from "path";
import { convertSlashes } from "../src/utils";
import picomatch from "picomatch";
import { test, beforeEach, TestContext, vi } from "vitest";
import { apiTypes, APITypes, cwd, restricted, root } from "./utils";
import { fdir } from "../src/index";
import { convertSlashes } from "../src/utils";

beforeEach(() => {
mock.restore();
Expand Down Expand Up @@ -381,9 +381,6 @@ for (const type of apiTypes) {
}

test(`[async] crawl directory & use abort signal to abort`, async (t) => {
// AbortController is not present on Node v14
if (!("AbortController" in globalThis)) return;

const totalFiles = new fdir().onlyCounts().crawl("node_modules").sync();
const abortController = new AbortController();
const api = new fdir()
Expand Down
8 changes: 4 additions & 4 deletions __tests__/symlinks.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { afterAll, beforeAll, beforeEach, describe, test } from "vitest";
import { apiTypes, normalize, root } from "./utils";
import path from "node:path";
import mock from "mock-fs";
import { fdir, Options } from "../src";
import path from "path";
import { afterAll, beforeAll, describe, test } from "vitest";
import { apiTypes, normalize, root } from "./utils";
import { fdir } from "../src";

const fsWithRelativeSymlinks = {
"../../sym-relative/linked": {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from "path";
import path from "node:path";

export type APITypes = (typeof apiTypes)[number];
export const apiTypes = ["withPromise", "sync"] as const;
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import recursiveFs from "recursive-fs";
import b from "benny";
import { getAllFilesSync, getAllFiles } from "get-all-files";
import packageJson from "../package.json";
import { readFileSync, readdirSync, writeFileSync } from "fs";
import { readFileSync, readdirSync, writeFileSync } from "node:fs";
import CSV2MD from "csv-to-markdown-table";
import { getSystemInfo } from "./export";
import { readdir } from "fs/promises";
import { readdir } from "node:fs/promises";

async function benchmark() {
const DIRECTORY = "node_modules";
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/fdir-benchmark.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import child_process from "child_process";
import child_process from "node:child_process";
import { Fdir } from "../src/index";
import b from "benny";

Expand All @@ -9,7 +9,7 @@ const syncSuites: ReturnType<(typeof b)["add"]>[] = [];
const asyncSuites: ReturnType<(typeof b)["add"]>[] = [];

function normalizeVersion(version: Version) {
return version.replace(/\./g, "");
return version.replaceAll(".", "");
}

function makeSuite(version: Version) {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,8 @@
"picomatch": {
"optional": true
}
},
"engines": {
"node": ">=16.0.0"
}
}
2 changes: 1 addition & 1 deletion src/api/functions/join-path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { relative } from "path";
import { relative } from "node:path";
import { Options, PathSeparator } from "../../types";
import { convertSlashes } from "../../utils";

Expand Down
4 changes: 2 additions & 2 deletions src/api/functions/push-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export function build(root: string, options: Options): PushDirectoryFunction {
if (!includeDirs) return empty;

if (relativePaths)
return filters && filters.length
return filters?.length
? pushDirectoryFilterWithRelativePath(root)
: pushDirectoryWithRelativePath(root);
return filters && filters.length ? pushDirectoryFilter : pushDirectory;
return filters?.length ? pushDirectoryFilter : pushDirectory;
}
2 changes: 1 addition & 1 deletion src/api/functions/push-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function build(options: Options): PushFileFunction {
const { excludeFiles, filters, onlyCounts } = options;
if (excludeFiles) return empty;

if (filters && filters.length) {
if (filters?.length) {
return onlyCounts ? pushFileFilterAndCount : pushFileFilter;
} else if (onlyCounts) {
return pushFileCount;
Expand Down
4 changes: 2 additions & 2 deletions src/api/functions/resolve-symlink.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "fs";
import fs from "node:fs";
import { dirname } from "node:path";
import { WalkerState, Options } from "../../types";
import { dirname } from "path";

export type ResolveSymlinkFunction = (
path: string,
Expand Down
2 changes: 1 addition & 1 deletion src/api/functions/walk-directory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from "node:fs";
import { WalkerState } from "../../types";
import fs from "fs";

export type WalkDirectoryFunction = (
state: WalkerState,
Expand Down
11 changes: 5 additions & 6 deletions src/api/walker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { basename, dirname } from "path";
import { Dirent } from "node:fs";
import { basename, dirname } from "node:path";
import { isRootDirectory, normalizePath } from "../utils";
import { ResultCallback, WalkerState, Options } from "../types";
import * as joinPath from "./functions/join-path";
Expand All @@ -10,7 +11,6 @@ import * as resolveSymlink from "./functions/resolve-symlink";
import * as invokeCallback from "./functions/invoke-callback";
import * as walkDirectory from "./functions/walk-directory";
import { Queue } from "./queue";
import { Dirent } from "fs";
import { Output } from "../types";
import { Counter } from "./counter";

Expand Down Expand Up @@ -95,7 +95,7 @@ export class Walker<TOutput extends Output> {

if (
controller.signal.aborted ||
(signal && signal.aborted) ||
signal?.aborted ||
(maxFiles && paths.length > maxFiles)
)
return;
Expand All @@ -116,7 +116,7 @@ export class Walker<TOutput extends Output> {
directoryPath,
this.state.options.pathSeparator
);
if (exclude && exclude(entry.name, path)) continue;
if (exclude?.(entry.name, path)) continue;
this.pushDirectory(path, paths, filters);
this.walkDirectory(this.state, path, path, depth - 1, this.walk);
} else if (this.resolveSymlink && entry.isSymbolicLink()) {
Expand All @@ -125,8 +125,7 @@ export class Walker<TOutput extends Output> {
if (stat.isDirectory()) {
resolvedPath = normalizePath(resolvedPath, this.state.options);
if (
exclude &&
exclude(
exclude?.(
entry.name,
useRealPaths ? resolvedPath : path + pathSeparator
)
Expand Down
10 changes: 5 additions & 5 deletions src/builder/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sep } from "path";
import { sep } from "node:path";
import {
Output,
OnlyCountsOutput,
Expand All @@ -12,7 +12,7 @@ import {
} from "../types";
import { APIBuilder } from "./api-builder";
import type picomatch from "picomatch";
import type { Matcher, PicomatchOptions } from "picomatch";
import type { Matcher } from "picomatch";

var pm: typeof picomatch | null = null;
/* c8 ignore next 6 */
Expand Down Expand Up @@ -125,7 +125,7 @@ export class Builder<
}

crawl(root?: string) {
return new APIBuilder<TReturnType>(root || ".", this.options);
return new APIBuilder<TReturnType>(root ?? ".", this.options);
}

withGlobFunction<TFunc>(fn: TFunc) {
Expand All @@ -144,7 +144,7 @@ export class Builder<
/* c8 ignore next 4 */
crawlWithOptions(root: string, options: Partial<Options<TGlobFunction>>) {
this.options = { ...this.options, ...options };
return new APIBuilder<TReturnType>(root || ".", this.options);
return new APIBuilder<TReturnType>(root ?? ".", this.options);
}

glob(...patterns: string[]) {
Expand All @@ -166,7 +166,7 @@ export class Builder<
patterns: string[],
...options: GlobParams<TGlobFunction> | []
) {
const globFn = (this.globFunction || pm) as GlobFunction | null;
const globFn = (this.globFunction ?? pm) as GlobFunction | null;
/* c8 ignore next 5 */
if (!globFn) {
throw new Error("Please specify a glob function to use glob matching.");
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sep, normalize, resolve } from "path";
import { sep, normalize, resolve } from "node:path";
import { PathSeparator } from "./types";

export function cleanPath(path: string) {
Expand All @@ -14,7 +14,7 @@ export function cleanPath(path: string) {

const SLASHES_REGEX = /[\\/]/g;
export function convertSlashes(path: string, separator: PathSeparator) {
return path.replace(SLASHES_REGEX, separator);
return path.replaceAll(SLASHES_REGEX, separator);
Copy link
Owner

Choose a reason for hiding this comment

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

What's the benefit of using replaceAll over replace when using RegExp?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it requires the regex to have the g flag and otherwise throws. useful so that 1) the intentions of replacing more than one entry are clear without needing to look at the regex that could be defined somewhere else 2) it makes forgetting the flag throw an error

}

const WINDOWS_ROOT_DIR_REGEX = /^[a-z]:[\\/]$/i;
Expand Down