2
0
mirror of https://github.com/iconify/iconify.git synced 2024-09-29 05:29:09 +00:00

feat(utils): allow provide cwd options to node loader

This commit is contained in:
Anthony Fu 2024-02-04 02:36:47 +01:00
parent 0398360de9
commit 1a97b64a10
6 changed files with 993 additions and 1292 deletions

View File

@ -413,7 +413,8 @@
"@iconify/types": "workspace:^", "@iconify/types": "workspace:^",
"debug": "^4.3.4", "debug": "^4.3.4",
"kolorist": "^1.8.0", "kolorist": "^1.8.0",
"local-pkg": "^0.5.0" "local-pkg": "^0.5.0",
"mlly": "^1.5.0"
}, },
"devDependencies": { "devDependencies": {
"@iconify-json/flat-color-icons": "^1.1.6", "@iconify-json/flat-color-icons": "^1.1.6",

View File

@ -12,7 +12,8 @@ import { getPossibleIconNames } from './utils';
*/ */
export function createExternalPackageIconLoader( export function createExternalPackageIconLoader(
packageName: ExternalPkgName, packageName: ExternalPkgName,
autoInstall: AutoInstall = false autoInstall: AutoInstall = false,
cwd?: string
) { ) {
let scope: string; let scope: string;
let collection: string; let collection: string;
@ -39,7 +40,8 @@ export function createExternalPackageIconLoader(
collections[collection] = createCustomIconLoader( collections[collection] = createCustomIconLoader(
scope, scope,
collection, collection,
autoInstall autoInstall,
cwd
); );
return collections; return collections;
@ -48,10 +50,16 @@ export function createExternalPackageIconLoader(
function createCustomIconLoader( function createCustomIconLoader(
scope: string, scope: string,
collection: string, collection: string,
autoInstall: AutoInstall autoInstall: AutoInstall,
cwd?: string
) { ) {
// create the custom collection loader // create the custom collection loader
const iconSetPromise = loadCollectionFromFS(collection, autoInstall, scope); const iconSetPromise = loadCollectionFromFS(
collection,
autoInstall,
scope,
cwd
);
return <CustomIconLoader>(async (icon) => { return <CustomIconLoader>(async (icon) => {
// await until the collection is loaded // await until the collection is loaded
const iconSet = await iconSetPromise; const iconSet = await iconSetPromise;

View File

@ -3,6 +3,7 @@ import { isPackageExists, resolveModule, importModule } 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'; import type { AutoInstall } from './types';
import { resolvePath } from 'mlly';
const _collections: Record<string, Promise<IconifyJSON | undefined>> = {}; const _collections: Record<string, Promise<IconifyJSON | undefined>> = {};
const isLegacyExists = isPackageExists('@iconify/json'); const isLegacyExists = isPackageExists('@iconify/json');
@ -18,7 +19,8 @@ const isLegacyExists = isPackageExists('@iconify/json');
export async function loadCollectionFromFS( export async function loadCollectionFromFS(
name: string, name: string,
autoInstall: AutoInstall = false, autoInstall: AutoInstall = false,
scope = '@iconify-json' scope = '@iconify-json',
cwd = process.cwd()
): Promise<IconifyJSON | undefined> { ): Promise<IconifyJSON | undefined> {
if (!(await _collections[name])) { if (!(await _collections[name])) {
_collections[name] = task(); _collections[name] = task();
@ -27,22 +29,33 @@ export async function loadCollectionFromFS(
async function task() { async function task() {
const packageName = scope.length === 0 ? name : `${scope}/${name}`; const packageName = scope.length === 0 ? name : `${scope}/${name}`;
let jsonPath = resolveModule(`${packageName}/icons.json`); let jsonPath = await resolvePath(`${packageName}/icons.json`, {
url: cwd,
}).catch(() => undefined);
// Legacy support for @iconify/json // Legacy support for @iconify/json
if (scope === '@iconify-json') { if (scope === '@iconify-json') {
if (!jsonPath && isLegacyExists) { if (!jsonPath && isLegacyExists) {
jsonPath = resolveModule(`@iconify/json/json/${name}.json`); jsonPath = await resolvePath(
`@iconify/json/json/${name}.json`,
{
url: cwd,
}
).catch(() => undefined);
} }
// Try to install the package if it doesn't exist // Try to install the package if it doesn't exist
if (!jsonPath && !isLegacyExists && autoInstall) { if (!jsonPath && !isLegacyExists && autoInstall) {
await tryInstallPkg(packageName, autoInstall); await tryInstallPkg(packageName, autoInstall);
jsonPath = resolveModule(`${packageName}/icons.json`); jsonPath = await resolvePath(`${packageName}/icons.json`, {
url: cwd,
}).catch(() => undefined);
} }
} else if (!jsonPath && autoInstall) { } else if (!jsonPath && autoInstall) {
await tryInstallPkg(packageName, autoInstall); await tryInstallPkg(packageName, autoInstall);
jsonPath = resolveModule(`${packageName}/icons.json`); jsonPath = await resolvePath(`${packageName}/icons.json`, {
url: cwd,
}).catch(() => undefined);
} }
// Try to import module if it exists // Try to import module if it exists

View File

@ -17,7 +17,9 @@ export const loadNodeIcon: UniversalIconLoader = async (
const iconSet = await loadCollectionFromFS( const iconSet = await loadCollectionFromFS(
collection, collection,
options?.autoInstall options?.autoInstall,
undefined,
options?.cwd
); );
if (iconSet) { if (iconSet) {
result = await searchForIcon( result = await searchForIcon(

View File

@ -179,4 +179,12 @@ export type IconifyLoaderOptions = {
* If you need that properties just add an empty object here, useful for example when using the `svg` on `CSS`. * If you need that properties just add an empty object here, useful for example when using the `svg` on `CSS`.
*/ */
usedProps?: Record<string, string>; usedProps?: Record<string, string>;
/**
* Current working directory, used to resolve the @iconify-json package.
*
* Only used on `node` environment.
* @default process.cwd()
*/
cwd?: string;
}; };

File diff suppressed because it is too large Load Diff