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:
```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": {}
}
}
```

24
app.js
View File

@ -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);
}

View File

@ -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
}

View File

@ -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"