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

200 lines
5.1 KiB
JavaScript
Raw Permalink Normal View History

2015-02-27 00:25:57 +00:00
'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);
}
});
};
},
2015-03-14 15:16:31 +00:00
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]];
data = data.replace(regex,regexReplace);
}
callback(null, data);
}
});
};
},
2015-02-27 00:25:57 +00:00
};
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)));
};
},
2015-02-27 00:25:57 +00:00
};
2015-02-27 02:31:44 +00:00
var builderEmptyCallback = function () {
2015-02-27 20:50:19 +00:00
return function (object, callback) {
if (callback !== undefined) {
callback();
}
};
2015-02-27 02:31:44 +00:00
};
2015-02-27 00:25:57 +00:00
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,
});
};
},
};
2015-02-27 00:25:57 +00:00
module.exports = {
assembler: {
constructor : assemblerConstructors,
event : {
log : assemblerEventLog,
result : assemblerEventResult,
},
},
builder: {
configCreate: builderConfigCreate,
2015-02-27 00:25:57 +00:00
configFill: { nop: builderEmptyCallback },
assemblerCreate: builderAssemblerCreate,
assemblerFill: { nop: builderEmptyCallback },
},
lib: {
async: libAsync,
},
};