-
Notifications
You must be signed in to change notification settings - Fork 1
/
parseSvgs.ts
118 lines (103 loc) · 3.55 KB
/
parseSvgs.ts
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
console.time("program run");
import { readdir } from "fs/promises";
import { join } from "path";
const svgsList = await readdir("./svg");
let categories = [
"face",
"makeup",
"wrinkles",
"eyebrows",
"eyes",
"nose",
"mouth",
"mustache",
"goatee",
"hair",
"glasses",
"hat",
];
let json: { [key: string]: string[] } = {};
function parseSvg(svg: string): string {
svg = svg
.replace(/fill="#6C7070"/g, 'fill="var(--eye-color)"')
.replace(/stroke="#0010BF"/g, 'stroke="black"')
.replace(/fill="#0010BF"/g, 'fill="black"')
.replace(
/fill="white" stroke="#6F6F6F"/g,
'fill="var(--icon-face-fill)" stroke="var(--icon-face-stroke)"'
)
// Hair icons
.replace(
/fill="white" stroke="#BFBFBF"/g,
'fill="var(--icon-head-fill)" stroke="var(--icon-head-stroke)"'
)
.replace(/fill="#5F5F5F"\/>/g, 'fill="var(--icon-hair-fill)"/>')
.replace(/fill="#1E1E1E"/g, 'fill="var(--icon-facial-hair-fill)"')
.replace(/fill="#BFBFBF"/g, 'fill="var(--icon-hair-tie)"')
// Glasses
.replace(
/fill="white" stroke="#BFBEBD"/g,
'fill="var(--icon-head-fill)" stroke="var(--icon-head-stroke)"'
)
.replace(/stroke="#7B0000"/g, 'stroke="var(--icon-glasses-fill)"')
.replace(/fill="#7B0000"/g, 'fill="var(--icon-glasses-fill)"')
.replace(/fill="#030303"/g, 'fill="currentColor"')
.replace(/stroke="#030303"/g, 'stroke="currentColor"')
.replace(/fill="#F60000"/g, 'fill="var(--icon-glasses-shade)"')
.replace(
/fill="white" stroke="#999999"/g,
'fill="var(--icon-head-fill)" stroke="var(--icon-head-stroke)"'
)
.replace(/fill="#808080"/g, 'fill="var(--icon-hat-fill)"')
.replace(/fill="#404040"/g, 'fill="var(--icon-hat-stroke)"')
.replace(/fill="#8D8D8D"/g, 'fill="var(--icon-face-detail)"')
.replace(/#996D54/g, "var(--icon-face-wrinkles)")
.replace(/#E30000/g, "var(--icon-eyebrow-fill)")
.replace(/#FF5F5F/g, "currentColor")
.replace(/#0055FF/g, "var(--icon-mouth-tooth)")
.replace(/#712A04/g, "var(--icon-lip-color-top)")
.replace(/#BE4E26/g, "var(--icon-lip-color-bottom)")
.replace(/<rect[^>]*\/>/g, "");
return svg;
}
// Step 1: Sort the files by category then number
svgsList.sort((a, b) => {
const categoryA = a.split("-")[0];
const categoryB = b.split("-")[0];
const numberA = parseInt(a.split("-")[1]);
const numberB = parseInt(b.split("-")[1]);
// Compare categories first
if (categoryA !== categoryB) {
return categories.indexOf(categoryA) - categories.indexOf(categoryB);
}
// If categories are the same, compare numbers
return numberA - numberB;
});
for (const item of svgsList) {
const cat = categories.find((cat) => item.startsWith(cat));
if (cat) {
if (!json[cat]) {
json[cat] = [];
}
try {
let fileData = await Bun.file(join("./svg", item)).text();
fileData = parseSvg(fileData);
json[cat].push(fileData);
} catch (error) {
console.log("Error in", item);
}
} else {
}
}
// Sort the keys of 'datas' based on the order of 'categories'
const sortedKeys = Object.keys(json).sort((a, b) => {
return categories.indexOf(a) - categories.indexOf(b);
});
// Create a new object with the sorted keys
const sortedDatas: { [key: string]: string[] } = {};
for (const key of sortedKeys) {
sortedDatas[key] = json[key];
}
Bun.write("./public/dist/icons.json", JSON.stringify(sortedDatas, null, 2));
console.timeEnd("program run");
console.log("Done");