You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/meta/templates.js

150 lines
4.3 KiB
JavaScript

8 years ago
'use strict';
10 years ago
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');
const winston = require('winston');
const path = require('path');
const util = require('util');
const fs = require('fs');
const fsReadFile = util.promisify(fs.readFile);
const fsWriteFile = util.promisify(fs.writeFile);
const nconf = require('nconf');
const _ = require('lodash');
const Benchpress = require('benchpressjs');
const plugins = require('../plugins');
const file = require('../file');
const db = require('../database');
const viewsPath = nconf.get('views_dir');
const Templates = module.exports;
async function processImports(paths, templatePath, source) {
var regex = /<!-- IMPORT (.+?) -->/;
8 years ago
var matches = source.match(regex);
8 years ago
if (!matches) {
return source;
}
8 years ago
var partial = matches[1];
if (paths[partial] && templatePath !== partial) {
const partialSource = await fsReadFile(paths[partial], 'utf8');
source = source.replace(regex, partialSource);
return await processImports(paths, templatePath, source);
}
8 years ago
winston.warn('[meta/templates] Partial not loaded: ' + matches[1]);
source = source.replace(regex, '');
8 years ago
return await processImports(paths, templatePath, source);
}
Templates.processImports = processImports;
8 years ago
const themeNamePattern = /^(@.*?\/)?nodebb-theme-.*$/;
async function getTemplateDirs(activePlugins) {
const pluginTemplates = activePlugins.map(function (id) {
if (themeNamePattern.test(id)) {
return nconf.get('theme_templates_path');
}
if (!plugins.pluginsData[id]) {
return '';
}
7 years ago
return path.join(__dirname, '../../node_modules/', id, plugins.pluginsData[id].templates || 'templates');
}).filter(Boolean);
8 years ago
let themeConfig = require(nconf.get('theme_config'));
let theme = themeConfig.baseTheme;
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));
templateDirs = await Promise.all(templateDirs.map(async path => (await file.exists(path) ? path : false)));
return templateDirs.filter(Boolean);
}
async function getTemplateFiles(dirs) {
fix(style): updated code to follow new eslint recommendations Squashed commit of the following: commit f9ce878b269b3568f0d649309aae1af4dcfdfeef Author: Julian Lam <[email protected]> Date: Tue Aug 13 14:30:46 2019 -0400 fix(style): updated code to follow new eslint recommendations commit 80dd370e413f22badb96ff2138e7991dfff6d836 Author: Julian Lam <[email protected]> Date: Tue Aug 13 14:14:58 2019 -0400 fix(deps): update dependency sitemap to v4 Squashed commit of the following: commit f4dd9cabb21e26fdc21f8413be822ea7c64251f8 Author: Julian Lam <[email protected]> Date: Tue Aug 13 11:33:05 2019 -0400 fix: resolved breaking changes from sitemap v4 upgrade commit 9043415ee16dcc27a8dcc2e4479d1bc5e2d1b60e Merge: e3352b272 72590b346 Author: Julian Lam <[email protected]> Date: Tue Aug 13 11:09:55 2019 -0400 Merge branch 'master' into renovate/sitemap-4.x commit e3352b272eb9400bdb00774973181397803765e4 Author: Renovate Bot <[email protected]> Date: Mon Aug 12 07:59:05 2019 +0000 fix(deps): update dependency sitemap to v4 commit 8e3c0cdcae22acc32d352be8bb72d60e7502dbc5 Author: Renovate Bot <[email protected]> Date: Fri Aug 9 00:49:51 2019 +0000 fix(deps): update dependency commander to v3 commit 2104449d38818f2fa4d44b3a58a0a168781acbfb Author: Renovate Bot <[email protected]> Date: Tue Aug 13 15:00:27 2019 +0000 fix(deps): update dependency mongodb to v3.3.0 commit d2937f446a21131c070ae5d0ff33d67cfe465b8c Author: Barış Soner Uşaklı <[email protected]> Date: Tue Aug 13 10:36:48 2019 -0400 feat: async/await admin/controllers commit 1b97e8b199f960dc24e5722702f27499ae049914 Author: Misty (Bot) <[email protected]> Date: Tue Aug 13 09:28:39 2019 +0000 Latest translations and fallbacks commit 69a48957a2f0d23c4d194b664bda3a0431179c01 Author: Barış Soner Uşaklı <[email protected]> Date: Mon Aug 12 21:56:09 2019 -0400 feat: async/await commit b9b2a7e593a452de4bef6d0ab6abe368a3bdb8dd Author: Barış Soner Uşaklı <[email protected]> Date: Mon Aug 12 20:58:29 2019 -0400 feat: async/await refactor controllers/accounts commit a8d43a175974a0c8ae3dc132bf51a7ed9a4c6305 Author: Baris Usakli <[email protected]> Date: Mon Aug 12 14:49:40 2019 -0400 feat: async/await controllers/accounts commit 2f25aae57bf9dbe98d655276770e56bed9ec023b Author: Barış Soner Uşaklı <[email protected]> Date: Sun Aug 11 23:09:50 2019 -0400 fix: #7831, fix pagination convert to async/await commit c9e83f2374572264855a04156278eef256b0a20c Author: Barış Soner Uşaklı <[email protected]> Date: Sun Aug 11 00:14:35 2019 -0400 fix: remove empty line commit 30be91b26c4dd7583412c4e8d56e9c1688e48a44 Author: Barış Soner Uşaklı <[email protected]> Date: Sun Aug 11 00:13:41 2019 -0400 fix: remove useless catchs and empty line commit 2e4a71c0b6104738f15ffbfe3246105b922fdfb3 Author: Renovate Bot <[email protected]> Date: Sat Aug 10 06:51:50 2019 +0000 chore(deps): update dependency eslint-config-airbnb-base to v14
6 years ago
const buckets = await Promise.all(dirs.map(async dir => {
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;
}
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: global.env !== 'development',
});
return await fsWriteFile(path.join(viewsPath, filename.replace(/\.tpl$/, '.js')), compiled);
}
Templates.compileTemplate = compileTemplate;
async function compile() {
const _rimraf = util.promisify(rimraf);
const _mkdirp = util.promisify(mkdirp);
await _rimraf(viewsPath);
await _mkdirp(viewsPath);
let files = await db.getSortedSetRange('plugins:active', 0, -1);
files = await getTemplateDirs(files);
files = await getTemplateFiles(files);
fix(style): updated code to follow new eslint recommendations Squashed commit of the following: commit f9ce878b269b3568f0d649309aae1af4dcfdfeef Author: Julian Lam <[email protected]> Date: Tue Aug 13 14:30:46 2019 -0400 fix(style): updated code to follow new eslint recommendations commit 80dd370e413f22badb96ff2138e7991dfff6d836 Author: Julian Lam <[email protected]> Date: Tue Aug 13 14:14:58 2019 -0400 fix(deps): update dependency sitemap to v4 Squashed commit of the following: commit f4dd9cabb21e26fdc21f8413be822ea7c64251f8 Author: Julian Lam <[email protected]> Date: Tue Aug 13 11:33:05 2019 -0400 fix: resolved breaking changes from sitemap v4 upgrade commit 9043415ee16dcc27a8dcc2e4479d1bc5e2d1b60e Merge: e3352b272 72590b346 Author: Julian Lam <[email protected]> Date: Tue Aug 13 11:09:55 2019 -0400 Merge branch 'master' into renovate/sitemap-4.x commit e3352b272eb9400bdb00774973181397803765e4 Author: Renovate Bot <[email protected]> Date: Mon Aug 12 07:59:05 2019 +0000 fix(deps): update dependency sitemap to v4 commit 8e3c0cdcae22acc32d352be8bb72d60e7502dbc5 Author: Renovate Bot <[email protected]> Date: Fri Aug 9 00:49:51 2019 +0000 fix(deps): update dependency commander to v3 commit 2104449d38818f2fa4d44b3a58a0a168781acbfb Author: Renovate Bot <[email protected]> Date: Tue Aug 13 15:00:27 2019 +0000 fix(deps): update dependency mongodb to v3.3.0 commit d2937f446a21131c070ae5d0ff33d67cfe465b8c Author: Barış Soner Uşaklı <[email protected]> Date: Tue Aug 13 10:36:48 2019 -0400 feat: async/await admin/controllers commit 1b97e8b199f960dc24e5722702f27499ae049914 Author: Misty (Bot) <[email protected]> Date: Tue Aug 13 09:28:39 2019 +0000 Latest translations and fallbacks commit 69a48957a2f0d23c4d194b664bda3a0431179c01 Author: Barış Soner Uşaklı <[email protected]> Date: Mon Aug 12 21:56:09 2019 -0400 feat: async/await commit b9b2a7e593a452de4bef6d0ab6abe368a3bdb8dd Author: Barış Soner Uşaklı <[email protected]> Date: Mon Aug 12 20:58:29 2019 -0400 feat: async/await refactor controllers/accounts commit a8d43a175974a0c8ae3dc132bf51a7ed9a4c6305 Author: Baris Usakli <[email protected]> Date: Mon Aug 12 14:49:40 2019 -0400 feat: async/await controllers/accounts commit 2f25aae57bf9dbe98d655276770e56bed9ec023b Author: Barış Soner Uşaklı <[email protected]> Date: Sun Aug 11 23:09:50 2019 -0400 fix: #7831, fix pagination convert to async/await commit c9e83f2374572264855a04156278eef256b0a20c Author: Barış Soner Uşaklı <[email protected]> Date: Sun Aug 11 00:14:35 2019 -0400 fix: remove empty line commit 30be91b26c4dd7583412c4e8d56e9c1688e48a44 Author: Barış Soner Uşaklı <[email protected]> Date: Sun Aug 11 00:13:41 2019 -0400 fix: remove useless catchs and empty line commit 2e4a71c0b6104738f15ffbfe3246105b922fdfb3 Author: Renovate Bot <[email protected]> Date: Sat Aug 10 06:51:50 2019 +0000 chore(deps): update dependency eslint-config-airbnb-base to v14
6 years ago
await Promise.all(Object.keys(files).map(async name => {
const filePath = files[name];
let imported = await fsReadFile(filePath, 'utf8');
imported = await processImports(files, name, imported);
await _mkdirp(path.join(viewsPath, path.dirname(name)));
await fsWriteFile(path.join(viewsPath, name), imported);
const compiled = await Benchpress.precompile(imported, { minify: global.env !== 'development' });
await fsWriteFile(path.join(viewsPath, name.replace(/\.tpl$/, '.js')), compiled);
}));
winston.verbose('[meta/templates] Successfully compiled templates.');
}
Templates.compile = compile;