From a6ab1dfba85707643ae575bdd39f9ba91c9037e1 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Wed, 8 Feb 2023 09:04:52 +0200 Subject: [PATCH] feat(utils): autoinstall as callback --- packages/utils/src/loader/fs.ts | 5 +++-- packages/utils/src/loader/install-pkg.ts | 6 +++--- packages/utils/src/loader/types.ts | 10 +++++++++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/utils/src/loader/fs.ts b/packages/utils/src/loader/fs.ts index 2b23369..475f28e 100644 --- a/packages/utils/src/loader/fs.ts +++ b/packages/utils/src/loader/fs.ts @@ -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> = {}; const isLegacyExists = isPackageExists('@iconify/json'); export async function loadCollectionFromFS( name: string, - autoInstall = false + autoInstall: AutoInstall = false ): Promise { 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; diff --git a/packages/utils/src/loader/install-pkg.ts b/packages/utils/src/loader/install-pkg.ts index 1cd68b0..0d97595 100644 --- a/packages/utils/src/loader/install-pkg.ts +++ b/packages/utils/src/loader/install-pkg.ts @@ -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 | undefined; @@ -8,7 +9,7 @@ const tasks: Record | undefined> = {}; export async function tryInstallPkg( name: string, - autoInstall: boolean | ((name: string) => Promise) + autoInstall: AutoInstall ): Promise { 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); }) diff --git a/packages/utils/src/loader/types.ts b/packages/utils/src/loader/types.ts index 031c5b3..3e09d9f 100644 --- a/packages/utils/src/loader/types.ts +++ b/packages/utils/src/loader/types.ts @@ -16,6 +16,13 @@ export type UniversalIconLoader = ( */ export type CustomIconLoader = (name: string) => Awaitable; +/** + * Auto-install options + */ +export type AutoInstall = + | boolean + | ((name: string) => Promise); + /** * 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); + autoInstall?: AutoInstall; + /** * The additional icon properties applied to the svg. *