chore: allow npm fallback if cannot resolve local package

This commit is contained in:
Vjacheslav Trushkin 2024-01-23 23:46:40 +02:00
parent 95423f118b
commit 0b9427baa4
2 changed files with 29 additions and 8 deletions

View File

@ -3,7 +3,7 @@
"description": "Iconify API", "description": "Iconify API",
"author": "Vjacheslav Trushkin", "author": "Vjacheslav Trushkin",
"license": "MIT", "license": "MIT",
"version": "3.1.0-beta.5", "version": "3.1.0-beta.6",
"publishConfig": { "publishConfig": {
"access": "public", "access": "public",
"tag": "next" "tag": "next"

View File

@ -4,16 +4,37 @@ import { Importer } from '../../types/importers.js';
import { createIconSetsPackageImporter } from '../../importers/full/json.js'; import { createIconSetsPackageImporter } from '../../importers/full/json.js';
import { ImportedData } from '../../types/importers/common.js'; import { ImportedData } from '../../types/importers/common.js';
import { DirectoryDownloader } from '../../downloaders/directory.js'; import { DirectoryDownloader } from '../../downloaders/directory.js';
import type { RemoteDownloaderOptions } from '../../types/downloaders/remote.js';
import { RemoteDownloader } from '../../downloaders/remote.js';
/** /**
* Create importer for package * Create importer for package
*/ */
export function createPackageIconSetImporter(packageName = '@iconify/json'): Importer { export function createPackageIconSetImporter(
// const dir = dirname(import.meta.resolve(`${packageName}/package.json`)); packageName = '@iconify/json',
useRemoteFallback = false,
autoUpdateRemotePackage = false
): Importer {
// Try to locate package
let dir: string | undefined;
try {
const req = createRequire(import.meta.url);
const filename = req.resolve(`${packageName}/package.json`);
dir = filename ? dirname(filename) : undefined;
} catch (err) {
//
}
if (dir) {
return createIconSetsPackageImporter(new DirectoryDownloader<ImportedData>(dir), {});
}
if (!useRemoteFallback) {
throw new Error(`Cannot find package "${packageName}"`);
}
const req = createRequire(import.meta.url); // Try to download it, update if
const filename = req.resolve(`${packageName}/package.json`); const npm: RemoteDownloaderOptions = {
const dir = dirname(filename); downloadType: 'npm',
package: packageName,
return createIconSetsPackageImporter(new DirectoryDownloader<ImportedData>(dir), {}); };
return createIconSetsPackageImporter(new RemoteDownloader<ImportedData>(npm, autoUpdateRemotePackage));
} }