Open History to see the last commit
1import * as NodeChildProcess from "node:child_process";
2import * as NodeFS from "node:fs";
3import * as NodePath from "node:path";
4import * as NodeURL from "node:url";
5
6import { getBuiltInSpriteSheet } from "@pierre/trees";
7
8const scriptDirectory = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url));
9const appDirectory = NodePath.resolve(scriptDirectory, "..");
10const repositoryRoot = NodePath.resolve(appDirectory, "../..");
11const outputDirectory = NodePath.join(appDirectory, "assets/file-icons");
12const generatedModulePath = NodePath.join(appDirectory, "src/lib/markdownFileIcons.generated.ts");
13const webIconSource = NodeFS.readFileSync(
14 NodePath.join(repositoryRoot, "apps/web/src/pierre-icons.ts"),
15 "utf8",
16);
17const customSprite = webIconSource.match(/const T3_FILE_ICON_SPRITE = `([\s\S]*?)`;/)?.[1];
18
19if (!customSprite) {
20 throw new Error("Could not read the T3 Pierre icon sprite from apps/web/src/pierre-icons.ts");
21}
22
23const colors = {
24 astro: "#a631be",
25 babel: "#d5a910",
26 bash: "#199f43",
27 biome: "#1a85d4",
28 bootstrap: "#693acf",
29 browserslist: "#d5a910",
30 bun: "#594c5b",
31 c: "#1a85d4",
32 claude: "#d47628",
33 cpp: "#1a85d4",
34 csharp: "#a631be",
35 css: "#693acf",
36 database: "#a631be",
37 default: "#84848a",
38 docker: "#1a85d4",
39 elixir: "#a631be",
40 eslint: "#693acf",
41 font: "#84848a",
42 git: "#ff8c5b",
43 go: "#1ca1c7",
44 graphql: "#d32a61",
45 html: "#d47628",
46 image: "#d32a61",
47 javascript: "#d5a910",
48 javascript_test: "#d52c36",
49 json: "#d47628",
50 kotlin: "#d32a61",
51 markdown: "#199f43",
52 mcp: "#17a5af",
53 nextjs: "#84848a",
54 npm: "#d52c36",
55 objc: "#d5512f",
56 oxc: "#1ca1c7",
57 pnpm: "#d47628",
58 postcss: "#d52c36",
59 prettier: "#17a5af",
60 python: "#1a85d4",
61 react: "#1ca1c7",
62 rolldown: "#d5512f",
63 ruby: "#d52c36",
64 rust: "#d47628",
65 sass: "#d32a61",
66 stylelint: "#84848a",
67 svelte: "#d52c36",
68 svg: "#d47628",
69 svgo: "#199f43",
70 swift: "#d47628",
71 table: "#17a5af",
72 tailwind: "#1ca1c7",
73 terraform: "#693acf",
74 text: "#84848a",
75 typescript: "#1a85d4",
76 typescript_test: "#d52c36",
77 video: "#a631be",
78 vite: "#a631be",
79 vitest: "#199f43",
80 vscode: "#1a85d4",
81 vue: "#199f43",
82 wasm: "#693acf",
83 webpack: "#1a85d4",
84 yml: "#d52c36",
85 zig: "#d47628",
86 zip: "#d47628",
87};
88
89const customIcons = {
90 agents: "t3-file-icon-agents",
91 csharp: "t3-file-icon-csharp",
92 elixir: "t3-file-icon-elixir",
93 javascript_test: "t3-file-icon-javascript-test",
94 kotlin: "t3-file-icon-kotlin",
95 objc: "t3-file-icon-objc",
96 pnpm: "t3-file-icon-pnpm",
97 rolldown: "t3-file-icon-rolldown",
98 typescript_test: "t3-file-icon-typescript-test",
99 video: "t3-file-icon-video",
100 vitest: "t3-file-icon-vitest",
101};
102
103function symbolFromSprite(sprite, id) {
104 const escapedId = id.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
105 const match = sprite.match(
106 new RegExp(`<symbol id="${escapedId}"([^>]*)>([\\s\\S]*?)<\\/symbol>`),
107 );
108 if (!match) throw new Error(`Missing Pierre icon symbol: ${id}`);
109 return {
110 body: match[2],
111 viewBox: match[1].match(/viewBox="([^"]+)"/)?.[1] ?? "0 0 16 16",
112 };
113}
114
115function renderIcon(token, symbol, color) {
116 const svgPath = NodePath.join(outputDirectory, `.pierre-${token}.svg`);
117 const pngPath = NodePath.join(outputDirectory, `pierre_${token}.png`);
118 NodeFS.writeFileSync(
119 svgPath,
120 `<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="${symbol.viewBox}" style="color:${color}">${symbol.body}</svg>`,
121 );
122 NodeChildProcess.execFileSync("sips", ["-s", "format", "png", svgPath, "--out", pngPath], {
123 stdio: "ignore",
124 });
125 NodeFS.rmSync(svgPath);
126}
127
128NodeFS.rmSync(outputDirectory, { recursive: true, force: true });
129NodeFS.mkdirSync(outputDirectory, { recursive: true });
130
131const builtInSprite = getBuiltInSpriteSheet("complete");
132const builtInTokens = [...builtInSprite.matchAll(/<symbol id="file-tree-builtin-([^"]+)"/g)]
133 .map((match) => match[1])
134 .sort();
135
136for (const token of builtInTokens) {
137 renderIcon(
138 token,
139 symbolFromSprite(builtInSprite, `file-tree-builtin-${token}`),
140 colors[token] ?? colors.default,
141 );
142}
143for (const [token, symbolId] of Object.entries(customIcons)) {
144 renderIcon(token, symbolFromSprite(customSprite, symbolId), colors[token] ?? colors.default);
145}
146
147const tokens = [...new Set([...builtInTokens, ...Object.keys(customIcons)])].sort();
148const generatedSource = `import type { ImageSourcePropType } from "react-native";\n\nexport const MARKDOWN_FILE_ICON_SOURCES = {\n${tokens
149 .map((token) => ` ${token}: require("../../assets/file-icons/pierre_${token}.png"),`)
150 .join("\n")}\n} as const satisfies Readonly<Record<string, ImageSourcePropType>>;\n`;
151NodeFS.writeFileSync(generatedModulePath, generatedSource);