-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
735 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,5 +130,4 @@ typings/ | |
|
||
# End of https://www.gitignore.io/api/node,intellij,visualstudiocode | ||
|
||
lib/ | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
'use strict'; | ||
|
||
var _dec, _class; | ||
|
||
var _flowRuntime = require('flow-runtime'); | ||
|
||
var _flowRuntime2 = _interopRequireDefault(_flowRuntime); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
const Command = require('../Command'); | ||
|
||
let CommandBuilder = (_dec = _flowRuntime2.default.annotate(_flowRuntime2.default.class('CommandBuilder', _flowRuntime2.default.method('constructor', _flowRuntime2.default.param('commandManager', _flowRuntime2.default.any())), _flowRuntime2.default.method('parent', _flowRuntime2.default.param('parent', _flowRuntime2.default.any())), _flowRuntime2.default.method('middleware', _flowRuntime2.default.param('middleware', _flowRuntime2.default.union(_flowRuntime2.default.function(), _flowRuntime2.default.array(_flowRuntime2.default.function())))), _flowRuntime2.default.method('prefix', _flowRuntime2.default.param('prefix', _flowRuntime2.default.string())), _flowRuntime2.default.method('command', _flowRuntime2.default.param('cmd', _flowRuntime2.default.string())), _flowRuntime2.default.method('handler', _flowRuntime2.default.param('handler', _flowRuntime2.default.function())), _flowRuntime2.default.method('build'), _flowRuntime2.default.method('register'))), _dec(_class = class CommandBuilder { | ||
|
||
constructor(commandManager) { | ||
this._commandManager = commandManager; | ||
this._prefix = ''; | ||
this._middlewares = []; | ||
this._command = ''; | ||
this._args = []; | ||
this._parent = null; | ||
this._handler = null; | ||
} | ||
|
||
parent(parent) { | ||
this._parent = parent; | ||
return this; | ||
} | ||
|
||
middleware(middleware) { | ||
let _middlewareType = _flowRuntime2.default.union(_flowRuntime2.default.function(), _flowRuntime2.default.array(_flowRuntime2.default.function())); | ||
|
||
_flowRuntime2.default.param('middleware', _middlewareType).assert(middleware); | ||
|
||
if (typeof middleware === 'function') this._middlewares.push(middleware);else if (middleware instanceof Array) this._middlewares = this._middlewares.concat(middleware); | ||
return this; | ||
} | ||
|
||
prefix(prefix) { | ||
let _prefixType = _flowRuntime2.default.string(); | ||
|
||
_flowRuntime2.default.param('prefix', _prefixType).assert(prefix); | ||
|
||
if (!this._parent || this.parent && !this._parent.prefix) this._prefix = prefix; | ||
return this; | ||
} | ||
|
||
command(cmd) { | ||
let _cmdType = _flowRuntime2.default.string(); | ||
|
||
_flowRuntime2.default.param('cmd', _cmdType).assert(cmd); | ||
|
||
const split = cmd.split(' '); | ||
this._command = split.shift(); | ||
split.forEach(arg => { | ||
let optional = false; | ||
let list = false; | ||
let regex = new RegExp('', 'g'); | ||
|
||
if (arg === '*') arg = '[dynamic...]'; | ||
if (arg[0] === '[') optional = true; | ||
|
||
arg = arg.slice(1, arg.length - 1); | ||
|
||
if (arg.includes(':')) [arg, regex] = arg.split(':'); | ||
regex = new RegExp(regex, 'g'); | ||
|
||
if (arg.endsWith('...')) [list, arg] = [true, arg.replace('...')]; | ||
|
||
this._args.push({ | ||
key: arg, | ||
optional, | ||
list, | ||
regex | ||
}); | ||
}); | ||
return this; | ||
} | ||
|
||
handler(handler) { | ||
let _handlerType = _flowRuntime2.default.function(); | ||
|
||
_flowRuntime2.default.param('handler', _handlerType).assert(handler); | ||
|
||
this._handler = handler; | ||
return this; | ||
} | ||
|
||
build() { | ||
return new Command(this._prefix + this._command, this._args, this._middlewares, this._handler); | ||
} | ||
|
||
register() { | ||
const cmd = this.build(); | ||
if (this._parent) this._parent.sub(cmd);else this._commandManager.register(cmd); | ||
return cmd; | ||
} | ||
|
||
}) || _class); | ||
|
||
|
||
module.exports = CommandBuilder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
|
||
var _dec, _class; | ||
|
||
var _flowRuntime = require('flow-runtime'); | ||
|
||
var _flowRuntime2 = _interopRequireDefault(_flowRuntime); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
let GroupBuilder = (_dec = _flowRuntime2.default.annotate(_flowRuntime2.default.class('GroupBuilder', _flowRuntime2.default.method('constructor', _flowRuntime2.default.param('commandManager', _flowRuntime2.default.any()), _flowRuntime2.default.param('defaultPrefix', _flowRuntime2.default.any())), _flowRuntime2.default.method('parent', _flowRuntime2.default.param('parent', _flowRuntime2.default.any())), _flowRuntime2.default.method('prefix', _flowRuntime2.default.param('prefix', _flowRuntime2.default.string())), _flowRuntime2.default.method('middleware', _flowRuntime2.default.param('middleware', _flowRuntime2.default.union(_flowRuntime2.default.function(), _flowRuntime2.default.array(_flowRuntime2.default.function())))), _flowRuntime2.default.method('apply', _flowRuntime2.default.param('cb', _flowRuntime2.default.function())))), _dec(_class = class GroupBuilder { | ||
|
||
constructor(commandManager, defaultPrefix) { | ||
this._commandManager = commandManager; | ||
this._middlewares = []; | ||
this._prefix = defaultPrefix; | ||
this._parent = null; | ||
} | ||
|
||
parent(parent) { | ||
console.log(parent); | ||
this._parent = parent; | ||
return this; | ||
} | ||
|
||
prefix(prefix) { | ||
let _prefixType = _flowRuntime2.default.string(); | ||
|
||
_flowRuntime2.default.param('prefix', _prefixType).assert(prefix); | ||
|
||
if (!this._parent || this.parent && !this._parent.prefix) this._prefix = prefix; | ||
return this; | ||
} | ||
|
||
middleware(middleware) { | ||
let _middlewareType = _flowRuntime2.default.union(_flowRuntime2.default.function(), _flowRuntime2.default.array(_flowRuntime2.default.function())); | ||
|
||
_flowRuntime2.default.param('middleware', _middlewareType).assert(middleware); | ||
|
||
if (typeof middleware === 'function') this._middlewares.push(middleware);else if (middleware instanceof Array) this._middlewares.concat(middleware); | ||
return this; | ||
} | ||
|
||
apply(cb) { | ||
let _cbType = _flowRuntime2.default.function(); | ||
|
||
_flowRuntime2.default.param('cb', _cbType).assert(cb); | ||
|
||
this._commandManager.push({ | ||
prefix: this._prefix, | ||
middlewares: this._middlewares, | ||
parent: this._parent | ||
}); | ||
cb(); | ||
this._commandManager.pop(); | ||
} | ||
|
||
}) || _class); | ||
|
||
|
||
module.exports = GroupBuilder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
'use strict'; | ||
|
||
var _dec, _class; | ||
|
||
var _flowRuntime = require('flow-runtime'); | ||
|
||
var _flowRuntime2 = _interopRequireDefault(_flowRuntime); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } | ||
|
||
let Command = (_dec = _flowRuntime2.default.annotate(_flowRuntime2.default.class('Command', Command => { | ||
return [_flowRuntime2.default.method('constructor', _flowRuntime2.default.param('command', _flowRuntime2.default.any()), _flowRuntime2.default.param('args', _flowRuntime2.default.any()), _flowRuntime2.default.param('middlewares', _flowRuntime2.default.any()), _flowRuntime2.default.param('handler', _flowRuntime2.default.any())), _flowRuntime2.default.method('call', _flowRuntime2.default.param('parser', _flowRuntime2.default.any()), _flowRuntime2.default.param('message', _flowRuntime2.default.any()), _flowRuntime2.default.param('args', _flowRuntime2.default.any())), _flowRuntime2.default.method('callMiddlewares', _flowRuntime2.default.param('message', _flowRuntime2.default.any())), _flowRuntime2.default.method('sub', _flowRuntime2.default.param('cmd', _flowRuntime2.default.union(_flowRuntime2.default.string(), _flowRuntime2.default.ref(Command))), _flowRuntime2.default.param('handler', _flowRuntime2.default.function())), _flowRuntime2.default.method('command'), _flowRuntime2.default.method('args'), _flowRuntime2.default.method('middlewares'), _flowRuntime2.default.method('handler'), _flowRuntime2.default.method('subs')]; | ||
})), _dec(_class = class Command { | ||
|
||
constructor(command, args, middlewares, handler) { | ||
this._command = command; | ||
this._args = args; | ||
this._middlewares = middlewares; | ||
this._handler = handler; | ||
this._subs = []; | ||
} | ||
|
||
call(parser, message, args) { | ||
var _this = this; | ||
|
||
return _asyncToGenerator(function* () { | ||
let subCalled = false; | ||
if (args.length > 0) { | ||
_this._subs.forEach(function (sub) { | ||
if (sub.command === args[0]) { | ||
if (_this.callMiddlewares(message)) { | ||
sub.call(message, args.slice(1)); | ||
subCalled = true; | ||
} | ||
return; | ||
} | ||
}); | ||
} | ||
if (!subCalled) { | ||
const map = new Map(); | ||
for (let i = 0; i < _this._args.length; i++) { | ||
let argument = args[i]; | ||
|
||
if (!_this._args[i].optional && !argument) return; | ||
|
||
if (_this._args[i].list) { | ||
argument = args.splice(i); | ||
let match = true; | ||
argument.every(function (val) { | ||
if (_this._args[i].regex.test(val)) return true;else return match = false; | ||
}); | ||
if (!match) return; | ||
} else { | ||
const test = _this._args[i].regex.test(argument); | ||
if (!test) return; | ||
if (i + 1 === _this._args.length && i + 1 < args.length) return; | ||
} | ||
|
||
if (parser && parser instanceof Array) for (let i = 0; i < parser.length; i++) { | ||
const parse = parser[i]; | ||
let cond = false; | ||
if (parse.match instanceof RegExp) cond = parse.match.test(argument);else if (typeof parse.match === 'function') cond = parse.match(message, argument); | ||
if (cond && typeof parse.perform === 'function') { | ||
let res = parse.perform(message, argument); | ||
if (res instanceof Promise) res = yield res; | ||
argument = res; | ||
} | ||
}; | ||
|
||
map.set(_this._args[i].key, argument); | ||
} | ||
|
||
if (_this.callMiddlewares(message, map)) _this._handler(message, map); | ||
} | ||
})(); | ||
} | ||
|
||
callMiddlewares(message) { | ||
let ret = true; | ||
this._middlewares.every(middleware => { | ||
return !middleware(this, message) ? ret = false : true; | ||
}); | ||
return ret; | ||
} | ||
|
||
sub(cmd, handler = () => null) { | ||
let _cmdType = _flowRuntime2.default.union(_flowRuntime2.default.string(), _flowRuntime2.default.ref(Command)); | ||
|
||
let _handlerType = _flowRuntime2.default.function(); | ||
|
||
_flowRuntime2.default.param('cmd', _cmdType).assert(cmd); | ||
|
||
_flowRuntime2.default.param('handler', _handlerType).assert(handler); | ||
|
||
const CommandBuilder = require('./Builders/CommandBuilder'); | ||
if (cmd instanceof Command) this._subs.push(cmd);else if (typeof cmd === 'string' && typeof handler === 'function') return new CommandBuilder(null).parent(this).command(cmd).handler(handler); | ||
} | ||
|
||
get command() { | ||
return this._command; | ||
} | ||
get args() { | ||
return this._args; | ||
} | ||
get middlewares() { | ||
return this._middlewares; | ||
} | ||
get handler() { | ||
return this._handler; | ||
} | ||
get subs() { | ||
return this._subs; | ||
} | ||
|
||
}) || _class); | ||
|
||
|
||
module.exports = Command; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
'use strict'; | ||
|
||
var _dec, _class; | ||
|
||
var _flowRuntime = require('flow-runtime'); | ||
|
||
var _flowRuntime2 = _interopRequireDefault(_flowRuntime); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
const GroupBuilder = require('./Builders/GroupBuilder'); | ||
const CommandBuilder = require('./Builders/CommandBuilder'); | ||
|
||
const typeConf = { | ||
parse: [] | ||
}; | ||
|
||
let CommandManager = (_dec = _flowRuntime2.default.annotate(_flowRuntime2.default.class('CommandManager', _flowRuntime2.default.method('constructor', _flowRuntime2.default.param('config', _flowRuntime2.default.any())), _flowRuntime2.default.method('group'), _flowRuntime2.default.method('command', _flowRuntime2.default.param('command', _flowRuntime2.default.string()), _flowRuntime2.default.param('handler', _flowRuntime2.default.function())), _flowRuntime2.default.method('register', _flowRuntime2.default.param('command', _flowRuntime2.default.any())), _flowRuntime2.default.method('push', _flowRuntime2.default.param('group', _flowRuntime2.default.any())), _flowRuntime2.default.method('pop'), _flowRuntime2.default.method('defaultPrefix'), _flowRuntime2.default.method('dispatch', _flowRuntime2.default.param('message', _flowRuntime2.default.any())))), _dec(_class = class CommandManager { | ||
|
||
constructor(config) { | ||
this._config = Object.assign({}, typeConf, config); | ||
this._commands = []; | ||
this._defaultPrefix = '/'; | ||
this._groupStack = []; | ||
} | ||
|
||
group() { | ||
const group = new GroupBuilder(this, this.defaultPrefix); | ||
if (this._groupStack.length > 0) group.parent(this._groupStack[this._groupStack.length - 1]); | ||
return group; | ||
} | ||
|
||
command(command, handler) { | ||
let _commandType = _flowRuntime2.default.string(); | ||
|
||
let _handlerType = _flowRuntime2.default.function(); | ||
|
||
_flowRuntime2.default.param('command', _commandType).assert(command); | ||
|
||
_flowRuntime2.default.param('handler', _handlerType).assert(handler); | ||
|
||
const builder = new CommandBuilder(this); | ||
let prefix = this._defaultPrefix; | ||
this._groupStack.forEach(group => { | ||
prefix = group.prefix || this._defaultPrefix; | ||
if (group.parent) builder.parent(group.parent); | ||
builder.middleware(group.middlewares); | ||
}); | ||
return builder.prefix(prefix).command(command).handler(handler); | ||
} | ||
|
||
register(command) { | ||
this._commands.push(command); | ||
} | ||
|
||
push(group) { | ||
this._groupStack.push(group); | ||
} | ||
|
||
pop() { | ||
this._groupStack.pop(); | ||
} | ||
|
||
get defaultPrefix() { | ||
return this._defaultPrefix; | ||
} | ||
|
||
dispatch(message) { | ||
const splitWithQuotes = text => text.match(new RegExp("[^\\s\"']+|\"([^\"]*)\"|'([^']*)'", 'g')).map(val => val.replace(/\"|\'/g, '')); | ||
const lines = splitWithQuotes(message.content); | ||
if (lines.length == 0) return; | ||
this._commands.forEach(cmd => { | ||
if (cmd.command === lines[0]) { | ||
cmd.call(this._config.parse, message, lines.splice(1)); | ||
return true; | ||
} | ||
}); | ||
return false; | ||
} | ||
|
||
}) || _class); | ||
|
||
|
||
module.exports = CommandManager; |
Oops, something went wrong.