-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
83 lines (71 loc) · 1.86 KB
/
index.js
File metadata and controls
83 lines (71 loc) · 1.86 KB
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
const fs = require("fs");
const path = require("path");
const pathExistedOrCreate = require("path-existed-or-create");
const mjml2html = require("mjml");
const rimraf = require("rimraf");
const { htmlToText } = require("html-to-text");
function isDir(dirPath) {
return fs.lstatSync(dirPath).isDirectory();
}
function isEmailTemplate(templatePath) {
return ".mjml" === path.extname(templatePath);
}
let rootTemplate = "";
function handleTemplateDir(
templateDirPath,
genDirPath,
ignore = [],
clear = false
) {
if (clear) {
rimraf.sync(genDirPath);
}
if (!rootTemplate) {
rootTemplate = templateDirPath;
}
const files = fs.readdirSync(templateDirPath);
const lengthFiles = files.length;
for (let i = 0; i < lengthFiles; i++) {
const name = files[i];
let willGenPath = "";
if (rootTemplate != templateDirPath) {
willGenPath = templateDirPath.replace(`${rootTemplate}/`, "");
}
const genPath = path.resolve(genDirPath, willGenPath);
try {
pathExistedOrCreate(genPath);
} catch (e) {
console.log(e);
}
if (ignore.includes(name)) {
continue;
}
const currentPath = path.resolve(templateDirPath, name);
if (isDir(currentPath)) {
handleTemplateDir(currentPath, genDirPath, ignore, false);
}
if (isEmailTemplate(currentPath)) {
const html = mjml2html(
fs.readFileSync(currentPath, { encoding: "utf8" }),
{ filePath: currentPath }
);
fs.writeFileSync(
`${path.resolve(
genPath,
`${path.basename(currentPath, ".mjml")}.html`
)}`,
html.html
);
fs.writeFileSync(
`${path.resolve(
genPath,
`${path.basename(currentPath, ".mjml")}.txt`
)}`,
htmlToText(html.html, {
wordwrap: 130,
})
);
}
}
}
module.exports = handleTemplateDir;