mirror of
https://github.com/iconify/iconify.git
synced 2024-12-12 13:47:49 +00:00
feat: customInstall
This commit is contained in:
parent
0dbc5da8b3
commit
5be9c3f8f9
@ -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<IconifyJSON | undefined> {
|
||||
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`);
|
||||
}
|
||||
|
||||
|
@ -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<void> | undefined;
|
||||
const tasks: Record<string, Promise<void> | undefined> = {};
|
||||
|
||||
export async function tryInstallPkg(name: string): Promise<void | undefined> {
|
||||
export async function tryInstallPkg(
|
||||
name: string,
|
||||
options?: IconifyLoaderOptions
|
||||
): Promise<void | undefined> {
|
||||
if (pending) {
|
||||
await pending;
|
||||
}
|
||||
@ -14,19 +18,28 @@ export async function tryInstallPkg(name: string): Promise<void | undefined> {
|
||||
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];
|
||||
|
@ -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 = [
|
||||
|
@ -156,6 +156,7 @@ export type IconifyLoaderOptions = {
|
||||
*/
|
||||
autoInstall?: boolean;
|
||||
|
||||
customInstall?: Function;
|
||||
/**
|
||||
* The additional icon properties applied to the svg.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user