@wooksjs/event-cli 0.4.11 → 0.4.12
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 +1 -152
- package/dist/index.d.ts +266 -282
- package/dist/index.mjs +1 -152
- package/package.json +12 -4
package/dist/index.cjs
CHANGED
|
@@ -14,11 +14,6 @@ function createCliContext(data, options) {
|
|
|
14
14
|
options,
|
|
15
15
|
});
|
|
16
16
|
}
|
|
17
|
-
/**
|
|
18
|
-
* Wrapper on top of useEventContext that provides
|
|
19
|
-
* proper context types for CLI event
|
|
20
|
-
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
21
|
-
*/
|
|
22
17
|
function useCliContext() {
|
|
23
18
|
return eventCore.useEventContext('CLI');
|
|
24
19
|
}
|
|
@@ -37,53 +32,15 @@ class WooksCli extends wooks.WooksAdapterBase {
|
|
|
37
32
|
? opts.cliHelp
|
|
38
33
|
: new cliHelp.CliHelpRenderer(opts?.cliHelp);
|
|
39
34
|
}
|
|
40
|
-
/**
|
|
41
|
-
* ### Register CLI Command
|
|
42
|
-
* Command path segments may be separated by / or space.
|
|
43
|
-
*
|
|
44
|
-
* For example the folowing path are interpreted the same:
|
|
45
|
-
* - "command test use:dev :name"
|
|
46
|
-
* - "command/test/use:dev/:name"
|
|
47
|
-
*
|
|
48
|
-
* Where name will become an argument
|
|
49
|
-
*
|
|
50
|
-
* ```js
|
|
51
|
-
* // example without options
|
|
52
|
-
* app.cli('command/:arg', () => 'arg = ' + useRouteParams().params.arg )
|
|
53
|
-
*
|
|
54
|
-
* // example with options
|
|
55
|
-
* app.cli('command/:arg', {
|
|
56
|
-
* description: 'Description of the command',
|
|
57
|
-
* options: [{ keys: ['project', 'p'], description: 'Description of the option', value: 'myProject' }],
|
|
58
|
-
* args: { arg: 'Description of the arg' },
|
|
59
|
-
* aliases: ['cmd'], // alias "cmd/:arg" will be registered
|
|
60
|
-
* examples: [{
|
|
61
|
-
* description: 'Example of usage with someProject',
|
|
62
|
-
* cmd: 'argValue -p=someProject',
|
|
63
|
-
* // will result in help display:
|
|
64
|
-
* // "# Example of usage with someProject\n" +
|
|
65
|
-
* // "$ myCli command argValue -p=someProject\n"
|
|
66
|
-
* }],
|
|
67
|
-
* handler: () => 'arg = ' + useRouteParams().params.arg
|
|
68
|
-
* })
|
|
69
|
-
* ```
|
|
70
|
-
*
|
|
71
|
-
* @param path command path
|
|
72
|
-
* @param _options handler or options
|
|
73
|
-
*
|
|
74
|
-
* @returns
|
|
75
|
-
*/
|
|
76
35
|
cli(path, _options) {
|
|
77
36
|
const options = typeof _options === 'function' ? { handler: _options } : _options;
|
|
78
37
|
const handler = typeof _options === 'function' ? _options : _options.handler;
|
|
79
38
|
const makePath = (s) => '/' + s.replace(/\s+/g, '/');
|
|
80
|
-
// register handler
|
|
81
39
|
const targetPath = makePath(path);
|
|
82
40
|
const routed = this.on('CLI', targetPath, handler);
|
|
83
41
|
if (options.onRegister) {
|
|
84
42
|
options.onRegister(targetPath, 0, routed);
|
|
85
43
|
}
|
|
86
|
-
// register direct aliases
|
|
87
44
|
for (const alias of options.aliases || []) {
|
|
88
45
|
const vars = routed.getArgs().map((k) => ':' + k).join('/');
|
|
89
46
|
const targetPath = makePath(alias) + (vars ? '/' + vars : '');
|
|
@@ -92,7 +49,6 @@ class WooksCli extends wooks.WooksAdapterBase {
|
|
|
92
49
|
options.onRegister(targetPath, 1, routed);
|
|
93
50
|
}
|
|
94
51
|
}
|
|
95
|
-
// register helpCli entry
|
|
96
52
|
const command = routed.getStaticPart().replace(/\//g, ' ').trim();
|
|
97
53
|
const args = {
|
|
98
54
|
...(options.args || {}),
|
|
@@ -104,7 +60,7 @@ class WooksCli extends wooks.WooksAdapterBase {
|
|
|
104
60
|
}
|
|
105
61
|
this.cliHelp.addEntry({
|
|
106
62
|
command,
|
|
107
|
-
aliases: options.aliases?.map(alias => alias.replace(/\\:/g, ':')),
|
|
63
|
+
aliases: options.aliases?.map(alias => alias.replace(/\\:/g, ':')),
|
|
108
64
|
args,
|
|
109
65
|
description: options.description,
|
|
110
66
|
examples: options.examples,
|
|
@@ -133,17 +89,6 @@ class WooksCli extends wooks.WooksAdapterBase {
|
|
|
133
89
|
}
|
|
134
90
|
}
|
|
135
91
|
}
|
|
136
|
-
/**
|
|
137
|
-
* ## run
|
|
138
|
-
* ### Start command processing
|
|
139
|
-
* Triggers command processing
|
|
140
|
-
*
|
|
141
|
-
* By default takes `process.argv.slice(2)` as a command
|
|
142
|
-
*
|
|
143
|
-
* It's possible to replace the command by passing an argument
|
|
144
|
-
*
|
|
145
|
-
* @param _argv optionally overwrite `process.argv.slice(2)` with your `argv` array
|
|
146
|
-
*/
|
|
147
92
|
async run(_argv, _opts) {
|
|
148
93
|
const argv = _argv || process.argv.slice(2);
|
|
149
94
|
const parsedFlags = minimist(argv, _opts);
|
|
@@ -155,7 +100,6 @@ class WooksCli extends wooks.WooksAdapterBase {
|
|
|
155
100
|
this.computeAliases();
|
|
156
101
|
const { handlers: foundHandlers, firstStatic } = this.wooks.lookup('CLI', path);
|
|
157
102
|
if (typeof firstStatic === 'string') {
|
|
158
|
-
// overwriting command with firstStatic to properly search for help
|
|
159
103
|
store('event').set('command', firstStatic.replace(/\//g, ' ').trim());
|
|
160
104
|
}
|
|
161
105
|
const handlers = foundHandlers ||
|
|
@@ -203,10 +147,6 @@ class WooksCli extends wooks.WooksAdapterBase {
|
|
|
203
147
|
process.exit(1);
|
|
204
148
|
}
|
|
205
149
|
}
|
|
206
|
-
/**
|
|
207
|
-
* Triggers `unknown command` processing and callbacks
|
|
208
|
-
* @param pathParams `string[]` containing command
|
|
209
|
-
*/
|
|
210
150
|
onUnknownCommand(pathParams) {
|
|
211
151
|
const raiseError = () => {
|
|
212
152
|
this.error('[0m' + 'Unknown command: ' + pathParams.join(' '));
|
|
@@ -228,29 +168,10 @@ class WooksCli extends wooks.WooksAdapterBase {
|
|
|
228
168
|
}
|
|
229
169
|
}
|
|
230
170
|
}
|
|
231
|
-
/**
|
|
232
|
-
* Factory for WooksCli App
|
|
233
|
-
* @param opts TWooksCliOptions
|
|
234
|
-
* @param wooks Wooks | WooksAdapterBase
|
|
235
|
-
* @returns WooksCli
|
|
236
|
-
*/
|
|
237
171
|
function createCliApp(opts, wooks) {
|
|
238
172
|
return new WooksCli(opts, wooks);
|
|
239
173
|
}
|
|
240
174
|
|
|
241
|
-
/**
|
|
242
|
-
* ## useCliHelp
|
|
243
|
-
* ### Composable
|
|
244
|
-
* ```js
|
|
245
|
-
* // example of printing cli instructions
|
|
246
|
-
* const { print } = useCliHelp()
|
|
247
|
-
* // print with colors
|
|
248
|
-
* print(true)
|
|
249
|
-
* // print with no colors
|
|
250
|
-
* // print(false)
|
|
251
|
-
* ```
|
|
252
|
-
* @returns
|
|
253
|
-
*/
|
|
254
175
|
function useCliHelp() {
|
|
255
176
|
const event = useCliContext().store('event');
|
|
256
177
|
const getCliHelp = () => event.get('cliHelp');
|
|
@@ -262,74 +183,14 @@ function useCliHelp() {
|
|
|
262
183
|
print: (withColors) => getCliHelp().print(event.get('command'), withColors),
|
|
263
184
|
};
|
|
264
185
|
}
|
|
265
|
-
/**
|
|
266
|
-
* ## useAutoHelp
|
|
267
|
-
* ### Composable
|
|
268
|
-
*
|
|
269
|
-
* Prints help if `--help` option provided.
|
|
270
|
-
*
|
|
271
|
-
* ```js
|
|
272
|
-
* // example of use: print help and exit
|
|
273
|
-
* app.cli('test', () => {
|
|
274
|
-
* useAutoHelp() && process.exit(0)
|
|
275
|
-
* return 'hit test command'
|
|
276
|
-
* })
|
|
277
|
-
*
|
|
278
|
-
* // add option -h to print help, no colors
|
|
279
|
-
* app.cli('test/nocolors', () => {
|
|
280
|
-
* useAutoHelp(['help', 'h'], false) && process.exit(0)
|
|
281
|
-
* return 'hit test nocolors command'
|
|
282
|
-
* })
|
|
283
|
-
* ```
|
|
284
|
-
* @param keys default `['help']` - list of options to trigger help render
|
|
285
|
-
* @param colors default `true`, prints with colors when true
|
|
286
|
-
* @returns true when --help was provided. Otherwise returns false
|
|
287
|
-
*/
|
|
288
186
|
function useAutoHelp(keys = ['help'], colors = true) {
|
|
289
187
|
for (const option of keys) {
|
|
290
188
|
if (useCliOption(option) === true) {
|
|
291
|
-
// try {
|
|
292
189
|
useCliHelp().print(colors);
|
|
293
190
|
return true;
|
|
294
|
-
// } catch (e) {
|
|
295
|
-
// throw new
|
|
296
|
-
// }
|
|
297
191
|
}
|
|
298
192
|
}
|
|
299
193
|
}
|
|
300
|
-
/**
|
|
301
|
-
* ##useCommandLookupHelp
|
|
302
|
-
* ### Composable
|
|
303
|
-
*
|
|
304
|
-
* Tries to find valid command based on provided command.
|
|
305
|
-
*
|
|
306
|
-
* If manages to find a valid command, throws an error
|
|
307
|
-
* suggesting a list of valid commands
|
|
308
|
-
*
|
|
309
|
-
* Best to use in `onUnknownCommand` callback:
|
|
310
|
-
*
|
|
311
|
-
* ```js
|
|
312
|
-
* const app = createCliApp({
|
|
313
|
-
* onUnknownCommand: (path, raiseError) => {
|
|
314
|
-
* // will throw an error suggesting a list
|
|
315
|
-
* // of valid commands if could find some
|
|
316
|
-
* useCommandLookupHelp()
|
|
317
|
-
* // fallback to a regular error handler
|
|
318
|
-
* raiseError()
|
|
319
|
-
* },
|
|
320
|
-
* })
|
|
321
|
-
* ```
|
|
322
|
-
*
|
|
323
|
-
* @param lookupDepth depth of search in backwards
|
|
324
|
-
* @example
|
|
325
|
-
*
|
|
326
|
-
* For provided command `run test:drive dir`
|
|
327
|
-
* - lookup1: `run test:drive dir` (deep = 0)
|
|
328
|
-
* - lookup2: `run test:drive` (deep = 1)
|
|
329
|
-
* - lookup3: `run test` (deep = 2)
|
|
330
|
-
* - lookup4: `run` (deep = 3)
|
|
331
|
-
* ...
|
|
332
|
-
*/
|
|
333
194
|
function useCommandLookupHelp(lookupDepth = 3) {
|
|
334
195
|
const parts = useCliContext()
|
|
335
196
|
.store('event')
|
|
@@ -374,11 +235,6 @@ function useCommandLookupHelp(lookupDepth = 3) {
|
|
|
374
235
|
}
|
|
375
236
|
}
|
|
376
237
|
|
|
377
|
-
/**
|
|
378
|
-
* Get CLI Options
|
|
379
|
-
*
|
|
380
|
-
* @returns an object with CLI options
|
|
381
|
-
*/
|
|
382
238
|
function useCliOptions() {
|
|
383
239
|
const { store } = useCliContext();
|
|
384
240
|
const flags = store('flags');
|
|
@@ -388,12 +244,6 @@ function useCliOptions() {
|
|
|
388
244
|
}
|
|
389
245
|
return flags.value;
|
|
390
246
|
}
|
|
391
|
-
/**
|
|
392
|
-
* Getter for Cli Option value
|
|
393
|
-
*
|
|
394
|
-
* @param name name of the option
|
|
395
|
-
* @returns value of a CLI option
|
|
396
|
-
*/
|
|
397
247
|
function useCliOption(name) {
|
|
398
248
|
try {
|
|
399
249
|
const options = useCliHelp().getEntry()?.options || [];
|
|
@@ -407,7 +257,6 @@ function useCliOption(name) {
|
|
|
407
257
|
}
|
|
408
258
|
}
|
|
409
259
|
catch (e) {
|
|
410
|
-
//
|
|
411
260
|
}
|
|
412
261
|
return useCliOptions()[name];
|
|
413
262
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,282 +1,266 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import { TCliHelpOptions } from '@prostojs/cli-help';
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
9
|
-
import {
|
|
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
|
-
|
|
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
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
*
|
|
268
|
-
* It's possible to replace the command by passing an argument
|
|
269
|
-
*
|
|
270
|
-
* @param _argv optionally overwrite `process.argv.slice(2)` with your `argv` array
|
|
271
|
-
*/
|
|
272
|
-
run(_argv?: string[], _opts?: minimist.Opts): Promise<void>;
|
|
273
|
-
protected onError(e: Error): void;
|
|
274
|
-
/**
|
|
275
|
-
* Triggers `unknown command` processing and callbacks
|
|
276
|
-
* @param pathParams `string[]` containing command
|
|
277
|
-
*/
|
|
278
|
-
onUnknownCommand(pathParams: string[]): void;
|
|
279
|
-
protected error(e: string | Error): void;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
export { }
|
|
1
|
+
import * as _wooksjs_event_core from '@wooksjs/event-core';
|
|
2
|
+
import { TEventOptions, TEmpty } from '@wooksjs/event-core';
|
|
3
|
+
import * as _prostojs_cli_help from '@prostojs/cli-help';
|
|
4
|
+
import { CliHelpRenderer, TCliHelpOptions, TCliEntry } from '@prostojs/cli-help';
|
|
5
|
+
import * as _prostojs_router from '@prostojs/router';
|
|
6
|
+
import { TProstoRouterPathHandle } from '@prostojs/router';
|
|
7
|
+
import { TWooksHandler, TWooksOptions, WooksAdapterBase, Wooks } from 'wooks';
|
|
8
|
+
import minimist from 'minimist';
|
|
9
|
+
import { TConsoleBase } from '@prostojs/logger';
|
|
10
|
+
|
|
11
|
+
interface TCliEventData {
|
|
12
|
+
argv: string[];
|
|
13
|
+
pathParams: string[];
|
|
14
|
+
command: string;
|
|
15
|
+
opts?: minimist.Opts;
|
|
16
|
+
type: 'CLI';
|
|
17
|
+
cliHelp: TCliHelpRenderer;
|
|
18
|
+
}
|
|
19
|
+
interface TCliContextStore {
|
|
20
|
+
flags?: {
|
|
21
|
+
[name: string]: boolean | string;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
type TCliHelpCustom = {
|
|
25
|
+
handler: TWooksHandler<any>;
|
|
26
|
+
/**
|
|
27
|
+
* ### Callback for registered path
|
|
28
|
+
*
|
|
29
|
+
* @param path registered path
|
|
30
|
+
* @param aliasType 0 - direct command, 1 - direct alias, 2 - computed alias
|
|
31
|
+
*/
|
|
32
|
+
cb?: <T>(path: string, aliasType: number, route?: TProstoRouterPathHandle<T>) => void;
|
|
33
|
+
};
|
|
34
|
+
type TCliHelpRenderer = CliHelpRenderer<TCliHelpCustom>;
|
|
35
|
+
|
|
36
|
+
declare function createCliContext(data: Omit<TCliEventData, 'type'>, options: TEventOptions): {
|
|
37
|
+
getCtx: () => TCliContextStore & _wooksjs_event_core.TGenericContextStore<TCliEventData>;
|
|
38
|
+
restoreCtx: () => _wooksjs_event_core.TGenericContextStore<TEmpty>;
|
|
39
|
+
clearCtx: () => null;
|
|
40
|
+
store: <K extends "flags" | keyof _wooksjs_event_core.TGenericContextStore<TCliEventData>>(key: K) => {
|
|
41
|
+
value: (TCliContextStore & _wooksjs_event_core.TGenericContextStore<TCliEventData>)[K];
|
|
42
|
+
hook: <K2 extends keyof Required<TCliContextStore & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K]>(key2: K2) => {
|
|
43
|
+
value: Required<TCliContextStore & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K][K2];
|
|
44
|
+
isDefined: boolean;
|
|
45
|
+
};
|
|
46
|
+
init: <K2_1 extends keyof Required<TCliContextStore & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K]>(key2: K2_1, getter: () => Required<Required<TCliContextStore & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K]>[K2_1]) => Required<Required<TCliContextStore & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K]>[K2_1];
|
|
47
|
+
set: <K2_2 extends keyof Required<TCliContextStore & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K]>(key2: K2_2, v: Required<(TCliContextStore & _wooksjs_event_core.TGenericContextStore<TCliEventData>)[K]>[K2_2]) => Required<(TCliContextStore & _wooksjs_event_core.TGenericContextStore<TCliEventData>)[K]>[K2_2];
|
|
48
|
+
get: <K2_3 extends keyof Required<TCliContextStore & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K]>(key2: K2_3) => Required<TCliContextStore & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K][K2_3];
|
|
49
|
+
has: <K2_4 extends keyof Required<TCliContextStore & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K]>(key2: K2_4) => boolean;
|
|
50
|
+
del: <K2_5 extends keyof Required<TCliContextStore & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K]>(key2: K2_5) => void;
|
|
51
|
+
entries: () => [string, unknown][];
|
|
52
|
+
clear: () => void;
|
|
53
|
+
};
|
|
54
|
+
getStore: <K_1 extends "flags" | keyof _wooksjs_event_core.TGenericContextStore<TCliEventData>>(key: K_1) => (TCliContextStore & _wooksjs_event_core.TGenericContextStore<TCliEventData>)[K_1];
|
|
55
|
+
setStore: <K_2 extends "flags" | keyof _wooksjs_event_core.TGenericContextStore<TCliEventData>>(key: K_2, v: (TCliContextStore & _wooksjs_event_core.TGenericContextStore<TCliEventData>)[K_2]) => void;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Wrapper on top of useEventContext that provides
|
|
59
|
+
* proper context types for CLI event
|
|
60
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
61
|
+
*/
|
|
62
|
+
declare function useCliContext<T extends TEmpty>(): {
|
|
63
|
+
getCtx: () => TCliContextStore & T & _wooksjs_event_core.TGenericContextStore<TCliEventData>;
|
|
64
|
+
restoreCtx: () => _wooksjs_event_core.TGenericContextStore<TEmpty>;
|
|
65
|
+
clearCtx: () => null;
|
|
66
|
+
store: <K extends "flags" | keyof _wooksjs_event_core.TGenericContextStore<TCliEventData> | keyof T>(key: K) => {
|
|
67
|
+
value: (TCliContextStore & T & _wooksjs_event_core.TGenericContextStore<TCliEventData>)[K];
|
|
68
|
+
hook: <K2 extends keyof Required<TCliContextStore & T & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K]>(key2: K2) => {
|
|
69
|
+
value: Required<TCliContextStore & T & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K][K2];
|
|
70
|
+
isDefined: boolean;
|
|
71
|
+
};
|
|
72
|
+
init: <K2_1 extends keyof Required<TCliContextStore & T & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K]>(key2: K2_1, getter: () => Required<Required<TCliContextStore & T & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K]>[K2_1]) => Required<Required<TCliContextStore & T & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K]>[K2_1];
|
|
73
|
+
set: <K2_2 extends keyof Required<TCliContextStore & T & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K]>(key2: K2_2, v: Required<(TCliContextStore & T & _wooksjs_event_core.TGenericContextStore<TCliEventData>)[K]>[K2_2]) => Required<(TCliContextStore & T & _wooksjs_event_core.TGenericContextStore<TCliEventData>)[K]>[K2_2];
|
|
74
|
+
get: <K2_3 extends keyof Required<TCliContextStore & T & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K]>(key2: K2_3) => Required<TCliContextStore & T & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K][K2_3];
|
|
75
|
+
has: <K2_4 extends keyof Required<TCliContextStore & T & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K]>(key2: K2_4) => boolean;
|
|
76
|
+
del: <K2_5 extends keyof Required<TCliContextStore & T & _wooksjs_event_core.TGenericContextStore<TCliEventData>>[K]>(key2: K2_5) => void;
|
|
77
|
+
entries: () => [string, unknown][];
|
|
78
|
+
clear: () => void;
|
|
79
|
+
};
|
|
80
|
+
getStore: <K_1 extends "flags" | keyof _wooksjs_event_core.TGenericContextStore<TCliEventData> | keyof T>(key: K_1) => (TCliContextStore & T & _wooksjs_event_core.TGenericContextStore<TCliEventData>)[K_1];
|
|
81
|
+
setStore: <K_2 extends "flags" | keyof _wooksjs_event_core.TGenericContextStore<TCliEventData> | keyof T>(key: K_2, v: (TCliContextStore & T & _wooksjs_event_core.TGenericContextStore<TCliEventData>)[K_2]) => void;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
declare const cliShortcuts: {
|
|
85
|
+
cli: string;
|
|
86
|
+
};
|
|
87
|
+
interface TWooksCliOptions {
|
|
88
|
+
onError?(e: Error): void;
|
|
89
|
+
onNotFound?: TWooksHandler<unknown>;
|
|
90
|
+
onUnknownCommand?: (params: string[], raiseError: () => void) => unknown;
|
|
91
|
+
logger?: TConsoleBase;
|
|
92
|
+
eventOptions?: TEventOptions;
|
|
93
|
+
cliHelp?: TCliHelpRenderer | TCliHelpOptions;
|
|
94
|
+
router?: TWooksOptions['router'];
|
|
95
|
+
}
|
|
96
|
+
interface TWooksCliEntry<T> extends Omit<TCliEntry<TWooksHandler<T>>, 'custom' | 'command'> {
|
|
97
|
+
onRegister?: TCliHelpCustom['cb'];
|
|
98
|
+
handler: TWooksHandler<T>;
|
|
99
|
+
}
|
|
100
|
+
declare class WooksCli extends WooksAdapterBase {
|
|
101
|
+
protected opts?: TWooksCliOptions | undefined;
|
|
102
|
+
protected logger: TConsoleBase;
|
|
103
|
+
protected cliHelp: TCliHelpRenderer;
|
|
104
|
+
constructor(opts?: TWooksCliOptions | undefined, wooks?: Wooks | WooksAdapterBase);
|
|
105
|
+
/**
|
|
106
|
+
* ### Register CLI Command
|
|
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
|
+
* ```js
|
|
116
|
+
* // example without options
|
|
117
|
+
* app.cli('command/:arg', () => 'arg = ' + useRouteParams().params.arg )
|
|
118
|
+
*
|
|
119
|
+
* // example with options
|
|
120
|
+
* app.cli('command/:arg', {
|
|
121
|
+
* description: 'Description of the command',
|
|
122
|
+
* options: [{ keys: ['project', 'p'], description: 'Description of the option', value: 'myProject' }],
|
|
123
|
+
* args: { arg: 'Description of the arg' },
|
|
124
|
+
* aliases: ['cmd'], // alias "cmd/:arg" will be registered
|
|
125
|
+
* examples: [{
|
|
126
|
+
* description: 'Example of usage with someProject',
|
|
127
|
+
* cmd: 'argValue -p=someProject',
|
|
128
|
+
* // will result in help display:
|
|
129
|
+
* // "# Example of usage with someProject\n" +
|
|
130
|
+
* // "$ myCli command argValue -p=someProject\n"
|
|
131
|
+
* }],
|
|
132
|
+
* handler: () => 'arg = ' + useRouteParams().params.arg
|
|
133
|
+
* })
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @param path command path
|
|
137
|
+
* @param _options handler or options
|
|
138
|
+
*
|
|
139
|
+
* @returns
|
|
140
|
+
*/
|
|
141
|
+
cli<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, _options: TWooksCliEntry<ResType> | TWooksHandler<ResType>): _prostojs_router.TProstoRouterPathHandle<ParamsType>;
|
|
142
|
+
protected alreadyComputedAliases: boolean;
|
|
143
|
+
protected computeAliases(): void;
|
|
144
|
+
/**
|
|
145
|
+
* ## run
|
|
146
|
+
* ### Start command processing
|
|
147
|
+
* Triggers command processing
|
|
148
|
+
*
|
|
149
|
+
* By default takes `process.argv.slice(2)` as a command
|
|
150
|
+
*
|
|
151
|
+
* It's possible to replace the command by passing an argument
|
|
152
|
+
*
|
|
153
|
+
* @param _argv optionally overwrite `process.argv.slice(2)` with your `argv` array
|
|
154
|
+
*/
|
|
155
|
+
run(_argv?: string[], _opts?: minimist.Opts): Promise<void>;
|
|
156
|
+
protected onError(e: Error): void;
|
|
157
|
+
/**
|
|
158
|
+
* Triggers `unknown command` processing and callbacks
|
|
159
|
+
* @param pathParams `string[]` containing command
|
|
160
|
+
*/
|
|
161
|
+
onUnknownCommand(pathParams: string[]): void;
|
|
162
|
+
protected error(e: string | Error): void;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Factory for WooksCli App
|
|
166
|
+
* @param opts TWooksCliOptions
|
|
167
|
+
* @param wooks Wooks | WooksAdapterBase
|
|
168
|
+
* @returns WooksCli
|
|
169
|
+
*/
|
|
170
|
+
declare function createCliApp(opts?: TWooksCliOptions, wooks?: Wooks | WooksAdapterBase): WooksCli;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Get CLI Options
|
|
174
|
+
*
|
|
175
|
+
* @returns an object with CLI options
|
|
176
|
+
*/
|
|
177
|
+
declare function useCliOptions(): {
|
|
178
|
+
[name: string]: string | boolean;
|
|
179
|
+
};
|
|
180
|
+
/**
|
|
181
|
+
* Getter for Cli Option value
|
|
182
|
+
*
|
|
183
|
+
* @param name name of the option
|
|
184
|
+
* @returns value of a CLI option
|
|
185
|
+
*/
|
|
186
|
+
declare function useCliOption(name: string): string | boolean;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* ## useCliHelp
|
|
190
|
+
* ### Composable
|
|
191
|
+
* ```js
|
|
192
|
+
* // example of printing cli instructions
|
|
193
|
+
* const { print } = useCliHelp()
|
|
194
|
+
* // print with colors
|
|
195
|
+
* print(true)
|
|
196
|
+
* // print with no colors
|
|
197
|
+
* // print(false)
|
|
198
|
+
* ```
|
|
199
|
+
* @returns
|
|
200
|
+
*/
|
|
201
|
+
declare function useCliHelp(): {
|
|
202
|
+
getCliHelp: () => TCliHelpRenderer;
|
|
203
|
+
getEntry: () => _prostojs_cli_help.TCliEntry<TCliHelpCustom>;
|
|
204
|
+
render: (width?: number, withColors?: boolean) => string[];
|
|
205
|
+
print: (withColors?: boolean) => void;
|
|
206
|
+
};
|
|
207
|
+
/**
|
|
208
|
+
* ## useAutoHelp
|
|
209
|
+
* ### Composable
|
|
210
|
+
*
|
|
211
|
+
* Prints help if `--help` option provided.
|
|
212
|
+
*
|
|
213
|
+
* ```js
|
|
214
|
+
* // example of use: print help and exit
|
|
215
|
+
* app.cli('test', () => {
|
|
216
|
+
* useAutoHelp() && process.exit(0)
|
|
217
|
+
* return 'hit test command'
|
|
218
|
+
* })
|
|
219
|
+
*
|
|
220
|
+
* // add option -h to print help, no colors
|
|
221
|
+
* app.cli('test/nocolors', () => {
|
|
222
|
+
* useAutoHelp(['help', 'h'], false) && process.exit(0)
|
|
223
|
+
* return 'hit test nocolors command'
|
|
224
|
+
* })
|
|
225
|
+
* ```
|
|
226
|
+
* @param keys default `['help']` - list of options to trigger help render
|
|
227
|
+
* @param colors default `true`, prints with colors when true
|
|
228
|
+
* @returns true when --help was provided. Otherwise returns false
|
|
229
|
+
*/
|
|
230
|
+
declare function useAutoHelp(keys?: string[], colors?: boolean): true | undefined;
|
|
231
|
+
/**
|
|
232
|
+
* ##useCommandLookupHelp
|
|
233
|
+
* ### Composable
|
|
234
|
+
*
|
|
235
|
+
* Tries to find valid command based on provided command.
|
|
236
|
+
*
|
|
237
|
+
* If manages to find a valid command, throws an error
|
|
238
|
+
* suggesting a list of valid commands
|
|
239
|
+
*
|
|
240
|
+
* Best to use in `onUnknownCommand` callback:
|
|
241
|
+
*
|
|
242
|
+
* ```js
|
|
243
|
+
* const app = createCliApp({
|
|
244
|
+
* onUnknownCommand: (path, raiseError) => {
|
|
245
|
+
* // will throw an error suggesting a list
|
|
246
|
+
* // of valid commands if could find some
|
|
247
|
+
* useCommandLookupHelp()
|
|
248
|
+
* // fallback to a regular error handler
|
|
249
|
+
* raiseError()
|
|
250
|
+
* },
|
|
251
|
+
* })
|
|
252
|
+
* ```
|
|
253
|
+
*
|
|
254
|
+
* @param lookupDepth depth of search in backwards
|
|
255
|
+
* @example
|
|
256
|
+
*
|
|
257
|
+
* For provided command `run test:drive dir`
|
|
258
|
+
* - lookup1: `run test:drive dir` (deep = 0)
|
|
259
|
+
* - lookup2: `run test:drive` (deep = 1)
|
|
260
|
+
* - lookup3: `run test` (deep = 2)
|
|
261
|
+
* - lookup4: `run` (deep = 3)
|
|
262
|
+
* ...
|
|
263
|
+
*/
|
|
264
|
+
declare function useCommandLookupHelp(lookupDepth?: number): void;
|
|
265
|
+
|
|
266
|
+
export { type TCliContextStore, type TCliEventData, type TCliHelpCustom, type TCliHelpRenderer, type TWooksCliEntry, type TWooksCliOptions, WooksCli, cliShortcuts, createCliApp, createCliContext, useAutoHelp, useCliContext, useCliHelp, useCliOption, useCliOptions, useCommandLookupHelp };
|
package/dist/index.mjs
CHANGED
|
@@ -12,11 +12,6 @@ function createCliContext(data, options) {
|
|
|
12
12
|
options,
|
|
13
13
|
});
|
|
14
14
|
}
|
|
15
|
-
/**
|
|
16
|
-
* Wrapper on top of useEventContext that provides
|
|
17
|
-
* proper context types for CLI event
|
|
18
|
-
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
19
|
-
*/
|
|
20
15
|
function useCliContext() {
|
|
21
16
|
return useEventContext('CLI');
|
|
22
17
|
}
|
|
@@ -35,53 +30,15 @@ class WooksCli extends WooksAdapterBase {
|
|
|
35
30
|
? opts.cliHelp
|
|
36
31
|
: new CliHelpRenderer(opts?.cliHelp);
|
|
37
32
|
}
|
|
38
|
-
/**
|
|
39
|
-
* ### Register CLI Command
|
|
40
|
-
* Command path segments may be separated by / or space.
|
|
41
|
-
*
|
|
42
|
-
* For example the folowing path are interpreted the same:
|
|
43
|
-
* - "command test use:dev :name"
|
|
44
|
-
* - "command/test/use:dev/:name"
|
|
45
|
-
*
|
|
46
|
-
* Where name will become an argument
|
|
47
|
-
*
|
|
48
|
-
* ```js
|
|
49
|
-
* // example without options
|
|
50
|
-
* app.cli('command/:arg', () => 'arg = ' + useRouteParams().params.arg )
|
|
51
|
-
*
|
|
52
|
-
* // example with options
|
|
53
|
-
* app.cli('command/:arg', {
|
|
54
|
-
* description: 'Description of the command',
|
|
55
|
-
* options: [{ keys: ['project', 'p'], description: 'Description of the option', value: 'myProject' }],
|
|
56
|
-
* args: { arg: 'Description of the arg' },
|
|
57
|
-
* aliases: ['cmd'], // alias "cmd/:arg" will be registered
|
|
58
|
-
* examples: [{
|
|
59
|
-
* description: 'Example of usage with someProject',
|
|
60
|
-
* cmd: 'argValue -p=someProject',
|
|
61
|
-
* // will result in help display:
|
|
62
|
-
* // "# Example of usage with someProject\n" +
|
|
63
|
-
* // "$ myCli command argValue -p=someProject\n"
|
|
64
|
-
* }],
|
|
65
|
-
* handler: () => 'arg = ' + useRouteParams().params.arg
|
|
66
|
-
* })
|
|
67
|
-
* ```
|
|
68
|
-
*
|
|
69
|
-
* @param path command path
|
|
70
|
-
* @param _options handler or options
|
|
71
|
-
*
|
|
72
|
-
* @returns
|
|
73
|
-
*/
|
|
74
33
|
cli(path, _options) {
|
|
75
34
|
const options = typeof _options === 'function' ? { handler: _options } : _options;
|
|
76
35
|
const handler = typeof _options === 'function' ? _options : _options.handler;
|
|
77
36
|
const makePath = (s) => '/' + s.replace(/\s+/g, '/');
|
|
78
|
-
// register handler
|
|
79
37
|
const targetPath = makePath(path);
|
|
80
38
|
const routed = this.on('CLI', targetPath, handler);
|
|
81
39
|
if (options.onRegister) {
|
|
82
40
|
options.onRegister(targetPath, 0, routed);
|
|
83
41
|
}
|
|
84
|
-
// register direct aliases
|
|
85
42
|
for (const alias of options.aliases || []) {
|
|
86
43
|
const vars = routed.getArgs().map((k) => ':' + k).join('/');
|
|
87
44
|
const targetPath = makePath(alias) + (vars ? '/' + vars : '');
|
|
@@ -90,7 +47,6 @@ class WooksCli extends WooksAdapterBase {
|
|
|
90
47
|
options.onRegister(targetPath, 1, routed);
|
|
91
48
|
}
|
|
92
49
|
}
|
|
93
|
-
// register helpCli entry
|
|
94
50
|
const command = routed.getStaticPart().replace(/\//g, ' ').trim();
|
|
95
51
|
const args = {
|
|
96
52
|
...(options.args || {}),
|
|
@@ -102,7 +58,7 @@ class WooksCli extends WooksAdapterBase {
|
|
|
102
58
|
}
|
|
103
59
|
this.cliHelp.addEntry({
|
|
104
60
|
command,
|
|
105
|
-
aliases: options.aliases?.map(alias => alias.replace(/\\:/g, ':')),
|
|
61
|
+
aliases: options.aliases?.map(alias => alias.replace(/\\:/g, ':')),
|
|
106
62
|
args,
|
|
107
63
|
description: options.description,
|
|
108
64
|
examples: options.examples,
|
|
@@ -131,17 +87,6 @@ class WooksCli extends WooksAdapterBase {
|
|
|
131
87
|
}
|
|
132
88
|
}
|
|
133
89
|
}
|
|
134
|
-
/**
|
|
135
|
-
* ## run
|
|
136
|
-
* ### Start command processing
|
|
137
|
-
* Triggers command processing
|
|
138
|
-
*
|
|
139
|
-
* By default takes `process.argv.slice(2)` as a command
|
|
140
|
-
*
|
|
141
|
-
* It's possible to replace the command by passing an argument
|
|
142
|
-
*
|
|
143
|
-
* @param _argv optionally overwrite `process.argv.slice(2)` with your `argv` array
|
|
144
|
-
*/
|
|
145
90
|
async run(_argv, _opts) {
|
|
146
91
|
const argv = _argv || process.argv.slice(2);
|
|
147
92
|
const parsedFlags = minimist(argv, _opts);
|
|
@@ -153,7 +98,6 @@ class WooksCli extends WooksAdapterBase {
|
|
|
153
98
|
this.computeAliases();
|
|
154
99
|
const { handlers: foundHandlers, firstStatic } = this.wooks.lookup('CLI', path);
|
|
155
100
|
if (typeof firstStatic === 'string') {
|
|
156
|
-
// overwriting command with firstStatic to properly search for help
|
|
157
101
|
store('event').set('command', firstStatic.replace(/\//g, ' ').trim());
|
|
158
102
|
}
|
|
159
103
|
const handlers = foundHandlers ||
|
|
@@ -201,10 +145,6 @@ class WooksCli extends WooksAdapterBase {
|
|
|
201
145
|
process.exit(1);
|
|
202
146
|
}
|
|
203
147
|
}
|
|
204
|
-
/**
|
|
205
|
-
* Triggers `unknown command` processing and callbacks
|
|
206
|
-
* @param pathParams `string[]` containing command
|
|
207
|
-
*/
|
|
208
148
|
onUnknownCommand(pathParams) {
|
|
209
149
|
const raiseError = () => {
|
|
210
150
|
this.error('[0m' + 'Unknown command: ' + pathParams.join(' '));
|
|
@@ -226,29 +166,10 @@ class WooksCli extends WooksAdapterBase {
|
|
|
226
166
|
}
|
|
227
167
|
}
|
|
228
168
|
}
|
|
229
|
-
/**
|
|
230
|
-
* Factory for WooksCli App
|
|
231
|
-
* @param opts TWooksCliOptions
|
|
232
|
-
* @param wooks Wooks | WooksAdapterBase
|
|
233
|
-
* @returns WooksCli
|
|
234
|
-
*/
|
|
235
169
|
function createCliApp(opts, wooks) {
|
|
236
170
|
return new WooksCli(opts, wooks);
|
|
237
171
|
}
|
|
238
172
|
|
|
239
|
-
/**
|
|
240
|
-
* ## useCliHelp
|
|
241
|
-
* ### Composable
|
|
242
|
-
* ```js
|
|
243
|
-
* // example of printing cli instructions
|
|
244
|
-
* const { print } = useCliHelp()
|
|
245
|
-
* // print with colors
|
|
246
|
-
* print(true)
|
|
247
|
-
* // print with no colors
|
|
248
|
-
* // print(false)
|
|
249
|
-
* ```
|
|
250
|
-
* @returns
|
|
251
|
-
*/
|
|
252
173
|
function useCliHelp() {
|
|
253
174
|
const event = useCliContext().store('event');
|
|
254
175
|
const getCliHelp = () => event.get('cliHelp');
|
|
@@ -260,74 +181,14 @@ function useCliHelp() {
|
|
|
260
181
|
print: (withColors) => getCliHelp().print(event.get('command'), withColors),
|
|
261
182
|
};
|
|
262
183
|
}
|
|
263
|
-
/**
|
|
264
|
-
* ## useAutoHelp
|
|
265
|
-
* ### Composable
|
|
266
|
-
*
|
|
267
|
-
* Prints help if `--help` option provided.
|
|
268
|
-
*
|
|
269
|
-
* ```js
|
|
270
|
-
* // example of use: print help and exit
|
|
271
|
-
* app.cli('test', () => {
|
|
272
|
-
* useAutoHelp() && process.exit(0)
|
|
273
|
-
* return 'hit test command'
|
|
274
|
-
* })
|
|
275
|
-
*
|
|
276
|
-
* // add option -h to print help, no colors
|
|
277
|
-
* app.cli('test/nocolors', () => {
|
|
278
|
-
* useAutoHelp(['help', 'h'], false) && process.exit(0)
|
|
279
|
-
* return 'hit test nocolors command'
|
|
280
|
-
* })
|
|
281
|
-
* ```
|
|
282
|
-
* @param keys default `['help']` - list of options to trigger help render
|
|
283
|
-
* @param colors default `true`, prints with colors when true
|
|
284
|
-
* @returns true when --help was provided. Otherwise returns false
|
|
285
|
-
*/
|
|
286
184
|
function useAutoHelp(keys = ['help'], colors = true) {
|
|
287
185
|
for (const option of keys) {
|
|
288
186
|
if (useCliOption(option) === true) {
|
|
289
|
-
// try {
|
|
290
187
|
useCliHelp().print(colors);
|
|
291
188
|
return true;
|
|
292
|
-
// } catch (e) {
|
|
293
|
-
// throw new
|
|
294
|
-
// }
|
|
295
189
|
}
|
|
296
190
|
}
|
|
297
191
|
}
|
|
298
|
-
/**
|
|
299
|
-
* ##useCommandLookupHelp
|
|
300
|
-
* ### Composable
|
|
301
|
-
*
|
|
302
|
-
* Tries to find valid command based on provided command.
|
|
303
|
-
*
|
|
304
|
-
* If manages to find a valid command, throws an error
|
|
305
|
-
* suggesting a list of valid commands
|
|
306
|
-
*
|
|
307
|
-
* Best to use in `onUnknownCommand` callback:
|
|
308
|
-
*
|
|
309
|
-
* ```js
|
|
310
|
-
* const app = createCliApp({
|
|
311
|
-
* onUnknownCommand: (path, raiseError) => {
|
|
312
|
-
* // will throw an error suggesting a list
|
|
313
|
-
* // of valid commands if could find some
|
|
314
|
-
* useCommandLookupHelp()
|
|
315
|
-
* // fallback to a regular error handler
|
|
316
|
-
* raiseError()
|
|
317
|
-
* },
|
|
318
|
-
* })
|
|
319
|
-
* ```
|
|
320
|
-
*
|
|
321
|
-
* @param lookupDepth depth of search in backwards
|
|
322
|
-
* @example
|
|
323
|
-
*
|
|
324
|
-
* For provided command `run test:drive dir`
|
|
325
|
-
* - lookup1: `run test:drive dir` (deep = 0)
|
|
326
|
-
* - lookup2: `run test:drive` (deep = 1)
|
|
327
|
-
* - lookup3: `run test` (deep = 2)
|
|
328
|
-
* - lookup4: `run` (deep = 3)
|
|
329
|
-
* ...
|
|
330
|
-
*/
|
|
331
192
|
function useCommandLookupHelp(lookupDepth = 3) {
|
|
332
193
|
const parts = useCliContext()
|
|
333
194
|
.store('event')
|
|
@@ -372,11 +233,6 @@ function useCommandLookupHelp(lookupDepth = 3) {
|
|
|
372
233
|
}
|
|
373
234
|
}
|
|
374
235
|
|
|
375
|
-
/**
|
|
376
|
-
* Get CLI Options
|
|
377
|
-
*
|
|
378
|
-
* @returns an object with CLI options
|
|
379
|
-
*/
|
|
380
236
|
function useCliOptions() {
|
|
381
237
|
const { store } = useCliContext();
|
|
382
238
|
const flags = store('flags');
|
|
@@ -386,12 +242,6 @@ function useCliOptions() {
|
|
|
386
242
|
}
|
|
387
243
|
return flags.value;
|
|
388
244
|
}
|
|
389
|
-
/**
|
|
390
|
-
* Getter for Cli Option value
|
|
391
|
-
*
|
|
392
|
-
* @param name name of the option
|
|
393
|
-
* @returns value of a CLI option
|
|
394
|
-
*/
|
|
395
245
|
function useCliOption(name) {
|
|
396
246
|
try {
|
|
397
247
|
const options = useCliHelp().getEntry()?.options || [];
|
|
@@ -405,7 +255,6 @@ function useCliOption(name) {
|
|
|
405
255
|
}
|
|
406
256
|
}
|
|
407
257
|
catch (e) {
|
|
408
|
-
//
|
|
409
258
|
}
|
|
410
259
|
return useCliOptions()[name];
|
|
411
260
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/event-cli",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.12",
|
|
4
4
|
"description": "@wooksjs/event-cli",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -8,6 +8,14 @@
|
|
|
8
8
|
"files": [
|
|
9
9
|
"dist"
|
|
10
10
|
],
|
|
11
|
+
"exports": {
|
|
12
|
+
"./package.json": "./package.json",
|
|
13
|
+
".": {
|
|
14
|
+
"require": "./dist/index.cjs",
|
|
15
|
+
"import": "./dist/index.mjs",
|
|
16
|
+
"types": "./dist/index.d.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
11
19
|
"repository": {
|
|
12
20
|
"type": "git",
|
|
13
21
|
"url": "git+https://github.com/wooksjs/wooksjs.git",
|
|
@@ -31,14 +39,14 @@
|
|
|
31
39
|
"url": "https://github.com/wooksjs/wooksjs/issues"
|
|
32
40
|
},
|
|
33
41
|
"peerDependencies": {
|
|
34
|
-
"wooks": "0.4.
|
|
35
|
-
"@wooksjs/event-core": "0.4.
|
|
42
|
+
"wooks": "0.4.12",
|
|
43
|
+
"@wooksjs/event-core": "0.4.12"
|
|
36
44
|
},
|
|
37
45
|
"dependencies": {
|
|
38
46
|
"minimist": "^1.2.6",
|
|
39
47
|
"@prostojs/cli-help": "^0.0.10",
|
|
40
48
|
"@prostojs/router": "^0.2.1",
|
|
41
|
-
"@prostojs/logger": "^0.
|
|
49
|
+
"@prostojs/logger": "^0.4.0"
|
|
42
50
|
},
|
|
43
51
|
"devDependencies": {
|
|
44
52
|
"@types/minimist": "^1.2.5"
|