Commit | Line | Data |
---|---|---|
1360a454 D |
1 | var fs = require('fs'); |
2 | ||
3 | var config_filename = 'config.js', | |
11dbb00f | 4 | config_dirs = ['/etc/kiwiirc/', __dirname + '/../'], |
1360a454 | 5 | environment = 'production', |
11dbb00f | 6 | loaded_config = Object.create(null); |
1360a454 D |
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); | |
1360a454 D |
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) { | |
11dbb00f | 39 | loaded_config = new_config; |
b737610b | 40 | global.config = new_config[environment]; |
11dbb00f | 41 | return loaded_config; |
1360a454 | 42 | } else { |
11dbb00f | 43 | return false; |
1360a454 D |
44 | } |
45 | } | |
46 | ||
47 | ||
48 | ||
49 | module.exports.setEnvironment = function (new_environment) { | |
11dbb00f | 50 | environment = new_environment; |
1360a454 D |
51 | }; |
52 | ||
53 | // Get the current config. Optionally for a different environment than currently set | |
54 | module.exports.get = function (specific_environment) { | |
11dbb00f D |
55 | specific_environment = specific_environment || environment; |
56 | ||
57 | return loaded_config[specific_environment]; | |
1360a454 D |
58 | }; |
59 | ||
60 | module.exports.loadConfig = loadConfig; |