"use strict";
|
Object.defineProperty(exports, "__esModule", { value: true });
|
const utils = require("../../utils");
|
class EntryFilter {
|
constructor(_settings, _micromatchOptions) {
|
this._settings = _settings;
|
this._micromatchOptions = _micromatchOptions;
|
this.index = new Map();
|
}
|
getFilter(positive, negative) {
|
const [absoluteNegative, relativeNegative] = utils.pattern.partitionAbsoluteAndRelative(negative);
|
const patterns = {
|
positive: {
|
all: utils.pattern.convertPatternsToRe(positive, this._micromatchOptions)
|
},
|
negative: {
|
absolute: utils.pattern.convertPatternsToRe(absoluteNegative, Object.assign(Object.assign({}, this._micromatchOptions), { dot: true })),
|
relative: utils.pattern.convertPatternsToRe(relativeNegative, Object.assign(Object.assign({}, this._micromatchOptions), { dot: true }))
|
}
|
};
|
return (entry) => this._filter(entry, patterns);
|
}
|
_filter(entry, patterns) {
|
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;
|
}
|
const isMatched = this._isMatchToPatternsSet(filepath, patterns, entry.dirent.isDirectory());
|
if (this._settings.unique && isMatched) {
|
this._createIndexRecord(filepath);
|
}
|
return isMatched;
|
}
|
_isDuplicateEntry(filepath) {
|
return this.index.has(filepath);
|
}
|
_createIndexRecord(filepath) {
|
this.index.set(filepath, undefined);
|
}
|
_onlyFileFilter(entry) {
|
return this._settings.onlyFiles && !entry.dirent.isFile();
|
}
|
_onlyDirectoryFilter(entry) {
|
return this._settings.onlyDirectories && !entry.dirent.isDirectory();
|
}
|
_isMatchToPatternsSet(filepath, patterns, isDirectory) {
|
const isMatched = this._isMatchToPatterns(filepath, patterns.positive.all, isDirectory);
|
if (!isMatched) {
|
return false;
|
}
|
const isMatchedByRelativeNegative = this._isMatchToPatterns(filepath, patterns.negative.relative, isDirectory);
|
if (isMatchedByRelativeNegative) {
|
return false;
|
}
|
const isMatchedByAbsoluteNegative = this._isMatchToAbsoluteNegative(filepath, patterns.negative.absolute, isDirectory);
|
if (isMatchedByAbsoluteNegative) {
|
return false;
|
}
|
return true;
|
}
|
_isMatchToAbsoluteNegative(filepath, patternsRe, isDirectory) {
|
if (patternsRe.length === 0) {
|
return false;
|
}
|
const fullpath = utils.path.makeAbsolute(this._settings.cwd, filepath);
|
return this._isMatchToPatterns(fullpath, patternsRe, isDirectory);
|
}
|
_isMatchToPatterns(filepath, patternsRe, isDirectory) {
|
if (patternsRe.length === 0) {
|
return false;
|
}
|
// 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;
|
}
|
}
|
exports.default = EntryFilter;
|