ddee24376d863dc6f3e6940f83f84faae2adb88e2310b68a9af6fe654fac513ee6faf4edb99607e002c474eefbbbbb84315d7624c8db2e7e1c1e6b6ea03025 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const utils = require("../../utils");
  4. class EntryFilter {
  5. constructor(_settings, _micromatchOptions) {
  6. this._settings = _settings;
  7. this._micromatchOptions = _micromatchOptions;
  8. this.index = new Map();
  9. }
  10. getFilter(positive, negative) {
  11. const [absoluteNegative, relativeNegative] = utils.pattern.partitionAbsoluteAndRelative(negative);
  12. const patterns = {
  13. positive: {
  14. all: utils.pattern.convertPatternsToRe(positive, this._micromatchOptions)
  15. },
  16. negative: {
  17. absolute: utils.pattern.convertPatternsToRe(absoluteNegative, Object.assign(Object.assign({}, this._micromatchOptions), { dot: true })),
  18. relative: utils.pattern.convertPatternsToRe(relativeNegative, Object.assign(Object.assign({}, this._micromatchOptions), { dot: true }))
  19. }
  20. };
  21. return (entry) => this._filter(entry, patterns);
  22. }
  23. _filter(entry, patterns) {
  24. const filepath = utils.path.removeLeadingDotSegment(entry.path);
  25. if (this._settings.unique && this._isDuplicateEntry(filepath)) {
  26. return false;
  27. }
  28. if (this._onlyFileFilter(entry) || this._onlyDirectoryFilter(entry)) {
  29. return false;
  30. }
  31. const isMatched = this._isMatchToPatternsSet(filepath, patterns, entry.dirent.isDirectory());
  32. if (this._settings.unique && isMatched) {
  33. this._createIndexRecord(filepath);
  34. }
  35. return isMatched;
  36. }
  37. _isDuplicateEntry(filepath) {
  38. return this.index.has(filepath);
  39. }
  40. _createIndexRecord(filepath) {
  41. this.index.set(filepath, undefined);
  42. }
  43. _onlyFileFilter(entry) {
  44. return this._settings.onlyFiles && !entry.dirent.isFile();
  45. }
  46. _onlyDirectoryFilter(entry) {
  47. return this._settings.onlyDirectories && !entry.dirent.isDirectory();
  48. }
  49. _isMatchToPatternsSet(filepath, patterns, isDirectory) {
  50. const isMatched = this._isMatchToPatterns(filepath, patterns.positive.all, isDirectory);
  51. if (!isMatched) {
  52. return false;
  53. }
  54. const isMatchedByRelativeNegative = this._isMatchToPatterns(filepath, patterns.negative.relative, isDirectory);
  55. if (isMatchedByRelativeNegative) {
  56. return false;
  57. }
  58. const isMatchedByAbsoluteNegative = this._isMatchToAbsoluteNegative(filepath, patterns.negative.absolute, isDirectory);
  59. if (isMatchedByAbsoluteNegative) {
  60. return false;
  61. }
  62. return true;
  63. }
  64. _isMatchToAbsoluteNegative(filepath, patternsRe, isDirectory) {
  65. if (patternsRe.length === 0) {
  66. return false;
  67. }
  68. const fullpath = utils.path.makeAbsolute(this._settings.cwd, filepath);
  69. return this._isMatchToPatterns(fullpath, patternsRe, isDirectory);
  70. }
  71. _isMatchToPatterns(filepath, patternsRe, isDirectory) {
  72. if (patternsRe.length === 0) {
  73. return false;
  74. }
  75. // Trying to match files and directories by patterns.
  76. const isMatched = utils.pattern.matchAny(filepath, patternsRe);
  77. // A pattern with a trailling slash can be used for directory matching.
  78. // To apply such pattern, we need to add a tralling slash to the path.
  79. if (!isMatched && isDirectory) {
  80. return utils.pattern.matchAny(filepath + '/', patternsRe);
  81. }
  82. return isMatched;
  83. }
  84. }
  85. exports.default = EntryFilter;