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

View File

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

View File

@ -16,6 +16,13 @@ export type UniversalIconLoader = (
*/ */
export type CustomIconLoader = (name: string) => Awaitable<string | undefined>; 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. * 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 * @default false
*/ */
autoInstall?: boolean | ((name: string) => Promise<void | undefined>); autoInstall?: AutoInstall;
/** /**
* The additional icon properties applied to the svg. * The additional icon properties applied to the svg.
* *