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 | 1
1
1
1
1
1
7
7
7
7
1
6
6
1
1
1
1
2
2
1
7
7
7
4
4
6
4
1
8
8
8
7
7
7
1
1
1
1
1
1
2
1
1
1
1
2
1
1
2
2
2
1
1
1
2
1
1
1
2
2
2
1
1
7
7
1
1
8
19
19
8
1
| var path = require('path');
var fs = require('fs');
var util = require('util');
var yaml = require('js-yaml');
var async = require('async');
var Loader = function() {
this.load = this.load.bind(this);
this.parseYaml = this.parseYaml.bind(this);
this.config = {};
this.loads = [];
};
/**
* Register a single yaml configuration path to be merged into the configuration.
*
* @param filePath
* The path on disk to register.
*/
Loader.prototype.addFile = function(filePath) {
this.loads.push(this.loadFile.bind(this, filePath));
return this;
};
/**
* Register a directory that will have config files loaded where older files will override younger.
*/
Loader.prototype.addDirectory = function(directoryPath) {
this.loads.push(this.loadDirectory.bind(this, directoryPath));
return this;
};
/**
* Register a directory that will have config files loaded into an array.
*/
Loader.prototype.addDirectoryArray = function(directoryPath, configKey) {
this.loads.push(this.loadDirectoryArray.bind(this, directoryPath, configKey));
return this;
};
/**
* Construct a configuration object from the registered paths.
*/
Loader.prototype.load = function(done) {
var self = this;
async.parallel(this.loads, function(error, configArray) {
if (error) return done(error);
var config = {};
for (i in configArray) {
config = self.mergeConifguration(config, configArray[i]);
}
done(null, config);
});
};
/**
* Load configuration for an individual file.
*/
Loader.prototype.loadFile = function(path, done) {
var self = this;
fs.exists(path, function(exists) {
if (exists) {
fs.readFile(path, 'utf8', function(error, data) {
/* istanbul ignore if: This error condition is near impossible to test. */
Iif (error) return done(error);
self.parseYaml(data, done);
});
}
else {
done(new Error(util.format('Specified configuration file `%s` not found.', path)));
}
});
};
/**
* Load config files from a directory allowing new entries to override old.
*/
Loader.prototype.loadDirectory = function(dirPath, done) {
var self = this;
fs.readdir(dirPath, function(error, files) {
/* istanbul ignore if: This error condition is near impossible to test. */
Iif (error) return done(error);
var loadFile = function(filePath, cb) {
self.loadFile(path.join(dirPath, filePath), cb);
};
async.map(files, loadFile, function(error, confs) {
/* istanbul ignore if: This error condition is near impossible to test. */
Iif (error) return done(error);
var conf = {};
for (i in confs) {
conf = self.mergeConifguration(conf, confs[i]);
}
done(null, conf);
});
});
};
/**
* Load config files from a directory into an array.
*/
Loader.prototype.loadDirectoryArray = function(dirPath, configKey, done) {
var self = this;
fs.readdir(dirPath, function(error, files) {
/* istanbul ignore if: This error condition is near impossible to test. */
if (error) return done(error);
var output = {};
output[configKey] = [];
var fileLoadHandler = function(file, cb){
fs.readFile(path.join(dirPath, file), 'utf8', cb);
};
async.map(files, fileLoadHandler, function(error, confs) {
/* istanbul ignore if: This error condition is near impossible to test. */
Iif (error) return done(error);
for (i in confs) {
try {
var conf = yaml.safeLoad(confs[i]);
output[configKey].push(conf);
}
catch(e) {
// Do something?
}
}
done(null, output);
});
});
};
/**
* Parse a yaml file and report an error if necessary.
*/
Loader.prototype.parseYaml = function(data, done) {
try {
return done(null, yaml.safeLoad(data));
}
catch (error) {
setImmediate(done.bind(null, error));
}
}
/**
* Merge two confiugration objects overriding values on the first with values on
* the second.
*/
Loader.prototype.mergeConifguration = function(one, two) {
for (i in two) {
Eif (two.hasOwnProperty(i)) {
one[i] = two[i];
}
}
return one;
};
module.exports = Loader;
|