@moostjs/event-cli 0.2.28 → 0.2.30

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/dist/index.cjs CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  var moost = require('moost');
4
4
  var eventCli = require('@wooksjs/event-cli');
5
- var cliHelp = require('@prostojs/cli-help');
6
5
 
7
6
  /******************************************************************************
8
7
  Copyright (c) Microsoft Corporation.
@@ -35,34 +34,6 @@ function getCliMate() {
35
34
  return moost.getMoostMate();
36
35
  }
37
36
 
38
- /**
39
- * ### setCliHelpForEvent
40
- * Used internally to set CliHelpRenderer instance for an event state
41
- * @param cliHelp CliHelpRenderer
42
- */
43
- function setCliHelpForEvent(cliHelp) {
44
- eventCli.useCliContext().store('event').set('cliHelp', cliHelp);
45
- }
46
- /**
47
- * ## useCliHelp
48
- * ### Composable
49
- * ```js
50
- * // example of printing cli instructions
51
- * const { print } = useCliHelp()
52
- * print(true)
53
- * ```
54
- * @returns
55
- */
56
- function useCliHelp() {
57
- const event = eventCli.useCliContext().store('event');
58
- const getCliHelp = () => event.get('cliHelp');
59
- return {
60
- getCliHelp,
61
- render: (width, withColors) => getCliHelp().render(event.get('pathParams').join(' '), width, withColors),
62
- print: (withColors) => getCliHelp().print(event.get('pathParams').join(' '), withColors),
63
- };
64
- }
65
-
66
37
  const LOGGER_TITLE = 'moost-cli';
67
38
  const CONTEXT_TYPE = 'CLI';
