Open History to see the last commit
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 );
33}
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 "server",
47 "terminal",
48];
49
50async function lucideSvg(name) {
51 const module = await import(
52 NodeURL.pathToFileURL(NodePath.join(lucideIconsDirectory, `${name}.js`)).href
53 );
54 const body = module.__iconNode
55 .map(([tag, { key: _key, ...attributes }]) => {
56 const attributeText = Object.entries(attributes)
57 .map(([attribute, value]) => `${attribute}="${value}"`)
58 .join(" ");
59 return `<${tag} ${attributeText}/>`;
60 })
61 .join("");
62 return `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">${body}</svg>`;
63}
64
65async function renderIcon(token, svg) {
66 const sized = svg
67 .replace(/^<svg[^>]*?>/, (tag) =>
68 tag
69 .replace(/ (?:width|height)="[^"]*"/g, "")
70 .replace("<svg ", '<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" '),
71 )
72 .replaceAll("currentColor", "#ffffff");
73 await sharp(Buffer.from(sized), { density: 72 * (64 / 24) })
74 .resize(64, 64)
75 .png()
76 .toFile(NodePath.join(outputDirectory, `${token}.png`));
77}
78
79NodeFS.rmSync(outputDirectory, { recursive: true, force: true });
80NodeFS.mkdirSync(outputDirectory, { recursive: true });
81
82for (const name of lucideIcons) {
83 await renderIcon(name, await lucideSvg(name));
84}
85await renderIcon("skill", skillSvg);
86
87const tokens = [...lucideIcons, "skill"].sort();
88const generatedSource = `import type { ImageSourcePropType } from "react-native";\n\nexport const CONTEXT_CHIP_ICON_SOURCES = {\n${tokens
89 .map(
90 (token) =>
91 ` ${/^\w+$/.test(token) ? token : `"${token}"`}: require("../../assets/context-chip-icons/${token}.png"),`,
92 )
93 .join("\n")}\n} as const satisfies Readonly<Record<string, ImageSourcePropType>>;\n`;
94NodeFS.writeFileSync(generatedModulePath, generatedSource);