add expandCommand

master
lub 7 years ago
parent 09b7f91e67
commit f0e11a0289

@ -78,6 +78,42 @@ const findShortcut = (shortcut, objects) => {
return expansion; return expansion;
}; };
const expandCommand = (msg, projects) => {
return new Promise((resolve, reject) => {
const regex = /^!([a-zA-Z0-9]+) ([a-zA-Z0-9]+) ([a-zA-Z0-9]+) ?(.*)$/;
const shortcuts = msg.match(regex);
const expansion = {};
expansion.project = findShortcut(shortcuts[1], projects);
if(expansion.project) {
expansion.project = expansion.project.toLowerCase();
} else {
reject('could not resolve project: ' + shortcuts[1]);
}
expansion.service = findShortcut(shortcuts[2], projects[expansion.project]);
if(expansion.service) {
expansion.service = expansion.service.toLowerCase();
} else {
reject('could not resolve service: ' + shortcuts[2]);
}
expansion.subcommand = findShortcut(shortcuts[3], projects[expansion.project][expansion.service]);
if(expansion.subcommand) {
expansion.subcommand = expansion.subcommand.toLowerCase();
} else {
reject('could not resolve subcommand: ' + shortcuts[4]);
}
// if args are given, write them in command
if(shortcuts[4]) expansion.args = shortcuts[4];
resolve(expansion);
});
};
commands.projects = {} commands.projects = {}
fs.readdirSync('./projects').forEach((file) => { fs.readdirSync('./projects').forEach((file) => {
let project = file.match(/^([a-z]*)\.js$/); let project = file.match(/^([a-z]*)\.js$/);
@ -90,6 +126,7 @@ fs.readdirSync('./projects').forEach((file) => {
commands.projects[project] = require('./projects/' + file); commands.projects[project] = require('./projects/' + file);
}); });
/* //Test code /* //Test code
const commands = {}; const commands = {};
commands['homeserver'] = {}; commands['homeserver'] = {};
@ -110,3 +147,12 @@ commands['imninja']['register'].exec = (args) => {
console.log(fetchCommand("inminja", commands)); // -> imninja console.log(fetchCommand("inminja", commands)); // -> imninja
console.log(fetchCommand("34C21", commands, 4)); // -> {}*/ console.log(fetchCommand("34C21", commands, 4)); // -> {}*/
//console.log(getEditDistance("saturday", "sunday")); //should be 3 //console.log(getEditDistance("saturday", "sunday")); //should be 3
/*
expandCommand('!lubi infra reb saeifsaef', commands.projects)
.then((success) => {
console.log(success);
}, (fail) => {
console.log(fail);
});
*/