forked from wso2/oxygen-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-icons.js
More file actions
134 lines (110 loc) · 4.55 KB
/
build-icons.js
File metadata and controls
134 lines (110 loc) · 4.55 KB
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
/**
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import fs from "fs";
import path from "path";
import { XMLParser } from "fast-xml-parser";
import prettier from "prettier";
const PACKAGE_NAME = "@wso2/oxygen-icons";
const iconsDir = path.resolve("icons");
const srcDir = path.resolve("src");
const distDir = path.resolve("dist");
const distIconsDir = path.join(distDir, "icons");
fs.rmSync(distDir, { recursive: true, force: true });
fs.mkdirSync(distIconsDir, { recursive: true });
// Copy CSS file to dist
if (fs.existsSync(path.join(srcDir, "icons.css"))) {
fs.copyFileSync(path.join(srcDir, "icons.css"), path.join(distDir, "icons.css"));
}
const parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: "",
});
const files = fs.readdirSync(iconsDir).filter((f) => f.endsWith(".svg"));
const indexExports = [];
const indexTypeExports = [];
/** Normalize SVG attributes (e.g. stroke-width → strokeWidth) */
function normalizeAttributes(attrs) {
const normalized = {};
for (const [key, value] of Object.entries(attrs)) {
const camelKey = key.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
normalized[camelKey] = value;
}
return normalized;
}
/** Convert filenames to PascalCase */
function toPascalCase(string) {
return string
.replace(/[-_]+/g, " ")
.replace(/\s(.)/g, (s) => s.toUpperCase())
.replace(/\s/g, "")
.replace(/^(.)/, (s) => s.toUpperCase());
}
async function generateIcons() {
for (const file of files) {
const svgContent = fs.readFileSync(path.join(iconsDir, file), "utf8");
const parsed = parser.parse(svgContent);
const svg = parsed.svg;
const baseName = path.basename(file, ".svg");
const svgName = svg.name || svg.id || toPascalCase(baseName);
const svgClass = svg.class || undefined;
const nodeElements = Object.entries(svg)
.filter(([tag]) =>
["path", "circle", "rect", "line", "polyline", "polygon", "ellipse", "g"].includes(tag)
)
.flatMap(([tag, val]) => {
const arr = Array.isArray(val) ? val : [val];
return arr.map((attrs) => [tag, normalizeAttributes(attrs)]);
});
const iconNodeJson = JSON.stringify(nodeElements, null, 2);
const tsSource = `
import { createLucideIcon } from "lucide-react";
const ${svgName}Icon = createLucideIcon("${svgName}", ${iconNodeJson}) as any;
${svgClass ? `${svgName}Icon.defaultProps = { className: "${svgClass}" };` : ""}
export const ${svgName} = ${svgName}Icon;
`;
const dtsSource = `
import type { LucideIcon } from "lucide-react";
export declare const ${svgName}: LucideIcon;
`;
const formattedTs = await prettier.format(tsSource, { parser: "typescript" });
const formattedDts = await prettier.format(dtsSource, { parser: "typescript" });
fs.writeFileSync(path.join(distIconsDir, `${svgName}.ts`), formattedTs);
fs.writeFileSync(path.join(distIconsDir, `${svgName}.d.ts`), formattedDts);
// --- For JS builds, include ".js" extension ---
indexExports.push(`export { ${svgName} } from "./icons/${svgName}.js";`);
// --- For .d.ts builds, no extension ---
indexTypeExports.push(`export { ${svgName} } from "./icons/${svgName}";`);
}
// Add CSS import at the beginning
const cssImport = `import "./icons.css";`;
// Add lucide-react re-export
indexExports.push(`export * from "lucide-react";`);
indexTypeExports.push(`export * from "lucide-react";`);
const indexContent = await prettier.format(
cssImport + "\n" + indexExports.join("\n"),
{ parser: "typescript" }
);
const indexTypesContent = await prettier.format(indexTypeExports.join("\n"), { parser: "typescript" });
fs.writeFileSync(path.join(distDir, "index.ts"), indexContent);
fs.writeFileSync(path.join(distDir, "index.d.ts"), indexTypesContent);
// eslint-disable-next-line no-undef
console.log(`✅ Generated ${files.length} icons in dist/icons`);
// eslint-disable-next-line no-undef
console.log(`✨ All icons + lucide-react are exported from ${PACKAGE_NAME}`);
}
await generateIcons();