Removed cosole.log()
[KiwiIRC.git] / server / configuration.js
1 var fs = require('fs');
2
3 var config_filename = 'config.js',
4 config_dirs = ['/etc/kiwiirc/', __dirname + '/../'],
5 environment = 'production',
6 loaded_config = Object.create(null);
7
8
9 function loadConfig() {
10 var new_config,
11 conf_filepath;
12
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;
16
17 try {
18 if (fs.lstatSync(conf_filepath).isFile() === true) {
19 // Clear the loaded config cache
20 delete require.cache[require.resolve(conf_filepath)];
21
22 // Try load the new config file
23 new_config = require(conf_filepath);
24 break;
25 }
26 } catch (e) {
27 switch (e.code) {
28 case 'ENOENT': // No file/dir
29 break;
30 default:
31 console.log('An error occured parsing the config file ' + config_dirs[i] + config_filename + ': ' + e.message);
32 return false;
33 }
34 continue;
35 }
36 }
37
38 if (new_config) {
39 loaded_config = new_config;
40 global.config = new_config[environment] || {};
41 return loaded_config;
42 } else {
43 return false;
44 }
45 }
46
47
48
49 module.exports.setEnvironment = function (new_environment) {
50 environment = new_environment;
51 };
52
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;
56
57 return loaded_config[specific_environment] || {};
58 };
59
60 module.exports.loadConfig = loadConfig;