From 3107db94882b941e03c341cf2e31ccf602dbcca0 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Tue, 7 Feb 2023 23:24:44 +0800 Subject: [PATCH] perf: use autoInstall --- packages/utils/src/loader/fs.ts | 7 +++---- packages/utils/src/loader/install-pkg.ts | 7 +++---- packages/utils/src/loader/node-loader.ts | 5 ++++- packages/utils/src/loader/types.ts | 4 +--- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/packages/utils/src/loader/fs.ts b/packages/utils/src/loader/fs.ts index 7a2a338..2b23369 100644 --- a/packages/utils/src/loader/fs.ts +++ b/packages/utils/src/loader/fs.ts @@ -1,5 +1,4 @@ import { promises as fs, Stats } from 'fs'; -import type { IconifyLoaderOptions } from './types'; import { isPackageExists, resolveModule } from 'local-pkg'; import type { IconifyJSON } from '@iconify/types'; import { tryInstallPkg } from './install-pkg'; @@ -9,7 +8,7 @@ const isLegacyExists = isPackageExists('@iconify/json'); export async function loadCollectionFromFS( name: string, - options?: IconifyLoaderOptions + autoInstall = false ): Promise { if (!(await _collections[name])) { _collections[name] = task(); @@ -22,8 +21,8 @@ export async function loadCollectionFromFS( jsonPath = resolveModule(`@iconify/json/json/${name}.json`); } - if (!jsonPath && !isLegacyExists && options?.autoInstall) { - await tryInstallPkg(`@iconify-json/${name}`, options); + if (!jsonPath && !isLegacyExists && autoInstall) { + await tryInstallPkg(`@iconify-json/${name}`, autoInstall); jsonPath = resolveModule(`@iconify-json/${name}/icons.json`); } diff --git a/packages/utils/src/loader/install-pkg.ts b/packages/utils/src/loader/install-pkg.ts index 1dfe547..fa7399a 100644 --- a/packages/utils/src/loader/install-pkg.ts +++ b/packages/utils/src/loader/install-pkg.ts @@ -2,14 +2,13 @@ import { installPackage } from '@antfu/install-pkg'; import { sleep } from '@antfu/utils'; import { cyan } from 'kolorist'; import { warnOnce } from './warn'; -import type { IconifyLoaderOptions } from './types'; let pending: Promise | undefined; const tasks: Record | undefined> = {}; export async function tryInstallPkg( name: string, - options?: IconifyLoaderOptions + autoInstall = true ): Promise { if (pending) { await pending; @@ -18,8 +17,8 @@ export async function tryInstallPkg( if (!tasks[name]) { // eslint-disable-next-line no-console console.log(cyan(`Installing ${name}...`)); - if (options?.customInstall) { - tasks[name] = pending = options + if (typeof autoInstall === 'function') { + tasks[name] = pending = autoInstall .customInstall(name) .then(() => sleep(300)) .finally(() => { diff --git a/packages/utils/src/loader/node-loader.ts b/packages/utils/src/loader/node-loader.ts index 3809e27..5ee221a 100644 --- a/packages/utils/src/loader/node-loader.ts +++ b/packages/utils/src/loader/node-loader.ts @@ -14,7 +14,10 @@ export const loadNodeIcon: UniversalIconLoader = async ( return result; } - const iconSet = await loadCollectionFromFS(collection, options); + const iconSet = await loadCollectionFromFS( + collection, + options?.autoInstall + ); if (iconSet) { // possible icon names const ids = [ diff --git a/packages/utils/src/loader/types.ts b/packages/utils/src/loader/types.ts index 7c8ce9b..031c5b3 100644 --- a/packages/utils/src/loader/types.ts +++ b/packages/utils/src/loader/types.ts @@ -154,9 +154,7 @@ export type IconifyLoaderOptions = { * * @default false */ - autoInstall?: boolean; - - customInstall?: (name: string) => Promise; + autoInstall?: boolean | ((name: string) => Promise); /** * The additional icon properties applied to the svg. *