Open History to see the last commit
1import {
2 createFileTreeIconResolver,
3 getBuiltInSpriteSheet,
4 type FileTreeIcons,
5} from "@pierre/trees";
6import { VIDEO_FILE_EXTENSIONS } from "@t3tools/shared/video";
7
8export interface PierreIconResolution {
9 name: string;
10 token?: string;
11}
12
13const PIERRE_ICON_SPRITE_ID = "t3code-pierre-file-icon-sprite";
14
15const T3_FILE_ICON_SPRITE = `
16<svg xmlns="http://www.w3.org/2000/svg" width="0" height="0" aria-hidden="true">
17 <!-- Lucide Film icon, ISC license. -->
18 <symbol id="t3-file-icon-video" viewBox="0 0 24 24">
19 <g fill="none" stroke="#a631be" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
20 <rect width="18" height="18" x="3" y="3" rx="2" />
21 <path d="M7 3v18M3 7.5h4M3 12h18M3 16.5h4M17 3v18M17 7.5h4M17 16.5h4" />
22 </g>
23 </symbol>
24 <symbol id="t3-file-icon-agents" viewBox="0 0 32 32">
25 <path fill="currentColor" d="M27.2 16c0-6.19-5.01-11.2-11.2-11.2C9.81 4.8 4.8 9.81 4.8 16S9.81 27.2 16 27.2c6.19 0 11.2-5.01 11.2-11.2Zm-5.6 2.1a1.4 1.4 0 1 1 0 2.8h-4.2a1.4 1.4 0 1 1 0-2.8Zm-11.2-6.8c.622-.373 1.42-.208 1.84.361l.079.119 2.1 3.5.088.171c.15.351.15.748 0 1.1l-.088.171-2.1 3.5a1.4 1.4 0 0 1-2.4-1.44L11.59 16l-1.67-2.78-.067-.127c-.302-.642-.075-1.42.547-1.79ZM30 16c0 7.73-6.27 14-14 14S2 23.73 2 16 8.27 2 16 2s14 6.27 14 14Z" />
26 </symbol>
27 <symbol id="t3-file-icon-pnpm" viewBox="0 0 32 32">
28 <path fill="#f9ad00" d="M30 10.75h-8.749V2H30Zm-9.626 0h-8.75V2h8.75Zm-9.625 0H2V2h8.749ZM30 20.375h-8.749v-8.75H30Z" />
29 <path fill="currentColor" d="M20.374 20.375h-8.75v-8.75h8.75Zm0 9.625h-8.75v-8.75h8.75ZM30 30h-8.749v-8.75H30Zm-19.251 0H2v-8.75h8.749Z" />
30 </symbol>
31</svg>`;
32
33export const T3_PIERRE_ICONS = {
34 set: "complete",
35 colored: true,
36 spriteSheet: T3_FILE_ICON_SPRITE,
37 byFileName: {
38 "package.json": "file-tree-builtin-npm",
39 "tsconfig.json": "file-tree-builtin-typescript",
40 "agents.md": "t3-file-icon-agents",
41 "pnpm-lock.yaml": "t3-file-icon-pnpm",
42 "pnpm-workspace.yaml": "t3-file-icon-pnpm",
43 },
44 byFileExtension: Object.fromEntries(
45 VIDEO_FILE_EXTENSIONS.map((extension) => [extension, "t3-file-icon-video"]),
46 ),
47} satisfies FileTreeIcons;
48
49const completeIconResolver = createFileTreeIconResolver(T3_PIERRE_ICONS);
50
51const LANGUAGE_EXTENSION_ALIASES: Record<string, string> = {
52 bash: "sh",
53 csharp: "cs",
54 javascript: "js",
55 jsx: "jsx",
56 markdown: "md",
57 mdx: "mdx",
58 plaintext: "txt",
59 python: "py",
60 ruby: "rb",
61 rust: "rs",
62 shell: "sh",
63 shellscript: "sh",
64 swift: "swift",
65 typescript: "ts",
66 tsx: "tsx",
67 yaml: "yml",
68};
69
70export function basenameOfPath(pathValue: string): string {
71 const slashIndex = pathValue.lastIndexOf("/");
72 return slashIndex === -1 ? pathValue : pathValue.slice(slashIndex + 1);
73}
74
75export function inferEntryKindFromPath(pathValue: string): "file" | "directory" {
76 const base = basenameOfPath(pathValue);
77 if (base.startsWith(".") && !base.slice(1).includes(".")) return "directory";
78 return base.includes(".") ? "file" : "directory";
79}
80
81/** Languages whose files are recognised by name rather than by extension. */
82const LANGUAGE_FILE_NAMES: Record<string, string> = {
83 dockerfile: "Dockerfile",
84};
85
86export function syntheticFileNameForLanguageId(languageId: string): string {
87 const normalized = languageId.toLowerCase();
88 return (
89 LANGUAGE_FILE_NAMES[normalized] ??
90 `file.${LANGUAGE_EXTENSION_ALIASES[normalized] ?? normalized}`
91 );
92}
93
94export function resolvePierreIconForEntry(
95 pathValue: string,
96 kind: "file" | "directory",
97): PierreIconResolution | null {
98 if (kind === "directory") return null;
99 return completeIconResolver.resolveIcon("file-tree-icon-file", pathValue);
100}
101
102export function hasSpecificPierreIconForFileName(fileName: string): boolean {
103 return resolvePierreIconForEntry(fileName, "file")?.token !== "default";
104}
105
106export function ensurePierreIconSprite(): void {
107 if (typeof document === "undefined" || document.getElementById(PIERRE_ICON_SPRITE_ID)) return;
108 const container = document.createElement("div");
109 container.id = PIERRE_ICON_SPRITE_ID;
110 container.setAttribute("aria-hidden", "true");
111 container.style.position = "absolute";
112 container.style.width = "0";
113 container.style.height = "0";
114 container.style.overflow = "hidden";
115 container.style.pointerEvents = "none";
116 container.innerHTML = `${getBuiltInSpriteSheet("complete")}${T3_FILE_ICON_SPRITE}`;
117 document.body.prepend(container);
118}