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

ProjectFaviconResolver.ts

133 lines3.9 KB

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