1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | const fs_1 = __importDefault(require("fs"));
|
6 | const tmp_1 = __importDefault(require("tmp"));
|
7 | const path_1 = __importDefault(require("path"));
|
8 | const broccoli_source_1 = require("broccoli-source");
|
9 | const transform_node_1 = __importDefault(require("./wrappers/transform-node"));
|
10 | const source_node_1 = __importDefault(require("./wrappers/source-node"));
|
11 | const builder_1 = __importDefault(require("./errors/builder"));
|
12 | const node_setup_1 = __importDefault(require("./errors/node-setup"));
|
13 | const build_1 = __importDefault(require("./errors/build"));
|
14 | const cancelation_request_1 = __importDefault(require("./cancelation-request"));
|
15 | const filter_map_1 = __importDefault(require("./utils/filter-map"));
|
16 | const events_1 = require("events");
|
17 | const node_1 = __importDefault(require("./wrappers/node"));
|
18 | const heimdall = require('heimdalljs');
|
19 | const underscoreString = require('underscore.string');
|
20 | const broccoliNodeInfo = require('broccoli-node-info');
|
21 | const logger = require('heimdalljs-logger')('broccoli:builder');
|
22 |
|
23 | tmp_1.default.setGracefulCleanup();
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | class Builder extends events_1.EventEmitter {
|
42 | constructor(outputNode, options = {}) {
|
43 | super();
|
44 | this.outputNode = outputNode;
|
45 | this.tmpdir = options.tmpdir;
|
46 | this.unwatchedPaths = [];
|
47 | this.watchedPaths = [];
|
48 |
|
49 |
|
50 | this._nodeWrappers = new Map();
|
51 |
|
52 | this.outputNodeWrapper = this.makeNodeWrapper(this.outputNode);
|
53 |
|
54 |
|
55 | this.checkInputPathsExist();
|
56 | this.setupTmpDirs();
|
57 | this.setupHeimdall();
|
58 | this._cancelationRequest = undefined;
|
59 |
|
60 |
|
61 | try {
|
62 | this.setupNodes();
|
63 | this.outputPath = this.outputNodeWrapper.outputPath;
|
64 | this.buildId = 0;
|
65 | }
|
66 | catch (e) {
|
67 | this.cleanup();
|
68 | throw e;
|
69 | }
|
70 | }
|
71 | static get BuilderError() { return builder_1.default; }
|
72 | static get InvalidNodeError() { return broccoliNodeInfo.InvalidNodeError; }
|
73 | static get NodeSetupError() { return node_setup_1.default; }
|
74 | static get BuildError() { return build_1.default; }
|
75 | static get NodeWrapper() { return node_1.default; }
|
76 | static get TransformNodeWrapper() { return transform_node_1.default; }
|
77 | static get SourceNodeWrapper() { return source_node_1.default; }
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | async build() {
|
85 | if (this._cancelationRequest) {
|
86 | throw new builder_1.default('Cannot start a build if one is already running');
|
87 | }
|
88 | let pipeline = Promise.resolve();
|
89 | this.buildId++;
|
90 | for (const nw of this._nodeWrappers.values()) {
|
91 |
|
92 | nw.buildState = {};
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | pipeline = pipeline.then(async () => {
|
100 |
|
101 | this._cancelationRequest.throwIfRequested();
|
102 | this.emit('beginNode', nw);
|
103 | try {
|
104 | await nw.build();
|
105 | this.emit('endNode', nw);
|
106 | }
|
107 | catch (e) {
|
108 | this.emit('endNode', nw);
|
109 |
|
110 |
|
111 |
|
112 | throw new build_1.default(e, nw);
|
113 | }
|
114 | });
|
115 | }
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | this._cancelationRequest = new cancelation_request_1.default(pipeline);
|
122 | try {
|
123 | await pipeline;
|
124 | this.buildHeimdallTree(this.outputNodeWrapper);
|
125 | }
|
126 | finally {
|
127 | let buildsSkipped = filter_map_1.default(this._nodeWrappers.values(), (nw) => nw.buildState.built === false).length;
|
128 | logger.debug(`Total nodes skipped: ${buildsSkipped} out of ${this._nodeWrappers.size}`);
|
129 | this._cancelationRequest = null;
|
130 | }
|
131 | }
|
132 | async cancel() {
|
133 | if (this._cancelationRequest) {
|
134 | return this._cancelationRequest.cancel();
|
135 | }
|
136 | }
|
137 |
|
138 | async cleanup() {
|
139 | try {
|
140 | await this.cancel();
|
141 | }
|
142 | finally {
|
143 | await this.builderTmpDirCleanup();
|
144 | }
|
145 | }
|
146 |
|
147 |
|
148 | makeNodeWrapper(node, _stack = []) {
|
149 | let wrapper = this._nodeWrappers.get(node);
|
150 | if (wrapper !== undefined) {
|
151 | return wrapper;
|
152 | }
|
153 |
|
154 | const originalNode = node;
|
155 | if (typeof node === 'string') {
|
156 | node = new broccoli_source_1.WatchedDir(node, { annotation: 'string node' });
|
157 | }
|
158 |
|
159 | let nodeInfo;
|
160 | try {
|
161 | nodeInfo = broccoliNodeInfo.getNodeInfo(node);
|
162 | }
|
163 | catch (e) {
|
164 | if (!(e instanceof broccoliNodeInfo.InvalidNodeError))
|
165 | throw e;
|
166 |
|
167 |
|
168 | const messageSuffix = _stack.length > 0
|
169 | ? '\nused as input node to ' +
|
170 | _stack[_stack.length - 1].label +
|
171 | _stack[_stack.length - 1].formatInstantiationStackForTerminal()
|
172 | : '\nused as output node';
|
173 | throw new broccoliNodeInfo.InvalidNodeError(e.message + messageSuffix);
|
174 | }
|
175 |
|
176 | let label = nodeInfo.name;
|
177 | const labelExtras = [];
|
178 | if (nodeInfo.nodeType === 'source')
|
179 | labelExtras.push(nodeInfo.sourceDirectory);
|
180 | if (nodeInfo.annotation != null)
|
181 | labelExtras.push(nodeInfo.annotation);
|
182 | if (labelExtras.length > 0)
|
183 | label += ' (' + labelExtras.join('; ') + ')';
|
184 |
|
185 |
|
186 | const nodeWrapper = nodeInfo.nodeType === 'transform' ? new transform_node_1.default() : new source_node_1.default();
|
187 | nodeWrapper.nodeInfo = nodeInfo;
|
188 | nodeWrapper.originalNode = originalNode;
|
189 | nodeWrapper.node = node;
|
190 | nodeWrapper.label = label;
|
191 |
|
192 | for (let i = 0; i < _stack.length; i++) {
|
193 | if (_stack[i].node === originalNode) {
|
194 | let cycleMessage = 'Cycle in node graph: ';
|
195 | for (let j = i; j < _stack.length; j++) {
|
196 | cycleMessage += _stack[j].label + ' -> ';
|
197 | }
|
198 | cycleMessage += nodeWrapper.label;
|
199 | throw new builder_1.default(cycleMessage);
|
200 | }
|
201 | }
|
202 |
|
203 |
|
204 | let inputNodeWrappers = [];
|
205 | if (nodeInfo.nodeType === 'transform') {
|
206 | const newStack = _stack.concat([nodeWrapper]);
|
207 | inputNodeWrappers = nodeInfo.inputNodes.map((inputNode) => {
|
208 | return this.makeNodeWrapper(inputNode, newStack);
|
209 | });
|
210 | }
|
211 | else {
|
212 |
|
213 | if (nodeInfo.watched) {
|
214 | this.watchedPaths.push(nodeInfo.sourceDirectory);
|
215 | }
|
216 | else {
|
217 | this.unwatchedPaths.push(nodeInfo.sourceDirectory);
|
218 | }
|
219 | }
|
220 |
|
221 |
|
222 | nodeWrapper.inputNodeWrappers = inputNodeWrappers;
|
223 | nodeWrapper.id = this._nodeWrappers.size;
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | this._nodeWrappers.set(nodeWrapper.originalNode, nodeWrapper);
|
231 | return nodeWrapper;
|
232 | }
|
233 | get watchedSourceNodeWrappers() {
|
234 | return filter_map_1.default(this._nodeWrappers.values(), (nw) => {
|
235 | return nw.nodeInfo.nodeType === 'source' && nw.nodeInfo.watched;
|
236 | });
|
237 | }
|
238 | checkInputPathsExist() {
|
239 |
|
240 | for (let i = 0; i < this.watchedPaths.length; i++) {
|
241 | let isDirectory;
|
242 | try {
|
243 | isDirectory = fs_1.default.statSync(this.watchedPaths[i]).isDirectory();
|
244 | }
|
245 | catch (err) {
|
246 | throw new builder_1.default('Directory not found: ' + this.watchedPaths[i]);
|
247 | }
|
248 | if (!isDirectory) {
|
249 | throw new builder_1.default('Not a directory: ' + this.watchedPaths[i]);
|
250 | }
|
251 | }
|
252 | }
|
253 | setupTmpDirs() {
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 | const tmpObj = tmp_1.default.dirSync({
|
273 | prefix: 'broccoli-',
|
274 | unsafeCleanup: true,
|
275 | dir: this.tmpdir || undefined,
|
276 | });
|
277 | this.builderTmpDir = tmpObj.name;
|
278 | this.builderTmpDirCleanup = tmpObj.removeCallback;
|
279 | for (let nodeWrapper of this._nodeWrappers.values()) {
|
280 | if (nodeWrapper.nodeInfo.nodeType === 'transform') {
|
281 | nodeWrapper.inputPaths = nodeWrapper.inputNodeWrappers.map((nw) => nw.outputPath);
|
282 | nodeWrapper.outputPath = this.mkTmpDir(nodeWrapper, 'out');
|
283 | if (nodeWrapper.nodeInfo.needsCache) {
|
284 | nodeWrapper.cachePath = this.mkTmpDir(nodeWrapper, 'cache');
|
285 | }
|
286 | }
|
287 | else {
|
288 |
|
289 |
|
290 | nodeWrapper.outputPath = nodeWrapper.nodeInfo.sourceDirectory;
|
291 | }
|
292 | }
|
293 | }
|
294 |
|
295 |
|
296 |
|
297 | mkTmpDir(nodeWrapper, type) {
|
298 | let nameAndAnnotation = nodeWrapper.nodeInfo.name + ' ' + (nodeWrapper.nodeInfo.annotation || '');
|
299 |
|
300 |
|
301 | let suffix = underscoreString.underscored(nameAndAnnotation.substr(0, 60));
|
302 | suffix = underscoreString.slugify(suffix).replace(/-/g, '_');
|
303 |
|
304 | const paddedId = underscoreString.pad('' + nodeWrapper.id, ('' + this._nodeWrappers.size).length, '0');
|
305 | const dirname = type + '-' + paddedId + '-' + suffix;
|
306 | const tmpDir = path_1.default.join(this.builderTmpDir, dirname);
|
307 | fs_1.default.mkdirSync(tmpDir);
|
308 | return tmpDir;
|
309 | }
|
310 |
|
311 | get nodeWrappers() {
|
312 | return [...this._nodeWrappers.values()];
|
313 | }
|
314 | setupNodes() {
|
315 | for (let nw of this._nodeWrappers.values()) {
|
316 | try {
|
317 | nw.setup(this.features);
|
318 | }
|
319 | catch (err) {
|
320 | throw new node_setup_1.default(err, nw);
|
321 | }
|
322 | }
|
323 | }
|
324 | setupHeimdall() {
|
325 | this.on('beginNode', node => {
|
326 | let name;
|
327 | if (node instanceof source_node_1.default) {
|
328 | name = node.nodeInfo.sourceDirectory;
|
329 | }
|
330 | else {
|
331 | name = node.nodeInfo.annotation || node.nodeInfo.name;
|
332 | }
|
333 | node.__heimdall_cookie__ = heimdall.start({
|
334 | name,
|
335 | label: node.label,
|
336 | broccoliNode: true,
|
337 | broccoliId: node.id,
|
338 |
|
339 |
|
340 | broccoliCachedNode: false,
|
341 | broccoliPluginName: node.nodeInfo.name,
|
342 | });
|
343 | node.__heimdall__ = heimdall.current;
|
344 | });
|
345 | this.on('endNode', node => {
|
346 | if (node.__heimdall__) {
|
347 | node.__heimdall_cookie__.stop();
|
348 | }
|
349 | });
|
350 | }
|
351 | buildHeimdallTree(outputNodeWrapper) {
|
352 | if (!outputNodeWrapper.__heimdall__) {
|
353 | return;
|
354 | }
|
355 |
|
356 | reParentNodes(outputNodeWrapper);
|
357 |
|
358 | aggregateTime();
|
359 | }
|
360 | get features() {
|
361 | return broccoliNodeInfo.features;
|
362 | }
|
363 | }
|
364 | function reParentNodes(outputNodeWrapper) {
|
365 |
|
366 | const seen = new Set();
|
367 | const queue = [outputNodeWrapper];
|
368 | let node;
|
369 | let parent;
|
370 | let stack = [];
|
371 | while ((node = queue.pop()) !== undefined) {
|
372 | if (parent === node) {
|
373 | parent = stack.pop();
|
374 | }
|
375 | else {
|
376 | queue.push(node);
|
377 | let heimdallNode = node.__heimdall__;
|
378 | if (heimdallNode === undefined || seen.has(heimdallNode)) {
|
379 |
|
380 | const cookie = heimdall.start(Object.assign({}, heimdallNode.id));
|
381 | heimdallNode = heimdall.current;
|
382 | heimdallNode.id.broccoliCachedNode = true;
|
383 | cookie.stop();
|
384 | heimdallNode.stats.time.self = 0;
|
385 | }
|
386 | else {
|
387 | seen.add(heimdallNode);
|
388 |
|
389 | const inputNodeWrappers = node.inputNodeWrappers;
|
390 | for (let i = inputNodeWrappers.length - 1; i >= 0; i--) {
|
391 | queue.push(inputNodeWrappers[i]);
|
392 | }
|
393 | }
|
394 | if (parent) {
|
395 | heimdallNode.remove();
|
396 | parent.__heimdall__.addChild(heimdallNode);
|
397 | stack.push(parent);
|
398 | }
|
399 | parent = node;
|
400 | }
|
401 | }
|
402 | }
|
403 | function aggregateTime() {
|
404 | let queue = [heimdall.current];
|
405 | let stack = [];
|
406 | let parent;
|
407 | let node;
|
408 | while ((node = queue.pop()) !== undefined) {
|
409 | if (parent === node) {
|
410 | parent = stack.pop();
|
411 | if (parent !== undefined) {
|
412 | parent.stats.time.total += node.stats.time.total;
|
413 | }
|
414 | }
|
415 | else {
|
416 | const children = node._children;
|
417 | queue.push(node);
|
418 | for (let i = children.length - 1; i >= 0; i--) {
|
419 | queue.push(children[i]);
|
420 | }
|
421 | if (parent) {
|
422 | stack.push(parent);
|
423 | }
|
424 | node.stats.time.total = node.stats.time.self;
|
425 | parent = node;
|
426 | }
|
427 | }
|
428 | }
|
429 | module.exports = Builder;
|
430 |
|
\ | No newline at end of file |