1 var fs
= require('fs'),
2 events
= require('events'),
3 util
= require('util');
5 var config_filename
= 'config.js',
6 config_dirs
= ['/etc/kiwiirc/', __dirname
+ '/../'],
7 environment
= 'production',
8 loaded_config
= Object
.create(null);
10 var Config = function () {
11 events
.EventEmitter
.call(this);
13 util
.inherits(Config
, events
.EventEmitter
);
15 Config
.prototype.loadConfig = function () {
20 // Loop through the possible config paths and find a usable one
21 for (i
= 0; i
< config_dirs
.length
; i
++) {
22 conf_filepath
= config_dirs
[i
] + config_filename
;
25 if (fs
.lstatSync(conf_filepath
).isFile() === true) {
26 // Clear the loaded config cache
27 delete require
.cache
[require
.resolve(conf_filepath
)];
29 // Try load the new config file
30 new_config
= require(conf_filepath
);
35 case 'ENOENT': // No file/dir
38 console
.log('An error occured parsing the config file ' + config_dirs
[i
] + config_filename
+ ': ' + e
.message
);
46 loaded_config
= new_config
;
47 global
.config
= new_config
[environment
] || {};
57 Config
.prototype.setEnvironment = function (new_environment
) {
58 environment
= new_environment
;
61 // Get the current config. Optionally for a different environment than currently set
62 Config
.prototype.get = function (specific_environment
) {
63 specific_environment
= specific_environment
|| environment
;
65 return loaded_config
[specific_environment
] || {};
68 module
.exports
= new Config();