2
0
Fork 0
node-ff-assets/lib/assets-builder.js
2015-02-27 01:25:57 +01:00

116 lines
2.9 KiB
JavaScript

'use strict';
var events = require('events');
var async = require('async');
var config = require('./assets-builder-config');
function buildStepAssemblerRun(builder, buildConfig, assembler, callback) {
assembler.run(callback);
}
function buildStepAssemblerFill(builder, buildConfig, assembler, callback) {
var assemblerFill = buildConfig.assemblerFill || builder.config.assemblerFill;
assemblerFill(assembler, function(err) {
if (err) {
callback(err);
} else {
buildStepAssemblerRun(builder, buildConfig, assembler, callback);
}
});
}
function buildStepAssemblerCreate(builder, buildConfig, assemblerConfig, callback) {
var assemblerCreate = buildConfig.assemblerCreate || builder.config.assemblerCreate;
assemblerCreate(assemblerConfig, function(err, assembler) {
if (err) {
callback(err);
} else {
buildStepAssemblerFill(builder, buildConfig, assembler, callback);
}
});
}
function buildStepConfigFill(builder, buildConfig, assemblerConfig, callback) {
var configFill = buildConfig.configFill || builder.config.configFill;
if (assemblerConfig.assetType === undefined) {
assemblerConfig.assetType = builder.currentAssetType;
}
configFill(assemblerConfig, function(err) {
if (err) {
callback(err);
} else {
buildStepAssemblerCreate(builder, buildConfig, assemblerConfig, callback);
}
});
}
function buildStepConfigCreate(builder, buildConfig, callback) {
var configCreate = buildConfig.configCreate || builder.config.configCreate;
configCreate(function(err, assemblerConfig) {
if (err) {
callback(err);
} else {
buildStepConfigFill(builder, buildConfig, assemblerConfig, callback);
}
});
}
function AssetsBuilder(config) {
this.config = config;
this.startTime = 0;
this.currentAssetType = '';
events.EventEmitter.call(this);
}
AssetsBuilder.prototype.__proto__ = events.EventEmitter.prototype;
AssetsBuilder.prototype.runAll = function(callback) {
var self = this;
config.checkConfig(self.config,function(err) {
if (err) {
callback(err);
} else {
config.fillDefaults(self.config, function (err) {
if (err) {
callback(err);
} else {
var runners = [];
for (var i = 0; i < self.config.buildOrder.length; i++) {
var assetKey = self.config.buildOrder[i];
runners.push(self.runAsset(assetKey));
}
async.series(runners,self.config.buildDone);
}
});
}
});
};
AssetsBuilder.prototype.runAsset = function(name) {
var self = this;
return function(callback) {
if (name === undefined) {
throw new Error('no name');
}
if (callback === undefined) {
throw new Error('no callback');
}
config.checkConfig(self.config,function(err) {
if (err) {
callback(err);
} else {
var buildConfig = self.config.assets[name];
if (buildConfig === undefined) {
throw new Error('no asset config found');
}
self.currentAssetType = name;
buildStepConfigCreate(self,buildConfig,callback);
}
});
};
};
module.exports = AssetsBuilder;