-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
86 lines (68 loc) · 2 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
'use strict';
/*jshint node: true, laxcomma: true*/
var _ = require('lodash')
, fs = require('fs')
, os = require('os')
, yaml = require('js-yaml');
var configOrder = [
'default',
process.env.HOST || process.env.HOSTNAME || os.hostname(),
process.env.NODE_ENV
];
var watch = process.env.MECH_WATCH;
function mergeConfigs(dst, src) {
_.each(src || {}, function (value, key) {
// if destination exists & already has `~override` flag, keep it intact
if (_.isObject(dst[key]) && dst[key]['~override']) {
return;
}
// if source has `~override` flag - override whole value in destination
if (value && value['~override']) {
dst[key] = value;
return;
}
// if both nodes are objects - merge recursively
if (_.isObject(value) && _.isObject(dst[key])) {
mergeConfigs(dst[key], value);
return;
}
// destination node does not exist - create
// or both nodes are of different types - override.
dst[key] = value;
return;
});
return dst;
}
function Config () {
var config = {}
, that = this;
(function parse () {
var oldConfig = config;
config = {};
_.each(_.map(configOrder, function (file) {
return './config/' + file + '.yml';
}), function (file) {
if (fs.existsSync(file)) {
var contents = fs.readFileSync(file, 'utf8');
var current = yaml.safeLoad(contents, { filename: file });
mergeConfigs(config, current);
if (watch) {
fs.unwatchFile(file);
fs.watchFile(file, { persistent: false, interval: 1000 }, function () {
parse();
});
}
}
});
_.each(_.difference(_.keys(oldConfig), _.keys(config)), function (key) {
delete that[key];
});
_.each(config, _.bind(function (value, key) {
that.__defineGetter__(key, function () {
return value;
});
}, that));
}());
}
global.NODE_CONFIG = global.NODE_CONFIG || new Config();
module.exports = global.NODE_CONFIG;