-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
511 lines (416 loc) · 15.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
'use strict';
const path = require('path').posix;
const Minimatch = require('minimatch').Minimatch;
const arrayEqual = require('array-equal');
const Plugin = require('broccoli-plugin');
const debug = require('debug');
const FSTree = require('fs-tree-diff');
const heimdall = require('heimdalljs');
const fs = require('fs');
function ApplyPatchesSchema() {
this.mkdir = 0;
this.rmdir = 0;
this.unlink = 0;
this.change = 0;
this.create = 0;
this.other = 0;
this.processed = 0;
this.linked = 0;
}
// copied mostly from node-glob cc @isaacs
function isNotAPattern(pattern) {
let set = new Minimatch(pattern).set;
if (set.length > 1) {
return false;
}
for (let j = 0; j < set[0].length; j++) {
if (typeof set[0][j] !== 'string') {
return false;
}
}
return true;
}
function existsSync(path) {
let error = {};
try {
fs.accessSync(path);
fs.statSync(path);
} catch (err) {
error = err;
}
if (error.errno && error.errno !== 0) {
return false;
}
return true;
}
class Funnel extends Plugin {
constructor(inputs, options = {}) {
let inputNodes = Array.isArray(inputs) ? inputs : [inputs];
super(inputNodes, {
annotation: options.annotation,
persistentOutput: true,
needsCache: false,
});
this._includeFileCache = Object.create(null);
this._destinationPathCache = Object.create(null);
this._currentTree = new FSTree();
this._isRebuild = false;
let keys = Object.keys(options || {});
for (let i = 0, l = keys.length; i < l; i++) {
let key = keys[i];
this[key] = options[key];
}
this.destDir = this.destDir || './';
this.count = 0;
if (this.files && typeof this.files === 'function') {
// Save dynamic files func as a different variable and let the rest of the code
// still assume that this.files is always an array.
this._dynamicFilesFunc = this.files;
delete this.files;
} else if (this.files && !Array.isArray(this.files)) {
throw new Error('Invalid files option, it must be an array or function (that returns an array).');
}
if ((this.files || this._dynamicFilesFunc) && (this.include || this.exclude)) {
throw new Error('Cannot pass files option (array or function) and a include/exlude filter. You can have one or the other');
}
if (this.files) {
if (this.files.filter(isNotAPattern).length !== this.files.length) {
console.warn('broccoli-funnel does not support `files:` option with globs, please use `include:` instead');
this.include = this.files;
this.files = undefined;
}
}
this._setupFilter('include');
this._setupFilter('exclude');
this._matchedWalk = this.canMatchWalk();
this._buildStart = undefined;
}
canMatchWalk() {
let include = this.include;
let exclude = this.exclude;
if (!include && !exclude) { return false; }
let includeIsOk = true;
if (include) {
includeIsOk = include.filter(isMinimatch).length === include.length;
}
let excludeIsOk = true;
if (exclude) {
excludeIsOk = exclude.filter(isMinimatch).length === exclude.length;
}
return includeIsOk && excludeIsOk;
}
_debugName() {
return this.description || this._annotation || this.name || this.constructor.name;
}
_debug() {
debug(`broccoli-funnel:${this._debugName()}`).apply(null, arguments);
}
_setupFilter(type) {
if (!this[type]) {
return;
}
if (!Array.isArray(this[type])) {
throw new Error(`Invalid ${type} option, it must be an array. You specified \`${typeof this[type]}\`.`);
}
// Clone the filter array so we are not mutating an external variable
let filters = this[type] = this[type].slice(0);
for (let i = 0, l = filters.length; i < l; i++) {
filters[i] = this._processPattern(filters[i]);
}
}
_processPattern(pattern) {
if (pattern instanceof RegExp) {
return pattern;
}
let type = typeof pattern;
if (type === 'string') {
return new Minimatch(pattern);
}
if (type === 'function') {
return pattern;
}
throw new Error(`include/exclude patterns can be a RegExp, glob string, or function. You supplied \`${typeof pattern}\`.`);
}
shouldLinkRoots() {
return !this.files && !this.include && !this.exclude && !this.getDestinationPath;
}
build() {
this._buildStart = new Date();
this.destPath = path.join(this.outputPath, this.destDir);
if (this.destPath[this.destPath.length - 1] === '/') {
this.destPath = this.destPath.slice(0, -1);
}
let inputPath = this.inputPaths[0];
if (this.srcDir) {
this.srcDir = ensureRelative(this.srcDir);
inputPath = path.join(inputPath, this.srcDir);
}
if (this._dynamicFilesFunc) {
this.lastFiles = this.files;
this.files = this._dynamicFilesFunc() || [];
// Blow away the include cache if the list of files is new
if (this.lastFiles !== undefined && !arrayEqual(this.lastFiles, this.files)) {
this._includeFileCache = Object.create(null);
}
}
let inputPathExists = this.input.at(0).fs.existsSync(this.srcDir || './');
let linkedRoots = false;
if (this.shouldLinkRoots()) {
linkedRoots = true;
/**
* We want to link the roots of these directories, but there are a few
* edge cases we must account for.
*
* 1. It's possible that the original input doesn't actually exist.
* 2. It's possible that the output symlink has been broken.
* 3. We need slightly different behavior on rebuilds.
*
* Behavior has been modified to always having an `else` clause so that
* the code is forced to account for all scenarios. Not accounting for
* all scenarios made it possible for initial builds to succeed without
* specifying `this.allowEmpty`.
*/
// This is specifically looking for broken symlinks.
let outputPathExists = this.output.existsSync('./');
// We need to keep this till node 10,12 get fix for windows broken symlink issue.
// https://github.com/nodejs/node/issues/30538
let isWin = process.platform === 'win32';
if (isWin) {
outputPathExists = existsSync(this.outputPath);
}
// Doesn't count as a rebuild if there's not an existing outputPath.
this._isRebuild = this._isRebuild && outputPathExists;
/*eslint-disable no-lonely-if*/
if (this._isRebuild) {
if (inputPathExists) {
// Already works because of symlinks. Do nothing.
} else if (!inputPathExists && this.allowEmpty) {
// Make sure we're safely using a new outputPath since we were previously symlinked:
this.output.rmdirSync('./', { recursive: true });
// Create a new empty folder:
this.output.mkdirSync(this.destDir, { recursive: true });
} else { // this._isRebuild && !inputPathExists && !this.allowEmpty
// Need to remove it on the rebuild.
// Can blindly remove a symlink if path exists.
this.output.rmdirSync('./', { recursive: true });
}
} else { // Not a rebuild.
if (inputPathExists) {
// We don't want to use the generated-for-us folder.
// Instead let's remove it:
this.output.rmdirSync('./', { recursive: true });
// And then symlinkOrCopy over top of it:
this._copy(inputPath, this.destPath, './');
} else if (!inputPathExists && this.allowEmpty) {
// Can't symlink nothing, so make an empty folder at `destPath`:
this.output.mkdirSync(this.destDir, { recursive: true });
} else { // !this._isRebuild && !inputPathExists && !this.allowEmpty
throw new Error(`You specified a \`"srcDir": ${this.srcDir}\` which does not exist and did not specify \`"allowEmpty": true\`.`);
}
}
/*eslint-enable no-lonely-if*/
this._isRebuild = true;
} else if (inputPathExists) {
this.processFilters(inputPath);
} else if (!this.allowEmpty) {
throw new Error(`You specified a \`"srcDir": ${this.srcDir}\` which does not exist and did not specify \`"allowEmpty": true\`.`);
} else { // !inputPathExists && this.allowEmpty
// Just make an empty folder so that any downstream consumers who don't know
// to ignore this on `allowEmpty` don't get trolled.
this.output.mkdirSync(this.destDir, { recursive: true });
}
this._debug('build, %o', {
in: `${new Date() - this._buildStart}ms`,
linkedRoots,
inputPath,
destPath: this.destPath,
});
}
_processEntries(entries) {
return entries.filter(function(entry) {
// support the second set of filters walk-sync does not support
// * regexp
// * excludes
return this.includeFile(entry.relativePath);
}, this).map(function(entry) {
let relativePath = entry.relativePath;
entry.relativePath = this.lookupDestinationPath(relativePath);
this.outputToInputMappings[entry.relativePath] = relativePath;
return entry;
}, this);
}
_processPaths(paths) {
return paths.
slice(0).
filter(this.includeFile, this).
map(function(relativePath) {
let output = this.lookupDestinationPath(relativePath);
this.outputToInputMappings[output] = relativePath;
return output;
}, this);
}
processFilters(inputPath) {
let nextTree;
let instrumentation = heimdall.start('derivePatches');
let entries;
this.outputToInputMappings = {}; // we allow users to rename files
if (this.files && !this.exclude && !this.include) {
entries = this._processPaths(this.files);
// clone to be compatible with walkSync
nextTree = FSTree.fromPaths(entries, { sortAndExpand: true });
} else {
if (this._matchedWalk) {
entries = this.input.at(0).entries(this.srcDir || './', { globs: this.include, ignore: this.exclude });
} else {
entries = this.input.at(0).entries(this.srcDir || './');
}
entries = this._processEntries(entries);
nextTree = FSTree.fromEntries(entries, { sortAndExpand: true });
}
let patches = this._currentTree.calculatePatch(nextTree);
this._currentTree = nextTree;
instrumentation.stats.patches = patches.length;
instrumentation.stats.entries = entries.length;
let outputPath = this.outputPath;
instrumentation.stop();
instrumentation = heimdall.start('applyPatch', ApplyPatchesSchema);
patches.forEach(function(entry) {
this._applyPatch(entry, inputPath, outputPath, instrumentation.stats);
}, this);
instrumentation.stop();
}
_applyPatch(entry, inputPath, _outputPath, stats) {
let outputToInput = this.outputToInputMappings;
let operation = entry[0];
let outputRelative = entry[1];
if (!outputRelative) {
// broccoli itself maintains the roots, we can skip any operation on them
return;
}
let outputPath = `${_outputPath}/${outputRelative}`;
this._debug('%s %s', operation, outputPath);
switch (operation) {
case 'unlink' :
stats.unlink++;
this.output.unlinkSync(outputRelative);
break;
case 'rmdir' :
stats.rmdir++;
this.output.rmdirSync(outputRelative);
break;
case 'mkdir' :
stats.mkdir++;
this.output.mkdirSync(outputRelative);
break;
case 'change':
stats.change++;
/* falls through */
case 'create': {
if (operation === 'create') {
stats.create++;
}
let relativePath = outputToInput[outputRelative];
if (relativePath === undefined) {
relativePath = outputToInput[`/${outputRelative}`];
}
this.processFile(`${inputPath}/${relativePath}`, outputPath, relativePath);
break;
}
default: throw new Error(`Unknown operation: ${operation}`);
}
}
lookupDestinationPath(relativePath) {
if (this._destinationPathCache[relativePath] !== undefined) {
return this._destinationPathCache[relativePath];
}
// the destDir is absolute to prevent '..' above the output dir
if (this.getDestinationPath) {
return this._destinationPathCache[relativePath] = ensureRelative(path.join(this.destDir, this.getDestinationPath(relativePath)));
}
return this._destinationPathCache[relativePath] = ensureRelative(path.join(this.destDir, relativePath));
}
includeFile(relativePath) {
let includeFileCache = this._includeFileCache;
if (includeFileCache[relativePath] !== undefined) {
return includeFileCache[relativePath];
}
// do not include directories, only files
if (relativePath[relativePath.length - 1] === '/') {
return includeFileCache[relativePath] = false;
}
let i, l, pattern;
// Check for specific files listing
if (this.files) {
return includeFileCache[relativePath] = this.files.indexOf(relativePath) > -1;
}
if (this._matchedWalk) {
return true;
}
// Check exclude patterns
if (this.exclude) {
for (i = 0, l = this.exclude.length; i < l; i++) {
// An exclude pattern that returns true should be ignored
pattern = this.exclude[i];
if (this._matchesPattern(pattern, relativePath)) {
return includeFileCache[relativePath] = false;
}
}
}
// Check include patterns
if (this.include && this.include.length > 0) {
for (i = 0, l = this.include.length; i < l; i++) {
// An include pattern that returns true (and wasn't excluded at all)
// should _not_ be ignored
pattern = this.include[i];
if (this._matchesPattern(pattern, relativePath)) {
return includeFileCache[relativePath] = true;
}
}
// If no include patterns were matched, ignore this file.
return includeFileCache[relativePath] = false;
}
// Otherwise, don't ignore this file
return includeFileCache[relativePath] = true;
}
_matchesPattern(pattern, relativePath) {
if (pattern instanceof RegExp) {
return pattern.test(relativePath);
} else if (pattern instanceof Minimatch) {
return pattern.match(relativePath);
} else if (typeof pattern === 'function') {
return pattern(relativePath);
}
throw new Error(`Pattern \`${pattern}\` was not a RegExp, Glob, or Function.`);
}
processFile(sourcePath, destPath, relativePath) {
this._copy(sourcePath, destPath, relativePath);
}
_copy(sourcePath, destPath, relativePath) {
relativePath = this.lookupDestinationPath(relativePath);
let destDir = path.dirname(relativePath);
try {
this.output.symlinkOrCopySync(sourcePath, relativePath);
} catch (e) {
this.output.mkdirSync(destDir, { recursive: true });
try {
this.output.unlinkSync(relativePath);
} catch (e) {
// swallow the error
}
this.output.symlinkOrCopySync(sourcePath, relativePath);
}
}
}
function isMinimatch(x) {
return x instanceof Minimatch;
}
function ensureRelative(string) {
if (string.charAt(0) === '/') {
return string.substring(1);
}
return string;
}
module.exports = function funnel(...params) {
return new Funnel(...params);
};
module.exports.Funnel = Funnel;