-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconf.js
240 lines (206 loc) · 6.38 KB
/
conf.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* Copyright: E2E Technologies Ltd
*/
'use strict';
var nconf = require('nconf');
var path = require('path');
var fs = require('fs-extra');
var async = require('async');
var initialized = false;
function compare(left, right) {
if (left instanceof Object) {
if (!(right instanceof Object)) {
return false;
}
if (left.length !== right.length) {
return false;
}
return Object.keys(left).every(function (prop) {
return compare(left[prop], right[prop]);
});
} else {
return left === right;
}
}
function _checkInitialized() {
if(!initialized) {
throw new Error("e2e-conf hasn't been initialized.");
}
}
function _initFiles(configPath, localConfig) {
if (!configPath) {
throw new Error("Cannot initialize e2e-conf without config path");
}
this.defaultFileName = localConfig ? configPath : path.resolve(configPath, 'config/default/config.json');
this.localDirectory = localConfig ? path.dirname(localConfig) : path.resolve(configPath, 'config/local');
this.localFileName = localConfig ? localConfig : path.resolve(this.localDirectory, 'config.json');
nconf.file('local', {file: this.localFileName})
.file('default', {file: this.defaultFileName});
}
/**
* Initialize the configuration with custom path(s).
*
* @param {String} configPath Directory where the configuration files are looked up or path to default config file.
* @param {String?} localConfig Path to local config file. If given, configPath is interpreted as path to default config file.
*
*/
exports.init = function init(configPath, localConfig) {
if (!initialized) {
nconf.argv()
.env('__');
_initFiles.apply(this, arguments);
initialized = true;
} else {
throw new Error("Calling init() twice does not work. Call cleanUp() in between.");
}
};
/**
* Initialize the configuration with custom path(s) and only values from files and not from program arguments
* or environment.
*
* @param {String} configPath Directory where the configuration files are looked up or path to default config file.
* @param {String?} localConfig Path to local config file. If given, configPath is interpreted as path to default config file.
*/
exports.initOnlyFiles = function init(configPath, localConfig) {
if (!initialized) {
_initFiles.apply(this, arguments);
initialized = true;
} else {
throw new Error("Calling init() twice does not work. Call cleanUp() in between.");
}
};
/**
* Do a cleanup and after it you can call init() with a different argument.
*/
exports.cleanUp = function cleanUp() {
// Remove our stores.
nconf.remove('local');
nconf.remove('default');
nconf.remove('env');
initialized = false;
};
/**
* Get a value.
*
* @param {String?} key If you want to get a property of an object use 'object_name:property'
* otherwise use simple 'root_property'.
* @returns {*} Value of name or undefined. Can be a simple type, object or array.
*/
exports.get = function get(key) {
_checkInitialized();
return nconf.get(key);
};
/**
* Sets the `value` for the specified `key`.
*
* @param {String} key Key to set in this instance
* @param {literal|Object} value Value for the specified key
*/
exports.set = function set(key, value) {
_checkInitialized();
return nconf.set(key, value);
};
/**
* Sets all properties from the object.
*
* @param {Object} object Values to set.
*/
exports.setObject = function setObject(object) {
_checkInitialized();
Object.keys(object).forEach(function (prop) {
nconf.set(prop, object[prop]);
});
};
/**
* Save changes to local configuration file (difference only).
* @param [actualConf] Current configuration which should be saved.
* @param callback
*/
exports.save = function save(actualConf, callback) {
var self = this;
if (typeof actualConf === 'function') {
callback = actualConf;
actualConf = nconf.stores.local && nconf.stores.local.store;
}
try {
_checkInitialized();
} catch(e) {
if(typeof callback === 'function') {
return callback(e);
} else {
throw e;
}
}
async.waterfall([
function (callback) {
fs.ensureDir(self.localDirectory, function (err, dirName) {
callback(err, dirName);
});
},
function (dirName, callback) {
fs.readJson(self.defaultFileName, function (err, defaultConf) {
callback(err, defaultConf);
});
},
function (defaultConf, callback) {
var diff = difference(defaultConf, actualConf);
if( diff === undefined) {
fs.remove(self.localFileName, callback);
} else {
fs.writeFile(self.localFileName, JSON.stringify(diff, null, ' '), callback);
}
}
], callback);
};
/**
* Get path of local configuration file.
*
* @returns {String} Local configuration file path
*/
exports.localFile = function localFile() {
_checkInitialized();
return this.localFileName;
};
/**
* Get path of default configuration file.
*
* @returns {String} Default configuration file path
*/
exports.defaultFile = function defaultFile() {
_checkInitialized();
return this.defaultFileName;
};
var difference = exports._difference = function difference(base, actual) {
var diff = {},
same = true;
if (Array.isArray(base)) {
diff = compare(base, actual) ? undefined : actual;
} else {
Object.keys(actual).forEach(function (prop) {
var value = base[prop],
localDiff;
if (prop in base) {
if (value instanceof Object) {
localDiff = difference(value, actual[prop]);
if( localDiff !== undefined) {
diff[prop] = localDiff;
same = false;
}
} else {
if (value !== actual[prop]) {
diff[prop] = actual[prop];
same = false;
}
}
}
else {
diff[prop] = actual[prop];
same = false;
}
});
if(same) {
diff = undefined;
}
}
return diff;
};