Urara-Blog/node_modules/.pnpm-store/v3/files/b4/e74f146522eeb4d792c077ce112c0f64d49f6b4217c8cfde552f54473a1386a368adaa2bfc360dc5d0c00f3aa6efcea4b190780c2a2e06e359fa60b9bd15de
2022-08-14 01:14:53 +08:00

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 };