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",
"author": "Vjacheslav Trushkin",
"license": "MIT",
"version": "3.1.0-beta.5",
"version": "3.1.0-beta.6",
"publishConfig": {
"access": "public",
"tag": "next"

View File

@ -4,16 +4,37 @@ import { Importer } from '../../types/importers.js';
import { createIconSetsPackageImporter } from '../../importers/full/json.js';
import { ImportedData } from '../../types/importers/common.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
*/
export function createPackageIconSetImporter(packageName = '@iconify/json'): Importer {
// const dir = dirname(import.meta.resolve(`${packageName}/package.json`));
export function createPackageIconSetImporter(
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);
const filename = req.resolve(`${packageName}/package.json`);
const dir = dirname(filename);
return createIconSetsPackageImporter(new DirectoryDownloader<ImportedData>(dir), {});
// Try to download it, update if
const npm: RemoteDownloaderOptions = {
downloadType: 'npm',
package: packageName,
};
return createIconSetsPackageImporter(new RemoteDownloader<ImportedData>(npm, autoUpdateRemotePackage));
}