UNPKG

4.27 kBJavaScriptView Raw
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
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.Ignore = void 0;
8const minimatch_1 = require("minimatch");
9const pattern_js_1 = require("./pattern.js");
10const 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 */
18class 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}
118exports.Ignore = Ignore;
119//# sourceMappingURL=ignore.js.map
\No newline at end of file