feat: #7743 meta/templates.js

v1.18.x
Julian Lam 6 years ago
parent 6a486e35bb
commit 79eed9ae60

@ -1,55 +1,54 @@
'use strict'; 'use strict';
var mkdirp = require('mkdirp'); const mkdirp = require('mkdirp');
var rimraf = require('rimraf'); const rimraf = require('rimraf');
var winston = require('winston'); const winston = require('winston');
var async = require('async'); const path = require('path');
var path = require('path');
var fs = require('fs');
var nconf = require('nconf');
var _ = require('lodash');
var Benchpress = require('benchpressjs');
var plugins = require('../plugins'); const util = require('util');
var file = require('../file'); const fs = require('fs');
var db = require('../database'); const fsReadFile = util.promisify(fs.readFile);
const fsWriteFile = util.promisify(fs.writeFile);
var viewsPath = nconf.get('views_dir'); const nconf = require('nconf');
const _ = require('lodash');
const Benchpress = require('benchpressjs');
var Templates = module.exports; const plugins = require('../plugins');
const file = require('../file');
const db = require('../database');
function processImports(paths, templatePath, source, callback) { const viewsPath = nconf.get('views_dir');
const Templates = module.exports;
async function processImports(paths, templatePath, source) {
var regex = /<!-- IMPORT (.+?) -->/; var regex = /<!-- IMPORT (.+?) -->/;
var matches = source.match(regex); var matches = source.match(regex);
if (!matches) { if (!matches) {
return callback(null, source); return source;
} }
var partial = matches[1]; var partial = matches[1];
if (paths[partial] && templatePath !== partial) { if (paths[partial] && templatePath !== partial) {
fs.readFile(paths[partial], 'utf8', function (err, partialSource) { const partialSource = await fsReadFile(paths[partial], 'utf8');
if (err) { source = source.replace(regex, partialSource);
return callback(err); return await processImports(paths, templatePath, source);
} }
source = source.replace(regex, partialSource);
processImports(paths, templatePath, source, callback);
});
} else {
winston.warn('[meta/templates] Partial not loaded: ' + matches[1]); winston.warn('[meta/templates] Partial not loaded: ' + matches[1]);
source = source.replace(regex, ''); source = source.replace(regex, '');
processImports(paths, templatePath, source, callback); return await processImports(paths, templatePath, source);
}
} }
Templates.processImports = processImports; Templates.processImports = processImports;
var themeNamePattern = /^(@.*?\/)?nodebb-theme-.*$/; const themeNamePattern = /^(@.*?\/)?nodebb-theme-.*$/;
function getTemplateDirs(activePlugins, callback) { async function getTemplateDirs(activePlugins) {
var pluginTemplates = activePlugins.map(function (id) { const pluginTemplates = activePlugins.map(function (id) {
if (themeNamePattern.test(id)) { if (themeNamePattern.test(id)) {
return nconf.get('theme_templates_path'); return nconf.get('theme_templates_path');
} }
@ -59,11 +58,11 @@ function getTemplateDirs(activePlugins, callback) {
return path.join(__dirname, '../../node_modules/', id, plugins.pluginsData[id].templates || 'templates'); return path.join(__dirname, '../../node_modules/', id, plugins.pluginsData[id].templates || 'templates');
}).filter(Boolean); }).filter(Boolean);
var themeConfig = require(nconf.get('theme_config')); let themeConfig = require(nconf.get('theme_config'));
var theme = themeConfig.baseTheme; let theme = themeConfig.baseTheme;
var themePath; let themePath;
var themeTemplates = []; let themeTemplates = [];
while (theme) { while (theme) {
themePath = path.join(nconf.get('themes_path'), theme); themePath = path.join(nconf.get('themes_path'), theme);
themeConfig = require(path.join(themePath, 'theme.json')); themeConfig = require(path.join(themePath, 'theme.json'));
@ -79,16 +78,13 @@ function getTemplateDirs(activePlugins, callback) {
var templateDirs = _.uniq([coreTemplatesPath].concat(themeTemplates, pluginTemplates)); var templateDirs = _.uniq([coreTemplatesPath].concat(themeTemplates, pluginTemplates));
async.filter(templateDirs, file.exists, callback); templateDirs = await Promise.all(templateDirs.map(async path => (await file.exists(path) ? path : false)));
return templateDirs.filter(Boolean);
} }
function getTemplateFiles(dirs, callback) { async function getTemplateFiles(dirs) {
async.waterfall([ const buckets = await Promise.all(dirs.map(async (dir) => {
function (cb) { let files = await file.walk(dir);
async.map(dirs, function (dir, next) {
file.walk(dir, function (err, files) {
if (err) { return next(err); }
files = files.filter(function (path) { files = files.filter(function (path) {
return path.endsWith('.tpl'); return path.endsWith('.tpl');
}).map(function (file) { }).map(function (file) {
@ -97,11 +93,9 @@ function getTemplateFiles(dirs, callback) {
path: file, path: file,
}; };
}); });
next(null, files); return files;
}); }));
}, cb);
},
function (buckets, cb) {
var dict = {}; var dict = {};
buckets.forEach(function (files) { buckets.forEach(function (files) {
files.forEach(function (file) { files.forEach(function (file) {
@ -109,94 +103,47 @@ function getTemplateFiles(dirs, callback) {
}); });
}); });
cb(null, dict); return dict;
},
], callback);
} }
function compileTemplate(filename, source, callback) { async function compileTemplate(filename, source) {
async.waterfall([ let paths = await file.walk(viewsPath);
function (next) {
file.walk(viewsPath, next);
},
function (paths, next) {
paths = _.fromPairs(paths.map(function (p) { paths = _.fromPairs(paths.map(function (p) {
var relative = path.relative(viewsPath, p).replace(/\\/g, '/'); var relative = path.relative(viewsPath, p).replace(/\\/g, '/');
return [relative, p]; return [relative, p];
})); }));
async.waterfall([
function (next) { source = await processImports(paths, filename, source);
processImports(paths, filename, source, next); const compiled = await Benchpress.precompile(source, {
},
function (source, next) {
Benchpress.precompile(source, {
minify: global.env !== 'development', minify: global.env !== 'development',
}, next); });
}, return await fsWriteFile(path.join(viewsPath, filename.replace(/\.tpl$/, '.js')), compiled);
function (compiled, next) {
fs.writeFile(path.join(viewsPath, filename.replace(/\.tpl$/, '.js')), compiled, next);
},
], next);
},
], callback);
} }
Templates.compileTemplate = compileTemplate; Templates.compileTemplate = compileTemplate;
function compile(callback) { async function compile() {
callback = callback || function () {}; const _rimraf = util.promisify(rimraf);
const _mkdirp = util.promisify(mkdirp);
async.waterfall([
function (next) { await _rimraf(viewsPath);
rimraf(viewsPath, function (err) { next(err); }); await _mkdirp(viewsPath);
},
function (next) { let files = await db.getSortedSetRange('plugins:active', 0, -1);
mkdirp(viewsPath, function (err) { next(err); }); files = await getTemplateDirs(files);
}, files = await getTemplateFiles(files);
function (next) {
db.getSortedSetRange('plugins:active', 0, -1, next); await Promise.all(Object.keys(files).map(async (name) => {
}, const filePath = files[name];
getTemplateDirs, let imported = await fsReadFile(filePath, 'utf8');
getTemplateFiles, imported = await processImports(files, name, imported);
function (files, next) {
async.each(Object.keys(files), function (name, next) { await _mkdirp(path.join(viewsPath, path.dirname(name)));
var filePath = files[name];
await fsWriteFile(path.join(viewsPath, name), imported);
async.waterfall([ const compiled = await Benchpress.precompile(imported, { minify: global.env !== 'development' });
function (next) { await fsWriteFile(path.join(viewsPath, name.replace(/\.tpl$/, '.js')), compiled);
fs.readFile(filePath, 'utf8', next); }));
},
function (source, next) {
processImports(files, name, source, next);
},
function (source, next) {
mkdirp(path.join(viewsPath, path.dirname(name)), function (err) {
next(err, source);
});
},
function (imported, next) {
async.parallel([
function (cb) {
fs.writeFile(path.join(viewsPath, name), imported, cb);
},
function (cb) {
Benchpress.precompile(imported, { minify: global.env !== 'development' }, function (err, compiled) {
if (err) {
cb(err);
return;
}
fs.writeFile(path.join(viewsPath, name.replace(/\.tpl$/, '.js')), compiled, cb);
});
},
], next);
},
], next);
}, next);
},
function (next) {
winston.verbose('[meta/templates] Successfully compiled templates.'); winston.verbose('[meta/templates] Successfully compiled templates.');
next();
},
], callback);
} }
Templates.compile = compile; Templates.compile = compile;

@ -103,14 +103,14 @@ describe('Controllers', function () {
var name = 'custom.tpl'; var name = 'custom.tpl';
var tplPath = path.join(nconf.get('views_dir'), name); var tplPath = path.join(nconf.get('views_dir'), name);
before(function (done) { before(async () => {
plugins.registerHook('myTestPlugin', { plugins.registerHook('myTestPlugin', {
hook: 'action:homepage.get:custom', hook: 'action:homepage.get:custom',
method: hookMethod, method: hookMethod,
}); });
fs.writeFileSync(tplPath, message); fs.writeFileSync(tplPath, message);
meta.templates.compileTemplate(name, message, done); await meta.templates.compileTemplate(name, message);
}); });
it('should load default', function (done) { it('should load default', function (done) {

Loading…
Cancel
Save