Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Torathion committed Feb 20, 2025
1 parent 32a5fce commit 9de3b72
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
31 changes: 19 additions & 12 deletions src/fdir.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<PathsOutput> {
export function buildFdir(
options: GlobOptions,
props: InternalProps,
processed: ProcessedPatterns,
cwd: string,
root: string): APIBuilder<PathsOutput> {
const { absolute, debug, followSymbolicLinks, onlyDirectories } = options
const nocase = !options.caseSensitiveMatch;

Expand All @@ -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);
Expand Down
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -100,24 +100,24 @@ const defaultOptions = {
caseSensitiveMatch: true,
followSymbolicLinks: true,
onlyFiles: true
}
};

function getOptions(input: Input, options?: Partial<GlobOptions>): GlobOptions {
const opts = {
...defaultOptions,
...(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<GlobOptions> | undefined, sync: false): Promise<string[]>;
function crawl(input: Input, options: Partial<GlobOptions> | undefined, sync: true): string[];
function crawl(input: Input, options: Partial<GlobOptions> | 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);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9de3b72

Please sign in to comment.