-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.tsx
183 lines (157 loc) · 5.14 KB
/
build.tsx
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import * as path from "node:path";
import * as fs from "node:fs";
import { fileURLToPath } from "node:url";
import React from "react";
import * as allIcons from "react-icons";
import decamelize from "decamelize";
import * as mkdirp from "mkdirp";
import { renderToStaticMarkup } from "react-dom/server";
import * as cheerio from "cheerio";
import ora, { spinners } from "ora";
type IconInfo = {
name: string;
projectUrl: string;
license: string;
licenseUrl: string;
components: string[];
elements: string[];
};
/**
* Constants
*/
const DECAMELIZE_OPTS = {
separator: "-",
};
const STRING_BEFORE_NUMBER_REGX = /([a-zA-Z])(\d)/;
const DEVMODE = process.env.DEVMODE;
const IconSet: Set<[elementName: string, elementContent: string]> = new Set();
const IconInfo: Record<string, IconInfo> = {};
const IconList: Set<{ id: string; label: string }> = new Set();
/**
* Path, template, etc
*/
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const distPath = path.resolve(__dirname, "dist");
mkdirp.sync(distPath);
// Template
const templatePath = path.resolve(__dirname, "templates");
const customElementTemplate = fs.readFileSync(path.join(templatePath, "custom-element.html"), "utf8");
const metaPath = path.resolve(distPath, "meta");
mkdirp.sync(metaPath);
// Public
const publicPath = path.resolve(__dirname, "public");
/**
* Runtime
*/
const spinner = ora({
spinner: {
interval: 80,
frames: spinners.dots.frames,
},
}).start();
/**
* Functions, misc
*/
const writeCustomElement = async (iconName: string, content: string) => {
const iconPath = path.join(distPath, `${iconName}.html`);
await fs.promises.writeFile(iconPath, content);
};
const cleanupSVG = (content: string) => {
if (!content) return "";
try {
const $ = cheerio.load(content);
const svg = $("svg");
svg.removeAttr("width");
svg.removeAttr("height");
return svg.prop("outerHTML");
} catch (err) {
console.error("error", content, err);
}
};
const buildCustomElements = async () => {
//@ts-expect-error
const iconManifest = allIcons.IconsManifest;
const iconManifestLength = !DEVMODE ? iconManifest.length : 1;
for (let { id, ...info } of iconManifest.sort((a, z) => a.id.localeCompare(z.id)).slice(0, iconManifestLength)) {
const importPath = `react-icons/${id}`;
const icons = await import(importPath);
if (!icons) continue;
const iconEntries = Object.entries(icons);
const iconEntriesLength = !DEVMODE ? iconEntries.length : 10;
const iconList = iconEntries.reduce((acc, [componentName]) => {
if (componentName === "default") return acc;
return [...acc, [componentName, decamelize(componentName.replace(STRING_BEFORE_NUMBER_REGX, "$1-$2"), DECAMELIZE_OPTS)]];
}, []);
IconInfo[id] = {
...info,
components: iconList.map(([componentName]) => componentName),
elements: iconList.map(([, elementName]) => elementName),
};
IconList.add({ id, label: info.name });
for (const [iconName, elementName] of iconList.slice(0, iconEntriesLength)) {
const IconComponent = await import(importPath).then((module) => module[iconName]);
const svgIcon = cleanupSVG(await renderToStaticMarkup(<IconComponent />));
spinner.text = `Icon \`${iconName}\` rendered.`;
spinner.render();
const elementContent = customElementTemplate.replace("$name", elementName).replace("$svg", svgIcon);
IconSet.add([elementName, elementContent]);
}
}
try {
const iconArray = Array.from(IconSet).sort(([a], [z]) => a.localeCompare(z));
const sliceArray = !DEVMODE ? iconArray.length : 10;
await Promise.allSettled(
iconArray.slice(0, sliceArray).map(
async ([elementName, elementContent]) =>
await writeCustomElement(elementName, elementContent).then(() => {
spinner.text = `Icon \`${elementName}\` written.`;
spinner.render();
})
)
);
spinner.stopAndPersist({
symbol: "✅",
text: "All 🦄 unicons elements are written.",
});
} catch (err) {
console.error(err);
}
};
const copyIconMeta = async () => {
try {
for (const [iconId, icons] of Object.entries(IconInfo)) {
await fs.promises.writeFile(path.join(metaPath, `icon-${iconId}.json`), JSON.stringify(icons));
}
if (!DEVMODE) {
await fs.promises.writeFile(path.join(metaPath, "icon-list.json"), JSON.stringify(Array.from(IconList)));
}
spinner.stopAndPersist({
symbol: "✅",
text: "Required JSON data are copied!",
});
} catch (err) {
console.error(err);
}
};
const copyPublicDir = async () => {
const publicFiles = fs.readdirSync(publicPath);
try {
for await (const file of publicFiles) {
const filePath = path.join(publicPath, file);
const distFilePath = path.join(distPath, file);
fs.cpSync(filePath, distFilePath, { recursive: true });
spinner.text = `File \`${file}\` copied.`;
spinner.render();
}
spinner.stopAndPersist({
symbol: "✅",
text: "All public files are copied!",
});
} catch (err) {
console.error(err);
}
};
// Start the build
await buildCustomElements();
await copyIconMeta();
await copyPublicDir();