external-pkg.mjs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { loadCollectionFromFS } from './fs.mjs';
  2. import { searchForIcon } from './modern.mjs';
  3. import { warnOnce } from './warn.mjs';
  4. import { getPossibleIconNames } from './utils.mjs';
  5. import 'fs';
  6. import 'local-pkg';
  7. import './install-pkg.mjs';
  8. import '@antfu/install-pkg';
  9. import '@antfu/utils';
  10. import 'kolorist';
  11. import 'mlly';
  12. import '../svg/build.mjs';
  13. import '../icon/defaults.mjs';
  14. import '../customisations/defaults.mjs';
  15. import '../svg/size.mjs';
  16. import '../svg/defs.mjs';
  17. import '../icon-set/get-icon.mjs';
  18. import '../icon/merge.mjs';
  19. import '../icon/transformations.mjs';
  20. import '../icon-set/tree.mjs';
  21. import 'debug';
  22. function createExternalPackageIconLoader(packageName, autoInstall = false, cwd) {
  23. let scope;
  24. let collection;
  25. const collections = {};
  26. if (typeof packageName === "string") {
  27. if (packageName.length === 0) {
  28. warnOnce(`invalid package name, it is empty`);
  29. return collections;
  30. }
  31. if (packageName[0] === "@") {
  32. if (packageName.indexOf("/") === -1) {
  33. warnOnce(`invalid scoped package name "${packageName}"`);
  34. return collections;
  35. }
  36. [scope, collection] = packageName.split("/");
  37. } else {
  38. scope = "";
  39. collection = packageName;
  40. }
  41. } else {
  42. [scope, collection] = packageName;
  43. }
  44. collections[collection] = createCustomIconLoader(
  45. scope,
  46. collection,
  47. autoInstall,
  48. cwd
  49. );
  50. return collections;
  51. }
  52. function createCustomIconLoader(scope, collection, autoInstall, cwd) {
  53. const iconSetPromise = loadCollectionFromFS(
  54. collection,
  55. autoInstall,
  56. scope,
  57. cwd
  58. );
  59. return async (icon) => {
  60. const iconSet = await iconSetPromise;
  61. let result;
  62. if (iconSet) {
  63. result = await searchForIcon(
  64. iconSet,
  65. collection,
  66. getPossibleIconNames(icon)
  67. );
  68. }
  69. return result;
  70. };
  71. }
  72. export { createExternalPackageIconLoader };