Skip to content

Commit 01e47b5

Browse files
committed
Add compiled lib
1 parent 00f2b0b commit 01e47b5

File tree

12 files changed

+735
-1
lines changed

12 files changed

+735
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,4 @@ typings/
130130

131131
# End of https://www.gitignore.io/api/node,intellij,visualstudiocode
132132

133-
lib/
134133
.vscode/
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
'use strict';
2+
3+
var _dec, _class;
4+
5+
var _flowRuntime = require('flow-runtime');
6+
7+
var _flowRuntime2 = _interopRequireDefault(_flowRuntime);
8+
9+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10+
11+
const Command = require('../Command');
12+
13+
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 {
14+
15+
constructor(commandManager) {
16+
this._commandManager = commandManager;
17+
this._prefix = '';
18+
this._middlewares = [];
19+
this._command = '';
20+
this._args = [];
21+
this._parent = null;
22+
this._handler = null;
23+
}
24+
25+
parent(parent) {
26+
this._parent = parent;
27+
return this;
28+
}
29+
30+
middleware(middleware) {
31+
let _middlewareType = _flowRuntime2.default.union(_flowRuntime2.default.function(), _flowRuntime2.default.array(_flowRuntime2.default.function()));
32+
33+
_flowRuntime2.default.param('middleware', _middlewareType).assert(middleware);
34+
35+
if (typeof middleware === 'function') this._middlewares.push(middleware);else if (middleware instanceof Array) this._middlewares = this._middlewares.concat(middleware);
36+
return this;
37+
}
38+
39+
prefix(prefix) {
40+
let _prefixType = _flowRuntime2.default.string();
41+
42+
_flowRuntime2.default.param('prefix', _prefixType).assert(prefix);
43+
44+
if (!this._parent || this.parent && !this._parent.prefix) this._prefix = prefix;
45+
return this;
46+
}
47+
48+
command(cmd) {
49+
let _cmdType = _flowRuntime2.default.string();
50+
51+
_flowRuntime2.default.param('cmd', _cmdType).assert(cmd);
52+
53+
const split = cmd.split(' ');
54+
this._command = split.shift();
55+
split.forEach(arg => {
56+
let optional = false;
57+
let list = false;
58+
let regex = new RegExp('', 'g');
59+
60+
if (arg === '*') arg = '[dynamic...]';
61+
if (arg[0] === '[') optional = true;
62+
63+
arg = arg.slice(1, arg.length - 1);
64+
65+
if (arg.includes(':')) [arg, regex] = arg.split(':');
66+
regex = new RegExp(regex, 'g');
67+
68+
if (arg.endsWith('...')) [list, arg] = [true, arg.replace('...')];
69+
70+
this._args.push({
71+
key: arg,
72+
optional,
73+
list,
74+
regex
75+
});
76+
});
77+
return this;
78+
}
79+
80+
handler(handler) {
81+
let _handlerType = _flowRuntime2.default.function();
82+
83+
_flowRuntime2.default.param('handler', _handlerType).assert(handler);
84+
85+
this._handler = handler;
86+
return this;
87+
}
88+
89+
build() {
90+
return new Command(this._prefix + this._command, this._args, this._middlewares, this._handler);
91+
}
92+
93+
register() {
94+
const cmd = this.build();
95+
if (this._parent) this._parent.sub(cmd);else this._commandManager.register(cmd);
96+
return cmd;
97+
}
98+
99+
}) || _class);
100+
101+
102+
module.exports = CommandBuilder;

