mirror of
https://github.com/iconify/iconify.git
synced 2024-12-12 13:47:49 +00:00
perf: use autoInstall
This commit is contained in:
parent
b7fc04c931
commit
3107db9488
@ -1,5 +1,4 @@
|
|||||||
import { promises as fs, Stats } from 'fs';
|
import { promises as fs, Stats } from 'fs';
|
||||||
import type { IconifyLoaderOptions } from './types';
|
|
||||||
import { isPackageExists, resolveModule } from 'local-pkg';
|
import { isPackageExists, resolveModule } from 'local-pkg';
|
||||||
import type { IconifyJSON } from '@iconify/types';
|
import type { IconifyJSON } from '@iconify/types';
|
||||||
import { tryInstallPkg } from './install-pkg';
|
import { tryInstallPkg } from './install-pkg';
|
||||||
@ -9,7 +8,7 @@ const isLegacyExists = isPackageExists('@iconify/json');
|
|||||||
|
|
||||||
export async function loadCollectionFromFS(
|
export async function loadCollectionFromFS(
|
||||||
name: string,
|
name: string,
|
||||||
options?: IconifyLoaderOptions
|
autoInstall = false
|
||||||
): Promise<IconifyJSON | undefined> {
|
): Promise<IconifyJSON | undefined> {
|
||||||
if (!(await _collections[name])) {
|
if (!(await _collections[name])) {
|
||||||
_collections[name] = task();
|
_collections[name] = task();
|
||||||
@ -22,8 +21,8 @@ export async function loadCollectionFromFS(
|
|||||||
jsonPath = resolveModule(`@iconify/json/json/${name}.json`);
|
jsonPath = resolveModule(`@iconify/json/json/${name}.json`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!jsonPath && !isLegacyExists && options?.autoInstall) {
|
if (!jsonPath && !isLegacyExists && autoInstall) {
|
||||||
await tryInstallPkg(`@iconify-json/${name}`, options);
|
await tryInstallPkg(`@iconify-json/${name}`, autoInstall);
|
||||||
jsonPath = resolveModule(`@iconify-json/${name}/icons.json`);
|
jsonPath = resolveModule(`@iconify-json/${name}/icons.json`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,14 +2,13 @@ import { installPackage } from '@antfu/install-pkg';
|
|||||||
import { sleep } from '@antfu/utils';
|
import { sleep } from '@antfu/utils';
|
||||||
import { cyan } from 'kolorist';
|
import { cyan } from 'kolorist';
|
||||||
import { warnOnce } from './warn';
|
import { warnOnce } from './warn';
|
||||||
import type { IconifyLoaderOptions } from './types';
|
|
||||||
|
|
||||||
let pending: Promise<void> | undefined;
|
let pending: Promise<void> | undefined;
|
||||||
const tasks: Record<string, Promise<void> | undefined> = {};
|
const tasks: Record<string, Promise<void> | undefined> = {};
|
||||||
|
|
||||||
export async function tryInstallPkg(
|
export async function tryInstallPkg(
|
||||||
name: string,
|
name: string,
|
||||||
options?: IconifyLoaderOptions
|
autoInstall = true
|
||||||
): Promise<void | undefined> {
|
): Promise<void | undefined> {
|
||||||
if (pending) {
|
if (pending) {
|
||||||
await pending;
|
await pending;
|
||||||
@ -18,8 +17,8 @@ export async function tryInstallPkg(
|
|||||||
if (!tasks[name]) {
|
if (!tasks[name]) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log(cyan(`Installing ${name}...`));
|
console.log(cyan(`Installing ${name}...`));
|
||||||
if (options?.customInstall) {
|
if (typeof autoInstall === 'function') {
|
||||||
tasks[name] = pending = options
|
tasks[name] = pending = autoInstall
|
||||||
.customInstall(name)
|
.customInstall(name)
|
||||||
.then(() => sleep(300))
|
.then(() => sleep(300))
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
|
@ -14,7 +14,10 @@ export const loadNodeIcon: UniversalIconLoader = async (
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
const iconSet = await loadCollectionFromFS(collection, options);
|
const iconSet = await loadCollectionFromFS(
|
||||||
|
collection,
|
||||||
|
options?.autoInstall
|
||||||
|
);
|
||||||
if (iconSet) {
|
if (iconSet) {
|
||||||
// possible icon names
|
// possible icon names
|
||||||
const ids = [
|
const ids = [
|
||||||
|
@ -154,9 +154,7 @@ export type IconifyLoaderOptions = {
|
|||||||
*
|
*
|
||||||
* @default false
|
* @default false
|
||||||
*/
|
*/
|
||||||
autoInstall?: boolean;
|
autoInstall?: boolean | ((name: string) => Promise<void | undefined>);
|
||||||
|
|
||||||
customInstall?: (name: string) => Promise<void | undefined>;
|
|
||||||
/**
|
/**
|
||||||
* The additional icon properties applied to the svg.
|
* The additional icon properties applied to the svg.
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user