-
Notifications
You must be signed in to change notification settings - Fork 14
/
build.js
81 lines (72 loc) · 2.54 KB
/
build.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
const fs = require("fs");
const path = require("path");
const { exit } = require("process");
const exludeFolders = [".git", ".github"];
const imageExtensions = [".png", ".gif", ".webp"];
try {
fs
// get folders
.readdirSync("./", { withFileTypes: true })
// filter to get emojiFolders
.filter((item) => item.isDirectory() && !exludeFolders.includes(item.name))
// get folder name
.map((item) => item.name)
// return a promise handling emojis
.forEach((folderName) => {
const folderPath = path.resolve(__dirname, folderName);
const infoPath = path.resolve(folderPath, "info.json");
// test if info file exists
if (!fs.existsSync(infoPath))
throw new Error(`Info File in ${folderPath} is missing`);
// parse info
const info = JSON.parse(fs.readFileSync(infoPath, { encoding: "utf-8" }));
const images =
// read emoji dir
fs
.readdirSync(folderPath, { withFileTypes: true })
// filter to get image files
.filter(
(item) =>
item.isFile() && imageExtensions.includes(path.extname(item.name))
)
// get image name
.map((item) => item.name);
const { prefix, type } = info;
// check if the given info is valid
if (
typeof info.name !== "string" ||
!info.name ||
info.items.some((item) =>
item.includes(`${prefix}${info.name}.${type}`)
)
)
throw new Error(`Field "name" is invalid in ${folderPath} info file.`);
if (!images.every((item) => item.startsWith(prefix)))
throw new Error(
`Field "prefix" is invalid in ${folderPath} info file.`
);
if (!images.every((item) => item.endsWith(type)))
throw new Error(`Field "type" is invalid in ${folderPath} info file.`);
if (!Array.isArray(info.items))
throw new Error(`Field "items" is invalid in ${folderPath} info file.`);
const items = images.map((item) =>
item
// remove prefix
.replace(new RegExp(`^${prefix}`), "")
// remove type
.replace(new RegExp(`\.${type}$`), "")
);
info.items = Array.from(
new Set([
// filter old emojis and remain existing ones
...info.items.filter((item) => items.includes(item)),
...items,
])
);
// update info
fs.writeFileSync(infoPath, JSON.stringify(info), { encoding: "utf-8" });
});
} catch (err) {
console.error("Process end with error:", err);
exit(1);
}