2
0
Fork 0
node-ff-assets/lib/factory.js
2015-02-27 03:31:44 +01:00

147 lines
3.4 KiB
JavaScript

'use strict';
var fs = require('fs');
var assemblerConstructors = {
readFile : function() {
return function(file, callback) {
fs.readFile(file, function(err, data) {
if (err) {
callback(err);
} else {
callback(null, data);
}
});
};
},
readFileMinify : function(options) {
if (options === undefined) {
options = {};
}
var minify = require('minify');
return function(file, callback) {
minify(file, options, function(err, data) {
if (err) {
callback(err);
} else {
callback(null, data);
}
});
};
},
};
var assemblerEventLog = {
console : function(logType) {
if (logType === undefined) {
throw new Error('no logType');
}
return function(logLevel, logMessage) {
console.log(logLevel + ': node-ff-assets-' + logType + ' ' + logMessage);
};
},
winston: function(logger, logType) {
if (logger === undefined) {
throw new Error('no logger');
}
if (logType === undefined) {
throw new Error('no logType');
}
return function(logLevel, logMessage) {
logger.log(logLevel, 'node-ff-assets-'+logType+' '+logMessage);
};
},
};
var assemblerEventResult = {
objectFunction : function(object, functionName, resultKey) {
if (object === undefined) {
throw new Error('no object');
}
if (functionName === undefined) {
throw new Error('no functionName');
}
return function(resultValue) {
if (resultKey === undefined) {
object[functionName](resultValue);
} else {
object[functionName](resultKey, resultValue);
}
};
},
objectAdd : function(object, resultKey) {
return assemblerEventResult.objectFunction(object, 'add', resultKey);
},
objectSet : function(object, resultKey) {
return assemblerEventResult.objectFunction(object, 'set', resultKey);
},
objectPut : function(object, resultKey) {
return assemblerEventResult.objectFunction(object, 'put', resultKey);
},
arrayPush : function(object) {
return assemblerEventResult.objectFunction(object, 'push');
},
};
var libAsync = {
pushLinkSources: function (builder, linkPrefix, fileFolder) {
return function(callback) {
fs.readdir(fileFolder, function (err, fileList) {
if (err) {
callback(err);
} else {
fileList.forEach(function (file) {
if (file.indexOf('.js') > 0 || file.indexOf('.css') > 0) {
builder.linkSources.push(linkPrefix+file);
}
});
callback(null,null);
}
});
};
},
runBuilder: function (builder) {
return function(callback) {
builder.run(callback);
};
},
};
var builderAssemblerCreate = {
readFile: function () {
return function (assemblerConfig, callback) {
var AS = require('./asset-assembler');
callback(null,new AS(assemblerConfig,assemblerConstructors.readFile()));
};
},
readFileMinify: function(options) {
return function (assemblerConfig, callback) {
var AS = require('./asset-assembler');
callback(null,new AS(assemblerConfig,assemblerConstructors.readFileMinify(options)));
};
},
};
var builderEmptyCallback = function () {
return function () {};
};
module.exports = {
assembler: {
constructor : assemblerConstructors,
event : {
log : assemblerEventLog,
result : assemblerEventResult,
},
},
builder: {
configCreate: { nop: builderEmptyCallback },
configFill: { nop: builderEmptyCallback },
assemblerCreate: builderAssemblerCreate,
assemblerFill: { nop: builderEmptyCallback },
},
lib: {
async: libAsync,
},
};