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() {
14 // Loop through the possible config paths and find a usable one
15 for (i
= 0; i
< config_dirs
.length
; i
++) {
16 conf_filepath
= config_dirs
[i
] + config_filename
;
19 if (fs
.lstatSync(conf_filepath
).isFile() === true) {
20 // Clear the loaded config cache
21 delete require
.cache
[require
.resolve(conf_filepath
)];
23 // Try load the new config file
24 new_config
= require(conf_filepath
);
29 case 'ENOENT': // No file/dir
32 console
.log('An error occured parsing the config file ' + config_dirs
[i
] + config_filename
+ ': ' + e
.message
);
40 loaded_config
= new_config
;
41 global
.config
= new_config
[environment
] || {};
50 module
.exports
.setEnvironment = function (new_environment
) {
51 environment
= new_environment
;
54 // Get the current config. Optionally for a different environment than currently set
55 module
.exports
.get = function (specific_environment
) {
56 specific_environment
= specific_environment
|| environment
;
58 return loaded_config
[specific_environment
] || {};
61 module
.exports
.loadConfig
= loadConfig
;