-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathtemplate.js
62 lines (52 loc) · 1.76 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
const { resolve } = require("path");
const { readdir, readFile } = require("fs/promises");
const { object, string, number, array, is, optional } = require("superstruct");
const { objectFromBuffer, Logger } = require("./utils");
const dirIgnore = ["config.json"];
async function getTemplates(templatesDir, encode = "utf8") {
try {
const allDirFiles = await readdir(templatesDir);
const templatesDirNames = allDirFiles.filter((fileName) => !dirIgnore.includes(fileName));
const templates = await Promise.all(
templatesDirNames.map(async (templateName) => {
const templateBuffer = await readFile(
resolve(templatesDir, templateName, "template.json"),
encode,
);
return {
name: templateName,
path: templatesDir,
params: objectFromBuffer(templateBuffer),
};
}),
);
if (!templates.some(validateTemplate)) {
Logger.err("Templates validation error.");
return;
}
return templates;
} catch (error) {
Logger.err(error);
}
}
// TODO: May be with postEffects, images and etc? (optional fontSize, fill and etc)
const Layout = object({
type: optional(string()),
name: string(),
fill: optional(string()),
stroke: optional(string()),
top: optional(number()),
left: optional(number()),
transform: optional(string()),
fontSize: optional(number()),
fontWeight: optional(number()),
});
const Template = object({
image: string(),
font: string(),
layout: array(Layout),
});
function validateTemplate({ params }) {
return is(params, Template);
}
module.exports = { getTemplates };