@forge/cli-shared

8.20.08.20.0-experimental-a6c6519
out/apps/template-module.js
+out/apps/template-module.jsNew file
+93
Index: package/out/apps/template-module.js
===================================================================
--- package/out/apps/template-module.js
+++ package/out/apps/template-module.js
@@ -0,0 +1,93 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.TemplateModuleServices = exports.BifrostFetchError = void 0;
+const tslib_1 = require("tslib");
+const fs_1 = tslib_1.__importDefault(require("fs"));
+const path_1 = tslib_1.__importDefault(require("path"));
+const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
+const os_1 = tslib_1.__importDefault(require("os"));
+const url_1 = require("url");
+const text_1 = require("../ui/text");
+const BIFROST_BASE_URL = 'https://forge-templates-bifrost.ddev.frontend.public.atl-paas.net/assets/';
+const MODULE_INDEX_FILE = 'template-modules.json';
+class BifrostFetchError extends Error {
+    constructor(message) {
+        super(message);
+        this.name = 'BifrostFetchError';
+    }
+}
+exports.BifrostFetchError = BifrostFetchError;
+class TemplateModuleServices {
+    baseUrl;
+    templatesCache = null;
+    constructor(baseUrl) {
+        this.baseUrl = baseUrl ?? BIFROST_BASE_URL;
+    }
+    async getAvailableModules(product) {
+        const infos = await this.fetchModuleTemplates();
+        const filtered = infos.filter((i) => i.product === product);
+        return filtered;
+    }
+    async fetchModuleTemplates() {
+        if (this.templatesCache) {
+            return this.templatesCache;
+        }
+        const indexUrl = new url_1.URL(MODULE_INDEX_FILE, this.baseUrl);
+        const res = await (0, node_fetch_1.default)(indexUrl);
+        if (!res.ok) {
+            throw new BifrostFetchError(text_1.Text.module.errors.bifrostIndexFetchFailed(res.status, res.statusText));
+        }
+        const json = await res.json();
+        if (Array.isArray(json)) {
+            this.templatesCache = json;
+            return this.templatesCache;
+        }
+        throw new BifrostFetchError(text_1.Text.module.errors.bifrostIndexInvalid);
+    }
+    async fetchManifestFragment(manifestPath) {
+        const url = `${this.baseUrl}${manifestPath}`;
+        const res = await (0, node_fetch_1.default)(url);
+        if (!res.ok) {
+            throw new BifrostFetchError(text_1.Text.module.errors.bifrostManifestFragmentFetchFailed(res.status, res.statusText));
+        }
+        return await res.text();
+    }
+    async downloadModuleAssets(template, uiFramework) {
+        let manifestFragment;
+        let variantData;
+        if (uiFramework) {
+            variantData = template.variants?.[uiFramework];
+            if (!variantData)
+                throw new Error(text_1.Text.module.add.errorInvalidUIFramework(uiFramework, template.moduleKey));
+        }
+        if (variantData?.manifestPath) {
+            manifestFragment = await this.fetchManifestFragment(variantData.manifestPath);
+        }
+        const cacheDir = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), 'forge-module-'));
+        if (manifestFragment) {
+            fs_1.default.writeFileSync(path_1.default.join(cacheDir, 'manifest-fragment.yml'), manifestFragment);
+        }
+        return {
+            ...template,
+            ...(variantData
+                ? {
+                    uiFramework,
+                    dependencies: variantData.dependencies ?? template.dependencies,
+                    devDependencies: variantData.devDependencies ?? template.devDependencies,
+                    frontend: variantData.frontend ?? template.frontend,
+                    variables: variantData.variables ?? template.variables,
+                    fileOperations: variantData.fileOperations ?? template.fileOperations
+                }
+                : {}),
+            cacheDir
+        };
+    }
+    cleanupCache(cacheDir) {
+        try {
+            fs_1.default.rmSync(cacheDir, { recursive: true, force: true });
+        }
+        catch {
+        }
+    }
+}
+exports.TemplateModuleServices = TemplateModuleServices;