mirror of
https://github.com/jcreek/OpenNetworkDiagram.git
synced 2026-07-12 18:43:44 +00:00
163 lines
4.5 KiB
JavaScript
163 lines
4.5 KiB
JavaScript
import { promises as fs } from 'node:fs';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const currentFilePath = fileURLToPath(import.meta.url);
|
|
const currentDirPath = path.dirname(currentFilePath);
|
|
const projectRoot = path.resolve(currentDirPath, '..');
|
|
|
|
const vendorRoot = path.join(projectRoot, 'static/icons/vendor/homarr');
|
|
const outputPath = path.join(projectRoot, 'src/lib/config/vendorIconManifest.ts');
|
|
|
|
const allowedExtensions = new Set(['.svg', '.png', '.webp']);
|
|
const extensionPriority = {
|
|
'.svg': 0,
|
|
'.png': 1,
|
|
'.webp': 2
|
|
};
|
|
|
|
/** @param {string} input */
|
|
function toPosixPath(input) {
|
|
return input.split(path.sep).join('/');
|
|
}
|
|
|
|
/** @param {string} filename */
|
|
function toSlug(filename) {
|
|
return filename
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9._-]/g, '-')
|
|
.replace(/[._]+/g, '-')
|
|
.replace(/-+/g, '-')
|
|
.replace(/^-|-$/g, '');
|
|
}
|
|
|
|
/** @param {string} slug */
|
|
function toLabel(slug) {
|
|
return slug
|
|
.split('-')
|
|
.filter(Boolean)
|
|
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
.join(' ');
|
|
}
|
|
|
|
/** @param {string} dir */
|
|
async function walk(dir) {
|
|
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
const files = entries
|
|
.filter((entry) => entry.isFile())
|
|
.map((entry) => path.join(dir, entry.name));
|
|
const directories = entries
|
|
.filter((entry) => entry.isDirectory())
|
|
.map((entry) => path.join(dir, entry.name));
|
|
const nestedFileLists = await Promise.all(directories.map((childDirectory) => walk(childDirectory)));
|
|
|
|
for (const nestedFiles of nestedFileLists) {
|
|
files.push(...nestedFiles);
|
|
}
|
|
return files;
|
|
}
|
|
|
|
function compareIconRecords(a, b) {
|
|
const labelCompare = a.label.localeCompare(b.label);
|
|
if (labelCompare !== 0) {
|
|
return labelCompare;
|
|
}
|
|
return a.key.localeCompare(b.key);
|
|
}
|
|
|
|
async function main() {
|
|
const vendorExists = await fs
|
|
.access(vendorRoot)
|
|
.then(() => true)
|
|
.catch(() => false);
|
|
|
|
if (!vendorExists) {
|
|
throw new Error(`Vendor icon directory not found: ${vendorRoot}`);
|
|
}
|
|
|
|
const allFiles = await walk(vendorRoot);
|
|
const dedupedBySlug = new Map();
|
|
|
|
for (const absoluteFilePath of allFiles) {
|
|
const extension = path.extname(absoluteFilePath).toLowerCase();
|
|
if (!allowedExtensions.has(extension)) {
|
|
continue;
|
|
}
|
|
|
|
const relativePath = toPosixPath(path.relative(vendorRoot, absoluteFilePath));
|
|
const basename = path.basename(relativePath, extension);
|
|
const slug = toSlug(basename);
|
|
if (!slug) {
|
|
continue;
|
|
}
|
|
|
|
const key = `homarr:${slug}`;
|
|
const candidate = {
|
|
key,
|
|
label: toLabel(slug),
|
|
path: `/icons/vendor/homarr/${relativePath}`,
|
|
source: 'homarr-dashboard-icons',
|
|
extension,
|
|
priority: extensionPriority[extension] ?? 99,
|
|
relativePath
|
|
};
|
|
|
|
const existing = dedupedBySlug.get(slug);
|
|
if (!existing) {
|
|
dedupedBySlug.set(slug, candidate);
|
|
continue;
|
|
}
|
|
|
|
if (candidate.priority < existing.priority) {
|
|
dedupedBySlug.set(slug, candidate);
|
|
continue;
|
|
}
|
|
|
|
if (candidate.priority === existing.priority && candidate.relativePath < existing.relativePath) {
|
|
dedupedBySlug.set(slug, candidate);
|
|
}
|
|
}
|
|
|
|
const records = Array.from(dedupedBySlug.values())
|
|
.sort(compareIconRecords)
|
|
.map(({ key, label, path: iconPath, source }) => ({ key, label, path: iconPath, source }));
|
|
|
|
const header = [
|
|
'/* eslint-disable */',
|
|
'// prettier-ignore',
|
|
'// Auto-generated by scripts/generate-vendor-icon-manifest.mjs',
|
|
'// Do not edit manually.',
|
|
'',
|
|
'export interface VendorIconDefinition {',
|
|
' key: string;',
|
|
' label: string;',
|
|
' path: string;',
|
|
" source: 'homarr-dashboard-icons';",
|
|
'}',
|
|
'',
|
|
'export const VENDOR_ICON_DEFINITIONS: VendorIconDefinition[] = ['
|
|
].join('\n');
|
|
|
|
const body = records
|
|
.map((record) => {
|
|
const key = JSON.stringify(record.key);
|
|
const label = JSON.stringify(record.label);
|
|
const iconPath = JSON.stringify(record.path);
|
|
return ` { key: ${key}, label: ${label}, path: ${iconPath}, source: 'homarr-dashboard-icons' },`;
|
|
})
|
|
.join('\n');
|
|
|
|
const footer = ['];', ''].join('\n');
|
|
const output = `${header}\n${body}\n${footer}`;
|
|
|
|
await fs.mkdir(path.dirname(outputPath), { recursive: true });
|
|
await fs.writeFile(outputPath, output, 'utf8');
|
|
|
|
}
|
|
|
|
main().catch((error) => {
|
|
const message = error instanceof Error ? error.stack ?? error.message : String(error);
|
|
process.stderr.write(`${message}\n`);
|
|
process.exitCode = 1;
|
|
});
|