@moostjs/event-cli 0.3.23 → 0.3.25

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
@@ -54,4 +54,4 @@ Here are some basic steps to get you started:
54
54
 
55
55
  Don't hesitate to ask for help if you need it. We believe in fostering a friendly and respectful environment for all contributors.
56
56
 
57
- Thank you for your interest in Moostjs. We look forward to building something amazing together!
57
+ Thank you for your interest in Moostjs. We look forward to building something amazing together!
package/dist/index.cjs CHANGED
@@ -7,6 +7,30 @@ function getCliMate() {
7
7
  return moost.getMoostMate();
8
8
  }
9
9
 
10
+ function Cli(path) {
11
+ return getCliMate().decorate('handlers', { path: path?.replace(/\s+/g, '/'), type: 'CLI' }, true);
12
+ }
13
+ function CliAlias(alias) {
14
+ return getCliMate().decorate('cliAliases', alias, true);
15
+ }
16
+ function CliExample(cmd, description) {
17
+ return getCliMate().decorate('cliExamples', { cmd, description }, true);
18
+ }
19
+
20
+ function formatParams(keys) {
21
+ const names = [keys].flat();
22
+ return names.map(n => (n.length === 1 ? `-${n}` : `--${n}`));
23
+ }
24
+
25
+ function CliOption(...keys) {
26
+ const mate = getCliMate();
27
+ return mate.apply(mate.decorate('cliOptionsKeys', keys, false), moost.Resolve(() => eventCli.useCliOption(keys[0]), formatParams(keys).join(', ')));
28
+ }
29
+ function CliGlobalOption(option) {
30
+ const mate = getCliMate();
31
+ return mate.decorate('cliOptions', option, true);
32
+ }
33
+
10
34
  const LOGGER_TITLE = 'moost-cli';
11
35
  const CONTEXT_TYPE = 'CLI';
12
36
  class MoostCli {
@@ -33,7 +57,7 @@ class MoostCli {
33
57
  }
34
58
  }
