Debian Codegraph @

fs2/is-ignored.js

raw
1 /* eslint max-statements: off, max-lines: off */
2
3 "use strict";
4
5 const invoke = require("es5-ext/function/invoke")
6 , noop = require("es5-ext/function/noop")
7 , isCallable = require("es5-ext/object/is-callable")
8 , isValue = require("es5-ext/object/is-value")
9 , forEach = require("es5-ext/object/for-each")
10 , memoize = require("memoizee")
11 , deferred = require("deferred")
12 , ignore = require("ignore")
13 , pathUtils = require("path")
14 , modes = require("./lib/ignore-modes")
15 , getMap = require("./lib/get-conf-file-map")
16 , memoizeWatcher = require("./lib/memoize-watcher")
17 , findRoot = require("./lib/find-root");
18
19 const { isArray } = Array
20 , { push } = Array.prototype
21 , { call } = Function.prototype
22 , trim = call.bind(String.prototype.trim)
23 , { dirname } = pathUtils
24 , { resolve } = pathUtils
25 , { sep } = pathUtils
26 , { ConfMap } = getMap
27 , eolRe = /(?:\r\n|[\n\r\u2028\u2029])/u;
28
29 const prepareRules = function (data) { return data.map(trim).filter(Boolean).reverse(); };
30
31 const parseSrc = function (src) { return prepareRules(String(src).split(eolRe)); };
32
33 const compile = function (maps, result) {
34 const data = (result.data = {}), paths = (result.paths = []);
35
36 // Merge rules found in ignorefiles
37 maps.forEach(map => {
38 forEach(map.map, (rules, path) => {
39 if (!rules.length) return;
40 if (!data[path]) {
41 paths.push(path);
42 data[path] = [];
43 }
44 data[path].push(rules);
45 });
46 });
47 result.paths.sort();
48 return result;
49 };
50
51 const applyRules = function (rules, rootPath, path) {
52 if (!rootPath.endsWith(sep)) rootPath += sep;
53 if (!path.startsWith(rootPath)) return { value: false, target: path };
54 rules = rules
55 .slice()
56 .reverse()
57 .filter(rule => !rule.startsWith("#"));
58 const ig = ignore().add(rules);
59 const testPath = path.slice(rootPath.length);
60 let result = ig.ignores(testPath);
61 if (!result) {
62 const excludeRules = rules.filter(rule => rule.startsWith("!"));
63 if (excludeRules.length) {
64 const ig2 = ignore().add(excludeRules.map(rule => rule.slice(1)));
65 if (!ig2.ignores(testPath)) result = null;
66 } else {
67 result = null;
68 }
69 }
70 return { value: result, target: path };
71 };
72
73 const applyGlobalRules = function (path, rules) {
74 // Check global rules
75 const value = applyRules(rules, path.slice(0, path.indexOf(sep) + 1), path);
76 return Boolean(value.value);
77 };
78
79 const buildMap = function (lDirname, lGetMap, watch) {
80 let promise, maps;
81 const data = {};
82 lGetMap = lGetMap.map((getSubMap, index) => {
83 const map = getSubMap(lDirname);
84 if (watch) {
85 map.on("change", targetMap => {
86 if (maps) {
87 maps[index] = targetMap;
88 compile(maps, data);
89 promise.emit("change", data);
90 }
91 });
92 }
93 return map;
94 });
95 if (lGetMap.length > 1) {
96 promise = deferred.map(lGetMap)(result => {
97 maps = result;
98 return compile(maps, data);
99 });
100 } else {
101 promise = lGetMap[0](map => {
102 maps = [map];
103 return compile(maps, data);
104 });
105 }
106 if (watch) {
107 promise.close = function () { lGetMap.forEach(invoke("close")); };
108 }
109 return promise;
110 };
111
112 const IsIgnored = function (path, watch) {
113 this.path = path;
114 this.dirname = dirname(path);
115 this.watch = watch;
116 };
117
118 IsIgnored.prototype = {
119 init(mapPromise) {
120 this.mapPromise = mapPromise;
121 this.promise = mapPromise(data => {
122 this.data = data;
123 return this.calculate();
124 });
125 if (this.watch) {
126 mapPromise.on("change", () => {
127 const value = this.calculate();
128 if (value !== this.promise.value) {
129 this.promise.value = value;
130 this.promise.emit("change", value, this.path);
131 }
132 });
133 this.promise.close = this.close.bind(this);
134 }
135 return this.promise;
136 },
137 close() { this.mapPromise.close(); },
138 calculate() {
139 let current, result = false;
140
141 if (!this.data.paths) return false;
142
143 // Apply rules
144 current = this.path;
145 this.data.paths.some(function (rulesPath, index) {
146 if (rulesPath.length > this.dirname.length) return true;
147 if (index) {
148 const dirIgnored = this.data.paths
149 .slice(0, index)
150 .some(function (preRulesPath) {
151 return this.data.data[preRulesPath].some(
152 rules => applyRules(rules, preRulesPath, rulesPath).value, this
153 );
154 }, this);
155 if (dirIgnored) return false;
156 }
157 this.data.data[rulesPath].forEach(function (rules) {
158 let data = applyRules(rules, rulesPath, current);
159 if (data.value === false && current !== pathUtils) {
160 data = applyRules(rules, rulesPath, this.path);
161 }
162 if (data.target !== current || isValue(data.value)) {
163 result = data.value;
164 }
165 current = data.target;
166 }, this);
167 return false;
168 }, this);
169 return Boolean(result);
170 }
171 };
172
173 const isIgnored = function (mode, path, options) {
174 let globalRules, getMapFns, promise;
175
176 if (isValue(options.globalRules)) {
177 globalRules = isArray(options.globalRules)
178 ? options.globalRules
179 : String(options.globalRules).split(eolRe);
180 }
181
182 const { watch } = options;
183 const lIsIgnored = new IsIgnored(path, watch);
184 const lDirname = lIsIgnored.dirname;
185
186 if (mode) {
187 getMapFns = [];
188 if (!globalRules) globalRules = [];
189 if (!isArray(mode)) {
190 if (!modes[mode]) throw new Error(`Unknown mode '${ mode }'`);
191 mode = [mode];
192 }
193 mode.forEach(name => {
194 const lMode = modes[name];
195 if (!lMode) throw new Error(`Unknown mode '${ name }'`);
196 getMapFns.push(pathIgnored => getMap(lDirname, lMode, watch, parseSrc));
197 if (lMode.globalRules) push.apply(globalRules, lMode.globalRules);
198 });
199 }
200
201 if (globalRules) {
202 globalRules = prepareRules(globalRules);
203 if (applyGlobalRules(path, globalRules)) {
204 promise = deferred(true);
205 if (watch) promise.close = noop;
206 return promise;
207 }
208 }
209
210 if (!mode) {
211 promise = deferred(false);
212 if (watch) promise.close = noop;
213 return promise;
214 }
215
216 return lIsIgnored.init(buildMap(lDirname, getMapFns, watch));
217 };
218 isIgnored.returnsPromise = true;
219
220 module.exports = exports = function (mode, path, options = {}, cb = null) {
221 path = resolve(String(path));
222
223 if (!cb) {
224 if (isCallable(options)) {
225 cb = options;
226 options = {};
227 }
228 }
229 return isIgnored(mode, path, options).cb(cb);
230 };
231
232 exports.returnsPromise = true;
233 exports.isIgnored = isIgnored;
234 exports.IsIgnored = IsIgnored;
235 exports.applyGlobalRules = applyGlobalRules;
236
237 exports.getIsIgnored = function (modeNames, globalRules, watch) {
238 const mapGetters = [];
239 if (!globalRules) globalRules = [];
240 const memo = watch ? memoizeWatcher : memoize;
241 modeNames.forEach(name => {
242 const mode = modes[name];
243 if (!mode) throw new Error(`Unknown mode '${ name }'`);
244 const isRoot = memo(mode[watch ? "isRootWatcher" : "isRoot"], { primitive: true });
245 const readRules = memo(getMap[watch ? "readRulesWatcher" : "readRules"], {
246 primitive: true
247 });
248 mapGetters.push(path => {
249 const map = new ConfMap(path, watch);
250 map.filename = mode.filename;
251 map.readRules = readRules;
252 map.parse = parseSrc;
253 return map.init(findRoot(isRoot, path, { watch }));
254 });
255 if (mode.globalRules) push.apply(globalRules, mode.globalRules);
256 });
257 const build = memo(lDirname => buildMap(lDirname, mapGetters, watch), { primitive: true });
258
259 return {
260 isIgnored(path) {
261 const lIsIgnored = new IsIgnored(path, watch);
262 return lIsIgnored.init(build(lIsIgnored.dirname));
263 },
264 globalRules: globalRules.length ? prepareRules(globalRules) : null
265 };
266 };