2
0
Fork 0
tcrud/lib/plugin/server/config/config-resources-mobile.js

64 lines
1.8 KiB
JavaScript
Raw Normal View History

2017-06-10 14:53:29 +00:00
var debug = require('debug')('ff:tcrud:plugin:server:config:resouces:mobile');
var configRegistry = require('./../../../config-registry');
var configUtil = require('./../../../config-util');
var fetch = require('fetch');
module.exports = (function () {
var webAssets = [];
var fetchHashResource = function(fetchEntry,cb) {
fetch.fetchUrl(fetchEntry.url,function(err, meta, data) {
debug('fetched url: '+fetchEntry.url+' length: '+meta.responseHeaders['content-length']);
var assetHash = configUtil.stringHash(''+data);
webAssets.push({
url: fetchEntry.url,
type: fetchEntry.type,
hash: assetHash
});
cb();
});
};
var fetchHashResources = function(fetchList, cb) {
var startTime = new Date().getTime();
debug('createHashResources: '+fetchList.length);
var resourceStack = fetchList;
var resourceLoader = function() {
resourceStack = resourceStack.slice(1);
if (resourceStack.length === 0) {
debug('fetchHashResources done in '+(new Date().getTime()-startTime)+' ms.');
cb();
} else {
fetchHashResource(resourceStack[0],resourceLoader);
}
};
fetchHashResource(resourceStack[0],resourceLoader);
};
return function ServerConfigResourcesMobilePlugin() {
this.configPlugin = function (ctx) {
ctx.key = 'serverConfigResourcesMobile';
ctx.description = 'Exports client resources mobile.';
ctx.localDir = __dirname;
ctx.localConfigTemplate = 'config-resources-mobile.json';
};
this.configServer = function(ctx) {
ctx.server.get(ctx.createSlugApiServerBase(),ctx.renderFunctionJSON(function () {
return {
resources: webAssets
}
}));
};
this.configPostBoot = function(ctx) {
fetchHashResources(configRegistry.createClientResourceFetchList(), function() {
debug('Total assets build: '+webAssets.length);
});
}
};
})();