The best way to control Claude Code, Codex and more from any computer or phone

main
ts 79 lines 3.26 KB
Raw
1 // The native markdown renderer draws a link's icon from a raster image, so each source control
2 // host's mark is rendered from its definition's artwork into a bundled PNG.
3 import * as NodeFS from "node:fs";
4 import * as NodeModule from "node:module";
5 import * as NodePath from "node:path";
6 import * as NodeURL from "node:url";
7
8 import {
9 sourceControlClients,
10 type SourceControlIcon,
11 } from "@t3tools/client-runtime/source-control-clients";
12
13 const appDirectory = NodePath.resolve(
14 NodePath.dirname(NodeURL.fileURLToPath(import.meta.url)),
15 "..",
16 );
17 const outputDirectory = NodePath.join(appDirectory, "assets/link-icons");
18 const generatedModulePath = NodePath.join(appDirectory, "src/lib/markdownLinkIcons.generated.ts");
19 const sharp = NodeModule.createRequire(
20 NodePath.resolve(appDirectory, "../../scripts/package.json"),
21 )("sharp");
22
23 const SIZE = 64;
24
25 function attribute(name: string, value: string | number | undefined): string {
26 return value === undefined ? "" : ` ${name}="${value}"`;
27 }
28
29 // A monochrome mark is tinted by the renderer, which only reads its alpha.
30 function svgMarkup(icon: SourceControlIcon): string {
31 const paint = (value: SourceControlIcon["paths"][number]["fill"]) =>
32 value === undefined
33 ? "none"
34 : typeof value === "string"
35 ? value.replace("currentColor", "#ffffff")
36 : `url(#${value.gradient})`;
37 const gradients = (icon.gradients ?? [])
38 .map(
39 (gradient) =>
40 `<linearGradient id="${gradient.id}" x1="${gradient.x1}" y1="${gradient.y1}" x2="${gradient.x2}" y2="${gradient.y2}" gradientUnits="userSpaceOnUse"${attribute("gradientTransform", gradient.transform)}>${gradient.stops
41 .map(
42 (stop) =>
43 `<stop offset="${stop.offset}" stop-color="${stop.color}"${attribute("stop-opacity", stop.opacity)}/>`,
44 )
45 .join("")}</linearGradient>`,
46 )
47 .join("");
48 const paths = icon.paths
49 .map(
50 (path) =>
51 `<path d="${path.d}" fill="${paint(path.fill)}"${attribute("fill-rule", path.fillRule)}${attribute("fill-opacity", path.fillOpacity)}${attribute("stroke", path.stroke === undefined ? undefined : paint(path.stroke))}${attribute("stroke-width", path.strokeWidth)}/>`,
52 )
53 .join("");
54 return `<svg xmlns="http://www.w3.org/2000/svg" width="${SIZE}" height="${SIZE}" viewBox="${icon.viewBox}" fill="none"><defs>${gradients}</defs>${paths}</svg>`;
55 }
56
57 NodeFS.rmSync(outputDirectory, { recursive: true, force: true });
58 NodeFS.mkdirSync(outputDirectory, { recursive: true });
59
60 const kinds: string[] = [];
61 for (const definition of sourceControlClients.definitions) {
62 if (definition.icon === null) continue;
63 await sharp(Buffer.from(svgMarkup(definition.icon)), { density: 72 * 4 })
64 .resize(SIZE, SIZE, { fit: "contain", background: { r: 0, g: 0, b: 0, alpha: 0 } })
65 .png()
66 .toFile(NodePath.join(outputDirectory, `${definition.kind}.png`));
67 kinds.push(definition.kind);
68 }
69
70 NodeFS.writeFileSync(
71 generatedModulePath,
72 `import type { ImageSourcePropType } from "react-native";\n\nexport const SOURCE_CONTROL_LINK_ICON_SOURCES: Readonly<Record<string, ImageSourcePropType>> = {\n${kinds
73 .toSorted()
74 .map(
75 (kind) =>
76 ` ${/^\w+$/u.test(kind) ? kind : `"${kind}"`}: require("../../assets/link-icons/${kind}.png"),`,
77 )
78 .join("\n")}\n};\n`,
79 );