@omnia/tooling-composers
Version:
Provide tooling to work with manifest things.
212 lines (211 loc) • 11.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResourceRegistry = void 0;
const LoadableManifestRegistry_1 = require("./LoadableManifestRegistry");
const ManifestRegistry_1 = require("./ManifestRegistry");
const Utils_1 = require("./Utils");
const Enums_1 = require("./models/Enums");
class ResourceRegistration {
constructor(description, manifestPath, manifest) {
this.description = description;
this.manifestPath = manifestPath;
this.manifest = manifest;
this.orderOptions = null;
this.loadRules = [];
this.extendApiRules = [];
this.buildOptions = null;
}
}
class ResourceRegistry {
constructor() {
///Todo handle method hiding, shoud be only be visible to ManifestRegistry, i.e. as provider
this.getClientLoadableManifests = (currentServiceId) => {
currentServiceId = Utils_1.Utils.ensureValidServiceId(currentServiceId, "Can't get client loadable manifests for service id undefined/empty/null");
let manifests = [];
for (let key in ResourceRegistry.ResourceRegistrations) {
let resourceRegistrations = ResourceRegistry.ResourceRegistrations[key];
//No merging of registration info neededanymore we only support adding of rules etc on first registration of manifest
if (resourceRegistrations.length > 0) {
let resourceManifest = {
manifestType: Enums_1.ClientManifestTypes.Resource,
resourceId: key,
resourceName: resourceRegistrations[0].manifest.resourceName,
omniaServiceId: currentServiceId,
combinedLoadRules: resourceRegistrations[0].loadRules,
dependingOnManifests: LoadableManifestRegistry_1.LoadableManifestRegistry.getManifestDependencies(key, currentServiceId),
version: LoadableManifestRegistry_1.LoadableManifestRegistry.getManifestVersion(key),
availableBundleTargetTypes: LoadableManifestRegistry_1.LoadableManifestRegistry.getManifestBundleTypes(key),
authDisabled: resourceRegistrations[0].manifest.authDisabled,
};
if (resourceRegistrations[0].manifest.api) {
resourceManifest.api = resourceRegistrations[0].manifest.api;
}
if (resourceRegistrations[0].manifest.extendApi) {
resourceManifest.extendApi = resourceRegistrations[0].manifest.extendApi;
}
if (resourceRegistrations[0].manifest.extendApiConfiguration) {
resourceManifest.extendApiConfiguration = resourceRegistrations[0].manifest.extendApiConfiguration;
}
if (resourceRegistrations[0].manifest.registerApiConfiguration) {
resourceManifest.registerApiConfiguration = resourceRegistrations[0].manifest.registerApiConfiguration;
}
if (resourceRegistrations[0].extendApiRules && resourceRegistrations[0].extendApiRules.length > 0) {
resourceManifest.extendApiRules = resourceRegistrations[0].extendApiRules;
}
//console.dir(resourceManifest);
manifests.push(resourceManifest);
}
}
return manifests;
};
this.clearState = () => {
ResourceRegistry.ResourceRegistrations = {};
};
}
}
exports.ResourceRegistry = ResourceRegistry;
ResourceRegistry.ResourceRegistrations = {};
ResourceRegistry.registerResource = (manifest, resourceDescription) => {
ResourceRegistry.ensureCorrectManifest(manifest);
if (!resourceDescription) {
throw new Error("Can't register resource with missing resourceDescription");
}
if (!resourceDescription.resourcePaths ||
(resourceDescription.resourcePaths && resourceDescription.resourcePaths.length <= 0)) {
throw new Error("Can't register resources with missing mandatory attributes: " + JSON.stringify(resourceDescription));
}
if (ResourceRegistry.ResourceRegistrations[manifest.resourceId] == null) {
ResourceRegistry.ResourceRegistrations[manifest.resourceId] = new Array();
}
ResourceRegistry.ResourceRegistrations[manifest.resourceId].push(new ResourceRegistration(resourceDescription, ManifestRegistry_1.ManifestRegistry.getCurrentManifestPath(), manifest));
};
ResourceRegistry.addOrdering = (manifest, orderOption) => {
ResourceRegistry.ensureCorrectManifest(manifest);
if (orderOption &&
orderOption.priority) {
let registrationsForBundle = ResourceRegistry.ResourceRegistrations[manifest.resourceId];
if (registrationsForBundle == null) {
throw new Error("Unexpected state, ordering should apply to the last registered resource description");
}
let lastAdded = registrationsForBundle.pop();
if (!lastAdded ||
(lastAdded &&
lastAdded.manifest !== manifest)) {
throw new Error("Unexpected state, ordering should apply to the last registered resource description");
}
lastAdded.orderOptions = orderOption;
let index = -1;
for (let i = 0; i < registrationsForBundle.length; i++) {
if (registrationsForBundle[i].orderOptions == null ||
(registrationsForBundle[i].orderOptions &&
registrationsForBundle[i].orderOptions.priority &&
lastAdded.orderOptions.priority > registrationsForBundle[i].orderOptions.priority)) {
//Found registration without priority or a registration with lower priority
index = i;
break;
}
}
if (index > -1) {
registrationsForBundle.splice(index, 0, lastAdded);
}
else {
//Add last again
registrationsForBundle.push(lastAdded);
}
}
};
ResourceRegistry.addBuildOptions = (manifest, buildOptions) => {
ResourceRegistry.ensureCorrectManifest(manifest);
if (buildOptions) {
let registrationsForBundle = ResourceRegistry.ResourceRegistrations[manifest.resourceId];
if (registrationsForBundle == null) {
throw new Error("Unexpected state, build options should apply be applied to a registered manifest, no registrations found for manifest id:" + manifest.resourceId);
}
if (registrationsForBundle.length > 1) {
throw new Error("Unexpected state, build options should only be applied to first registration, not if manifest is reopened");
}
registrationsForBundle[0].buildOptions = buildOptions;
}
};
ResourceRegistry.newCombinedRules = (manifest, rules) => {
ResourceRegistry.ensureCorrectManifest(manifest);
if (rules) {
Utils_1.Utils.validateSupportedLoadRuleTypes(rules);
let registrationsForBundle = ResourceRegistry.ResourceRegistrations[manifest.resourceId];
if (registrationsForBundle == null) {
throw new Error("Unexpected state, load rule should apply be applied to a registered manifest, no registrations found for manifest id:" + manifest.resourceId);
}
if (registrationsForBundle.length > 1) {
throw new Error("Unexpected state, load rules should only be applied to first registration, not if manifest is reopened");
}
//let lastAdded = registrationsForBundle[registrationsForBundle.length - 1];
//if (!lastAdded ||
// (lastAdded &&
// lastAdded.manifest !== manifest)) {
// throw new Error("Unexpected state, load rules should be added to the last registered resource description");
//}
//lastAdded.loadRules = rules;
registrationsForBundle[0].loadRules = rules;
}
};
ResourceRegistry.addExtendApiRules = (manifest, rules) => {
ResourceRegistry.ensureCorrectManifest(manifest);
if (rules) {
Utils_1.Utils.validateSupportedLoadRuleTypes(rules);
let registrationsForBundle = ResourceRegistry.ResourceRegistrations[manifest.resourceId];
if (registrationsForBundle == null) {
throw new Error("Unexpected state, load rule should apply be applied to a registered manifest, no registrations found for manifest id:" + manifest.resourceId);
}
if (registrationsForBundle.length > 1) {
throw new Error("Unexpected state, load rules should only be applied to first registration, not if manifest is reopened");
}
registrationsForBundle[0].extendApiRules = rules;
}
};
ResourceRegistry.getRegisteredResourceBundles = (pathJoiner) => {
let bundleResources = {};
if (!pathJoiner) {
throw new Error("Missing path joiner method");
}
//Resource manifests should have been ordered on insert
for (let key in ResourceRegistry.ResourceRegistrations) {
let bundleRegistrations = ResourceRegistry.ResourceRegistrations[key];
if (bundleRegistrations &&
bundleRegistrations.length > 0) {
let pathsWithOtions = {
buildOptions: null,
paths: []
};
for (let r of bundleRegistrations) {
if (r.buildOptions) {
pathsWithOtions.buildOptions = r.buildOptions;
}
if (r.manifest.resourceId !== key) {
throw new Error("Unexpected state, sanity check failed, all resources should belong to same bundle, expected:" + key + " found: " + r.manifest.resourceId);
}
//for (let p of r.description.resourcePaths) {
// pathsWithOtions.paths = pathsWithOtions.paths.concat(pathJoiner(r.manifestPath, p));
//}
pathsWithOtions.paths = pathsWithOtions.paths.concat(pathJoiner(r.manifestPath, r.description.resourcePaths));
}
bundleResources[key] = pathsWithOtions;
}
}
LoadableManifestRegistry_1.LoadableManifestRegistry.registerManifestProvider(new ResourceRegistry());
return bundleResources;
};
ResourceRegistry.getResourceRegistrations = () => {
let resources = {};
for (let key in ResourceRegistry.ResourceRegistrations) {
resources[key] = ResourceRegistry.ResourceRegistrations[key];
}
return resources;
};
ResourceRegistry.ensureCorrectManifest = (manifest) => {
if (!manifest ||
!manifest.manifestType ||
manifest.manifestType !== Enums_1.ClientManifestTypes.Resource) {
throw new Error("Manifest :" + JSON.stringify(manifest) + " can't be used as resource manifest. Mandatory properties maybe missing or the manifest is not of resource type");
}
manifest.resourceId = Utils_1.Utils.ensureValidManifestId(manifest.resourceId);
};