This commit is contained in:
2026-11-12 09:32:23 +01:00
commit 53f1896d05
27 changed files with 6336 additions and 0 deletions

70
lib/icons.js Normal file
View File

@@ -0,0 +1,70 @@
// Finds a real app icon in a project (Android launcher icon, .ico, icon.png, logo).
// Bounded breadth-first walk; results are cached because the list endpoint
// asks for every project.
const fs = require('fs')
const path = require('path')
const { SKIP } = require('./tech')
const EXTRA_SKIP = new Set(['Packages', 'test', 'tests', 'node_modules', 'PackageCache'])
const MAX_DEPTH = 8
const MAX_ENTRIES = 8000
const TTL_MS = 10 * 60 * 1000
const cache = new Map() // projectPath -> { result, at }
function score(rel, fileName) {
const lower = fileName.toLowerCase()
const ext = path.extname(lower)
if (!['.png', '.ico', '.webp', '.jpg', '.jpeg', '.svg'].includes(ext)) return -1
const base = path.basename(lower, ext)
const relL = rel.toLowerCase().replace(/\\/g, '/')
let s = -1
if (relL.includes('mipmap') && base.startsWith('ic_launcher')) {
s = 100
const m = relL.match(/mipmap-(x*)hdpi/)
if (m) s += m[1].length * 2
if (/foreground|background|monochrome/.test(base)) s -= 30
if (base.includes('round')) s -= 1
} else if (ext === '.ico') s = 85
else if (/^(app[_-]?icon|icon|appicon|launcher[_-]?icon|store[_-]?icon|favicon)([_-]?\d+)?$/.test(base) || /^(app[_-]?icon|icon)[_-]/.test(base)) s = 75
else if (/^logo([_-]|$)/.test(base)) s = 50
return s
}
function scan(projectPath) {
const queue = [{ dir: projectPath, depth: 0 }]
let seen = 0
let best = null
let bestScore = -1
while (queue.length) {
const { dir, depth } = queue.shift()
let entries
try { entries = fs.readdirSync(dir, { withFileTypes: true }) } catch { continue }
for (const e of entries) {
if (++seen > MAX_ENTRIES) return best
if (e.isDirectory()) {
if (depth < MAX_DEPTH && !e.name.startsWith('.') && !SKIP.has(e.name) && !EXTRA_SKIP.has(e.name)) {
queue.push({ dir: path.join(dir, e.name), depth: depth + 1 })
}
continue
}
if (!e.isFile()) continue
const s = score(path.relative(projectPath, dir), e.name)
if (s > bestScore) { bestScore = s; best = path.join(dir, e.name) }
}
}
return best
}
// -> absolute path of the icon, or null
function findIcon(projectPath) {
const c = cache.get(projectPath)
if (c && Date.now() - c.at < TTL_MS) return c.result
const result = scan(projectPath)
cache.set(projectPath, { result, at: Date.now() })
return result
}
function forget(projectPath) { cache.delete(projectPath) }
function clear() { cache.clear() }
module.exports = { findIcon, forget, clear }