153 lines
4.4 KiB
JavaScript
153 lines
4.4 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const immutable_1 = __importDefault(require("immutable"));
|
|
const file_1 = __importDefault(require("./file"));
|
|
const pluginDependency_1 = __importDefault(require("./pluginDependency"));
|
|
const configDefault_1 = __importDefault(require("../constants/configDefault"));
|
|
const reducedObject_1 = __importDefault(require("../utils/reducedObject"));
|
|
class Config extends immutable_1.default.Record({
|
|
file: new file_1.default(),
|
|
values: configDefault_1.default
|
|
}, "Config") {
|
|
getFile() {
|
|
return this.get("file");
|
|
}
|
|
getValues() {
|
|
return this.get("values");
|
|
}
|
|
/**
|
|
* Return minimum version of configuration,
|
|
* Basically it returns the current config minus the default one
|
|
* @return {Map}
|
|
*/
|
|
toReducedVersion() {
|
|
return (0, reducedObject_1.default)(configDefault_1.default, this.getValues());
|
|
}
|
|
/**
|
|
* Render config as text
|
|
* @return {Promise<String>}
|
|
*/
|
|
toText() {
|
|
return JSON.stringify(this.toReducedVersion().toJS(), null, 4);
|
|
}
|
|
/**
|
|
* Change the file for the configuration
|
|
* @param {File} file
|
|
* @return {Config}
|
|
*/
|
|
setFile(file) {
|
|
return this.set("file", file);
|
|
}
|
|
/**
|
|
* Return a configuration value by its key path
|
|
*/
|
|
getValue(keyPath, def) {
|
|
const values = this.getValues();
|
|
keyPath = Config.keyToKeyPath(keyPath);
|
|
if (!values.hasIn(keyPath)) {
|
|
return immutable_1.default.fromJS(def);
|
|
}
|
|
return values.getIn(keyPath);
|
|
}
|
|
/**
|
|
* Update a configuration value
|
|
* @return {Config}
|
|
*/
|
|
setValue(keyPath, value) {
|
|
keyPath = Config.keyToKeyPath(keyPath);
|
|
value = immutable_1.default.fromJS(value);
|
|
let values = this.getValues();
|
|
values = values.setIn(keyPath, value);
|
|
return this.set("values", values);
|
|
}
|
|
/**
|
|
* Return a list of plugin dependencies
|
|
* @return {List<PluginDependency>}
|
|
*/
|
|
getPluginDependencies() {
|
|
const plugins = this.getValue("plugins");
|
|
if (typeof plugins === "string") {
|
|
return pluginDependency_1.default.listFromString(plugins);
|
|
}
|
|
else {
|
|
return pluginDependency_1.default.listFromArray(plugins);
|
|
}
|
|
}
|
|
/**
|
|
* Return a plugin dependency by its name
|
|
* @param {string} name
|
|
* @return {PluginDependency}
|
|
*/
|
|
getPluginDependency(name) {
|
|
const plugins = this.getPluginDependencies();
|
|
return plugins.find((dep) => {
|
|
return dep.getName() === name;
|
|
});
|
|
}
|
|
/**
|
|
* Update the list of plugins dependencies
|
|
* @param {List<PluginDependency>}
|
|
* @return {Config}
|
|
*/
|
|
setPluginDependencies(deps) {
|
|
const plugins = pluginDependency_1.default.listToArray(deps);
|
|
return this.setValue("plugins", plugins);
|
|
}
|
|
/**
|
|
* Update values for an existing configuration
|
|
* @param {Object} values
|
|
* @returns {Config}
|
|
*/
|
|
updateValues(values) {
|
|
values = immutable_1.default.fromJS(values);
|
|
return this.set("values", values);
|
|
}
|
|
/**
|
|
* Update values for an existing configuration
|
|
* @param {Config} config
|
|
* @param {Object} values
|
|
* @returns {Config}
|
|
*/
|
|
mergeValues(values) {
|
|
let currentValues = this.getValues();
|
|
values = immutable_1.default.fromJS(values);
|
|
currentValues = currentValues.mergeDeep(values);
|
|
return this.set("values", currentValues);
|
|
}
|
|
/**
|
|
* Create a new config for a file
|
|
* @param {File} file
|
|
* @param {Object} values
|
|
* @returns {Config}
|
|
*/
|
|
static create(file, values) {
|
|
return new Config({
|
|
file: file,
|
|
values: immutable_1.default.fromJS(values)
|
|
});
|
|
}
|
|
/**
|
|
* Create a new config
|
|
* @param {Object} values
|
|
* @returns {Config}
|
|
*/
|
|
static createWithValues(values) {
|
|
return new Config({
|
|
values: immutable_1.default.fromJS(values)
|
|
});
|
|
}
|
|
/**
|
|
* Convert a keyPath to an array of keys
|
|
*/
|
|
static keyToKeyPath(keyPath) {
|
|
if (typeof keyPath === "string") {
|
|
return keyPath.split(".");
|
|
}
|
|
return keyPath;
|
|
}
|
|
}
|
|
exports.default = Config;
|