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