forked from C2FO/gofigure
/
index.js
157 lines (145 loc) · 5.37 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
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
/**
* @projectName gofigure
* @github http://github.com/C2FO/gofigure
* @header [../README.md]
* @includeDoc [Change Log] ../History.md
*/
const _ = require('lodash');
const PatternEventEmitter = require('./PatternEventEmitter');
const loader = require('./loader');
const processor = require('./processor');
class ConfigLoader extends PatternEventEmitter {
/**
* Aggregate class to load configurations from and merge into a single config.
*
* @param loaderConfig a the configuration to pass to [loader}
* @param [options.environment=process.env.NODE_ENV] the environment to use when loading configurations
* @param [options.nodeType=process.env.NODE_TYPE] the node type to use when loading configurations
* @param [options.environmentVariables=process.env] an object of environment variables to use
* @param [options.defaultEnvironment='*'] the name of the default environment to use as a base to merge other
* environments into
* @param [options.monitor=false] the name of the default environment to use as a base to merge other environments
* into.
*/
constructor(loaderConfig, opts) {
super({});
if (!loaderConfig) {
throw new Error('A loader configuration is required');
}
const options = opts || {};
this.environment = options.environment || process.env.NODE_ENV || null;
this.nodeType = options.nodeType || process.env.NODE_TYPE || null;
this.environmentVariables = options.environmentVariables || process.env || {};
this.defaultEnvironment = options.defaultEnvironment || '*';
this.config = {};
this.monitor = !!options.monitor;
this.loaderConfig = loaderConfig;
this.loaded = false;
}
/**
* Stops monitoring confgurations for changes.
*
* @return {Config} this config object.
*/
stop() {
this.__loaders.forEach(l => l.unWatch());
return this;
}
/**
* Asynchronously loads configs.
*
* @example
* configLoader.load().then((config) => {
* // use the config.
* })
*
* @return {Promise<any>|Promise<any[] | never>} resolves with the merged configuration from all locations.s
*/
load() {
if (this.__loaded) {
return Promise.resolve(this.config);
}
return loader.createLoadersAsync(this.loaderConfig, { monitor: this.monitor })
.then((loaders) => {
this.__loaders = loaders;
const loads = loaders.map(l => l.load());
return Promise.all(loads)
.then(configs => this.__postLoad(configs));
});
}
/**
* Synchronously loads configurations.
*
* @example
* const config = configLoader.loadSync()
* //use your config.
*
* @return {Object} the merged configuration from all locations.
*/
loadSync() {
if (this.__loaded) {
return this.config;
}
this.__loaders = loader.createLoadersSync(this.loaderConfig, { monitor: this.monitor });
return this.__postLoad(this.__loaders.map(l => l.loadSync()));
}
__mergeConfigs(configs) {
return processor(_.cloneDeep(this.config), configs, {
environment: this.environment,
defaultEnvironment: this.defaultEnvironment,
nodeType: this.nodeType,
eventEmitter: this,
environmentVariables: this.environmentVariables,
});
}
/**
* Merges configs and starts monitoring of configs if `monitor` is true.
* @param configs {Array<Object>} an array of configurations to merge into a single object.
* @return {Object} the merged config
* @private
*/
__postLoad(configs) {
this.config = this.__mergeConfigs(configs);
this.__listenToChanges();
this.__loaded = true;
return this.config;
}
__listenToChanges() {
this.__loaders.forEach(l => l.on('change', (path, config) => {
this.config = this.__mergeConfigs([ config ]);
}));
}
}
/**
* Factory method to create a new {@link ConfigLoader}.
*
* @example
*
* const loader = gofigure({locations: ["path/to/config.json"]})
*
* @example
* const loader = gofigure({locations: ["path/to/config_dir"]})
*
* @param options {{locations: String|String}}
* @param options.locations locations to load files from
* @param [options.environment=process.env.NODE_ENV] the environment to use when loading configurations
* @param [options.nodeType=process.env.NODE_TYPE] the node type to use when loading configurations
* @param [options.environmentVariables=process.env] an object of environment variables to use
* @param [options.defaultEnvironment='*'] the name of the default environment to use as a base to merge other
* environments into
* @param [options.monitor=false] the name of the default environment to use as a base to merge other environments into.
* @return {ConfigLoader} the Config to use to load configurations.
*/
function gofigure(options = {}) {
const locations = (options.locations || []).map((file) => {
if (_.isPlainObject(file)) {
return file;
}
return { file };
});
if (!locations) {
throw new Error('Please provide either locations or etcd endpoints');
}
return new ConfigLoader({ locations }, options);
}
module.exports = gofigure;