pp-parachute
Version:
Airdrop JS Applications
175 lines (149 loc) • 5.44 kB
JavaScript
var assert = require('../util/assert');
var EventEmitter = require('events').EventEmitter;
var path = require('path');
var ParachuteBase = function (name) {
assert.isString(name, 'Name is required and must be a string');
this.emitter = new EventEmitter();
this.name = name;
this.viewFile = null;
this.bundlers = {};
this.outputDirectory = null;
this.rootPath = null;
this.parent = null;
this.servedBuildSucceeded = null;
this.servedBuildFailed = null;
this.servedBuildCompleted = null;
this.servedBuildInitialized = null;
};
ParachuteBase.prototype.inheritFrom = function(parent) {
assert(!parent || parent instanceof ParachuteBase);
this.parent = parent;
return this;
};
ParachuteBase.prototype.tap = function(fn) {
fn && fn(this);
return this;
};
ParachuteBase.prototype.handle = function(eventName, fn) {
this.emitter.on(eventName, fn);
return this;
};
ParachuteBase.prototype.emit = function() {
this.emitter.emit.apply(this.emitter, arguments);
};
ParachuteBase.prototype.onServedBuildInitialized = function(fn) {
assert.isFunction(fn, 'onServedBuildInitialized handler must a function');
this.servedBuildInitialized = fn;
return this;
};
ParachuteBase.prototype.onServedBuildFailed = function(fn) {
assert.isFunction(fn, 'onServedBuildFailed handler must a function');
this.servedBuildFailed = fn;
return this;
};
ParachuteBase.prototype.onServedBuildSucceeded = function(fn) {
assert.isFunction(fn, 'onServedBuildSucceeded handle must be a function');
this.servedBuildSucceeded = fn;
return this;
};
ParachuteBase.prototype.onServedBuildCompleted = function(fn) {
assert.isFunction(fn, 'onServedBuildCompleted handle must be a function');
this.servedBuildCompleted = fn;
return this;
};
ParachuteBase.prototype.getServedBuildFailedHandler = function() {
if(this.servedBuildFailed) return this.servedBuildFailed;
if(this.parent) return this.parent.getServedBuildFailedHandler();
return null;
};
ParachuteBase.prototype.getServedBuildCompletedHandler = function() {
if(this.servedBuildCompleted) return this.servedBuildCompleted;
if(this.parent) return this.parent.getServedBuildCompletedHandler();
return null;
};
ParachuteBase.prototype.getServedBuildSucceededHandler = function() {
if(this.servedBuildSucceeded) return this.servedBuildSucceeded;
if(this.parent) return this.parent.getServedBuildSucceededHandler();
return null;
};
ParachuteBase.prototype.getServedBuildInitializedHandler = function() {
if(this.servedBuildInitialized) return this.servedBuildInitialized;
if(this.parent) return this.parent.getServedBuildInitializedHandler();
return null;
};
ParachuteBase.prototype.getActiveBundlers = function () {
//todo this can cached to not recompute this all the time
var activeBundlers = this.parent && this.parent.getActiveBundlers() || {};
var bundlerIds = Object.keys(this.bundlers);
for(var i = 0; i < bundlerIds.length; i++) {
var id = bundlerIds[i];
var bundlerProperties = this.bundlers[id];
if(bundlerProperties.skipped) {
delete activeBundlers[id];
} else {
activeBundlers[id] = bundlerProperties.bundler;
}
}
return activeBundlers;
};
ParachuteBase.prototype.getBundlerById = function(id) {
if(this.bundlers[id]) return this.bundlers[id];
if(this.parent) return this.parent.bundlers[id];
throw new Error('No bundler in chain with id: ' + id);
};
ParachuteBase.prototype.useBundler = function (bundler, mountPath) {
assert.isString(mountPath);
if(mountPath.indexOf('/') === -1) {
mountPath = '/' + mountPath;
}
this.bundlers[bundler.bundlerId] = {
skipped: false,
bundler: bundler,
directoryName: path.dirname(mountPath),
fileName: path.basename(mountPath),
mountPath: mountPath
};
return this;
};
ParachuteBase.prototype.skipBundler = function (bundler) {
this.bundlers[bundler.bundlerId] = {
skipped: true,
bundler: bundler,
mountPath: null,
fileName: null
};
return this;
};
ParachuteBase.prototype.shutdown = function() {
var bundlerIds = Object.keys(this.bundlers);
bundlerIds.forEach(function(id) {
this.bundlers[id].shutdown();
}, this);
};
ParachuteBase.prototype.getRootPath = function () {
if(this.rootPath) return this.rootPath;
if(this.parent) return this.parent.getRootPath();
return null;
};
ParachuteBase.prototype.getViewFile = function() {
if (this.viewFile) return path.join(this.getRootPath(), this.viewFile);
if (this.parent) return this.parent.getViewFile();
return path.join(this.getRootPath(), 'views/' + this.name + '.html');
};
ParachuteBase.prototype.setViewFile = function(viewFile) {
this.viewFile = viewFile;
return this;
};
ParachuteBase.prototype.getAssetRoot = function() {
return path.join(this.getRootPath(), this.getOutputDirectory());
};
ParachuteBase.prototype.getOutputDirectory = function() {
if (this.outputDirectory) return this.outputDirectory;
if (this.parent) return this.parent.getOutputDirectory();
return null;
};
ParachuteBase.prototype.setOutputDirectory = function(outputDirectory) {
this.outputDirectory = outputDirectory;
return this;
};
module.exports = ParachuteBase;