lib/Commands/Builders/GroupBuilder.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
3+
var _dec, _class;
4+
5+
var _flowRuntime = require('flow-runtime');
6+
7+
var _flowRuntime2 = _interopRequireDefault(_flowRuntime);
8+
9+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10+
11+
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 {
12+
13+
constructor(commandManager, defaultPrefix) {
14+
this._commandManager = commandManager;
15+
this._middlewares = [];
16+
this._prefix = defaultPrefix;
17+
this._parent = null;
18+
}
19+
20+
parent(parent) {
21+
console.log(parent);
22+
this._parent = parent;
23+
return this;
24+
}
25+
26+
prefix(prefix) {
27+
let _prefixType = _flowRuntime2.default.string();
28+
29+
_flowRuntime2.default.param('prefix', _prefixType).assert(prefix);
30+
31+
if (!this._parent || this.parent && !this._parent.prefix) this._prefix = prefix;
32+
return this;
33+
}
34+
35+
middleware(middleware) {
36+
let _middlewareType = _flowRuntime2.default.union(_flowRuntime2.default.function(), _flowRuntime2.default.array(_flowRuntime2.default.function()));
37+
38+
_flowRuntime2.default.param('middleware', _middlewareType).assert(middleware);
39+
40+
if (typeof middleware === 'function') this._middlewares.push(middleware);else if (middleware instanceof Array) this._middlewares.concat(middleware);
41+
return this;
42+
}
43+
44+
apply(cb) {
45+
let _cbType = _flowRuntime2.default.function();
46+
47+
_flowRuntime2.default.param('cb', _cbType).assert(cb);
48+
49+
this._commandManager.push({
50+
prefix: this._prefix,
51+
middlewares: this._middlewares,
52+
parent: this._parent
53+
});
54+
cb();
55+
this._commandManager.pop();
56+
}
57+
58+
}) || _class);
59+
60+
61+
module.exports = GroupBuilder;

lib/Commands/Command.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
'use strict';
2+
3+
var _dec, _class;
4+
5+
var _flowRuntime = require('flow-runtime');
6+
7+
var _flowRuntime2 = _interopRequireDefault(_flowRuntime);
8+
9+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10+
11+
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"); }); }; }
12+
13+
let Command = (_dec = _flowRuntime2.default.annotate(_flowRuntime2.default.class('Command', Command => {
14+
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')];
15+
})), _dec(_class = class Command {
16+
17+
constructor(command, args, middlewares, handler) {
18+
this._command = command;
19+
this._args = args;
20+
this._middlewares = middlewares;
21+
this._handler = handler;
22+
this._subs = [];
23+
}
24+
25+
call(parser, message, args) {
26+
var _this = this;
27+
28+
return _asyncToGenerator(function* () {
29+
let subCalled = false;
30+
if (args.length > 0) {
31+
_this._subs.forEach(function (sub) {
32+
if (sub.command === args[0]) {
33+
if (_this.callMiddlewares(message)) {
34+
sub.call(message, args.slice(1));
35+
subCalled = true;
36+
}
37+
return;
38+
}
39+
});
40+
}
41+
if (!subCalled) {
42+
const map = new Map();
43+
for (let i = 0; i < _this._args.length; i++) {
44+
let argument = args[i];
45+
46+
if (!_this._args[i].optional && !argument) return;
47+
48+
if (_this._args[i].list) {
49+
argument = args.splice(i);
50+
let match = true;
51+
argument.every(function (val) {
52+
if (_this._args[i].regex.test(val)) return true;else return match = false;
53+
});
54+
if (!match) return;
55+
} else {
56+
const test = _this._args[i].regex.test(argument);
57+
if (!test) return;
58+
if (i + 1 === _this._args.length && i + 1 < args.length) return;
59+
}
60+
61+
if (parser && parser instanceof Array) for (let i = 0; i < parser.length; i++) {
62+
const parse = parser[i];
63+
let cond = false;
64+
if (parse.match instanceof RegExp) cond = parse.match.test(argument);else if (typeof parse.match === 'function') cond = parse.match(message, argument);
65+
if (cond && typeof parse.perform === 'function') {
66+
let res = parse.perform(message, argument);
67+
if (res instanceof Promise) res = yield res;
68+
argument = res;
69+
}
70+
};
71+
72+
map.set(_this._args[i].key, argument);
73+
}
74+
75+
if (_this.callMiddlewares(message, map)) _this._handler(message, map);
76+
}
77+
})();
78+
}
79+
80+
callMiddlewares(message) {
81+
let ret = true;
82+
this._middlewares.every(middleware => {
83+
return !middleware(this, message) ? ret = false : true;
84+
});
85+
return ret;
86+
}
87+
88+
sub(cmd, handler = () => null) {
89+
let _cmdType = _flowRuntime2.default.union(_flowRuntime2.default.string(), _flowRuntime2.default.ref(Command));
90+
91+
let _handlerType = _flowRuntime2.default.function();
92+
93+
_flowRuntime2.default.param('cmd', _cmdType).assert(cmd);
94+
95+
_flowRuntime2.default.param('handler', _handlerType).assert(handler);
96+
97+
const CommandBuilder = require('./Builders/CommandBuilder');
98+
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);
99+
}
100+
101+
get command() {
102+
return this._command;
103+
}
104+
get args() {
105+
return this._args;
106+
}
107+
get middlewares() {
108+
return this._middlewares;
109+
}
110+
get handler() {
111+
return this._handler;
112+
}
113+
get subs() {
114+
return this._subs;
115+
}
116+
117+
}) || _class);
118+
119+
120+
module.exports = Command;

