blob: 83dd6d4c99d506d4339f04e3f6d4a62e8b929df7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
var is = require('is');
var Immutable = require('immutable');
var File = require('./file');
var PluginDependency = require('./pluginDependency');
var configDefault = require('../constants/configDefault');
var Config = Immutable.Record({
file: File(),
values: configDefault
}, 'Config');
Config.prototype.getFile = function() {
return this.get('file');
};
Config.prototype.getValues = function() {
return this.get('values');
};
/**
Return a configuration value by its key path
@param {String} key
@return {Mixed}
*/
Config.prototype.getValue = function(keyPath, def) {
var values = this.getValues();
keyPath = Config.keyToKeyPath(keyPath);
if (!values.hasIn(keyPath)) {
return Immutable.fromJS(def);
}
return values.getIn(keyPath);
};
/**
Update a configuration value
@param {String} key
@param {Mixed} value
@return {Mixed}
*/
Config.prototype.setValue = function(keyPath, value) {
keyPath = Config.keyToKeyPath(keyPath);
value = Immutable.fromJS(value);
var values = this.getValues();
values = values.setIn(keyPath, value);
return this.set('values', values);
};
/**
Return a list of plugin dependencies
@return {List<PluginDependency>}
*/
Config.prototype.getPluginDependencies = function() {
var plugins = this.getValue('plugins');
if (is.string(plugins)) {
return PluginDependency.listFromString(plugins);
} else {
return PluginDependency.listFromArray(plugins);
}
};
/**
Update the list of plugins dependencies
@param {List<PluginDependency>}
@return {Config}
*/
Config.prototype.setPluginDependencies = function(deps) {
var plugins = PluginDependency.listToArray(deps);
return this.setValue('plugins', plugins);
};
/**
Create a new config for a file
@param {File} file
@param {Object} values
@returns {Config}
*/
Config.create = function(file, values) {
return new Config({
file: file,
values: Immutable.fromJS(values)
});
};
/**
Create a new config
@param {Object} values
@returns {Config}
*/
Config.createWithValues = function(values) {
return new Config({
values: Immutable.fromJS(values)
});
};
/**
Update values for an existing configuration
@param {Config} config
@param {Object} values
@returns {Config}
*/
Config.updateValues = function(config, values) {
values = Immutable.fromJS(values);
return config.set('values', values);
};
/**
Convert a keyPath to an array of keys
@param {String|Array}
@return {Array}
*/
Config.keyToKeyPath = function(keyPath) {
if (is.string(keyPath)) keyPath = keyPath.split('.');
return keyPath;
};
module.exports = Config;
|