meta/templates.js refactor

v1.18.x
psychobunny 9 years ago
parent da5494fdf1
commit 914fc1543e

@ -47,17 +47,15 @@ function getBaseTemplates(theme) {
return baseTemplatesPaths.reverse(); return baseTemplatesPaths.reverse();
} }
function compile(callback) { function preparePaths(baseTemplatesPaths, callback) {
var coreTemplatesPath = nconf.get('core_templates_path'), var coreTemplatesPath = nconf.get('core_templates_path'),
themeConfig = require(nconf.get('theme_config')),
baseTemplatesPaths = themeConfig.baseTheme ? getBaseTemplates(themeConfig.baseTheme) : [nconf.get('base_templates_path')],
viewsPath = nconf.get('views_dir'); viewsPath = nconf.get('views_dir');
plugins.getTemplates(function(err, pluginTemplates) { plugins.getTemplates(function(err, pluginTemplates) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
winston.verbose('[meta/templates] Compiling templates'); winston.verbose('[meta/templates] Compiling templates');
rimraf.sync(viewsPath); rimraf.sync(viewsPath);
mkdirp.sync(viewsPath); mkdirp.sync(viewsPath);
@ -86,7 +84,7 @@ function compile(callback) {
paths = {}; paths = {};
coreTpls.forEach(function(el, i) { coreTpls.forEach(function(el, i) {
paths[coreTpls[i].replace(coreTemplatesPath, '')] = path.join(coreTemplatesPath, coreTpls[i]); paths[coreTpls[i].replace(coreTemplatesPath, '')] = coreTpls[i];
}); });
baseThemes.forEach(function(baseTpls) { baseThemes.forEach(function(baseTpls) {
@ -101,45 +99,60 @@ function compile(callback) {
} }
} }
async.each(Object.keys(paths), function(relativePath, next) { callback(err, paths);
var file = fs.readFileSync(paths[relativePath]).toString(), });
matches = null, });
regex = /[ \t]*<!-- IMPORT ([\s\S]*?)? -->[ \t]*/; }
while((matches = file.match(regex)) !== null) { function compile(callback) {
var partial = "/" + matches[1]; var themeConfig = require(nconf.get('theme_config')),
baseTemplatesPaths = themeConfig.baseTheme ? getBaseTemplates(themeConfig.baseTheme) : [nconf.get('base_templates_path')],
viewsPath = nconf.get('views_dir');
if (paths[partial] && relativePath !== partial) { preparePaths(baseTemplatesPaths, function(err, paths) {
file = file.replace(regex, fs.readFileSync(paths[partial]).toString()); if (err) {
} else { return callback(err);
winston.warn('[meta/templates] Partial not loaded: ' + matches[1]); }
file = file.replace(regex, "");
}
}
if (relativePath.match(/^\/admin\/[\s\S]*?/)) { async.each(Object.keys(paths), function(relativePath, next) {
addIndex(relativePath, file); var file = fs.readFileSync(paths[relativePath]).toString(),
} matches = null,
regex = /[ \t]*<!-- IMPORT ([\s\S]*?)? -->[ \t]*/;
while((matches = file.match(regex)) !== null) {
var partial = "/" + matches[1];
mkdirp.sync(path.join(viewsPath, relativePath.split('/').slice(0, -1).join('/'))); if (paths[partial] && relativePath !== partial) {
fs.writeFile(path.join(viewsPath, relativePath), file, next); file = file.replace(regex, fs.readFileSync(paths[partial]).toString());
}, function(err) { } else {
if (err) { winston.warn('[meta/templates] Partial not loaded: ' + matches[1]);
winston.error('[meta/templates] ' + err.stack); file = file.replace(regex, "");
return callback(err);
} }
}
compileIndex(viewsPath, function() { if (relativePath.match(/^\/admin\/[\s\S]*?/)) {
winston.verbose('[meta/templates] Successfully compiled templates.'); addIndex(relativePath, file);
}
emitter.emit('templates:compiled'); mkdirp.sync(path.join(viewsPath, relativePath.split('/').slice(0, -1).join('/')));
if (process.send) { fs.writeFile(path.join(viewsPath, relativePath), file, next);
process.send({ }, function(err) {
action: 'templates:compiled' if (err) {
}); winston.error('[meta/templates] ' + err.stack);
} return callback(err);
callback(); }
});
compileIndex(viewsPath, function() {
winston.verbose('[meta/templates] Successfully compiled templates.');
emitter.emit('templates:compiled');
if (process.send) {
process.send({
action: 'templates:compiled'
});
}
callback();
}); });
}); });
}); });

Loading…
Cancel
Save