ignore.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. platform;
  24. mmopts;
  25. constructor(ignored, { nobrace, nocase, noext, noglobstar, platform = defaultPlatform, }) {
  26. this.relative = [];
  27. this.absolute = [];
  28. this.relativeChildren = [];
  29. this.absoluteChildren = [];
  30. this.platform = platform;
  31. this.mmopts = {
  32. dot: true,
  33. nobrace,
  34. nocase,
  35. noext,
  36. noglobstar,
  37. optimizationLevel: 2,
  38. platform,
  39. nocomment: true,
  40. nonegate: true,
  41. };
  42. for (const ign of ignored)
  43. this.add(ign);
  44. }
  45. add(ign) {
  46. // this is a little weird, but it gives us a clean set of optimized
  47. // minimatch matchers, without getting tripped up if one of them
  48. // ends in /** inside a brace section, and it's only inefficient at
  49. // the start of the walk, not along it.
  50. // It'd be nice if the Pattern class just had a .test() method, but
  51. // handling globstars is a bit of a pita, and that code already lives
  52. // in minimatch anyway.
  53. // Another way would be if maybe Minimatch could take its set/globParts
  54. // as an option, and then we could at least just use Pattern to test
  55. // for absolute-ness.
  56. // Yet another way, Minimatch could take an array of glob strings, and
  57. // a cwd option, and do the right thing.
  58. const mm = new minimatch_1.Minimatch(ign, this.mmopts);
  59. for (let i = 0; i < mm.set.length; i++) {
  60. const parsed = mm.set[i];
  61. const globParts = mm.globParts[i];
  62. /* c8 ignore start */
  63. if (!parsed || !globParts) {
  64. throw new Error('invalid pattern object');
  65. }
  66. // strip off leading ./ portions
  67. // https://github.com/isaacs/node-glob/issues/570
  68. while (parsed[0] === '.' && globParts[0] === '.') {
  69. parsed.shift();
  70. globParts.shift();
  71. }
  72. /* c8 ignore stop */
  73. const p = new pattern_js_1.Pattern(parsed, globParts, 0, this.platform);
  74. const m = new minimatch_1.Minimatch(p.globString(), this.mmopts);
  75. const children = globParts[globParts.length - 1] === '**';
  76. const absolute = p.isAbsolute();
  77. if (absolute)
  78. this.absolute.push(m);
  79. else
  80. this.relative.push(m);
  81. if (children) {
  82. if (absolute)
  83. this.absoluteChildren.push(m);
  84. else
  85. this.relativeChildren.push(m);
  86. }
  87. }
  88. }
  89. ignored(p) {
  90. const fullpath = p.fullpath();
  91. const fullpaths = `${fullpath}/`;
  92. const relative = p.relative() || '.';
  93. const relatives = `${relative}/`;
  94. for (const m of this.relative) {
  95. if (m.match(relative) || m.match(relatives))
  96. return true;
  97. }
  98. for (const m of this.absolute) {
  99. if (m.match(fullpath) || m.match(fullpaths))
  100. return true;
  101. }
  102. return false;
  103. }
  104. childrenIgnored(p) {
  105. const fullpath = p.fullpath() + '/';
  106. const relative = (p.relative() || '.') + '/';
  107. for (const m of this.relativeChildren) {
  108. if (m.match(relative))
  109. return true;
  110. }
  111. for (const m of this.absoluteChildren) {
  112. if (m.match(fullpath))
  113. return true;
  114. }
  115. return false;
  116. }
  117. }
  118. exports.Ignore = Ignore;
  119. //# sourceMappingURL=ignore.js.map