ignore.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. // give it a pattern, and it'll be able to tell you if
  3. // a given path should be ignored.
  4. // Ignoring a path ignores its children if the pattern ends in /**
  5. // Ignores are always parsed in dot:true mode
  6. Object.defineProperty(exports, "__esModule", { value: true });
  7. exports.Ignore = void 0;
  8. const minimatch_1 = require("minimatch");
  9. const pattern_js_1 = require("./pattern.js");
  10. const defaultPlatform = typeof process === 'object' &&
  11. process &&
  12. typeof process.platform === 'string'
  13. ? process.platform
  14. : 'linux';
  15. /**
  16. * Class used to process ignored patterns
  17. */
  18. class Ignore {
  19. relative;
  20. relativeChildren;
  21. absolute;
  22. absoluteChildren;
  23. constructor(ignored, { nobrace, nocase, noext, noglobstar, platform = defaultPlatform, }) {
  24. this.relative = [];
  25. this.absolute = [];
  26. this.relativeChildren = [];
  27. this.absoluteChildren = [];
  28. const mmopts = {
  29. dot: true,
  30. nobrace,
  31. nocase,
  32. noext,
  33. noglobstar,
  34. optimizationLevel: 2,
  35. platform,
  36. };
  37. // this is a little weird, but it gives us a clean set of optimized
  38. // minimatch matchers, without getting tripped up if one of them
  39. // ends in /** inside a brace section, and it's only inefficient at
  40. // the start of the walk, not along it.
  41. // It'd be nice if the Pattern class just had a .test() method, but
  42. // handling globstars is a bit of a pita, and that code already lives
  43. // in minimatch anyway.
  44. // Another way would be if maybe Minimatch could take its set/globParts
  45. // as an option, and then we could at least just use Pattern to test
  46. // for absolute-ness.
  47. // Yet another way, Minimatch could take an array of glob strings, and
  48. // a cwd option, and do the right thing.
  49. for (const ign of ignored) {
  50. const mm = new minimatch_1.Minimatch(ign, mmopts);
  51. for (let i = 0; i < mm.set.length; i++) {
  52. const parsed = mm.set[i];
  53. const globParts = mm.globParts[i];
  54. const p = new pattern_js_1.Pattern(parsed, globParts, 0, platform);
  55. const m = new minimatch_1.Minimatch(p.globString(), mmopts);
  56. const children = globParts[globParts.length - 1] === '**';
  57. const absolute = p.isAbsolute();
  58. if (absolute)
  59. this.absolute.push(m);
  60. else
  61. this.relative.push(m);
  62. if (children) {
  63. if (absolute)
  64. this.absoluteChildren.push(m);
  65. else
  66. this.relativeChildren.push(m);
  67. }
  68. }
  69. }
  70. }
  71. ignored(p) {
  72. const fullpath = p.fullpath();
  73. const fullpaths = `${fullpath}/`;
  74. const relative = p.relative() || '.';
  75. const relatives = `${relative}/`;
  76. for (const m of this.relative) {
  77. if (m.match(relative) || m.match(relatives))
  78. return true;
  79. }
  80. for (const m of this.absolute) {
  81. if (m.match(fullpath) || m.match(fullpaths))
  82. return true;
  83. }
  84. return false;
  85. }
  86. childrenIgnored(p) {
  87. const fullpath = p.fullpath() + '/';
  88. const relative = (p.relative() || '.') + '/';
  89. for (const m of this.relativeChildren) {
  90. if (m.match(relative))
  91. return true;
  92. }
  93. for (const m of this.absoluteChildren) {
  94. if (m.match(fullpath))
  95. true;
  96. }
  97. return false;
  98. }
  99. }
  100. exports.Ignore = Ignore;
  101. //# sourceMappingURL=ignore.js.map