mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-10 07:55:46 +01:00
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:
27
src/utils.js
27
src/utils.js
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user