@wooksjs/event-cli 0.3.0 → 0.3.2

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/README.md CHANGED
@@ -21,13 +21,13 @@ As a part of `wooks` event processing framework, `@wooksjs/event-cli` implements
21
21
 
22
22
  ```js
23
23
  import { useRouteParams } from 'wooks'
24
- import { createCliApp, useFlags } from '@wooksjs/event-cli'
24
+ import { createCliApp, useCliOptions } from '@wooksjs/event-cli'
25
25
 
26
26
  const app = createCliApp()
27
27
 
28
28
  app.cli('test', () => {
29
- console.log('flags:')
30
- return useFlags()
29
+ console.log('options:')
30
+ return useCliOptions()
31
31
  })
32
32
 
33
33
  app.cli(':arg', () => {
@@ -43,4 +43,4 @@ app.run()
43
43
 
44
44
  ## Documentation
45
45
 
46
- To check out docs, visit [wooksjs.org](https://wooksjs.org/).
46
+ To check out docs, visit [wooksjs.org](https://wooksjs.org/guide/cli/).
package/dist/index.cjs CHANGED
@@ -11,6 +11,11 @@ function createCliContext(data, options) {
11
11
  options,
12
12
  });
13
13
  }
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
+ */
14
19
  function useCliContext() {
15
20
  return eventCore.useEventContext('CLI');
16
21
  }
@@ -93,6 +98,7 @@ class WooksCli extends wooks.WooksAdapterBase {
93
98
  * @returns
94
99
  */
95
100
  cli(path, _options) {
101
+ var _a;
96
102
  const options = typeof _options === 'function' ? { handler: _options } : _options;
97
103
  const handler = typeof _options === 'function' ? _options : _options.handler;
98
104
  const makePath = (s) => '/' + s.replace(/\s+/g, '/');
@@ -121,7 +127,7 @@ class WooksCli extends wooks.WooksAdapterBase {
121
127
  }
122
128
  this.cliHelp.addEntry({
123
129
  command,
124
- aliases: options.aliases,
130
+ aliases: (_a = options.aliases) === null || _a === void 0 ? void 0 : _a.map(alias => alias.replace(/\\:/g, ':')),
125
131
  args,
126
132
  description: options.description,
127
133
  examples: options.examples,
@@ -165,10 +171,7 @@ class WooksCli extends wooks.WooksAdapterBase {
165
171
  var _a, _b;
166
172
  return __awaiter(this, void 0, void 0, function* () {
167
173
  const argv = _argv || process.argv.slice(2);
168
- const firstFlagIndex = argv.findIndex((a) => a.startsWith('-')) + 1;
169
- const pathParams = firstFlagIndex
170
- ? argv.slice(0, firstFlagIndex - 1)
171
- : argv;
174
+ const pathParams = argv.filter(a => !a.startsWith('-'));
172
175
  const path = '/' +
173
176
  pathParams.map((v) => encodeURI(v).replace(/\//g, '%2F')).join('/');
174
177
  const { restoreCtx, clearCtx, store } = createCliContext({ argv, pathParams, cliHelp: this.cliHelp, command: path.replace(/\//g, ' ').trim() }, this.mergeEventOptions((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions));
@@ -251,6 +254,12 @@ class WooksCli extends wooks.WooksAdapterBase {
251
254
  }
252
255
  }
253
256
  }
257
+ /**
258
+ * Factory for WooksCli App
259
+ * @param opts TWooksCliOptions
260
+ * @param wooks Wooks | WooksAdapterBase
261
+ * @returns WooksHttp
262
+ */
254
263
  function createCliApp(opts, wooks) {
255
264
  return new WooksCli(opts, wooks);
256
265
  }
@@ -304,7 +313,7 @@ function useCliHelp() {
304
313
  */
305
314
  function useAutoHelp(keys = ['help'], colors = true) {
306
315
  for (const option of keys) {
307
- if (useFlag(option) === true) {
316
+ if (useCliOption(option) === true) {
308
317
  // try {
309
318
  useCliHelp().print(colors);
310
319
  return true;
@@ -391,7 +400,12 @@ function useCommandLookupHelp(lookupDepth = 3) {
391
400
  }
392
401
  }
393
402
 
394
- function useFlags() {
403
+ /**
404
+ * Get CLI Options
405
+ *
406
+ * @returns an object with CLI options
407
+ */
408
+ function useCliOptions() {
395
409
  const { store } = useCliContext();
396
410
  const flags = store('flags');
397
411
  if (!flags.value) {
@@ -399,15 +413,21 @@ function useFlags() {
399
413
  }
400
414
  return flags.value;
401
415
  }
402
- function useFlag(name) {
416
+ /**
417
+ * Getter for Cli Option value
418
+ *
419
+ * @param name name of the option
420
+ * @returns value of a CLI option
421
+ */
422
+ function useCliOption(name) {
403
423
  var _a;
404
424
  try {
405
425
  const options = ((_a = useCliHelp().getEntry()) === null || _a === void 0 ? void 0 : _a.options) || [];
406
426
  const opt = options.find(o => o.keys.includes(name));
407
427
  if (opt) {
408
428
  for (const key of opt.keys) {
409
- if (useFlags()[key]) {
410
- return useFlags()[key];
429
+ if (useCliOptions()[key]) {
430
+ return useCliOptions()[key];
411
431
  }
412
432
  }
413
433
  }
@@ -415,13 +435,7 @@ function useFlag(name) {
415
435
  catch (e) {
416
436
  //
417
437
  }
418
- return useFlags()[name];
419
- }
420
-
421
- function usePathParams() {
422
- const { store } = useCliContext();
423
- const event = store('event');
424
- return event.get('pathParams');
438
+ return useCliOptions()[name];
425
439
  }
426
440
 
427
441
  exports.WooksCli = WooksCli;
@@ -431,7 +445,6 @@ exports.createCliContext = createCliContext;
431
445
  exports.useAutoHelp = useAutoHelp;
432
446
  exports.useCliContext = useCliContext;
433
447
  exports.useCliHelp = useCliHelp;
448
+ exports.useCliOption = useCliOption;
449
+ exports.useCliOptions = useCliOptions;
434
450
  exports.useCommandLookupHelp = useCommandLookupHelp;
435
- exports.useFlag = useFlag;
436
- exports.useFlags = useFlags;
437
- exports.usePathParams = usePathParams;
package/dist/index.d.ts CHANGED
@@ -15,6 +15,12 @@ export declare const cliShortcuts: {
15
15
  cli: string;
16
16
  };
17
17
 
18
+ /**
19
+ * Factory for WooksCli App
20
+ * @param opts TWooksCliOptions
21
+ * @param wooks Wooks | WooksAdapterBase
22
+ * @returns WooksHttp
23
+ */
18
24
  export declare function createCliApp(opts?: TWooksCliOptions, wooks?: Wooks | WooksAdapterBase): WooksCli;
19
25
 
20
26
  export declare function createCliContext(data: Omit<TCliEventData, 'type'>, options: TEventOptions): {
@@ -105,6 +111,11 @@ export declare interface TWooksCliOptions {
105
111
  */
106
112
  export declare function useAutoHelp(keys?: string[], colors?: boolean): true | undefined;
107
113
 
114
+ /**
115
+ * Wrapper on top of useEventContext that provides
116
+ * proper context types for CLI event
117
+ * @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
118
+ */
108
119
  export declare function useCliContext<T extends TEmpty>(): {
109
120
  getCtx: () => TCliContextStore & T & TGenericContextStore<TCliEventData>;
110
121
  restoreCtx: () => TGenericContextStore<TEmpty>;
@@ -147,6 +158,23 @@ export declare function useCliHelp(): {
147
158
  print: (withColors?: boolean) => void;
148
159
  };
149
160
 
161
+ /**
162
+ * Getter for Cli Option value
163
+ *
164
+ * @param name name of the option
165
+ * @returns value of a CLI option
166
+ */
167
+ export declare function useCliOption(name: string): string | boolean;
168
+
169
+ /**
170
+ * Get CLI Options
171
+ *
172
+ * @returns an object with CLI options
173
+ */
174
+ export declare function useCliOptions(): {
175
+ [name: string]: string | boolean;
176
+ };
177
+
150
178
  /**
151
179
  * ##useCommandLookupHelp
152
180
  * ### Composable
@@ -182,14 +210,6 @@ export declare function useCliHelp(): {
182
210
  */
183
211
  export declare function useCommandLookupHelp(lookupDepth?: number): void;
184
212
 
185
- export declare function useFlag(name: string): string | boolean;
186
-
187
- export declare function useFlags(): {
188
- [name: string]: string | boolean;
189
- };
190
-
191
- export declare function usePathParams(): string[];
192
-
193
213
  export declare class WooksCli extends WooksAdapterBase {
194
214
  protected opts?: TWooksCliOptions | undefined;
195
215
  protected logger: TConsoleBase;
package/dist/index.mjs CHANGED
@@ -9,6 +9,11 @@ function createCliContext(data, options) {
9
9
  options,
10
10
  });
11
11
  }
12
+ /**
13
+ * Wrapper on top of useEventContext that provides
14
+ * proper context types for CLI event
15
+ * @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
16
+ */
12
17
  function useCliContext() {
13
18
  return useEventContext('CLI');
14
19
  }
@@ -91,6 +96,7 @@ class WooksCli extends WooksAdapterBase {
91
96
  * @returns
92
97
  */
93
98
  cli(path, _options) {
99
+ var _a;
94
100
  const options = typeof _options === 'function' ? { handler: _options } : _options;
95
101
  const handler = typeof _options === 'function' ? _options : _options.handler;
96
102
  const makePath = (s) => '/' + s.replace(/\s+/g, '/');
@@ -119,7 +125,7 @@ class WooksCli extends WooksAdapterBase {
119
125
  }
120
126
  this.cliHelp.addEntry({
121
127
  command,
122
- aliases: options.aliases,
128
+ aliases: (_a = options.aliases) === null || _a === void 0 ? void 0 : _a.map(alias => alias.replace(/\\:/g, ':')),
123
129
  args,
124
130
  description: options.description,
125
131
  examples: options.examples,
@@ -163,10 +169,7 @@ class WooksCli extends WooksAdapterBase {
163
169
  var _a, _b;
164
170
  return __awaiter(this, void 0, void 0, function* () {
165
171
  const argv = _argv || process.argv.slice(2);
166
- const firstFlagIndex = argv.findIndex((a) => a.startsWith('-')) + 1;
167
- const pathParams = firstFlagIndex
168
- ? argv.slice(0, firstFlagIndex - 1)
169
- : argv;
172
+ const pathParams = argv.filter(a => !a.startsWith('-'));
170
173
  const path = '/' +
171
174
  pathParams.map((v) => encodeURI(v).replace(/\//g, '%2F')).join('/');
172
175
  const { restoreCtx, clearCtx, store } = createCliContext({ argv, pathParams, cliHelp: this.cliHelp, command: path.replace(/\//g, ' ').trim() }, this.mergeEventOptions((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions));
@@ -249,6 +252,12 @@ class WooksCli extends WooksAdapterBase {
249
252
  }
250
253
  }
251
254
  }
255
+ /**
256
+ * Factory for WooksCli App
257
+ * @param opts TWooksCliOptions
258
+ * @param wooks Wooks | WooksAdapterBase
259
+ * @returns WooksHttp
260
+ */
252
261
  function createCliApp(opts, wooks) {
253
262
  return new WooksCli(opts, wooks);
254
263
  }
@@ -302,7 +311,7 @@ function useCliHelp() {
302
311
  */
303
312
  function useAutoHelp(keys = ['help'], colors = true) {
304
313
  for (const option of keys) {
305
- if (useFlag(option) === true) {
314
+ if (useCliOption(option) === true) {
306
315
  // try {
307
316
  useCliHelp().print(colors);
308
317
  return true;
@@ -389,7 +398,12 @@ function useCommandLookupHelp(lookupDepth = 3) {
389
398
  }
390
399
  }
391
400
 
392
- function useFlags() {
401
+ /**
402
+ * Get CLI Options
403
+ *
404
+ * @returns an object with CLI options
405
+ */
406
+ function useCliOptions() {
393
407
  const { store } = useCliContext();
394
408
  const flags = store('flags');
395
409
  if (!flags.value) {
@@ -397,15 +411,21 @@ function useFlags() {
397
411
  }
398
412
  return flags.value;
399
413
  }
400
- function useFlag(name) {
414
+ /**
415
+ * Getter for Cli Option value
416
+ *
417
+ * @param name name of the option
418
+ * @returns value of a CLI option
419
+ */
420
+ function useCliOption(name) {
401
421
  var _a;
402
422
  try {
403
423
  const options = ((_a = useCliHelp().getEntry()) === null || _a === void 0 ? void 0 : _a.options) || [];
404
424
  const opt = options.find(o => o.keys.includes(name));
405
425
  if (opt) {
406
426
  for (const key of opt.keys) {
407
- if (useFlags()[key]) {
408
- return useFlags()[key];
427
+ if (useCliOptions()[key]) {
428
+ return useCliOptions()[key];
409
429
  }
410
430
  }
411
431
  }
@@ -413,13 +433,7 @@ function useFlag(name) {
413
433
  catch (e) {
414
434
  //
415
435
  }
416
- return useFlags()[name];
417
- }
418
-
419
- function usePathParams() {
420
- const { store } = useCliContext();
421
- const event = store('event');
422
- return event.get('pathParams');
436
+ return useCliOptions()[name];
423
437
  }
424
438
 
425
- export { WooksCli, cliShortcuts, createCliApp, createCliContext, useAutoHelp, useCliContext, useCliHelp, useCommandLookupHelp, useFlag, useFlags, usePathParams };
439
+ export { WooksCli, cliShortcuts, createCliApp, createCliContext, useAutoHelp, useCliContext, useCliHelp, useCliOption, useCliOptions, useCommandLookupHelp };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wooksjs/event-cli",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "@wooksjs/event-cli",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -31,8 +31,8 @@
31
31
  "url": "https://github.com/wooksjs/wooksjs/issues"
32
32
  },
33
33
  "peerDependencies": {
34
- "wooks": "0.3.0",
35
- "@wooksjs/event-core": "0.3.0"
34
+ "wooks": "0.3.2",
35
+ "@wooksjs/event-core": "0.3.2"
36
36
  },
37
37
  "dependencies": {
38
38
  "minimist": "^1.2.6",