-
Notifications
You must be signed in to change notification settings - Fork 101
/
entry.ts
82 lines (60 loc) · 2.7 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
import Settings from '../../settings';
import { Entry, EntryFilterFunction, MicromatchOptions, Pattern, PatternRe } from '../../types';
import * as utils from '../../utils';
export default class EntryFilter {
public readonly index: Map<string, undefined> = new Map();
constructor(private readonly _settings: Settings, private readonly _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);
return (entry) => this._filter(entry, positiveRe, negativeRe);
}
private _filter(entry: Entry, positiveRe: PatternRe[], negativeRe: PatternRe[]): boolean {
if (this._settings.unique && this._isDuplicateEntry(entry)) {
return false;
}
if (this._onlyFileFilter(entry) || this._onlyDirectoryFilter(entry)) {
return false;
}
if (this._isSkippedByAbsoluteNegativePatterns(entry.path, negativeRe)) {
return false;
}
const filepath = this._settings.baseNameMatch ? entry.name : entry.path;
const isDirectory = entry.dirent.isDirectory();
const isMatched = this._isMatchToPatterns(filepath, positiveRe, isDirectory) && !this._isMatchToPatterns(entry.path, negativeRe, isDirectory);
if (this._settings.unique && isMatched) {
this._createIndexRecord(entry);
}
return isMatched;
}
private _isDuplicateEntry(entry: Entry): boolean {
return this.index.has(entry.path);
}
private _createIndexRecord(entry: Entry): void {
this.index.set(entry.path, undefined);
}
private _onlyFileFilter(entry: Entry): boolean {
return this._settings.onlyFiles && !entry.dirent.isFile();
}
private _onlyDirectoryFilter(entry: Entry): boolean {
return this._settings.onlyDirectories && !entry.dirent.isDirectory();
}
private _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);
}
private _isMatchToPatterns(entryPath: string, patternsRe: PatternRe[], isDirectory: boolean): boolean {
const filepath = utils.path.removeLeadingDotSegment(entryPath);
// 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;
}
}