2
0
Fork 0
node-ff-assets/lib/assets-builder.js

125 lines
3.5 KiB
JavaScript
Raw Permalink Normal View History

2015-02-27 00:25:57 +00:00
'use strict';
var events = require('events');
var async = require('async');
var config = require('./assets-builder-config');
function buildStepAssemblerRun(builder, buildConfig, assembler, callback) {
2015-02-27 20:50:19 +00:00
builder.emit('log','debug','runAssembler '+assembler.config.assetType);
2015-02-27 00:25:57 +00:00
assembler.run(callback);
}
function buildStepAssemblerFill(builder, buildConfig, assembler, callback) {
var assemblerFill = buildConfig.assemblerFill || builder.config.assemblerFill;
2015-02-27 20:50:19 +00:00
builder.emit('log','debug','assemblerFill: '+Object.keys(assemblerFill));
2015-02-27 00:25:57 +00:00
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;
2015-02-27 20:50:19 +00:00
builder.emit('log','debug','assemblerCreate: '+Object.keys(assemblerCreate));
2015-02-27 00:25:57 +00:00
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;
}
2015-02-27 20:50:19 +00:00
builder.emit('log','debug','configFill: '+Object.keys(configFill));
2015-02-27 00:25:57 +00:00
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;
2015-02-27 20:50:19 +00:00
builder.emit('log','debug','configCreate: '+Object.keys(configCreate));
2015-02-27 00:25:57 +00:00
configCreate(function(err, assemblerConfig) {
if (err) {
callback(err);
} else {
buildStepConfigFill(builder, buildConfig, assemblerConfig, callback);
}
});
}
function AssetsBuilder(config) {
2015-02-27 02:31:44 +00:00
if (config === undefined) {
throw new Error('no config');
}
2015-02-27 00:25:57 +00:00
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;
2015-02-27 20:50:19 +00:00
self.emit('log','debug','runAll started.');
2015-02-27 00:25:57 +00:00
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));
}
2015-02-27 14:19:48 +00:00
async.series(runners,callback);
2015-02-27 00:25:57 +00:00
}
});
}
});
};
AssetsBuilder.prototype.runAsset = function(name) {
var self = this;
2015-02-27 20:50:19 +00:00
if (name === undefined) {
throw new Error('no name');
}
self.emit('log','debug','runAsset started for: '+name);
2015-02-27 00:25:57 +00:00
return function(callback) {
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) {
2015-02-27 20:50:19 +00:00
callback(new Error('no asset config found'));
2015-02-27 00:25:57 +00:00
}
self.currentAssetType = name;
buildStepConfigCreate(self,buildConfig,callback);
}
});
};
};
module.exports = AssetsBuilder;