2021-12-11 20:36:36 +00:00
|
|
|
import { promises as fs } from 'fs';
|
|
|
|
import type { IconifyJSON } from '@iconify/types';
|
|
|
|
import type { FullIconifyIcon } from '@iconify/utils';
|
2021-12-09 21:12:00 +00:00
|
|
|
import { defaultCustomisations as DefaultIconCustomizations, iconToSVG, getIconData, tryInstallPkg } from '@iconify/utils';
|
2021-12-11 20:36:36 +00:00
|
|
|
import createDebugger from 'debug';
|
|
|
|
import { isPackageExists, resolveModule } from 'local-pkg';
|
2021-12-09 21:12:00 +00:00
|
|
|
|
2021-12-11 20:36:36 +00:00
|
|
|
const debug = createDebugger('@iconify-core:icon');
|
|
|
|
const debugModern = createDebugger('@iconify-core:modern');
|
|
|
|
const debugLegacy = createDebugger('@iconify-core:legacy');
|
2021-12-09 21:12:00 +00:00
|
|
|
|
|
|
|
export interface ResolvedIconPath {
|
|
|
|
collection: string
|
|
|
|
icon: string
|
|
|
|
query: Record<string, string | undefined>
|
|
|
|
}
|
|
|
|
|
2021-12-11 20:36:36 +00:00
|
|
|
const _collections: Record<string, Promise<IconifyJSON | undefined>> = {};
|
|
|
|
const isLegacyExists = isPackageExists('@iconify/json');
|
2021-12-09 21:12:00 +00:00
|
|
|
|
|
|
|
export async function loadCollection(name: string, autoInstall = false): Promise<IconifyJSON | undefined> {
|
2021-12-11 20:36:36 +00:00
|
|
|
if (!_collections[name]) {
|
|
|
|
_collections[name] = task();
|
|
|
|
}
|
2021-12-09 21:12:00 +00:00
|
|
|
|
2021-12-11 20:36:36 +00:00
|
|
|
return _collections[name];
|
2021-12-09 21:12:00 +00:00
|
|
|
|
|
|
|
async function task(): Promise<IconifyJSON | undefined> {
|
2021-12-11 20:36:36 +00:00
|
|
|
let jsonPath = resolveModule(`@iconify-json/${name}/icons.json`);
|
|
|
|
if (jsonPath) {
|
|
|
|
debugModern(name);
|
|
|
|
}
|
2021-12-09 21:12:00 +00:00
|
|
|
|
|
|
|
if (!jsonPath && isLegacyExists) {
|
2021-12-11 20:36:36 +00:00
|
|
|
jsonPath = resolveModule(`@iconify/json/json/${name}.json`);
|
|
|
|
if (jsonPath) {
|
|
|
|
debugLegacy(name);
|
|
|
|
}
|
2021-12-09 21:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!jsonPath && !isLegacyExists && autoInstall) {
|
2021-12-11 20:36:36 +00:00
|
|
|
await tryInstallPkg(`@iconify-json/${name}`);
|
|
|
|
jsonPath = resolveModule(`@iconify-json/${name}/icons.json`);
|
2021-12-09 21:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (jsonPath) {
|
2021-12-11 20:36:36 +00:00
|
|
|
return JSON.parse(await fs.readFile(jsonPath, 'utf8'));
|
2021-12-09 21:12:00 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-12-11 20:36:36 +00:00
|
|
|
debugModern(`failed to load ${name}`);
|
|
|
|
return undefined;
|
2021-12-09 21:12:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function searchForIcon(iconSet: IconifyJSON, collection: string, ids: string[], scale = 1): FullIconifyIcon | null {
|
2021-12-11 20:36:36 +00:00
|
|
|
let iconData: FullIconifyIcon | null;
|
2021-12-09 21:12:00 +00:00
|
|
|
for (const id of ids) {
|
2021-12-11 20:36:36 +00:00
|
|
|
iconData = getIconData(iconSet, id, true);
|
2021-12-09 21:12:00 +00:00
|
|
|
if (iconData) {
|
2021-12-11 20:36:36 +00:00
|
|
|
debug(`${collection}:${id}`);
|
2021-12-09 21:12:00 +00:00
|
|
|
const { attributes, body } = iconToSVG(iconData, {
|
|
|
|
...DefaultIconCustomizations,
|
|
|
|
height: `${scale}em`,
|
|
|
|
width: `${scale}em`,
|
2021-12-11 20:36:36 +00:00
|
|
|
});
|
|
|
|
return `<svg ${Object.entries(attributes).map(i => `${i[0]}="${i[1]}"`).join(' ')}>${body}</svg>`;
|
2021-12-09 21:12:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-11 20:36:36 +00:00
|
|
|
return null;
|
2021-12-09 21:12:00 +00:00
|
|
|
}
|