Skip to content

Commit c7c792b

Browse files
committed
feat: add withGlobFunction method
1 parent 59a80b7 commit c7c792b

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

__tests__/fdir.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import mock from "mock-fs";
44
import { test, beforeEach, TestContext, vi } from "vitest";
55
import path, { sep } from "path";
66
import { convertSlashes } from "../src/utils";
7+
import picomatch from "picomatch";
78

89
beforeEach(() => {
910
mock.restore();
@@ -405,6 +406,28 @@ for (const type of apiTypes) {
405406
t.expect(globFunction).toHaveBeenCalled();
406407
t.expect(files.every((file) => file.endsWith(".js"))).toBeTruthy();
407408
});
409+
410+
test(`[${type}] crawl files that match using a picomatch`, async (t) => {
411+
const globFunction = picomatch;
412+
const api = new fdir({globFunction})
413+
.withBasePath()
414+
.glob("**/*.js")
415+
.crawl("node_modules");
416+
const files = await api[type]();
417+
t.expect(files.every((file) => file.endsWith(".js"))).toBeTruthy();
418+
});
419+
420+
test(`[${type}] using withGlobFunction to set glob`, async (t) => {
421+
const globFunction = vi.fn((glob: string | string[], input: string) => {
422+
return (test: string): boolean => test === input;
423+
});
424+
new fdir()
425+
.withBasePath()
426+
.withGlobFunction(globFunction)
427+
.globWithOptions(["**/*.js"], "bleep")
428+
.crawl("node_modules");
429+
t.expect(globFunction).toHaveBeenCalledWith(["**/*.js"], "bleep");
430+
});
408431
}
409432

410433
test(`[async] crawl directory & use abort signal to abort`, async (t) => {

src/builder/index.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const defaultGlobParams: [PicomatchOptions] = [{dot: true}];
2727

2828
export class Builder<
2929
TReturnType extends Output = PathsOutput,
30-
TGlobFunction extends GlobFunction = typeof picomatch
30+
TGlobFunction = typeof picomatch
3131
> {
3232
private readonly globCache: Record<string, Matcher> = {};
3333
private options: Options<TGlobFunction> = {
@@ -36,9 +36,11 @@ export class Builder<
3636
pathSeparator: sep,
3737
filters: [],
3838
};
39+
private globFunction?: TGlobFunction;
3940

4041
constructor(options?: Partial<Options<TGlobFunction>>) {
4142
this.options = { ...this.options, ...options };
43+
this.globFunction = this.options.globFunction;
4244
}
4345

4446
group(): Builder<GroupOutput, TGlobFunction> {
@@ -127,6 +129,12 @@ export class Builder<
127129
return new APIBuilder<TReturnType>(root || ".", this.options);
128130
}
129131

132+
withGlobFunction<TFunc>(fn: TFunc) {
133+
// cast this since we don't have the new type params yet
134+
this.globFunction = fn as unknown as TGlobFunction;
135+
return this as unknown as Builder<TReturnType, TFunc>;
136+
}
137+
130138
/**
131139
* @deprecated Pass options using the constructor instead:
132140
* ```ts
@@ -141,7 +149,7 @@ export class Builder<
141149
}
142150

143151
glob(...patterns: string[]) {
144-
if (this.options.globFunction) {
152+
if (this.globFunction) {
145153
return this.globWithOptions(patterns);
146154
}
147155
return this.globWithOptions(
@@ -153,11 +161,11 @@ export class Builder<
153161
globWithOptions(patterns: string[]): Builder<TReturnType, TGlobFunction>;
154162
globWithOptions(patterns: string[], ...options: GlobParams<TGlobFunction>): Builder<TReturnType, TGlobFunction>;
155163
globWithOptions(patterns: string[], ...options: GlobParams<TGlobFunction>|[]) {
156-
const globFn = this.options.globFunction || (pm as TGlobFunction|null);
164+
const globFn = (this.globFunction || pm) as GlobFunction | null;
157165
/* c8 ignore next 5 */
158166
if (!globFn) {
159167
throw new Error(
160-
`Please install picomatch: "npm i picomatch" to use glob matching.`
168+
'Please specify a glob function to use glob matching.'
161169
);
162170
}
163171

src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export type ResultCallback<TOutput extends Output> = (
4141
export type FilterPredicate = (path: string, isDirectory: boolean) => boolean;
4242
export type ExcludePredicate = (dirName: string, dirPath: string) => boolean;
4343
export type PathSeparator = "/" | "\\";
44-
export type Options<TGlobFunction extends GlobFunction = GlobFunction> = {
44+
export type Options<TGlobFunction = unknown> = {
4545
includeBasePath?: boolean;
4646
includeDirs?: boolean;
4747
normalizePath?: boolean;
@@ -63,8 +63,8 @@ export type Options<TGlobFunction extends GlobFunction = GlobFunction> = {
6363

6464
export type GlobMatcher = (test: string) => boolean;
6565
export type GlobFunction =
66-
((glob: string | string[], ...params: never[]) => GlobMatcher);
67-
export type GlobParams<T extends GlobFunction> =
66+
((glob: string | string[], ...params: unknown[]) => GlobMatcher);
67+
export type GlobParams<T> =
6868
T extends (globs: string|string[], ...params: infer TParams extends unknown[]) => GlobMatcher
6969
? TParams
7070
: [];

0 commit comments

Comments
 (0)