1 import { createElement } from "react"
2 import { renderToStaticMarkup } from "react-dom/server"
3 import * as icons from "@medusajs/icons"
4 import { readdirSync, readFileSync, writeFileSync } from "fs"
5 import { basename, dirname, extname, join } from "path"
6 import { fileURLToPath } from "url"
7
8 const ICON_MAP = {
9 bolt: "Bolt",
10 cloud: "CloudSolid",
11 computer: "ComputerDesktop",
12 database: "CircleStack",
13 folder: "Folder",
14 globe: "Globe",
15 key: "Key",
16 search: "MagnifyingGlass",
17 server: "Server",
18 sync: "ArrowPath",
19 }
20
21 const scriptsDir = dirname(fileURLToPath(import.meta.url))
22 const CUSTOM_ICONS_DIR = join(scriptsDir, "mermaid-icons")
23 const OUTPUT_PATH = join(
24 scriptsDir,
25 "../src/components/MermaidDiagram/icons.json"
26 )
27
28 // the icons are drawn at 15px but rendered at ~48px in diagrams, where their
29 // original stroke width reads as too heavy
30 const STROKE_WIDTH_SCALE = 0.35
31
32 const VIEWBOX_REGEX = /viewBox="(-?[\d.]+) (-?[\d.]+) ([\d.]+) ([\d.]+)"/
33 const SVG_TAG_REGEX = /<svg([^>]*)>([\s\S]*)<\/svg>/
34 const ROOT_FILL_REGEX = /fill="([^"]+)"/
35
36 const lightenStrokes = (body) =>
37 body.replace(
38 /stroke-width="([\d.]+)"/g,
39 (_, width) =>
40 `stroke-width="${+(parseFloat(width) * STROKE_WIDTH_SCALE).toFixed(3)}"`
41 )
42
43 const parseSvg = (markup, name) => {
44 const svgTag = SVG_TAG_REGEX.exec(markup)
45 const viewBox = VIEWBOX_REGEX.exec(markup)
46
47 if (!svgTag || !viewBox) {
48 throw new Error(`Couldn't extract the SVG of the ${name} icon.`)
49 }
50
51 const [, attributes, content] = svgTag
52
53 // IDs aren't scoped per icon, so they'd clash between icons on the same page
54 const body = content
55 .replace(/id="([^"]+)"/g, `id="medusa-${name}-$1"`)
56 .replace(/url\(#([^)]+)\)/g, `url(#medusa-${name}-$1)`)
57
58 // the root SVG's fill is dropped with its tag, so elements relying on it to
59 // inherit `none` would otherwise fall back to a black fill
60 const rootFill = ROOT_FILL_REGEX.exec(attributes)?.[1]
61
62 const [, left, top, width, height] = viewBox
63
64 return {
65 body: rootFill ? `<g fill="${rootFill}">${body}</g>` : body,
66 left: parseFloat(left),
67 top: parseFloat(top),
68 width: parseFloat(width),
69 height: parseFloat(height),
70 }
71 }
72
73 const iconsJson = {
74 prefix: "medusa",
75 icons: {},
76 }
77
78 for (const [name, componentName] of Object.entries(ICON_MAP)) {
79 const component = icons[componentName]
80
81 if (!component) {
82 throw new Error(`Icon ${componentName} isn't exported by @medusajs/icons.`)
83 }
84
85 const icon = parseSvg(renderToStaticMarkup(createElement(component)), name)
86
87 iconsJson.icons[name] = { ...icon, body: lightenStrokes(icon.body) }
88 }
89
90 for (const file of readdirSync(CUSTOM_ICONS_DIR)) {
91 if (extname(file) !== ".svg") {
92 continue
93 }
94
95 const name = basename(file, ".svg")
96
97 iconsJson.icons[name] = parseSvg(
98 readFileSync(join(CUSTOM_ICONS_DIR, file), "utf-8"),
99 name
100 )
101 }
102
103 const sortedIcons = Object.fromEntries(
104 Object.entries(iconsJson.icons).sort(([a], [b]) => a.localeCompare(b))
105 )
106
107 writeFileSync(
108 OUTPUT_PATH,
109 `${JSON.stringify({ ...iconsJson, icons: sortedIcons }, null, 2)}\n`
110 )
111
112 console.log(
113 `Generated ${Object.keys(sortedIcons).length} Mermaid icons at ${OUTPUT_PATH}`
114 )