Files
NodeBB/src/meta/templates.js

146 lines
4.3 KiB
JavaScript
Raw Normal View History

2017-02-18 01:56:23 -07:00
'use strict';
2014-09-24 14:49:16 -04:00
const util = require('util');
let mkdirp = require('mkdirp');
mkdirp = mkdirp.hasOwnProperty('native') ? mkdirp : util.promisify(mkdirp);
2019-07-23 14:26:02 -04:00
const rimraf = require('rimraf');
const winston = require('winston');
const path = require('path');
const fs = require('fs');
2019-07-23 14:26:02 -04:00
const nconf = require('nconf');
const _ = require('lodash');
const Benchpress = require('benchpressjs');
2019-07-23 14:26:02 -04:00
const plugins = require('../plugins');
const file = require('../file');
const db = require('../database');
const { themeNamePattern, paths } = require('../constants');
2019-07-23 14:26:02 -04:00
const viewsPath = nconf.get('views_dir');
const Templates = module.exports;
async function processImports(paths, templatePath, source) {
var regex = /<!-- IMPORT (.+?) -->/;
2017-05-24 16:13:23 -04:00
var matches = source.match(regex);
2017-05-24 16:13:23 -04:00
if (!matches) {
2019-07-23 14:26:02 -04:00
return source;
}
2017-05-24 16:13:23 -04:00
var partial = matches[1];
if (paths[partial] && templatePath !== partial) {
2020-08-14 00:05:03 -04:00
const partialSource = await fs.promises.readFile(paths[partial], 'utf8');
2019-07-23 14:26:02 -04:00
source = source.replace(regex, partialSource);
return await processImports(paths, templatePath, source);
}
2017-05-24 16:13:23 -04:00
2019-07-23 14:26:02 -04:00
winston.warn('[meta/templates] Partial not loaded: ' + matches[1]);
source = source.replace(regex, '');
2017-05-24 16:13:23 -04:00
2019-07-23 14:26:02 -04:00
return await processImports(paths, templatePath, source);
}
Templates.processImports = processImports;
2017-05-24 16:13:23 -04:00
2019-07-23 14:26:02 -04:00
async function getTemplateDirs(activePlugins) {
const pluginTemplates = activePlugins.map(function (id) {
2018-06-09 16:26:28 -06:00
if (themeNamePattern.test(id)) {
return nconf.get('theme_templates_path');
}
2018-04-24 10:26:19 -04:00
if (!plugins.pluginsData[id]) {
return '';
}
return path.join(paths.nodeModules, id, plugins.pluginsData[id].templates || 'templates');
2018-04-24 10:26:19 -04:00
}).filter(Boolean);
2017-05-24 16:13:23 -04:00
2019-07-23 14:26:02 -04:00
let themeConfig = require(nconf.get('theme_config'));
let theme = themeConfig.baseTheme;
2019-07-23 14:26:02 -04:00
let themePath;
let themeTemplates = [];
while (theme) {
themePath = path.join(nconf.get('themes_path'), theme);
themeConfig = require(path.join(themePath, 'theme.json'));
themeTemplates.push(path.join(themePath, themeConfig.templates || 'templates'));
theme = themeConfig.baseTheme;
}
themeTemplates.push(nconf.get('base_templates_path'));
themeTemplates = _.uniq(themeTemplates.reverse());
var coreTemplatesPath = nconf.get('core_templates_path');
var templateDirs = _.uniq([coreTemplatesPath].concat(themeTemplates, pluginTemplates));
2019-07-23 14:26:02 -04:00
templateDirs = await Promise.all(templateDirs.map(async path => (await file.exists(path) ? path : false)));
return templateDirs.filter(Boolean);
}
2019-07-23 14:26:02 -04:00
async function getTemplateFiles(dirs) {
const buckets = await Promise.all(dirs.map(async (dir) => {
2019-07-23 14:26:02 -04:00
let files = await file.walk(dir);
files = files.filter(function (path) {
return path.endsWith('.tpl');
}).map(function (file) {
return {
name: path.relative(dir, file).replace(/\\/g, '/'),
path: file,
};
});
return files;
}));
var dict = {};
buckets.forEach(function (files) {
files.forEach(function (file) {
dict[file.name] = file.path;
});
});
return dict;
}
2019-07-23 14:26:02 -04:00
async function compileTemplate(filename, source) {
let paths = await file.walk(viewsPath);
paths = _.fromPairs(paths.map(function (p) {
var relative = path.relative(viewsPath, p).replace(/\\/g, '/');
return [relative, p];
}));
source = await processImports(paths, filename, source);
const compiled = await Benchpress.precompile(source, {
minify: process.env.NODE_ENV !== 'development',
2019-07-23 14:26:02 -04:00
});
2020-08-14 00:05:03 -04:00
return await fs.promises.writeFile(path.join(viewsPath, filename.replace(/\.tpl$/, '.js')), compiled);
}
Templates.compileTemplate = compileTemplate;
2019-07-23 14:26:02 -04:00
async function compile() {
const _rimraf = util.promisify(rimraf);
await _rimraf(viewsPath);
2020-01-31 14:10:00 -05:00
await mkdirp(viewsPath);
2019-07-23 14:26:02 -04:00
let files = await db.getSortedSetRange('plugins:active', 0, -1);
files = await getTemplateDirs(files);
files = await getTemplateFiles(files);
await Promise.all(Object.keys(files).map(async (name) => {
2019-07-23 14:26:02 -04:00
const filePath = files[name];
2020-08-14 00:05:03 -04:00
let imported = await fs.promises.readFile(filePath, 'utf8');
2019-07-23 14:26:02 -04:00
imported = await processImports(files, name, imported);
2020-01-31 14:10:00 -05:00
await mkdirp(path.join(viewsPath, path.dirname(name)));
2019-07-23 14:26:02 -04:00
2020-08-14 00:05:03 -04:00
await fs.promises.writeFile(path.join(viewsPath, name), imported);
const compiled = await Benchpress.precompile(imported, { minify: process.env.NODE_ENV !== 'development' });
2020-08-14 00:05:03 -04:00
await fs.promises.writeFile(path.join(viewsPath, name.replace(/\.tpl$/, '.js')), compiled);
2019-07-23 14:26:02 -04:00
}));
winston.verbose('[meta/templates] Successfully compiled templates.');
}
Templates.compile = compile;