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

201 lines
5.2 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);
}
});
};
},
readFileRegex : function(regexData) {
if (regexData === undefined) {
// Simple regex which cleans most stuff from js and css.
regexData = {
'(\\s\\\|\\|\\s)': '||', // clean bit-or space
'(\\s\\\&\\&\\s)': '&&', // clean bit-and space
'(if\\s\\(\\s)': 'if(', // clean if space
'(for\\s\\(\\s)': 'for(', // clean for space
'(\\s*{\\s*)': '{', // clean bracket open
'(^\\s*}\\s*)': '}\n', // clean bracket close
'(^\\s+)': '', // clean up white space
'(\\{[\\s\\/]+\\/.*)': '{', // rm comment on functions
'(^|\\s\\/\\/.*)': '', // rm comment lines
'(\\/\\*[\\s\\*\\!][\\s\\S]*?\\*\\/)': '', // rm comment blocks
};
}
return function(file, callback) {
fs.readFile(file, function(err, data) {
if (err) {
callback(err);
} else {
data = ''+data;
var regexList = Object.keys(regexData);
for (var i = 0; i < regexList.length; i++) {
var regex = new RegExp(regexList[i],'gm');
var regexReplace = regexData[regexList[i]];
console.log('regex: \''+regex+'\' replace: \''+regexReplace+'\'');
data = data.replace(regex,regexReplace);
}
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);
}
});
};
},
};
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)));
};
},
readFileRegex: function(options) {
return function (assemblerConfig, callback) {
var AS = require('./asset-assembler');
callback(null,new AS(assemblerConfig,assemblerConstructors.readFileRegex(options)));
};
},
};
var builderEmptyCallback = function () {
return function (object, callback) {
if (callback !== undefined) {
callback();
}
};
};
var builderConfigCreate = {
nop: builderEmptyCallback,
fromJSON: function (configJSON,type) {
return function(callback) {
callback(null,{
// TODO: add merging
linkMapping: configJSON.linkMapping,
linkTarget: configJSON[type].linkTarget,
linkSources: configJSON[type].linkSources,
assetType: type,
});
};
},
};
module.exports = {
assembler: {
constructor : assemblerConstructors,
event : {
log : assemblerEventLog,
result : assemblerEventResult,
},
},
builder: {
configCreate: builderConfigCreate,
configFill: { nop: builderEmptyCallback },
assemblerCreate: builderAssemblerCreate,
assemblerFill: { nop: builderEmptyCallback },
},
lib: {
async: libAsync,
},
};