kulmapaikka-ircbot/plugins/pluginManager.js

52 lines
1.2 KiB
JavaScript

var logger = require('winston');
var fs = require('fs');
var Promise = require('promise');
class PluginManager {
constructor(config) {
this.enabledPlugins = [];
this.plugins = [];
this.config = config;
var that = this;
config.plugins.forEach(function(pluginName) {
logger.info('checking ' + pluginName + '...');
if(fs.existsSync('./plugins/'+pluginName.toLowerCase()+'.js')) {
logger.info('* Exists. Enabling...');
that.enabledPlugins.push(pluginName.toLowerCase());
}
});
}
loadAllPlugins() {
var that = this;
this.enabledPlugins.forEach(function(pluginName) {
var Plugin = require('./' + pluginName + '.js');
var plugin = new Plugin(that.config);
that.plugins.push(plugin);
});
}
testPlugins(input) {
var promises = [];
for(let i = 0; i < this.plugins.length; i++) {
var plugin = this.plugins[i];
if(plugin.test(input)) {
logger.info('* Plugin ' + plugin.name + ' reported hit');
promises.push(plugin.result(input));
}
}
return Promise.all(promises);
}
}
// export default BasePlugin;
module.exports = PluginManager;