-
Notifications
You must be signed in to change notification settings - Fork 112
/
entry.ts
91 lines (67 loc) · 2.73 KB
/
entry.ts
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
import * as utils from '../../utils';
import type Settings from '../../settings';
import type { MicromatchOptions, Entry, EntryFilterFunction, Pattern, PatternRe } from '../../types';
export default class EntryFilter {
public readonly index = new Map<string, undefined>();
readonly #settings: Settings;
readonly #micromatchOptions: MicromatchOptions;
constructor(settings: Settings, micromatchOptions: MicromatchOptions) {
this.#settings = settings;
this.#micromatchOptions = micromatchOptions;
}
public getFilter(positive: Pattern[], negative: Pattern[]): EntryFilterFunction {
const positiveRe = utils.pattern.convertPatternsToRe(positive, this.#micromatchOptions);
const negativeRe = utils.pattern.convertPatternsToRe(negative, {
...this.#micromatchOptions,
dot: true,
});
return (entry) => this.#filter(entry, positiveRe, negativeRe);
}
#filter(entry: Entry, positiveRe: PatternRe[], negativeRe: PatternRe[]): boolean {
const filepath = utils.path.removeLeadingDotSegment(entry.path);
if (this.#settings.unique && this.#isDuplicateEntry(filepath)) {
return false;
}
if (this.#onlyFileFilter(entry) || this.#onlyDirectoryFilter(entry)) {
return false;
}
if (this.#isSkippedByAbsoluteNegativePatterns(filepath, negativeRe)) {
return false;
}
const isDirectory = entry.dirent.isDirectory();
const isMatched = this.#isMatchToPatterns(filepath, positiveRe, isDirectory) && !this.#isMatchToPatterns(filepath, negativeRe, isDirectory);
if (this.#settings.unique && isMatched) {
this.#createIndexRecord(filepath);
}
return isMatched;
}
#isDuplicateEntry(filepath: string): boolean {
return this.index.has(filepath);
}
#createIndexRecord(filepath: string): void {
this.index.set(filepath, undefined);
}
#onlyFileFilter(entry: Entry): boolean {
return this.#settings.onlyFiles && !entry.dirent.isFile();
}
#onlyDirectoryFilter(entry: Entry): boolean {
return this.#settings.onlyDirectories && !entry.dirent.isDirectory();
}
#isSkippedByAbsoluteNegativePatterns(entryPath: string, patternsRe: PatternRe[]): boolean {
if (!this.#settings.absolute) {
return false;
}
const fullpath = utils.path.makeAbsolute(this.#settings.cwd, entryPath);
return utils.pattern.matchAny(fullpath, patternsRe);
}
#isMatchToPatterns(filepath: string, patternsRe: PatternRe[], isDirectory: boolean): boolean {
// Trying to match files and directories by patterns.
const isMatched = utils.pattern.matchAny(filepath, patternsRe);
// A pattern with a trailling slash can be used for directory matching.
// To apply such pattern, we need to add a tralling slash to the path.
if (!isMatched && isDirectory) {
return utils.pattern.matchAny(`${filepath}/`, patternsRe);
}
return isMatched;
}
}