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 5import { Minimatch } from 'minimatch'; 6import { Pattern } from './pattern.js'; 7const 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 */ 15export 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 nocomment: true, 34 nonegate: true, 35 }; 36 // this is a little weird, but it gives us a clean set of optimized 37 // minimatch matchers, without getting tripped up if one of them 38 // ends in /** inside a brace section, and it's only inefficient at 39 // the start of the walk, not along it. 40 // It'd be nice if the Pattern class just had a .test() method, but 41 // handling globstars is a bit of a pita, and that code already lives 42 // in minimatch anyway. 43 // Another way would be if maybe Minimatch could take its set/globParts 44 // as an option, and then we could at least just use Pattern to test 45 // for absolute-ness. 46 // Yet another way, Minimatch could take an array of glob strings, and 47 // a cwd option, and do the right thing. 48 for (const ign of ignored) { 49 const mm = new Minimatch(ign, mmopts); 50 for (let i = 0; i < mm.set.length; i++) { 51 const parsed = mm.set[i]; 52 const globParts = mm.globParts[i]; 53 const p = new Pattern(parsed, globParts, 0, platform); 54 const m = new Minimatch(p.globString(), mmopts); 55 const children = globParts[globParts.length - 1] === '**'; 56 const absolute = p.isAbsolute(); 57 if (absolute) 58 this.absolute.push(m); 59 else 60 this.relative.push(m); 61 if (children) { 62 if (absolute) 63 this.absoluteChildren.push(m); 64 else 65 this.relativeChildren.push(m); 66 } 67 } 68 } 69 } 70 ignored(p) { 71 const fullpath = p.fullpath(); 72 const fullpaths = `${fullpath}/`; 73 const relative = p.relative() || '.'; 74 const relatives = `${relative}/`; 75 for (const m of this.relative) { 76 if (m.match(relative) || m.match(relatives)) 77 return true; 78 } 79 for (const m of this.absolute) { 80 if (m.match(fullpath) || m.match(fullpaths)) 81 return true; 82 } 83 return false; 84 } 85 childrenIgnored(p) { 86 const fullpath = p.fullpath() + '/'; 87 const relative = (p.relative() || '.') + '/'; 88 for (const m of this.relativeChildren) { 89 if (m.match(relative)) 90 return true; 91 } 92 for (const m of this.absoluteChildren) { 93 if (m.match(fullpath)) 94 true; 95 } 96 return false; 97 } 98} 99//# sourceMappingURL=ignore.js.map