Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

sync-context-chip-icons.mjs

93 lines3.3 KB

1import * as NodeFS from "node:fs";
2import * as NodeModule from "node:module";
3import * as NodePath from "node:path";
4import * as NodeURL from "node:url";
5​
6const scriptDirectory = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url));
7const appDirectory = NodePath.resolve(scriptDirectory, "..");
8const repositoryRoot = NodePath.resolve(appDirectory, "../..");
9const outputDirectory = NodePath.join(appDirectory, "assets/context-chip-icons");
10const generatedModulePath = NodePath.join(appDirectory, "src/lib/contextChipIcons.generated.ts");
11​
12const requireFromWeb = NodeModule.createRequire(
13 NodePath.join(repositoryRoot, "apps/web/package.json"),
14);
15const requireFromScripts = NodeModule.createRequire(
16 NodePath.join(repositoryRoot, "scripts/package.json"),
17);
18const sharp = requireFromScripts("sharp");
19const lucideIconsDirectory = NodePath.join(
20 NodePath.dirname(requireFromWeb.resolve("lucide-react/package.json")),
21 "dist/esm/icons",
22);
23const webChipSource = NodeFS.readFileSync(
24 NodePath.join(repositoryRoot, "apps/web/src/components/composerInlineChip.ts"),
25 "utf8",
26);
27const skillSvg = webChipSource.match(/SKILL_CHIP_ICON_SVG = `([\s\S]*?)`;/)?.[1];
28​
29if (!skillSvg) {
30 throw new Error(
31 "Could not read SKILL_CHIP_ICON_SVG from apps/web/src/components/composerInlineChip.ts",
32 );
34​
35const lucideIcons = [
36 "circle-dashed",
37 "film",
38 "git-merge",
39 "git-pull-request-arrow",
40 "git-pull-request-closed",
41 "git-pull-request-draft",
42 "image",
43 "message-circle",
44 "messages-square",
45 "mouse-pointer-click",
46 "terminal",
47];
48​
49async function lucideSvg(name) {
50 const module = await import(
51 NodeURL.pathToFileURL(NodePath.join(lucideIconsDirectory, `${name}.js`)).href
52 );
53 const body = module.__iconNode
54 .map(([tag, { key: _key, ...attributes }]) => {
55 const attributeText = Object.entries(attributes)
56 .map(([attribute, value]) => `${attribute}="${value}"`)
57 .join(" ");
58 return `<${tag} ${attributeText}/>`;
59 })
60 .join("");
61 return `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">${body}</svg>`;
63​
64async function renderIcon(token, svg) {
65 const sized = svg
66 .replace(/^<svg[^>]*?>/, (tag) =>
67 tag
68 .replace(/ (?:width|height)="[^"]*"/g, "")
69 .replace("<svg ", '<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" '),
70 )
71 .replaceAll("currentColor", "#ffffff");
72 await sharp(Buffer.from(sized), { density: 72 * (64 / 24) })
73 .resize(64, 64)
74 .png()
75 .toFile(NodePath.join(outputDirectory, `${token}.png`));
77​
78NodeFS.rmSync(outputDirectory, { recursive: true, force: true });
79NodeFS.mkdirSync(outputDirectory, { recursive: true });
80​
81for (const name of lucideIcons) {
82 await renderIcon(name, await lucideSvg(name));
84await renderIcon("skill", skillSvg);
85​
86const tokens = [...lucideIcons, "skill"].sort();
87const generatedSource = `import type { ImageSourcePropType } from "react-native";\n\nexport const CONTEXT_CHIP_ICON_SOURCES = {\n${tokens
88 .map(
89 (token) =>
90 ` ${/^\w+$/.test(token) ? token : `"${token}"`}: require("../../assets/context-chip-icons/${token}.png"),`,
91 )
92 .join("\n")}\n} as const satisfies Readonly<Record<string, ImageSourcePropType>>;\n`;
93NodeFS.writeFileSync(generatedModulePath, generatedSource);