-
Notifications
You must be signed in to change notification settings - Fork 1
/
configStore.js
217 lines (175 loc) · 5.01 KB
/
configStore.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
/* jshint -W069 */ // Ignore 'dot notation' errors
(function () {
var root = this,
previousStore,
argv = getArgv(),
env = getEnv(),
compiled;
if (root !== null) {
previousStore = root.configStore;
}
function configStore (config, callback) {
function done (err, store) {
if (typeof callback === 'function')
callback(err, store);
if (err) throw err;
return store;
}
if (typeof config === 'string') {
if (typeof require === 'function') {
config = parse(require(config));
} else if (JSON && XMLHttpRequest) {
var oCallback = callback;
callback = null;
var xhr = new XMLHttpRequest();
xhr.open('GET', config, true);
xhr.overrideMimeType('application/json');
config = {};
xhr.onreadystatechange = function () {
if (xhr.readyState !== 4)
return;
var err = null;
var s = xhr.status;
if (!s && xhr.response || s >= 200 && s < 300 || s === 304) {
try {
config = parse(JSON.parse(xhr.responseText));
} catch (e) {
err = e;
config = {};
}
} else {
err = new Error('HTTP Error ' + xhr.status);
}
if (typeof oCallback === 'function') {
oCallback(err, store);
}
};
try {
xhr.send(null);
} catch (e) {
xhr.abort();
return done(e, store);
}
}
} else {
config = parse(config);
}
function store (param, defaultValue) {
param = normalise(param);
if (param in argv)
return argv[param];
if (param in config)
return config[param];
if (param in env)
return env[param];
if (arguments.length > 1) {
if (typeof defaultValue === 'function')
return defaultValue(param);
else
return defaultValue;
}
throw new ReferenceError('Unable to find ' + param + ' configuration');
}
store.find = function (param, defaultValue) {
if (typeof compiled === 'undefined') {
compiled = compile(argv, config, env);
console.log(compiled);
}
var parts = normalise(param).toLowerCase().split('_').reverse(),
data = compiled,
part;
while (parts.length) {
part = parts.pop();
if (typeof data[part] === 'undefined')
return defaultValue;
if (!parts.length)
return data[part];
data = data[part];
}
return data || defaultValue;
};
return done(null, store);
}
configStore.noConflict = function () {
root.configStore = previousStore;
return configStore;
};
// AMD / RequireJS
if (typeof define !== 'undefined' && define.amd) {
define([], function () {
return configStore;
});
}
// Node.js
else if (typeof module !== 'undefined' && module.exports) {
module.exports = configStore;
}
// included directly via <script> tag
else {
root.configStore = configStore;
}
function normalise (value) {
return (''+value).replace(/[^a-z_]+/i, '_').replace(/([^A-Z_])([A-Z])/g, '$1_$2').toUpperCase();
}
function parse (config) {
config = config || {};
var parsed = {};
Object.keys(config).forEach(function (key) {
var normalised = normalise(key);
if (Object.prototype.toString.call(config[key]) === '[object Object]') {
var subconfig = parse(config[key]);
Object.keys(subconfig).forEach(function(key) {
parsed[normalised+'_'+key] = subconfig[key];
});
} else {
parsed[normalised] = config[key];
}
});
return parsed;
}
function compile () {
var configs = Array.prototype.slice.call(arguments);
return configs.reduce(function(compiled, config) {
return Object.keys(config).reduce(function(compiled, key) {
if (key.charAt(0) === '_') return compiled;
var parts = key.toLowerCase().split(/_+/),
last = parts.length - 1,
obj = compiled;
parts.forEach(function(part, index) {
var type = Object.prototype.toString.call(obj[part]);
if (!type.match(/^\[object (Undefined|Object|Array)\]$/))
return;
if (index === last) {
if (!obj.hasOwnProperty(part))
obj[part] = config[key];
} else {
if (!obj.hasOwnProperty(part))
obj[part] = {};
obj = obj[part];
}
});
return compiled;
}, compiled);
}, {});
}
function getArgv () {
try {
if (!process || !process.argv || (typeof require !== 'function')) {
return {};
}
var argv = require('optimist').argv;
delete argv['_'];
delete argv['$0'];
return parse(argv);
} catch (e) {
return {};
}
}
function getEnv () {
try {
return parse((process || {}).env || {});
} catch (e) {
return {};
}
}
})();