diff --git a/lib/command.js b/lib/command.js index 7248123..cc04914 100644 --- a/lib/command.js +++ b/lib/command.js @@ -10,16 +10,6 @@ const defaultVersionAction = instance => console.log(instance.meta.version); const lastCommandHost = new WeakMap(); const lastAddedOption = new WeakMap(); -const handlers = ['init', 'applyConfig', 'finishContext', 'action'].reduce((res, name) => { - res.initial[name] = name === 'action' ? self : noop; - res.setters[name] = function(fn) { - this.handlers[name] = fn.bind(null); - - return this; - }; - return res; -}, { initial: {}, setters: {} }); - export default class Command { constructor(usage = '') { const [name, params] = usage.trim().split(/(\s+.*)$/); @@ -34,12 +24,34 @@ export default class Command { help: null }; - this.handlers = { ...handlers.initial }; - Object.assign(this, handlers.setters); + this.handlers = { + init: noop, + applyConfig: noop, + finishContext: noop, + action: self + }; this.help(); } + // handlers + init(fn) { + this.handlers.init = fn.bind(null); + return this; + } + applyConfig(fn) { + this.handlers.applyConfig = fn.bind(null); + return this; + } + finishContext(fn) { + this.handlers.finishContext = fn.bind(null); + return this; + } + action(fn) { + this.handlers.action = fn.bind(null); + return this; + } + // definition chaining extend(fn, ...args) { fn(this, ...args);