diff --git a/src/fdir.ts b/src/fdir.ts index 795bbb3..48b3b9a 100644 --- a/src/fdir.ts +++ b/src/fdir.ts @@ -1,9 +1,9 @@ import { posix } from 'node:path'; -import { fdir, type PathsOutput } from 'fdir'; +import { type PathsOutput, fdir } from 'fdir'; import type { APIBuilder } from 'fdir/dist/builder/api-builder'; -import picomatch from "picomatch"; -import type { GlobOptions, InternalProps, PartialMatcherOptions, ProcessedPatterns } from "./types"; -import { getPartialMatcher, log } from "./utils.ts"; +import picomatch from 'picomatch'; +import type { GlobOptions, InternalProps, PartialMatcherOptions, ProcessedPatterns } from './types'; +import { getPartialMatcher, log } from './utils.ts'; // #region getRelativePath // TODO: this is slow, find a better way to do this @@ -35,7 +35,12 @@ export function formatPaths(paths: string[], cwd: string, root: string): string[ // #endregion formatPaths // #region buildFdir -export function buildFdir(options: GlobOptions, props: InternalProps, processed: ProcessedPatterns, cwd: string, root: string): APIBuilder { +export function buildFdir( + options: GlobOptions, + props: InternalProps, + processed: ProcessedPatterns, + cwd: string, + root: string): APIBuilder { const { absolute, debug, followSymbolicLinks, onlyDirectories } = options const nocase = !options.caseSensitiveMatch; @@ -50,14 +55,16 @@ export function buildFdir(options: GlobOptions, props: InternalProps, processed: const partialMatcher = getPartialMatcher(processed.match, partialMatcherOptions); return new fdir({ - filters: [(p, isDirectory) => { - const path = processPath(p, cwd, root, isDirectory, absolute); - const matches = matcher(path); - if (debug && matches) { - log(`matched ${path}`); + filters: [ + (p, isDirectory) => { + const path = processPath(p, cwd, root, isDirectory, absolute); + const matches = matcher(path); + if (debug && matches) { + log(`matched ${path}`); + } + return matches; } - return matches; - }], + ], exclude: (_, p) => { const relativePath = processPath(p, cwd, root, true, true); const skipped = (relativePath !== '.' && !partialMatcher(relativePath)) || ignore(relativePath); diff --git a/src/index.ts b/src/index.ts index 5926c46..50e351f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,14 +1,14 @@ import path, { posix } from 'node:path'; -import { ensureStringArray, escapePath, isDynamicPattern, log, splitPattern } from './utils.ts'; -import type { GlobOptions, Input, InternalProps, ProcessedPatterns } from './types.ts'; import { buildFdir, formatPaths } from './fdir.ts'; +import type { GlobOptions, Input, InternalProps, ProcessedPatterns } from './types.ts'; +import { ensureStringArray, escapePath, isDynamicPattern, log, splitPattern } from './utils.ts'; const PARENT_DIRECTORY = /^(\/?\.\.)+/; const ESCAPING_BACKSLASHES = /\\(?=[()[\]{}!*+?@|])/g; const BACKSLASHES = /\\/g; function normalizePattern(pattern: string, props: InternalProps, opts: GlobOptions, isIgnore: boolean): string { - const cwd = opts.cwd + const cwd = opts.cwd; let result: string = pattern; if (pattern.endsWith('/')) { result = pattern.slice(0, -1); @@ -100,7 +100,7 @@ const defaultOptions = { caseSensitiveMatch: true, followSymbolicLinks: true, onlyFiles: true -} +}; function getOptions(input: Input, options?: Partial): GlobOptions { const opts = { @@ -108,16 +108,16 @@ function getOptions(input: Input, options?: Partial): GlobOptions { ...(Array.isArray(input) || typeof input === 'string' ? { ...options, patterns: input } : input) }; opts.cwd = (opts.cwd ? path.resolve(opts.cwd) : process.cwd()).replace(BACKSLASHES, '/'); - opts.ignore = ensureStringArray(opts.ignore) - opts.patterns = ensureStringArray(opts.patterns) - return opts as GlobOptions + opts.ignore = ensureStringArray(opts.ignore); + opts.patterns = ensureStringArray(opts.patterns); + return opts as GlobOptions; } function crawl(input: Input, options: Partial | undefined, sync: false): Promise; function crawl(input: Input, options: Partial | undefined, sync: true): string[]; function crawl(input: Input, options: Partial | undefined, sync: boolean) { if (input && options?.patterns) { - throw new Error('Cannot pass patterns as both an argument and an option.') + throw new Error('Cannot pass patterns as both an argument and an option.'); } const opts = getOptions(input, options); diff --git a/src/utils.ts b/src/utils.ts index de47935..b9d3e6c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -146,6 +146,6 @@ export function log(...tasks: unknown[]): void { // #region ensureStringArray export function ensureStringArray(value: string | string[]): string[] { - return typeof value === 'string' ? [value] : value + return typeof value === 'string' ? [value] : value; } // #endregion ensureStringArray