@wooksjs/event-cli 0.4.10 → 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 CHANGED
@@ -7,113 +7,40 @@ var minimist = require('minimist');
7
7
 
8
8
  function createCliContext(data, options) {
9
9
  return eventCore.createEventContext({
10
- event: Object.assign(Object.assign({}, data), { type: 'CLI' }),
10
+ event: {
11
+ ...data,
12
+ type: 'CLI',
13
+ },
11
14
  options,
12
15
  });
13
16
  }
14
- /**
15
- * Wrapper on top of useEventContext that provides
16
- * proper context types for CLI event
17
- * @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
18
- */
19
17
  function useCliContext() {
20
18
  return eventCore.useEventContext('CLI');
21
19
  }
22
20
 
23
- /******************************************************************************
24
- Copyright (c) Microsoft Corporation.
25
-
26
- Permission to use, copy, modify, and/or distribute this software for any
27
- purpose with or without fee is hereby granted.
28
-
29
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
30
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
31
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
32
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
33
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
34
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
35
- PERFORMANCE OF THIS SOFTWARE.
36
- ***************************************************************************** */
37
- /* global Reflect, Promise, SuppressedError, Symbol */
38
-
39
-
40
- function __awaiter(thisArg, _arguments, P, generator) {
41
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
42
- return new (P || (P = Promise))(function (resolve, reject) {
43
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
44
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
45
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
46
- step((generator = generator.apply(thisArg, _arguments || [])).next());
47
- });
48
- }
49
-
50
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
51
- var e = new Error(message);
52
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
53
- };
54
-
55
21
  const cliShortcuts = {
56
22
  cli: 'CLI',
57
23
  };
