UNPKG

13.2 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getMainFileMatchers = getMainFileMatchers;
7exports.getNodeModuleFileMatcher = getNodeModuleFileMatcher;
8exports.getFileMatchers = getFileMatchers;
9exports.copyFiles = copyFiles;
10exports.FileMatcher = exports.excludedExts = exports.excludedNames = void 0;
11
12function _bluebirdLst() {
13 const data = _interopRequireDefault(require("bluebird-lst"));
14
15 _bluebirdLst = function () {
16 return data;
17 };
18
19 return data;
20}
21
22function _builderUtil() {
23 const data = require("builder-util");
24
25 _builderUtil = function () {
26 return data;
27 };
28
29 return data;
30}
31
32function _fs() {
33 const data = require("builder-util/out/fs");
34
35 _fs = function () {
36 return data;
37 };
38
39 return data;
40}
41
42function _fsExtra() {
43 const data = require("fs-extra");
44
45 _fsExtra = function () {
46 return data;
47 };
48
49 return data;
50}
51
52function _minimatch() {
53 const data = require("minimatch");
54
55 _minimatch = function () {
56 return data;
57 };
58
59 return data;
60}
61
62var path = _interopRequireWildcard(require("path"));
63
64function _filter() {
65 const data = require("./util/filter");
66
67 _filter = function () {
68 return data;
69 };
70
71 return data;
72}
73
74function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
75
76function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
77
78function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
79
80// https://github.com/electron-userland/electron-builder/issues/733
81const minimatchOptions = {
82 dot: true
83}; // noinspection SpellCheckingInspection
84
85const excludedNames = ".git,.hg,.svn,CVS,RCS,SCCS," + "__pycache__,.DS_Store,thumbs.db,.gitignore,.gitkeep,.gitattributes,.npmignore," + ".idea,.vs,.flowconfig,.jshintrc,.eslintrc,.circleci," + ".yarn-integrity,.yarn-metadata.json,yarn-error.log,yarn.lock,package-lock.json,npm-debug.log," + "appveyor.yml,.travis.yml,circle.yml,.nyc_output";
86exports.excludedNames = excludedNames;
87const excludedExts = "iml,hprof,orig,pyc,pyo,rbc,swp,csproj,sln,suo,xproj,cc,d.ts";
88exports.excludedExts = excludedExts;
89
90function ensureNoEndSlash(file) {
91 if (path.sep !== "/") {
92 file = file.replace(/\//g, path.sep);
93 }
94
95 if (path.sep !== "\\") {
96 file = file.replace(/\\/g, path.sep);
97 }
98
99 if (file.endsWith(path.sep)) {
100 return file.substring(0, file.length - 1);
101 } else {
102 return file;
103 }
104}
105/** @internal */
106
107
108class FileMatcher {
109 constructor(from, to, macroExpander, patterns) {
110 this.macroExpander = macroExpander;
111 this.excludePatterns = null;
112 this.from = ensureNoEndSlash(macroExpander(from));
113 this.to = ensureNoEndSlash(macroExpander(to));
114 this.patterns = (0, _builderUtil().asArray)(patterns).map(it => this.normalizePattern(it));
115 this.isSpecifiedAsEmptyArray = Array.isArray(patterns) && patterns.length === 0;
116 }
117
118 normalizePattern(pattern) {
119 if (pattern.startsWith("./")) {
120 pattern = pattern.substring("./".length);
121 }
122
123 return path.posix.normalize(this.macroExpander(pattern.replace(/\\/g, "/")));
124 }
125
126 addPattern(pattern) {
127 this.patterns.push(this.normalizePattern(pattern));
128 }
129
130 prependPattern(pattern) {
131 this.patterns.unshift(this.normalizePattern(pattern));
132 }
133
134 isEmpty() {
135 return this.patterns.length === 0;
136 }
137
138 containsOnlyIgnore() {
139 return !this.isEmpty() && this.patterns.find(it => !it.startsWith("!")) == null;
140 }
141
142 computeParsedPatterns(result, fromDir) {
143 const relativeFrom = fromDir == null ? null : path.relative(fromDir, this.from);
144
145 if (this.patterns.length === 0 && relativeFrom != null) {
146 // file mappings, from here is a file
147 result.push(new (_minimatch().Minimatch)(relativeFrom, minimatchOptions));
148 return;
149 }
150
151 for (let pattern of this.patterns) {
152 if (relativeFrom != null) {
153 pattern = path.join(relativeFrom, pattern);
154 }
155
156 const parsedPattern = new (_minimatch().Minimatch)(pattern, minimatchOptions);
157 result.push(parsedPattern); // do not add if contains dot (possibly file if has extension)
158
159 if (!pattern.includes(".") && !(0, _filter().hasMagic)(parsedPattern)) {
160 // https://github.com/electron-userland/electron-builder/issues/545
161 // add **/*
162 result.push(new (_minimatch().Minimatch)(`${pattern}/**/*`, minimatchOptions));
163 }
164 }
165 }
166
167 createFilter() {
168 const parsedPatterns = [];
169 this.computeParsedPatterns(parsedPatterns);
170 return (0, _filter().createFilter)(this.from, parsedPatterns, this.excludePatterns);
171 }
172
173 toString() {
174 return `from: ${this.from}, to: ${this.to}, patterns: ${this.patterns.join(", ")}`;
175 }
176
177}
178/** @internal */
179
180
181exports.FileMatcher = FileMatcher;
182
183function getMainFileMatchers(appDir, destination, macroExpander, platformSpecificBuildOptions, platformPackager, outDir, isElectronCompile) {
184 const packager = platformPackager.info;
185 const buildResourceDir = path.resolve(packager.projectDir, packager.buildResourcesDir);
186 let matchers = packager.isPrepackedAppAsar ? null : getFileMatchers(packager.config, "files", destination, {
187 macroExpander,
188 customBuildOptions: platformSpecificBuildOptions,
189 globalOutDir: outDir,
190 defaultSrc: appDir
191 });
192
193 if (matchers == null) {
194 matchers = [new FileMatcher(appDir, destination, macroExpander)];
195 }
196
197 const matcher = matchers[0]; // add default patterns, but only if from equals to app dir
198
199 if (matcher.from !== appDir) {
200 return matchers;
201 } // https://github.com/electron-userland/electron-builder/issues/1741#issuecomment-311111418 so, do not use inclusive patterns
202
203
204 const patterns = matcher.patterns;
205 const customFirstPatterns = []; // electron-webpack - we need to copy only package.json and node_modules from root dir (and these files are added by default), so, explicit empty array is specified
206
207 if (!matcher.isSpecifiedAsEmptyArray && (matcher.isEmpty() || matcher.containsOnlyIgnore())) {
208 customFirstPatterns.push("**/*");
209 } else if (!patterns.includes("package.json")) {
210 patterns.push("package.json");
211 } // https://github.com/electron-userland/electron-builder/issues/1482
212
213
214 const relativeBuildResourceDir = path.relative(matcher.from, buildResourceDir);
215
216 if (relativeBuildResourceDir.length !== 0 && !relativeBuildResourceDir.startsWith(".")) {
217 customFirstPatterns.push(`!${relativeBuildResourceDir}{,/**/*}`);
218 }
219
220 const relativeOutDir = matcher.normalizePattern(path.relative(packager.projectDir, outDir));
221
222 if (!relativeOutDir.startsWith(".")) {
223 customFirstPatterns.push(`!${relativeOutDir}{,/**/*}`);
224 } // add our default exclusions after last user possibly defined "all"/permissive pattern
225
226
227 let insertIndex = 0;
228
229 for (let i = patterns.length - 1; i >= 0; i--) {
230 if (patterns[i].startsWith("**/")) {
231 insertIndex = i + 1;
232 break;
233 }
234 }
235
236 patterns.splice(insertIndex, 0, ...customFirstPatterns);
237 patterns.push(`!**/*.{${excludedExts}${packager.config.includePdb === true ? "" : ",pdb"}`);
238 patterns.push("!**/._*");
239 patterns.push("!**/electron-builder.{yaml,yml,json,json5,toml}");
240 patterns.push(`!**/{${excludedNames}}`);
241
242 if (isElectronCompile) {
243 patterns.push("!.cache{,/**/*}");
244 }
245
246 patterns.push("!.yarn{,/**/*}"); // https://github.com/electron-userland/electron-builder/issues/1969
247 // exclude ony for app root, use .yarnclean to clean node_modules
248
249 patterns.push("!.editorconfig");
250 patterns.push("!.yarnrc.yml");
251 const debugLogger = packager.debugLogger;
252
253 if (debugLogger.isEnabled) {
254 //tslint:disable-next-line:no-invalid-template-strings
255 debugLogger.add(`${macroExpander("${arch}")}.firstOrDefaultFilePatterns`, patterns);
256 }
257
258 return matchers;
259}
260/** @internal */
261
262
263function getNodeModuleFileMatcher(appDir, destination, macroExpander, platformSpecificBuildOptions, packager) {
264 // https://github.com/electron-userland/electron-builder/pull/2948#issuecomment-392241632
265 // grab only excludes
266 const matcher = new FileMatcher(appDir, destination, macroExpander);
267
268 function addPatterns(patterns) {
269 if (patterns == null) {
270 return;
271 } else if (!Array.isArray(patterns)) {
272 if (typeof patterns === "string" && patterns.startsWith("!")) {
273 matcher.addPattern(patterns);
274 return;
275 } // ignore object form
276
277
278 return;
279 }
280
281 for (const pattern of patterns) {
282 if (typeof pattern === "string") {
283 if (pattern.startsWith("!")) {
284 matcher.addPattern(pattern);
285 }
286 } else {
287 const fileSet = pattern;
288
289 if (fileSet.from == null || fileSet.from === ".") {
290 for (const p of (0, _builderUtil().asArray)(fileSet.filter)) {
291 matcher.addPattern(p);
292 }
293 }
294 }
295 }
296 }
297
298 addPatterns(packager.config.files);
299 addPatterns(platformSpecificBuildOptions.files);
300
301 if (!matcher.isEmpty()) {
302 matcher.prependPattern("**/*");
303 }
304
305 const debugLogger = packager.debugLogger;
306
307 if (debugLogger.isEnabled) {
308 //tslint:disable-next-line:no-invalid-template-strings
309 debugLogger.add(`${macroExpander("${arch}")}.nodeModuleFilePatterns`, matcher.patterns);
310 }
311
312 return matcher;
313}
314/** @internal */
315
316
317function getFileMatchers(config, name, defaultDestination, options) {
318 const defaultMatcher = new FileMatcher(options.defaultSrc, defaultDestination, options.macroExpander);
319 const fileMatchers = [];
320
321 function addPatterns(patterns) {
322 if (patterns == null) {
323 return;
324 } else if (!Array.isArray(patterns)) {
325 if (typeof patterns === "string") {
326 defaultMatcher.addPattern(patterns);
327 return;
328 }
329
330 patterns = [patterns];
331 }
332
333 for (const pattern of patterns) {
334 if (typeof pattern === "string") {
335 // use normalize to transform ./foo to foo
336 defaultMatcher.addPattern(pattern);
337 } else if (name === "asarUnpack") {
338 throw new Error(`Advanced file copying not supported for "${name}"`);
339 } else {
340 const from = pattern.from == null ? options.defaultSrc : path.resolve(options.defaultSrc, pattern.from);
341 const to = pattern.to == null ? defaultDestination : path.resolve(defaultDestination, pattern.to);
342 fileMatchers.push(new FileMatcher(from, to, options.macroExpander, pattern.filter));
343 }
344 }
345 }
346
347 if (name !== "extraDistFiles") {
348 addPatterns(config[name]);
349 }
350
351 addPatterns(options.customBuildOptions[name]);
352
353 if (!defaultMatcher.isEmpty()) {
354 // default matcher should be first in the array
355 fileMatchers.unshift(defaultMatcher);
356 } // we cannot exclude the whole out dir, because sometimes users want to use some file in the out dir in the patterns
357
358
359 const relativeOutDir = defaultMatcher.normalizePattern(path.relative(options.defaultSrc, options.globalOutDir));
360
361 if (!relativeOutDir.startsWith(".")) {
362 defaultMatcher.addPattern(`!${relativeOutDir}/*-unpacked{,/**/*}`);
363 }
364
365 return fileMatchers.length === 0 ? null : fileMatchers;
366}
367/** @internal */
368
369
370function copyFiles(matchers, transformer, isUseHardLink) {
371 if (matchers == null || matchers.length === 0) {
372 return Promise.resolve();
373 }
374
375 return _bluebirdLst().default.map(matchers, async matcher => {
376 const fromStat = await (0, _fs().statOrNull)(matcher.from);
377
378 if (fromStat == null) {
379 _builderUtil().log.warn({
380 from: matcher.from
381 }, `file source doesn't exist`);
382
383 return;
384 }
385
386 if (fromStat.isFile()) {
387 const toStat = await (0, _fs().statOrNull)(matcher.to); // https://github.com/electron-userland/electron-builder/issues/1245
388
389 if (toStat != null && toStat.isDirectory()) {
390 return await (0, _fs().copyOrLinkFile)(matcher.from, path.join(matcher.to, path.basename(matcher.from)), fromStat, isUseHardLink);
391 }
392
393 await (0, _fsExtra().ensureDir)(path.dirname(matcher.to));
394 return await (0, _fs().copyOrLinkFile)(matcher.from, matcher.to, fromStat);
395 }
396
397 if (matcher.isEmpty() || matcher.containsOnlyIgnore()) {
398 matcher.prependPattern("**/*");
399 }
400
401 _builderUtil().log.debug({
402 matcher
403 }, "copying files using pattern");
404
405 return await (0, _fs().copyDir)(matcher.from, matcher.to, {
406 filter: matcher.createFilter(),
407 transformer,
408 isUseHardLink: isUseHardLink ? _fs().USE_HARD_LINKS : null
409 });
410 });
411}
412// __ts-babel@6.0.4
413//# sourceMappingURL=fileMatcher.js.map
\No newline at end of file