Skip to content

Commit b48caec

Browse files
committed
core: add automatic project icon discovery from favicon/logo files
1 parent 380c34a commit b48caec

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

.opencode/command/commit.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
description: git commit and push
3+
model: opencode/glm-4.6
34
---
45

56
commit and push

packages/opencode/src/project/project.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,29 @@ export namespace Project {
119119
return existing
120120
}
121121

122-
async function discover(input: Pick<Info, "id" | "worktree">) {}
122+
export async function discover(input: Pick<Info, "id" | "worktree">) {
123+
const glob = new Bun.Glob("**/{favicon,icon,logo}.{ico,png,svg,jpg,jpeg,webp}")
124+
for await (const match of glob.scan({
125+
cwd: input.worktree,
126+
absolute: true,
127+
onlyFiles: true,
128+
followSymlinks: false,
129+
dot: false,
130+
})) {
131+
const file = Bun.file(match)
132+
const buffer = await file.arrayBuffer()
133+
const base64 = Buffer.from(buffer).toString("base64")
134+
const mime = file.type || "image/png"
135+
const url = `data:${mime};base64,${base64}`
136+
await Storage.update<Info>(["project", input.id], (draft) => {
137+
draft.icon = {
138+
url,
139+
color: draft.icon?.color ?? "#000000",
140+
}
141+
})
142+
return
143+
}
144+
}
123145

124146
async function migrateFromGlobal(newProjectID: string, worktree: string) {
125147
const globalProject = await Storage.read<Info>(["project", "global"]).catch(() => undefined)

packages/opencode/test/project/project.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, test } from "bun:test"
22
import { Project } from "../../src/project/project"
33
import { Log } from "../../src/util/log"
4+
import { Storage } from "../../src/storage/storage"
45
import { $ } from "bun"
56
import path from "path"
67
import { tmpdir } from "../fixture/fixture"
@@ -39,3 +40,79 @@ describe("Project.fromDirectory", () => {
3940
expect(fileExists).toBe(true)
4041
})
4142
})
43+
44+
describe("Project.discover", () => {
45+
test("should discover favicon.png in root", async () => {
46+
await using tmp = await tmpdir({ git: true })
47+
const project = await Project.fromDirectory(tmp.path)
48+
49+
const pngData = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
50+
await Bun.write(path.join(tmp.path, "favicon.png"), pngData)
51+
52+
await Project.discover({ id: project.id, worktree: tmp.path })
53+
54+
const updated = await Storage.read<Project.Info>(["project", project.id])
55+
expect(updated.icon).toBeDefined()
56+
expect(updated.icon?.url).toStartWith("data:")
57+
expect(updated.icon?.url).toContain("base64")
58+
expect(updated.icon?.color).toBe("#000000")
59+
})
60+
61+
test("should discover icon.svg in subdirectory", async () => {
62+
await using tmp = await tmpdir({ git: true })
63+
const project = await Project.fromDirectory(tmp.path)
64+
65+
await $`mkdir -p ${path.join(tmp.path, "public")}`.quiet()
66+
await Bun.write(path.join(tmp.path, "public", "icon.svg"), "<svg></svg>")
67+
68+
await Project.discover({ id: project.id, worktree: tmp.path })
69+
70+
const updated = await Storage.read<Project.Info>(["project", project.id])
71+
expect(updated.icon).toBeDefined()
72+
expect(updated.icon?.url).toStartWith("data:")
73+
expect(updated.icon?.url).toContain("base64")
74+
})
75+
76+
test("should discover logo.ico", async () => {
77+
await using tmp = await tmpdir({ git: true })
78+
const project = await Project.fromDirectory(tmp.path)
79+
80+
const icoData = Buffer.from([0x00, 0x00, 0x01, 0x00])
81+
await Bun.write(path.join(tmp.path, "logo.ico"), icoData)
82+
83+
await Project.discover({ id: project.id, worktree: tmp.path })
84+
85+
const updated = await Storage.read<Project.Info>(["project", project.id])
86+
expect(updated.icon).toBeDefined()
87+
expect(updated.icon?.url).toStartWith("data:")
88+
})
89+
90+
test("should not discover non-image files", async () => {
91+
await using tmp = await tmpdir({ git: true })
92+
const project = await Project.fromDirectory(tmp.path)
93+
94+
await Bun.write(path.join(tmp.path, "favicon.txt"), "not an image")
95+
96+
await Project.discover({ id: project.id, worktree: tmp.path })
97+
98+
const updated = await Storage.read<Project.Info>(["project", project.id])
99+
expect(updated.icon).toBeUndefined()
100+
})
101+
102+
test("should preserve existing color when discovering icon", async () => {
103+
await using tmp = await tmpdir({ git: true })
104+
const project = await Project.fromDirectory(tmp.path)
105+
106+
await Storage.update<Project.Info>(["project", project.id], (draft) => {
107+
draft.icon = { url: "", color: "#ff0000" }
108+
})
109+
110+
const pngData = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
111+
await Bun.write(path.join(tmp.path, "favicon.png"), pngData)
112+
113+
await Project.discover({ id: project.id, worktree: tmp.path })
114+
115+
const updated = await Storage.read<Project.Info>(["project", project.id])
116+
expect(updated.icon?.color).toBe("#ff0000")
117+
})
118+
})

0 commit comments

Comments
 (0)