2
0
mirror of https://github.com/iconify/iconify.git synced 2024-09-19 16:59:02 +00:00

Merge remote-tracking branch 'txp1035/feat_customInstall' into next

This commit is contained in:
Vjacheslav Trushkin 2023-02-08 08:58:22 +02:00
commit f8f8ce9510
3 changed files with 26 additions and 16 deletions

View File

@ -22,7 +22,7 @@ export async function loadCollectionFromFS(
}
if (!jsonPath && !isLegacyExists && autoInstall) {
await tryInstallPkg(`@iconify-json/${name}`);
await tryInstallPkg(`@iconify-json/${name}`, autoInstall);
jsonPath = resolveModule(`@iconify-json/${name}/icons.json`);
}

View File

@ -6,7 +6,10 @@ import { warnOnce } from './warn';
let pending: Promise<void> | undefined;
const tasks: Record<string, Promise<void> | undefined> = {};
export async function tryInstallPkg(name: string): Promise<void | undefined> {
export async function tryInstallPkg(
name: string,
autoInstall: boolean | ((name: string) => Promise<void | undefined>)
): Promise<void | undefined> {
if (pending) {
await pending;
}
@ -14,19 +17,27 @@ export async function tryInstallPkg(name: string): Promise<void | undefined> {
if (!tasks[name]) {
// eslint-disable-next-line no-console
console.log(cyan(`Installing ${name}...`));
tasks[name] = pending = installPackage(name, {
dev: true,
preferOffline: true,
})
.then(() => sleep(300))
// eslint-disable-next-line
.catch((e: any) => {
warnOnce(`Failed to install ${name}`);
console.error(e);
if (typeof autoInstall === 'function') {
tasks[name] = pending = autoInstall(name)
.then(() => sleep(300))
.finally(() => {
pending = undefined;
});
} else {
tasks[name] = pending = installPackage(name, {
dev: true,
preferOffline: true,
})
.finally(() => {
pending = undefined;
});
.then(() => sleep(300))
// eslint-disable-next-line
.catch((e: any) => {
warnOnce(`Failed to install ${name}`);
console.error(e);
})
.finally(() => {
pending = undefined;
});
}
}
return tasks[name];

View File

@ -154,8 +154,7 @@ export type IconifyLoaderOptions = {
*
* @default false
*/
autoInstall?: boolean;
autoInstall?: boolean | ((name: string) => Promise<void | undefined>);
/**
* The additional icon properties applied to the svg.
*