2021-12-11 20:36:36 +00:00
|
|
|
import { promises as fs } from 'fs';
|
|
|
|
import type { IconifyJSON } from '@iconify/types';
|
2022-01-10 11:17:05 +00:00
|
|
|
import type { FullIconifyIcon } from '../icon';
|
2022-01-10 11:21:34 +00:00
|
|
|
import { iconToSVG } from '../svg/build';
|
|
|
|
import { getIconData } from '../icon-set/get-icon';
|
|
|
|
import { tryInstallPkg } from './utils';
|
2021-12-11 20:36:36 +00:00
|
|
|
import createDebugger from 'debug';
|
|
|
|
import { isPackageExists, resolveModule } from 'local-pkg';
|
2022-01-10 11:17:05 +00:00
|
|
|
import { defaults as DefaultIconCustomizations } from '../customisations';
|
|
|
|
import type { FullIconCustomisations } from '../customisations';
|
2021-12-09 21:12:00 +00:00
|
|
|
|
2022-01-10 11:17:05 +00:00
|
|
|
const debug = createDebugger('@iconify-loader:icon');
|
|
|
|
const debugModern = createDebugger('@iconify-loader:modern');
|
|
|
|
const debugLegacy = createDebugger('@iconify-loader:legacy');
|
2021-12-09 21:12:00 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-12 10:08:44 +00:00
|
|
|
export function searchForIcon(
|
|
|
|
iconSet: IconifyJSON,
|
|
|
|
collection: string,
|
|
|
|
ids: string[],
|
|
|
|
customize?: (defaultCustomizations: FullIconCustomisations) => FullIconCustomisations
|
|
|
|
): string | 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-12 10:08:44 +00:00
|
|
|
const defaultCustomizations = { ...DefaultIconCustomizations }
|
|
|
|
const { attributes, body } = iconToSVG(
|
|
|
|
iconData,
|
|
|
|
typeof customize === 'function' ? customize(defaultCustomizations) : defaultCustomizations
|
|
|
|
);
|
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
|
|
|
}
|