File size: 3,427 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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`
<svg width="400" height="300" viewBox="0 0 400 300" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect x="85" y="112" width="6" height="67" fill="#16402D"/>
<rect x="108" y="116" width="207" height="10" fill="#2C7A51"/>
<rect x="108" y="140" width="207" height="10" fill="#2C7A51"/>
<rect x="142" y="164" width="173" height="10" fill="#2C7A51"/>
<line x1="107" y1="168.5" x2="131" y2="168.5" stroke="#16402D"/>
</svg>
`

/* -----------------------------------------------------------------------------
 * 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("</svg>;", "</svg>")}
    )
})
`

  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)