@zero-scripts/core
Version:
Framework for creating presets, configurations and extensions
42 lines • 2.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractOptionsContainer = void 0;
require("reflect-metadata");
const graph_1 = require("./graph");
const metadata_1 = require("./metadata");
class AbstractOptionsContainer {
constructor(externalOptions) {
Object.keys(externalOptions).forEach(option => {
const prevMeta = Reflect.getMetadata(metadata_1.METADATA_OPTIONS, this.constructor.prototype, option);
if (prevMeta) {
Reflect.deleteMetadata(metadata_1.METADATA_OPTIONS, this.constructor.prototype, option);
}
Reflect.defineMetadata(metadata_1.METADATA_OPTIONS, Object.assign(Object.assign({}, (prevMeta ? prevMeta : {})), { externalValue: externalOptions[option] }), this.constructor.prototype, option);
});
}
build() {
const properties = Object.getOwnPropertyNames(this);
const optionsMeta = properties.map(optionKey => {
if (!Reflect.hasMetadata(metadata_1.METADATA_OPTIONS, this.constructor.prototype, optionKey)) {
throw new Error(`Must need to use Option decorator on your ${this.constructor.name} properties`);
}
return Object.assign(Object.assign({}, Reflect.getMetadata(metadata_1.METADATA_OPTIONS, this.constructor.prototype, optionKey)), { optionKey });
});
const { instance: rootNode } = Reflect.getMetadata(metadata_1.METADATA_ROOT_DEPENDENCY_NODE, this.constructor.prototype);
const resolvedOptions = graph_1.resolve(rootNode)
.filter(node => node.id !== 'root')
.map(node => optionsMeta.find(meta => meta.optionKey === node.id));
const builtOptions = resolvedOptions.reduce((result, { optionKey, getOptionValue, externalValue }) => (Object.assign(Object.assign({}, result), { [optionKey]: getOptionValue
? getOptionValue(result, externalValue)
: this[optionKey] })), {});
// apply post modifier
optionsMeta.forEach(({ optionKey, postModifier }) => {
if (postModifier) {
builtOptions[optionKey] = postModifier(builtOptions[optionKey], builtOptions);
}
});
return builtOptions;
}
}
exports.AbstractOptionsContainer = AbstractOptionsContainer;
//# sourceMappingURL=AbstractOptionsContainer.js.map