mirror of
https://github.com/Sevichecc/Urara-Blog.git
synced 2025-05-02 01:29:31 +08:00
39 lines
1.2 KiB
Text
39 lines
1.2 KiB
Text
import { promises } from 'fs';
|
|
import { isPackageExists, resolveModule } from 'local-pkg';
|
|
import { tryInstallPkg } from './install-pkg.mjs';
|
|
import '@antfu/install-pkg';
|
|
import '@antfu/utils';
|
|
import 'kolorist';
|
|
import './warn.mjs';
|
|
|
|
const _collections = {};
|
|
const isLegacyExists = isPackageExists("@iconify/json");
|
|
async function loadCollectionFromFS(name, autoInstall = false) {
|
|
if (!await _collections[name]) {
|
|
_collections[name] = task();
|
|
}
|
|
return _collections[name];
|
|
async function task() {
|
|
let jsonPath = resolveModule(`@iconify-json/${name}/icons.json`);
|
|
if (!jsonPath && isLegacyExists) {
|
|
jsonPath = resolveModule(`@iconify/json/json/${name}.json`);
|
|
}
|
|
if (!jsonPath && !isLegacyExists && autoInstall) {
|
|
await tryInstallPkg(`@iconify-json/${name}`);
|
|
jsonPath = resolveModule(`@iconify-json/${name}/icons.json`);
|
|
}
|
|
let stat;
|
|
try {
|
|
stat = jsonPath ? await promises.lstat(jsonPath) : void 0;
|
|
} catch (err) {
|
|
return void 0;
|
|
}
|
|
if (stat && stat.isFile()) {
|
|
return JSON.parse(await promises.readFile(jsonPath, "utf8"));
|
|
} else {
|
|
return void 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
export { loadCollectionFromFS };
|