-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgenerate-icon-components.mjs
71 lines (56 loc) · 1.94 KB
/
generate-icon-components.mjs
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
import { pascalCase } from "change-case";
import { existsSync } from "node:fs";
import { mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
import { basename, dirname, extname, join } from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const ICON_FOLDER = join(__dirname, "..", "public", "icons");
const COMPONENT_ICON_FOLDER = join(
__dirname,
"..",
"addon",
"components",
"icons",
);
const iconsWithFill = [];
const files = await readdir(ICON_FOLDER);
const icons = files
.filter((file) => extname(file) === ".svg")
.map((svg) => basename(svg, ".svg"));
await prepareOutputDir();
const promises = icons.map((svg) => {
return generateComponent(svg);
});
await Promise.all(promises);
if (iconsWithFill.length > 0) {
throw new Error(
`The following icons have a fill attribute which might cause issues: ${iconsWithFill.join(", ")}`,
);
}
async function generateComponent(iconName) {
const componentName = pascalCase(iconName, {
mergeAmbiguousCharacters: true,
});
const iconContent = (await readFile(join(ICON_FOLDER, iconName + ".svg")))
.toString()
.replace(">", " ...attributes>"); // We assume the first closing bracket belongs to the svg element
if (iconContent.includes('fill="')) {
iconsWithFill.push(iconName);
}
const componentContent = `// THIS FILE IS GENERATED. ANY CHANGES TO THIS FILE WILL BE LOST.
import type { TOC } from '@ember/component/template-only';
export interface ${componentName}IconSignature {
Element: SVGSVGElement;
}
export const ${componentName}Icon: TOC<${componentName}IconSignature> = <template>${iconContent}</template>;`;
await writeFile(
join(COMPONENT_ICON_FOLDER, iconName + ".gts"),
componentContent,
);
}
async function prepareOutputDir() {
if (existsSync(COMPONENT_ICON_FOLDER)) {
await rm(COMPONENT_ICON_FOLDER, { recursive: true });
}
await mkdir(COMPONENT_ICON_FOLDER);
}