-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
58 lines (50 loc) · 1.14 KB
/
index.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
"use strict";
var
_ = require('lodash'),
env = process.env,
url = require('url'),
validUrl = require('valid-url');
var nodeMode = env.NODE_ENV;
var contexts = [];
function sconf(conf, development, test, production){
if(_.isString(conf)){
var prefix = contexts.join('_');
var val = env[prefix ? prefix+'_'+conf : conf];
var extra;
switch(nodeMode){
case 'development':
extra = development;
break;
case 'test':
extra = test;
break;
case 'production':
extra = production
break;
}
if(_.isUndefined(val)){
val = extra || development || test || production || '';
}
if(_.isString(val) && validUrl.isUri(val)){
return url.parse(val);
}else{
switch(val){
case 'true': return true;
case 'false': return false;
default: return val;
}
}
} else{
if(contexts.length){
contexts.pop();
}
return conf;
}
}
sconf.prefix = function(prefix){
// Push a prefix in the contexts stack.
contexts.push(prefix);
return sconf;
}
sconf.extend = _.bind(_.extend, _);
module.exports = sconf;