2
0
mirror of https://github.com/iconify/iconify.git synced 2024-12-12 13:47:49 +00:00

feat(utils): autoinstall as callback

This commit is contained in:
Vjacheslav Trushkin 2023-02-08 09:04:52 +02:00
parent f8f8ce9510
commit a6ab1dfba8
3 changed files with 15 additions and 6 deletions

View File

@ -2,13 +2,14 @@ import { promises as fs, Stats } from 'fs';
import { isPackageExists, resolveModule } from 'local-pkg';
import type { IconifyJSON } from '@iconify/types';
import { tryInstallPkg } from './install-pkg';
import type { AutoInstall } from './types';
const _collections: Record<string, Promise<IconifyJSON | undefined>> = {};
const isLegacyExists = isPackageExists('@iconify/json');
export async function loadCollectionFromFS(
name: string,
autoInstall = false
autoInstall: AutoInstall = false
): Promise<IconifyJSON | undefined> {
if (!(await _collections[name])) {
_collections[name] = task();
@ -32,7 +33,7 @@ export async function loadCollectionFromFS(
} catch (err) {
return undefined;
}
if (stat && stat.isFile()) {
if (stat?.isFile()) {
return JSON.parse(
await fs.readFile(jsonPath as string, 'utf8')
) as IconifyJSON;

View File

@ -1,6 +1,7 @@
import { installPackage } from '@antfu/install-pkg';
import { sleep } from '@antfu/utils';
import { cyan } from 'kolorist';
import type { AutoInstall } from './types';
import { warnOnce } from './warn';
let pending: Promise<void> | undefined;
@ -8,7 +9,7 @@ const tasks: Record<string, Promise<void> | undefined> = {};
export async function tryInstallPkg(
name: string,
autoInstall: boolean | ((name: string) => Promise<void | undefined>)
autoInstall: AutoInstall
): Promise<void | undefined> {
if (pending) {
await pending;
@ -29,8 +30,7 @@ export async function tryInstallPkg(
preferOffline: true,
})
.then(() => sleep(300))
// eslint-disable-next-line
.catch((e: any) => {
.catch((e) => {
warnOnce(`Failed to install ${name}`);
console.error(e);
})

View File

@ -16,6 +16,13 @@ export type UniversalIconLoader = (
*/
export type CustomIconLoader = (name: string) => Awaitable<string | undefined>;
/**
* Auto-install options
*/
export type AutoInstall =
| boolean
| ((name: string) => Promise<void | undefined>);
/**
* Custom icon customizer, it will allow to customize all icons on a collection or individual icons.
*/
@ -154,7 +161,8 @@ export type IconifyLoaderOptions = {
*
* @default false
*/
autoInstall?: boolean | ((name: string) => Promise<void | undefined>);
autoInstall?: AutoInstall;
/**
* The additional icon properties applied to the svg.
*