-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathindex.ts
More file actions
177 lines (152 loc) · 4.23 KB
/
Copy pathindex.ts
File metadata and controls
177 lines (152 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { sep } from "path";
import {
Output,
OnlyCountsOutput,
GroupOutput,
PathsOutput,
Options,
FilterPredicate,
ExcludePredicate,
GlobFunction,
GlobMatcher,
GlobParams,
} from "../types";
import { APIBuilder } from "./api-builder";
import type picomatch from "picomatch";
export class Builder<
TReturnType extends Output = PathsOutput,
TGlobFunction = typeof picomatch,
> {
private readonly globCache: Record<string, GlobMatcher> = {};
private options: Options<TGlobFunction> = {
maxDepth: Infinity,
suppressErrors: true,
pathSeparator: sep,
filters: [],
};
private globFunction?: TGlobFunction;
constructor(options?: Partial<Options<TGlobFunction>>) {
this.options = { ...this.options, ...options };
this.globFunction = this.options.globFunction;
}
group(): Builder<GroupOutput, TGlobFunction> {
this.options.group = true;
return this as Builder<GroupOutput, TGlobFunction>;
}
withPathSeparator(separator: "/" | "\\") {
this.options.pathSeparator = separator;
return this;
}
withBasePath() {
this.options.includeBasePath = true;
return this;
}
withRelativePaths() {
this.options.relativePaths = true;
return this;
}
withDirs() {
this.options.includeDirs = true;
return this;
}
withMaxDepth(depth: number) {
this.options.maxDepth = depth;
return this;
}
withMaxFiles(limit: number) {
this.options.maxFiles = limit;
return this;
}
withFullPaths() {
this.options.resolvePaths = true;
this.options.includeBasePath = true;
return this;
}
withErrors() {
this.options.suppressErrors = false;
return this;
}
withSymlinks({ resolvePaths = true } = {}) {
this.options.resolveSymlinks = true;
this.options.useRealPaths = resolvePaths;
return this.withFullPaths();
}
withAbortSignal(signal: AbortSignal) {
this.options.signal = signal;
return this;
}
normalize() {
this.options.normalizePath = true;
return this;
}
filter(predicate: FilterPredicate) {
this.options.filters.push(predicate);
return this;
}
onlyDirs() {
this.options.excludeFiles = true;
this.options.includeDirs = true;
return this;
}
exclude(predicate: ExcludePredicate) {
this.options.exclude = predicate;
return this;
}
onlyCounts(): Builder<OnlyCountsOutput, TGlobFunction> {
this.options.onlyCounts = true;
return this as Builder<OnlyCountsOutput, TGlobFunction>;
}
crawl(root?: string) {
return new APIBuilder<TReturnType>(root || ".", this.options);
}
withGlobFunction<TFunc>(fn: TFunc) {
// cast this since we don't have the new type params yet
this.globFunction = fn as unknown as TGlobFunction;
return this as unknown as Builder<TReturnType, TFunc>;
}
/**
* @deprecated Pass options using the constructor instead:
* ```ts
* new fdir(options).crawl("/path/to/root");
* ```
* This method will be removed in v7.0
*/
/* c8 ignore next 4 */
crawlWithOptions(root: string, options: Partial<Options<TGlobFunction>>) {
this.options = { ...this.options, ...options };
return new APIBuilder<TReturnType>(root || ".", this.options);
}
glob(...patterns: string[]) {
if (this.globFunction) {
return this.globWithOptions(patterns);
}
return this.globWithOptions(
patterns,
...([{ dot: true }] as unknown as GlobParams<TGlobFunction>)
);
}
globWithOptions(patterns: string[]): Builder<TReturnType, TGlobFunction>;
globWithOptions(
patterns: string[],
...options: GlobParams<TGlobFunction>
): Builder<TReturnType, TGlobFunction>;
globWithOptions(
patterns: string[],
...options: GlobParams<TGlobFunction> | []
) {
const globFn = (this.globFunction ||
Builder.defaultGlobFunction) as GlobFunction | null;
/* c8 ignore next 5 */
if (!globFn) {
throw new Error("Please specify a glob function to use glob matching.");
}
var isMatch = this.globCache[patterns.join("\0")];
if (!isMatch) {
isMatch = globFn(patterns, ...options);
this.globCache[patterns.join("\0")] = isMatch;
}
this.options.filters.push((path) => isMatch(path));
return this;
}
static defaultGlobFunction: typeof picomatch | null = null;
}