1 | 'use strict';
|
2 |
|
3 | const Promise = require('bluebird');
|
4 | const abbrev = require('abbrev');
|
5 |
|
6 | function Console() {
|
7 | this.store = {};
|
8 | this.alias = {};
|
9 | }
|
10 |
|
11 | Console.prototype.get = function(name) {
|
12 | name = name.toLowerCase();
|
13 | return this.store[this.alias[name]];
|
14 | };
|
15 |
|
16 | Console.prototype.list = function() {
|
17 | return this.store;
|
18 | };
|
19 |
|
20 | Console.prototype.register = function(name, desc, options, fn) {
|
21 | if (!name) throw new TypeError('name is required');
|
22 |
|
23 | if (!fn) {
|
24 | if (options) {
|
25 | if (typeof options === 'function') {
|
26 | fn = options;
|
27 |
|
28 | if (typeof desc === 'object') {
|
29 | options = desc;
|
30 | desc = '';
|
31 | } else {
|
32 | options = {};
|
33 | }
|
34 | } else {
|
35 | throw new TypeError('fn must be a function');
|
36 | }
|
37 | } else {
|
38 |
|
39 | if (typeof desc === 'function') {
|
40 | fn = desc;
|
41 | options = {};
|
42 | desc = '';
|
43 | } else {
|
44 | throw new TypeError('fn must be a function');
|
45 | }
|
46 | }
|
47 | }
|
48 |
|
49 | if (fn.length > 1) {
|
50 | fn = Promise.promisify(fn);
|
51 | } else {
|
52 | fn = Promise.method(fn);
|
53 | }
|
54 |
|
55 | const c = fn;
|
56 |
|
57 | this.store[name.toLowerCase()] = c;
|
58 | c.options = options;
|
59 | c.desc = desc;
|
60 |
|
61 | this.alias = abbrev(Object.keys(this.store));
|
62 | };
|
63 |
|
64 | module.exports = Console;
|