@moostjs/event-cli 0.5.28 → 0.5.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 +154 -25
- package/dist/index.d.ts +198 -6
- package/dist/index.mjs +148 -18
- package/package.json +8 -6
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
//#region rolldown:runtime
|
|
3
2
|
var __create = Object.create;
|
|
4
3
|
var __defProp = Object.defineProperty;
|
|
@@ -32,16 +31,46 @@ function getCliMate() {
|
|
|
32
31
|
|
|
33
32
|
//#endregion
|
|
34
33
|
//#region packages/event-cli/src/decorators/cli.decorator.ts
|
|
35
|
-
|
|
34
|
+
/**
|
|
35
|
+
* ## Define CLI Command
|
|
36
|
+
* ### @MethodDecorator
|
|
37
|
+
*
|
|
38
|
+
* Command path segments may be separated by / or space.
|
|
39
|
+
*
|
|
40
|
+
* For example the folowing path are interpreted the same:
|
|
41
|
+
* - "command test use:dev :name"
|
|
42
|
+
* - "command/test/use:dev/:name"
|
|
43
|
+
*
|
|
44
|
+
* Where name will become an argument
|
|
45
|
+
*
|
|
46
|
+
* @param path - command path
|
|
47
|
+
* @returns
|
|
48
|
+
*/ function Cli(path) {
|
|
36
49
|
return getCliMate().decorate("handlers", {
|
|
37
50
|
path: path?.replace(/\s+/g, "/"),
|
|
38
51
|
type: "CLI"
|
|
39
52
|
}, true);
|
|
40
53
|
}
|
|
41
|
-
|
|
54
|
+
/**
|
|
55
|
+
* ## Define CLI Command Alias
|
|
56
|
+
* ### @MethodDecorator
|
|
57
|
+
*
|
|
58
|
+
* Use it to define alias for @Cli('...') command
|
|
59
|
+
*
|
|
60
|
+
* @param path - command alias path
|
|
61
|
+
* @returns
|
|
62
|
+
*/ function CliAlias(alias) {
|
|
42
63
|
return getCliMate().decorate("cliAliases", alias, true);
|
|
43
64
|
}
|
|
44
|
-
|
|
65
|
+
/**
|
|
66
|
+
* ## Define CLI Example
|
|
67
|
+
* ### @MethodDecorator
|
|
68
|
+
*
|
|
69
|
+
* Use it to define example for Cli Help display
|
|
70
|
+
*
|
|
71
|
+
* @param path - command alias path
|
|
72
|
+
* @returns
|
|
73
|
+
*/ function CliExample(cmd, description) {
|
|
45
74
|
return getCliMate().decorate("cliExamples", {
|
|
46
75
|
cmd,
|
|
47
76
|
description
|
|
@@ -57,11 +86,35 @@ function formatParams(keys) {
|
|
|
57
86
|
|
|
58
87
|
//#endregion
|
|
59
88
|
//#region packages/event-cli/src/decorators/option.decorator.ts
|
|
60
|
-
|
|
89
|
+
/**
|
|
90
|
+
* ## Define CLI Option
|
|
91
|
+
* ### @ParameterDecorator
|
|
92
|
+
* Use together with @Description('...') to document cli option
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* │ @Cli('command')
|
|
96
|
+
* │ command(
|
|
97
|
+
* │ @Description('Test option...')
|
|
98
|
+
* │ @CliOption('test', 't')
|
|
99
|
+
* │ test: boolean,
|
|
100
|
+
* │ ) {
|
|
101
|
+
* │ return `test=${ test }`
|
|
102
|
+
* │ }
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* @param keys list of keys (short and long alternatives)
|
|
106
|
+
* @returns
|
|
107
|
+
*/ function CliOption(...keys) {
|
|
61
108
|
const mate = getCliMate();
|
|
62
109
|
return mate.apply(mate.decorate("cliOptionsKeys", keys, false), (0, moost.Resolve)(() => (0, __wooksjs_event_cli.useCliOption)(keys[0]), formatParams(keys).join(", ")));
|
|
63
110
|
}
|
|
64
|
-
|
|
111
|
+
/**
|
|
112
|
+
* ## Define Global CLI Option
|
|
113
|
+
* ### @ClassDecorator
|
|
114
|
+
* The option described here will appear in every command instructions
|
|
115
|
+
* @param option keys and description of CLI option
|
|
116
|
+
* @returns
|
|
117
|
+
*/ function CliGlobalOption(option) {
|
|
65
118
|
const mate = getCliMate();
|
|
66
119
|
return mate.decorate("cliOptions", option, true);
|
|
67
120
|
}
|
|
@@ -80,25 +133,54 @@ function _define_property$1(obj, key, value) {
|
|
|
80
133
|
}
|
|
81
134
|
const LOGGER_TITLE = "moost-cli";
|
|
82
135
|
const CONTEXT_TYPE = "CLI";
|
|
83
|
-
|
|
136
|
+
/**
|
|
137
|
+
* ## Moost Cli Adapter
|
|
138
|
+
*
|
|
139
|
+
* Moost Adapter for CLI events
|
|
140
|
+
*
|
|
141
|
+
* ```ts
|
|
142
|
+
* │ // Quick example
|
|
143
|
+
* │ import { MoostCli, Cli, CliOption, cliHelpInterceptor } from '@moostjs/event-cli'
|
|
144
|
+
* │ import { Moost, Param } from 'moost'
|
|
145
|
+
* │
|
|
146
|
+
* │ class MyApp extends Moost {
|
|
147
|
+
* │ @Cli('command/:arg')
|
|
148
|
+
* │ command(
|
|
149
|
+
* │ @Param('arg')
|
|
150
|
+
* │ arg: string,
|
|
151
|
+
* │ @CliOption('test', 't')
|
|
152
|
+
* │ test: boolean,
|
|
153
|
+
* │ ) {
|
|
154
|
+
* │ return `command run with flag arg=${ arg }, test=${ test }`
|
|
155
|
+
* │ }
|
|
156
|
+
* │ }
|
|
157
|
+
* │
|
|
158
|
+
* │ const app = new MyApp()
|
|
159
|
+
* │ app.applyGlobalInterceptors(cliHelpInterceptor())
|
|
160
|
+
* │
|
|
161
|
+
* │ const cli = new MoostCli()
|
|
162
|
+
* │ app.adapter(cli)
|
|
163
|
+
* │ app.init()
|
|
164
|
+
* ```
|
|
165
|
+
*/ var MoostCli = class {
|
|
84
166
|
async onNotFound() {
|
|
85
167
|
const pathParams = (0, __wooksjs_event_cli.useCliContext)().store("event").get("pathParams") || [];
|
|
86
168
|
const response = await (0, moost.defineMoostEventHandler)({
|
|
87
169
|
loggerTitle: LOGGER_TITLE,
|
|
88
170
|
getIterceptorHandler: () => this.moost?.getGlobalInterceptorHandler(),
|
|
89
171
|
getControllerInstance: () => this.moost,
|
|
90
|
-
callControllerMethod: () =>
|
|
172
|
+
callControllerMethod: () => void 0,
|
|
91
173
|
logErrors: this.opts?.debug,
|
|
92
174
|
targetPath: "",
|
|
93
175
|
handlerType: "__SYSTEM__"
|
|
94
176
|
})();
|
|
95
|
-
if (response ===
|
|
177
|
+
if (response === void 0) this.cliApp.onUnknownCommand(pathParams);
|
|
96
178
|
return response;
|
|
97
179
|
}
|
|
98
180
|
onInit(moost$1) {
|
|
99
181
|
this.moost = moost$1;
|
|
100
182
|
const boolean = Object.entries(this.optionTypes).filter(([_key, val]) => val.length === 1 && val[0] === Boolean).map(([key, _val]) => key);
|
|
101
|
-
this.cliApp.run(
|
|
183
|
+
this.cliApp.run(void 0, { boolean });
|
|
102
184
|
}
|
|
103
185
|
bindHandler(opts) {
|
|
104
186
|
let fn;
|
|
@@ -121,7 +203,7 @@ var MoostCli = class {
|
|
|
121
203
|
});
|
|
122
204
|
const meta = getCliMate().read(opts.fakeInstance, opts.method);
|
|
123
205
|
const classMeta = getCliMate().read(opts.fakeInstance);
|
|
124
|
-
const cliOptions = new Map();
|
|
206
|
+
const cliOptions = /* @__PURE__ */ new Map();
|
|
125
207
|
[
|
|
126
208
|
...this.opts?.globalCliOptions?.length ? this.opts.globalCliOptions : [],
|
|
127
209
|
...classMeta?.cliOptions || [],
|
|
@@ -155,7 +237,7 @@ var MoostCli = class {
|
|
|
155
237
|
handler: fn,
|
|
156
238
|
onRegister: (path$1, aliasType, route) => {
|
|
157
239
|
opts.register(handler, path$1, route?.getArgs() || routerBinding.getArgs());
|
|
158
|
-
if (this.opts?.debug) opts.logHandler(
|
|
240
|
+
if (this.opts?.debug) opts.logHandler(`[36m(${aliasTypes[aliasType]})[32m${path$1}`);
|
|
159
241
|
}
|
|
160
242
|
});
|
|
161
243
|
opts.register(handler, targetPath, routerBinding.getArgs());
|
|
@@ -193,7 +275,18 @@ const aliasTypes = [
|
|
|
193
275
|
|
|
194
276
|
//#endregion
|
|
195
277
|
//#region packages/event-cli/src/interceptors/help.interceptor.ts
|
|
196
|
-
|
|
278
|
+
/**
|
|
279
|
+
* ### Interceptor Factory for CliHelpRenderer
|
|
280
|
+
*
|
|
281
|
+
* By default intercepts cli calls with flag --help
|
|
282
|
+
* and prints help.
|
|
283
|
+
*
|
|
284
|
+
* ```js
|
|
285
|
+
* new Moost().applyGlobalInterceptors(cliHelpInterceptor({ colors: true }))
|
|
286
|
+
* ```
|
|
287
|
+
* @param opts {} { helpOptions: ['help', 'h'], colors: true } cli options to invoke help renderer
|
|
288
|
+
* @returns TInterceptorFn
|
|
289
|
+
*/ const cliHelpInterceptor = (opts) => (0, moost.defineInterceptorFn)(() => {
|
|
197
290
|
try {
|
|
198
291
|
if ((0, __wooksjs_event_cli.useAutoHelp)(opts?.helpOptions, opts?.colors)) return "";
|
|
199
292
|
} catch (error) {}
|
|
@@ -202,7 +295,27 @@ const cliHelpInterceptor = (opts) => (0, moost.defineInterceptorFn)(() => {
|
|
|
202
295
|
if (!getMethod()) (0, __wooksjs_event_cli.useCommandLookupHelp)(opts.lookupLevel);
|
|
203
296
|
}
|
|
204
297
|
}, moost.TInterceptorPriority.BEFORE_ALL);
|
|
205
|
-
|
|
298
|
+
/**
|
|
299
|
+
* ## @Decorator
|
|
300
|
+
* ### Interceptor Factory for CliHelpRenderer
|
|
301
|
+
*
|
|
302
|
+
* By default intercepts cli calls with flag --help
|
|
303
|
+
* and prints help.
|
|
304
|
+
*
|
|
305
|
+
* ```ts
|
|
306
|
+
* // default configuration
|
|
307
|
+
* • @CliHelpInterceptor({ helpOptions: 'help', colors: true })
|
|
308
|
+
*
|
|
309
|
+
* // additional option -h to invoke help renderer
|
|
310
|
+
* • @CliHelpInterceptor({ helpOptions: ['help', 'h'], colors: true })
|
|
311
|
+
*
|
|
312
|
+
* // redefine cli option to invoke help renderer
|
|
313
|
+
* • @CliHelpInterceptor({ helpOptions: ['usage'] })
|
|
314
|
+
* ```
|
|
315
|
+
*
|
|
316
|
+
* @param opts {} { helpOptions: ['help', 'h'], colors: true } cli options to invoke help renderer
|
|
317
|
+
* @returns Decorator
|
|
318
|
+
*/ const CliHelpInterceptor = (...opts) => (0, moost.Intercept)(cliHelpInterceptor(...opts));
|
|
206
319
|
|
|
207
320
|
//#endregion
|
|
208
321
|
//#region packages/event-cli/src/quick-cli.ts
|
|
@@ -216,7 +329,23 @@ function _define_property(obj, key, value) {
|
|
|
216
329
|
else obj[key] = value;
|
|
217
330
|
return obj;
|
|
218
331
|
}
|
|
219
|
-
|
|
332
|
+
/**
|
|
333
|
+
* Quick CLI App factory class.
|
|
334
|
+
*
|
|
335
|
+
* Use this class to quickly build a CLI application with controllers, help options,
|
|
336
|
+
* and global CLI options. It extends the Moost class and wraps the initialization of MoostCli.
|
|
337
|
+
*
|
|
338
|
+
* @example
|
|
339
|
+
* ```typescript
|
|
340
|
+
* import { CliApp } from '@wooksjs/event-cli'
|
|
341
|
+
* import { Commands } from './commands.controller.ts'
|
|
342
|
+
* new CliApp()
|
|
343
|
+
* .controllers(Commands)
|
|
344
|
+
* .useHelp({ name: 'myCli' })
|
|
345
|
+
* .useOptions([{ keys: ['help'], description: 'Display instructions for the command.' }])
|
|
346
|
+
* .start()
|
|
347
|
+
* ```
|
|
348
|
+
*/ var CliApp = class extends moost.Moost {
|
|
220
349
|
/**
|
|
221
350
|
* Registers one or more CLI controllers.
|
|
222
351
|
*
|
|
@@ -269,7 +398,7 @@ var CliApp = class extends moost.Moost {
|
|
|
269
398
|
maxWidth: this._helpOpts.maxWidth,
|
|
270
399
|
maxLeft: this._helpOpts.maxLeft,
|
|
271
400
|
mark: this._helpOpts.mark
|
|
272
|
-
} :
|
|
401
|
+
} : void 0 },
|
|
273
402
|
globalCliOptions: this._globalOpts
|
|
274
403
|
});
|
|
275
404
|
this.adapter(cli);
|
|
@@ -285,13 +414,13 @@ var CliApp = class extends moost.Moost {
|
|
|
285
414
|
};
|
|
286
415
|
|
|
287
416
|
//#endregion
|
|
288
|
-
exports.Cli = Cli
|
|
289
|
-
exports.CliAlias = CliAlias
|
|
290
|
-
exports.CliApp = CliApp
|
|
291
|
-
exports.CliExample = CliExample
|
|
292
|
-
exports.CliGlobalOption = CliGlobalOption
|
|
293
|
-
exports.CliHelpInterceptor = CliHelpInterceptor
|
|
294
|
-
exports.CliOption = CliOption
|
|
417
|
+
exports.Cli = Cli;
|
|
418
|
+
exports.CliAlias = CliAlias;
|
|
419
|
+
exports.CliApp = CliApp;
|
|
420
|
+
exports.CliExample = CliExample;
|
|
421
|
+
exports.CliGlobalOption = CliGlobalOption;
|
|
422
|
+
exports.CliHelpInterceptor = CliHelpInterceptor;
|
|
423
|
+
exports.CliOption = CliOption;
|
|
295
424
|
Object.defineProperty(exports, 'Controller', {
|
|
296
425
|
enumerable: true,
|
|
297
426
|
get: function () {
|
|
@@ -310,7 +439,7 @@ Object.defineProperty(exports, 'Intercept', {
|
|
|
310
439
|
return moost.Intercept;
|
|
311
440
|
}
|
|
312
441
|
});
|
|
313
|
-
exports.MoostCli = MoostCli
|
|
442
|
+
exports.MoostCli = MoostCli;
|
|
314
443
|
Object.defineProperty(exports, 'Optional', {
|
|
315
444
|
enumerable: true,
|
|
316
445
|
get: function () {
|
|
@@ -323,7 +452,7 @@ Object.defineProperty(exports, 'Param', {
|
|
|
323
452
|
return moost.Param;
|
|
324
453
|
}
|
|
325
454
|
});
|
|
326
|
-
exports.cliHelpInterceptor = cliHelpInterceptor
|
|
455
|
+
exports.cliHelpInterceptor = cliHelpInterceptor;
|
|
327
456
|
Object.defineProperty(exports, 'useCliContext', {
|
|
328
457
|
enumerable: true,
|
|
329
458
|
get: function () {
|
package/dist/index.d.ts
CHANGED
|
@@ -4,11 +4,70 @@ import * as moost from 'moost';
|
|
|
4
4
|
import { TMoostAdapter, Moost, TMoostAdapterOptions } from 'moost';
|
|
5
5
|
export { Controller, Description, Intercept, Optional, Param, TInterceptorPriority, defineInterceptorFn } from 'moost';
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* ## Define CLI Command
|
|
9
|
+
* ### @MethodDecorator
|
|
10
|
+
*
|
|
11
|
+
* Command path segments may be separated by / or space.
|
|
12
|
+
*
|
|
13
|
+
* For example the folowing path are interpreted the same:
|
|
14
|
+
* - "command test use:dev :name"
|
|
15
|
+
* - "command/test/use:dev/:name"
|
|
16
|
+
*
|
|
17
|
+
* Where name will become an argument
|
|
18
|
+
*
|
|
19
|
+
* @param path - command path
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
7
22
|
declare function Cli(path?: string): MethodDecorator;
|
|
23
|
+
/**
|
|
24
|
+
* ## Define CLI Command Alias
|
|
25
|
+
* ### @MethodDecorator
|
|
26
|
+
*
|
|
27
|
+
* Use it to define alias for @Cli('...') command
|
|
28
|
+
*
|
|
29
|
+
* @param path - command alias path
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
8
32
|
declare function CliAlias(alias: string): MethodDecorator;
|
|
33
|
+
/**
|
|
34
|
+
* ## Define CLI Example
|
|
35
|
+
* ### @MethodDecorator
|
|
36
|
+
*
|
|
37
|
+
* Use it to define example for Cli Help display
|
|
38
|
+
*
|
|
39
|
+
* @param path - command alias path
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
9
42
|
declare function CliExample(cmd: string, description?: string): MethodDecorator;
|
|
10
43
|
|
|
44
|
+
/**
|
|
45
|
+
* ## Define CLI Option
|
|
46
|
+
* ### @ParameterDecorator
|
|
47
|
+
* Use together with @Description('...') to document cli option
|
|
48
|
+
*
|
|
49
|
+
* ```ts
|
|
50
|
+
* │ @Cli('command')
|
|
51
|
+
* │ command(
|
|
52
|
+
* │ @Description('Test option...')
|
|
53
|
+
* │ @CliOption('test', 't')
|
|
54
|
+
* │ test: boolean,
|
|
55
|
+
* │ ) {
|
|
56
|
+
* │ return `test=${ test }`
|
|
57
|
+
* │ }
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* @param keys list of keys (short and long alternatives)
|
|
61
|
+
* @returns
|
|
62
|
+
*/
|
|
11
63
|
declare function CliOption(...keys: string[]): ParameterDecorator;
|
|
64
|
+
/**
|
|
65
|
+
* ## Define Global CLI Option
|
|
66
|
+
* ### @ClassDecorator
|
|
67
|
+
* The option described here will appear in every command instructions
|
|
68
|
+
* @param option keys and description of CLI option
|
|
69
|
+
* @returns
|
|
70
|
+
*/
|
|
12
71
|
declare function CliGlobalOption(option: {
|
|
13
72
|
keys: string[];
|
|
14
73
|
description?: string;
|
|
@@ -21,14 +80,53 @@ interface TCliHandlerMeta {
|
|
|
21
80
|
path: string;
|
|
22
81
|
}
|
|
23
82
|
interface TMoostCliOpts {
|
|
83
|
+
/**
|
|
84
|
+
* WooksCli options or instance
|
|
85
|
+
*/
|
|
24
86
|
wooksCli?: WooksCli | TWooksCliOptions;
|
|
87
|
+
/**
|
|
88
|
+
* more internal logs are printed when true
|
|
89
|
+
*/
|
|
25
90
|
debug?: boolean;
|
|
91
|
+
/**
|
|
92
|
+
* Array of cli options applicable to every cli command
|
|
93
|
+
*/
|
|
26
94
|
globalCliOptions?: Array<{
|
|
27
95
|
keys: string[];
|
|
28
96
|
description?: string;
|
|
29
97
|
type?: TFunction;
|
|
30
98
|
}>;
|
|
31
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* ## Moost Cli Adapter
|
|
102
|
+
*
|
|
103
|
+
* Moost Adapter for CLI events
|
|
104
|
+
*
|
|
105
|
+
* ```ts
|
|
106
|
+
* │ // Quick example
|
|
107
|
+
* │ import { MoostCli, Cli, CliOption, cliHelpInterceptor } from '@moostjs/event-cli'
|
|
108
|
+
* │ import { Moost, Param } from 'moost'
|
|
109
|
+
* │
|
|
110
|
+
* │ class MyApp extends Moost {
|
|
111
|
+
* │ @Cli('command/:arg')
|
|
112
|
+
* │ command(
|
|
113
|
+
* │ @Param('arg')
|
|
114
|
+
* │ arg: string,
|
|
115
|
+
* │ @CliOption('test', 't')
|
|
116
|
+
* │ test: boolean,
|
|
117
|
+
* │ ) {
|
|
118
|
+
* │ return `command run with flag arg=${ arg }, test=${ test }`
|
|
119
|
+
* │ }
|
|
120
|
+
* │ }
|
|
121
|
+
* │
|
|
122
|
+
* │ const app = new MyApp()
|
|
123
|
+
* │ app.applyGlobalInterceptors(cliHelpInterceptor())
|
|
124
|
+
* │
|
|
125
|
+
* │ const cli = new MoostCli()
|
|
126
|
+
* │ app.adapter(cli)
|
|
127
|
+
* │ app.init()
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
32
130
|
declare class MoostCli implements TMoostAdapter<TCliHandlerMeta> {
|
|
33
131
|
protected opts?: TMoostCliOpts | undefined;
|
|
34
132
|
readonly name = "cli";
|
|
@@ -41,16 +139,57 @@ declare class MoostCli implements TMoostAdapter<TCliHandlerMeta> {
|
|
|
41
139
|
bindHandler<T extends object = object>(opts: TMoostAdapterOptions<TCliHandlerMeta, T>): void;
|
|
42
140
|
}
|
|
43
141
|
|
|
142
|
+
/**
|
|
143
|
+
* ### Interceptor Factory for CliHelpRenderer
|
|
144
|
+
*
|
|
145
|
+
* By default intercepts cli calls with flag --help
|
|
146
|
+
* and prints help.
|
|
147
|
+
*
|
|
148
|
+
* ```js
|
|
149
|
+
* new Moost().applyGlobalInterceptors(cliHelpInterceptor({ colors: true }))
|
|
150
|
+
* ```
|
|
151
|
+
* @param opts {} { helpOptions: ['help', 'h'], colors: true } cli options to invoke help renderer
|
|
152
|
+
* @returns TInterceptorFn
|
|
153
|
+
*/
|
|
44
154
|
declare const cliHelpInterceptor: (opts?: {
|
|
155
|
+
/**
|
|
156
|
+
* CLI Options that invoke help
|
|
157
|
+
* ```js
|
|
158
|
+
* helpOptions: ['help', 'h']
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
45
161
|
helpOptions?: string[];
|
|
162
|
+
/**
|
|
163
|
+
* Enable colored help
|
|
164
|
+
*/
|
|
46
165
|
colors?: boolean;
|
|
166
|
+
/**
|
|
167
|
+
* Enable lookup for a command
|
|
168
|
+
*/
|
|
47
169
|
lookupLevel?: number;
|
|
48
170
|
}) => moost.TInterceptorFn;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
171
|
+
/**
|
|
172
|
+
* ## @Decorator
|
|
173
|
+
* ### Interceptor Factory for CliHelpRenderer
|
|
174
|
+
*
|
|
175
|
+
* By default intercepts cli calls with flag --help
|
|
176
|
+
* and prints help.
|
|
177
|
+
*
|
|
178
|
+
* ```ts
|
|
179
|
+
* // default configuration
|
|
180
|
+
* • @CliHelpInterceptor({ helpOptions: 'help', colors: true })
|
|
181
|
+
*
|
|
182
|
+
* // additional option -h to invoke help renderer
|
|
183
|
+
* • @CliHelpInterceptor({ helpOptions: ['help', 'h'], colors: true })
|
|
184
|
+
*
|
|
185
|
+
* // redefine cli option to invoke help renderer
|
|
186
|
+
* • @CliHelpInterceptor({ helpOptions: ['usage'] })
|
|
187
|
+
* ```
|
|
188
|
+
*
|
|
189
|
+
* @param opts {} { helpOptions: ['help', 'h'], colors: true } cli options to invoke help renderer
|
|
190
|
+
* @returns Decorator
|
|
191
|
+
*/
|
|
192
|
+
declare const CliHelpInterceptor: (...opts: Parameters<typeof cliHelpInterceptor>) => ClassDecorator & MethodDecorator;
|
|
54
193
|
|
|
55
194
|
type THelpOptions = {
|
|
56
195
|
name?: string;
|
|
@@ -61,13 +200,66 @@ type THelpOptions = {
|
|
|
61
200
|
colors?: boolean;
|
|
62
201
|
lookupLevel?: number;
|
|
63
202
|
};
|
|
203
|
+
/**
|
|
204
|
+
* Quick CLI App factory class.
|
|
205
|
+
*
|
|
206
|
+
* Use this class to quickly build a CLI application with controllers, help options,
|
|
207
|
+
* and global CLI options. It extends the Moost class and wraps the initialization of MoostCli.
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* ```typescript
|
|
211
|
+
* import { CliApp } from '@wooksjs/event-cli'
|
|
212
|
+
* import { Commands } from './commands.controller.ts'
|
|
213
|
+
* new CliApp()
|
|
214
|
+
* .controllers(Commands)
|
|
215
|
+
* .useHelp({ name: 'myCli' })
|
|
216
|
+
* .useOptions([{ keys: ['help'], description: 'Display instructions for the command.' }])
|
|
217
|
+
* .start()
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
64
220
|
declare class CliApp extends Moost {
|
|
65
221
|
protected _helpOpts?: THelpOptions;
|
|
66
222
|
protected _globalOpts?: TMoostCliOpts['globalCliOptions'];
|
|
223
|
+
/**
|
|
224
|
+
* Registers one or more CLI controllers.
|
|
225
|
+
*
|
|
226
|
+
* (Shortcut for `registerControllers` method.)
|
|
227
|
+
*
|
|
228
|
+
* @param {...(object|Function|[string, object|Function])} controllers - List of controllers.
|
|
229
|
+
* Each controller can be an object, a class, or a tuple with a name and the controller.
|
|
230
|
+
* @returns {this} The CliApp instance for method chaining.
|
|
231
|
+
*/
|
|
67
232
|
controllers(...controllers: Array<object | Function | [string, object | Function]>): this;
|
|
233
|
+
/**
|
|
234
|
+
* Configures the CLI help options.
|
|
235
|
+
*
|
|
236
|
+
* This method sets the help options for the CLI. It ensures that colored output is enabled
|
|
237
|
+
* (unless explicitly disabled) and that the lookup level defaults to 3 if not provided.
|
|
238
|
+
*
|
|
239
|
+
* @param {THelpOptions} helpOpts - Help options configuration.
|
|
240
|
+
* @param {boolean} helpOpts.colors - Enable colored output.
|
|
241
|
+
* @param {number} helpOpts.lookupLevel - Level for help lookup.
|
|
242
|
+
* @returns {this} The CliApp instance for method chaining.
|
|
243
|
+
*/
|
|
68
244
|
useHelp(helpOpts: THelpOptions): this;
|
|
245
|
+
/**
|
|
246
|
+
* Sets the global CLI options.
|
|
247
|
+
*
|
|
248
|
+
* @param {TMoostCliOpts['globalCliOptions']} globalOpts - Global options for the CLI.
|
|
249
|
+
* @returns {this} The CliApp instance for method chaining.
|
|
250
|
+
*/
|
|
69
251
|
useOptions(globalOpts: TMoostCliOpts['globalCliOptions']): this;
|
|
252
|
+
/**
|
|
253
|
+
* Starts the CLI application.
|
|
254
|
+
*
|
|
255
|
+
* This method creates a MoostCli instance using the configured help and global options,
|
|
256
|
+
* attaches it via the adapter, applies the CLI help interceptor (if help options are set),
|
|
257
|
+
* and then initializes the application.
|
|
258
|
+
*
|
|
259
|
+
* @returns {any} The result of the initialization process.
|
|
260
|
+
*/
|
|
70
261
|
start(): Promise<void>;
|
|
71
262
|
}
|
|
72
263
|
|
|
73
|
-
export { Cli, CliAlias, CliApp, CliExample, CliGlobalOption, CliHelpInterceptor, CliOption, MoostCli,
|
|
264
|
+
export { Cli, CliAlias, CliApp, CliExample, CliGlobalOption, CliHelpInterceptor, CliOption, MoostCli, cliHelpInterceptor };
|
|
265
|
+
export type { TCliHandlerMeta, TMoostCliOpts };
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Controller, Description, Intercept, Moost, Optional, Param, Resolve, TInterceptorPriority, defineInterceptorFn, defineMoostEventHandler, getMoostMate, setInfactLoggingOptions, useControllerContext } from "moost";
|
|
2
|
-
import { WooksCli, createCliApp, useAutoHelp, useCliContext, useCliOption, useCommandLookupHelp } from "@wooksjs/event-cli";
|
|
1
|
+
import { Controller, Description, Intercept, Intercept as Intercept$1, Moost, Optional, Param, Resolve, TInterceptorPriority, defineInterceptorFn, defineMoostEventHandler, getMoostMate, setInfactLoggingOptions, useControllerContext } from "moost";
|
|
2
|
+
import { WooksCli, createCliApp, useAutoHelp, useCliContext, useCliContext as useCliContext$1, useCliOption, useCommandLookupHelp } from "@wooksjs/event-cli";
|
|
3
3
|
|
|
4
4
|
//#region packages/event-cli/src/meta-types.ts
|
|
5
5
|
function getCliMate() {
|
|
@@ -8,16 +8,46 @@ function getCliMate() {
|
|
|
8
8
|
|
|
9
9
|
//#endregion
|
|
10
10
|
//#region packages/event-cli/src/decorators/cli.decorator.ts
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* ## Define CLI Command
|
|
13
|
+
* ### @MethodDecorator
|
|
14
|
+
*
|
|
15
|
+
* Command path segments may be separated by / or space.
|
|
16
|
+
*
|
|
17
|
+
* For example the folowing path are interpreted the same:
|
|
18
|
+
* - "command test use:dev :name"
|
|
19
|
+
* - "command/test/use:dev/:name"
|
|
20
|
+
*
|
|
21
|
+
* Where name will become an argument
|
|
22
|
+
*
|
|
23
|
+
* @param path - command path
|
|
24
|
+
* @returns
|
|
25
|
+
*/ function Cli(path) {
|
|
12
26
|
return getCliMate().decorate("handlers", {
|
|
13
27
|
path: path?.replace(/\s+/g, "/"),
|
|
14
28
|
type: "CLI"
|
|
15
29
|
}, true);
|
|
16
30
|
}
|
|
17
|
-
|
|
31
|
+
/**
|
|
32
|
+
* ## Define CLI Command Alias
|
|
33
|
+
* ### @MethodDecorator
|
|
34
|
+
*
|
|
35
|
+
* Use it to define alias for @Cli('...') command
|
|
36
|
+
*
|
|
37
|
+
* @param path - command alias path
|
|
38
|
+
* @returns
|
|
39
|
+
*/ function CliAlias(alias) {
|
|
18
40
|
return getCliMate().decorate("cliAliases", alias, true);
|
|
19
41
|
}
|
|
20
|
-
|
|
42
|
+
/**
|
|
43
|
+
* ## Define CLI Example
|
|
44
|
+
* ### @MethodDecorator
|
|
45
|
+
*
|
|
46
|
+
* Use it to define example for Cli Help display
|
|
47
|
+
*
|
|
48
|
+
* @param path - command alias path
|
|
49
|
+
* @returns
|
|
50
|
+
*/ function CliExample(cmd, description) {
|
|
21
51
|
return getCliMate().decorate("cliExamples", {
|
|
22
52
|
cmd,
|
|
23
53
|
description
|
|
@@ -33,11 +63,35 @@ function formatParams(keys) {
|
|
|
33
63
|
|
|
34
64
|
//#endregion
|
|
35
65
|
//#region packages/event-cli/src/decorators/option.decorator.ts
|
|
36
|
-
|
|
66
|
+
/**
|
|
67
|
+
* ## Define CLI Option
|
|
68
|
+
* ### @ParameterDecorator
|
|
69
|
+
* Use together with @Description('...') to document cli option
|
|
70
|
+
*
|
|
71
|
+
* ```ts
|
|
72
|
+
* │ @Cli('command')
|
|
73
|
+
* │ command(
|
|
74
|
+
* │ @Description('Test option...')
|
|
75
|
+
* │ @CliOption('test', 't')
|
|
76
|
+
* │ test: boolean,
|
|
77
|
+
* │ ) {
|
|
78
|
+
* │ return `test=${ test }`
|
|
79
|
+
* │ }
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* @param keys list of keys (short and long alternatives)
|
|
83
|
+
* @returns
|
|
84
|
+
*/ function CliOption(...keys) {
|
|
37
85
|
const mate = getCliMate();
|
|
38
86
|
return mate.apply(mate.decorate("cliOptionsKeys", keys, false), Resolve(() => useCliOption(keys[0]), formatParams(keys).join(", ")));
|
|
39
87
|
}
|
|
40
|
-
|
|
88
|
+
/**
|
|
89
|
+
* ## Define Global CLI Option
|
|
90
|
+
* ### @ClassDecorator
|
|
91
|
+
* The option described here will appear in every command instructions
|
|
92
|
+
* @param option keys and description of CLI option
|
|
93
|
+
* @returns
|
|
94
|
+
*/ function CliGlobalOption(option) {
|
|
41
95
|
const mate = getCliMate();
|
|
42
96
|
return mate.decorate("cliOptions", option, true);
|
|
43
97
|
}
|
|
@@ -56,25 +110,54 @@ function _define_property$1(obj, key, value) {
|
|
|
56
110
|
}
|
|
57
111
|
const LOGGER_TITLE = "moost-cli";
|
|
58
112
|
const CONTEXT_TYPE = "CLI";
|
|
59
|
-
|
|
113
|
+
/**
|
|
114
|
+
* ## Moost Cli Adapter
|
|
115
|
+
*
|
|
116
|
+
* Moost Adapter for CLI events
|
|
117
|
+
*
|
|
118
|
+
* ```ts
|
|
119
|
+
* │ // Quick example
|
|
120
|
+
* │ import { MoostCli, Cli, CliOption, cliHelpInterceptor } from '@moostjs/event-cli'
|
|
121
|
+
* │ import { Moost, Param } from 'moost'
|
|
122
|
+
* │
|
|
123
|
+
* │ class MyApp extends Moost {
|
|
124
|
+
* │ @Cli('command/:arg')
|
|
125
|
+
* │ command(
|
|
126
|
+
* │ @Param('arg')
|
|
127
|
+
* │ arg: string,
|
|
128
|
+
* │ @CliOption('test', 't')
|
|
129
|
+
* │ test: boolean,
|
|
130
|
+
* │ ) {
|
|
131
|
+
* │ return `command run with flag arg=${ arg }, test=${ test }`
|
|
132
|
+
* │ }
|
|
133
|
+
* │ }
|
|
134
|
+
* │
|
|
135
|
+
* │ const app = new MyApp()
|
|
136
|
+
* │ app.applyGlobalInterceptors(cliHelpInterceptor())
|
|
137
|
+
* │
|
|
138
|
+
* │ const cli = new MoostCli()
|
|
139
|
+
* │ app.adapter(cli)
|
|
140
|
+
* │ app.init()
|
|
141
|
+
* ```
|
|
142
|
+
*/ var MoostCli = class {
|
|
60
143
|
async onNotFound() {
|
|
61
|
-
const pathParams = useCliContext().store("event").get("pathParams") || [];
|
|
144
|
+
const pathParams = useCliContext$1().store("event").get("pathParams") || [];
|
|
62
145
|
const response = await defineMoostEventHandler({
|
|
63
146
|
loggerTitle: LOGGER_TITLE,
|
|
64
147
|
getIterceptorHandler: () => this.moost?.getGlobalInterceptorHandler(),
|
|
65
148
|
getControllerInstance: () => this.moost,
|
|
66
|
-
callControllerMethod: () =>
|
|
149
|
+
callControllerMethod: () => void 0,
|
|
67
150
|
logErrors: this.opts?.debug,
|
|
68
151
|
targetPath: "",
|
|
69
152
|
handlerType: "__SYSTEM__"
|
|
70
153
|
})();
|
|
71
|
-
if (response ===
|
|
154
|
+
if (response === void 0) this.cliApp.onUnknownCommand(pathParams);
|
|
72
155
|
return response;
|
|
73
156
|
}
|
|
74
157
|
onInit(moost) {
|
|
75
158
|
this.moost = moost;
|
|
76
159
|
const boolean = Object.entries(this.optionTypes).filter(([_key, val]) => val.length === 1 && val[0] === Boolean).map(([key, _val]) => key);
|
|
77
|
-
this.cliApp.run(
|
|
160
|
+
this.cliApp.run(void 0, { boolean });
|
|
78
161
|
}
|
|
79
162
|
bindHandler(opts) {
|
|
80
163
|
let fn;
|
|
@@ -97,7 +180,7 @@ var MoostCli = class {
|
|
|
97
180
|
});
|
|
98
181
|
const meta = getCliMate().read(opts.fakeInstance, opts.method);
|
|
99
182
|
const classMeta = getCliMate().read(opts.fakeInstance);
|
|
100
|
-
const cliOptions = new Map();
|
|
183
|
+
const cliOptions = /* @__PURE__ */ new Map();
|
|
101
184
|
[
|
|
102
185
|
...this.opts?.globalCliOptions?.length ? this.opts.globalCliOptions : [],
|
|
103
186
|
...classMeta?.cliOptions || [],
|
|
@@ -131,7 +214,7 @@ var MoostCli = class {
|
|
|
131
214
|
handler: fn,
|
|
132
215
|
onRegister: (path$1, aliasType, route) => {
|
|
133
216
|
opts.register(handler, path$1, route?.getArgs() || routerBinding.getArgs());
|
|
134
|
-
if (this.opts?.debug) opts.logHandler(
|
|
217
|
+
if (this.opts?.debug) opts.logHandler(`[36m(${aliasTypes[aliasType]})[32m${path$1}`);
|
|
135
218
|
}
|
|
136
219
|
});
|
|
137
220
|
opts.register(handler, targetPath, routerBinding.getArgs());
|
|
@@ -169,7 +252,18 @@ const aliasTypes = [
|
|
|
169
252
|
|
|
170
253
|
//#endregion
|
|
171
254
|
//#region packages/event-cli/src/interceptors/help.interceptor.ts
|
|
172
|
-
|
|
255
|
+
/**
|
|
256
|
+
* ### Interceptor Factory for CliHelpRenderer
|
|
257
|
+
*
|
|
258
|
+
* By default intercepts cli calls with flag --help
|
|
259
|
+
* and prints help.
|
|
260
|
+
*
|
|
261
|
+
* ```js
|
|
262
|
+
* new Moost().applyGlobalInterceptors(cliHelpInterceptor({ colors: true }))
|
|
263
|
+
* ```
|
|
264
|
+
* @param opts {} { helpOptions: ['help', 'h'], colors: true } cli options to invoke help renderer
|
|
265
|
+
* @returns TInterceptorFn
|
|
266
|
+
*/ const cliHelpInterceptor = (opts) => defineInterceptorFn(() => {
|
|
173
267
|
try {
|
|
174
268
|
if (useAutoHelp(opts?.helpOptions, opts?.colors)) return "";
|
|
175
269
|
} catch (error) {}
|
|
@@ -178,7 +272,27 @@ const cliHelpInterceptor = (opts) => defineInterceptorFn(() => {
|
|
|
178
272
|
if (!getMethod()) useCommandLookupHelp(opts.lookupLevel);
|
|
179
273
|
}
|
|
180
274
|
}, TInterceptorPriority.BEFORE_ALL);
|
|
181
|
-
|
|
275
|
+
/**
|
|
276
|
+
* ## @Decorator
|
|
277
|
+
* ### Interceptor Factory for CliHelpRenderer
|
|
278
|
+
*
|
|
279
|
+
* By default intercepts cli calls with flag --help
|
|
280
|
+
* and prints help.
|
|
281
|
+
*
|
|
282
|
+
* ```ts
|
|
283
|
+
* // default configuration
|
|
284
|
+
* • @CliHelpInterceptor({ helpOptions: 'help', colors: true })
|
|
285
|
+
*
|
|
286
|
+
* // additional option -h to invoke help renderer
|
|
287
|
+
* • @CliHelpInterceptor({ helpOptions: ['help', 'h'], colors: true })
|
|
288
|
+
*
|
|
289
|
+
* // redefine cli option to invoke help renderer
|
|
290
|
+
* • @CliHelpInterceptor({ helpOptions: ['usage'] })
|
|
291
|
+
* ```
|
|
292
|
+
*
|
|
293
|
+
* @param opts {} { helpOptions: ['help', 'h'], colors: true } cli options to invoke help renderer
|
|
294
|
+
* @returns Decorator
|
|
295
|
+
*/ const CliHelpInterceptor = (...opts) => Intercept$1(cliHelpInterceptor(...opts));
|
|
182
296
|
|
|
183
297
|
//#endregion
|
|
184
298
|
//#region packages/event-cli/src/quick-cli.ts
|
|
@@ -192,7 +306,23 @@ function _define_property(obj, key, value) {
|
|
|
192
306
|
else obj[key] = value;
|
|
193
307
|
return obj;
|
|
194
308
|
}
|
|
195
|
-
|
|
309
|
+
/**
|
|
310
|
+
* Quick CLI App factory class.
|
|
311
|
+
*
|
|
312
|
+
* Use this class to quickly build a CLI application with controllers, help options,
|
|
313
|
+
* and global CLI options. It extends the Moost class and wraps the initialization of MoostCli.
|
|
314
|
+
*
|
|
315
|
+
* @example
|
|
316
|
+
* ```typescript
|
|
317
|
+
* import { CliApp } from '@wooksjs/event-cli'
|
|
318
|
+
* import { Commands } from './commands.controller.ts'
|
|
319
|
+
* new CliApp()
|
|
320
|
+
* .controllers(Commands)
|
|
321
|
+
* .useHelp({ name: 'myCli' })
|
|
322
|
+
* .useOptions([{ keys: ['help'], description: 'Display instructions for the command.' }])
|
|
323
|
+
* .start()
|
|
324
|
+
* ```
|
|
325
|
+
*/ var CliApp = class extends Moost {
|
|
196
326
|
/**
|
|
197
327
|
* Registers one or more CLI controllers.
|
|
198
328
|
*
|
|
@@ -245,7 +375,7 @@ var CliApp = class extends Moost {
|
|
|
245
375
|
maxWidth: this._helpOpts.maxWidth,
|
|
246
376
|
maxLeft: this._helpOpts.maxLeft,
|
|
247
377
|
mark: this._helpOpts.mark
|
|
248
|
-
} :
|
|
378
|
+
} : void 0 },
|
|
249
379
|
globalCliOptions: this._globalOpts
|
|
250
380
|
});
|
|
251
381
|
this.adapter(cli);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moostjs/event-cli",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.30",
|
|
4
4
|
"description": "@moostjs/event-cli",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -37,14 +37,16 @@
|
|
|
37
37
|
"url": "https://github.com/moostjs/moostjs/issues"
|
|
38
38
|
},
|
|
39
39
|
"homepage": "https://github.com/moostjs/moostjs/tree/main/packages/event-cli#readme",
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@wooksjs/event-core": "^0.6.1",
|
|
42
|
+
"wooks": "^0.6.1",
|
|
43
|
+
"moost": "^0.5.30"
|
|
44
|
+
},
|
|
40
45
|
"dependencies": {
|
|
41
|
-
"@wooksjs/event-cli": "^0.6.
|
|
42
|
-
"@wooksjs/event-core": "^0.6.0",
|
|
43
|
-
"wooks": "^0.6.0",
|
|
44
|
-
"moost": "^0.5.28"
|
|
46
|
+
"@wooksjs/event-cli": "^0.6.1"
|
|
45
47
|
},
|
|
46
48
|
"devDependencies": {
|
|
47
|
-
"vitest": "
|
|
49
|
+
"vitest": "3.2.4"
|
|
48
50
|
},
|
|
49
51
|
"scripts": {
|
|
50
52
|
"pub": "pnpm publish --access public",
|