| 1 |
import { Entry, MicromatchOptions, EntryFilterFunction, Pattern, PatternRe } from '../../types'; |
| 2 |
import Settings from '../../settings'; |
| 3 |
import * as utils from '../../utils'; |
| 4 |
import PartialMatcher from '../matchers/partial'; |
| 5 |
|
| 6 |
export default class DeepFilter { |
| 7 |
constructor(private readonly _settings: Settings, private readonly _micromatchOptions: MicromatchOptions) { } |
| 8 |
|
| 9 |
public getFilter(basePath: string, positive: Pattern[], negative: Pattern[]): EntryFilterFunction { |
| 10 |
const matcher = this._getMatcher(positive); |
| 11 |
const negativeRe = this._getNegativePatternsRe(negative); |
| 12 |
|
| 13 |
return (entry) => this._filter(basePath, entry, matcher, negativeRe); |
| 14 |
} |
| 15 |
|
| 16 |
private _getMatcher(patterns: Pattern[]): PartialMatcher { |
| 17 |
return new PartialMatcher(patterns, this._settings, this._micromatchOptions); |
| 18 |
} |
| 19 |
|
| 20 |
private _getNegativePatternsRe(patterns: Pattern[]): PatternRe[] { |
| 21 |
const affectDepthOfReadingPatterns = patterns.filter(utils.pattern.isAffectDepthOfReadingPattern); |
| 22 |
|
| 23 |
return utils.pattern.convertPatternsToRe(affectDepthOfReadingPatterns, this._micromatchOptions); |
| 24 |
} |
| 25 |
|
| 26 |
private _filter(basePath: string, entry: Entry, matcher: PartialMatcher, negativeRe: PatternRe[]): boolean { |
| 27 |
if (this._isSkippedByDeep(basePath, entry.path)) { |
| 28 |
return false; |
| 29 |
} |
| 30 |
|
| 31 |
if (this._isSkippedSymbolicLink(entry)) { |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
const filepath = utils.path.removeLeadingDotSegment(entry.path); |
| 36 |
|
| 37 |
if (this._isSkippedByPositivePatterns(filepath, matcher)) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
return this._isSkippedByNegativePatterns(filepath, negativeRe); |
| 42 |
} |
| 43 |
|
| 44 |
private _isSkippedByDeep(basePath: string, entryPath: string): boolean { |
| 45 |
/** |
| 46 |
* Avoid unnecessary depth calculations when it doesn't matter. |
| 47 |
*/ |
| 48 |
if (this._settings.deep === Infinity) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
return this._getEntryLevel(basePath, entryPath) >= this._settings.deep; |
| 53 |
} |
| 54 |
|
| 55 |
private _getEntryLevel(basePath: string, entryPath: string): number { |
| 56 |
const entryPathDepth = entryPath.split('/').length; |
| 57 |
|
| 58 |
if (basePath === '') { |
| 59 |
return entryPathDepth; |
| 60 |
} |
| 61 |
|
| 62 |
const basePathDepth = basePath.split('/').length; |
| 63 |
|
| 64 |
return entryPathDepth - basePathDepth; |
| 65 |
} |
| 66 |
|
| 67 |
private _isSkippedSymbolicLink(entry: Entry): boolean { |
| 68 |
return !this._settings.followSymbolicLinks && entry.dirent.isSymbolicLink(); |
| 69 |
} |
| 70 |
|
| 71 |
private _isSkippedByPositivePatterns(entryPath: string, matcher: PartialMatcher): boolean { |
| 72 |
return !this._settings.baseNameMatch && !matcher.match(entryPath); |
| 73 |
} |
| 74 |
|
| 75 |
private _isSkippedByNegativePatterns(entryPath: string, patternsRe: PatternRe[]): boolean { |
| 76 |
return !utils.pattern.matchAny(entryPath, patternsRe); |
| 77 |
} |
| 78 |
} |