mirror of
https://github.com/iconify/iconify.git
synced 2024-10-25 01:52:03 +00:00
72 lines
2.3 KiB
TypeScript
72 lines
2.3 KiB
TypeScript
import { promises as fs } from 'fs';
|
|
import type { IconifyJSON } from '@iconify/types';
|
|
import type { FullIconifyIcon } from '@iconify/utils/lib/icon';
|
|
import { defaultCustomisations as DefaultIconCustomizations, iconToSVG, getIconData, tryInstallPkg } from '@iconify/utils';
|
|
import createDebugger from 'debug';
|
|
import { isPackageExists, resolveModule } from 'local-pkg';
|
|
import type { FullIconCustomisations } from '@iconify/utils/lib/customisations';
|
|
|
|
const debug = createDebugger('@iconify-core:icon');
|
|
const debugModern = createDebugger('@iconify-core:modern');
|
|
const debugLegacy = createDebugger('@iconify-core:legacy');
|
|
|
|
const _collections: Record<string, Promise<IconifyJSON | undefined>> = {};
|
|
const isLegacyExists = isPackageExists('@iconify/json');
|
|
|
|
export async function loadCollection(name: string, autoInstall = false): Promise<IconifyJSON | undefined> {
|
|
if (!_collections[name]) {
|
|
_collections[name] = task();
|
|
}
|
|
|
|
return _collections[name];
|
|
|
|
async function task(): Promise<IconifyJSON | undefined> {
|
|
let jsonPath = resolveModule(`@iconify-json/${name}/icons.json`);
|
|
if (jsonPath) {
|
|
debugModern(name);
|
|
}
|
|
|
|
if (!jsonPath && isLegacyExists) {
|
|
jsonPath = resolveModule(`@iconify/json/json/${name}.json`);
|
|
if (jsonPath) {
|
|
debugLegacy(name);
|
|
}
|
|
}
|
|
|
|
if (!jsonPath && !isLegacyExists && autoInstall) {
|
|
await tryInstallPkg(`@iconify-json/${name}`);
|
|
jsonPath = resolveModule(`@iconify-json/${name}/icons.json`);
|
|
}
|
|
|
|
if (jsonPath) {
|
|
return JSON.parse(await fs.readFile(jsonPath, 'utf8'));
|
|
}
|
|
else {
|
|
debugModern(`failed to load ${name}`);
|
|
return undefined;
|
|
}
|
|
}
|
|
}
|
|
|
|
export function searchForIcon(
|
|
iconSet: IconifyJSON,
|
|
collection: string,
|
|
ids: string[],
|
|
customize?: (defaultCustomizations: FullIconCustomisations) => FullIconCustomisations
|
|
): string | null {
|
|
let iconData: FullIconifyIcon | null;
|
|
for (const id of ids) {
|
|
iconData = getIconData(iconSet, id, true);
|
|
if (iconData) {
|
|
debug(`${collection}:${id}`);
|
|
const defaultCustomizations = { ...DefaultIconCustomizations }
|
|
const { attributes, body } = iconToSVG(
|
|
iconData,
|
|
typeof customize === 'function' ? customize(defaultCustomizations) : defaultCustomizations
|
|
);
|
|
return `<svg ${Object.entries(attributes).map(i => `${i[0]}="${i[1]}"`).join(' ')}>${body}</svg>`;
|
|
}
|
|
}
|
|
return null;
|
|
}
|