68
39
  /**
@@ -110,16 +81,6 @@ class MoostCli {
110
81
  onNotFound: this.onNotFound.bind(this),
111
82
  });
112
83
  }
113
- const cliHelpOpts = opts === null || opts === void 0 ? void 0 : opts.cliHelp;
114
- if (cliHelpOpts && cliHelpOpts instanceof cliHelp.CliHelpRenderer) {
115
- this.cliHelp = cliHelpOpts;
116
- }
117
- else if (cliHelpOpts) {
118
- this.cliHelp = new cliHelp.CliHelpRenderer(cliHelpOpts);
119
- }
120
- else {
121
- this.cliHelp = new cliHelp.CliHelpRenderer();
122
- }
123
84
  if (!(opts === null || opts === void 0 ? void 0 : opts.debug)) {
124
85
  moost.getMoostInfact().silent(true);
125
86
  }
@@ -134,9 +95,6 @@ class MoostCli {
134
95
  getControllerInstance: () => this.moost,
135
96
  callControllerMethod: () => undefined,
136
97
  logErrors: (_a = this.opts) === null || _a === void 0 ? void 0 : _a.debug,
137
- hooks: {
138
- init: () => setCliHelpForEvent(this.cliHelp),
139
- },
140
98
  })();
141
99
  if (typeof response === 'undefined') {
142
100
  this.cliApp.onUnknownCommand(pathParams);
@@ -149,7 +107,7 @@ class MoostCli {
149
107
  void this.cliApp.run();
150
108
  }
151
109
  bindHandler(opts) {
152
- var _a, _b, _c;
110
+ var _a, _b, _c, _d, _e;
153
111
  let fn;
154
112
  for (const handler of opts.handlers) {
155
113
  if (handler.type !== 'CLI')
@@ -159,11 +117,12 @@ class MoostCli {
159
117
  : typeof opts.method === 'string'
160
118
  ? opts.method
161
119
  : '';
162
- const targetPath = `${opts.prefix.replace(/\s+/g, '/') || ''}/${path}`
120
+ const prefix = opts.prefix.replace(/\s+/g, '/') || '';
121
+ const makePath = (p) => `${prefix}/${p}`
163
122
  .replace(/\/\/+/g, '/')
164
123
  // avoid interpreting "cmd:tail" as "cmd/:tail"
165
- .replace(/\/\\:/g, '\\:');
166
- let cliCommand = '';
124
+ .replace(/\/\\:/g, '\\:')
125
+ .replace(/^\/+/g, '');
167
126
  if (!fn) {
168
127
  fn = moost.defineMoostEventHandler({
169
128
  contextType: CONTEXT_TYPE,
@@ -173,37 +132,48 @@ class MoostCli {
173
132
  controllerMethod: opts.method,
174
133
  resolveArgs: opts.resolveArgs,
175
134
  logErrors: (_a = this.opts) === null || _a === void 0 ? void 0 : _a.debug,
176
- hooks: {
177
- init: () => setCliHelpForEvent(this.cliHelp),
178
- },
179
135
  });
180
136
  }
181
- const { getArgs, getStaticPart } = this.cliApp.cli(targetPath, fn);
137
+ const targetPath = makePath(path);
182
138
  const meta = getCliMate().read(opts.fakeInstance, opts.method);
183
- const args = {};
184
- getArgs().forEach(a => {
185
- var _a;
186
- const argParam = (_a = meta === null || meta === void 0 ? void 0 : meta.params) === null || _a === void 0 ? void 0 : _a.find(p => p.label === a && p.description);
187
- args[a] = (argParam === null || argParam === void 0 ? void 0 : argParam.description) || '';
188
- });
189
- cliCommand = getStaticPart().replace(/\//g, ' ').trim();
190
- this.cliHelp.addEntry({
191
- description: (meta === null || meta === void 0 ? void 0 : meta.description) || '',
192
- command: cliCommand,
193
- options: ((_b = meta === null || meta === void 0 ? void 0 : meta.params) === null || _b === void 0 ? void 0 : _b.filter(param => !!param.cliParamKeys && param.cliParamKeys.length > 0).map(param => ({
194
- keys: param.cliParamKeys,
139
+ const classMeta = getCliMate().read(opts.fakeInstance);
140
+ const cliOptions = new Map();
141
+ [
142
+ ...(((_c = (_b = this.opts) === null || _b === void 0 ? void 0 : _b.globalCliOptions) === null || _c === void 0 ? void 0 : _c.length) ? this.opts.globalCliOptions : []),
143
+ ...((classMeta === null || classMeta === void 0 ? void 0 : classMeta.cliOptions) || []),
144
+ ...(((_d = meta === null || meta === void 0 ? void 0 : meta.params) === null || _d === void 0 ? void 0 : _d.filter(param => !!param.cliOptionsKeys && param.cliOptionsKeys.length > 0).map(param => ({
145
+ keys: param.cliOptionsKeys,
195
146
  value: typeof param.value === 'string' ? param.value : '',
196
147
  description: param.description || '',
197
- }))) || [],
148
+ }))) || []),
149
+ ].forEach(o => cliOptions.set(o.keys[0], o));
150
+ const aliases = [];
151
+ if (meta === null || meta === void 0 ? void 0 : meta.cliAliases) {
152
+ for (const alias of meta.cliAliases) {
153
+ const targetPath = makePath(alias);
154
+ aliases.push(targetPath);
155
+ }
156
+ }
157
+ const args = {};
158
+ (_e = meta === null || meta === void 0 ? void 0 : meta.params) === null || _e === void 0 ? void 0 : _e.filter(p => p.isRouteParam && p.description).forEach(p => args[p.isRouteParam] = p.description);
159
+ this.cliApp.cli(targetPath, {
160
+ description: (meta === null || meta === void 0 ? void 0 : meta.description) || '',
161
+ options: Array.from(cliOptions.values()),
198
162
  args,
199
- aliases: meta === null || meta === void 0 ? void 0 : meta.cliAliases,
163
+ aliases,
164
+ examples: (meta === null || meta === void 0 ? void 0 : meta.cliExamples) || [],
165
+ handler: fn,
166
+ onRegister: (path, aliasType) => {
167
+ var _a;
168
+ if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.debug) {
169
+ opts.logHandler(`${''}(${aliasTypes[aliasType]})${''}${path}`);
170
+ }
171
+ },
200
172
  });
201
- if ((_c = this.opts) === null || _c === void 0 ? void 0 : _c.debug) {
202
- opts.logHandler(`${''}(CLI)${''}${targetPath}`);
203
- }
204
173
  }
205
174
  }
206
- }
175
+ }
176
+ const aliasTypes = ['CLI', 'CLI-alias', 'CLI-alias*', 'CLI-alias*'];
207
177
 
208
178
  function formatParams(keys) {
209
179
  const names = [keys].flat();
@@ -218,7 +188,7 @@ function formatParams(keys) {
218
188
  * ```ts
219
189
  * │ @Cli('command')
220
190
  * │ command(
221
- * │ @Description('Test flag...')
191
+ * │ @Description('Test option...')
222
192
  * │ @CliOption('test', 't')
223
193
  * │ test: boolean,
224
194
  * │ ) {
@@ -231,23 +201,18 @@ function formatParams(keys) {
231
201
  */
