UNPKG

1.47 kBJavaScriptView Raw
1'use strict';
2
3const Plugin = require('broccoli-plugin');
4const MergeTrees = require('merge-trees');
5
6class BroccoliMergeTrees extends Plugin {
7 constructor(inputNodes, options = {}) {
8 let name = 'broccoli-merge-trees:' + (options.annotation || '');
9 if (!Array.isArray(inputNodes)) {
10 throw new TypeError(name + ': Expected array, got: [' + inputNodes +']');
11 }
12 super(inputNodes, {
13 persistentOutput: true,
14 needsCache: false,
15 annotation: options.annotation
16 });
17
18 this.inputNodes = inputNodes;
19 this.options = options;
20 }
21
22 build() {
23 if (this.mergeTrees == null) {
24 // Defer instantiation until the first build because we only
25 // have this.inputPaths and this.outputPath once we build.
26 this.mergeTrees = new MergeTrees(this.inputPaths, this.outputPath, {
27 overwrite: this.options.overwrite,
28 annotation: this.options.annotation
29 });
30 }
31
32 try {
33 this.mergeTrees.merge();
34 } catch(err) {
35 if (err !== null && typeof err === 'object') {
36 let nodesList = this.inputNodes.map((node, i) => ` ${i+1}. ${node.toString()}`).join('\n');
37 let message = `${this.toString()} error while merging the following:\n${nodesList}`;
38
39 err.message = `${message}\n${err.message}`;
40 }
41 throw err;
42 }
43 }
44}
45
46module.exports = function mergeTrees(...params) {
47 return new BroccoliMergeTrees(...params);
48};
49
50module.exports.MergeTrees = BroccoliMergeTrees;