import { transform } from "@svgr/core" import { readFileSync, writeFileSync } from "fs" import { format } from "prettier" const html = String.raw /* ----------------------------------------------------------------------------- * Edit code here * -----------------------------------------------------------------------------*/ const name = "Blockquote" // prettier-ignore const svgCode = html` ` /* ----------------------------------------------------------------------------- * Implmentation * -----------------------------------------------------------------------------*/ function toDashCase(str: string) { return str .replace(/([A-Z])/g, "-$1") .replace(/^-/, "") .toLowerCase() } const dashName = toDashCase(name) const defaultPalette = { 0: "white", 1: "#2CFF80", //accentColor 2: "#2C7A51", 3: "#16402D", 4: "#1C4D37", 5: "#287753", 6: "#1F8B56", 7: "#2AB26B", 8: "#1E6943", 9: "#C1FFDF", 10: "#41B883", 11: "#299464", 12: "#2AB36B", 13: "#9FFFCD", 14: "#0E432B", 15: "#D9D9D9", } function findColors(code: string) { const pattern = /"#.*?"/g const matches = code.match(pattern) || [] return Array.from(new Set(matches.map((match) => match.slice(1, -1)))) } function replaceColors(code: string) { const colors = findColors(code) if (colors.length === 0) { return code.replaceAll(`"white"`, `{palette[0]}`) } const regex = new RegExp( colors .map((c) => `"${c}"`) .map((str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")) .join("|"), "g", ) return code .replace(regex, (c) => { const num = Object.entries(defaultPalette).find( (cc) => `"${cc[1]}"` === c, ) return `{palette[${num?.[0]}]}` }) .replaceAll(`"white"`, `{palette[0]}`) } export const main = async () => { const jsxCode = transform.sync(svgCode, { plugins: ["@svgr/plugin-jsx"], }) let template = ` import { createComponent } from "./create-component" export const ${name}Anatomy = createComponent((props) => { const { palette, ...rest } = props return ( ${jsxCode .replace('import * as React from "react";\n', "") .replace("const SvgComponent = props =>", "") .replace("export default SvgComponent;", "") .replace("{...props}", "{...rest}") .replace(";", "")} ) }) ` template = await format(replaceColors(template), { parser: "typescript" }) if (process.argv.includes("--dry-run")) { console.log(template) process.exit(0) } writeFileSync(`./components/illustrations/${dashName}.tsx`, template, "utf-8") const content = readFileSync( "./components/illustrations/index.ts", "utf-8", ).replace( "\n\nexport const allComponents = {", ` import { ${name}Anatomy } from "./${dashName}" export const allComponents = { "${dashName}": ${name}Anatomy,`, ) writeFileSync("./components/illustrations/index.ts", content, "utf-8") } main().catch(console.error)