-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·117 lines (100 loc) · 2.89 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
const MergeConfig = require("merge-config-updated");
const storage = require("./lib/storage");
const envify = require("./lib/envify");
/**
* Config class
*
* It accepts a path to the config directory, provider name for loading configs
* and selected format for opening/parsing configs
*/
class Config {
/**
* constructor method
*
* @param {Object} options
* @param {String} options.provider Name of the provider class
* @param {String} options.format Format of config files
* @param {String} options.env NODE_ENV env content
* @param {String} options.region REGION env content
*/
constructor(options = {}) {
this.options = options;
this.options.provider = options.provider || "FileSystem";
this.options.format = options.format || "json";
this.options.env = options.env || "default";
this.options.region = options.region || "default";
this.conf = {};
}
static createMergeConfig(configs) {
const mergeConfig = new MergeConfig();
// Default config to empty
mergeConfig.merge({});
// Loop over the given configs and add them based on array's hierarchy
configs.forEach(config => {
mergeConfig.merge(config);
});
// Add a command-line arguments override
mergeConfig.argv();
// Add an environment variables override
// mergeConfig.env();
mergeConfig.merge(envify.envify(mergeConfig.get()));
return mergeConfig;
}
/**
* It loads the content configs based on the priority that has been provided
*/
load() {
this.options.files = this.getPriorities();
return storage
.load(this.options)
.then(Config.createMergeConfig)
.then(mergeConfig => {
this.conf = mergeConfig;
return this;
});
}
/**
* It synchronously loads the content configs based on the priority that has been provided
*/
loadSync() {
this.options.files = this.getPriorities();
this.conf = Config.createMergeConfig(storage.loadSync(this.options));
return this;
}
/**
* Returns array of prioritised config files based on env and region
*
* @return {[type]} [description]
*/
getPriorities() {
const files = [`default.${this.options.format}`];
// Load default node env first
if (this.options.env !== "default") {
files.push(`${this.options.env}/default.${this.options.format}`);
}
// Load region after default env, in order to keep the hierarchy correctly
if (this.options.env !== "default" && this.options.region !== "default") {
files.push(
`${this.options.env}/${this.options.region}.${this.options.format}`
);
}
return files;
}
/**
* Returns specific key from configs
*
* @return {String|Number|Boolean}
*/
get(key) {
return this.conf.get(key);
}
/**
* Returns all configs
*
* @return {Object}
*/
getAll() {
return this.conf.get();
}
}
module.exports = Config;