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

95.05% Statements 96/101
91.3% Branches 42/46
94.74% Functions 18/19
95.51% Lines 85/89

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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 1892x 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             47x   2x     14x 26x   14x     13x 4x 4x 4x 4x 4x 8x             26x       14x 33x 34x       2x       5x 5x 14x 14x       3x   11x      
import semverGt from "semver/functions/gt";
import { join, relative } 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);
    });
};
 
const toUnixPath = (filePath: string) => filePath.replace(/\\/g, "/");
 
export const createPreserveFilesFilter = (
  preserveFiles: string[] | undefined,
) => {
  const caseInsensitiveMatch = (file: string, entry: string) => {
    return file.toLowerCase().includes(entry.toLowerCase());
  };
  const compiled = preserveFiles
    ? preserveFiles.map((entry) => {
        // If entry was a regex use regex matching
        if (entry.startsWith("/") && entry.lastIndexOf("/") > 0) {
          const lastSlash = entry.lastIndexOf("/");
          const pattern = entry.slice(1, lastSlash);
          const flags = entry.slice(lastSlash + 1);
          try {
            const regex = new RegExp(pattern, flags);
            return (file: string) => regex.test(file);
          } catch {
            // Invalid regex, fallback to substring match
            return (file: string) => caseInsensitiveMatch(file, entry);
          }
        }
        // Non-regex use substring match
        return (file: string) => caseInsensitiveMatch(file, entry);
      })
    : [];
 
  return (file: string) => {
    const unixFile = toUnixPath(file);
    return compiled.some((match) => match(unixFile));
  };
};
 
export const createRemoveFilesFilter = (
  preserveFiles: string[] | undefined,
  pluginRoot: string,
) => {
  const preserveFilter = createPreserveFilesFilter(preserveFiles);
  return (file: string) => {
    const relativeFilePath = toUnixPath(relative(pluginRoot, file));
    if (
      relativeFilePath.endsWith(".gbsres") ||
      relativeFilePath.endsWith(".gbsres.bak")
    ) {
      return false;
    }
    return !preserveFilter(relativeFilePath);
  };
};