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