58
24
  class WooksCli extends wooks.WooksAdapterBase {
59
25
  constructor(opts, wooks) {
60
- super(wooks, opts === null || opts === void 0 ? void 0 : opts.logger, opts === null || opts === void 0 ? void 0 : opts.router);
26
+ super(wooks, opts?.logger, opts?.router);
61
27
  this.opts = opts;
62
28
  this.alreadyComputedAliases = false;
63
- this.logger = (opts === null || opts === void 0 ? void 0 : opts.logger) || this.getLogger('wooks-cli');
29
+ this.logger = opts?.logger || this.getLogger('wooks-cli');
64
30
  this.cliHelp =
65
- (opts === null || opts === void 0 ? void 0 : opts.cliHelp) instanceof cliHelp.CliHelpRenderer
31
+ opts?.cliHelp instanceof cliHelp.CliHelpRenderer
66
32
  ? opts.cliHelp
67
- : new cliHelp.CliHelpRenderer(opts === null || opts === void 0 ? void 0 : opts.cliHelp);
33
+ : new cliHelp.CliHelpRenderer(opts?.cliHelp);
68
34
  }
69
- /**
70
- * ### Register CLI Command
71
- * Command path segments may be separated by / or space.
72
- *
73
- * For example the folowing path are interpreted the same:
74
- * - "command test use:dev :name"
75
- * - "command/test/use:dev/:name"
76
- *
77
- * Where name will become an argument
78
- *
79
- * ```js
80
- * // example without options
81
- * app.cli('command/:arg', () => 'arg = ' + useRouteParams().params.arg )
82
- *
83
- * // example with options
84
- * app.cli('command/:arg', {
85
- * description: 'Description of the command',
86
- * options: [{ keys: ['project', 'p'], description: 'Description of the option', value: 'myProject' }],
87
- * args: { arg: 'Description of the arg' },
88
- * aliases: ['cmd'], // alias "cmd/:arg" will be registered
89
- * examples: [{
90
- * description: 'Example of usage with someProject',
91
- * cmd: 'argValue -p=someProject',
92
- * // will result in help display:
93
- * // "# Example of usage with someProject\n" +
94
- * // "$ myCli command argValue -p=someProject\n"
95
- * }],
96
- * handler: () => 'arg = ' + useRouteParams().params.arg
97
- * })
98
- * ```
99
- *
100
- * @param path command path
101
- * @param _options handler or options
102
- *
103
- * @returns
104
- */
105
35
  cli(path, _options) {
106
- var _a;
107
36
  const options = typeof _options === 'function' ? { handler: _options } : _options;
108
37
  const handler = typeof _options === 'function' ? _options : _options.handler;
109
38
  const makePath = (s) => '/' + s.replace(/\s+/g, '/');
110
- // register handler
111
39
  const targetPath = makePath(path);
112
40
  const routed = this.on('CLI', targetPath, handler);
113
41
  if (options.onRegister) {
114
42
  options.onRegister(targetPath, 0, routed);
115
43
  }
116
- // register direct aliases
117
44
  for (const alias of options.aliases || []) {
118
45
  const vars = routed.getArgs().map((k) => ':' + k).join('/');
119
46
  const targetPath = makePath(alias) + (vars ? '/' + vars : '');
@@ -122,9 +49,10 @@ class WooksCli extends wooks.WooksAdapterBase {
122
49
  options.onRegister(targetPath, 1, routed);
123
50
  }
124
51
  }
125
- // register helpCli entry
126
52
  const command = routed.getStaticPart().replace(/\//g, ' ').trim();
127
- const args = Object.assign({}, (options.args || {}));
53
+ const args = {
54
+ ...(options.args || {}),
55
+ };
128
56
  for (const arg of routed.getArgs()) {
129
57
  if (!args[arg]) {
130
58
  args[arg] = '';
@@ -132,7 +60,7 @@ class WooksCli extends wooks.WooksAdapterBase {
132
60
  }
133
61
  this.cliHelp.addEntry({
134
62
  command,
135
- aliases: (_a = options.aliases) === null || _a === void 0 ? void 0 : _a.map(alias => alias.replace(/\\:/g, ':')), // unescape ":" character
63
+ aliases: options.aliases?.map(alias => alias.replace(/\\:/g, ':')),
136
64
  args,
137
65
  description: options.description,
138
66
  examples: options.examples,
@@ -161,73 +89,57 @@ class WooksCli extends wooks.WooksAdapterBase {
161
89
  }
162
90
  }
163
91
  }
164
- /**
165
- * ## run
166
- * ### Start command processing
167
- * Triggers command processing
168
- *
169
- * By default takes `process.argv.slice(2)` as a command
170
- *
171
- * It's possible to replace the command by passing an argument
172
- *
173
- * @param _argv optionally overwrite `process.argv.slice(2)` with your `argv` array
174
- */
175
- run(_argv, _opts) {
176
- var _a, _b;
177
- return __awaiter(this, void 0, void 0, function* () {
178
- const argv = _argv || process.argv.slice(2);
179
- const parsedFlags = minimist(argv, _opts);
180
- const pathParams = parsedFlags._;
181
- const path = '/' +
182
- pathParams.map((v) => encodeURI(v).replace(/\//g, '%2F')).join('/');
183
- const { restoreCtx, clearCtx, store } = createCliContext({ opts: _opts, argv, pathParams, cliHelp: this.cliHelp, command: path.replace(/\//g, ' ').trim() }, this.mergeEventOptions((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions));
184
- store('flags').value = parsedFlags;
185
- this.computeAliases();
186
- const { handlers: foundHandlers, firstStatic } = this.wooks.lookup('CLI', path);
187
- if (typeof firstStatic === 'string') {
188
- // overwriting command with firstStatic to properly search for help
189
- store('event').set('command', firstStatic.replace(/\//g, ' ').trim());
190
- }
191
- const handlers = foundHandlers ||
192
- (((_b = this.opts) === null || _b === void 0 ? void 0 : _b.onNotFound) && [this.opts.onNotFound]) ||
193
- null;
194
- if (handlers) {
195
- try {
196
- for (const handler of handlers) {
197
- restoreCtx();
198
- const response = yield handler();
199
- if (typeof response === 'string') {
200
- console.log(response);
201
- }
202
- else if (Array.isArray(response)) {
203
- response.forEach((r) => console.log(typeof r === 'string'
204
- ? r
205
- : JSON.stringify(r, null, ' ')));
206
- }
207
- else if (response instanceof Error) {
208
- this.onError(response);
209
- }
210
- else if (response) {
211
- if (response) {
212
- console.log(JSON.stringify(response, null, ' '));
213
- }
92
+ async run(_argv, _opts) {
93
+ const argv = _argv || process.argv.slice(2);
94
+ const parsedFlags = minimist(argv, _opts);
95
+ const pathParams = parsedFlags._;
96
+ const path = '/' +
97
+ pathParams.map((v) => encodeURI(v).replace(/\//g, '%2F')).join('/');
98
+ const { restoreCtx, clearCtx, store } = createCliContext({ opts: _opts, argv, pathParams, cliHelp: this.cliHelp, command: path.replace(/\//g, ' ').trim() }, this.mergeEventOptions(this.opts?.eventOptions));
99
+ store('flags').value = parsedFlags;
100
+ this.computeAliases();
101
+ const { handlers: foundHandlers, firstStatic } = this.wooks.lookup('CLI', path);
102
+ if (typeof firstStatic === 'string') {
103
+ store('event').set('command', firstStatic.replace(/\//g, ' ').trim());
104
+ }
105
+ const handlers = foundHandlers ||
106
+ (this.opts?.onNotFound && [this.opts.onNotFound]) ||
107
+ null;
108
+ if (handlers) {
109
+ try {
110
+ for (const handler of handlers) {
111
+ restoreCtx();
112
+ const response = await handler();
113
+ if (typeof response === 'string') {
114
+ console.log(response);
115
+ }
116
+ else if (Array.isArray(response)) {
117
+ response.forEach((r) => console.log(typeof r === 'string'
118
+ ? r
119
+ : JSON.stringify(r, null, ' ')));
120
+ }
121
+ else if (response instanceof Error) {
122
+ this.onError(response);
123
+ }
124
+ else if (response) {
125
+ if (response) {
126
+ console.log(JSON.stringify(response, null, ' '));
214
127
  }
215
128
  }
216
129
  }
217
- catch (e) {
218
- this.onError(e);
219
- }
220
- clearCtx();
221
130
  }
222
- else {
223
- this.onUnknownCommand(pathParams);
224
- clearCtx();
131
+ catch (e) {
132
+ this.onError(e);
225
133
  }
226
- });
134
+ clearCtx();
135
+ }
136
+ else {
137
+ this.onUnknownCommand(pathParams);
138
+ clearCtx();
139
+ }
227
140
  }
228
141
  onError(e) {
229
- var _a;
230
- if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onError) {
142
+ if (this.opts?.onError) {
231
143
  this.opts.onError(e);
232
144
  }
233
145
  else {
@@ -235,17 +147,12 @@ class WooksCli extends wooks.WooksAdapterBase {
235
147
  process.exit(1);
236
148
  }
237
149
  }
238
- /**
239
- * Triggers `unknown command` processing and callbacks
240
- * @param pathParams `string[]` containing command
241
- */
242
150
  onUnknownCommand(pathParams) {
243
- var _a;
244
151
  const raiseError = () => {
245
152
  this.error('' + 'Unknown command: ' + pathParams.join(' '));
246
153
  process.exit(1);
247
154
  };
248
- if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onUnknownCommand) {
155
+ if (this.opts?.onUnknownCommand) {
249
156
  this.opts.onUnknownCommand(pathParams, raiseError);
250
157
  }
251
158
  else {
@@ -261,33 +168,14 @@ class WooksCli extends wooks.WooksAdapterBase {
261
168
  }
262
169
  }
263
170
  }
264
- /**
265
- * Factory for WooksCli App
266
- * @param opts TWooksCliOptions
267
- * @param wooks Wooks | WooksAdapterBase
268
- * @returns WooksCli
269
- */
270
171
  function createCliApp(opts, wooks) {
271
172
  return new WooksCli(opts, wooks);
272
173
  }
273
174
 
274
- /**
275
- * ## useCliHelp
276
- * ### Composable
277
- * ```js
278
- * // example of printing cli instructions
279
- * const { print } = useCliHelp()
280
- * // print with colors
281
- * print(true)
282
- * // print with no colors
283
- * // print(false)
284
- * ```
285
- * @returns
286
- */
287
175
  function useCliHelp() {
288
176
  const event = useCliContext().store('event');
289
177
  const getCliHelp = () => event.get('cliHelp');
290
- const getEntry = () => { var _a; return (_a = getCliHelp().match(event.get('command'))) === null || _a === void 0 ? void 0 : _a.main; };
178
+ const getEntry = () => getCliHelp().match(event.get('command'))?.main;
291
179
  return {
292
180
  getCliHelp,
293
181
  getEntry,
@@ -295,74 +183,14 @@ function useCliHelp() {
295
183
  print: (withColors) => getCliHelp().print(event.get('command'), withColors),
296
184
  };
297
185
  }
298
- /**
299
- * ## useAutoHelp
300
- * ### Composable
301
- *
302
- * Prints help if `--help` option provided.
303
- *
304
- * ```js
305
- * // example of use: print help and exit
306
- * app.cli('test', () => {
307
- * useAutoHelp() && process.exit(0)
308
- * return 'hit test command'
309
- * })
310
- *
311
- * // add option -h to print help, no colors
312
- * app.cli('test/nocolors', () => {
313
- * useAutoHelp(['help', 'h'], false) && process.exit(0)
314
- * return 'hit test nocolors command'
315
- * })
316
- * ```
317
- * @param keys default `['help']` - list of options to trigger help render
318
- * @param colors default `true`, prints with colors when true
319
- * @returns true when --help was provided. Otherwise returns false
320
- */
321
186
  function useAutoHelp(keys = ['help'], colors = true) {
322
187
  for (const option of keys) {
323
188
  if (useCliOption(option) === true) {
324
- // try {
325
189
  useCliHelp().print(colors);
326
190
  return true;
327
- // } catch (e) {
328
- // throw new
329
- // }
330
191
  }
331
192
  }
332
193
  }
333
- /**
334
- * ##useCommandLookupHelp
335
- * ### Composable
336
- *
337
- * Tries to find valid command based on provided command.
338
- *
339
- * If manages to find a valid command, throws an error
340
- * suggesting a list of valid commands
341
- *
342
- * Best to use in `onUnknownCommand` callback:
343
- *
344
- * ```js
345
- * const app = createCliApp({
346
- * onUnknownCommand: (path, raiseError) => {
347
- * // will throw an error suggesting a list
348
- * // of valid commands if could find some
349
- * useCommandLookupHelp()
350
- * // fallback to a regular error handler
351
- * raiseError()
352
- * },
353
- * })
354
- * ```
355
- *
356
- * @param lookupDepth depth of search in backwards
357
- * @example
358
- *
359
- * For provided command `run test:drive dir`
360
- * - lookup1: `run test:drive dir` (deep = 0)
361
- * - lookup2: `run test:drive` (deep = 1)
362
- * - lookup3: `run test` (deep = 2)
363
- * - lookup4: `run` (deep = 3)
364
- * ...
365
- */
366
194
  function useCommandLookupHelp(lookupDepth = 3) {
367
195
  const parts = useCliContext()
368
196
  .store('event')
@@ -407,11 +235,6 @@ function useCommandLookupHelp(lookupDepth = 3) {
407
235
  }
408
236
  }
409
237
 
410
- /**
411
- * Get CLI Options
412
- *
413
- * @returns an object with CLI options
414
- */
415
238
  function useCliOptions() {
416
239
  const { store } = useCliContext();
417
240
  const flags = store('flags');
@@ -421,16 +244,9 @@ function useCliOptions() {
421
244
  }
422
245
  return flags.value;
423
246
  }
424
- /**
425
- * Getter for Cli Option value
426
- *
427
- * @param name name of the option
428
- * @returns value of a CLI option
429
- */
430
247
  function useCliOption(name) {
431
- var _a;
432
248
  try {
433
- const options = ((_a = useCliHelp().getEntry()) === null || _a === void 0 ? void 0 : _a.options) || [];
249
+ const options = useCliHelp().getEntry()?.options || [];
434
250
  const opt = options.find(o => o.keys.includes(name));
435
251
  if (opt) {
436
252
  for (const key of opt.keys) {
@@ -441,7 +257,6 @@ function useCliOption(name) {
441
257
  }
442
258
  }
443
259
  catch (e) {
444
- //
445
260
  }
446
261
  return useCliOptions()[name];
447
262
  }