Commit | Line | Data |
---|---|---|
cbe80532 JA |
1 | var fs = require('fs'), |
2 | events = require('events'), | |
60e7704b JA |
3 | util = require('util'), |
4 | path = require('path'); | |
1360a454 D |
5 | |
6 | var config_filename = 'config.js', | |
11dbb00f | 7 | config_dirs = ['/etc/kiwiirc/', __dirname + '/../'], |
1360a454 | 8 | environment = 'production', |
11dbb00f | 9 | loaded_config = Object.create(null); |
1360a454 | 10 | |
cbe80532 JA |
11 | var Config = function () { |
12 | events.EventEmitter.call(this); | |
13 | }; | |
14 | util.inherits(Config, events.EventEmitter); | |
1360a454 | 15 | |
da0ec4f7 | 16 | Config.prototype.loadConfig = function (manual_config_file) { |
1360a454 | 17 | var new_config, |
33edb4a0 T |
18 | conf_filepath, |
19 | i; | |
1360a454 | 20 | |
60e7704b JA |
21 | if ((manual_config_file) || (this.manual_config_file)) { |
22 | manual_config_file = path.resolve(path.normalize(manual_config_file || this.manual_config_file)); | |
da0ec4f7 JA |
23 | if (fs.existsSync(manual_config_file)) { |
24 | try { | |
25 | if (fs.lstatSync(manual_config_file).isFile() === true) { | |
26 | // Clear the loaded config cache | |
27 | delete require.cache[require.resolve(manual_config_file)]; | |
1360a454 | 28 | |
da0ec4f7 JA |
29 | // Try load the new config file |
30 | new_config = require(manual_config_file); | |
3f5e22c6 JA |
31 | |
32 | // Save location of configuration file so that we can re-load it later | |
33 | this.manual_config_file = manual_config_file; | |
da0ec4f7 JA |
34 | } |
35 | } catch (e) { | |
36 | console.log('An error occured parsing the config file ' + manual_config_file + ': ' + e.message); | |
37 | process.exit(1); | |
1360a454 | 38 | } |
da0ec4f7 JA |
39 | } else { |
40 | console.log('Could not find config file ' + manual_config_file); | |
41 | process.exit(1); | |
42 | } | |
43 | } else { | |
44 | // Loop through the possible config paths and find a usable one | |
45 | for (i = 0; i < config_dirs.length; i++) { | |
46 | conf_filepath = config_dirs[i] + config_filename; | |
47 | ||
48 | try { | |
49 | if (fs.lstatSync(conf_filepath).isFile() === true) { | |
50 | // Clear the loaded config cache | |
51 | delete require.cache[require.resolve(conf_filepath)]; | |
52 | ||
53 | // Try load the new config file | |
54 | new_config = require(conf_filepath); | |
55 | break; | |
56 | } | |
57 | } catch (e) { | |
58 | switch (e.code) { | |
59 | case 'ENOENT': // No file/dir | |
60 | break; | |
61 | default: | |
62 | console.log('An error occured parsing the config file ' + config_dirs[i] + config_filename + ': ' + e.message); | |
63 | return false; | |
64 | } | |
65 | continue; | |
1360a454 | 66 | } |
1360a454 D |
67 | } |
68 | } | |
69 | ||
70 | if (new_config) { | |
11dbb00f | 71 | loaded_config = new_config; |
0a8a61ad | 72 | global.config = new_config[environment] || {}; |
cbe80532 | 73 | this.emit('loaded'); |
11dbb00f | 74 | return loaded_config; |
1360a454 | 75 | } else { |
11dbb00f | 76 | return false; |
1360a454 | 77 | } |
cbe80532 | 78 | }; |
1360a454 D |
79 | |
80 | ||
81 | ||
cbe80532 | 82 | Config.prototype.setEnvironment = function (new_environment) { |
11dbb00f | 83 | environment = new_environment; |
1360a454 D |
84 | }; |
85 | ||
86 | // Get the current config. Optionally for a different environment than currently set | |
cbe80532 | 87 | Config.prototype.get = function (specific_environment) { |
11dbb00f D |
88 | specific_environment = specific_environment || environment; |
89 | ||
0a8a61ad | 90 | return loaded_config[specific_environment] || {}; |
1360a454 D |
91 | }; |
92 | ||
cbe80532 | 93 | module.exports = new Config(); |