← Files Flows by plugin.spaceARCHIVED FILE
lib/project-icon.mjs
1.5 KB · Nov 30, 2026 · 00:17 UTC
import { readFile, realpath, stat } from 'node:fs/promises';
import { extname, isAbsolute, join, relative, sep } from 'node:path';
import { isProjectIcon } from './project-icon-data.mjs';
const candidates = ['public/favicon.svg', 'public/favicon.ico', 'public/favicon.png', 'app/icon.svg', 'src/app/icon.svg', 'app/favicon.ico', 'src/app/favicon.ico', 'public/apple-touch-icon.png', 'public/icon.png', 'favicon.svg', 'favicon.ico', 'favicon.png'];
const types = { '.svg': 'image/svg+xml', '.ico': 'image/x-icon', '.png': 'image/png' };
export async function projectIcon(root) {
try { root = await realpath(root); } catch { return; }
for (const candidate of candidates) {
try {
const path = await realpath(join(root, candidate));
const scoped = relative(root, path);
if (scoped === '..' || scoped.startsWith(`..${sep}`) || isAbsolute(scoped)) continue;
const info = await stat(path);
if (!info.isFile() || info.size > 64 * 1024) continue;
const data = await readFile(path);
if (!data.length || data.length > 64 * 1024) continue;
const value = `data:${types[extname(candidate)]};base64,${data.toString('base64')}`;
if (isProjectIcon(value)) return value;
} catch { /* Missing icons use the folder fallback. */ }
}
}
export async function applicationWithIcon(application, root) {
if (isProjectIcon(application?.icon)) return application;
const icon = await projectIcon(root);
return { id: application.id, name: application.name, ...(icon ? { icon } : {}) };
}
SHA-256: 690c938318d4827819e5c4e712b2c8b95290bc632ad9566c1af63cc554be546e