@wooksjs/event-cli 0.6.0 → 0.6.1
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 +112 -22
- package/dist/index.d.ts +148 -0
- package/dist/index.mjs +102 -11
- package/package.json +8 -8
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;
|
|
@@ -37,6 +36,11 @@ function createCliContext(data, options) {
|
|
|
37
36
|
options
|
|
38
37
|
});
|
|
39
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Wrapper on top of useEventContext that provides
|
|
41
|
+
* proper context types for CLI event
|
|
42
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
43
|
+
*/
|
|
40
44
|
function useCliContext() {
|
|
41
45
|
return (0, __wooksjs_event_core.useAsyncEventContext)("CLI");
|
|
42
46
|
}
|
|
@@ -50,7 +54,7 @@ var WooksCli = class extends wooks.WooksAdapterBase {
|
|
|
50
54
|
constructor(opts, wooks$1) {
|
|
51
55
|
super(wooks$1, opts?.logger, opts?.router);
|
|
52
56
|
this.opts = opts;
|
|
53
|
-
this.logger = opts?.logger || this.getLogger(
|
|
57
|
+
this.logger = opts?.logger || this.getLogger(`[96m[wooks-cli]`);
|
|
54
58
|
this.cliHelp = opts?.cliHelp instanceof __prostojs_cli_help.CliHelpRenderer ? opts.cliHelp : new __prostojs_cli_help.CliHelpRenderer(opts?.cliHelp);
|
|
55
59
|
}
|
|
56
60
|
/**
|
|
@@ -166,10 +170,10 @@ var WooksCli = class extends wooks.WooksAdapterBase {
|
|
|
166
170
|
for (const handler of handlers) {
|
|
167
171
|
const response = await handler();
|
|
168
172
|
if (typeof response === "string") console.log(response);
|
|
169
|
-
else if (Array.isArray(response)) response.forEach((r) => {
|
|
173
|
+
else if (Array.isArray(response)) response.forEach((r) => {
|
|
170
174
|
console.log(typeof r === "string" ? r : JSON.stringify(r, null, " "));
|
|
171
175
|
});
|
|
172
|
-
else if (response instanceof Error) {
|
|
176
|
+
else if (response instanceof Error) {
|
|
173
177
|
this.onError(response);
|
|
174
178
|
return response;
|
|
175
179
|
} else if (response) console.log(JSON.stringify(response, null, " "));
|
|
@@ -178,15 +182,15 @@ else if (response instanceof Error) {
|
|
|
178
182
|
this.onError(error);
|
|
179
183
|
return error;
|
|
180
184
|
}
|
|
181
|
-
else {
|
|
185
|
+
else {
|
|
182
186
|
this.onUnknownCommand(pathParams);
|
|
183
|
-
return new Error("Unknown command");
|
|
187
|
+
return /* @__PURE__ */ new Error("Unknown command");
|
|
184
188
|
}
|
|
185
189
|
});
|
|
186
190
|
}
|
|
187
191
|
onError(e) {
|
|
188
192
|
if (this.opts?.onError) this.opts.onError(e);
|
|
189
|
-
else {
|
|
193
|
+
else {
|
|
190
194
|
this.error(e.message);
|
|
191
195
|
process.exit(1);
|
|
192
196
|
}
|
|
@@ -197,23 +201,34 @@ else {
|
|
|
197
201
|
*/
|
|
198
202
|
onUnknownCommand(pathParams) {
|
|
199
203
|
const raiseError = () => {
|
|
200
|
-
this.error(
|
|
204
|
+
this.error(`[0mUnknown command: ${pathParams.join(" ")}`);
|
|
201
205
|
process.exit(1);
|
|
202
206
|
};
|
|
203
207
|
if (this.opts?.onUnknownCommand) this.opts.onUnknownCommand(pathParams, raiseError);
|
|
204
|
-
else raiseError();
|
|
208
|
+
else raiseError();
|
|
205
209
|
}
|
|
206
210
|
error(e) {
|
|
207
|
-
if (typeof e === "string") console.error(
|
|
208
|
-
else console.error(
|
|
211
|
+
if (typeof e === "string") console.error(`[31mERROR: [0m${e}`);
|
|
212
|
+
else console.error(`[31mERROR: [0m${e.message}`);
|
|
209
213
|
}
|
|
210
214
|
};
|
|
215
|
+
/**
|
|
216
|
+
* Factory for WooksCli App
|
|
217
|
+
* @param opts TWooksCliOptions
|
|
218
|
+
* @param wooks Wooks | WooksAdapterBase
|
|
219
|
+
* @returns WooksCli
|
|
220
|
+
*/
|
|
211
221
|
function createCliApp(opts, wooks$1) {
|
|
212
222
|
return new WooksCli(opts, wooks$1);
|
|
213
223
|
}
|
|
214
224
|
|
|
215
225
|
//#endregion
|
|
216
226
|
//#region packages/event-cli/src/composables/options.ts
|
|
227
|
+
/**
|
|
228
|
+
* Get CLI Options
|
|
229
|
+
*
|
|
230
|
+
* @returns an object with CLI options
|
|
231
|
+
*/
|
|
217
232
|
function useCliOptions() {
|
|
218
233
|
const { store } = useCliContext();
|
|
219
234
|
const flags = store("flags");
|
|
@@ -223,6 +238,12 @@ function useCliOptions() {
|
|
|
223
238
|
}
|
|
224
239
|
return flags.value;
|
|
225
240
|
}
|
|
241
|
+
/**
|
|
242
|
+
* Getter for Cli Option value
|
|
243
|
+
*
|
|
244
|
+
* @param name name of the option
|
|
245
|
+
* @returns value of a CLI option
|
|
246
|
+
*/
|
|
226
247
|
function useCliOption(name) {
|
|
227
248
|
try {
|
|
228
249
|
const options = useCliHelp().getEntry().options || [];
|
|
@@ -236,6 +257,19 @@ function useCliOption(name) {
|
|
|
236
257
|
|
|
237
258
|
//#endregion
|
|
238
259
|
//#region packages/event-cli/src/composables/cli-help.ts
|
|
260
|
+
/**
|
|
261
|
+
* ## useCliHelp
|
|
262
|
+
* ### Composable
|
|
263
|
+
* ```js
|
|
264
|
+
* // example of printing cli instructions
|
|
265
|
+
* const { print } = useCliHelp()
|
|
266
|
+
* // print with colors
|
|
267
|
+
* print(true)
|
|
268
|
+
* // print with no colors
|
|
269
|
+
* // print(false)
|
|
270
|
+
* ```
|
|
271
|
+
* @returns
|
|
272
|
+
*/
|
|
239
273
|
function useCliHelp() {
|
|
240
274
|
const event = useCliContext().store("event");
|
|
241
275
|
const getCliHelp = () => event.get("cliHelp");
|
|
@@ -249,12 +283,68 @@ function useCliHelp() {
|
|
|
249
283
|
}
|
|
250
284
|
};
|
|
251
285
|
}
|
|
286
|
+
/**
|
|
287
|
+
* ## useAutoHelp
|
|
288
|
+
* ### Composable
|
|
289
|
+
*
|
|
290
|
+
* Prints help if `--help` option provided.
|
|
291
|
+
*
|
|
292
|
+
* ```js
|
|
293
|
+
* // example of use: print help and exit
|
|
294
|
+
* app.cli('test', () => {
|
|
295
|
+
* useAutoHelp() && process.exit(0)
|
|
296
|
+
* return 'hit test command'
|
|
297
|
+
* })
|
|
298
|
+
*
|
|
299
|
+
* // add option -h to print help, no colors
|
|
300
|
+
* app.cli('test/nocolors', () => {
|
|
301
|
+
* useAutoHelp(['help', 'h'], false) && process.exit(0)
|
|
302
|
+
* return 'hit test nocolors command'
|
|
303
|
+
* })
|
|
304
|
+
* ```
|
|
305
|
+
* @param keys default `['help']` - list of options to trigger help render
|
|
306
|
+
* @param colors default `true`, prints with colors when true
|
|
307
|
+
* @returns true when --help was provided. Otherwise returns false
|
|
308
|
+
*/
|
|
252
309
|
function useAutoHelp(keys = ["help"], colors = true) {
|
|
253
310
|
for (const option of keys) if (useCliOption(option) === true) {
|
|
254
311
|
useCliHelp().print(colors);
|
|
255
312
|
return true;
|
|
256
313
|
}
|
|
257
314
|
}
|
|
315
|
+
/**
|
|
316
|
+
* ##useCommandLookupHelp
|
|
317
|
+
* ### Composable
|
|
318
|
+
*
|
|
319
|
+
* Tries to find valid command based on provided command.
|
|
320
|
+
*
|
|
321
|
+
* If manages to find a valid command, throws an error
|
|
322
|
+
* suggesting a list of valid commands
|
|
323
|
+
*
|
|
324
|
+
* Best to use in `onUnknownCommand` callback:
|
|
325
|
+
*
|
|
326
|
+
* ```js
|
|
327
|
+
* const app = createCliApp({
|
|
328
|
+
* onUnknownCommand: (path, raiseError) => {
|
|
329
|
+
* // will throw an error suggesting a list
|
|
330
|
+
* // of valid commands if could find some
|
|
331
|
+
* useCommandLookupHelp()
|
|
332
|
+
* // fallback to a regular error handler
|
|
333
|
+
* raiseError()
|
|
334
|
+
* },
|
|
335
|
+
* })
|
|
336
|
+
* ```
|
|
337
|
+
*
|
|
338
|
+
* @param lookupDepth depth of search in backwards
|
|
339
|
+
* @example
|
|
340
|
+
*
|
|
341
|
+
* For provided command `run test:drive dir`
|
|
342
|
+
* - lookup1: `run test:drive dir` (deep = 0)
|
|
343
|
+
* - lookup2: `run test:drive` (deep = 1)
|
|
344
|
+
* - lookup3: `run test` (deep = 2)
|
|
345
|
+
* - lookup4: `run` (deep = 3)
|
|
346
|
+
* ...
|
|
347
|
+
*/
|
|
258
348
|
function useCommandLookupHelp(lookupDepth = 3) {
|
|
259
349
|
const parts = useCliContext().store("event").get("pathParams")?.flatMap((p) => `${p} `.split(":").map((s, i) => i ? `:${s}` : s)) || [];
|
|
260
350
|
const cliHelp = useCliHelp().getCliHelp();
|
|
@@ -273,18 +363,18 @@ function useCommandLookupHelp(lookupDepth = 3) {
|
|
|
273
363
|
if (data) {
|
|
274
364
|
const { main, children } = data;
|
|
275
365
|
if (main.args && Object.keys(main.args).length > 0) throw new Error(`Arguments expected: ${Object.keys(main.args).map((l) => `<${l}>`).join(", ")}`);
|
|
276
|
-
else if (children?.length > 0) throw new Error(`Wrong command, did you mean:\n${children.slice(0, 7).map((c) => ` $ ${cmd} ${c.command}`).join("\n")}`);
|
|
366
|
+
else if (children?.length > 0) throw new Error(`Wrong command, did you mean:\n${children.slice(0, 7).map((c) => ` $ ${cmd} ${c.command}`).join("\n")}`);
|
|
277
367
|
}
|
|
278
368
|
}
|
|
279
369
|
|
|
280
370
|
//#endregion
|
|
281
|
-
exports.WooksCli = WooksCli
|
|
282
|
-
exports.cliShortcuts = cliShortcuts
|
|
283
|
-
exports.createCliApp = createCliApp
|
|
284
|
-
exports.createCliContext = createCliContext
|
|
285
|
-
exports.useAutoHelp = useAutoHelp
|
|
286
|
-
exports.useCliContext = useCliContext
|
|
287
|
-
exports.useCliHelp = useCliHelp
|
|
288
|
-
exports.useCliOption = useCliOption
|
|
289
|
-
exports.useCliOptions = useCliOptions
|
|
290
|
-
exports.useCommandLookupHelp = useCommandLookupHelp
|
|
371
|
+
exports.WooksCli = WooksCli;
|
|
372
|
+
exports.cliShortcuts = cliShortcuts;
|
|
373
|
+
exports.createCliApp = createCliApp;
|
|
374
|
+
exports.createCliContext = createCliContext;
|
|
375
|
+
exports.useAutoHelp = useAutoHelp;
|
|
376
|
+
exports.useCliContext = useCliContext;
|
|
377
|
+
exports.useCliHelp = useCliHelp;
|
|
378
|
+
exports.useCliOption = useCliOption;
|
|
379
|
+
exports.useCliOptions = useCliOptions;
|
|
380
|
+
exports.useCommandLookupHelp = useCommandLookupHelp;
|
package/dist/index.d.ts
CHANGED
|
@@ -21,6 +21,12 @@ interface TCliContextStore {
|
|
|
21
21
|
}
|
|
22
22
|
interface TCliHelpCustom {
|
|
23
23
|
handler: TWooksHandler<any>;
|
|
24
|
+
/**
|
|
25
|
+
* ### Callback for registered path
|
|
26
|
+
*
|
|
27
|
+
* @param path registered path
|
|
28
|
+
* @param aliasType 0 - direct command, 1 - direct alias, 2 - computed alias
|
|
29
|
+
*/
|
|
24
30
|
cb?: <T>(path: string, aliasType: number, route?: TProstoRouterPathHandle<T>) => void;
|
|
25
31
|
}
|
|
26
32
|
type TCliHelpRenderer = CliHelpRenderer<TCliHelpCustom>;
|
|
@@ -46,29 +52,171 @@ declare class WooksCli extends WooksAdapterBase {
|
|
|
46
52
|
protected logger: TConsoleBase;
|
|
47
53
|
protected cliHelp: TCliHelpRenderer;
|
|
48
54
|
constructor(opts?: TWooksCliOptions | undefined, wooks?: Wooks | WooksAdapterBase);
|
|
55
|
+
/**
|
|
56
|
+
* ### Register CLI Command
|
|
57
|
+
* Command path segments may be separated by / or space.
|
|
58
|
+
*
|
|
59
|
+
* For example the folowing path are interpreted the same:
|
|
60
|
+
* - "command test use:dev :name"
|
|
61
|
+
* - "command/test/use:dev/:name"
|
|
62
|
+
*
|
|
63
|
+
* Where name will become an argument
|
|
64
|
+
*
|
|
65
|
+
* ```js
|
|
66
|
+
* // example without options
|
|
67
|
+
* app.cli('command/:arg', () => 'arg = ' + useRouteParams().params.arg )
|
|
68
|
+
*
|
|
69
|
+
* // example with options
|
|
70
|
+
* app.cli('command/:arg', {
|
|
71
|
+
* description: 'Description of the command',
|
|
72
|
+
* options: [{ keys: ['project', 'p'], description: 'Description of the option', value: 'myProject' }],
|
|
73
|
+
* args: { arg: 'Description of the arg' },
|
|
74
|
+
* aliases: ['cmd'], // alias "cmd/:arg" will be registered
|
|
75
|
+
* examples: [{
|
|
76
|
+
* description: 'Example of usage with someProject',
|
|
77
|
+
* cmd: 'argValue -p=someProject',
|
|
78
|
+
* // will result in help display:
|
|
79
|
+
* // "# Example of usage with someProject\n" +
|
|
80
|
+
* // "$ myCli command argValue -p=someProject\n"
|
|
81
|
+
* }],
|
|
82
|
+
* handler: () => 'arg = ' + useRouteParams().params.arg
|
|
83
|
+
* })
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* @param path command path
|
|
87
|
+
* @param _options handler or options
|
|
88
|
+
*
|
|
89
|
+
* @returns
|
|
90
|
+
*/
|
|
49
91
|
cli<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, _options: TWooksCliEntry<ResType> | TWooksHandler<ResType>): wooks.TProstoRouterPathHandle<ParamsType>;
|
|
50
92
|
protected alreadyComputedAliases: boolean;
|
|
51
93
|
protected computeAliases(): void;
|
|
94
|
+
/**
|
|
95
|
+
* ## run
|
|
96
|
+
* ### Start command processing
|
|
97
|
+
* Triggers command processing
|
|
98
|
+
*
|
|
99
|
+
* By default takes `process.argv.slice(2)` as a command
|
|
100
|
+
*
|
|
101
|
+
* It's possible to replace the command by passing an argument
|
|
102
|
+
*
|
|
103
|
+
* @param _argv optionally overwrite `process.argv.slice(2)` with your `argv` array
|
|
104
|
+
*/
|
|
52
105
|
run(_argv?: string[], _opts?: minimist.Opts): Promise<unknown>;
|
|
53
106
|
protected onError(e: Error): void;
|
|
107
|
+
/**
|
|
108
|
+
* Triggers `unknown command` processing and callbacks
|
|
109
|
+
* @param pathParams `string[]` containing command
|
|
110
|
+
*/
|
|
54
111
|
onUnknownCommand(pathParams: string[]): void;
|
|
55
112
|
protected error(e: string | Error): void;
|
|
56
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Factory for WooksCli App
|
|
116
|
+
* @param opts TWooksCliOptions
|
|
117
|
+
* @param wooks Wooks | WooksAdapterBase
|
|
118
|
+
* @returns WooksCli
|
|
119
|
+
*/
|
|
57
120
|
declare function createCliApp(opts?: TWooksCliOptions, wooks?: Wooks | WooksAdapterBase): WooksCli;
|
|
58
121
|
|
|
122
|
+
/**
|
|
123
|
+
* ## useCliHelp
|
|
124
|
+
* ### Composable
|
|
125
|
+
* ```js
|
|
126
|
+
* // example of printing cli instructions
|
|
127
|
+
* const { print } = useCliHelp()
|
|
128
|
+
* // print with colors
|
|
129
|
+
* print(true)
|
|
130
|
+
* // print with no colors
|
|
131
|
+
* // print(false)
|
|
132
|
+
* ```
|
|
133
|
+
* @returns
|
|
134
|
+
*/
|
|
59
135
|
declare function useCliHelp(): {
|
|
60
136
|
getCliHelp: () => TCliHelpRenderer;
|
|
61
137
|
getEntry: () => _prostojs_cli_help.TCliEntry<TCliHelpCustom>;
|
|
62
138
|
render: (width?: number, withColors?: boolean) => string[];
|
|
63
139
|
print: (withColors?: boolean) => void;
|
|
64
140
|
};
|
|
141
|
+
/**
|
|
142
|
+
* ## useAutoHelp
|
|
143
|
+
* ### Composable
|
|
144
|
+
*
|
|
145
|
+
* Prints help if `--help` option provided.
|
|
146
|
+
*
|
|
147
|
+
* ```js
|
|
148
|
+
* // example of use: print help and exit
|
|
149
|
+
* app.cli('test', () => {
|
|
150
|
+
* useAutoHelp() && process.exit(0)
|
|
151
|
+
* return 'hit test command'
|
|
152
|
+
* })
|
|
153
|
+
*
|
|
154
|
+
* // add option -h to print help, no colors
|
|
155
|
+
* app.cli('test/nocolors', () => {
|
|
156
|
+
* useAutoHelp(['help', 'h'], false) && process.exit(0)
|
|
157
|
+
* return 'hit test nocolors command'
|
|
158
|
+
* })
|
|
159
|
+
* ```
|
|
160
|
+
* @param keys default `['help']` - list of options to trigger help render
|
|
161
|
+
* @param colors default `true`, prints with colors when true
|
|
162
|
+
* @returns true when --help was provided. Otherwise returns false
|
|
163
|
+
*/
|
|
65
164
|
declare function useAutoHelp(keys?: string[], colors?: boolean): true | undefined;
|
|
165
|
+
/**
|
|
166
|
+
* ##useCommandLookupHelp
|
|
167
|
+
* ### Composable
|
|
168
|
+
*
|
|
169
|
+
* Tries to find valid command based on provided command.
|
|
170
|
+
*
|
|
171
|
+
* If manages to find a valid command, throws an error
|
|
172
|
+
* suggesting a list of valid commands
|
|
173
|
+
*
|
|
174
|
+
* Best to use in `onUnknownCommand` callback:
|
|
175
|
+
*
|
|
176
|
+
* ```js
|
|
177
|
+
* const app = createCliApp({
|
|
178
|
+
* onUnknownCommand: (path, raiseError) => {
|
|
179
|
+
* // will throw an error suggesting a list
|
|
180
|
+
* // of valid commands if could find some
|
|
181
|
+
* useCommandLookupHelp()
|
|
182
|
+
* // fallback to a regular error handler
|
|
183
|
+
* raiseError()
|
|
184
|
+
* },
|
|
185
|
+
* })
|
|
186
|
+
* ```
|
|
187
|
+
*
|
|
188
|
+
* @param lookupDepth depth of search in backwards
|
|
189
|
+
* @example
|
|
190
|
+
*
|
|
191
|
+
* For provided command `run test:drive dir`
|
|
192
|
+
* - lookup1: `run test:drive dir` (deep = 0)
|
|
193
|
+
* - lookup2: `run test:drive` (deep = 1)
|
|
194
|
+
* - lookup3: `run test` (deep = 2)
|
|
195
|
+
* - lookup4: `run` (deep = 3)
|
|
196
|
+
* ...
|
|
197
|
+
*/
|
|
66
198
|
declare function useCommandLookupHelp(lookupDepth?: number): void;
|
|
67
199
|
|
|
200
|
+
/**
|
|
201
|
+
* Get CLI Options
|
|
202
|
+
*
|
|
203
|
+
* @returns an object with CLI options
|
|
204
|
+
*/
|
|
68
205
|
declare function useCliOptions(): Record<string, string | boolean>;
|
|
206
|
+
/**
|
|
207
|
+
* Getter for Cli Option value
|
|
208
|
+
*
|
|
209
|
+
* @param name name of the option
|
|
210
|
+
* @returns value of a CLI option
|
|
211
|
+
*/
|
|
69
212
|
declare function useCliOption(name: string): string | boolean;
|
|
70
213
|
|
|
71
214
|
declare function createCliContext(data: Omit<TCliEventData, 'type'>, options: TEventOptions): <T>(cb: (...a: any[]) => T) => T;
|
|
215
|
+
/**
|
|
216
|
+
* Wrapper on top of useEventContext that provides
|
|
217
|
+
* proper context types for CLI event
|
|
218
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
219
|
+
*/
|
|
72
220
|
declare function useCliContext<T extends TEmpty>(): _wooksjs_event_core.TCtxHelpers<TCliContextStore & T & _wooksjs_event_core.TGenericContextStore<TCliEventData>>;
|
|
73
221
|
|
|
74
222
|
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
|
@@ -13,6 +13,11 @@ function createCliContext(data, options) {
|
|
|
13
13
|
options
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Wrapper on top of useEventContext that provides
|
|
18
|
+
* proper context types for CLI event
|
|
19
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
20
|
+
*/
|
|
16
21
|
function useCliContext() {
|
|
17
22
|
return useAsyncEventContext("CLI");
|
|
18
23
|
}
|
|
@@ -26,7 +31,7 @@ var WooksCli = class extends WooksAdapterBase {
|
|
|
26
31
|
constructor(opts, wooks) {
|
|
27
32
|
super(wooks, opts?.logger, opts?.router);
|
|
28
33
|
this.opts = opts;
|
|
29
|
-
this.logger = opts?.logger || this.getLogger(
|
|
34
|
+
this.logger = opts?.logger || this.getLogger(`[96m[wooks-cli]`);
|
|
30
35
|
this.cliHelp = opts?.cliHelp instanceof CliHelpRenderer ? opts.cliHelp : new CliHelpRenderer(opts?.cliHelp);
|
|
31
36
|
}
|
|
32
37
|
/**
|
|
@@ -142,10 +147,10 @@ var WooksCli = class extends WooksAdapterBase {
|
|
|
142
147
|
for (const handler of handlers) {
|
|
143
148
|
const response = await handler();
|
|
144
149
|
if (typeof response === "string") console.log(response);
|
|
145
|
-
else if (Array.isArray(response)) response.forEach((r) => {
|
|
150
|
+
else if (Array.isArray(response)) response.forEach((r) => {
|
|
146
151
|
console.log(typeof r === "string" ? r : JSON.stringify(r, null, " "));
|
|
147
152
|
});
|
|
148
|
-
else if (response instanceof Error) {
|
|
153
|
+
else if (response instanceof Error) {
|
|
149
154
|
this.onError(response);
|
|
150
155
|
return response;
|
|
151
156
|
} else if (response) console.log(JSON.stringify(response, null, " "));
|
|
@@ -154,15 +159,15 @@ else if (response instanceof Error) {
|
|
|
154
159
|
this.onError(error);
|
|
155
160
|
return error;
|
|
156
161
|
}
|
|
157
|
-
else {
|
|
162
|
+
else {
|
|
158
163
|
this.onUnknownCommand(pathParams);
|
|
159
|
-
return new Error("Unknown command");
|
|
164
|
+
return /* @__PURE__ */ new Error("Unknown command");
|
|
160
165
|
}
|
|
161
166
|
});
|
|
162
167
|
}
|
|
163
168
|
onError(e) {
|
|
164
169
|
if (this.opts?.onError) this.opts.onError(e);
|
|
165
|
-
else {
|
|
170
|
+
else {
|
|
166
171
|
this.error(e.message);
|
|
167
172
|
process.exit(1);
|
|
168
173
|
}
|
|
@@ -173,23 +178,34 @@ else {
|
|
|
173
178
|
*/
|
|
174
179
|
onUnknownCommand(pathParams) {
|
|
175
180
|
const raiseError = () => {
|
|
176
|
-
this.error(
|
|
181
|
+
this.error(`[0mUnknown command: ${pathParams.join(" ")}`);
|
|
177
182
|
process.exit(1);
|
|
178
183
|
};
|
|
179
184
|
if (this.opts?.onUnknownCommand) this.opts.onUnknownCommand(pathParams, raiseError);
|
|
180
|
-
else raiseError();
|
|
185
|
+
else raiseError();
|
|
181
186
|
}
|
|
182
187
|
error(e) {
|
|
183
|
-
if (typeof e === "string") console.error(
|
|
184
|
-
else console.error(
|
|
188
|
+
if (typeof e === "string") console.error(`[31mERROR: [0m${e}`);
|
|
189
|
+
else console.error(`[31mERROR: [0m${e.message}`);
|
|
185
190
|
}
|
|
186
191
|
};
|
|
192
|
+
/**
|
|
193
|
+
* Factory for WooksCli App
|
|
194
|
+
* @param opts TWooksCliOptions
|
|
195
|
+
* @param wooks Wooks | WooksAdapterBase
|
|
196
|
+
* @returns WooksCli
|
|
197
|
+
*/
|
|
187
198
|
function createCliApp(opts, wooks) {
|
|
188
199
|
return new WooksCli(opts, wooks);
|
|
189
200
|
}
|
|
190
201
|
|
|
191
202
|
//#endregion
|
|
192
203
|
//#region packages/event-cli/src/composables/options.ts
|
|
204
|
+
/**
|
|
205
|
+
* Get CLI Options
|
|
206
|
+
*
|
|
207
|
+
* @returns an object with CLI options
|
|
208
|
+
*/
|
|
193
209
|
function useCliOptions() {
|
|
194
210
|
const { store } = useCliContext();
|
|
195
211
|
const flags = store("flags");
|
|
@@ -199,6 +215,12 @@ function useCliOptions() {
|
|
|
199
215
|
}
|
|
200
216
|
return flags.value;
|
|
201
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Getter for Cli Option value
|
|
220
|
+
*
|
|
221
|
+
* @param name name of the option
|
|
222
|
+
* @returns value of a CLI option
|
|
223
|
+
*/
|
|
202
224
|
function useCliOption(name) {
|
|
203
225
|
try {
|
|
204
226
|
const options = useCliHelp().getEntry().options || [];
|
|
@@ -212,6 +234,19 @@ function useCliOption(name) {
|
|
|
212
234
|
|
|
213
235
|
//#endregion
|
|
214
236
|
//#region packages/event-cli/src/composables/cli-help.ts
|
|
237
|
+
/**
|
|
238
|
+
* ## useCliHelp
|
|
239
|
+
* ### Composable
|
|
240
|
+
* ```js
|
|
241
|
+
* // example of printing cli instructions
|
|
242
|
+
* const { print } = useCliHelp()
|
|
243
|
+
* // print with colors
|
|
244
|
+
* print(true)
|
|
245
|
+
* // print with no colors
|
|
246
|
+
* // print(false)
|
|
247
|
+
* ```
|
|
248
|
+
* @returns
|
|
249
|
+
*/
|
|
215
250
|
function useCliHelp() {
|
|
216
251
|
const event = useCliContext().store("event");
|
|
217
252
|
const getCliHelp = () => event.get("cliHelp");
|
|
@@ -225,12 +260,68 @@ function useCliHelp() {
|
|
|
225
260
|
}
|
|
226
261
|
};
|
|
227
262
|
}
|
|
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
|
+
*/
|
|
228
286
|
function useAutoHelp(keys = ["help"], colors = true) {
|
|
229
287
|
for (const option of keys) if (useCliOption(option) === true) {
|
|
230
288
|
useCliHelp().print(colors);
|
|
231
289
|
return true;
|
|
232
290
|
}
|
|
233
291
|
}
|
|
292
|
+
/**
|
|
293
|
+
* ##useCommandLookupHelp
|
|
294
|
+
* ### Composable
|
|
295
|
+
*
|
|
296
|
+
* Tries to find valid command based on provided command.
|
|
297
|
+
*
|
|
298
|
+
* If manages to find a valid command, throws an error
|
|
299
|
+
* suggesting a list of valid commands
|
|
300
|
+
*
|
|
301
|
+
* Best to use in `onUnknownCommand` callback:
|
|
302
|
+
*
|
|
303
|
+
* ```js
|
|
304
|
+
* const app = createCliApp({
|
|
305
|
+
* onUnknownCommand: (path, raiseError) => {
|
|
306
|
+
* // will throw an error suggesting a list
|
|
307
|
+
* // of valid commands if could find some
|
|
308
|
+
* useCommandLookupHelp()
|
|
309
|
+
* // fallback to a regular error handler
|
|
310
|
+
* raiseError()
|
|
311
|
+
* },
|
|
312
|
+
* })
|
|
313
|
+
* ```
|
|
314
|
+
*
|
|
315
|
+
* @param lookupDepth depth of search in backwards
|
|
316
|
+
* @example
|
|
317
|
+
*
|
|
318
|
+
* For provided command `run test:drive dir`
|
|
319
|
+
* - lookup1: `run test:drive dir` (deep = 0)
|
|
320
|
+
* - lookup2: `run test:drive` (deep = 1)
|
|
321
|
+
* - lookup3: `run test` (deep = 2)
|
|
322
|
+
* - lookup4: `run` (deep = 3)
|
|
323
|
+
* ...
|
|
324
|
+
*/
|
|
234
325
|
function useCommandLookupHelp(lookupDepth = 3) {
|
|
235
326
|
const parts = useCliContext().store("event").get("pathParams")?.flatMap((p) => `${p} `.split(":").map((s, i) => i ? `:${s}` : s)) || [];
|
|
236
327
|
const cliHelp = useCliHelp().getCliHelp();
|
|
@@ -249,7 +340,7 @@ function useCommandLookupHelp(lookupDepth = 3) {
|
|
|
249
340
|
if (data) {
|
|
250
341
|
const { main, children } = data;
|
|
251
342
|
if (main.args && Object.keys(main.args).length > 0) throw new Error(`Arguments expected: ${Object.keys(main.args).map((l) => `<${l}>`).join(", ")}`);
|
|
252
|
-
else if (children?.length > 0) throw new Error(`Wrong command, did you mean:\n${children.slice(0, 7).map((c) => ` $ ${cmd} ${c.command}`).join("\n")}`);
|
|
343
|
+
else if (children?.length > 0) throw new Error(`Wrong command, did you mean:\n${children.slice(0, 7).map((c) => ` $ ${cmd} ${c.command}`).join("\n")}`);
|
|
253
344
|
}
|
|
254
345
|
}
|
|
255
346
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/event-cli",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "@wooksjs/event-cli",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -39,19 +39,19 @@
|
|
|
39
39
|
"url": "https://github.com/wooksjs/wooksjs/issues"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
|
-
"@
|
|
43
|
-
"
|
|
42
|
+
"@prostojs/logger": "^0.4.3",
|
|
43
|
+
"@prostojs/router": "^0.2.1",
|
|
44
|
+
"@wooksjs/event-core": "^0.6.1",
|
|
45
|
+
"wooks": "^0.6.1"
|
|
44
46
|
},
|
|
45
47
|
"dependencies": {
|
|
46
48
|
"@prostojs/cli-help": "^0.0.11",
|
|
47
|
-
"
|
|
48
|
-
"@prostojs/router": "^0.2.1",
|
|
49
|
-
"minimist": "^1.2.6"
|
|
49
|
+
"minimist": "^1.2.8"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@types/minimist": "^1.2.5",
|
|
53
|
-
"typescript": "^5.
|
|
54
|
-
"vitest": "^2.
|
|
53
|
+
"typescript": "^5.8.3",
|
|
54
|
+
"vitest": "^3.2.4"
|
|
55
55
|
},
|
|
56
56
|
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/event-cli#readme",
|
|
57
57
|
"scripts": {
|