All files / src/lib/pluginManager globalPlugins.ts

60% Statements 27/45
0% Branches 0/4
50% Functions 2/4
56.1% Lines 23/41

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 677x 7x 7x 7x 7x 7x   7x 7x 7x 7x 7x   7x   7x   7x 4x 4x 4x     7x 1x 1x 1x     7x                                           7x                              
import { app } from "electron";
import { mkdir } from "fs-extra";
import { join, relative, dirname } from "path";
import glob from "glob";
import rimraf from "rimraf";
import { promisify } from "util";
import { InstalledPluginData } from "./types";
import { readJSON } from "fs-extra";
import { guardAssetWithinProject } from "lib/helpers/assets";
import confirmDeletePlugin from "lib/electron/dialog/confirmDeletePlugin";
import { removeEmptyFoldersBetweenPaths } from "lib/helpers/fs/removeEmptyFoldersBetweenPaths";
import { pathToPosix } from "shared/lib/helpers/path";
 
const rmdir = promisify(rimraf);
 
const globAsync = promisify(glob);
 
export const getGlobalPluginsPath = () => {
  const userDataPath = app.getPath("userData");
  const globalPluginsPath = join(userDataPath, "plugins");
  return globalPluginsPath;
};
 
export const ensureGlobalPluginsPath = async () => {
  const globalPluginsPath = getGlobalPluginsPath();
  await mkdir(globalPluginsPath, { recursive: true });
  return globalPluginsPath;
};
 
export const getPluginsInstalledGlobally = async () => {
  const globalPluginsPath = getGlobalPluginsPath();
  const pluginPaths = await globAsync(
    join(globalPluginsPath, "**/plugin.json")
  );
  const plugins: InstalledPluginData[] = [];
  for (const pluginPath of pluginPaths) {
    try {
      const pluginJSON = await readJSON(pluginPath);
      Iif ("version" in pluginJSON && typeof pluginJSON.version === "string") {
        plugins.push({
          path: pathToPosix(relative(globalPluginsPath, pluginPath)),
          version: pluginJSON.version,
        });
      }
    } catch (e) {
      console.error("Error: Unable to parse " + pluginPath);
    }
  }
  return plugins;
};
 
export const removeGlobalPlugin = async (pluginId: string) => {
  const globalPluginsPath = getGlobalPluginsPath();
  const outputPath = join(globalPluginsPath, pluginId);
  guardAssetWithinProject(outputPath, globalPluginsPath);
 
  const cancel = confirmDeletePlugin(
    pluginId,
    relative(globalPluginsPath, outputPath)
  );
  Iif (cancel) {
    return;
  }
  await rmdir(outputPath);
  await removeEmptyFoldersBetweenPaths(globalPluginsPath, dirname(outputPath));
};