@wocker/core 1.0.22-beta.0 → 1.0.22-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  ###### Docker workspace for web projects
4
4
 
5
+ [![npm version](https://img.shields.io/npm/v/@wocker/core.svg)](https://www.npmjs.com/package/@wocker/core)
6
+ [![Publish](https://github.com/kearisp/wocker-core/actions/workflows/publish-latest.yml/badge.svg?event=release)](https://github.com/kearisp/wocker-core/actions/workflows/publish-latest.yml)
7
+ [![License](https://img.shields.io/npm/l/@wocker/core)](https://github.com/kearisp/wocker-core/blob/main/LICENSE)
8
+
9
+ [![npm total downloads](https://img.shields.io/npm/dt/@wocker/core.svg)](https://www.npmjs.com/package/@wocker/core)
10
+ [![bundle size](https://img.shields.io/bundlephobia/minzip/@wocker/core)](https://bundlephobia.com/package/@wocker/core)
11
+ ![Coverage](https://gist.githubusercontent.com/kearisp/f17f46c6332ea3bb043f27b0bddefa9f/raw/coverage-wocker-core-latest.svg)
12
+
5
13
  ## Installation
6
14
 
7
15
  **Note:** It is recommended to install Wocker globally to ensure accessibility from any directory in your terminal.
@@ -33,21 +33,23 @@ class ApplicationContext {
33
33
  cli.command("").action(() => {
34
34
  const module = this.container.getModule(this.module);
35
35
  for (const [, container] of module.controllers) {
36
- const description = container.getDescription();
37
- if (!description) {
36
+ if (!container.description) {
38
37
  continue;
39
38
  }
40
- console.info(`${description}:`);
41
- const routes = container.getRoutes();
42
- const spaceLength = routes.reduce((space, route) => {
43
- return Math.max(space, route.command.length + 2);
39
+ console.info(`${container.description}:`);
40
+ const spaceLength = container.commands.reduce((space, route) => {
41
+ return route.commandNames.reduce((space, command) => {
42
+ return Math.max(space, command.length + 2);
43
+ }, space);
44
44
  }, 0);
45
- for (const route of routes) {
45
+ for (const route of container.commands) {
46
46
  if (!route.description) {
47
47
  continue;
48
48
  }
49
- const space = " ".repeat(Math.max(0, spaceLength - route.command.length));
50
- console.info(` ${route.command} ${space} ${route.description}`);
49
+ for (const commandName of route.commandNames) {
50
+ const space = " ".repeat(Math.max(0, spaceLength - commandName.length));
51
+ console.info(` ${commandName} ${space} ${route.description}`);
52
+ }
51
53
  }
52
54
  console.info("");
53
55
  }
@@ -1,13 +1,16 @@
1
1
  import "reflect-metadata";
2
+ import { CommandInput } from "@kearisp/cli";
2
3
  import { InstanceWrapper } from "./InstanceWrapper";
3
4
  import { Module } from "./Module";
4
5
  import { Type } from "../types/Type";
5
6
  import { Route } from "./Route";
6
- import { RouteCompletion } from "./RouteCompletion";
7
7
  export declare class ControllerWrapper<TInput = any> extends InstanceWrapper {
8
8
  readonly type: Type<TInput>;
9
+ description?: string;
10
+ commands: Route[];
11
+ completions: Route[];
9
12
  constructor(module: Module, type: Type<TInput>);
10
- getDescription(): string | undefined;
11
13
  getRoutes(): Route[];
12
- getCompletions(): RouteCompletion[];
14
+ getCompletionCommands(name: string, command: string): Route[];
15
+ run(route: Route, input: CommandInput): any;
13
16
  }
@@ -2,84 +2,76 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ControllerWrapper = void 0;
4
4
  require("reflect-metadata");
5
- const env_1 = require("../env");
6
5
  const InstanceWrapper_1 = require("./InstanceWrapper");
7
6
  const Route_1 = require("./Route");
8
- const RouteCompletion_1 = require("./RouteCompletion");
9
7
  class ControllerWrapper extends InstanceWrapper_1.InstanceWrapper {
10
8
  constructor(module, type) {
11
9
  super(module, type);
12
- }
13
- getDescription() {
14
- return Reflect.getMetadata("CONTROLLER_DESCRIPTION", this.type);
15
- // || this.type.prototype.constructor.name;
16
- }
17
- getRoutes() {
18
- const routes = [];
10
+ this.commands = [];
11
+ this.completions = [];
19
12
  if (!this.type) {
20
- return [];
13
+ return;
21
14
  }
15
+ this.description = Reflect.getMetadata("CONTROLLER_DESCRIPTION", this.type) || "";
22
16
  for (const method of Object.getOwnPropertyNames(this.type.prototype)) {
23
- const descriptor = Object.getOwnPropertyDescriptor(this.type.prototype, method);
24
- if (!descriptor) {
25
- continue;
17
+ const route = new Route_1.Route(this.type, method);
18
+ if (route.isCommand) {
19
+ this.commands.push(route);
26
20
  }
27
- const commandNames = Reflect.getMetadata(env_1.COMMAND_METADATA, descriptor.value) || [];
28
- if (commandNames.length === 0) {
29
- continue;
21
+ else if (route.isCompletion) {
22
+ this.completions.push(route);
30
23
  }
31
- const argsMeta = Reflect.getMetadata(env_1.ARGS_METADATA, this.type, method) || [];
32
- const designTypes = Reflect.getMetadata(env_1.PARAMTYPES_METADATA, this.type.prototype, method) || [];
33
- const description = Reflect.getMetadata(env_1.COMMAND_DESCRIPTION_METADATA, descriptor.value);
34
- for (const commandName of commandNames) {
35
- const route = new Route_1.Route(method, commandName, argsMeta, designTypes, description);
36
- routes.push(route);
24
+ else {
25
+ // TODO: Log
37
26
  }
38
27
  }
39
- return routes;
40
28
  }
41
- getCompletions() {
42
- const routes = [], allCommandNames = [], allCompletions = [];
29
+ getRoutes() {
30
+ const routes = [];
43
31
  if (!this.type) {
44
- return routes;
32
+ return [];
45
33
  }
46
34
  for (const method of Object.getOwnPropertyNames(this.type.prototype)) {
47
- const descriptor = Object.getOwnPropertyDescriptor(this.type.prototype, method);
48
- if (!descriptor) {
49
- continue;
50
- }
51
- const commandNames = Reflect.getMetadata(env_1.COMMAND_METADATA, descriptor.value) || [];
52
- if (commandNames.length > 0) {
53
- allCommandNames.push(...commandNames);
54
- }
55
- const completionsData = Reflect.getMetadata(env_1.COMPLETION_METADATA, descriptor.value) || [];
56
- if (completionsData.length > 0) {
57
- for (const completionData of completionsData) {
58
- allCompletions.push(Object.assign(Object.assign({}, completionData), { method }));
59
- }
60
- }
61
- }
62
- allCompletions.sort((a, b) => {
63
- return a.command < b.command ? -1 : 1;
64
- });
65
- for (const data of allCompletions) {
66
- const commandNames = data.command
67
- ? [data.command]
68
- : allCommandNames.filter((commandName) => {
69
- return !allCompletions.filter((c) => {
70
- return c.name === data.name && typeof c.command !== "undefined";
71
- }).map((c) => {
72
- return c.method;
73
- }).includes(commandName);
74
- });
75
- const argsMeta = Reflect.getMetadata(env_1.ARGS_METADATA, this.type, data.method) || [];
76
- const designTypes = Reflect.getMetadata(env_1.PARAMTYPES_METADATA, this.type.prototype, data.method) || [];
77
- for (const commandName of commandNames) {
78
- const route = new RouteCompletion_1.RouteCompletion(data.method, data.name, commandName, argsMeta, designTypes);
35
+ const route = new Route_1.Route(this.type, method);
36
+ if (route.isCommand || route.isCompletion) {
79
37
  routes.push(route);
80
38
  }
81
39
  }
82
40
  return routes;
83
41
  }
42
+ getCompletionCommands(name, command) {
43
+ const completions = this.completions.filter((route) => {
44
+ return route.completions.filter((completion) => {
45
+ return completion.name === name && completion.command === command;
46
+ }).length > 0;
47
+ });
48
+ if (completions.length > 0) {
49
+ return completions;
50
+ }
51
+ return this.completions.filter((route) => {
52
+ return route.completions.filter((completion) => {
53
+ return completion.name === name && !completion.command;
54
+ });
55
+ });
56
+ }
57
+ run(route, input) {
58
+ const args = [];
59
+ route.args.forEach((arg, index) => {
60
+ switch (arg.type) {
61
+ case "param":
62
+ args[index] = input.argument(arg.name);
63
+ break;
64
+ case "option":
65
+ if (route.designTypes[index] === Array) {
66
+ args[index] = input.options(arg.name);
67
+ }
68
+ else {
69
+ args[index] = input.option(arg.name);
70
+ }
71
+ break;
72
+ }
73
+ });
74
+ return this.instance[route.method](...args);
75
+ }
84
76
  }
85
77
  exports.ControllerWrapper = ControllerWrapper;
@@ -1,10 +1,23 @@
1
- import { CommandInput } from "@kearisp/cli";
1
+ import { ArgMeta } from "../types/ArgMeta";
2
+ import { Type } from "../types/Type";
2
3
  export declare class Route {
4
+ readonly type: Type;
3
5
  readonly method: string;
4
- readonly command: string;
5
- readonly argsMeta: any[];
6
- readonly designTypes: any[];
7
- readonly description?: string | undefined;
8
- constructor(method: string, command: string, argsMeta: any[], designTypes: any[], description?: string | undefined);
9
- getArgs(input: CommandInput): any[];
6
+ description?: string;
7
+ args: {
8
+ type: "param" | "option";
9
+ name: string;
10
+ params: any;
11
+ }[];
12
+ argsMeta: ArgMeta[];
13
+ designTypes: any[];
14
+ commandNames: string[];
15
+ completions: {
16
+ name: string;
17
+ command?: string;
18
+ }[];
19
+ constructor(type: Type, method: string);
20
+ get isCommand(): boolean;
21
+ get isCompletion(): boolean;
22
+ getArgType(index: number): string | undefined;
10
23
  }
package/lib/core/Route.js CHANGED
@@ -1,32 +1,64 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Route = void 0;
4
+ const env_1 = require("../env");
4
5
  class Route {
5
- constructor(method, command, argsMeta, designTypes, description) {
6
+ constructor(type, method) {
7
+ this.type = type;
6
8
  this.method = method;
7
- this.command = command;
9
+ this.args = [];
10
+ this.argsMeta = [];
11
+ this.designTypes = [];
12
+ this.commandNames = [];
13
+ this.completions = [];
14
+ const descriptor = Object.getOwnPropertyDescriptor(this.type.prototype, this.method);
15
+ if (!descriptor) {
16
+ return;
17
+ }
18
+ this.description = Reflect.getMetadata(env_1.COMMAND_DESCRIPTION_METADATA, descriptor.value) || "";
19
+ this.commandNames = Reflect.getMetadata(env_1.COMMAND_METADATA, descriptor.value) || [];
20
+ this.completions = Reflect.getMetadata(env_1.COMPLETION_METADATA, descriptor.value) || [];
21
+ const argsMeta = Reflect.getMetadata(env_1.ARGS_METADATA, this.type, this.method) || [], argsAliases = Reflect.getMetadata(env_1.ALIAS_METADATA, this.type, this.method) || {}, argsDescription = Reflect.getMetadata(env_1.ARGS_DESCRIPTION_METADATA, this.type, method) || {}, designTypes = Reflect.getMetadata(env_1.PARAMTYPES_METADATA, this.type.prototype, this.method) || [];
8
22
  this.argsMeta = argsMeta;
9
23
  this.designTypes = designTypes;
10
- this.description = description;
11
- }
12
- getArgs(input) {
13
- const args = [];
14
- this.argsMeta.forEach((argMeta) => {
15
- switch (argMeta.type) {
16
- case "param":
17
- args[argMeta.index] = input.argument(argMeta.name);
18
- break;
19
- case "option":
20
- if (this.designTypes[argMeta.index] === Array) {
21
- args[argMeta.index] = input.options(argMeta.name);
22
- }
23
- else {
24
- args[argMeta.index] = input.option(argMeta.name);
24
+ for (let i = 0; i < argsMeta.length; i++) {
25
+ const argMeta = argsMeta[i];
26
+ if (argMeta.type === "param") {
27
+ this.args[argMeta.index] = {
28
+ type: argMeta.type,
29
+ name: argMeta.name,
30
+ params: {
31
+ description: argsDescription[argMeta.index] || ""
25
32
  }
26
- break;
33
+ };
34
+ }
35
+ if (argMeta.type === "option") {
36
+ const { type, alias, description } = argMeta.params || {};
37
+ this.args[argMeta.index] = {
38
+ type: argMeta.type,
39
+ name: argMeta.name,
40
+ params: Object.assign(Object.assign({}, argMeta.params), { type: this.getArgType(argMeta.index) || type, alias: argsAliases[argMeta.index] || alias, description: argsDescription[argMeta.index] || description })
41
+ };
27
42
  }
28
- });
29
- return args;
43
+ }
44
+ }
45
+ get isCommand() {
46
+ return this.commandNames.length > 0;
47
+ }
48
+ get isCompletion() {
49
+ return this.completions.length > 0;
50
+ }
51
+ getArgType(index) {
52
+ switch (this.designTypes[index]) {
53
+ case String:
54
+ return "string";
55
+ case Boolean:
56
+ return "boolean";
57
+ case Number:
58
+ return "number";
59
+ default:
60
+ return undefined;
61
+ }
30
62
  }
31
63
  }
32
64
  exports.Route = Route;
@@ -94,26 +94,30 @@ class Scanner {
94
94
  }
95
95
  }
96
96
  scanControllerRoutes(cli, wrapper) {
97
- for (const route of wrapper.getRoutes()) {
98
- const command = cli.command(route.command);
99
- if (route.description) {
100
- command.help({
101
- description: route.description
97
+ for (const route of wrapper.commands) {
98
+ for (const commandName of route.commandNames) {
99
+ const command = cli.command(commandName);
100
+ if (route.description) {
101
+ command.help({
102
+ description: route.description
103
+ });
104
+ }
105
+ command.action((input) => {
106
+ return wrapper.run(route, input);
102
107
  });
103
- }
104
- route.argsMeta.forEach((argMeta) => {
105
- if (argMeta.type === "option") {
106
- command.option(argMeta.name, argMeta.params);
108
+ for (const arg of route.args) {
109
+ if (arg.type === "option") {
110
+ command.option(arg.name, arg.params);
111
+ }
112
+ command.completion(arg.name, (input) => {
113
+ const [completion] = wrapper.getCompletionCommands(arg.name, commandName);
114
+ if (!completion) {
115
+ return [];
116
+ }
117
+ return wrapper.run(completion, input);
118
+ });
107
119
  }
108
- });
109
- command.action((input) => {
110
- return wrapper.instance[route.method](...route.getArgs(input));
111
- });
112
- }
113
- for (const completion of wrapper.getCompletions()) {
114
- cli.command(completion.command).completion(completion.name, (input) => {
115
- return wrapper.instance[completion.method](...completion.getArgs(input));
116
- });
120
+ }
117
121
  }
118
122
  }
119
123
  scanDynamicModules() {
@@ -6,11 +6,11 @@ const env_1 = require("../env");
6
6
  const Completion = (name, command) => {
7
7
  return (_target, _propertyKey, descriptor) => {
8
8
  Reflect.defineMetadata(env_1.COMPLETION_METADATA, [
9
- ...Reflect.getMetadata(env_1.COMPLETION_METADATA, descriptor.value) || [],
10
9
  {
11
10
  name,
12
11
  command
13
- }
12
+ },
13
+ ...Reflect.getMetadata(env_1.COMPLETION_METADATA, descriptor.value) || []
14
14
  ], descriptor.value);
15
15
  };
16
16
  };
@@ -24,13 +24,7 @@ const Description = (description) => {
24
24
  return;
25
25
  }
26
26
  // Parameter
27
- Reflect.defineMetadata(env_1.COMMAND_DESCRIPTION_METADATA, [
28
- {
29
- index: descriptorOrIndex,
30
- description
31
- },
32
- ...Reflect.getMetadata(env_1.COMMAND_DESCRIPTION_METADATA, target.constructor, propertyKey) || []
33
- ], target.constructor, propertyKey);
27
+ Reflect.defineMetadata(env_1.ARGS_DESCRIPTION_METADATA, Object.assign(Object.assign({}, Reflect.getMetadata(env_1.ARGS_DESCRIPTION_METADATA, target.constructor, propertyKey) || {}), { [descriptorOrIndex]: description }), target.constructor, propertyKey);
34
28
  };
35
29
  };
36
30
  exports.Description = Description;
@@ -1,5 +1,5 @@
1
1
  import "reflect-metadata";
2
2
  import { Option as O } from "@kearisp/cli";
3
- type Params = Omit<O, "name">;
4
- export declare const Option: (name: string, params?: Partial<Params>) => ParameterDecorator;
3
+ type AliasOrParams = string | Omit<O, "name">;
4
+ export declare const Option: (name: string, params?: AliasOrParams) => ParameterDecorator;
5
5
  export {};
@@ -9,13 +9,13 @@ const Option = (name, params) => {
9
9
  return;
10
10
  }
11
11
  Reflect.defineMetadata(env_1.ARGS_METADATA, [
12
- ...Reflect.getMetadata(env_1.ARGS_METADATA, target.constructor, key) || [],
13
12
  {
14
13
  type: "option",
15
14
  name,
16
- params,
15
+ params: typeof params === "string" ? { alias: params } : params,
17
16
  index
18
- }
17
+ },
18
+ ...Reflect.getMetadata(env_1.ARGS_METADATA, target.constructor, key) || []
19
19
  ], target.constructor, key);
20
20
  };
21
21
  };
package/lib/env.d.ts CHANGED
@@ -6,6 +6,8 @@ export declare const COMMAND_METADATA = "command";
6
6
  export declare const COMMAND_DESCRIPTION_METADATA = "__COMMAND_DESCRIPTION__";
7
7
  export declare const COMPLETION_METADATA = "completion";
8
8
  export declare const ARGS_METADATA = "__ARGS__";
9
+ export declare const ALIAS_METADATA = "__ALIAS_METADATA__";
10
+ export declare const ARGS_DESCRIPTION_METADATA = "__ARGS_DESCRIPTION__";
9
11
  export declare const OPTION_META = "__OPTION_META__";
10
12
  export declare const PARAMTYPES_METADATA = "design:paramtypes";
11
13
  export declare const SELF_DECLARED_DEPS_METADATA = "self:paramtypes";
package/lib/env.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MODULE_METADATA = exports.PLUGIN_NAME_METADATA = exports.PLUGIN_DIR_KEY = exports.INJECT_TOKEN_METADATA = exports.SELF_DECLARED_DEPS_METADATA = exports.PARAMTYPES_METADATA = exports.OPTION_META = exports.ARGS_METADATA = exports.COMPLETION_METADATA = exports.COMMAND_DESCRIPTION_METADATA = exports.COMMAND_METADATA = exports.ARGS_META = exports.INJECTABLE_WATERMARK = exports.IS_MODULE_METADATA = exports.IS_GLOBAL_METADATA = void 0;
3
+ exports.MODULE_METADATA = exports.PLUGIN_NAME_METADATA = exports.PLUGIN_DIR_KEY = exports.INJECT_TOKEN_METADATA = exports.SELF_DECLARED_DEPS_METADATA = exports.PARAMTYPES_METADATA = exports.OPTION_META = exports.ARGS_DESCRIPTION_METADATA = exports.ALIAS_METADATA = exports.ARGS_METADATA = exports.COMPLETION_METADATA = exports.COMMAND_DESCRIPTION_METADATA = exports.COMMAND_METADATA = exports.ARGS_META = exports.INJECTABLE_WATERMARK = exports.IS_MODULE_METADATA = exports.IS_GLOBAL_METADATA = void 0;
4
4
  exports.IS_GLOBAL_METADATA = "IS_GLOBAL";
5
5
  exports.IS_MODULE_METADATA = "isModule";
6
6
  exports.INJECTABLE_WATERMARK = "__injectable__";
@@ -9,6 +9,8 @@ exports.COMMAND_METADATA = "command";
9
9
  exports.COMMAND_DESCRIPTION_METADATA = "__COMMAND_DESCRIPTION__";
10
10
  exports.COMPLETION_METADATA = "completion";
11
11
  exports.ARGS_METADATA = "__ARGS__";
12
+ exports.ALIAS_METADATA = "__ALIAS_METADATA__";
13
+ exports.ARGS_DESCRIPTION_METADATA = "__ARGS_DESCRIPTION__";
12
14
  exports.OPTION_META = "__OPTION_META__";
13
15
  exports.PARAMTYPES_METADATA = "design:paramtypes";
14
16
  exports.SELF_DECLARED_DEPS_METADATA = "self:paramtypes";
@@ -0,0 +1,6 @@
1
+ export type ArgMeta = {
2
+ index: number;
3
+ type: "param" | "option";
4
+ name: string;
5
+ params: any;
6
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wocker/core",
3
- "version": "1.0.22-beta.0",
3
+ "version": "1.0.22-beta.1",
4
4
  "author": "Kris Papercut <krispcut@gmail.com>",
5
5
  "description": "Core of the Wocker",
6
6
  "license": "MIT",
@@ -1,5 +0,0 @@
1
- import { Route } from "./Route";
2
- export declare class RouteCompletion extends Route {
3
- readonly name: string;
4
- constructor(method: string, name: string, command: string, argsMeta: any[], designTypes: any[]);
5
- }
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RouteCompletion = void 0;
4
- const Route_1 = require("./Route");
5
- class RouteCompletion extends Route_1.Route {
6
- constructor(method, name, command, argsMeta, designTypes) {
7
- super(method, command, argsMeta, designTypes);
8
- this.name = name;
9
- }
10
- }
11
- exports.RouteCompletion = RouteCompletion;