UNPKG

@omnia/tooling-composers

Version:

Provide tooling to work with manifest things.

308 lines (304 loc) • 16.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LoadableManifestRegistry = exports.StaticClientLoadableManifestProviderProxy = void 0; //import { DomMatchingRule, LoadByUrlMatchingRule, LoadableBundleManifest, BundleIdentity } from './models'; const Enums_1 = require("./models/Enums"); const ManifestRegistry_1 = require("./ManifestRegistry"); const Utils_1 = require("./Utils"); const models_1 = require("../../fx-models"); class StaticClientLoadableManifestProviderProxy { constructor(getClientLoadableManifestsFunc, clearStateFunc) { this.clearState = clearStateFunc; this.getClientLoadableManifests = getClientLoadableManifestsFunc; } } exports.StaticClientLoadableManifestProviderProxy = StaticClientLoadableManifestProviderProxy; class ManifestDependency { constructor(manifestId, manifestPath, externalOmniaServiceId = null) { this.manifestId = manifestId; this.manifestPath = manifestPath; this.externalOmniaServiceId = externalOmniaServiceId; } } class CircularDependencyValidator { constructor(manifestDependencyMap, withNoDependency, defaultDependencies, manifestId, dependency) { this.manifestDependencyMap = manifestDependencyMap; this.withNoDependency = withNoDependency; this.defaultDependencies = defaultDependencies; this.manifestId = manifestId; this.dependency = dependency; this.CurrentDependencyChain = []; this.IsPartOfDefault = false; this.checkCircularDependencies = (dependency) => { if (dependency.externalOmniaServiceId == null) { if (this.IsPartOfDefault && !this.manifestDependencyMap[dependency.manifestId] && !this.withNoDependency[dependency.manifestId]) { let defaultIds = []; for (let def of this.defaultDependencies) { defaultIds.push(def.resourceId); } throw new Error("Cirkular (default) dependency error: " + JSON.stringify(this.withNoDependency) + this.manifestId + "->" + this.CurrentDependencyChain.toString().replace(/,/g, "->") + "->" + dependency.manifestId.toString() + "->Defaults:[" + defaultIds.toString() + "]" + " .E.g. specify withDependency or withNoDependency, for manifest id: " + dependency.manifestId + " to not depend on defaults"); } if (dependency.manifestId.toString() === this.manifestId.toString()) { console.log(this.manifestId, dependency); throw new Error("Cirkular dependency error: " + this.manifestId + "->" + this.CurrentDependencyChain.toString().replace(/,/g, "->")); } if (this.CurrentDependencyChain.indexOf(dependency.manifestId.toString()) > -1) { return; } else { this.CurrentDependencyChain.push(dependency.manifestId); } if (this.manifestDependencyMap[dependency.manifestId]) { for (let dep of this.manifestDependencyMap[dependency.manifestId]) { this.checkCircularDependencies(dep); } } } }; if (!manifestId) { throw new Error("Manifest id can't be null/empty or undefined"); } if (!dependency.manifestId) { throw new Error("Manifest can't depend on manifest with id null/empty or undefined"); } for (let def of defaultDependencies) { if (def.resourceId == manifestId) { this.IsPartOfDefault = true; } } this.checkCircularDependencies(dependency); } } class LoadableManifestRegistry { } exports.LoadableManifestRegistry = LoadableManifestRegistry; LoadableManifestRegistry.ManifestDependencies = {}; LoadableManifestRegistry.WithNoManifestDependencies = {}; LoadableManifestRegistry.ManifestFileTypes = {}; LoadableManifestRegistry.ManifestVersions = {}; LoadableManifestRegistry.ClientManifests = {}; LoadableManifestRegistry.RegisteredManifests = {}; LoadableManifestRegistry.ClientManifestProviders = []; LoadableManifestRegistry.getRegisteredManifest = (manifestId) => { manifestId = Utils_1.Utils.ensureValidManifestId(manifestId); return LoadableManifestRegistry.RegisteredManifests[manifestId]; }; LoadableManifestRegistry.withNoDependency = (manifestId) => { manifestId = Utils_1.Utils.ensureValidManifestId(manifestId); if (LoadableManifestRegistry.ManifestDependencies[manifestId]) { new Error("Can't combine adding dependencies and also set withNoDependency, see manifest path: " + ManifestRegistry_1.ManifestRegistry.getCurrentManifestPath()); } LoadableManifestRegistry.WithNoManifestDependencies[manifestId] = true; }; LoadableManifestRegistry.isDefineWithNoDependency = (manifestId) => { return LoadableManifestRegistry.WithNoManifestDependencies[manifestId] === true; }; LoadableManifestRegistry.addManifestDependency = (manifestId, dependentOnManifestId, dependentManifestOmniaServiceId = null) => { manifestId = Utils_1.Utils.ensureValidManifestId(manifestId); dependentOnManifestId = Utils_1.Utils.ensureValidManifestId(dependentOnManifestId, "Invalid depending manifest id"); if (LoadableManifestRegistry.WithNoManifestDependencies[manifestId] === true) { new Error("Can't combine adding dependencies and also set withNoDependency, see manifest path: " + ManifestRegistry_1.ManifestRegistry.getCurrentManifestPath()); } if (dependentManifestOmniaServiceId != null) { dependentManifestOmniaServiceId = Utils_1.Utils.ensureValidServiceId(dependentManifestOmniaServiceId, "Invalid depending omnia service id"); } let dependency = new ManifestDependency(dependentOnManifestId, ManifestRegistry_1.ManifestRegistry.getCurrentManifestPath(), dependentManifestOmniaServiceId); if (LoadableManifestRegistry.ManifestDependencies[manifestId] == null) { LoadableManifestRegistry.ManifestDependencies[manifestId] = []; } let alreadyAdded = false; for (let currDep of LoadableManifestRegistry.ManifestDependencies[manifestId]) { if (currDep.manifestId.toString() === dependentOnManifestId.toString()) { alreadyAdded = true; break; } } if (!alreadyAdded) { LoadableManifestRegistry.ManifestDependencies[manifestId].push(dependency); } }; LoadableManifestRegistry.getManifestDependencies = (manifestId, currentServiceId) => { manifestId = Utils_1.Utils.ensureValidManifestId(manifestId); currentServiceId = Utils_1.Utils.ensureValidServiceId(currentServiceId); let result = []; if (!LoadableManifestRegistry.WithNoManifestDependencies[manifestId] && !LoadableManifestRegistry.ManifestDependencies[manifestId]) { result = LoadableManifestRegistry.getDefaultDependencies(); for (let def of result) { if (def.resourceId == manifestId) { throw new Error("Cirkular (default) dependency error for manifest id " + manifestId + ". Please specify withDependency or withNoDependency, as current manifest is part of default"); } } } else if (LoadableManifestRegistry.ManifestDependencies[manifestId]) { for (let dep of LoadableManifestRegistry.ManifestDependencies[manifestId]) { result.push({ resourceId: dep.manifestId, omniaServiceId: dep.externalOmniaServiceId != null ? dep.externalOmniaServiceId : currentServiceId }); } } return result; }; //This only seems to be used for detecting changes LoadableManifestRegistry.getAllManifestDependencies = () => { let dependencies = {}; for (let key in LoadableManifestRegistry.ManifestDependencies) { dependencies[key] = LoadableManifestRegistry.ManifestDependencies[key]; if (dependencies[key].length == 0 && LoadableManifestRegistry.WithNoManifestDependencies[key] !== true) { dependencies[key] = LoadableManifestRegistry.getDefaultDependencies(); } } return dependencies; }; LoadableManifestRegistry.addManifestVersion = (manifestId, type, version) => { manifestId = Utils_1.Utils.ensureValidManifestId(manifestId, "Can't add manifest version to manifest id null/empty/undefined."); if (!version) { throw new Error("Can't add manifest version null/empty/undefined to manifest id: " + manifestId); } LoadableManifestRegistry.ManifestVersions[manifestId] = LoadableManifestRegistry.ManifestVersions[manifestId] || {}; LoadableManifestRegistry.ManifestVersions[manifestId][type] = version; }; LoadableManifestRegistry.getManifestVersion = (manifestId) => { manifestId = Utils_1.Utils.ensureValidManifestId(manifestId, "Can't get manifest version for manifest id null/empty/undefined."); let result = LoadableManifestRegistry.ManifestVersions[manifestId]; if (!result) { throw new Error(`Unexpected manifest ${manifestId}, build step has not set manifest version`); } return result; }; LoadableManifestRegistry.addManifestBundleType = (manifestId, type) => { manifestId = Utils_1.Utils.ensureValidManifestId(manifestId, "Can't add manifest targetType to manifest id null/empty/undefined."); if (LoadableManifestRegistry.ManifestFileTypes[manifestId] == null) { LoadableManifestRegistry.ManifestFileTypes[manifestId] = []; } let exists = false; for (let t of LoadableManifestRegistry.ManifestFileTypes[manifestId]) { if (t === type) { exists = true; break; } } if (!exists) { LoadableManifestRegistry.ManifestFileTypes[manifestId].push(type); } }; LoadableManifestRegistry.getManifestBundleTypes = (manifestId) => { manifestId = Utils_1.Utils.ensureValidManifestId(manifestId, "Can't add manifest bundle target types for manifest id null/empty/undefined."); let result = LoadableManifestRegistry.ManifestFileTypes[manifestId]; if (!result || (result && result.length == 0)) { console.log("Manifest BundleType has not been set, using JS for now, will generate error"); result = [Enums_1.BundleTargetTypes.Javascript]; //throw new Error("Unexpected state, no bundle target type's have been added to manifest id:" + manifestId + " during build"); } return result; }; LoadableManifestRegistry.registerManifest = (manifest) => { if (!manifest || !manifest.manifestType || !manifest.resourceId) { throw new Error("Unexpected state, loadable client manifest does not contain all mandatory attributes " + JSON.stringify(manifest)); } manifest.resourceId = Utils_1.Utils.ensureValidManifestId(manifest.resourceId); manifest.resourceName = manifest.resourceName; return LoadableManifestRegistry.RegisteredManifests[manifest.resourceId] = manifest; }; LoadableManifestRegistry.registerManifestProvider = (provider) => { if (!provider) { throw new Error("Unexpected state, loadable client manifest provider can't be null/undefined"); } LoadableManifestRegistry.ClientManifestProviders.push(provider); }; LoadableManifestRegistry.getClientManifests = (currentServiceId) => { if (!currentServiceId) { throw new Error("Missing valid omnia service id"); } currentServiceId = Utils_1.Utils.ensureValidServiceId(currentServiceId); let manifests = []; //Note the order here could be important as the manifest types are ordered like this client side for (let i = LoadableManifestRegistry.ClientManifestProviders.length - 1; i >= 0; i--) { let provider = LoadableManifestRegistry.ClientManifestProviders[i]; let providerManifests = provider.getClientLoadableManifests(currentServiceId); for (let m of providerManifests) { //Check conflict etc LoadableManifestRegistry.addClientManifest(currentServiceId, m); } } LoadableManifestRegistry.validateDependencies(currentServiceId); for (let key in LoadableManifestRegistry.ClientManifests) { manifests.push(LoadableManifestRegistry.ClientManifests[key]); } return manifests; }; LoadableManifestRegistry.clearState = () => { for (let provider of LoadableManifestRegistry.ClientManifestProviders) { provider.clearState(); } LoadableManifestRegistry.ClientManifests = {}; LoadableManifestRegistry.ClientManifestProviders = []; LoadableManifestRegistry.ManifestDependencies = {}; }; LoadableManifestRegistry.validateDependencies = (currentServiceId) => { for (let manifestIdWithDep in LoadableManifestRegistry.ManifestDependencies) { for (let dep of LoadableManifestRegistry.ManifestDependencies[manifestIdWithDep]) { new CircularDependencyValidator(LoadableManifestRegistry.ManifestDependencies, LoadableManifestRegistry.WithNoManifestDependencies, LoadableManifestRegistry.getDefaultDependencies(), manifestIdWithDep, dep); /* This check is prob not needed, as groups is just a package from which individual stuff is loaded //Check groups if (LoadableManifestRegistry.ClientManifests[manifestIdWithDep].manifestType == ClientManifestTypes.ManifestGroup) { let groupManifest = <GroupedBundleManifest>LoadableManifestRegistry.ClientManifests[manifestIdWithDep]; for (let manifestIdInGroup of groupManifest.manifestIdsInGroup) { try { new CircularDependencyValidator(LoadableManifestRegistry.ManifestDependencies, LoadableManifestRegistry.WithNoManifestDependencies, LoadableManifestRegistry.getDefaultDependencies(), manifestIdInGroup, dep); } catch (e) { throw new Error("Group manifest with id: " + manifestIdWithDep + " includes manifest with id: " + manifestIdInGroup + " which causes dependency error: " + e.toString()); } } }*/ if (dep.externalOmniaServiceId != null && dep.externalOmniaServiceId === currentServiceId) { throw new Error("Manifest: " + dep.manifestPath + " has dependency to " + dep.manifestId + " which is marked as external, but the omnia service id matches the current omnia service id: " + currentServiceId + ". Add dependency as none external or insure a correct omnia service id"); } if (dep.externalOmniaServiceId == null && LoadableManifestRegistry.ClientManifests[dep.manifestId] == null) { throw new Error("Manifest: " + dep.manifestPath + " has dependency to " + dep.manifestId + " which could not be found. Mark the dependency as external if it's not part of the current project."); } } } }; LoadableManifestRegistry.addClientManifest = (omniaServiceId, manifest) => { if (!manifest) { throw new Error("Unexpected state, client manifest null cant't be added as client side manifest"); } if (!manifest.resourceId) { throw new Error("Unexpected state, client manifest resourceId cant't be null/empty/undefined"); } if (manifest.omniaServiceId !== omniaServiceId) { throw new Error("Unexpected state, client manifest omnia service id should match current omnia service id:" + omniaServiceId + " manifest: " + JSON.stringify(manifest)); } if (LoadableManifestRegistry.ClientManifests[manifest.resourceId] != null) { throw new Error("Unexpected state, client manifest id's should be unique and added once. Already added: " + JSON.stringify(LoadableManifestRegistry.ClientManifests[manifest.resourceId]) + " trying to add: " + JSON.stringify(manifest)); } LoadableManifestRegistry.ClientManifests[manifest.resourceId] = manifest; }; LoadableManifestRegistry.getDefaultDependencies = () => { return [ { omniaServiceId: 'bb000000-0000-bbbb-0000-0000000000bb', resourceId: models_1.OmniaResourceManifests.FxUx.toString() } ]; }; //Just dummy to register subscriber LoadableManifestRegistry.Subscription = ManifestRegistry_1.ManifestRegistry.registerClientManifestProvider(new ManifestRegistry_1.StaticClientManifestProviderProxy(LoadableManifestRegistry.getClientManifests, LoadableManifestRegistry.clearState));