lib/Commands/CommandManager.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
'use strict';
2+
3+
var _dec, _class;
4+
5+
var _flowRuntime = require('flow-runtime');
6+
7+
var _flowRuntime2 = _interopRequireDefault(_flowRuntime);
8+
9+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10+
11+
const GroupBuilder = require('./Builders/GroupBuilder');
12+
const CommandBuilder = require('./Builders/CommandBuilder');
13+
14+
const typeConf = {
15+
parse: []
16+
};
17+
18+
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 {
19+
20+
constructor(config) {
21+
this._config = Object.assign({}, typeConf, config);
22+
this._commands = [];
23+
this._defaultPrefix = '/';
24+
this._groupStack = [];
25+
}
26+
27+
group() {
28+
const group = new GroupBuilder(this, this.defaultPrefix);
29+
if (this._groupStack.length > 0) group.parent(this._groupStack[this._groupStack.length - 1]);
30+
return group;
31+
}
32+
33+
command(command, handler) {
34+
let _commandType = _flowRuntime2.default.string();
35+
36+
let _handlerType = _flowRuntime2.default.function();
37+
38+
_flowRuntime2.default.param('command', _commandType).assert(command);
39+
40+
_flowRuntime2.default.param('handler', _handlerType).assert(handler);
41+
42+
const builder = new CommandBuilder(this);
43+
let prefix = this._defaultPrefix;
44+
this._groupStack.forEach(group => {
45+
prefix = group.prefix || this._defaultPrefix;
46+
if (group.parent) builder.parent(group.parent);
47+
builder.middleware(group.middlewares);
48+
});
49+
return builder.prefix(prefix).command(command).handler(handler);
50+
}
51+
52+
register(command) {
53+
this._commands.push(command);
54+
}
55+
56+
push(group) {
57+
this._groupStack.push(group);
58+
}
59+
60+
pop() {
61+
this._groupStack.pop();
62+
}
63+
64+
get defaultPrefix() {
65+
return this._defaultPrefix;
66+
}
67+
68+
dispatch(message) {
69+
const splitWithQuotes = text => text.match(new RegExp("[^\\s\"']+|\"([^\"]*)\"|'([^']*)'", 'g')).map(val => val.replace(/\"|\'/g, ''));
70+
const lines = splitWithQuotes(message.content);
71+
if (lines.length == 0) return;
72+
this._commands.forEach(cmd => {
73+
if (cmd.command === lines[0]) {
74+
cmd.call(this._config.parse, message, lines.splice(1));
75+
return true;
76+
}
77+
});
78+
return false;
79+
}
80+
81+
}) || _class);
82+
83+
84+
module.exports = CommandManager;

0 commit comments

Comments
 (0)