-
Notifications
You must be signed in to change notification settings - Fork 0
/
settingsDecorator.js
72 lines (59 loc) · 1.6 KB
/
settingsDecorator.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
/**
* Decorates trellis.setting with helper methods that allow bundles to impose
* constraints on configuration during startup.
*/
var util = require('util');
module.exports = function(trellis) {
function exists(path) {
var parts = settingString.split('.'),
parentObject = trellis.settings;
for (var i = 0; i < parts.length; i++) {
var nextPart = parts[i];
if (!parentObject.hasOwnProperty(nextPart)) {
return false;
}
parentObject = parentObject[nextPart];
}
return true;
}
trellis.settings.defaultValue = function(settingString, defaultValue) {
var parts = settingString.split('.'),
parentObject = trellis.settings;
for (var i = 0; i < parts.length; i++) {
var nextPart = parts[i];
if (!parentObject.hasOwnProperty(nextPart)) {
if (i === parts.length - 1) {
parentObject[nextPart] = defaultValue;
}
else {
parentObject[nextPart] = {};
}
}
parentObject = parentObject[nextPart];
}
};
trellis.settings.require = function(settingString) {
var parts = settingString.split('.'),
parentObject = trellis.settings,
objectPath = '';
for (var i = 0; i < parts.length; i++) {
var nextPart = parts[i];
if (i > 0) {
objectPath += '.';
}
objectPath += nextPart;
if (!parentObject.hasOwnProperty(nextPart)) {
var errorMessage = util.format('Required setting "%s" not present in configuration', objectPath);
if (trellis.logger) {
trellis.logger.error(errorMessage);
}
else {
console.error(errorMessage);
}
process.exit(1);
}
parentObject = parentObject[nextPart];
}
return true;
};
};