35
59
  async onNotFound() {
36
- const pathParams = eventCli.useCliContext().store('event').get('pathParams');
60
+ const pathParams = eventCli.useCliContext().store('event').get('pathParams') || [];
37
61
  const response = await moost.defineMoostEventHandler({
38
62
  loggerTitle: LOGGER_TITLE,
39
63
  getIterceptorHandler: () => this.moost?.getGlobalInterceptorHandler(),
@@ -41,15 +65,14 @@ class MoostCli {
41
65
  callControllerMethod: () => undefined,
42
66
  logErrors: this.opts?.debug,
43
67
  })();
44
- if (typeof response === 'undefined') {
68
+ if (response === undefined) {
45
69
  this.cliApp.onUnknownCommand(pathParams);
46
70
  }
47
71
  return response;
48
72
  }
49
73
  onInit(moost) {
50
74
  this.moost = moost;
51
- const boolean = Object
52
- .entries(this.optionTypes)
75
+ const boolean = Object.entries(this.optionTypes)
53
76
  .filter(([_key, val]) => val.length === 1 && val[0] === Boolean)
54
77
  .map(([key, _val]) => key);
55
78
  void this.cliApp.run(undefined, {
@@ -59,8 +82,9 @@ class MoostCli {
59
82
  bindHandler(opts) {
60
83
  let fn;
61
84
  for (const handler of opts.handlers) {
62
- if (handler.type !== 'CLI')
85
+ if (handler.type !== 'CLI') {
63
86
  continue;
87
+ }
64
88
  const path = typeof handler.path === 'string'
65
89
  ? handler.path
66
90
  : typeof opts.method === 'string'
@@ -89,12 +113,16 @@ class MoostCli {
89
113
  [
90
114
  ...(this.opts?.globalCliOptions?.length ? this.opts.globalCliOptions : []),
91
115
  ...(classMeta?.cliOptions || []),
92
- ...(meta?.params ? meta.params.filter((param) => param.cliOptionsKeys?.length > 0).map((param) => ({
93
- keys: param.cliOptionsKeys,
94
- value: typeof param.value === 'string' ? param.value : '',
95
- description: param.description || '',
96
- type: param.type,
97
- })) : []),
116
+ ...(meta?.params
117
+ ? meta.params
118
+ .filter((param) => param.cliOptionsKeys.length > 0)
119
+ .map((param) => ({
120
+ keys: param.cliOptionsKeys,
121
+ value: typeof param.value === 'string' ? param.value : '',
122
+ description: param.description || '',
123
+ type: param.type,
124
+ }))
125
+ : []),
98
126
  ].forEach(o => cliOptions.set(o.keys[0], o));
99
127
  const aliases = [];
100
128
  if (meta?.cliAliases) {
@@ -109,14 +137,15 @@ class MoostCli {
109
137
  if (!this.optionTypes[key]) {
110
138
  this.optionTypes[key] = [];
111
139
  }
112
- if (!(this.optionTypes[key].includes(o.type))) {
140
+ if (!this.optionTypes[key].includes(o.type)) {
113
141
  this.optionTypes[key].push(o.type);
114
142
  }
115
143
  }
116
144
  });
117
145
  const args = {};
118
- meta?.params?.filter(p => p.paramSource === 'ROUTE' && p.description)
119
- .forEach(p => args[p.paramName] = p.description);
146
+ meta?.params
147
+ .filter(p => p.paramSource === 'ROUTE' && p.description)
148
+ .forEach(p => (args[p.paramName] = p.description));
120
149
  const routerBinding = this.cliApp.cli(targetPath, {
121
150
  description: meta?.description || '',
122
151
  options: cliOptionsArray,
@@ -137,52 +166,26 @@ class MoostCli {
137
166
  }
138
167
  const aliasTypes = ['CLI', 'CLI-alias', 'CLI-alias*', 'CLI-alias*'];
139
168
 
140
- function formatParams(keys) {
141
- const names = [keys].flat();
142
- return names.map((n) => (n.length === 1 ? '-' + n : '--' + n));
143
- }
144
-
145
- function CliOption(...keys) {
146
- const mate = getCliMate();
147
- return mate.apply(mate.decorate('cliOptionsKeys', keys, false), moost.Resolve(() => eventCli.useCliOption(keys[0]), formatParams(keys).join(', ')));
148
- }
149
- function CliGlobalOption(option) {
150
- const mate = getCliMate();
151
- return mate.decorate('cliOptions', option, true);
152
- }
153
-
154
- function Cli(path) {
155
- return getCliMate().decorate('handlers', { path: path?.replace(/\s+/g, '/'), type: 'CLI' }, true);
156
- }
157
- function CliAlias(alias) {
158
- return getCliMate().decorate('cliAliases', alias, true);
159
- }
160
- function CliExample(cmd, description) {
161
- return getCliMate().decorate('cliExamples', { cmd, description }, true);
162
- }
163
-
164
- const cliHelpInterceptor = (opts) => {
165
- return moost.defineInterceptorFn(() => {
166
- try {
167
- if (eventCli.useAutoHelp(opts?.helpOptions, opts?.colors)) {
168
- return '';
169
- }
170
- }
171
- catch (e) {
169
+ const cliHelpInterceptor = (opts) => moost.defineInterceptorFn(() => {
170
+ try {
171
+ if (eventCli.useAutoHelp(opts?.helpOptions, opts?.colors)) {
172
+ return '';
172
173
  }
173
- if (opts?.lookupLevel) {
174
- const { getMethod } = moost.useControllerContext();
175
- if (!getMethod()) {
176
- eventCli.useCommandLookupHelp(opts.lookupLevel);
177
- }
174
+ }
175
+ catch (error) {
176
+ }
177
+ if (opts?.lookupLevel) {
178
+ const { getMethod } = moost.useControllerContext();
179
+ if (!getMethod()) {
180
+ eventCli.useCommandLookupHelp(opts.lookupLevel);
178
181
  }
179
- }, moost.TInterceptorPriority.BEFORE_ALL);
180
- };
182
+ }
183
+ }, moost.TInterceptorPriority.BEFORE_ALL);
181
184
  const CliHelpInterceptor = (...opts) => moost.Intercept(cliHelpInterceptor(...opts));
182
185
 
183
186
  Object.defineProperty(exports, "useCliContext", {
184
- enumerable: true,
185
- get: function () { return eventCli.useCliContext; }
187
+ enumerable: true,
188
+ get: function () { return eventCli.useCliContext; }
186
189
  });
187
190
  exports.Cli = Cli;
188
191
  exports.CliAlias = CliAlias;
package/dist/index.d.ts CHANGED
@@ -1,7 +1,77 @@
1
- import * as moost from 'moost';
2
- import { TMoostAdapter, Moost, TMoostAdapterOptions } from 'moost';
3
1
  import { WooksCli, TWooksCliOptions } from '@wooksjs/event-cli';
4
2
  export { useCliContext } from '@wooksjs/event-cli';
3
+ import * as moost from 'moost';
4
+ import { TMoostAdapter, Moost, TMoostAdapterOptions } from 'moost';
5
+
6
+ /**
7
+ * ## Define CLI Command
8
+ * ### @MethodDecorator
9
+ *
10
+ * Command path segments may be separated by / or space.
11
+ *
12
+ * For example the folowing path are interpreted the same:
13
+ * - "command test use:dev :name"
14
+ * - "command/test/use:dev/:name"
15
+ *
16
+ * Where name will become an argument
17
+ *
18
+ * @param path - command path
19
+ * @returns
20
+ */
21
+ declare function Cli(path?: string): MethodDecorator;
22
+ /**
23
+ * ## Define CLI Command Alias
24
+ * ### @MethodDecorator
25
+ *
26
+ * Use it to define alias for @Cli('...') command
27
+ *
28
+ * @param path - command alias path
29
+ * @returns
30
+ */
31
+ declare function CliAlias(alias: string): MethodDecorator;
32
+ /**
33
+ * ## Define CLI Example
34
+ * ### @MethodDecorator
35
+ *
36
+ * Use it to define example for Cli Help display
37
+ *
38
+ * @param path - command alias path
39
+ * @returns
40
+ */
41
+ declare function CliExample(cmd: string, description?: string): MethodDecorator;
42
+
43
+ /**
44
+ * ## Define CLI Option
45
+ * ### @ParameterDecorator
46
+ * Use together with @Description('...') to document cli option
47
+ *
48
+ * ```ts
49
+ * │ @Cli('command')
50
+ * │ command(
51
+ * │ @Description('Test option...')
52
+ * │ @CliOption('test', 't')
53
+ * │ test: boolean,
54
+ * │ ) {
55
+ * │ return `test=${ test }`
56
+ * │ }
57
+ * ```
58
+ *
59
+ * @param keys list of keys (short and long alternatives)
60
+ * @returns
61
+ */
62
+ declare function CliOption(...keys: string[]): ParameterDecorator;
63
+ /**
64
+ * ## Define Global CLI Option
65
+ * ### @ClassDecorator
66
+ * The option described here will appear in every command instructions
67
+ * @param option keys and description of CLI option
68
+ * @returns
69
+ */
70
+ declare function CliGlobalOption(option: {
71
+ keys: string[];
72
+ description?: string;
73
+ value?: string;
74
+ }): ClassDecorator;
5
75
 
6
76
  type TFunction = Function;
7
77
 
@@ -20,11 +90,11 @@ interface TMoostCliOpts {
20
90
  /**
21
91
  * Array of cli options applicable to every cli command
22
92
  */
23
- globalCliOptions?: {
93
+ globalCliOptions?: Array<{
24
94
  keys: string[];
25
95
  description?: string;
26
96
  type?: TFunction;
27
- }[];
97
+ }>;
28
98
  }
29
99
  /**
30
100
  * ## Moost Cli Adapter
@@ -67,76 +137,6 @@ declare class MoostCli implements TMoostAdapter<TCliHandlerMeta> {
67
137
  bindHandler<T extends object = object>(opts: TMoostAdapterOptions<TCliHandlerMeta, T>): void;
68
138
  }
69
139
 
70
- /**
71
- * ## Define CLI Option
72
- * ### @ParameterDecorator
73
- * Use together with @Description('...') to document cli option
74
- *
75
- * ```ts
76
- * │ @Cli('command')
77
- * │ command(
78
- * │ @Description('Test option...')
79
- * │ @CliOption('test', 't')
80
- * │ test: boolean,
81
- * │ ) {
82
- * │ return `test=${ test }`
83
- * │ }
84
- * ```
85
- *
86
- * @param keys list of keys (short and long alternatives)
87
- * @returns
88
- */
89
- declare function CliOption(...keys: string[]): ParameterDecorator;
90
- /**
91
- * ## Define Global CLI Option
92
- * ### @ClassDecorator
93
- * The option described here will appear in every command instructions
94
- * @param option keys and description of CLI option
95
- * @returns
96
- */
97
- declare function CliGlobalOption(option: {
98
- keys: string[];
99
- description?: string;
100
- value?: string;
101
- }): ClassDecorator;
102
-
103
- /**
104
- * ## Define CLI Command
105
- * ### @MethodDecorator
106
- *
107
- * Command path segments may be separated by / or space.
108
- *
109
- * For example the folowing path are interpreted the same:
110
- * - "command test use:dev :name"
111
- * - "command/test/use:dev/:name"
112
- *
113
- * Where name will become an argument
114
- *
115
- * @param path - command path
116
- * @returns
117
- */
118
- declare function Cli(path?: string): MethodDecorator;
119
- /**
120
- * ## Define CLI Command Alias
121
- * ### @MethodDecorator
122
- *
123
- * Use it to define alias for @Cli('...') command
124
- *
125
- * @param path - command alias path
126
- * @returns
127
- */
128
- declare function CliAlias(alias: string): MethodDecorator;
129
- /**
130
- * ## Define CLI Example
131
- * ### @MethodDecorator
132
- *
133
- * Use it to define example for Cli Help display
134
- *
135
- * @param path - command alias path
136
- * @returns
137
- */
138
- declare function CliExample(cmd: string, description?: string): MethodDecorator;
139
-
140
140
  /**
141
141
  * ### Interceptor Factory for CliHelpRenderer
142
142
  *
package/dist/index.mjs CHANGED
@@ -1,11 +1,35 @@
1
- import { getMoostMate, getMoostInfact, defineMoostEventHandler, Resolve, defineInterceptorFn, useControllerContext, TInterceptorPriority, Intercept } from 'moost';
2
- import { WooksCli, createCliApp, useCliContext, useCliOption, useAutoHelp, useCommandLookupHelp } from '@wooksjs/event-cli';
1
+ import { getMoostMate, Resolve, getMoostInfact, defineMoostEventHandler, defineInterceptorFn, useControllerContext, TInterceptorPriority, Intercept } from 'moost';
2
+ import { useCliOption, WooksCli, createCliApp, useCliContext, useAutoHelp, useCommandLookupHelp } from '@wooksjs/event-cli';
3
3
  export { useCliContext } from '@wooksjs/event-cli';
4
4
 
5
5
  function getCliMate() {
6
6
  return getMoostMate();
7
7
  }
8
8
 
9
+ function Cli(path) {
10
+ return getCliMate().decorate('handlers', { path: path?.replace(/\s+/g, '/'), type: 'CLI' }, true);
11
+ }
12
+ function CliAlias(alias) {
13
+ return getCliMate().decorate('cliAliases', alias, true);
14
+ }
15
+ function CliExample(cmd, description) {
16
+ return getCliMate().decorate('cliExamples', { cmd, description }, true);
17
+ }
18
+
19
+ function formatParams(keys) {
20
+ const names = [keys].flat();
21
+ return names.map(n => (n.length === 1 ? `-${n}` : `--${n}`));
22
+ }
23
+
24
+ function CliOption(...keys) {
25
+ const mate = getCliMate();
26
+ return mate.apply(mate.decorate('cliOptionsKeys', keys, false), Resolve(() => useCliOption(keys[0]), formatParams(keys).join(', ')));
27
+ }
28
+ function CliGlobalOption(option) {
29
+ const mate = getCliMate();
30
+ return mate.decorate('cliOptions', option, true);
31
+ }
32
+
9
33
  const LOGGER_TITLE = 'moost-cli';
10
34
  const CONTEXT_TYPE = 'CLI';
11
35
  class MoostCli {
@@ -32,7 +56,7 @@ class MoostCli {
32
56
  }
33
57
  }
34
58
  async onNotFound() {
35
- const pathParams = useCliContext().store('event').get('pathParams');
59
+ const pathParams = useCliContext().store('event').get('pathParams') || [];
36
60
  const response = await defineMoostEventHandler({
37
61
  loggerTitle: LOGGER_TITLE,
38
62
  getIterceptorHandler: () => this.moost?.getGlobalInterceptorHandler(),
@@ -40,15 +64,14 @@ class MoostCli {
40
64
  callControllerMethod: () => undefined,
41
65
  logErrors: this.opts?.debug,
42
66
  })();
43
- if (typeof response === 'undefined') {
67
+ if (response === undefined) {
44
68
  this.cliApp.onUnknownCommand(pathParams);
45
69
  }
46
70
  return response;
47
71
  }
48
72
  onInit(moost) {
49
73
  this.moost = moost;
50
- const boolean = Object
51
- .entries(this.optionTypes)
74
+ const boolean = Object.entries(this.optionTypes)
52
75
  .filter(([_key, val]) => val.length === 1 && val[0] === Boolean)
53
76
  .map(([key, _val]) => key);
54
77
  void this.cliApp.run(undefined, {
@@ -58,8 +81,9 @@ class MoostCli {
58
81
  bindHandler(opts) {
59
82
  let fn;
60
83
  for (const handler of opts.handlers) {
61
- if (handler.type !== 'CLI')
84
+ if (handler.type !== 'CLI') {
62
85
  continue;
86
+ }
63
87
  const path = typeof handler.path === 'string'
64
88
  ? handler.path
65
89
  : typeof opts.method === 'string'
@@ -88,12 +112,16 @@ class MoostCli {
88
112
  [
89
113
  ...(this.opts?.globalCliOptions?.length ? this.opts.globalCliOptions : []),
90
114
  ...(classMeta?.cliOptions || []),
91
- ...(meta?.params ? meta.params.filter((param) => param.cliOptionsKeys?.length > 0).map((param) => ({
92
- keys: param.cliOptionsKeys,
93
- value: typeof param.value === 'string' ? param.value : '',
94
- description: param.description || '',
95
- type: param.type,
96
- })) : []),
115
+ ...(meta?.params
116
+ ? meta.params
117
+ .filter((param) => param.cliOptionsKeys.length > 0)
118
+ .map((param) => ({
119
+ keys: param.cliOptionsKeys,
120
+ value: typeof param.value === 'string' ? param.value : '',
121
+ description: param.description || '',
122
+ type: param.type,
123
+ }))
124
+ : []),
97
125
  ].forEach(o => cliOptions.set(o.keys[0], o));
98
126
  const aliases = [];
99
127
  if (meta?.cliAliases) {
@@ -108,14 +136,15 @@ class MoostCli {
108
136
  if (!this.optionTypes[key]) {
109
137
  this.optionTypes[key] = [];
110
138
  }
111
- if (!(this.optionTypes[key].includes(o.type))) {
139
+ if (!this.optionTypes[key].includes(o.type)) {
112
140
  this.optionTypes[key].push(o.type);
113
141
  }
114
142
  }
115
143
  });
116
144
  const args = {};
117
- meta?.params?.filter(p => p.paramSource === 'ROUTE' && p.description)
118
- .forEach(p => args[p.paramName] = p.description);
145
+ meta?.params
146
+ .filter(p => p.paramSource === 'ROUTE' && p.description)
147
+ .forEach(p => (args[p.paramName] = p.description));
119
148
  const routerBinding = this.cliApp.cli(targetPath, {
120
149
  description: meta?.description || '',
121
150
  options: cliOptionsArray,
@@ -136,47 +165,21 @@ class MoostCli {
136
165
  }
137
166
  const aliasTypes = ['CLI', 'CLI-alias', 'CLI-alias*', 'CLI-alias*'];
138
167
 
139
- function formatParams(keys) {
140
- const names = [keys].flat();
141
- return names.map((n) => (n.length === 1 ? '-' + n : '--' + n));
142
- }
143
-
144
- function CliOption(...keys) {
145
- const mate = getCliMate();
146
- return mate.apply(mate.decorate('cliOptionsKeys', keys, false), Resolve(() => useCliOption(keys[0]), formatParams(keys).join(', ')));
147
- }
148
- function CliGlobalOption(option) {
149
- const mate = getCliMate();
150
- return mate.decorate('cliOptions', option, true);
151
- }
152
-
153
- function Cli(path) {
154
- return getCliMate().decorate('handlers', { path: path?.replace(/\s+/g, '/'), type: 'CLI' }, true);
155
- }
156
- function CliAlias(alias) {
157
- return getCliMate().decorate('cliAliases', alias, true);
158
- }
159
- function CliExample(cmd, description) {
160
- return getCliMate().decorate('cliExamples', { cmd, description }, true);
161
- }
162
-
163
- const cliHelpInterceptor = (opts) => {
164
- return defineInterceptorFn(() => {
165
- try {
166
- if (useAutoHelp(opts?.helpOptions, opts?.colors)) {
167
- return '';
168
- }
169
- }
170
- catch (e) {
168
+ const cliHelpInterceptor = (opts) => defineInterceptorFn(() => {
169
+ try {
170
+ if (useAutoHelp(opts?.helpOptions, opts?.colors)) {
171
+ return '';
171
172
  }
172
- if (opts?.lookupLevel) {
173
- const { getMethod } = useControllerContext();
174
- if (!getMethod()) {
175
- useCommandLookupHelp(opts.lookupLevel);
176
- }
173
+ }
174
+ catch (error) {
175
+ }
176
+ if (opts?.lookupLevel) {
177
+ const { getMethod } = useControllerContext();
178
+ if (!getMethod()) {
179
+ useCommandLookupHelp(opts.lookupLevel);
177
180
  }
178
- }, TInterceptorPriority.BEFORE_ALL);
179
- };
181
+ }
182
+ }, TInterceptorPriority.BEFORE_ALL);
180
183
  const CliHelpInterceptor = (...opts) => Intercept(cliHelpInterceptor(...opts));
181
184
 
182
185
  export { Cli, CliAlias, CliExample, CliGlobalOption, CliHelpInterceptor, CliOption, MoostCli, cliHelpInterceptor };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moostjs/event-cli",
3
- "version": "0.3.23",
3
+ "version": "0.3.25",
4
4
  "description": "@moostjs/event-cli",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -38,9 +38,9 @@
38
38
  "homepage": "https://github.com/moostjs/moostjs/tree/main/packages/event-cli#readme",
39
39
  "peerDependencies": {},
40
40
  "dependencies": {
41
- "moost": "0.3.23",
42
- "wooks": "^0.4.26",
43
- "@wooksjs/event-core": "^0.4.26",
44
- "@wooksjs/event-cli": "^0.4.26"
41
+ "moost": "0.3.25",
42
+ "wooks": "^0.4.30",
43
+ "@wooksjs/event-core": "^0.4.30",
44
+ "@wooksjs/event-cli": "^0.4.30"
45
45
  }
46
46
  }