-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfucious.js
189 lines (157 loc) · 3.82 KB
/
confucious.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
'use strict';
module.exports = function () {
var fs = require('fs');
var argv = require('yargs').argv;
var extend = require('extend');
var globalValues = {};
var baseValues = {};
var valuesStack = [];
valuesStack.push(globalValues);
valuesStack.push(baseValues);
//
// Debug only function to get the current state of the value stack.
// NOTE: This deep copies the data so that it cannot be modified externally this
// ensures the managed configuration is immutable.
//
this.getValueStackCopy = function () {
return extend(true, [], valuesStack);
};
//
// Set a value by key at the top level of the key/value staci.
//
this.set = function (key, value) {
var keyParts = key.split(':');
var hash = valuesStack[valuesStack.length-1];
for (var i = 0; i < keyParts.length-1; ++i) {
var subKey = keyParts[i];
hash = hash[subKey];
if (typeof(hash) === 'undefined') {
hash = {};
hash[subKey] = hash;
}
}
var lastKeyPartIndex = keyParts.length-1;
var lastSubKey = keyParts[lastKeyPartIndex];
if (typeof(value) === 'object') {
hash[lastSubKey] = extend(true, {}, value);
}
else {
hash[lastSubKey] = value;
}
};
//
// Clear a key from the top level of the key/value stack.
//
this.clear = function (key) {
var keyParts = key.split(':');
var hash = valuesStack[valuesStack.length-1];
for (var i = 0; i < keyParts.length-1; ++i) {
var subKey = keyParts[i];
hash = hash[subKey];
if (typeof(hash) === 'undefined') {
return;
}
}
var lastKeyPartIndex = keyParts.length-1;
var lastSubKey = keyParts[lastKeyPartIndex];
delete hash[lastSubKey];
};
//
// Set a global key/value.
//
this.setGlobal = function (key, value) {
globalValues[key] = value;
};
//
// Clear a global key/value.
//
this.clearGlobal = function (key) {
delete globalValues[key];
};
//
// Get a value from the specified object.
// Returns undefined if not found.
//
var getValue = function (key, hash) {
var value = hash[key];
if (typeof(value) !== 'undefined') {
return value;
}
return undefined;
};
//
// Get a nested value from the specified object.
// Returns undefined if not found.
//
var getNestedValue = function (keyParts, hash) {
for (var i = 0; i < keyParts.length-1; ++i) {
hash = getValue(keyParts[i], hash);
if (typeof(hash) === 'undefined') {
return undefined;
}
}
var lastKeyPartIndex = keyParts.length-1;
return getValue(keyParts[lastKeyPartIndex], hash)
};
//
// Get a value by key.
// Key can reference a nested value.
// Searches all levels in the key/value stack.
// Returns undefined if the key does not exist anywhere.
//
this.get = function (key) {
var keyParts = key.split(':');
for (var i = valuesStack.length-1; i >= 0; --i) {
var value = getNestedValue(keyParts, valuesStack[i]);
if (typeof(value) !== 'undefined') {
return value;
}
}
return undefined;
};
//
// Push a set of key/values on the key/value stack.
//
this.push = function (values) {
valuesStack.push(extend(true, {}, values));
};
//
// Pop a set of values from the stack.
//
this.pop = function () {
if (valuesStack.length > 2) {
valuesStack.pop();
}
else {
// Can't push the base levels of the stack.
}
};
//
// Load a json file and push it on the key/value stack.
//
this.pushJsonFile = function (filePath) {
this.push(JSON.parse(fs.readFileSync(filePath, 'utf8')));
};
//
// Push arguments onto the key/value stack.
//
this.pushArgv = function () {
this.push(argv);
};
//
// Push environment variables onto the key/value stack.
//
this.pushEnv = function () {
this.push(process.env);
};
//
// Get the current configuration as a JavaScript object.
//
this.toObject = function () {
var base = {};
valuesStack.forEach(function (value) {
base = extend(base, value);
});
return base;
};
};