-
Notifications
You must be signed in to change notification settings - Fork 960
feat(desktop): add project icon support with custom protocol #1377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
apps/desktop/src/lib/trpc/routers/projects/utils/favicon-discovery.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { readFile, stat } from "node:fs/promises"; | ||
| import { extname } from "node:path"; | ||
| import fg from "fast-glob"; | ||
| import { | ||
| saveProjectIconFromBuffer, | ||
| saveProjectIconFromFile, | ||
| } from "main/lib/project-icons"; | ||
|
|
||
| /** Common favicon file names to search for in project roots */ | ||
| const FAVICON_PATTERNS = [ | ||
| "favicon.ico", | ||
| "favicon.png", | ||
| "favicon.svg", | ||
| "logo.png", | ||
| "logo.svg", | ||
| "icon.png", | ||
| "icon.svg", | ||
| ".github/logo.png", | ||
| ".github/logo.svg", | ||
| "public/favicon.ico", | ||
| "public/favicon.png", | ||
| "public/favicon.svg", | ||
| "public/logo.png", | ||
| "public/logo.svg", | ||
| "static/favicon.ico", | ||
| "static/favicon.png", | ||
| "static/favicon.svg", | ||
| "assets/favicon.ico", | ||
| "assets/favicon.png", | ||
| "assets/icon.png", | ||
| ]; | ||
|
|
||
| /** Max file size for discovered favicons: 256KB */ | ||
| const MAX_FAVICON_SIZE = 256 * 1024; | ||
|
|
||
| /** | ||
| * Discovers a favicon/icon in the project directory and saves it to disk. | ||
| * Returns the protocol URL if found, or null if no icon was discovered. | ||
| */ | ||
| export async function discoverAndSaveProjectIcon({ | ||
| projectId, | ||
| repoPath, | ||
| }: { | ||
| projectId: string; | ||
| repoPath: string; | ||
| }): Promise<string | null> { | ||
| try { | ||
| const matches = await fg(FAVICON_PATTERNS, { | ||
| cwd: repoPath, | ||
| absolute: true, | ||
| ignore: ["**/node_modules/**", "**/.git/**", "**/dist/**", "**/build/**"], | ||
| onlyFiles: true, | ||
| }); | ||
|
|
||
| if (matches.length === 0) return null; | ||
|
|
||
| // Use the first match (ordered by FAVICON_PATTERNS priority) | ||
| const iconPath = matches[0]; | ||
|
|
||
| // Check file size | ||
| const fileStat = await stat(iconPath); | ||
| if (fileStat.size > MAX_FAVICON_SIZE) { | ||
| console.log( | ||
| `[favicon-discovery] Icon too large (${Math.round(fileStat.size / 1024)}KB): ${iconPath}`, | ||
| ); | ||
| return null; | ||
| } | ||
|
|
||
| const ext = extname(iconPath).replace(".", "") || "png"; | ||
|
|
||
| // For .ico files, read as buffer since they may need special handling | ||
| if (ext === "ico") { | ||
| const buffer = await readFile(iconPath); | ||
| return await saveProjectIconFromBuffer({ | ||
| projectId, | ||
| buffer: Buffer.from(buffer), | ||
| ext: "ico", | ||
| }); | ||
| } | ||
|
|
||
| return await saveProjectIconFromFile({ projectId, sourcePath: iconPath }); | ||
| } catch (error) { | ||
| console.error("[favicon-discovery] Error discovering icon:", error); | ||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
fast-glob pattern order results documentation💡 Result:
In fast-glob (Node.js):
!…) are applied first, and only then the positive patterns—regardless of the order you wrote them in. If you need “pattern A results first, then pattern B”, you must split into multiple calls and combine/sort manually. [3]Sources:
[1] fast-glob README (GitHub) — “results are returned in arbitrary order”
[2] fast-glob package page (npm) — same statement
[3] fast-glob README (GitHub) — “does not respect the order of patterns… negative… then positive… use sorting or split calls”
fast-globdoes not preserve input pattern order in results.The comment on line 57 states results are "ordered by FAVICON_PATTERNS priority," but
fast-globreturns matches in arbitrary filesystem traversal order. This means icons may be selected in unintended priority (e.g.,public/logo.svgbeforefavicon.ico), contradicting the intention of the FAVICON_PATTERNS array.Sort matches by pattern priority after globbing:
Proposed fix
const matches = await fg(FAVICON_PATTERNS, { cwd: repoPath, absolute: true, ignore: ["**/node_modules/**", "**/.git/**", "**/dist/**", "**/build/**"], onlyFiles: true, }); if (matches.length === 0) return null; - // Use the first match (ordered by FAVICON_PATTERNS priority) - const iconPath = matches[0]; + // Sort matches by FAVICON_PATTERNS priority (fast-glob doesn't preserve pattern order) + const relativize = (abs: string) => abs.replace(`${repoPath}/`, "").replace(`${repoPath}\\`, ""); + matches.sort((a, b) => { + const aIdx = FAVICON_PATTERNS.indexOf(relativize(a)); + const bIdx = FAVICON_PATTERNS.indexOf(relativize(b)); + return (aIdx === -1 ? Infinity : aIdx) - (bIdx === -1 ? Infinity : bIdx); + }); + const iconPath = matches[0];🤖 Prompt for AI Agents