数通智联化工云平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

41 lines
1.2 KiB

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}`, autoInstall);
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?.isFile()) {
return JSON.parse(
await promises.readFile(jsonPath, "utf8")
);
} else {
return void 0;
}
}
}
export { loadCollectionFromFS };