232
202
  function CliOption(...keys) {
233
203
  const mate = getCliMate();
234
- return mate.apply(mate.decorate('cliParamKeys', keys, false), moost.Resolve(() => {
235
- const flags = eventCli.useFlags();
236
- const names = keys;
237
- const vals = [];
238
- for (const name of names) {
239
- if (flags[name]) {
240
- vals.push(flags[name]);
241
- }
242
- }
243
- if (vals.length > 1) {
244
- throw new Error(`Options ${formatParams(names).join(' and ')} are synonyms. Please use only one of them.`);
245
- }
246
- if (vals.length === 0) {
247
- return false;
248
- }
249
- return vals[0];
250
- }, formatParams(keys).join(', ')));
204
+ return mate.apply(mate.decorate('cliOptionsKeys', keys, false), moost.Resolve(() => eventCli.useCliOption(keys[0]), formatParams(keys).join(', ')));
205
+ }
206
+ /**
207
+ * ## Define Global CLI Option
208
+ * ### @ClassDecorator
209
+ * The option described here will appear in every command instructions
210
+ * @param option keys and description of CLI option
211
+ * @returns
212
+ */
213
+ function CliGlobalOption(option) {
214
+ const mate = getCliMate();
215
+ return mate.decorate('cliOptions', option, true);
251
216
  }
252
217
 
253
218
  /**
@@ -259,6 +224,7 @@ function CliOption(...keys) {
259
224
  * For example the folowing path are interpreted the same:
260
225
  * - "command test use:dev :name"
261
226
  * - "command/test/use:dev/:name"
227
+ *
262
228
  * Where name will become an argument
263
229
  *
264
230
  * @param path - command path
@@ -278,6 +244,18 @@ function Cli(path) {
278
244
  */
279
245
  function CliAlias(alias) {
280
246
  return getCliMate().decorate('cliAliases', alias, true);
247
+ }
248
+ /**
249
+ * ## Define CLI Example
250
+ * ### @MethodDecorator
251
+ *
252
+ * Use it to define example for Cli Help display
253
+ *
254
+ * @param path - command alias path
255
+ * @returns
256
+ */
257
+ function CliExample(cmd, description) {
258
+ return getCliMate().decorate('cliExamples', { cmd, description }, true);
281
259
  }
282
260
 
283
261
  /**
@@ -294,45 +272,18 @@ function CliAlias(alias) {
294
272
  */
