-
Notifications
You must be signed in to change notification settings - Fork 459
/
config.js
98 lines (82 loc) · 3.85 KB
/
config.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
/*───────────────────────────────────────────────────────────────────────────*\
│ Copyright 2016 PayPal │
│ │
│hh ,'""`. │
│ / _ _ \ Licensed under the Apache License, Version 2.0 (the "License"); │
│ |(@)(@)| you may not use this file except in compliance with the License. │
│ ) __ ( You may obtain a copy of the License at │
│ /,'))((`.\ │
│(( (( )) )) http://www.apache.org/licenses/LICENSE-2.0 │
│ `\ `)(' /' │
│ │
│ Unless required by applicable law or agreed to in writing, software │
│ distributed under the License is distributed on an "AS IS" BASIS, │
│ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │
│ See the License for the specific language governing permissions and │
│ limitations under the License. │
\*───────────────────────────────────────────────────────────────────────────*/
'use strict';
var Bluebird = require('bluebird');
var path = require('path');
var confit = require('confit');
var handlers = require('shortstop-handlers');
var ssresolve = require('shortstop-resolve');
function createHandlers(options) {
var result;
result = {
file: handlers.file(options.basedir),
path: handlers.path(options.basedir),
base64: handlers.base64(),
env: handlers.env(),
require: handlers.require(options.basedir),
exec: handlers.exec(options.basedir),
glob: handlers.glob(options.basedir)
};
Object.keys(options.protocols).forEach(function (protocol) {
result[protocol] = options.protocols[protocol];
});
return result;
}
function configPath(prefix, configdir) {
configdir = (configdir) ? configdir : 'config';
return path.join(prefix, configdir);
}
exports.create = function create(options) {
var deferred, appProtocols, baseProtocols, baseFactory, appFactory, baseConfig, appConfig;
deferred = Bluebird.pending();
appProtocols = createHandlers(options);
baseProtocols = createHandlers(options);
appProtocols.resolve = ssresolve(configPath(options.basedir, options.configdir));
baseProtocols.resolve = ssresolve(configPath(path.dirname(__dirname)));
baseConfig = Object.assign(
{
basedir: configPath(path.dirname(__dirname)),
protocols: baseProtocols
},
options.confit || {}
);
baseFactory = confit(baseConfig);
baseFactory.create(function(err, baseConf) {
if (err) {
deferred.reject(err);
return;
}
appConfig = Object.assign(
{
basedir: configPath(options.basedir, options.configdir),
protocols: appProtocols
},
options.confit || {}
);
appFactory = confit(appConfig);
appFactory.create(function(err, appConf) {
if (err) {
deferred.reject(err);
return;
}
baseConf.merge(appConf);
deferred.resolve(baseConf);
});
});
return deferred.promise;
};