1 var fs
= require('fs');
3 var config_filename
= 'config.js',
4 config_dirs
= ['/etc/kiwiirc/', __dirname
+ '/../'],
5 environment
= 'production',
6 loaded_config
= Object
.create(null);
9 function loadConfig() {
13 // Loop through the possible config paths and find a usable one
14 for (var i
in config_dirs
) {
15 conf_filepath
= config_dirs
[i
] + config_filename
;
18 if (fs
.lstatSync(conf_filepath
).isFile() === true) {
19 // Clear the loaded config cache
20 delete require
.cache
[require
.resolve(conf_filepath
)];
22 // Try load the new config file
23 new_config
= require(conf_filepath
);
28 case 'ENOENT': // No file/dir
31 console
.log('An error occured parsing the config file ' + config_dirs
[i
] + config_filename
+ ': ' + e
.message
);
39 loaded_config
= new_config
;
40 global
.config
= new_config
[environment
] || {};
49 module
.exports
.setEnvironment = function (new_environment
) {
50 environment
= new_environment
;
53 // Get the current config. Optionally for a different environment than currently set
54 module
.exports
.get = function (specific_environment
) {
55 specific_environment
= specific_environment
|| environment
;
57 return loaded_config
[specific_environment
] || {};
60 module
.exports
.loadConfig
= loadConfig
;