Allow arguments to command handlers

This commit is contained in:
Kegan Myers 2014-08-09 18:03:19 -05:00
parent 834b00be52
commit c04f81b2a4
4 changed files with 37 additions and 20 deletions

View file

@ -17,17 +17,17 @@ You must define what port for the app to listen on by defining `port: 80`
To use pre-written plugins write your configuration thusly: To use pre-written plugins write your configuration thusly:
```json ```json
{ {
"plugins": [ "plugins": {
"slack-roll-command" "slack-roll-command": {}
] }
} }
``` ```
Or if you have plugins of your own, you can do this: Or if you have plugins of your own, you can do this:
```json ```json
{ {
"local-plugins": [ "local-plugins": {
"directory/to/plugin.js" "directory/to/plugin.js": {}
] }
} }
``` ```

24
app.js
View file

@ -19,29 +19,37 @@ var pluginsToInstall = config.plugins || [],
pluginsToLoad = config["local-plugins"] || []; pluginsToLoad = config["local-plugins"] || [];
function installPlugins(callback) { function installPlugins(callback) {
if (pluginsToInstall.length === 0) { var pluginNames = _.keys(pluginsToInstall);
if (pluginNames.length === 0) {
callback([]); callback([]);
return; return;
} }
var npm = require('npm'); var npm = require('npm');
npm.load({}, function (err) { npm.load({}, function (err) {
if (err) throw err; if (err) throw err;
npm.commands["install"](pluginsToInstall, function (err) { npm.commands["install"](pluginNames, function (err) {
if (err) throw err; if (err) throw err;
var loadedPlugins = []; var loadedPlugins = [];
for (var i = 0; i < pluginsToInstall.length; i++) { _.forEach(pluginNames, function(name) {
loadedPlugins.push(require(pluginsToInstall[i])); //Load and configure each plugin
} loadedPlugins.push(require(name)(pluginsToInstall[name]));
});
callback(loadedPlugins); callback(loadedPlugins);
}); });
}); });
} }
function loadPlugins(callback) { function loadPlugins(callback) {
var loadedPlugins = []; var pluginNames = _.keys(pluginsToLoad);
for (var i = 0; i < pluginsToLoad.length; i++) { if (pluginNames.length === 0) {
loadedPlugins.push(require(path.resolve(__dirname, pluginsToLoad[i]))); callback([]);
return;
} }
var loadedPlugins = [];
_.forEach(pluginNames, function(path) {
//Load and configure each plugin
loadedPlugins.push(require(path.resolve(__dirname, path))(pluginsToLoad[path]));
});
callback(loadedPlugins); callback(loadedPlugins);
} }

View file

@ -1,7 +1,16 @@
{ {
"plugins": [ "plugins": {
"slack-roll-command" "slack-roll-command": {},
], "slack-phrase-command": {
"local-plugins": [], "phrases": [
"Test phrase 1",
"Test phrase 2"
],
"command": "test"
}
},
"local-plugins": {
"./my/local/plugin.js": {}
},
"port": 80 "port": 80
} }

View file

@ -1,7 +1,7 @@
{ {
"name": "slack-command-server", "name": "slack-command-server",
"version": "0.0.2", "version": "0.0.3",
"description": "A slack slash command hander framework", "description": "A slack slash command handler framework",
"main": "app.js", "main": "app.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"