@moostjs/event-cli 0.3.11 → 0.3.13
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 +0 -124
- package/dist/index.d.ts +208 -219
- package/dist/index.mjs +0 -124
- package/package.json +6 -5
package/dist/index.cjs
CHANGED
|
@@ -9,36 +9,6 @@ function getCliMate() {
|
|
|
9
9
|
|
|
10
10
|
const LOGGER_TITLE = 'moost-cli';
|
|
11
11
|
const CONTEXT_TYPE = 'CLI';
|
|
12
|
-
/**
|
|
13
|
-
* ## Moost Cli Adapter
|
|
14
|
-
*
|
|
15
|
-
* Moost Adapter for CLI events
|
|
16
|
-
*
|
|
17
|
-
* ```ts
|
|
18
|
-
* │ // Quick example
|
|
19
|
-
* │ import { MoostCli, Cli, CliOption, cliHelpInterceptor } from '@moostjs/event-cli'
|
|
20
|
-
* │ import { Moost, Param } from 'moost'
|
|
21
|
-
* │
|
|
22
|
-
* │ class MyApp extends Moost {
|
|
23
|
-
* │ @Cli('command/:arg')
|
|
24
|
-
* │ command(
|
|
25
|
-
* │ @Param('arg')
|
|
26
|
-
* │ arg: string,
|
|
27
|
-
* │ @CliOption('test', 't')
|
|
28
|
-
* │ test: boolean,
|
|
29
|
-
* │ ) {
|
|
30
|
-
* │ return `command run with flag arg=${ arg }, test=${ test }`
|
|
31
|
-
* │ }
|
|
32
|
-
* │ }
|
|
33
|
-
* │
|
|
34
|
-
* │ const app = new MyApp()
|
|
35
|
-
* │ app.applyGlobalInterceptors(cliHelpInterceptor())
|
|
36
|
-
* │
|
|
37
|
-
* │ const cli = new MoostCli()
|
|
38
|
-
* │ app.adapter(cli)
|
|
39
|
-
* │ app.init()
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
12
|
class MoostCli {
|
|
43
13
|
constructor(opts) {
|
|
44
14
|
this.opts = opts;
|
|
@@ -99,7 +69,6 @@ class MoostCli {
|
|
|
99
69
|
const prefix = opts.prefix.replace(/\s+/g, '/') || '';
|
|
100
70
|
const makePath = (p) => `${prefix}/${p}`
|
|
101
71
|
.replace(/\/\/+/g, '/')
|
|
102
|
-
// avoid interpreting "cmd:tail" as "cmd/:tail"
|
|
103
72
|
.replace(/\/\\:/g, '\\:')
|
|
104
73
|
.replace(/^\/+/g, '');
|
|
105
74
|
if (!fn) {
|
|
@@ -173,96 +142,25 @@ function formatParams(keys) {
|
|
|
173
142
|
return names.map((n) => (n.length === 1 ? '-' + n : '--' + n));
|
|
174
143
|
}
|
|
175
144
|
|
|
176
|
-
/**
|
|
177
|
-
* ## Define CLI Option
|
|
178
|
-
* ### @ParameterDecorator
|
|
179
|
-
* Use together with @Description('...') to document cli option
|
|
180
|
-
*
|
|
181
|
-
* ```ts
|
|
182
|
-
* │ @Cli('command')
|
|
183
|
-
* │ command(
|
|
184
|
-
* │ @Description('Test option...')
|
|
185
|
-
* │ @CliOption('test', 't')
|
|
186
|
-
* │ test: boolean,
|
|
187
|
-
* │ ) {
|
|
188
|
-
* │ return `test=${ test }`
|
|
189
|
-
* │ }
|
|
190
|
-
* ```
|
|
191
|
-
*
|
|
192
|
-
* @param keys list of keys (short and long alternatives)
|
|
193
|
-
* @returns
|
|
194
|
-
*/
|
|
195
145
|
function CliOption(...keys) {
|
|
196
146
|
const mate = getCliMate();
|
|
197
147
|
return mate.apply(mate.decorate('cliOptionsKeys', keys, false), moost.Resolve(() => eventCli.useCliOption(keys[0]), formatParams(keys).join(', ')));
|
|
198
148
|
}
|
|
199
|
-
/**
|
|
200
|
-
* ## Define Global CLI Option
|
|
201
|
-
* ### @ClassDecorator
|
|
202
|
-
* The option described here will appear in every command instructions
|
|
203
|
-
* @param option keys and description of CLI option
|
|
204
|
-
* @returns
|
|
205
|
-
*/
|
|
206
149
|
function CliGlobalOption(option) {
|
|
207
150
|
const mate = getCliMate();
|
|
208
151
|
return mate.decorate('cliOptions', option, true);
|
|
209
152
|
}
|
|
210
153
|
|
|
211
|
-
/**
|
|
212
|
-
* ## Define CLI Command
|
|
213
|
-
* ### @MethodDecorator
|
|
214
|
-
*
|
|
215
|
-
* Command path segments may be separated by / or space.
|
|
216
|
-
*
|
|
217
|
-
* For example the folowing path are interpreted the same:
|
|
218
|
-
* - "command test use:dev :name"
|
|
219
|
-
* - "command/test/use:dev/:name"
|
|
220
|
-
*
|
|
221
|
-
* Where name will become an argument
|
|
222
|
-
*
|
|
223
|
-
* @param path - command path
|
|
224
|
-
* @returns
|
|
225
|
-
*/
|
|
226
154
|
function Cli(path) {
|
|
227
155
|
return getCliMate().decorate('handlers', { path: path?.replace(/\s+/g, '/'), type: 'CLI' }, true);
|
|
228
156
|
}
|
|
229
|
-
/**
|
|
230
|
-
* ## Define CLI Command Alias
|
|
231
|
-
* ### @MethodDecorator
|
|
232
|
-
*
|
|
233
|
-
* Use it to define alias for @Cli('...') command
|
|
234
|
-
*
|
|
235
|
-
* @param path - command alias path
|
|
236
|
-
* @returns
|
|
237
|
-
*/
|
|
238
157
|
function CliAlias(alias) {
|
|
239
158
|
return getCliMate().decorate('cliAliases', alias, true);
|
|
240
159
|
}
|
|
241
|
-
/**
|
|
242
|
-
* ## Define CLI Example
|
|
243
|
-
* ### @MethodDecorator
|
|
244
|
-
*
|
|
245
|
-
* Use it to define example for Cli Help display
|
|
246
|
-
*
|
|
247
|
-
* @param path - command alias path
|
|
248
|
-
* @returns
|
|
249
|
-
*/
|
|
250
160
|
function CliExample(cmd, description) {
|
|
251
161
|
return getCliMate().decorate('cliExamples', { cmd, description }, true);
|
|
252
162
|
}
|
|
253
163
|
|
|
254
|
-
/**
|
|
255
|
-
* ### Interceptor Factory for CliHelpRenderer
|
|
256
|
-
*
|
|
257
|
-
* By default intercepts cli calls with flag --help
|
|
258
|
-
* and prints help.
|
|
259
|
-
*
|
|
260
|
-
* ```js
|
|
261
|
-
* new Moost().applyGlobalInterceptors(cliHelpInterceptor({ colors: true }))
|
|
262
|
-
* ```
|
|
263
|
-
* @param opts {} { helpOptions: ['help', 'h'], colors: true } cli options to invoke help renderer
|
|
264
|
-
* @returns TInterceptorFn
|
|
265
|
-
*/
|
|
266
164
|
const cliHelpInterceptor = (opts) => {
|
|
267
165
|
return moost.defineInterceptorFn(() => {
|
|
268
166
|
try {
|
|
@@ -271,7 +169,6 @@ const cliHelpInterceptor = (opts) => {
|
|
|
271
169
|
}
|
|
272
170
|
}
|
|
273
171
|
catch (e) {
|
|
274
|
-
//
|
|
275
172
|
}
|
|
276
173
|
if (opts?.lookupLevel) {
|
|
277
174
|
const { getMethod } = moost.useControllerContext();
|
|
@@ -281,27 +178,6 @@ const cliHelpInterceptor = (opts) => {
|
|
|
281
178
|
}
|
|
282
179
|
}, moost.TInterceptorPriority.BEFORE_ALL);
|
|
283
180
|
};
|
|
284
|
-
/**
|
|
285
|
-
* ## @Decorator
|
|
286
|
-
* ### Interceptor Factory for CliHelpRenderer
|
|
287
|
-
*
|
|
288
|
-
* By default intercepts cli calls with flag --help
|
|
289
|
-
* and prints help.
|
|
290
|
-
*
|
|
291
|
-
* ```ts
|
|
292
|
-
* // default configuration
|
|
293
|
-
* • @CliHelpInterceptor({ helpOptions: 'help', colors: true })
|
|
294
|
-
*
|
|
295
|
-
* // additional option -h to invoke help renderer
|
|
296
|
-
* • @CliHelpInterceptor({ helpOptions: ['help', 'h'], colors: true })
|
|
297
|
-
*
|
|
298
|
-
* // redefine cli option to invoke help renderer
|
|
299
|
-
* • @CliHelpInterceptor({ helpOptions: ['usage'] })
|
|
300
|
-
* ```
|
|
301
|
-
*
|
|
302
|
-
* @param opts {} { helpOptions: ['help', 'h'], colors: true } cli options to invoke help renderer
|
|
303
|
-
* @returns Decorator
|
|
304
|
-
*/
|
|
305
181
|
const CliHelpInterceptor = (...opts) => moost.Intercept(cliHelpInterceptor(...opts));
|
|
306
182
|
|
|
307
183
|
Object.defineProperty(exports, "useCliContext", {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,219 +1,208 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* @
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
* @
|
|
80
|
-
*
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
* Use
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
*/
|
|
210
|
-
globalCliOptions?: {
|
|
211
|
-
keys: string[];
|
|
212
|
-
description?: string;
|
|
213
|
-
type?: TFunction;
|
|
214
|
-
}[];
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
export { useCliContext }
|
|
218
|
-
|
|
219
|
-
export { }
|
|
1
|
+
import * as moost from 'moost';
|
|
2
|
+
import { TMoostAdapter, Moost, TMoostAdapterOptions } from 'moost';
|
|
3
|
+
import { WooksCli, TWooksCliOptions } from '@wooksjs/event-cli';
|
|
4
|
+
export { useCliContext } from '@wooksjs/event-cli';
|
|
5
|
+
|
|
6
|
+
type TFunction = Function;
|
|
7
|
+
|
|
8
|
+
interface TCliHandlerMeta {
|
|
9
|
+
path: string;
|
|
10
|
+
}
|
|
11
|
+
interface TMoostCliOpts {
|
|
12
|
+
/**
|
|
13
|
+
* WooksCli options or instance
|
|
14
|
+
*/
|
|
15
|
+
wooksCli?: WooksCli | TWooksCliOptions;
|
|
16
|
+
/**
|
|
17
|
+
* more internal logs are printed when true
|
|
18
|
+
*/
|
|
19
|
+
debug?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Array of cli options applicable to every cli command
|
|
22
|
+
*/
|
|
23
|
+
globalCliOptions?: {
|
|
24
|
+
keys: string[];
|
|
25
|
+
description?: string;
|
|
26
|
+
type?: TFunction;
|
|
27
|
+
}[];
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* ## Moost Cli Adapter
|
|
31
|
+
*
|
|
32
|
+
* Moost Adapter for CLI events
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* │ // Quick example
|
|
36
|
+
* │ import { MoostCli, Cli, CliOption, cliHelpInterceptor } from '@moostjs/event-cli'
|
|
37
|
+
* │ import { Moost, Param } from 'moost'
|
|
38
|
+
* │
|
|
39
|
+
* │ class MyApp extends Moost {
|
|
40
|
+
* │ @Cli('command/:arg')
|
|
41
|
+
* │ command(
|
|
42
|
+
* │ @Param('arg')
|
|
43
|
+
* │ arg: string,
|
|
44
|
+
* │ @CliOption('test', 't')
|
|
45
|
+
* │ test: boolean,
|
|
46
|
+
* │ ) {
|
|
47
|
+
* │ return `command run with flag arg=${ arg }, test=${ test }`
|
|
48
|
+
* │ }
|
|
49
|
+
* │ }
|
|
50
|
+
* │
|
|
51
|
+
* │ const app = new MyApp()
|
|
52
|
+
* │ app.applyGlobalInterceptors(cliHelpInterceptor())
|
|
53
|
+
* │
|
|
54
|
+
* │ const cli = new MoostCli()
|
|
55
|
+
* │ app.adapter(cli)
|
|
56
|
+
* │ app.init()
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
declare class MoostCli implements TMoostAdapter<TCliHandlerMeta> {
|
|
60
|
+
protected opts?: TMoostCliOpts | undefined;
|
|
61
|
+
protected cliApp: WooksCli;
|
|
62
|
+
protected optionTypes: Record<string, TFunction[]>;
|
|
63
|
+
constructor(opts?: TMoostCliOpts | undefined);
|
|
64
|
+
onNotFound(): Promise<unknown>;
|
|
65
|
+
protected moost?: Moost;
|
|
66
|
+
onInit(moost: Moost): void;
|
|
67
|
+
bindHandler<T extends object = object>(opts: TMoostAdapterOptions<TCliHandlerMeta, T>): void;
|
|
68
|
+
}
|
|
69
|
+
|
|
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
|
+
/**
|
|
141
|
+
* ### Interceptor Factory for CliHelpRenderer
|
|
142
|
+
*
|
|
143
|
+
* By default intercepts cli calls with flag --help
|
|
144
|
+
* and prints help.
|
|
145
|
+
*
|
|
146
|
+
* ```js
|
|
147
|
+
* new Moost().applyGlobalInterceptors(cliHelpInterceptor({ colors: true }))
|
|
148
|
+
* ```
|
|
149
|
+
* @param opts {} { helpOptions: ['help', 'h'], colors: true } cli options to invoke help renderer
|
|
150
|
+
* @returns TInterceptorFn
|
|
151
|
+
*/
|
|
152
|
+
declare const cliHelpInterceptor: (opts?: {
|
|
153
|
+
/**
|
|
154
|
+
* CLI Options that invoke help
|
|
155
|
+
* ```js
|
|
156
|
+
* helpOptions: ['help', 'h']
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
helpOptions?: string[];
|
|
160
|
+
/**
|
|
161
|
+
* Enable colored help
|
|
162
|
+
*/
|
|
163
|
+
colors?: boolean;
|
|
164
|
+
/**
|
|
165
|
+
* Enable lookup for a command
|
|
166
|
+
*/
|
|
167
|
+
lookupLevel?: number;
|
|
168
|
+
}) => moost.TInterceptorFn;
|
|
169
|
+
/**
|
|
170
|
+
* ## @Decorator
|
|
171
|
+
* ### Interceptor Factory for CliHelpRenderer
|
|
172
|
+
*
|
|
173
|
+
* By default intercepts cli calls with flag --help
|
|
174
|
+
* and prints help.
|
|
175
|
+
*
|
|
176
|
+
* ```ts
|
|
177
|
+
* // default configuration
|
|
178
|
+
* • @CliHelpInterceptor({ helpOptions: 'help', colors: true })
|
|
179
|
+
*
|
|
180
|
+
* // additional option -h to invoke help renderer
|
|
181
|
+
* • @CliHelpInterceptor({ helpOptions: ['help', 'h'], colors: true })
|
|
182
|
+
*
|
|
183
|
+
* // redefine cli option to invoke help renderer
|
|
184
|
+
* • @CliHelpInterceptor({ helpOptions: ['usage'] })
|
|
185
|
+
* ```
|
|
186
|
+
*
|
|
187
|
+
* @param opts {} { helpOptions: ['help', 'h'], colors: true } cli options to invoke help renderer
|
|
188
|
+
* @returns Decorator
|
|
189
|
+
*/
|
|
190
|
+
declare const CliHelpInterceptor: (opts?: {
|
|
191
|
+
/**
|
|
192
|
+
* CLI Options that invoke help
|
|
193
|
+
* ```js
|
|
194
|
+
* helpOptions: ['help', 'h']
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
helpOptions?: string[] | undefined;
|
|
198
|
+
/**
|
|
199
|
+
* Enable colored help
|
|
200
|
+
*/
|
|
201
|
+
colors?: boolean | undefined;
|
|
202
|
+
/**
|
|
203
|
+
* Enable lookup for a command
|
|
204
|
+
*/
|
|
205
|
+
lookupLevel?: number | undefined;
|
|
206
|
+
} | undefined) => ClassDecorator & MethodDecorator;
|
|
207
|
+
|
|
208
|
+
export { Cli, CliAlias, CliExample, CliGlobalOption, CliHelpInterceptor, CliOption, MoostCli, type TCliHandlerMeta, type TMoostCliOpts, cliHelpInterceptor };
|
package/dist/index.mjs
CHANGED
|
@@ -8,36 +8,6 @@ function getCliMate() {
|
|
|
8
8
|
|
|
9
9
|
const LOGGER_TITLE = 'moost-cli';
|
|
10
10
|
const CONTEXT_TYPE = 'CLI';
|
|
11
|
-
/**
|
|
12
|
-
* ## Moost Cli Adapter
|
|
13
|
-
*
|
|
14
|
-
* Moost Adapter for CLI events
|
|
15
|
-
*
|
|
16
|
-
* ```ts
|
|
17
|
-
* │ // Quick example
|
|
18
|
-
* │ import { MoostCli, Cli, CliOption, cliHelpInterceptor } from '@moostjs/event-cli'
|
|
19
|
-
* │ import { Moost, Param } from 'moost'
|
|
20
|
-
* │
|
|
21
|
-
* │ class MyApp extends Moost {
|
|
22
|
-
* │ @Cli('command/:arg')
|
|
23
|
-
* │ command(
|
|
24
|
-
* │ @Param('arg')
|
|
25
|
-
* │ arg: string,
|
|
26
|
-
* │ @CliOption('test', 't')
|
|
27
|
-
* │ test: boolean,
|
|
28
|
-
* │ ) {
|
|
29
|
-
* │ return `command run with flag arg=${ arg }, test=${ test }`
|
|
30
|
-
* │ }
|
|
31
|
-
* │ }
|
|
32
|
-
* │
|
|
33
|
-
* │ const app = new MyApp()
|
|
34
|
-
* │ app.applyGlobalInterceptors(cliHelpInterceptor())
|
|
35
|
-
* │
|
|
36
|
-
* │ const cli = new MoostCli()
|
|
37
|
-
* │ app.adapter(cli)
|
|
38
|
-
* │ app.init()
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
11
|
class MoostCli {
|
|
42
12
|
constructor(opts) {
|
|
43
13
|
this.opts = opts;
|
|
@@ -98,7 +68,6 @@ class MoostCli {
|
|
|
98
68
|
const prefix = opts.prefix.replace(/\s+/g, '/') || '';
|
|
99
69
|
const makePath = (p) => `${prefix}/${p}`
|
|
100
70
|
.replace(/\/\/+/g, '/')
|
|
101
|
-
// avoid interpreting "cmd:tail" as "cmd/:tail"
|
|
102
71
|
.replace(/\/\\:/g, '\\:')
|
|
103
72
|
.replace(/^\/+/g, '');
|
|
104
73
|
if (!fn) {
|
|
@@ -172,96 +141,25 @@ function formatParams(keys) {
|
|
|
172
141
|
return names.map((n) => (n.length === 1 ? '-' + n : '--' + n));
|
|
173
142
|
}
|
|
174
143
|
|
|
175
|
-
/**
|
|
176
|
-
* ## Define CLI Option
|
|
177
|
-
* ### @ParameterDecorator
|
|
178
|
-
* Use together with @Description('...') to document cli option
|
|
179
|
-
*
|
|
180
|
-
* ```ts
|
|
181
|
-
* │ @Cli('command')
|
|
182
|
-
* │ command(
|
|
183
|
-
* │ @Description('Test option...')
|
|
184
|
-
* │ @CliOption('test', 't')
|
|
185
|
-
* │ test: boolean,
|
|
186
|
-
* │ ) {
|
|
187
|
-
* │ return `test=${ test }`
|
|
188
|
-
* │ }
|
|
189
|
-
* ```
|
|
190
|
-
*
|
|
191
|
-
* @param keys list of keys (short and long alternatives)
|
|
192
|
-
* @returns
|
|
193
|
-
*/
|
|
194
144
|
function CliOption(...keys) {
|
|
195
145
|
const mate = getCliMate();
|
|
196
146
|
return mate.apply(mate.decorate('cliOptionsKeys', keys, false), Resolve(() => useCliOption(keys[0]), formatParams(keys).join(', ')));
|
|
197
147
|
}
|
|
198
|
-
/**
|
|
199
|
-
* ## Define Global CLI Option
|
|
200
|
-
* ### @ClassDecorator
|
|
201
|
-
* The option described here will appear in every command instructions
|
|
202
|
-
* @param option keys and description of CLI option
|
|
203
|
-
* @returns
|
|
204
|
-
*/
|
|
205
148
|
function CliGlobalOption(option) {
|
|
206
149
|
const mate = getCliMate();
|
|
207
150
|
return mate.decorate('cliOptions', option, true);
|
|
208
151
|
}
|
|
209
152
|
|
|
210
|
-
/**
|
|
211
|
-
* ## Define CLI Command
|
|
212
|
-
* ### @MethodDecorator
|
|
213
|
-
*
|
|
214
|
-
* Command path segments may be separated by / or space.
|
|
215
|
-
*
|
|
216
|
-
* For example the folowing path are interpreted the same:
|
|
217
|
-
* - "command test use:dev :name"
|
|
218
|
-
* - "command/test/use:dev/:name"
|
|
219
|
-
*
|
|
220
|
-
* Where name will become an argument
|
|
221
|
-
*
|
|
222
|
-
* @param path - command path
|
|
223
|
-
* @returns
|
|
224
|
-
*/
|
|
225
153
|
function Cli(path) {
|
|
226
154
|
return getCliMate().decorate('handlers', { path: path?.replace(/\s+/g, '/'), type: 'CLI' }, true);
|
|
227
155
|
}
|
|
228
|
-
/**
|
|
229
|
-
* ## Define CLI Command Alias
|
|
230
|
-
* ### @MethodDecorator
|
|
231
|
-
*
|
|
232
|
-
* Use it to define alias for @Cli('...') command
|
|
233
|
-
*
|
|
234
|
-
* @param path - command alias path
|
|
235
|
-
* @returns
|
|
236
|
-
*/
|
|
237
156
|
function CliAlias(alias) {
|
|
238
157
|
return getCliMate().decorate('cliAliases', alias, true);
|
|
239
158
|
}
|
|
240
|
-
/**
|
|
241
|
-
* ## Define CLI Example
|
|
242
|
-
* ### @MethodDecorator
|
|
243
|
-
*
|
|
244
|
-
* Use it to define example for Cli Help display
|
|
245
|
-
*
|
|
246
|
-
* @param path - command alias path
|
|
247
|
-
* @returns
|
|
248
|
-
*/
|
|
249
159
|
function CliExample(cmd, description) {
|
|
250
160
|
return getCliMate().decorate('cliExamples', { cmd, description }, true);
|
|
251
161
|
}
|
|
252
162
|
|
|
253
|
-
/**
|
|
254
|
-
* ### Interceptor Factory for CliHelpRenderer
|
|
255
|
-
*
|
|
256
|
-
* By default intercepts cli calls with flag --help
|
|
257
|
-
* and prints help.
|
|
258
|
-
*
|
|
259
|
-
* ```js
|
|
260
|
-
* new Moost().applyGlobalInterceptors(cliHelpInterceptor({ colors: true }))
|
|
261
|
-
* ```
|
|
262
|
-
* @param opts {} { helpOptions: ['help', 'h'], colors: true } cli options to invoke help renderer
|
|
263
|
-
* @returns TInterceptorFn
|
|
264
|
-
*/
|
|
265
163
|
const cliHelpInterceptor = (opts) => {
|
|
266
164
|
return defineInterceptorFn(() => {
|
|
267
165
|
try {
|
|
@@ -270,7 +168,6 @@ const cliHelpInterceptor = (opts) => {
|
|
|
270
168
|
}
|
|
271
169
|
}
|
|
272
170
|
catch (e) {
|
|
273
|
-
//
|
|
274
171
|
}
|
|
275
172
|
if (opts?.lookupLevel) {
|
|
276
173
|
const { getMethod } = useControllerContext();
|
|
@@ -280,27 +177,6 @@ const cliHelpInterceptor = (opts) => {
|
|
|
280
177
|
}
|
|
281
178
|
}, TInterceptorPriority.BEFORE_ALL);
|
|
282
179
|
};
|
|
283
|
-
/**
|
|
284
|
-
* ## @Decorator
|
|
285
|
-
* ### Interceptor Factory for CliHelpRenderer
|
|
286
|
-
*
|
|
287
|
-
* By default intercepts cli calls with flag --help
|
|
288
|
-
* and prints help.
|
|
289
|
-
*
|
|
290
|
-
* ```ts
|
|
291
|
-
* // default configuration
|
|
292
|
-
* • @CliHelpInterceptor({ helpOptions: 'help', colors: true })
|
|
293
|
-
*
|
|
294
|
-
* // additional option -h to invoke help renderer
|
|
295
|
-
* • @CliHelpInterceptor({ helpOptions: ['help', 'h'], colors: true })
|
|
296
|
-
*
|
|
297
|
-
* // redefine cli option to invoke help renderer
|
|
298
|
-
* • @CliHelpInterceptor({ helpOptions: ['usage'] })
|
|
299
|
-
* ```
|
|
300
|
-
*
|
|
301
|
-
* @param opts {} { helpOptions: ['help', 'h'], colors: true } cli options to invoke help renderer
|
|
302
|
-
* @returns Decorator
|
|
303
|
-
*/
|
|
304
180
|
const CliHelpInterceptor = (...opts) => Intercept(cliHelpInterceptor(...opts));
|
|
305
181
|
|
|
306
182
|
export { Cli, CliAlias, CliExample, CliGlobalOption, CliHelpInterceptor, CliOption, MoostCli, cliHelpInterceptor };
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moostjs/event-cli",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.13",
|
|
4
4
|
"description": "@moostjs/event-cli",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
8
|
"sideEffects": false,
|
|
9
9
|
"exports": {
|
|
10
|
+
"./package.json": "./package.json",
|
|
10
11
|
".": {
|
|
11
12
|
"import": "./dist/index.mjs",
|
|
12
13
|
"require": "./dist/index.cjs",
|
|
@@ -37,9 +38,9 @@
|
|
|
37
38
|
"homepage": "https://github.com/moostjs/moostjs/tree/main/packages/event-cli#readme",
|
|
38
39
|
"peerDependencies": {},
|
|
39
40
|
"dependencies": {
|
|
40
|
-
"moost": "0.3.
|
|
41
|
-
"wooks": "^0.4.
|
|
42
|
-
"@wooksjs/event-core": "^0.4.
|
|
43
|
-
"@wooksjs/event-cli": "^0.4.
|
|
41
|
+
"moost": "0.3.13",
|
|
42
|
+
"wooks": "^0.4.15",
|
|
43
|
+
"@wooksjs/event-core": "^0.4.15",
|
|
44
|
+
"@wooksjs/event-cli": "^0.4.15"
|
|
44
45
|
}
|
|
45
46
|
}
|