forked from broccolijs/broccoli-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathread_compat.js
104 lines (84 loc) · 3.21 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
'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
// 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 + ''))
}
}
this.pluginInterface.setup(null, {
inputPaths: this.inputPaths,
outputPath: this.outputPath,
cachePath: this.cachePath
})
this.callbackObject = this.pluginInterface.getCallbackObject()
if (plugin.description == null) {
plugin.description = this.pluginInterface.name
if (this.pluginInterface.annotation != null) {
plugin.description += ': ' + this.pluginInterface.annotation
}
}
}
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) {
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;
return self.callbackObject.build()
})
.then(function() {
return self.outputPath
})
}
ReadCompat.prototype.cleanup = function() {
quickTemp.remove(this, 'outputPath')
quickTemp.remove(this, 'cachePath')
quickTemp.remove(this, 'inputBasePath')
}