-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (51 loc) · 1.49 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict';
const irc = require('irc');
const fs = require('fs');
const watch = require('watch');
let handlers = require('./lib/handlers');
const NODE_ENV = process.env.NODE_ENV || 'development';
let config = loadEnv();
// @TODO suport all configurations
const client = new irc.Client(
config.server,
config.botNick,
{
channels: config.channels,
password: config.password || null
}
);
// Load env variables based on filename (duh~!?)
function loadEnv() {
try {
return JSON.parse(fs.readFileSync(`./config/${NODE_ENV}.json`));
} catch (err) {
console.error(err);
process.exit();
}
}
// Clean and (re)bind all listeners to irc client
function bindListeners() {
console.log('Listeners Binded');
Object.keys(handlers).forEach(key => {
client.removeAllListeners(key);
client.addListener(key, handlers[key]);
});
};
// Creating a watcher to monitore files
watch.createMonitor('./lib', (monitor) => {
bindListeners();
monitor.on('changed', () => reloadModuleCache());
});
// Sorcery to clean and reload require cache
function reloadModuleCache() {
fs.readdir('./lib', (err, files) => {
files.forEach(deleteFileCache);
console.log('All Modules Reloaded');
handlers = require('./lib/handlers');
bindListeners();
});
function deleteFileCache(file) {
console.log(`Delete Module Cache: ${file}`);
delete require.cache[require.resolve(`./lib/${file}`)];
}
}