All files / src/lib/templates templateManager.ts

25.64% Statements 10/39
0% Branches 0/15
14.29% Functions 1/7
23.68% Lines 9/38

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 971x 1x 1x 1x 1x   1x                 1x                                       1x       1x                                                                                                                  
import { getGlobalPluginsPath } from "lib/pluginManager/globalPlugins";
import glob from "glob";
import { promisify } from "util";
import { join, relative, dirname } from "path";
import { readJSON } from "fs-extra";
 
const globAsync = promisify(glob);
 
export interface TemplatePlugin {
  id: string;
  name: string;
  preview: string;
  description: string;
}
 
export const loadPlugin = async (
  path: string
): Promise<
  (JSON & { name: string; _resourceType: unknown; description?: string }) | null
> => {
  try {
    const template = await readJSON(path);
    Iif (!template.name) {
      throw new Error("Template is missing name");
    }
    Iif (!template._resourceType || template._resourceType !== "project") {
      throw new Error('Invalid _resourceType in template, should be "project"');
    }
    return template;
  } catch (e) {
    console.error("Unable to load template", e);
  }
  return null;
};
 
export class TemplateManager {
  pluginTemplates: Record<string, TemplatePlugin>;
 
  constructor() {
    this.pluginTemplates = {};
  }
 
  async loadPlugins() {
    this.pluginTemplates = {};
    const globalPluginsPath = getGlobalPluginsPath();
    const pluginPaths = await globAsync(
      join(globalPluginsPath, "**/project.gbsproj")
    );
    for (const path of pluginPaths) {
      const template = await loadPlugin(path);
      Iif (template) {
        const id = relative(globalPluginsPath, path);
        this.pluginTemplates[id] = {
          id,
          name: template.name,
          preview: `gbs://global-plugin/${dirname(id)}/thumbnail.png`,
          description: template.description ?? "",
        };
      }
    }
  }
 
  async loadPlugin(path: string) {
    const globalPluginsPath = getGlobalPluginsPath();
    const template = await loadPlugin(path);
    Iif (template) {
      const id = relative(globalPluginsPath, path);
      this.pluginTemplates[id] = {
        id,
        name: template.name,
        preview: `gbs://global-plugin/${dirname(id)}/thumbnail.png`,
        description: template.description ?? "",
      };
      return this.pluginTemplates[id];
    }
  }
 
  getTemplate(templateId: string) {
    const pluginTemplate = this.pluginTemplates[templateId];
    Iif (pluginTemplate) {
      return pluginTemplate;
    }
    return undefined;
  }
 
  getPluginTemplates(): TemplatePlugin[] {
    return Object.entries(this.pluginTemplates).map(([id, template]) => {
      return {
        id,
        name: template.name,
        preview: `gbs://global-plugin/${dirname(id)}/thumbnail.png`,
        description: template.description,
      };
    });
  }
}