diff --git a/__tests__/fdir.test.ts b/__tests__/fdir.test.ts index 0cec2c5a..b96454d5 100644 --- a/__tests__/fdir.test.ts +++ b/__tests__/fdir.test.ts @@ -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(); @@ -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() diff --git a/__tests__/symlinks.test.ts b/__tests__/symlinks.test.ts index 0a61ec2e..017dcba4 100644 --- a/__tests__/symlinks.test.ts +++ b/__tests__/symlinks.test.ts @@ -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": { diff --git a/__tests__/utils.ts b/__tests__/utils.ts index f3c7cb02..6d7c31c2 100644 --- a/__tests__/utils.ts +++ b/__tests__/utils.ts @@ -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; diff --git a/benchmarks/benchmark.js b/benchmarks/benchmark.js index 4edfda7f..37a42860 100644 --- a/benchmarks/benchmark.js +++ b/benchmarks/benchmark.js @@ -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"; diff --git a/benchmarks/fdir-benchmark.ts b/benchmarks/fdir-benchmark.ts index bfa6ea99..83208c7b 100644 --- a/benchmarks/fdir-benchmark.ts +++ b/benchmarks/fdir-benchmark.ts @@ -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"; @@ -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) { diff --git a/package.json b/package.json index f7663812..d3265836 100644 --- a/package.json +++ b/package.json @@ -86,5 +86,8 @@ "picomatch": { "optional": true } + }, + "engines": { + "node": ">=16.0.0" } } diff --git a/src/api/functions/join-path.ts b/src/api/functions/join-path.ts index d103cb2c..0ffb8a16 100644 --- a/src/api/functions/join-path.ts +++ b/src/api/functions/join-path.ts @@ -1,4 +1,4 @@ -import { relative } from "path"; +import { relative } from "node:path"; import { Options, PathSeparator } from "../../types"; import { convertSlashes } from "../../utils"; diff --git a/src/api/functions/push-directory.ts b/src/api/functions/push-directory.ts index 2de954b8..8ddf6770 100644 --- a/src/api/functions/push-directory.ts +++ b/src/api/functions/push-directory.ts @@ -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; } diff --git a/src/api/functions/push-file.ts b/src/api/functions/push-file.ts index 18ef77dc..8d08c9e7 100644 --- a/src/api/functions/push-file.ts +++ b/src/api/functions/push-file.ts @@ -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; diff --git a/src/api/functions/resolve-symlink.ts b/src/api/functions/resolve-symlink.ts index e4743a23..0a4748d2 100644 --- a/src/api/functions/resolve-symlink.ts +++ b/src/api/functions/resolve-symlink.ts @@ -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, diff --git a/src/api/functions/walk-directory.ts b/src/api/functions/walk-directory.ts index 9cddb3d6..5b065a34 100644 --- a/src/api/functions/walk-directory.ts +++ b/src/api/functions/walk-directory.ts @@ -1,5 +1,5 @@ +import fs from "node:fs"; import { WalkerState } from "../../types"; -import fs from "fs"; export type WalkDirectoryFunction = ( state: WalkerState, diff --git a/src/api/walker.ts b/src/api/walker.ts index 1f31b130..4c9ac017 100644 --- a/src/api/walker.ts +++ b/src/api/walker.ts @@ -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"; @@ -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"; @@ -95,7 +95,7 @@ export class Walker { if ( controller.signal.aborted || - (signal && signal.aborted) || + signal?.aborted || (maxFiles && paths.length > maxFiles) ) return; @@ -116,7 +116,7 @@ export class Walker { 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()) { @@ -125,8 +125,7 @@ export class Walker { if (stat.isDirectory()) { resolvedPath = normalizePath(resolvedPath, this.state.options); if ( - exclude && - exclude( + exclude?.( entry.name, useRealPaths ? resolvedPath : path + pathSeparator ) diff --git a/src/builder/index.ts b/src/builder/index.ts index 2b64646f..3a1a46c0 100644 --- a/src/builder/index.ts +++ b/src/builder/index.ts @@ -1,4 +1,4 @@ -import { sep } from "path"; +import { sep } from "node:path"; import { Output, OnlyCountsOutput, @@ -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 */ @@ -125,7 +125,7 @@ export class Builder< } crawl(root?: string) { - return new APIBuilder(root || ".", this.options); + return new APIBuilder(root ?? ".", this.options); } withGlobFunction(fn: TFunc) { @@ -144,7 +144,7 @@ export class Builder< /* c8 ignore next 4 */ crawlWithOptions(root: string, options: Partial>) { this.options = { ...this.options, ...options }; - return new APIBuilder(root || ".", this.options); + return new APIBuilder(root ?? ".", this.options); } glob(...patterns: string[]) { @@ -166,7 +166,7 @@ export class Builder< patterns: string[], ...options: GlobParams | [] ) { - 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."); diff --git a/src/utils.ts b/src/utils.ts index 66260cf8..723ae572 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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) { @@ -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); } const WINDOWS_ROOT_DIR_REGEX = /^[a-z]:[\\/]$/i;