All files / src/shared/lib/plugins pluginHelpers.ts

95.71% Statements 67/70
92.11% Branches 35/38
100% Functions 8/8
95.38% Lines 62/65

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 1382x 2x             2x 2x                         2x   2x 6x     2x 7x 1x   6x 1x   5x 1x   4x 1x   3x 1x   2x 1x   1x     2x 7x 1x   6x 1x   5x 1x   4x 1x   3x 1x   2x 1x   1x     2x       3x 3x 3x 5x 6x   5x                       3x     2x           5x   15x 3x   12x 2x     10x 10x 10x     5x 5x 5x 5x   5x 4x 1x       1x   1x 1x            
import semverGt from "semver/functions/gt";
import { join } from "path";
import type {
  InstalledPluginData,
  PluginMetadata,
  PluginRepositoryMetadata,
  PluginType,
} from "lib/pluginManager/types";
import { assertUnreachable } from "shared/lib/helpers/assert";
import l10n from "shared/lib/lang/l10n";
 
export type PluginItem = {
  id: string;
  name: string;
  plugin: PluginMetadata;
  repo: PluginRepositoryMetadata;
  installedVersion?: string;
  updateAvailable: boolean;
};
 
export type OptionalPluginType = PluginType | "";
 
const globalPlugins: PluginType[] = ["theme", "lang", "template"];
 
export const isGlobalPluginType = (type: PluginType) => {
  return globalPlugins.includes(type);
};
 
export const pluginNameForType = (type: PluginType) => {
  if (type === "assetPack") {
    return l10n("FIELD_ASSET_PACK");
  }
  if (type === "eventsPlugin") {
    return l10n("FIELD_EVENTS_PLUGIN");
  }
  if (type === "enginePlugin") {
    return l10n("FIELD_ENGINE_PLUGIN");
  }
  if (type === "lang") {
    return l10n("FIELD_LANGUAGE_PLUGIN");
  }
  if (type === "template") {
    return l10n("FIELD_TEMPLATE_PLUGIN");
  }
  if (type === "theme") {
    return l10n("MENU_THEME");
  }
  assertUnreachable(type);
};
 
export const pluginDescriptionForType = (type: PluginType) => {
  if (type === "assetPack") {
    return l10n("FIELD_ASSET_PACK_DESC");
  }
  if (type === "eventsPlugin") {
    return l10n("FIELD_EVENTS_PLUGIN_DESC");
  }
  if (type === "enginePlugin") {
    return l10n("FIELD_ENGINE_PLUGIN_DESC");
  }
  if (type === "lang") {
    return l10n("FIELD_LANGUAGE_PLUGIN_DESC");
  }
  if (type === "template") {
    return l10n("FIELD_TEMPLATE_PLUGIN_DESC");
  }
  if (type === "theme") {
    return l10n("FIELD_THEME_PLUGIN_DESC");
  }
  assertUnreachable(type);
};
 
export const buildPluginItems = (
  installedPlugins: InstalledPluginData[],
  repos: PluginRepositoryMetadata[]
): PluginItem[] => {
  const items: PluginItem[] = [];
  for (const repo of repos) {
    for (const plugin of repo.plugins) {
      const installedVersion = installedPlugins.find((p) => {
        return p.path === join(plugin.id, "plugin.json");
      })?.version;
      items.push({
        id: `${repo.id}-${plugin.id}`,
        name: plugin.name,
        installedVersion,
        updateAvailable: installedVersion
          ? semverGt(plugin.version, installedVersion)
          : false,
        plugin,
        repo,
      });
    }
  }
  return items;
};
 
export const filterPluginItems = (
  pluginItems: PluginItem[],
  searchTerm: string,
  typeFilter: OptionalPluginType,
  repoFilter: string
): PluginItem[] => {
  return pluginItems
    .filter((item) => {
      if (typeFilter && item.plugin.type !== typeFilter) {
        return false;
      }
      if (repoFilter && item.repo.id !== repoFilter) {
        return false;
      }
      const searchKey =
        `${item.plugin.filename} ${item.name}`.toLocaleUpperCase();
      const search = searchTerm.toLocaleUpperCase();
      return searchKey.includes(search);
    })
    .sort((a, b) => {
      const isCoreRepoA = a.repo.id === "core";
      const isCoreRepoB = b.repo.id === "core";
      const isCorePluginA = a.plugin.id.startsWith("core/");
      const isCorePluginB = b.plugin.id.startsWith("core/");
 
      if (isCoreRepoA && !isCoreRepoB) {
        return -1;
      } else Iif (!isCoreRepoA && isCoreRepoB) {
        return 1;
      }
 
      Iif (isCorePluginA && !isCorePluginB) {
        return -1;
      } else if (!isCorePluginA && isCorePluginB) {
        return 1;
      }
 
      return a.id.localeCompare(b.id);
    });
};