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';
|
2022-01-09 20:05:18 +00:00
|
|
|
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>>,
|
|
|
|
iconCustomizer?: IconCustomizer,
|
|
|
|
): Promise<string> {
|
|
|
|
const props: Record<string, string> = await propsProvider?.() ?? {}
|
|
|
|
await iconCustomizer?.(collection, icon, props)
|
|
|
|
Object.keys(additionalProps).forEach((p) => {
|
|
|
|
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(' ')}`)
|
|
|
|
}
|
|
|
|
|
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
|
2022-01-09 20:05:18 +00:00
|
|
|
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
|
|
|
}
|