From 5be9c3f8f94906cc33578ee22262e8f3d43ab537 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Tue, 7 Feb 2023 14:50:53 +0800 Subject: [PATCH 1/4] feat: customInstall --- packages/utils/src/loader/fs.ts | 7 +++-- packages/utils/src/loader/install-pkg.ts | 39 ++++++++++++++++-------- packages/utils/src/loader/node-loader.ts | 5 +-- packages/utils/src/loader/types.ts | 1 + 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/packages/utils/src/loader/fs.ts b/packages/utils/src/loader/fs.ts index bd906e1..7a2a338 100644 --- a/packages/utils/src/loader/fs.ts +++ b/packages/utils/src/loader/fs.ts @@ -1,4 +1,5 @@ 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'; @@ -8,7 +9,7 @@ const isLegacyExists = isPackageExists('@iconify/json'); export async function loadCollectionFromFS( name: string, - autoInstall = false + options?: IconifyLoaderOptions ): Promise { if (!(await _collections[name])) { _collections[name] = task(); @@ -21,8 +22,8 @@ export async function loadCollectionFromFS( jsonPath = resolveModule(`@iconify/json/json/${name}.json`); } - if (!jsonPath && !isLegacyExists && autoInstall) { - await tryInstallPkg(`@iconify-json/${name}`); + if (!jsonPath && !isLegacyExists && options?.autoInstall) { + await tryInstallPkg(`@iconify-json/${name}`, options); 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 fa248a8..1dfe547 100644 --- a/packages/utils/src/loader/install-pkg.ts +++ b/packages/utils/src/loader/install-pkg.ts @@ -2,11 +2,15 @@ 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): Promise { +export async function tryInstallPkg( + name: string, + options?: IconifyLoaderOptions +): Promise { if (pending) { await pending; } @@ -14,19 +18,28 @@ export async function tryInstallPkg(name: string): Promise { 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 (options?.customInstall) { + tasks[name] = pending = options + .customInstall(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]; diff --git a/packages/utils/src/loader/node-loader.ts b/packages/utils/src/loader/node-loader.ts index 5ee221a..3809e27 100644 --- a/packages/utils/src/loader/node-loader.ts +++ b/packages/utils/src/loader/node-loader.ts @@ -14,10 +14,7 @@ export const loadNodeIcon: UniversalIconLoader = async ( return result; } - const iconSet = await loadCollectionFromFS( - collection, - options?.autoInstall - ); + const iconSet = await loadCollectionFromFS(collection, options); if (iconSet) { // possible icon names const ids = [ diff --git a/packages/utils/src/loader/types.ts b/packages/utils/src/loader/types.ts index c694300..1757116 100644 --- a/packages/utils/src/loader/types.ts +++ b/packages/utils/src/loader/types.ts @@ -156,6 +156,7 @@ export type IconifyLoaderOptions = { */ autoInstall?: boolean; + customInstall?: Function; /** * The additional icon properties applied to the svg. * From b7fc04c9317c1ff29318dcf962b98ef6de8c203b Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Tue, 7 Feb 2023 14:59:45 +0800 Subject: [PATCH 2/4] perf: supplementary type --- packages/utils/src/loader/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/utils/src/loader/types.ts b/packages/utils/src/loader/types.ts index 1757116..7c8ce9b 100644 --- a/packages/utils/src/loader/types.ts +++ b/packages/utils/src/loader/types.ts @@ -156,7 +156,7 @@ export type IconifyLoaderOptions = { */ autoInstall?: boolean; - customInstall?: Function; + customInstall?: (name: string) => Promise; /** * The additional icon properties applied to the svg. * From 3107db94882b941e03c341cf2e31ccf602dbcca0 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Tue, 7 Feb 2023 23:24:44 +0800 Subject: [PATCH 3/4] 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. * From 27911a54470e09c0ffd71fd3f743129fcca35892 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Tue, 7 Feb 2023 23:35:49 +0800 Subject: [PATCH 4/4] fix: miss --- packages/utils/src/loader/install-pkg.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/utils/src/loader/install-pkg.ts b/packages/utils/src/loader/install-pkg.ts index fa7399a..1cd68b0 100644 --- a/packages/utils/src/loader/install-pkg.ts +++ b/packages/utils/src/loader/install-pkg.ts @@ -8,7 +8,7 @@ const tasks: Record | undefined> = {}; export async function tryInstallPkg( name: string, - autoInstall = true + autoInstall: boolean | ((name: string) => Promise) ): Promise { if (pending) { await pending; @@ -18,8 +18,7 @@ export async function tryInstallPkg( // eslint-disable-next-line no-console console.log(cyan(`Installing ${name}...`)); if (typeof autoInstall === 'function') { - tasks[name] = pending = autoInstall - .customInstall(name) + tasks[name] = pending = autoInstall(name) .then(() => sleep(300)) .finally(() => { pending = undefined;