2
0
mirror of https://github.com/iconify/iconify.git synced 2024-10-24 17:41:58 +00:00
iconify/packages/utils/src/loader/utils.ts

67 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-12-11 20:36:36 +00:00
import { installPackage } from '@antfu/install-pkg';
2022-01-10 12:43:35 +00:00
import { Awaitable, sleep } from '@antfu/utils';
import { cyan, yellow } from 'kolorist';
2022-01-10 12:43:35 +00:00
import type { IconCustomizer } from './types';
2021-12-09 21:12:00 +00:00
2021-12-12 14:52:01 +00:00
const warned = new Set<string>();
2021-12-09 21:12:00 +00:00
export function warnOnce(msg: string): void {
2021-12-12 14:52:01 +00:00
if (!warned.has(msg)) {
warned.add(msg);
console.warn(yellow(`[@iconify-loader] ${msg}`));
2021-12-09 21:12:00 +00:00
}
}
2021-12-11 20:36:36 +00:00
let pending: Promise<void> | undefined;
const tasks: Record<string, Promise<void> | undefined> = {};
2021-12-09 21:12:00 +00:00
2022-01-10 12:43:35 +00:00
export async function mergeIconProps(
svg: string,
collection: string,
icon: string,
additionalProps: Record<string, string | undefined>,
propsProvider?: () => Awaitable<Record<string, string>>,
2022-01-10 16:08:40 +00:00
iconCustomizer?: IconCustomizer
2022-01-10 12:43:35 +00:00
): Promise<string> {
2022-01-10 16:08:40 +00:00
const props: Record<string, string> = (await propsProvider?.()) ?? {};
await iconCustomizer?.(collection, icon, props);
2022-01-10 12:43:35 +00:00
Object.keys(additionalProps).forEach((p) => {
2022-01-10 16:08:40 +00:00
const v = additionalProps[p];
if (v !== undefined && v !== null) props[p] = v;
});
const replacement = svg.startsWith('<svg ') ? '<svg ' : '<svg';
return svg.replace(
replacement,
`${replacement}${Object.keys(props)
.map((p) => `${p}="${props[p]}"`)
.join(' ')}`
);
2022-01-10 12:43:35 +00:00
}
2021-12-09 21:12:00 +00:00
export async function tryInstallPkg(name: string): Promise<void | undefined> {
2021-12-11 20:36:36 +00:00
if (pending) {
await pending;
}
2021-12-09 21:12:00 +00:00
if (!tasks[name]) {
// eslint-disable-next-line no-console
console.log(cyan(`Installing ${name}...`));
tasks[name] = pending = installPackage(name, {
dev: true,
preferOffline: true,
})
2021-12-09 21:12:00 +00:00
.then(() => sleep(300))
// eslint-disable-next-line
.catch((e: any) => {
2021-12-12 14:52:01 +00:00
warnOnce(`Failed to install ${name}`);
console.error(e);
2021-12-09 21:12:00 +00:00
})
.finally(() => {
2021-12-12 14:52:01 +00:00
pending = undefined;
2021-12-11 20:36:36 +00:00
});
2021-12-09 21:12:00 +00:00
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2021-12-11 20:36:36 +00:00
return tasks[name]!;
2021-12-09 21:12:00 +00:00
}