forked from hjdivad/broccoli-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_compat.js
138 lines (111 loc) · 4.18 KB
/
read_compat.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
'use strict'
var fs = require('fs')
var path = require('path')
var quickTemp = require('quick-temp')
var mapSeries = require('promise-map-series')
var rimraf = require('rimraf')
var symlinkOrCopy = require('symlink-or-copy')
var symlinkOrCopySync = symlinkOrCopy.sync
const FSTree = require('fs-tree-diff');
const FSMergeTree = require('fs-tree-diff/lib/fs-merge-tree');
const RSVP = require('rsvp');
// Mimic how a Broccoli builder would call a plugin, using quickTemp to create
// directories
module.exports = ReadCompat
function ReadCompat(plugin) {
this.pluginInterface = plugin.__broccoliGetInfo__()
quickTemp.makeOrReuse(this, 'outputPath', this.pluginInterface.name)
if (this.pluginInterface.needsCache) {
quickTemp.makeOrReuse(this, 'cachePath', this.pluginInterface.name)
} else {
this.cachePath = undefined
}
quickTemp.makeOrReuse(this, 'inputBasePath', this.pluginInterface.name)
this.inputPaths = []
this._priorBuildInputNodeOutputPaths = [];
if (this.pluginInterface.inputNodes.length === 1) {
this.inputPaths.push(this.inputBasePath)
this._priorBuildInputNodeOutputPaths.push(this.inputBasePath);
} else {
for (var i = 0; i < this.pluginInterface.inputNodes.length; i++) {
this.inputPaths.push(path.join(this.inputBasePath, i + ''))
}
}
if (plugin.description == null) {
plugin.description = this.pluginInterface.name
if (this.pluginInterface.annotation != null) {
plugin.description += ': ' + this.pluginInterface.annotation
}
}
this._hasSetup = false;
}
function inputNode2FSTreeInput(outputPaths, node, index) {
if (typeof node === 'string') {
return path.resolve(node);
}
return node.out || node.outputPath || outputPaths[index];
}
ReadCompat.prototype.setupFS = function (outputPaths) {
if (this._hasSetup) { return; }
this.inTree = new FSMergeTree({
inputs: this.pluginInterface.inputNodes.map(inputNode2FSTreeInput.bind(null, outputPaths)),
srcTree: !this.pluginInterface.fsFacade,
});
this.outTree = new FSTree({
root: this.outputPath,
srcTree: !this.pluginInterface.fsFacade,
});
this.pluginInterface.setup(null, {
inTree: this.inTree,
outTree: this.outTree,
inputPaths: this.inputPaths,
outputPath: this.outputPath,
cachePath: this.cachePath
})
this.callbackObject = this.pluginInterface.getCallbackObject()
this._hasSetup = true;
}
ReadCompat.prototype.read = function(readTree) {
var self = this
if (!this.pluginInterface.persistentOutput) {
rimraf.sync(this.outputPath)
fs.mkdirSync(this.outputPath)
}
return mapSeries(this.pluginInterface.inputNodes, readTree)
.then(function(outputPaths) {
self.setupFS(outputPaths);
var priorBuildInputNodeOutputPaths = self._priorBuildInputNodeOutputPaths;
// In old .read-based Broccoli, the inputNodes's outputPaths can change
// on each rebuild. But the new API requires that our plugin sees fixed
// input paths. Therefore, we symlink the inputNodes' outputPaths to our
// (fixed) inputPaths on each .read.
for (var i = 0; i < outputPaths.length; i++) {
var priorPath = priorBuildInputNodeOutputPaths[i]
var currentPath = outputPaths[i]
// if this output path is different from last builds or
// if we cannot symlink then clear and symlink/copy manually
var hasDifferentPath = priorPath !== currentPath
var forceReSymlinking = !symlinkOrCopy.canSymlink || hasDifferentPath
if (forceReSymlinking) {
// avoid `rimraf.sync` for initial build
if (priorPath) {
rimraf.sync(self.inputPaths[i])
}
symlinkOrCopySync(currentPath, self.inputPaths[i])
}
}
// save for next builds comparison
self._priorBuildInputNodeOutputPaths = outputPaths;
self.inTree.map((fsTree, i) => fsTree.reread(outputPaths[i]));
self.outTree.start();
return RSVP.resolve(self.callbackObject.build()).finally(() => self.outTree.stop());
})
.then(function() {
return self.outputPath
})
}
ReadCompat.prototype.cleanup = function() {
quickTemp.remove(this, 'outputPath')
quickTemp.remove(this, 'cachePath')
quickTemp.remove(this, 'inputBasePath')
}