forked from carolineBda/compoxure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugMode.js
More file actions
39 lines (33 loc) · 1.25 KB
/
Copy pathDebugMode.js
File metadata and controls
39 lines (33 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* Simple in memory stats object per request for displaying stats in developer mode
*/
var _ = require('lodash');
var fs = require('fs');
var Hogan = require('hogan.js');
var crypto = require('crypto');
function DebugMode() {
var self = this;
self.data = {}
}
module.exports = DebugMode;
DebugMode.prototype.add = function(fragmentUrl, data) {
var self = this;
var debugData = _.clone(data);
var hash = crypto.createHash('md5').update(fragmentUrl).digest('hex');
debugData.hash = hash;
self.data[hash] = _.merge(debugData, self.data[hash] || {});
};
DebugMode.prototype.render = function() {
var self = this;
// sync read JS - ok as this is only used in dev or debug
var js = fs.readFileSync(__dirname + '/client/cx-stats.js');
var jsLib = fs.readFileSync(__dirname + '/client/cx-libs.js');
var css = fs.readFileSync(__dirname + '/client/cx-stats.css');
var htmlTemplate = Hogan.compile(fs.readFileSync(__dirname + '/client/cx-template.html').toString());
// Run each fragment through the template;
var output = ['<style>',css,'</style>','<script>','var cxStats = ' + JSON.stringify(self.data, null, 4),jsLib,js,'</script>'].join('\n');
for(var key in self.data) {
output += htmlTemplate.render(self.data[key]) + '\n';
}
return output;
};