ignore.js 3.5 KB

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