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 dockerfile: "dockerfile",
55 javascript: "js",
56 jsx: "jsx",
57 markdown: "md",
58 mdx: "mdx",
59 plaintext: "txt",
60 python: "py",
61 ruby: "rb",
62 rust: "rs",
63 shell: "sh",
64 shellscript: "sh",
65 swift: "swift",
66 typescript: "ts",
67 tsx: "tsx",
68 yaml: "yml",
69};
70
71export function basenameOfPath(pathValue: string): string {
72 const slashIndex = pathValue.lastIndexOf("/");
73 return slashIndex === -1 ? pathValue : pathValue.slice(slashIndex + 1);
74}
75
76export function inferEntryKindFromPath(pathValue: string): "file" | "directory" {
77 const base = basenameOfPath(pathValue);
78 if (base.startsWith(".") && !base.slice(1).includes(".")) return "directory";
79 return base.includes(".") ? "file" : "directory";
80}
81
82export function syntheticFileNameForLanguageId(languageId: string): string {
83 const normalized = languageId.toLowerCase();
84 return `file.${LANGUAGE_EXTENSION_ALIASES[normalized] ?? normalized}`;
85}
86
87export function resolvePierreIconForEntry(
88 pathValue: string,
89 kind: "file" | "directory",
90): PierreIconResolution | null {
91 if (kind === "directory") return null;
92 return completeIconResolver.resolveIcon("file-tree-icon-file", pathValue);
93}
94
95export function hasSpecificPierreIconForFileName(fileName: string): boolean {
96 return resolvePierreIconForEntry(fileName, "file")?.token !== "default";
97}
98
99export function ensurePierreIconSprite(): void {
100 if (typeof document === "undefined" || document.getElementById(PIERRE_ICON_SPRITE_ID)) return;
101 const container = document.createElement("div");
102 container.id = PIERRE_ICON_SPRITE_ID;
103 container.setAttribute("aria-hidden", "true");
104 container.style.position = "absolute";
105 container.style.width = "0";
106 container.style.height = "0";
107 container.style.overflow = "hidden";
108 container.style.pointerEvents = "none";
109 container.innerHTML = `${getBuiltInSpriteSheet("complete")}${T3_FILE_ICON_SPRITE}`;
110 document.body.prepend(container);
111}