UNPKG

@featurevisor/core

Version:

Core package of Featurevisor for Node.js usage

167 lines 5.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Datasource = void 0; class Datasource { constructor(config, rootDirectoryPath) { this.config = config; this.rootDirectoryPath = rootDirectoryPath; this.adapter = new config.adapter(config, rootDirectoryPath); } // @NOTE: only site generator needs it, find a way to get it out of here later getExtension() { return this.config.parser.extension; } /** * State */ readState(environment) { return this.adapter.readState(environment); } writeState(environment, existingState) { return this.adapter.writeState(environment, existingState); } /** * Revision */ readRevision() { return this.adapter.readRevision(); } writeRevision(revision) { return this.adapter.writeRevision(revision); } /** * Datafile */ readDatafile(options) { return this.adapter.readDatafile(options); } writeDatafile(datafileContent, options) { return this.adapter.writeDatafile(datafileContent, options); } /** * Entity specific methods */ // features listFeatures() { return this.adapter.listEntities("feature"); } featureExists(featureKey) { return this.adapter.entityExists("feature", featureKey); } readFeature(featureKey) { return this.adapter.readEntity("feature", featureKey); } writeFeature(featureKey, feature) { return this.adapter.writeEntity("feature", featureKey, feature); } deleteFeature(featureKey) { return this.adapter.deleteEntity("feature", featureKey); } async getRequiredFeaturesChain(featureKey, chain = new Set()) { chain.add(featureKey); if (!this.adapter.entityExists("feature", featureKey)) { throw new Error(`Feature not found: ${featureKey}`); } const feature = await this.readFeature(featureKey); if (!feature.required) { return chain; } for (const r of feature.required) { const requiredKey = typeof r === "string" ? r : r.key; if (chain.has(requiredKey)) { throw new Error(`Circular dependency detected: ${chain.toString()}`); } await this.getRequiredFeaturesChain(requiredKey, chain); } return chain; } // segments listSegments() { return this.adapter.listEntities("segment"); } segmentExists(segmentKey) { return this.adapter.entityExists("segment", segmentKey); } readSegment(segmentKey) { return this.adapter.readEntity("segment", segmentKey); } writeSegment(segmentKey, segment) { return this.adapter.writeEntity("segment", segmentKey, segment); } deleteSegment(segmentKey) { return this.adapter.deleteEntity("segment", segmentKey); } // attributes listAttributes() { return this.adapter.listEntities("attribute"); } async listFlattenedAttributes() { const attributes = await this.listAttributes(); const result = []; for (const key of attributes) { const attribute = await this.readAttribute(key); result.push(key); if (attribute.type === "object") { // @NOTE: in future, this can be recursive const propertyKeys = Object.keys(attribute.properties || {}); for (const propertyKey of propertyKeys) { result.push(`${key}.${propertyKey}`); } } } return result; } attributeExists(attributeKey) { return this.adapter.entityExists("attribute", attributeKey); } readAttribute(attributeKey) { return this.adapter.readEntity("attribute", attributeKey); } writeAttribute(attributeKey, attribute) { return this.adapter.writeEntity("attribute", attributeKey, attribute); } deleteAttribute(attributeKey) { return this.adapter.deleteEntity("attribute", attributeKey); } // groups listGroups() { return this.adapter.listEntities("group"); } groupExists(groupKey) { return this.adapter.entityExists("group", groupKey); } readGroup(groupKey) { return this.adapter.readEntity("group", groupKey); } writeGroup(groupKey, group) { return this.adapter.writeEntity("group", groupKey, group); } deleteGroup(groupKey) { return this.adapter.deleteEntity("group", groupKey); } // tests listTests() { return this.adapter.listEntities("test"); } readTest(testKey) { return this.adapter.readEntity("test", testKey); } writeTest(testKey, test) { return this.adapter.writeEntity("test", testKey, test); } deleteTest(testKey) { return this.adapter.deleteEntity("test", testKey); } getTestSpecName(testKey) { return `${testKey}.${this.getExtension()}`; } // history listHistoryEntries(entityType, entityKey) { return this.adapter.listHistoryEntries(entityType, entityKey); } readCommit(commitHash, entityType, entityKey) { return this.adapter.readCommit(commitHash, entityType, entityKey); } } exports.Datasource = Datasource; //# sourceMappingURL=datasource.js.map