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

133 lines
3.4 KiB
JavaScript
Raw Normal View History

2015-02-27 00:25:57 +00:00
'use strict';
2015-06-28 14:41:57 +00:00
var debug = require('debug')('ff:assets:assets-builder');
2015-02-27 00:25:57 +00:00
var async = require('async');
var config = require('./assets-builder-config');
function buildStepAssemblerRun(builder, buildConfig, assembler, callback) {
2015-06-28 14:41:57 +00:00
debug('runAssembler type: %s',assembler.config.assetType);
2015-02-27 00:25:57 +00:00
assembler.run(callback);
}
function buildStepAssemblerFill(builder, buildConfig, assembler, callback) {
2015-06-28 14:41:57 +00:00
var assemblerFill = buildConfig.assemblerFill;
if (assemblerFill === undefined) {
assemblerFill = builder.config.assemblerFill;
debug('assemblerFill from default');
}
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) {
2015-06-28 14:41:57 +00:00
var assemblerCreate = buildConfig.assemblerCreate;
if (assemblerCreate === undefined) {
assemblerCreate = builder.config.assemblerCreate;
debug('assemblerCreate from default');
}
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) {
2015-06-28 14:41:57 +00:00
var configFill = buildConfig.configFill;
if (configFill === undefined) {
configFill = builder.config.configFill;
debug('configFill from default');
}
2015-02-27 00:25:57 +00:00
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) {
2015-06-28 14:41:57 +00:00
var configCreate = buildConfig.configCreate;
if (configCreate === undefined) {
configCreate = builder.config.configCreate;
debug('configCreate from default');
}
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 = '';
}
AssetsBuilder.prototype.runAll = function(callback) {
var self = this;
2015-06-28 14:41:57 +00:00
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');
}
2015-06-28 14:41:57 +00:00
debug('runAsset started for: %s',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;