Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProjectFaviconResolver.ts

129 lines3.8 KB

1import { Effect, FileSystem, Layer, Path } from "effect";
2​
3import {
4 ProjectFaviconResolver,
5 type ProjectFaviconResolverShape,
6} from "../Services/ProjectFaviconResolver.ts";
7​
8// Well-known favicon paths checked in order.
9const FAVICON_CANDIDATES = [
10 "favicon.svg",
11 "favicon.ico",
12 "favicon.png",
13 "public/favicon.svg",
14 "public/favicon.ico",
15 "public/favicon.png",
16 "app/favicon.ico",
17 "app/favicon.png",
18 "app/icon.svg",
19 "app/icon.png",
20 "app/icon.ico",
21 "src/favicon.ico",
22 "src/favicon.svg",
23 "src/app/favicon.ico",
24 "src/app/icon.svg",
25 "src/app/icon.png",
26 "assets/icon.svg",
27 "assets/icon.png",
28 "assets/logo.svg",
29 "assets/logo.png",
30] as const;
31​
32// Files that may contain a <link rel="icon"> or icon metadata declaration.
33const ICON_SOURCE_FILES = [
34 "index.html",
35 "public/index.html",
36 "app/routes/__root.tsx",
37 "src/routes/__root.tsx",
38 "app/root.tsx",
39 "src/root.tsx",
40 "src/index.html",
41] as const;
42​
43// Matches <link ...> tags or object-like icon metadata where rel/href can appear in any order.
44const LINK_ICON_HTML_RE =
45 /<link\b(?=[^>]*\brel=["'](?:icon|shortcut icon)["'])(?=[^>]*\bhref=["']([^"'?]+))[^>]*>/i;
46const LINK_ICON_OBJ_RE =
47 /(?=[^}]*\brel\s*:\s*["'](?:icon|shortcut icon)["'])(?=[^}]*\bhref\s*:\s*["']([^"'?]+))[^}]*/i;
48​
49function extractIconHref(source: string): string | null {
50 const htmlMatch = source.match(LINK_ICON_HTML_RE);
51 if (htmlMatch?.[1]) return htmlMatch[1];
52 const objMatch = source.match(LINK_ICON_OBJ_RE);
53 if (objMatch?.[1]) return objMatch[1];
54 return null;
56​
57export const makeProjectFaviconResolver = Effect.gen(function* () {
58 const fileSystem = yield* FileSystem.FileSystem;
59 const path = yield* Path.Path;
60​
61 const resolveIconHref = (projectCwd: string, href: string): string[] => {
62 const clean = href.replace(/^\//, "");
63 return [path.join(projectCwd, "public", clean), path.join(projectCwd, clean)];
64 };
65​
66 const isPathWithinProject = (projectCwd: string, candidatePath: string): boolean => {
67 const relative = path.relative(path.resolve(projectCwd), path.resolve(candidatePath));
68 return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
69 };
70​
71 const findExistingFile = Effect.fn("ProjectFaviconResolver.findExistingFile")(function* (
72 projectCwd: string,
73 candidates: ReadonlyArray<string>,
74 ): Effect.fn.Return<string | null> {
75 for (const candidate of candidates) {
76 if (!isPathWithinProject(projectCwd, candidate)) {
77 continue;
78 }
79 const stats = yield* fileSystem
80 .stat(candidate)
81 .pipe(Effect.catch(() => Effect.succeed(null)));
82 if (stats?.type === "File") {
83 return candidate;
84 }
85 }
86 return null;
87 });
88​
89 const resolvePath: ProjectFaviconResolverShape["resolvePath"] = Effect.fn(
90 "ProjectFaviconResolver.resolvePath",
91 )(function* (cwd: string): Effect.fn.Return<string | null> {
92 for (const candidate of FAVICON_CANDIDATES) {
93 const resolved = path.join(cwd, candidate);
94 const existing = yield* findExistingFile(cwd, [resolved]);
95 if (existing) {
96 return existing;
97 }
98 }
99​
100 for (const sourceFile of ICON_SOURCE_FILES) {
101 const sourcePath = path.join(cwd, sourceFile);
102 const source = yield* fileSystem
103 .readFileString(sourcePath)
104 .pipe(Effect.catch(() => Effect.succeed(null)));
105 if (!source) {
106 continue;
107 }
108 const href = extractIconHref(source);
109 if (!href) {
110 continue;
111 }
112 const existing = yield* findExistingFile(cwd, resolveIconHref(cwd, href));
113 if (existing) {
114 return existing;
115 }
116 }
117​
118 return null;
119 });
120​
121 return {
122 resolvePath,
123 } satisfies ProjectFaviconResolverShape;
124});
125​
126export const ProjectFaviconResolverLive = Layer.effect(
127 ProjectFaviconResolver,
128 makeProjectFaviconResolver,
129);