UNPKG

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