Skip to content

Commit 9853d10

Browse files
committedDec 12, 2021
Refactor Command#handlers
1 parent 3c1a9b4 commit 9853d10

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed
 

‎lib/command.js

+24-12
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ const defaultVersionAction = instance => console.log(instance.meta.version);
1010
const lastCommandHost = new WeakMap();
1111
const lastAddedOption = new WeakMap();
1212

13-
const handlers = ['init', 'applyConfig', 'finishContext', 'action'].reduce((res, name) => {
14-
res.initial[name] = name === 'action' ? self : noop;
15-
res.setters[name] = function(fn) {
16-
this.handlers[name] = fn.bind(null);
17-
18-
return this;
19-
};
20-
return res;
21-
}, { initial: {}, setters: {} });
22-
2313
export default class Command {
2414
constructor(usage = '') {
2515
const [name, params] = usage.trim().split(/(\s+.*)$/);
@@ -34,12 +24,34 @@ export default class Command {
3424
help: null
3525
};
3626

37-
this.handlers = { ...handlers.initial };
38-
Object.assign(this, handlers.setters);
27+
this.handlers = {
28+
init: noop,
29+
applyConfig: noop,
30+
finishContext: noop,
31+
action: self
32+
};
3933

4034
this.help();
4135
}
4236

37+
// handlers
38+
init(fn) {
39+
this.handlers.init = fn.bind(null);
40+
return this;
41+
}
42+
applyConfig(fn) {
43+
this.handlers.applyConfig = fn.bind(null);
44+
return this;
45+
}
46+
finishContext(fn) {
47+
this.handlers.finishContext = fn.bind(null);
48+
return this;
49+
}
50+
action(fn) {
51+
this.handlers.action = fn.bind(null);
52+
return this;
53+
}
54+
4355
// definition chaining
4456
extend(fn, ...args) {
4557
fn(this, ...args);

0 commit comments

Comments
 (0)
Please sign in to comment.