1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | const utils = require("../../utils");
|
---|
4 | const partial_1 = require("../matchers/partial");
|
---|
5 | class DeepFilter {
|
---|
6 | constructor(_settings, _micromatchOptions) {
|
---|
7 | this._settings = _settings;
|
---|
8 | this._micromatchOptions = _micromatchOptions;
|
---|
9 | }
|
---|
10 | getFilter(basePath, positive, negative) {
|
---|
11 | const matcher = this._getMatcher(positive);
|
---|
12 | const negativeRe = this._getNegativePatternsRe(negative);
|
---|
13 | return (entry) => this._filter(basePath, entry, matcher, negativeRe);
|
---|
14 | }
|
---|
15 | _getMatcher(patterns) {
|
---|
16 | return new partial_1.default(patterns, this._settings, this._micromatchOptions);
|
---|
17 | }
|
---|
18 | _getNegativePatternsRe(patterns) {
|
---|
19 | const affectDepthOfReadingPatterns = patterns.filter(utils.pattern.isAffectDepthOfReadingPattern);
|
---|
20 | return utils.pattern.convertPatternsToRe(affectDepthOfReadingPatterns, this._micromatchOptions);
|
---|
21 | }
|
---|
22 | _filter(basePath, entry, matcher, negativeRe) {
|
---|
23 | if (this._isSkippedByDeep(basePath, entry.path)) {
|
---|
24 | return false;
|
---|
25 | }
|
---|
26 | if (this._isSkippedSymbolicLink(entry)) {
|
---|
27 | return false;
|
---|
28 | }
|
---|
29 | const filepath = utils.path.removeLeadingDotSegment(entry.path);
|
---|
30 | if (this._isSkippedByPositivePatterns(filepath, matcher)) {
|
---|
31 | return false;
|
---|
32 | }
|
---|
33 | return this._isSkippedByNegativePatterns(filepath, negativeRe);
|
---|
34 | }
|
---|
35 | _isSkippedByDeep(basePath, entryPath) {
|
---|
36 | /**
|
---|
37 | * Avoid unnecessary depth calculations when it doesn't matter.
|
---|
38 | */
|
---|
39 | if (this._settings.deep === Infinity) {
|
---|
40 | return false;
|
---|
41 | }
|
---|
42 | return this._getEntryLevel(basePath, entryPath) >= this._settings.deep;
|
---|
43 | }
|
---|
44 | _getEntryLevel(basePath, entryPath) {
|
---|
45 | const entryPathDepth = entryPath.split('/').length;
|
---|
46 | if (basePath === '') {
|
---|
47 | return entryPathDepth;
|
---|
48 | }
|
---|
49 | const basePathDepth = basePath.split('/').length;
|
---|
50 | return entryPathDepth - basePathDepth;
|
---|
51 | }
|
---|
52 | _isSkippedSymbolicLink(entry) {
|
---|
53 | return !this._settings.followSymbolicLinks && entry.dirent.isSymbolicLink();
|
---|
54 | }
|
---|
55 | _isSkippedByPositivePatterns(entryPath, matcher) {
|
---|
56 | return !this._settings.baseNameMatch && !matcher.match(entryPath);
|
---|
57 | }
|
---|
58 | _isSkippedByNegativePatterns(entryPath, patternsRe) {
|
---|
59 | return !utils.pattern.matchAny(entryPath, patternsRe);
|
---|
60 | }
|
---|
61 | }
|
---|
62 | exports.default = DeepFilter;
|
---|