From c04f81b2a436c32b71ffde33c039e56fc1c3733c Mon Sep 17 00:00:00 2001 From: Kegan Myers Date: Sat, 9 Aug 2014 18:03:19 -0500 Subject: [PATCH] Allow arguments to command handlers --- README.md | 12 ++++++------ app.js | 24 ++++++++++++++++-------- config.json.example | 17 +++++++++++++---- package.json | 4 ++-- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 41f4d11..52a0607 100644 --- a/README.md +++ b/README.md @@ -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: ```json { - "plugins": [ - "slack-roll-command" - ] + "plugins": { + "slack-roll-command": {} + } } ``` Or if you have plugins of your own, you can do this: ```json { - "local-plugins": [ - "directory/to/plugin.js" - ] + "local-plugins": { + "directory/to/plugin.js": {} + } } ``` diff --git a/app.js b/app.js index 8d7c3fe..947b7d2 100644 --- a/app.js +++ b/app.js @@ -19,29 +19,37 @@ var pluginsToInstall = config.plugins || [], pluginsToLoad = config["local-plugins"] || []; function installPlugins(callback) { - if (pluginsToInstall.length === 0) { + var pluginNames = _.keys(pluginsToInstall); + if (pluginNames.length === 0) { callback([]); return; } var npm = require('npm'); npm.load({}, function (err) { if (err) throw err; - npm.commands["install"](pluginsToInstall, function (err) { + npm.commands["install"](pluginNames, function (err) { if (err) throw err; var loadedPlugins = []; - for (var i = 0; i < pluginsToInstall.length; i++) { - loadedPlugins.push(require(pluginsToInstall[i])); - } + _.forEach(pluginNames, function(name) { + //Load and configure each plugin + loadedPlugins.push(require(name)(pluginsToInstall[name])); + }); callback(loadedPlugins); }); }); } function loadPlugins(callback) { - var loadedPlugins = []; - for (var i = 0; i < pluginsToLoad.length; i++) { - loadedPlugins.push(require(path.resolve(__dirname, pluginsToLoad[i]))); + var pluginNames = _.keys(pluginsToLoad); + if (pluginNames.length === 0) { + callback([]); + return; } + var loadedPlugins = []; + _.forEach(pluginNames, function(path) { + //Load and configure each plugin + loadedPlugins.push(require(path.resolve(__dirname, path))(pluginsToLoad[path])); + }); callback(loadedPlugins); } diff --git a/config.json.example b/config.json.example index b8088d9..1d4c377 100644 --- a/config.json.example +++ b/config.json.example @@ -1,7 +1,16 @@ { - "plugins": [ - "slack-roll-command" - ], - "local-plugins": [], + "plugins": { + "slack-roll-command": {}, + "slack-phrase-command": { + "phrases": [ + "Test phrase 1", + "Test phrase 2" + ], + "command": "test" + } + }, + "local-plugins": { + "./my/local/plugin.js": {} + }, "port": 80 } \ No newline at end of file diff --git a/package.json b/package.json index 207d7bb..1ede2b7 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "slack-command-server", - "version": "0.0.2", - "description": "A slack slash command hander framework", + "version": "0.0.3", + "description": "A slack slash command handler framework", "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1"