UNPKG

2.87 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.hasMagic = hasMagic;
7exports.createFilter = createFilter;
8
9var path = _interopRequireWildcard(require("path"));
10
11function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
12
13/** @internal */
14function hasMagic(pattern) {
15 const set = pattern.set;
16
17 if (set.length > 1) {
18 return true;
19 }
20
21 for (const i of set[0]) {
22 if (typeof i !== "string") {
23 return true;
24 }
25 }
26
27 return false;
28} // sometimes, destination may not contain path separator in the end (path to folder), but the src does. So let's ensure paths have path separators in the end
29
30
31function ensureEndSlash(s) {
32 return s.length === 0 || s.endsWith(path.sep) ? s : s + path.sep;
33}
34/** @internal */
35
36
37function createFilter(src, patterns, excludePatterns) {
38 const pathSeparator = path.sep;
39 const srcWithEndSlash = ensureEndSlash(src);
40 return (it, stat) => {
41 if (src === it) {
42 return true;
43 }
44
45 if (!it.startsWith(srcWithEndSlash)) {
46 throw new Error(`${it} must be under ${srcWithEndSlash}`);
47 }
48
49 let relative = it.substring(srcWithEndSlash.length);
50
51 if (pathSeparator === "\\") {
52 if (relative.startsWith("\\")) {
53 // windows problem: double backslash, the above substring call removes root path with a single slash, so here can me some leftovers
54 relative = relative.substring(1);
55 }
56
57 relative = relative.replace(/\\/g, "/");
58 } // https://github.com/electron-userland/electron-builder/issues/867
59
60
61 return minimatchAll(relative, patterns, stat) && (excludePatterns == null || stat.isDirectory() || !minimatchAll(relative, excludePatterns, stat));
62 };
63} // https://github.com/joshwnj/minimatch-all/blob/master/index.js
64
65
66function minimatchAll(path, patterns, stat) {
67 let match = false;
68
69 for (const pattern of patterns) {
70 // If we've got a match, only re-test for exclusions.
71 // if we don't have a match, only re-test for inclusions.
72 if (match !== pattern.negate) {
73 continue;
74 } // partial match — pattern: foo/bar.txt path: foo — we must allow foo
75 // use it only for non-negate patterns: const m = new Minimatch("!node_modules/@(electron-download|electron)/**/*", {dot: true }); m.match("node_modules", true) will return false, but must be true
76
77
78 match = pattern.match(path, stat.isDirectory() && !pattern.negate);
79 }
80
81 return match;
82}
83//# sourceMappingURL=filter.js.map
\No newline at end of file