295
273
  const cliHelpInterceptor = (opts) => {
296
274
  return moost.defineInterceptorFn(() => {
297
- const helpOptions = (opts === null || opts === void 0 ? void 0 : opts.helpOptions) || ['help'];
298
- for (const option of helpOptions) {
299
- if (eventCli.useFlag(option) === true) {
300
- try {
301
- useCliHelp().print(opts === null || opts === void 0 ? void 0 : opts.colors);
302
- return '';
303
- }
304
- catch (e) {
305
- //
306
- }
275
+ try {
276
+ if (eventCli.useAutoHelp(opts === null || opts === void 0 ? void 0 : opts.helpOptions, opts === null || opts === void 0 ? void 0 : opts.colors)) {
277
+ return '';
307
278
  }
308
279
  }
309
- if ((opts === null || opts === void 0 ? void 0 : opts.helpWithArgs) || (opts === null || opts === void 0 ? void 0 : opts.helpWithIncompleteCmd)) {
280
+ catch (e) {
281
+ //
282
+ }
283
+ if (opts === null || opts === void 0 ? void 0 : opts.lookupLevel) {
310
284
  const { getMethod } = moost.useControllerContext();
311
285
  if (!getMethod()) {
312
- const pathParams = eventCli.useCliContext().store('event').get('pathParams').join(' ');
313
- const cliHelp = useCliHelp().getCliHelp();
314
- const cmd = cliHelp.getCliName();
315
- let data;
316
- try {
317
- data = cliHelp.match(pathParams);
318
- }
319
- catch (e) {
320
- if (opts === null || opts === void 0 ? void 0 : opts.helpWithIncompleteCmd) {
321
- const variants = cliHelp.lookup(pathParams);
322
- if (variants.length) {
323
- throw new Error(`Command is incomplete, did you mean:\n${variants.slice(0, 7).map(c => ` $ ${cmd} ${c.main.command}`).join('\n')}`);
324
- }
325
- }
326
- }
327
- if (data) {
328
- const { main, children } = data;
329
- if ((opts === null || opts === void 0 ? void 0 : opts.helpWithArgs) && main.args && Object.keys(main.args).length) {
330
- throw new Error(`Arguments expected: ${Object.keys(main.args).map(l => `<${l}>`).join(', ')}`);
331
- }
332
- else if ((opts === null || opts === void 0 ? void 0 : opts.helpWithIncompleteCmd) && children) {
333
- throw new Error(`Command is incomplete, did you mean:\n${children.slice(0, 7).map(c => ` $ ${cmd} ${c.command}`).join('\n')}`);
334
- }
335
- }
286
+ eventCli.useCommandLookupHelp(opts.lookupLevel);
336
287
  }
337
288
  }
338
289
  }, moost.TInterceptorPriority.BEFORE_ALL);
@@ -362,9 +313,9 @@ const CliHelpInterceptor = (...opts) => moost.Intercept(cliHelpInterceptor(...op
362
313
 
363
314
  exports.Cli = Cli;
364
315
  exports.CliAlias = CliAlias;
316
+ exports.CliExample = CliExample;
317
+ exports.CliGlobalOption = CliGlobalOption;
365
318
  exports.CliHelpInterceptor = CliHelpInterceptor;
366
319
  exports.CliOption = CliOption;
367
320
  exports.MoostCli = MoostCli;
368
321
  exports.cliHelpInterceptor = cliHelpInterceptor;
369
- exports.setCliHelpForEvent = setCliHelpForEvent;
370
- exports.useCliHelp = useCliHelp;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,4 @@
1
- import { CliHelpRenderer } from '@prostojs/cli-help';
2
1
  import { Moost } from 'moost';
3
- import { TCliHelpOptions } from '@prostojs/cli-help';
4
2
  import { TInterceptorFn } from 'moost';
5
3
  import { TMoostAdapter } from 'moost';
6
4
  import { TMoostAdapterOptions } from 'moost';
@@ -16,6 +14,7 @@ import { WooksCli } from '@wooksjs/event-cli';
16
14
  * For example the folowing path are interpreted the same:
17
15
  * - "command test use:dev :name"
18
16
  * - "command/test/use:dev/:name"
17
+ *
19
18
  * Where name will become an argument
20
19
  *
21
20
  * @param path - command path
@@ -34,6 +33,30 @@ export declare function Cli(path?: string): MethodDecorator;
34
33
  */
35
34
  export declare function CliAlias(alias: string): MethodDecorator;
36
35
 
36
+ /**
37
+ * ## Define CLI Example
38
+ * ### @MethodDecorator
39
+ *
40
+ * Use it to define example for Cli Help display
41
+ *
42
+ * @param path - command alias path
43
+ * @returns
44
+ */
45
+ export declare function CliExample(cmd: string, description?: string): MethodDecorator;
46
+
47
+ /**
48
+ * ## Define Global CLI Option
49
+ * ### @ClassDecorator
50
+ * The option described here will appear in every command instructions
51
+ * @param option keys and description of CLI option
52
+ * @returns
53
+ */
54
+ export declare function CliGlobalOption(option: {
55
+ keys: string[];
56
+ description?: string;
57
+ value?: string;
58
+ }): ClassDecorator;
59
+
37
60
  /**
38
61
  * ## @Decorator
39
62
  * ### Interceptor Factory for CliHelpRenderer
@@ -68,14 +91,9 @@ export declare const CliHelpInterceptor: (opts?: {
68
91
  */
69
92
  colors?: boolean | undefined;
70
93
  /**
71
- * Enable help message when arguments are missing
72
- */
73
- helpWithArgs?: boolean | undefined;
74
- /**
75
- * Enable help message when command is incomplete
76
- * and it is possible to suggest related commands
94
+ * Enable lookup for a command
77
95
  */
78
- helpWithIncompleteCmd?: boolean | undefined;
96
+ lookupLevel?: number | undefined;
79
97
  } | undefined) => ClassDecorator & MethodDecorator;
80
98
 
81
99
  /**
@@ -103,14 +121,9 @@ export declare const cliHelpInterceptor: (opts?: {
103
121
  */
104
122
  colors?: boolean;
105
123
  /**
106
- * Enable help message when arguments are missing
124
+ * Enable lookup for a command
107
125
  */
108
- helpWithArgs?: boolean;
109
- /**
110
- * Enable help message when command is incomplete
111
- * and it is possible to suggest related commands
112
- */
113
- helpWithIncompleteCmd?: boolean;
126
+ lookupLevel?: number;
114
127
  }) => TInterceptorFn;
115
128
 
116
129
  /**
@@ -121,7 +134,7 @@ export declare const cliHelpInterceptor: (opts?: {
121
134
  * ```ts
122
135
  * │ @Cli('command')
123
136
  * │ command(
124
- * │ @Description('Test flag...')
137
+ * │ @Description('Test option...')
125
138
  * │ @CliOption('test', 't')
126
139
  * │ test: boolean,
127
140
  * │ ) {
@@ -167,7 +180,6 @@ export declare function CliOption(...keys: string[]): ParameterDecorator;
167
180
  export declare class MoostCli implements TMoostAdapter<TCliHandlerMeta> {
168
181
  protected opts?: TMoostCliOpts | undefined;
169
182
  protected cliApp: WooksCli;
170
- protected cliHelp: CliHelpRenderer;
171
183
  constructor(opts?: TMoostCliOpts | undefined);
172
184
  onNotFound(): Promise<unknown>;
173
185
  protected moost?: Moost;
@@ -175,13 +187,6 @@ export declare class MoostCli implements TMoostAdapter<TCliHandlerMeta> {
175
187
  bindHandler<T extends object = object>(opts: TMoostAdapterOptions<TCliHandlerMeta, T>): void | Promise<void>;
176
188
  }
177
189
 
178
- /**
179
- * ### setCliHelpForEvent
180
- * Used internally to set CliHelpRenderer instance for an event state
181
- * @param cliHelp CliHelpRenderer
182
- */
183
- export declare function setCliHelpForEvent(cliHelp: CliHelpRenderer): void;
184
-
185
190
  export declare interface TCliHandlerMeta {
186
191
  path: string;
187
192
  }
@@ -191,30 +196,17 @@ export declare interface TMoostCliOpts {
191
196
  * WooksCli options or instance
192
197
  */
193
198
  wooksCli?: WooksCli | TWooksCliOptions;
194
- /**
195
- * CliHelpRenderer options or instance
196
- */
197
- cliHelp?: CliHelpRenderer | TCliHelpOptions;
198
199
  /**
199
200
  * more internal logs are printed when true
200
201
  */
201
202
  debug?: boolean;
203
+ /**
204
+ * Array of cli options applicable to every cli command
205
+ */
206
+ globalCliOptions?: {
207
+ keys: string[];
208
+ description?: string;
209
+ }[];
202
210
  }
203
211
 
204
- /**
205
- * ## useCliHelp
206
- * ### Composable
207
- * ```js
208
- * // example of printing cli instructions
209
- * const { print } = useCliHelp()
210
- * print(true)
211
- * ```
212
- * @returns
213
- */
214
- export declare function useCliHelp(): {
215
- getCliHelp: () => CliHelpRenderer;
216
- render: (width?: number, withColors?: boolean) => string[];
217
- print: (withColors?: boolean) => void;
218
- };
219
-
220
212
  export { }
package/dist/index.mjs CHANGED
@@ -1,6 +1,5 @@
1
1
  import { getMoostMate, getMoostInfact, defineMoostEventHandler, Resolve, defineInterceptorFn, useControllerContext, TInterceptorPriority, Intercept } from 'moost';
2
- import { useCliContext, WooksCli, createCliApp, useFlags, useFlag } from '@wooksjs/event-cli';
3
- import { CliHelpRenderer } from '@prostojs/cli-help';
2
+ import { WooksCli, createCliApp, useCliContext, useCliOption, useAutoHelp, useCommandLookupHelp } from '@wooksjs/event-cli';
4
3
 
5
4
  /******************************************************************************
6
5
  Copyright (c) Microsoft Corporation.
@@ -33,34 +32,6 @@ function getCliMate() {
33
32
  return getMoostMate();
34
33
  }
35
34
 
36
- /**
37
- * ### setCliHelpForEvent
38
- * Used internally to set CliHelpRenderer instance for an event state
39
- * @param cliHelp CliHelpRenderer
40
- */
41
- function setCliHelpForEvent(cliHelp) {
42
- useCliContext().store('event').set('cliHelp', cliHelp);
43
- }
44
- /**
45
- * ## useCliHelp
46
- * ### Composable
47
- * ```js
48
- * // example of printing cli instructions
49
- * const { print } = useCliHelp()
50
- * print(true)
51
- * ```
52
- * @returns
53
- */
54
- function useCliHelp() {
55
- const event = useCliContext().store('event');
56
- const getCliHelp = () => event.get('cliHelp');
57
- return {
58
- getCliHelp,
59
- render: (width, withColors) => getCliHelp().render(event.get('pathParams').join(' '), width, withColors),
60
- print: (withColors) => getCliHelp().print(event.get('pathParams').join(' '), withColors),
61
- };
62
- }
63
-
64
35
  const LOGGER_TITLE = 'moost-cli';
65
36
  const CONTEXT_TYPE = 'CLI';
66
37
  /**
@@ -108,16 +79,6 @@ class MoostCli {
108
79
  onNotFound: this.onNotFound.bind(this),
109
80
  });
110
81
  }
111
- const cliHelpOpts = opts === null || opts === void 0 ? void 0 : opts.cliHelp;
112
- if (cliHelpOpts && cliHelpOpts instanceof CliHelpRenderer) {
113
- this.cliHelp = cliHelpOpts;
114
- }
115
- else if (cliHelpOpts) {
116
- this.cliHelp = new CliHelpRenderer(cliHelpOpts);
117
- }
118
- else {
119
- this.cliHelp = new CliHelpRenderer();
120
- }
121
82
  if (!(opts === null || opts === void 0 ? void 0 : opts.debug)) {
122
83
  getMoostInfact().silent(true);
123
84
  }
@@ -132,9 +93,6 @@ class MoostCli {
132
93
  getControllerInstance: () => this.moost,
133
94
  callControllerMethod: () => undefined,
134
95
  logErrors: (_a = this.opts) === null || _a === void 0 ? void 0 : _a.debug,
135
- hooks: {
136
- init: () => setCliHelpForEvent(this.cliHelp),
137
- },
138
96
  })();
139
97
  if (typeof response === 'undefined') {
140
98
  this.cliApp.onUnknownCommand(pathParams);
@@ -147,7 +105,7 @@ class MoostCli {
147
105
  void this.cliApp.run();
148
106
  }
149
107
  bindHandler(opts) {
150
- var _a, _b, _c;
108
+ var _a, _b, _c, _d, _e;
151
109
  let fn;
152
110
  for (const handler of opts.handlers) {
153
111
  if (handler.type !== 'CLI')
@@ -157,11 +115,12 @@ class MoostCli {
157
115
  : typeof opts.method === 'string'
158
116
  ? opts.method
159
117
  : '';
160
- const targetPath = `${opts.prefix.replace(/\s+/g, '/') || ''}/${path}`
118
+ const prefix = opts.prefix.replace(/\s+/g, '/') || '';
119
+ const makePath = (p) => `${prefix}/${p}`
161
120
  .replace(/\/\/+/g, '/')
162
121
  // avoid interpreting "cmd:tail" as "cmd/:tail"
163
- .replace(/\/\\:/g, '\\:');
164
- let cliCommand = '';
122
+ .replace(/\/\\:/g, '\\:')
123
+ .replace(/^\/+/g, '');
165
124
  if (!fn) {
166
125
  fn = defineMoostEventHandler({
167
126
  contextType: CONTEXT_TYPE,
@@ -171,37 +130,48 @@ class MoostCli {
171
130
  controllerMethod: opts.method,
172
131
  resolveArgs: opts.resolveArgs,
173
132
  logErrors: (_a = this.opts) === null || _a === void 0 ? void 0 : _a.debug,
174
- hooks: {
175
- init: () => setCliHelpForEvent(this.cliHelp),
176
- },
177
133
  });
178
134
  }
179
- const { getArgs, getStaticPart } = this.cliApp.cli(targetPath, fn);
135
+ const targetPath = makePath(path);
180
136
  const meta = getCliMate().read(opts.fakeInstance, opts.method);
181
- const args = {};
182
- getArgs().forEach(a => {
183
- var _a;
184
- const argParam = (_a = meta === null || meta === void 0 ? void 0 : meta.params) === null || _a === void 0 ? void 0 : _a.find(p => p.label === a && p.description);
185
- args[a] = (argParam === null || argParam === void 0 ? void 0 : argParam.description) || '';
186
- });
187
- cliCommand = getStaticPart().replace(/\//g, ' ').trim();
188
- this.cliHelp.addEntry({
189
- description: (meta === null || meta === void 0 ? void 0 : meta.description) || '',
190
- command: cliCommand,
191
- options: ((_b = meta === null || meta === void 0 ? void 0 : meta.params) === null || _b === void 0 ? void 0 : _b.filter(param => !!param.cliParamKeys && param.cliParamKeys.length > 0).map(param => ({
192
- keys: param.cliParamKeys,
137
+ const classMeta = getCliMate().read(opts.fakeInstance);
138
+ const cliOptions = new Map();
139
+ [
140
+ ...(((_c = (_b = this.opts) === null || _b === void 0 ? void 0 : _b.globalCliOptions) === null || _c === void 0 ? void 0 : _c.length) ? this.opts.globalCliOptions : []),
141
+ ...((classMeta === null || classMeta === void 0 ? void 0 : classMeta.cliOptions) || []),
142
+ ...(((_d = meta === null || meta === void 0 ? void 0 : meta.params) === null || _d === void 0 ? void 0 : _d.filter(param => !!param.cliOptionsKeys && param.cliOptionsKeys.length > 0).map(param => ({
143
+ keys: param.cliOptionsKeys,
193
144
  value: typeof param.value === 'string' ? param.value : '',
194
145
  description: param.description || '',
195
- }))) || [],
146
+ }))) || []),
147
+ ].forEach(o => cliOptions.set(o.keys[0], o));
148
+ const aliases = [];
149
+ if (meta === null || meta === void 0 ? void 0 : meta.cliAliases) {
150
+ for (const alias of meta.cliAliases) {
151
+ const targetPath = makePath(alias);
152
+ aliases.push(targetPath);
153
+ }
154
+ }
155
+ const args = {};
156
+ (_e = meta === null || meta === void 0 ? void 0 : meta.params) === null || _e === void 0 ? void 0 : _e.filter(p => p.isRouteParam && p.description).forEach(p => args[p.isRouteParam] = p.description);
157
+ this.cliApp.cli(targetPath, {
158
+ description: (meta === null || meta === void 0 ? void 0 : meta.description) || '',
159
+ options: Array.from(cliOptions.values()),
196
160
  args,
197
- aliases: meta === null || meta === void 0 ? void 0 : meta.cliAliases,
161
+ aliases,
162
+ examples: (meta === null || meta === void 0 ? void 0 : meta.cliExamples) || [],
163
+ handler: fn,
164
+ onRegister: (path, aliasType) => {
165
+ var _a;
166
+ if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.debug) {
167
+ opts.logHandler(`${''}(${aliasTypes[aliasType]})${''}${path}`);
168
+ }
169
+ },
198
170
  });
199
- if ((_c = this.opts) === null || _c === void 0 ? void 0 : _c.debug) {
200
- opts.logHandler(`${''}(CLI)${''}${targetPath}`);
201
- }
202
171
  }
203
172
  }
204
- }
173
+ }
174
+ const aliasTypes = ['CLI', 'CLI-alias', 'CLI-alias*', 'CLI-alias*'];
205
175
 
206
176
  function formatParams(keys) {
207
177
  const names = [keys].flat();
@@ -216,7 +186,7 @@ function formatParams(keys) {
216
186
  * ```ts
217
187
  * │ @Cli('command')
218
188
  * │ command(
219
- * │ @Description('Test flag...')
189
+ * │ @Description('Test option...')
220
190
  * │ @CliOption('test', 't')
221
191
  * │ test: boolean,
222
192
  * │ ) {
@@ -229,23 +199,18 @@ function formatParams(keys) {
229
199
  */
230
200
  function CliOption(...keys) {
231
201
  const mate = getCliMate();
232
- return mate.apply(mate.decorate('cliParamKeys', keys, false), Resolve(() => {
233
- const flags = useFlags();
234
- const names = keys;
235
- const vals = [];
236
- for (const name of names) {
237
- if (flags[name]) {
238
- vals.push(flags[name]);
239
- }
240
- }
241
- if (vals.length > 1) {
242
- throw new Error(`Options ${formatParams(names).join(' and ')} are synonyms. Please use only one of them.`);
243
- }
244
- if (vals.length === 0) {
245
- return false;
246
- }
247
- return vals[0];
248
- }, formatParams(keys).join(', ')));
202
+ return mate.apply(mate.decorate('cliOptionsKeys', keys, false), Resolve(() => useCliOption(keys[0]), formatParams(keys).join(', ')));
203
+ }
204
+ /**
205
+ * ## Define Global CLI Option
206
+ * ### @ClassDecorator
207
+ * The option described here will appear in every command instructions
208
+ * @param option keys and description of CLI option
209
+ * @returns
210
+ */
211
+ function CliGlobalOption(option) {
212
+ const mate = getCliMate();
213
+ return mate.decorate('cliOptions', option, true);
249
214
  }
250
215
 
251
216
  /**
@@ -257,6 +222,7 @@ function CliOption(...keys) {
257
222
  * For example the folowing path are interpreted the same:
258
223
  * - "command test use:dev :name"
259
224
  * - "command/test/use:dev/:name"
225
+ *
260
226
  * Where name will become an argument
261
227
  *
262
228
  * @param path - command path
@@ -276,6 +242,18 @@ function Cli(path) {
276
242
  */
277
243
  function CliAlias(alias) {
278
244
  return getCliMate().decorate('cliAliases', alias, true);
245
+ }
246
+ /**
247
+ * ## Define CLI Example
248
+ * ### @MethodDecorator
249
+ *
250
+ * Use it to define example for Cli Help display
251
+ *
252
+ * @param path - command alias path
253
+ * @returns
254
+ */
255
+ function CliExample(cmd, description) {
256
+ return getCliMate().decorate('cliExamples', { cmd, description }, true);
279
257
  }
280
258
 
281
259
  /**
@@ -292,45 +270,18 @@ function CliAlias(alias) {
292
270
  */
293
271
  const cliHelpInterceptor = (opts) => {
294
272
  return defineInterceptorFn(() => {
295
- const helpOptions = (opts === null || opts === void 0 ? void 0 : opts.helpOptions) || ['help'];
296
- for (const option of helpOptions) {
297
- if (useFlag(option) === true) {
298
- try {
299
- useCliHelp().print(opts === null || opts === void 0 ? void 0 : opts.colors);
300
- return '';
301
- }
302
- catch (e) {
303
- //
304
- }
273
+ try {
274
+ if (useAutoHelp(opts === null || opts === void 0 ? void 0 : opts.helpOptions, opts === null || opts === void 0 ? void 0 : opts.colors)) {
275
+ return '';
305
276
  }
306
277
  }
307
- if ((opts === null || opts === void 0 ? void 0 : opts.helpWithArgs) || (opts === null || opts === void 0 ? void 0 : opts.helpWithIncompleteCmd)) {
278
+ catch (e) {
279
+ //
280
+ }
281
+ if (opts === null || opts === void 0 ? void 0 : opts.lookupLevel) {
308
282
  const { getMethod } = useControllerContext();
309
283
  if (!getMethod()) {
310
- const pathParams = useCliContext().store('event').get('pathParams').join(' ');
311
- const cliHelp = useCliHelp().getCliHelp();
312
- const cmd = cliHelp.getCliName();
313
- let data;
314
- try {
315
- data = cliHelp.match(pathParams);
316
- }
317
- catch (e) {
318
- if (opts === null || opts === void 0 ? void 0 : opts.helpWithIncompleteCmd) {
319
- const variants = cliHelp.lookup(pathParams);
320
- if (variants.length) {
321
- throw new Error(`Command is incomplete, did you mean:\n${variants.slice(0, 7).map(c => ` $ ${cmd} ${c.main.command}`).join('\n')}`);
322
- }
323
- }
324
- }
325
- if (data) {
326
- const { main, children } = data;
327
- if ((opts === null || opts === void 0 ? void 0 : opts.helpWithArgs) && main.args && Object.keys(main.args).length) {
328
- throw new Error(`Arguments expected: ${Object.keys(main.args).map(l => `<${l}>`).join(', ')}`);
329
- }
330
- else if ((opts === null || opts === void 0 ? void 0 : opts.helpWithIncompleteCmd) && children) {
331
- throw new Error(`Command is incomplete, did you mean:\n${children.slice(0, 7).map(c => ` $ ${cmd} ${c.command}`).join('\n')}`);
332
- }
333
- }
284
+ useCommandLookupHelp(opts.lookupLevel);
334
285
  }
335
286
  }
336
287
  }, TInterceptorPriority.BEFORE_ALL);
@@ -358,4 +309,4 @@ const cliHelpInterceptor = (opts) => {
358
309
  */
359
310
  const CliHelpInterceptor = (...opts) => Intercept(cliHelpInterceptor(...opts));
360
311
 
361
- export { Cli, CliAlias, CliHelpInterceptor, CliOption, MoostCli, cliHelpInterceptor, setCliHelpForEvent, useCliHelp };
312
+ 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.2.28",
3
+ "version": "0.2.30",
4
4
  "description": "@moostjs/event-cli",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -28,12 +28,11 @@
28
28
  },
29
29
  "homepage": "https://github.com/moostjs/moostjs/tree/main/packages/event-cli#readme",
30
30
  "peerDependencies": {
31
- "moost": "0.2.28",
32
- "wooks": "^0.2.22",
33
- "@wooksjs/event-core": "^0.2.22"
31
+ "moost": "0.2.30",
32
+ "wooks": "^0.3.1",
33
+ "@wooksjs/event-core": "^0.3.1"
34
34
  },
35
35
  "dependencies": {
36
- "@wooksjs/event-cli": "^0.2.22",
37
- "@prostojs/cli-help": "^0.0.7"
36
+ "@wooksjs/event-cli": "^0.3.1"
38
37
  }
39
38
  }