-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.js
156 lines (132 loc) · 3.84 KB
/
template.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
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
const fs = require("fs");
const path = require("path");
const i18next = require("i18next");
require("dotenv").config();
const color = require("cli-color");
const _snarkdown = require("snarkdown");
const snarkdown = (md) => {
if (md) {
const htmls = md
.split(/(?:\r?\n){2,}/)
.map((l) =>
[" ", "\t", "#", "-", "*"].some((ch) => l.startsWith(ch))
? _snarkdown(l)
: `<p>${_snarkdown(l)}</p>`
);
return htmls.join("\n\n");
}
};
const from =
process.env.REACT_APP_TEMPLATES_FOLDER ||
process.env.REACT_APP_CONFIG_FOLDER + "email/digest/";
const configFolder = process.env.REACT_APP_CONFIG_FOLDER;
i18next.init({
lng: "en", // if you're using a language detector, do not define the lng option
debug: false,
resources: {
en: {},
},
});
const getCampaign = (campaign) => {
const p = path.resolve(
__dirname,
configFolder + `/campaign/${campaign}.json`
);
if (!fs.existsSync(p)) {
console.error(color.red("no campaign config file for", campaign));
process.exit(1);
}
return JSON.parse(fs.readFileSync(p, "utf8"));
}
const getSender = campaignName => {
const campaign = getCampaign(campaignName);
return campaign.config.component.email.sender;
}
const resolve = (campaign, name, lang, ext) => {
const p = path.resolve(
__dirname,
from + `${campaign}/${name}/${lang}.${ext}`
);
if (!fs.existsSync(p)) {
console.warn(
color.red("no template for ", campaign, name, lang),
", trying in en"
);
return path.resolve(__dirname, from + `${campaign}/${name}/en.${ext}`);
}
return p;
};
const pickOne = text => {
const variants = text
.split("\n")
.map((d) => d.trim())
.filter (d => d) //remove empty lines
return variants[Math.floor(Math.random() * variants.length)];
};
const subject = (campaign, name, lang) => {
let p = resolve(campaign, name, lang, "json");
if (!fs.existsSync(p)) {
console.error("config file missing", p);
return false;
}
let config = JSON.parse(fs.readFileSync(p, "utf8"));
if (!config.meta?.subject) {
let p = resolve(campaign, name, "en", "json");
config = JSON.parse(fs.readFileSync(p, "utf8"));
}
if (!config.meta.subject) return;
return pickOne(config.meta.subject);
};
const getExtra = (area, campaign) => {
const p = path.resolve(
__dirname,
configFolder + `campaign/${campaign}_extra.json`
);
return fs.existsSync(p)
? JSON.parse(fs.readFileSync(p, "utf8")).config?.locales?.en[area.toLowerCase()]
: 0
}
const insertVariables = (template, variables) => {
return i18next.t(template, {
...variables,
interpolation: { escapeValue: false },
});
};
const html = (campaign, name, lang) => {
let p = resolve(campaign, name, lang, "html");
if (!fs.existsSync(p)) {
console.error("HTML does not exist:", p);
return;
}
return fs.readFileSync(p, "utf8");
};
const getTokens = (html) => {
const tokens = [];
html.replace(/\{\{(.*?)}}/g, function (a, b) {
tokens.push(b);
});
return tokens;
};
const getLetter = (campaignName, locale = "en") => {
const campaign=getCampaign(campaignName);
const locales = campaign.config.locales;
let texts = locales[locale] || null;
if (!texts || !texts["server:"] || !texts["server:"].letter) {
console.warn(color.yellow(" no letter for", locale), "defaulting to en");
locale = "en";
texts = locales[locale];
}
return `<div style="border: solid #d3d3d3 10px; padding: 2%">${snarkdown(
texts["server:"].letter)}</div>`;
};
const getFallback = (fallbackFile) => {
const p = path.resolve(
__dirname,
from + `${fallbackFile}`
);
if (!fs.existsSync(p)) {
console.warn(color.red("no fallback for ", fallbackFile));
}
return fs.readFileSync(p, "utf8");
}
module.exports = { subject, html, insertVariables, getTokens, getLetter, getFallback, getCampaign, getSender,getExtra };