-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.js
48 lines (41 loc) · 1.5 KB
/
render.js
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
const fs = require('fs');
const hbs = require('handlebars');
const { root, project } = require('./config');
module.exports = ({
TemplateName, afterContent = [], extras = [],
}) => {
const [layout, template, groupId] = TemplateName.split('_');
const layoutBase = `${root}/${project}/server/api/${layout}/emails`;
const layoutHTML = fs.readFileSync(`${layoutBase}/${layout}.hbs`).toString();
const params = {
template: fs.readFileSync(`${layoutBase}/${template}/${template}_${groupId}.hbs`).toString(),
};
extras.forEach((part) => {
let piece = part;
let partPath = `${layoutBase}/${template}/${part}.hbs`;
if (part.includes(':')) {
const [type, currentPiece] = part.split(':');
const path = type === 'global' ? 'partials' : layout;
piece = currentPiece;
partPath = `${root}/${project}/server/emails/${path}/${currentPiece}.hbs`;
}
params[piece] = fs
.readFileSync(partPath)
.toString();
});
if (afterContent.length) {
params.afterContent = afterContent
.map((part) => {
let partPath = `${layoutBase}/${template}/${part}.hbs`;
if (part.includes(':')) {
const [type, piece] = part.split(':');
const path = type === 'global' ? 'partials' : layout;
partPath = `${root}/${project}/server/emails/${path}/${piece}.hbs`;
}
return fs
.readFileSync(partPath)
.toString();
}).join('');
}
return hbs.compile(layoutHTML)(params).replace('<!--ad-->', '{{{ad}}}');
};