2025-01-24 11:35:51 -06:00
|
|
|
import * as cheerio from "cheerio";
|
2023-10-08 11:15:19 -05:00
|
|
|
import fs from "fs/promises";
|
|
|
|
import path from "path";
|
|
|
|
import camelcase from "camelcase";
|
|
|
|
import * as prettier from "prettier";
|
2025-01-24 11:35:51 -06:00
|
|
|
import { nanoid } from "nanoid";
|
2023-10-08 11:15:19 -05:00
|
|
|
|
|
|
|
const prettierConfig = JSON.parse(await fs.readFile(".prettierrc", { encoding: "utf-8" }));
|
|
|
|
|
|
|
|
const iconsSrc = "./src/components/icons/svg/untitledui-icons";
|
|
|
|
const iconsDist = "./src/components/icons";
|
|
|
|
const iconsList = await fs.readdir(iconsSrc);
|
|
|
|
|
|
|
|
for (const filename of iconsList) {
|
|
|
|
const content = await fs.readFile(path.join(iconsSrc, filename), { encoding: "utf-8" });
|
|
|
|
const componentName = camelcase(path.basename(filename, ".svg"), { pascalCase: true });
|
|
|
|
|
|
|
|
const $ = cheerio.load(content);
|
|
|
|
|
|
|
|
const viewBox = $("svg").attr("viewBox");
|
|
|
|
const pathElements = $("path");
|
|
|
|
const paths = [];
|
|
|
|
for (const path of pathElements) {
|
2023-10-11 10:53:03 -05:00
|
|
|
// convert all attributes to camelcase
|
|
|
|
for (const [key, value] of Object.entries(path.attribs)) {
|
|
|
|
const ccKey = camelcase(key);
|
|
|
|
if (ccKey !== key) {
|
|
|
|
delete path.attribs[key];
|
|
|
|
path.attribs[ccKey] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-08 11:15:19 -05:00
|
|
|
if (path.attribs["stroke"]) {
|
|
|
|
path.attribs["stroke"] = "currentColor";
|
|
|
|
}
|
|
|
|
if (path.attribs["fill"]) {
|
|
|
|
path.attribs["fill"] = "currentColor";
|
|
|
|
} else path.attribs["fill"] = "none";
|
|
|
|
|
2025-01-24 11:35:51 -06:00
|
|
|
// add a "key" attribute to make react happy
|
|
|
|
path.attribs["key"] = nanoid(4);
|
|
|
|
|
2023-10-08 11:15:19 -05:00
|
|
|
paths.push($.html(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
const outputCode = await prettier.format(
|
|
|
|
`
|
|
|
|
import { createIcon } from "@chakra-ui/icons";
|
|
|
|
|
|
|
|
const ${componentName} = createIcon({
|
|
|
|
displayName: "${componentName}",
|
|
|
|
viewBox: "${viewBox}",
|
|
|
|
path: [
|
|
|
|
${paths.join(",\n")}
|
|
|
|
],
|
|
|
|
defaultProps: { boxSize: 4 },
|
|
|
|
});
|
|
|
|
|
|
|
|
export default ${componentName};
|
|
|
|
`,
|
|
|
|
{ ...prettierConfig, parser: "typescript" },
|
|
|
|
);
|
|
|
|
|
|
|
|
const outputPath = path.join(iconsDist, filename.replace(".svg", ".tsx"));
|
|
|
|
fs.writeFile(outputPath, outputCode, { encoding: "utf-8" });
|
|
|
|
console.log(`Wrote ${outputPath}`);
|
|
|
|
}
|