UNPKG

@omnia/tooling-composers

Version:

Provide tooling to work with manifest things.

157 lines (156 loc) • 7.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ComponentRegistry = exports.ComponentRegistration = void 0; const LoadableManifestRegistry_1 = require("./LoadableManifestRegistry"); const ManifestRegistry_1 = require("./ManifestRegistry"); const Utils_1 = require("./Utils"); const Enums_1 = require("./models/Enums"); class ComponentRegistration { constructor(componentOptions, manifestPath, manifest) { this.componentOptions = componentOptions; this.manifestPath = manifestPath; this.manifest = manifest; this.buildOptions = null; this.definition = null; this.extendApiRules = []; this.loadRules = []; } } exports.ComponentRegistration = ComponentRegistration; class ComponentRegistry { constructor() { ///Todo handle method hiding, shoud be only be visible to ManifestRegistry, i.e. as provider this.getClientLoadableManifests = (currentServiceId) => { let manifests = []; for (let key in ComponentRegistry.ComponentRegistrations) { let component = ComponentRegistry.ComponentRegistrations[key]; let componentManifest = { definition: component.definition, elementName: component.componentOptions.elementName, manifestType: Enums_1.ClientManifestTypes.Component, resourceId: component.manifest.resourceId, resourceName: component.manifest.resourceName, omniaServiceId: currentServiceId, dependingOnManifests: LoadableManifestRegistry_1.LoadableManifestRegistry.getManifestDependencies(key, currentServiceId), version: LoadableManifestRegistry_1.LoadableManifestRegistry.getManifestVersion(key), combinedLoadRules: component.loadRules, availableBundleTargetTypes: LoadableManifestRegistry_1.LoadableManifestRegistry.getManifestBundleTypes(key), authDisabled: component.manifest.authDisabled, // api: component.manifest.api, // extendApi: component.manifest.extendApi, // extendApiConfiguration: component.manifest.extendApiConfiguration }; if (component.manifest.api) { componentManifest.api = component.manifest.api; } if (component.manifest.extendApi) { componentManifest.extendApi = component.manifest.extendApi; } if (component.manifest.extendApiConfiguration) { componentManifest.extendApiConfiguration = component.manifest.extendApiConfiguration; } if (component.extendApiRules?.length > 0) { componentManifest.extendApiRules = component.extendApiRules; } manifests.push(componentManifest); } return manifests; }; this.clearState = () => { ComponentRegistry.ComponentElements = {}; ComponentRegistry.ComponentRegistrations = {}; }; } } exports.ComponentRegistry = ComponentRegistry; ComponentRegistry.ComponentRegistrations = {}; ComponentRegistry.ComponentElements = {}; ComponentRegistry.registerComponent = (manifest, componentOptions) => { ComponentRegistry.ensureCorrectManifest(manifest); if (!componentOptions) { throw new Error("Can't register component with missing componentOptions"); } if (!componentOptions.elementName || !componentOptions.entryPoint) { throw new Error("Can't register component with missing mandatory attributes: " + JSON.stringify(componentOptions)); } if (ComponentRegistry.ComponentRegistrations[manifest.resourceId]) { throw new Error("Can't re-register component on manifest with id: " + manifest.resourceId + " current info: " + JSON.stringify(ComponentRegistry.ComponentRegistrations[manifest.resourceId])); } if (ComponentRegistry.ComponentElements[componentOptions.elementName]) { throw new Error("Can't register component: " + JSON.stringify(componentOptions) + " elementName is already registered"); } try { ComponentRegistry.ComponentElements[componentOptions.elementName] = true; ComponentRegistry.ComponentRegistrations[manifest.resourceId] = new ComponentRegistration(componentOptions, ManifestRegistry_1.ManifestRegistry.getCurrentManifestPath(), manifest); } catch (e) { ComponentRegistry.ComponentElements[componentOptions.elementName] = false; ComponentRegistry.ComponentRegistrations[manifest.resourceId] = null; } }; ComponentRegistry.getComponentDefinition = (manifest) => { ComponentRegistry.ensureCorrectManifest(manifest); let registration = ComponentRegistry.ComponentRegistrations[manifest.resourceId]; if (registration != null) { return registration.definition; } return null; }; ComponentRegistry.addComponentDefinition = (manifest, definition) => { ComponentRegistry.ensureCorrectManifest(manifest); let registration = ComponentRegistry.ComponentRegistrations[manifest.resourceId]; if (registration != null) { registration.definition = definition; } else { throw new Error("Can't add ComponentDefinition for component manifest with id: " + manifest.resourceId + " a component has not been registered"); } }; ComponentRegistry.addComponentBuildOptions = (manifest, buildOptions) => { ComponentRegistry.ensureCorrectManifest(manifest); let registration = ComponentRegistry.ComponentRegistrations[manifest.resourceId]; if (registration != null) { registration.buildOptions = buildOptions; } else { throw new Error("Can't add buildOptions for component manifest with id: " + manifest.resourceId + " a component has not been registered"); } }; ComponentRegistry.newCombinedRules = (manifest, rules) => { if (rules) { ComponentRegistry.ensureCorrectManifest(manifest); Utils_1.Utils.validateSupportedLoadRuleTypes(rules); let registration = ComponentRegistry.ComponentRegistrations[manifest.resourceId]; if (registration != null) { registration.loadRules = rules; } else { throw new Error("Can't add load rules for component manifest with id: " + manifest.resourceId + " a component has not been registered"); } } }; ComponentRegistry.getRegistrations = () => { let components = []; for (let key in ComponentRegistry.ComponentRegistrations) { components.push(ComponentRegistry.ComponentRegistrations[key]); } LoadableManifestRegistry_1.LoadableManifestRegistry.registerManifestProvider(new ComponentRegistry()); return components; }; ComponentRegistry.getComponentRegistrations = () => { let components = []; for (let key in ComponentRegistry.ComponentRegistrations) { components.push(ComponentRegistry.ComponentRegistrations[key]); } return components; }; ComponentRegistry.ensureCorrectManifest = (manifest) => { if (!manifest || !manifest.manifestType || manifest.manifestType !== Enums_1.ClientManifestTypes.Component) { throw new Error("Manifest :" + JSON.stringify(manifest) + " can't be used as component manifest. Mandatory properties maybe missing or the manifest is not of component type"); } manifest.resourceId = Utils_1.Utils.ensureValidManifestId(manifest.resourceId); };