|
"use strict"; |
|
Object.defineProperty(exports, "__esModule", { value: true }); |
|
const fs = require("fs"); |
|
const path = require("path"); |
|
const quickTemp = require("quick-temp"); |
|
const mapSeries = require("promise-map-series"); |
|
const rimraf = require("rimraf"); |
|
const symlinkOrCopy = require("symlink-or-copy"); |
|
const symlinkOrCopySync = symlinkOrCopy.sync; |
|
const READ_COMPAT_FEATURES = Object.freeze({ |
|
|
|
persistentOutputFlag: true, |
|
sourceDirectories: true, |
|
|
|
needsCacheFlag: true, |
|
}); |
|
|
|
|
|
class ReadCompat { |
|
constructor(plugin) { |
|
this.pluginInterface = plugin.__broccoliGetInfo__(READ_COMPAT_FEATURES); |
|
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 (let i = 0; i < this.pluginInterface.inputNodes.length; i++) { |
|
this.inputPaths.push(path.join(this.inputBasePath, i + '')); |
|
} |
|
} |
|
this.pluginInterface.setup({}, { |
|
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; |
|
} |
|
} |
|
} |
|
read(readTree) { |
|
if (!this.pluginInterface.persistentOutput) { |
|
rimraf.sync(this.outputPath); |
|
fs.mkdirSync(this.outputPath); |
|
} |
|
return mapSeries(this.pluginInterface.inputNodes, readTree) |
|
.then((outputPaths) => { |
|
const priorBuildInputNodeOutputPaths = this._priorBuildInputNodeOutputPaths; |
|
|
|
|
|
|
|
|
|
for (let i = 0; i < outputPaths.length; i++) { |
|
const priorPath = priorBuildInputNodeOutputPaths[i]; |
|
const currentPath = outputPaths[i]; |
|
|
|
|
|
const hasDifferentPath = priorPath !== currentPath; |
|
const forceReSymlinking = !symlinkOrCopy.canSymlink || hasDifferentPath; |
|
if (forceReSymlinking) { |
|
|
|
if (priorPath) { |
|
rimraf.sync(this.inputPaths[i]); |
|
} |
|
symlinkOrCopySync(currentPath, this.inputPaths[i]); |
|
} |
|
} |
|
|
|
this._priorBuildInputNodeOutputPaths = outputPaths; |
|
return this.callbackObject.build(); |
|
}) |
|
.then(() => this.outputPath); |
|
} |
|
cleanup() { |
|
quickTemp.remove(this, 'outputPath'); |
|
quickTemp.remove(this, 'cachePath'); |
|
quickTemp.remove(this, 'inputBasePath'); |
|
} |
|
} |
|
exports.default = ReadCompat; |
|
|