all files / src/meta/ templates.js

10.39% Statements 8/77
0% Branches 0/30
0% Functions 0/25
10.39% Lines 8/77
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181                                                                                                                                                                                                                                                                                                                                                         
"use strict";
 
var mkdirp = require('mkdirp'),
	rimraf = require('rimraf'),
	winston = require('winston'),
	async = require('async'),
	path = require('path'),
	fs = require('fs'),
	nconf = require('nconf'),
 
	emitter = require('../emitter'),
	plugins = require('../plugins'),
	utils = require('../../public/src/utils'),
 
	Templates = {},
	searchIndex = {};
 
Templates.compile = function(callback) {
	callback = callback || function() {};
	var fromFile = nconf.get('from-file') || '';
 
	if (nconf.get('isPrimary') === 'false' || fromFile.match('tpl')) {
		if (fromFile.match('tpl')) {
			emitter.emit('templates:compiled');
			winston.info('[minifier] Compiling templates skipped');
		}
 
		return callback();
	}
 
	compile(callback);
};
 
 
function getBaseTemplates(theme) {
	var baseTemplatesPaths = [],
		baseThemePath, baseThemeConfig;
 
	while (theme) {
		baseThemePath = path.join(nconf.get('themes_path'), theme);
		baseThemeConfig = require(path.join(baseThemePath, 'theme.json'));
 
		baseTemplatesPaths.push(path.join(baseThemePath, baseThemeConfig.templates || 'templates'));
		theme = baseThemeConfig.baseTheme;
	}
 
	return baseTemplatesPaths.reverse();
}
 
function preparePaths(baseTemplatesPaths, callback) {
	var coreTemplatesPath = nconf.get('core_templates_path');
	var viewsPath = nconf.get('views_dir');
 
	async.waterfall([
		function (next) {
			rimraf(viewsPath, next);
		},
		function (next) {
			mkdirp(viewsPath, next);
		},
		function(viewsPath, next) {
			plugins.fireHook('static:templates.precompile', {}, next);
		},
		function(next) {
			plugins.getTemplates(next);
		}
	], function(err, pluginTemplates) {
		if (err) {
			return callback(err);
		}
 
		winston.verbose('[meta/templates] Compiling templates');
 
		async.parallel({
			coreTpls: function(next) {
				utils.walk(coreTemplatesPath, next);
			},
			baseThemes: function(next) {
				async.map(baseTemplatesPaths, function(baseTemplatePath, next) {
					utils.walk(baseTemplatePath, function(err, paths) {
						paths = paths.map(function(tpl) {
							return {
								base: baseTemplatePath,
								path: tpl.replace(baseTemplatePath, '')
							};
						});
 
						next(err, paths);
					});
				}, next);
			}
		}, function(err, data) {
			var baseThemes = data.baseThemes,
				coreTpls = data.coreTpls,
				paths = {};
 
			coreTpls.forEach(function(el, i) {
				paths[coreTpls[i].replace(coreTemplatesPath, '')] = coreTpls[i];
			});
 
			baseThemes.forEach(function(baseTpls) {
				baseTpls.forEach(function(el, i) {
					paths[baseTpls[i].path] = path.join(baseTpls[i].base, baseTpls[i].path);
				});
			});
 
			for (var tpl in pluginTemplates) {
				if (pluginTemplates.hasOwnProperty(tpl)) {
					paths[tpl] = pluginTemplates[tpl];
				}
			}
 
			callback(err, paths);
		});
	});
}
 
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');
 
 
	preparePaths(baseTemplatesPaths, function(err, paths) {
		if (err) {
			return callback(err);
		}
 
		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];
 
				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, "");
				}
			}
 
			if (relativePath.match(/^\/admin\/[\s\S]*?/)) {
				addIndex(relativePath, file);
			}
 
			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();
			});
		});
	});
}
 
 
function addIndex(path, file) {
	searchIndex[path] = file;
}
 
function compileIndex(viewsPath, callback) {
	fs.writeFile(path.join(viewsPath, '/indexed.json'), JSON.stringify(searchIndex), callback);
}
 
module.exports = Templates;