mirror of https://github.com/iconify/api.git
chore: allow npm fallback if cannot resolve local package
This commit is contained in:
parent
95423f118b
commit
0b9427baa4
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue