2
0
Fork 0

removed some code smells and made nodejs logger expleciet.

This commit is contained in:
Willem 2016-11-20 17:13:01 +01:00
parent d4f681e28c
commit 58142e9300
6 changed files with 118 additions and 68 deletions

View file

@ -31,8 +31,6 @@
* @module FFSpaLoader
*/
(function (root, factory) {
'use strict';
if ( typeof define === 'function' && define.amd ) {
define(factory(root));
} else if ( typeof exports === 'object' ) {
@ -40,9 +38,7 @@
} else {
root.FFSpaLoader = factory(root);
}
})(this || window, /** @lends module:FFSpaLoader */ function (rootWindow) {
'use strict';
/**
* The options to customize the loader.
@ -221,11 +217,21 @@
};
},
websql: function(opt) {
if (opt === undefined) { opt = {}; }
if (opt.name === undefined) { opt.name = 'FFSpaLoader'; }
if (opt.size === undefined) { opt.size = 4 * 1024 * 1024; } // reg 4MB let user do higher
if (opt.version === undefined) { opt.version = '1.0'; }
if (opt.table === undefined) { opt.table = 'cache_store'; }
if (opt === undefined) {
opt = {};
}
if (opt.name === undefined) {
opt.name = 'FFSpaLoader';
}
if (opt.size === undefined) {
opt.size = 4 * 1024 * 1024; // reg 4MB let user do higher
}
if (opt.version === undefined) {
opt.version = '1.0';
}
if (opt.table === undefined) {
opt.table = 'cache_store';
}
if (opt.open === undefined) {
opt.open = function(dbOpt) {
return rootWindow.openDatabase(dbOpt.name, dbOpt.version, dbOpt.name, dbOpt.size);
@ -335,7 +341,9 @@
try {
var stack = err.stack || '';
stack = stack.split('\n').map(function (line) { return line.trim()+'\n'; });
stack = stack.split('\n').map(function (line) {
return line.trim()+'\n';
});
var stackText = stack.splice(stack[0] === 'Error' ? 2 : 1);
err.stackText = stackText;
@ -1019,7 +1027,11 @@
if (options.cache[type] === null) {
if (factory.detect.cordovaDevice() && factory.detect.sqlitePlugin()) {
utilDebug('startCacheType auto sqlitePlugin for '+type);
options.cache[type] = factory.cache.websql({open: function(dbOpt) { return sqlitePlugin.openDatabase(dbOpt.name, dbOpt.version, dbOpt.name, dbOpt.size);}});
options.cache[type] = factory.cache.websql({
open: function(dbOpt) {
return sqlitePlugin.openDatabase(dbOpt.name, dbOpt.version, dbOpt.name, dbOpt.size);
}
});
} else if (factory.detect.openDatabase()) {
utilDebug('startCacheType auto openDatabase for '+type);
options.cache[type] = factory.cache.websql();
@ -1199,9 +1211,13 @@
}
}
bootCordova(function(err) {
if (err !== null) { return cb(err); }
if (err !== null) {
return cb(err);
}
startCache(function(err) {
if (err !== null) { return cb(err); }
if (err !== null) {
return cb(err);
}
if (options.server.url !== null) {
startLoader(cb);
return;
@ -1235,11 +1251,16 @@
}
if (cacheHasService('meta')) {
cacheDeleteValue('meta','server_url',function(err) {
if (err !== null) { return cb(err); }
if (err !== null) {
return cb(err);
}
cacheDeleteValue('meta','server_resources',function(err) {
if (err !== null) { return cb(err); }
setTimeout(function() {cb(null);}); // return next tick so (websql) transaction is flushed before location.reload
if (err !== null) {
return cb(err);
}
setTimeout(function() {
cb(null); // return next tick so (websql) transaction is flushed before location.reload
});
});
});
} else {
@ -1257,7 +1278,9 @@
cb2 = function() {};
}
var cb = function(err) {
setTimeout(function() {cb2(err);}); // next tick to flush transactions.
setTimeout(function() {
cb2(err); // next tick to flush transactions.
});
};
if (cacheHasService('meta')) {
cacheGetValue('meta','server_resources',function(err, value) {
@ -1282,7 +1305,9 @@
};
// Auto fill handlers and return public object.
options.boot.debug.handler = function(msg) {console.log(msg);};
options.boot.debug.handler = function(msg) {
console.log(msg);
};
return {
options: options,
factory: factory,

View file

@ -1,16 +1,21 @@
var fs = require('fs');
var logger = require('console');
module.exports = function(context) {
var Q = context.requireCordovaModule('q');
var deferral = new Q.defer();
console.log('copy-ff-spa-loader-css start');
fs.readFile('../../es5-ff-spa-loader.css',function(err,data) {
if (err) return deferral.reject(err);
fs.writeFile('www/es5-ff-spa-loader.css',data,function(err) {
if (err) return deferral.reject(err);
console.log('copy-ff-spa-loader-css done');
deferral.resolve();
})
});
return deferral.promise;
var Q = context.requireCordovaModule('q');
var deferral = new Q.defer();
logger.log('copy-ff-spa-loader-css start');
fs.readFile('../../es5-ff-spa-loader.css',function(err,data) {
if (err) {
return deferral.reject(err);
}
fs.writeFile('www/es5-ff-spa-loader.css',data,function(err) {
if (err) {
return deferral.reject(err);
}
logger.log('copy-ff-spa-loader-css done');
deferral.resolve();
})
});
return deferral.promise;
}

View file

@ -1,16 +1,21 @@
var fs = require('fs');
var logger = require('console');
module.exports = function(context) {
var Q = context.requireCordovaModule('q');
var deferral = new Q.defer();
console.log('copy-ff-spa-loader-js start');
fs.readFile('../../es5-ff-spa-loader.js',function(err,data) {
if (err) return deferral.reject(err);
fs.writeFile('www/es5-ff-spa-loader.js',data,function(err) {
if (err) return deferral.reject(err);
console.log('copy-ff-spa-loader-js done');
deferral.resolve();
})
});
return deferral.promise;
var Q = context.requireCordovaModule('q');
var deferral = new Q.defer();
logger.log('copy-ff-spa-loader-js start');
fs.readFile('../../es5-ff-spa-loader.js',function(err,data) {
if (err) {
return deferral.reject(err);
}
fs.writeFile('www/es5-ff-spa-loader.js',data,function(err) {
if (err) {
return deferral.reject(err);
}
logger.log('copy-ff-spa-loader-js done');
deferral.resolve();
})
});
return deferral.promise;
}

View file

@ -1,15 +1,18 @@
var fs = require('fs');
var logger = require('console');
module.exports = function(context) {
var Q = context.requireCordovaModule('q');
var deferral = new Q.defer();
var nameOld = 'platforms/android/build/outputs/apk/android-debug.apk';
var nameNew = 'platforms/android/build/outputs/apk/FFSpaLoaderExample.apk';
console.log('rename-android-apk start old: '+nameOld);
fs.rename(nameOld,nameNew,function(err) {
if (err) return deferral.reject(err);
console.log('rename-android-apk done new: '+nameNew);
deferral.resolve();
});
return deferral.promise;
var Q = context.requireCordovaModule('q');
var deferral = new Q.defer();
var nameOld = 'platforms/android/build/outputs/apk/android-debug.apk';
var nameNew = 'platforms/android/build/outputs/apk/FFSpaLoaderExample.apk';
logger.log('rename-android-apk start old: %s',nameOld);
fs.rename(nameOld,nameNew,function(err) {
if (err) {
return deferral.reject(err);
}
logger.log('rename-android-apk done new: %s',nameNew);
deferral.resolve();
});
return deferral.promise;
}

View file

@ -1,15 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src * 'self' 'unsafe-inline' 'unsafe-eval' data: gap:">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="es5-ff-spa-loader.css" />
</head>
<body>
<script type="text/javascript" src="cordova.js"></script>
<head>
<title>Loading</title>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src * 'self' 'unsafe-inline' 'unsafe-eval' data: gap:">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="es5-ff-spa-loader.css" />
</head>
<body>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="es5-ff-spa-loader.js"></script>
<script>
FFSpaLoader.options.boot.debug.enable = true;
@ -18,5 +19,5 @@
FFSpaLoader.options.server.header.response['X-My-Api'] = 'noknok';
FFSpaLoader.start();
</script>
</body>
</body>
</html>

View file

@ -10,6 +10,7 @@ var morgan = require('morgan');
var UglifyJS = require("uglify-js");
var Hashes = require('jshashes');
var minify = require('minify');
var logger = require('console');
var appPath = '/test'; // dynamic context path for version or proxy/etc of server app.
var clientResourcesWeb = [];
@ -56,14 +57,23 @@ var fetchHashResources = function(fetchList, cb) {
var createClientResourceFetchList = function() {
var fetchList = [];
for (var clientResourceIdxJs in clientResources.js) {
if (!clientResources.js.hasOwnProperty(clientResourceIdxJs)) {
continue;
}
var urlJs = clientResources.js[clientResourceIdxJs];
fetchList.push({url:urlJs,type:'js'});
}
for (var clientResourceIdxCss in clientResources.css) {
if (!clientResources.css.hasOwnProperty(clientResourceIdxCss)) {
continue;
}
var urlCss = clientResources.css[clientResourceIdxCss];
fetchList.push({url:urlCss,type:'css'});
}
for (var clientResourceIdxCssData in clientResources.dss) {
if (!clientResources.dss.hasOwnProperty(clientResourceIdxCssData)) {
continue;
}
var urlCssData = clientResources.cssData[clientResourceIdxCssData];
fetchList.push({url:urlCssData,type:'dss'});
}
@ -115,14 +125,15 @@ server.get(appPath+'/example-ui', renderIndex());
server.get('/', function (req, res) {res.redirect(appPath);});
server.listen(httpPort);
console.info('Server started on port '+httpPort);
logger.info('Server started on port '+httpPort);
fetchHashResources(createClientResourceFetchList(), function(err) {
if (err !== null) {
console.log('Fatal error '+err);
logger.log('Fatal error '+err);
process.exit(1);
} else {
console.log('Total assets build: '+clientResourcesWeb.length);
logger.log('Total assets build: '+clientResourcesWeb.length);
logger.log('Server boot done.');
}
});