1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | exports.Processor = exports.SubWalks = exports.MatchRecord = exports.HasWalkedCache = void 0;
|
5 | const minimatch_1 = require("minimatch");
|
6 |
|
7 |
|
8 |
|
9 | class HasWalkedCache {
|
10 | store;
|
11 | constructor(store = new Map()) {
|
12 | this.store = store;
|
13 | }
|
14 | copy() {
|
15 | return new HasWalkedCache(new Map(this.store));
|
16 | }
|
17 | hasWalked(target, pattern) {
|
18 | return this.store.get(target.fullpath())?.has(pattern.globString());
|
19 | }
|
20 | storeWalked(target, pattern) {
|
21 | const fullpath = target.fullpath();
|
22 | const cached = this.store.get(fullpath);
|
23 | if (cached)
|
24 | cached.add(pattern.globString());
|
25 | else
|
26 | this.store.set(fullpath, new Set([pattern.globString()]));
|
27 | }
|
28 | }
|
29 | exports.HasWalkedCache = HasWalkedCache;
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | class MatchRecord {
|
36 | store = new Map();
|
37 | add(target, absolute, ifDir) {
|
38 | const n = (absolute ? 2 : 0) | (ifDir ? 1 : 0);
|
39 | const current = this.store.get(target);
|
40 | this.store.set(target, current === undefined ? n : n & current);
|
41 | }
|
42 |
|
43 | entries() {
|
44 | return [...this.store.entries()].map(([path, n]) => [
|
45 | path,
|
46 | !!(n & 2),
|
47 | !!(n & 1),
|
48 | ]);
|
49 | }
|
50 | }
|
51 | exports.MatchRecord = MatchRecord;
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | class SubWalks {
|
57 | store = new Map();
|
58 | add(target, pattern) {
|
59 | if (!target.canReaddir()) {
|
60 | return;
|
61 | }
|
62 | const subs = this.store.get(target);
|
63 | if (subs) {
|
64 | if (!subs.find(p => p.globString() === pattern.globString())) {
|
65 | subs.push(pattern);
|
66 | }
|
67 | }
|
68 | else
|
69 | this.store.set(target, [pattern]);
|
70 | }
|
71 | get(target) {
|
72 | const subs = this.store.get(target);
|
73 |
|
74 | if (!subs) {
|
75 | throw new Error('attempting to walk unknown path');
|
76 | }
|
77 |
|
78 | return subs;
|
79 | }
|
80 | entries() {
|
81 | return this.keys().map(k => [k, this.store.get(k)]);
|
82 | }
|
83 | keys() {
|
84 | return [...this.store.keys()].filter(t => t.canReaddir());
|
85 | }
|
86 | }
|
87 | exports.SubWalks = SubWalks;
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | class Processor {
|
95 | hasWalkedCache;
|
96 | matches = new MatchRecord();
|
97 | subwalks = new SubWalks();
|
98 | patterns;
|
99 | follow;
|
100 | dot;
|
101 | opts;
|
102 | constructor(opts, hasWalkedCache) {
|
103 | this.opts = opts;
|
104 | this.follow = !!opts.follow;
|
105 | this.dot = !!opts.dot;
|
106 | this.hasWalkedCache =
|
107 | hasWalkedCache ? hasWalkedCache.copy() : new HasWalkedCache();
|
108 | }
|
109 | processPatterns(target, patterns) {
|
110 | this.patterns = patterns;
|
111 | const processingSet = patterns.map(p => [target, p]);
|
112 |
|
113 |
|
114 | for (let [t, pattern] of processingSet) {
|
115 | this.hasWalkedCache.storeWalked(t, pattern);
|
116 | const root = pattern.root();
|
117 | const absolute = pattern.isAbsolute() && this.opts.absolute !== false;
|
118 |
|
119 | if (root) {
|
120 | t = t.resolve(root === '/' && this.opts.root !== undefined ?
|
121 | this.opts.root
|
122 | : root);
|
123 | const rest = pattern.rest();
|
124 | if (!rest) {
|
125 | this.matches.add(t, true, false);
|
126 | continue;
|
127 | }
|
128 | else {
|
129 | pattern = rest;
|
130 | }
|
131 | }
|
132 | if (t.isENOENT())
|
133 | continue;
|
134 | let p;
|
135 | let rest;
|
136 | let changed = false;
|
137 | while (typeof (p = pattern.pattern()) === 'string' &&
|
138 | (rest = pattern.rest())) {
|
139 | const c = t.resolve(p);
|
140 | t = c;
|
141 | pattern = rest;
|
142 | changed = true;
|
143 | }
|
144 | p = pattern.pattern();
|
145 | rest = pattern.rest();
|
146 | if (changed) {
|
147 | if (this.hasWalkedCache.hasWalked(t, pattern))
|
148 | continue;
|
149 | this.hasWalkedCache.storeWalked(t, pattern);
|
150 | }
|
151 |
|
152 |
|
153 |
|
154 | if (typeof p === 'string') {
|
155 |
|
156 |
|
157 | const ifDir = p === '..' || p === '' || p === '.';
|
158 | this.matches.add(t.resolve(p), absolute, ifDir);
|
159 | continue;
|
160 | }
|
161 | else if (p === minimatch_1.GLOBSTAR) {
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | if (!t.isSymbolicLink() ||
|
168 | this.follow ||
|
169 | pattern.checkFollowGlobstar()) {
|
170 | this.subwalks.add(t, pattern);
|
171 | }
|
172 | const rp = rest?.pattern();
|
173 | const rrest = rest?.rest();
|
174 | if (!rest || ((rp === '' || rp === '.') && !rrest)) {
|
175 |
|
176 |
|
177 | this.matches.add(t, absolute, rp === '' || rp === '.');
|
178 | }
|
179 | else {
|
180 | if (rp === '..') {
|
181 |
|
182 |
|
183 |
|
184 | const tp = t.parent || t;
|
185 |
|
186 | if (!rrest)
|
187 | this.matches.add(tp, absolute, true);
|
188 | else if (!this.hasWalkedCache.hasWalked(tp, rrest)) {
|
189 | this.subwalks.add(tp, rrest);
|
190 | }
|
191 | }
|
192 | }
|
193 | }
|
194 | else if (p instanceof RegExp) {
|
195 | this.subwalks.add(t, pattern);
|
196 | }
|
197 | }
|
198 | return this;
|
199 | }
|
200 | subwalkTargets() {
|
201 | return this.subwalks.keys();
|
202 | }
|
203 | child() {
|
204 | return new Processor(this.opts, this.hasWalkedCache);
|
205 | }
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | filterEntries(parent, entries) {
|
211 | const patterns = this.subwalks.get(parent);
|
212 |
|
213 | const results = this.child();
|
214 | for (const e of entries) {
|
215 | for (const pattern of patterns) {
|
216 | const absolute = pattern.isAbsolute();
|
217 | const p = pattern.pattern();
|
218 | const rest = pattern.rest();
|
219 | if (p === minimatch_1.GLOBSTAR) {
|
220 | results.testGlobstar(e, pattern, rest, absolute);
|
221 | }
|
222 | else if (p instanceof RegExp) {
|
223 | results.testRegExp(e, p, rest, absolute);
|
224 | }
|
225 | else {
|
226 | results.testString(e, p, rest, absolute);
|
227 | }
|
228 | }
|
229 | }
|
230 | return results;
|
231 | }
|
232 | testGlobstar(e, pattern, rest, absolute) {
|
233 | if (this.dot || !e.name.startsWith('.')) {
|
234 | if (!pattern.hasMore()) {
|
235 | this.matches.add(e, absolute, false);
|
236 | }
|
237 | if (e.canReaddir()) {
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 | if (this.follow || !e.isSymbolicLink()) {
|
244 | this.subwalks.add(e, pattern);
|
245 | }
|
246 | else if (e.isSymbolicLink()) {
|
247 | if (rest && pattern.checkFollowGlobstar()) {
|
248 | this.subwalks.add(e, rest);
|
249 | }
|
250 | else if (pattern.markFollowGlobstar()) {
|
251 | this.subwalks.add(e, pattern);
|
252 | }
|
253 | }
|
254 | }
|
255 | }
|
256 |
|
257 |
|
258 | if (rest) {
|
259 | const rp = rest.pattern();
|
260 | if (typeof rp === 'string' &&
|
261 |
|
262 | rp !== '..' &&
|
263 | rp !== '' &&
|
264 | rp !== '.') {
|
265 | this.testString(e, rp, rest.rest(), absolute);
|
266 | }
|
267 | else if (rp === '..') {
|
268 |
|
269 | const ep = e.parent || e;
|
270 |
|
271 | this.subwalks.add(ep, rest);
|
272 | }
|
273 | else if (rp instanceof RegExp) {
|
274 | this.testRegExp(e, rp, rest.rest(), absolute);
|
275 | }
|
276 | }
|
277 | }
|
278 | testRegExp(e, p, rest, absolute) {
|
279 | if (!p.test(e.name))
|
280 | return;
|
281 | if (!rest) {
|
282 | this.matches.add(e, absolute, false);
|
283 | }
|
284 | else {
|
285 | this.subwalks.add(e, rest);
|
286 | }
|
287 | }
|
288 | testString(e, p, rest, absolute) {
|
289 |
|
290 | if (!e.isNamed(p))
|
291 | return;
|
292 | if (!rest) {
|
293 | this.matches.add(e, absolute, false);
|
294 | }
|
295 | else {
|
296 | this.subwalks.add(e, rest);
|
297 | }
|
298 | }
|
299 | }
|
300 | exports.Processor = Processor;
|
301 |
|
\ | No newline at end of file |