'use strict'; var debug = require('debug')('ff:assets:assets-builder'); var async = require('async'); var config = require('./assets-builder-config'); function buildStepAssemblerRun(builder, buildConfig, assembler, callback) { debug('runAssembler type: %s',assembler.config.assetType); assembler.run(callback); } function buildStepAssemblerFill(builder, buildConfig, assembler, callback) { var assemblerFill = buildConfig.assemblerFill; if (assemblerFill === undefined) { assemblerFill = builder.config.assemblerFill; debug('assemblerFill from default'); } assemblerFill(assembler, function(err) { if (err) { callback(err); } else { buildStepAssemblerRun(builder, buildConfig, assembler, callback); } }); } function buildStepAssemblerCreate(builder, buildConfig, assemblerConfig, callback) { var assemblerCreate = buildConfig.assemblerCreate; if (assemblerCreate === undefined) { assemblerCreate = builder.config.assemblerCreate; debug('assemblerCreate from default'); } 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; if (configFill === undefined) { configFill = builder.config.configFill; debug('configFill from default'); } 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; if (configCreate === undefined) { configCreate = builder.config.configCreate; debug('configCreate from default'); } configCreate(function(err, assemblerConfig) { if (err) { callback(err); } else { buildStepConfigFill(builder, buildConfig, assemblerConfig, callback); } }); } function AssetsBuilder(config) { if (config === undefined) { throw new Error('no config'); } this.config = config; this.startTime = 0; this.currentAssetType = ''; } AssetsBuilder.prototype.runAll = function(callback) { var self = this; debug('runAll started.'); 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,callback); } }); } }); }; AssetsBuilder.prototype.runAsset = function(name) { var self = this; if (name === undefined) { throw new Error('no name'); } debug('runAsset started for: %s',name); 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) { callback(new Error('no asset config found')); } self.currentAssetType = name; buildStepConfigCreate(self,buildConfig,callback); } }); }; }; module.exports = AssetsBuilder;