All files / src/lib/templates templateManager.ts

19.44% Statements 7/36
0% Branches 0/20
14.28% Functions 1/7
19.44% Lines 7/36

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 951x 1x 1x 1x                 1x                                       1x       1x                                                                                                                    
import { getGlobalPluginsPath } from "lib/pluginManager/globalPlugins";
import { glob } from "lib/helpers/glob";
import { relative, dirname } from "path";
import { readJSON } from "fs-extra";
 
export interface TemplatePlugin {
  id: string;
  name: string;
  preview: string;
  description: string;
}
 
const loadPlugin = async (
  path: string,
): Promise<
  (JSON & { name: string; _resourceType: unknown; description?: string }) | null
> => {
  try {
    const template = await readJSON(path);
    if (!template.name) {
      throw new Error("Template is missing name");
    }
    if (!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 glob("**/project.gbsproj", {
      cwd: globalPluginsPath,
      absolute: true,
    });
    for (const path of pluginPaths) {
      const template = await loadPlugin(path);
      if (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);
    if (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];
    if (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,
      };
    });
  }
}