templates refactor part 2

updated so that tpls get loaded on demand as opposed to all at once on
load; added a function to pull the tpl list on load instead of having to
manually define new templates in code; some clean up; added utils.walk;
This commit is contained in:
psychobunny
2013-05-17 15:20:08 -04:00
parent ba8e0ea420
commit fb52dfc6c8
6 changed files with 123 additions and 73 deletions

View File

@@ -1,3 +1,5 @@
var fs = require('fs');
var utils = {
generateUUID: function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
@@ -5,6 +7,31 @@ var utils = {
return v.toString(16);
});
},
//Adapted from http://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search
walk: function(dir, done) {
var main_dir = global.configuration.ROOT_DIRECTORY + '/public/templates/';
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
file = dir + '/' + file;
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
utils.walk(file, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
results.push(file.replace(main_dir, '').replace('.tpl', ''));
if (!--pending) done(null, results);
}
});
});
});
},
relativeTime: function(timestamp) {
var now = +new Date(),