Skip to content

Commit 8d524fc

Browse files
committed
refactor: function command structure change, include validators in plugins
1 parent 65c2e84 commit 8d524fc

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

packages/core/__tests__/helpers/cmdCreators.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ export const newCmdObj = (name, props = {}) => ({
33
run: () => {},
44
...props,
55
});
6-
export const newCmdFn = (name, props = {}) => (cmd: any) => {
7-
cmd.name = name;
8-
cmd.run = () => {};
9-
Object.keys(props).forEach((k: string) => {
10-
cmd[k] = props[k];
11-
});
6+
export const newCmdFn = (name, props = {}) => () => {
7+
return {
8+
name,
9+
run: () => {},
10+
...props,
11+
};
1212
};
1313
export const newCmdClass = (name, props = {}) => {
1414
return class CustomCommand {

packages/core/src/command.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ class LesyCommand {
139139
});
140140
});
141141
} else {
142-
cmdObj = { ...this.defaultProps, ...cmd };
143-
cmd(cmdObj);
142+
cmdObj = { ...this.defaultProps, ...cmd() };
144143
}
145144

146145
return this.formatNames(cmdObj);

packages/core/src/core.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { LesyCommand } from "./command";
2-
import { LesyFeature } from "./feature";
32
import { LesyLoader } from "./loader";
43
import { LesyMiddleware } from "./middleware";
54
import { MiddlewareContext, MiddlewarePlacement as $, State } from "./model";
@@ -23,14 +22,17 @@ class LesyCoreClass {
2322
config = {},
2423
}): Promise<LesyCoreClass> {
2524
this.loader = new LesyLoader(
26-
{ commands, features, middlewares, plugins },
25+
{ commands, features, middlewares, validators, plugins },
2726
root,
2827
);
2928
// todo: store plugin config under config.plugins
3029
for (const prop in this.loader.pluginConfigs) {
3130
config[prop] = this.loader.pluginConfigs[prop];
3231
}
3332

33+
// tslint:disable-next-line: no-parameter-reassignment
34+
validators = this.loader.validators;
35+
3436
this.mwCtrl = this.loader.mwCtrl;
3537
this.state = { root, utils, validators, config };
3638
if (this.mwCtrl) await this.hook($.INIT);

packages/core/src/loader.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,26 @@ class LesyLoader {
1414
commands: any[] = [];
1515
middlewares: any[] = [];
1616
features: any[] = [];
17+
validators: any[] = [];
1718

1819
constructor(
1920
{
2021
commands = [],
2122
features = [],
2223
middlewares = [],
2324
plugins = [],
25+
validators = [],
2426
}: {
2527
commands?: Command[] | string[];
2628
features?: string[];
2729
middlewares?: string[];
30+
validators?: any[];
2831
plugins?: Plugin[];
2932
},
3033
root: string,
3134
) {
3235
this.root = root;
33-
const corePlugin = { commands, middlewares, features };
36+
const corePlugin = { commands, middlewares, features, validators };
3437
this.loadPlugins([corePlugin, ...plugins]);
3538
}
3639

@@ -57,10 +60,12 @@ class LesyLoader {
5760
commands = [],
5861
middlewares = [],
5962
features = [],
63+
validators = [],
6064
}): void {
6165
this.commands = this.commands.concat(commands);
6266
this.middlewares = this.middlewares.concat(middlewares);
6367
this.features = this.features.concat(features);
68+
this.validators = this.validators.concat(validators);
6469
}
6570
loadPluginFromFile(path: string): void {
6671
let plugin = this.getModuleFromFile(path);

0 commit comments

Comments
 (0)