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();
}
function compile(callback) {
function preparePaths(baseTemplatesPaths, callback) {
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');
plugins.getTemplates(function(err, pluginTemplates) {
if (err) {
return callback(err);
}
winston.verbose('[meta/templates] Compiling templates');
rimraf.sync(viewsPath);
mkdirp.sync(viewsPath);
@ -86,7 +84,7 @@ function compile(callback) {
paths = {};
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) {
@ -101,45 +99,60 @@ function compile(callback) {
}
}
async.each(Object.keys(paths), function(relativePath, next) {
var file = fs.readFileSync(paths[relativePath]).toString(),
matches = null,
regex = /[ \t]*<!-- IMPORT ([\s\S]*?)? -->[ \t]*/;
callback(err, paths);
});
});
}
while((matches = file.match(regex)) !== null) {
var partial = "/" + matches[1];
function compile(callback) {
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) {
file = file.replace(regex, fs.readFileSync(paths[partial]).toString());
} else {
winston.warn('[meta/templates] Partial not loaded: ' + matches[1]);
file = file.replace(regex, "");
}
}
preparePaths(baseTemplatesPaths, function(err, paths) {
if (err) {
return callback(err);
}
if (relativePath.match(/^\/admin\/[\s\S]*?/)) {
addIndex(relativePath, file);
}
async.each(Object.keys(paths), function(relativePath, next) {
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('/')));
fs.writeFile(path.join(viewsPath, relativePath), file, next);
}, function(err) {
if (err) {
winston.error('[meta/templates] ' + err.stack);
return callback(err);
if (paths[partial] && relativePath !== partial) {
file = file.replace(regex, fs.readFileSync(paths[partial]).toString());
} else {
winston.warn('[meta/templates] Partial not loaded: ' + matches[1]);
file = file.replace(regex, "");
}
}
compileIndex(viewsPath, function() {
winston.verbose('[meta/templates] Successfully compiled templates.');
if (relativePath.match(/^\/admin\/[\s\S]*?/)) {
addIndex(relativePath, file);
}
emitter.emit('templates:compiled');
if (process.send) {
process.send({
action: 'templates:compiled'
});
}
callback();
});
mkdirp.sync(path.join(viewsPath, relativePath.split('/').slice(0, -1).join('/')));
fs.writeFile(path.join(viewsPath, relativePath), file, next);
}, function(err) {
if (err) {
winston.error('[meta/templates] ' + err.stack);
return callback(err);
}
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