mirror of
https://github.com/lucide-icons/lucide.git
synced 2026-12-02 16:49:06 +00:00
42 lines
1.5 KiB
TypeScript
42 lines
1.5 KiB
TypeScript
import path from 'path';
|
|
import { pathToFileURL } from 'url';
|
|
import { readSvgDirectory, readGroups } from '@lucide/helpers';
|
|
import { type IconMetadata } from '../types.ts';
|
|
import { resolveMetadataExtends } from '../../build-helpers/src/resolveMetadataExtends.ts';
|
|
|
|
interface GetIconMetaDataOptions {
|
|
/** Extendable icons left out of the result, e.g. `icons/` when building `lab/`. */
|
|
baseDirectory?: string;
|
|
}
|
|
|
|
async function importMetaData(iconDirectory: string): Promise<Record<string, IconMetadata>> {
|
|
const iconJsons = await readSvgDirectory(iconDirectory, '.json');
|
|
const metadataEntries = await Promise.all(
|
|
iconJsons.map(async (jsonFile: string) => {
|
|
/** eslint-disable */
|
|
const file = await import(pathToFileURL(path.join(iconDirectory, jsonFile)).href, {
|
|
with: { type: 'json' },
|
|
});
|
|
// Copy, so resolving markers doesn't change the cached module.
|
|
return [path.basename(jsonFile, '.json'), structuredClone(file.default)];
|
|
}),
|
|
);
|
|
|
|
return Object.fromEntries(metadataEntries);
|
|
}
|
|
|
|
async function getIconMetaData(
|
|
iconDirectory: string,
|
|
{ baseDirectory }: GetIconMetaDataOptions = {},
|
|
): Promise<Record<string, IconMetadata>> {
|
|
const metadataMap = await importMetaData(iconDirectory);
|
|
const baseMetadata = baseDirectory ? await importMetaData(baseDirectory) : {};
|
|
|
|
// Resolve extends and group references
|
|
const groups = await readGroups();
|
|
|
|
return await resolveMetadataExtends(metadataMap, { groups, baseMetadata });
|
|
}
|
|
|
|
export default getIconMetaData;
|