@wooksjs/event-cli 0.2.18 → 0.2.20

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
@@ -11,7 +11,7 @@
11
11
 
12
12
  As a part of `wooks` event processing framework, `@wooksjs/event-cli` implements CLI events and provides composables that let you:
13
13
 
14
- - access flags (- or --)
14
+ - access flags (- or --)
15
15
 
16
16
  ## Install
17
17
 
package/dist/index.cjs CHANGED
@@ -28,6 +28,8 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
28
28
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
29
29
  PERFORMANCE OF THIS SOFTWARE.
30
30
  ***************************************************************************** */
31
+ /* global Reflect, Promise */
32
+
31
33
 
32
34
  function __awaiter(thisArg, _arguments, P, generator) {
33
35
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
@@ -52,14 +54,16 @@ class WooksCli extends wooks.WooksAdapterBase {
52
54
  return this.on('CLI', path, handler);
53
55
  }
54
56
  run(_argv) {
55
- var _a;
57
+ var _a, _b;
56
58
  return __awaiter(this, void 0, void 0, function* () {
57
59
  const argv = process.argv.slice(2) || _argv;
58
- const firstFlagIndex = argv.findIndex(a => a.startsWith('-')) + 1;
59
- const pathParams = (firstFlagIndex ? argv.slice(0, firstFlagIndex - 1) : argv);
60
- const path = '/' + pathParams.map(v => encodeURIComponent(v)).join('/');
61
- const { restoreCtx, clearCtx } = createCliContext({ argv }, this.mergeEventOptions((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions));
62
- const handlers = this.wooks.lookup('CLI', path);
60
+ const firstFlagIndex = argv.findIndex((a) => a.startsWith('-')) + 1;
61
+ const pathParams = (firstFlagIndex
62
+ ? argv.slice(0, firstFlagIndex - 1)
63
+ : argv).map((v) => encodeURIComponent(v));
64
+ const path = '/' + pathParams.join('/');
65
+ const { restoreCtx, clearCtx } = createCliContext({ argv, pathParams }, this.mergeEventOptions((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions));
66
+ const handlers = this.wooks.lookup('CLI', path) || ((_b = this.opts) === null || _b === void 0 ? void 0 : _b.onNotFound) && [this.opts.onNotFound] || null;
63
67
  if (handlers) {
64
68
  try {
65
69
  for (const handler of handlers) {
@@ -69,7 +73,9 @@ class WooksCli extends wooks.WooksAdapterBase {
69
73
  console.log(response);
70
74
  }
71
75
  else if (Array.isArray(response)) {
72
- response.forEach(r => console.log(typeof r === 'string' ? r : JSON.stringify(r, null, ' ')));
76
+ response.forEach((r) => console.log(typeof r === 'string'
77
+ ? r
78
+ : JSON.stringify(r, null, ' ')));
73
79
  }
74
80
  else if (response instanceof Error) {
75
81
  console.error('' + response.message + '');
@@ -87,7 +93,8 @@ class WooksCli extends wooks.WooksAdapterBase {
87
93
  clearCtx();
88
94
  }
89
95
  else {
90
- this.onUnknownParams(pathParams);
96
+ this.onUnknownCommand(pathParams);
97
+ clearCtx();
91
98
  }
92
99
  });
93
100
  }
@@ -101,13 +108,16 @@ class WooksCli extends wooks.WooksAdapterBase {
101
108
  process.exit(1);
102
109
  }
103
110
  }
104
- onUnknownParams(pathParams) {
111
+ onUnknownCommand(pathParams) {
105
112
  var _a;
106
- if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onUnknownParams) {
107
- this.opts.onUnknownParams(pathParams);
113
+ if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onUnknownCommand) {
114
+ this.opts.onUnknownCommand(pathParams);
108
115
  }
109
116
  else {
110
- this.error('' + 'Unknown command parameters: ' + '' + pathParams.join(' '));
117
+ this.error('' +
118
+ 'Unknown command: ' +
119
+ '' +
120
+ pathParams.join(' '));
111
121
  process.exit(1);
112
122
  }
113
123
  }
@@ -136,6 +146,12 @@ function useFlag(name) {
136
146
  return useFlags()[name];
137
147
  }
138
148
 
149
+ function usePathParams() {
150
+ const { store } = useCliContext();
151
+ const event = store('event');
152
+ return event.get('pathParams');
153
+ }
154
+
139
155
  exports.WooksCli = WooksCli;
140
156
  exports.cliShortcuts = cliShortcuts;
141
157
  exports.createCliApp = createCliApp;
@@ -143,3 +159,4 @@ exports.createCliContext = createCliContext;
143
159
  exports.useCliContext = useCliContext;
144
160
  exports.useFlag = useFlag;
145
161
  exports.useFlags = useFlags;
162
+ exports.usePathParams = usePathParams;
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ import { TConsoleBase } from '@prostojs/logger';
2
2
  import { TEmpty } from '@wooksjs/event-core';
3
3
  import { TEventOptions } from '@wooksjs/event-core';
4
4
  import { TGenericContextStore } from '@wooksjs/event-core';
5
- import { TProstoRouterPathBuilder } from '@prostojs/router';
5
+ import { TProstoRouterPathHandle } from '@prostojs/router';
6
6
  import { TWooksHandler } from 'wooks';
7
7
  import { Wooks } from 'wooks';
8
8
  import { WooksAdapterBase } from 'wooks';
@@ -13,7 +13,7 @@ export declare const cliShortcuts: {
13
13
 
14
14
  export declare function createCliApp(opts?: TWooksCliOptions, wooks?: Wooks | WooksAdapterBase): WooksCli;
15
15
 
16
- export declare function createCliContext(data: TCliEventData, options: TEventOptions): {
16
+ export declare function createCliContext(data: Omit<TCliEventData, 'type'>, options: TEventOptions): {
17
17
  getCtx: () => TCliContextStore & TGenericContextStore<TCliEventData>;
18
18
  restoreCtx: () => TGenericContextStore<TEmpty>;
19
19
  clearCtx: () => null;
@@ -43,12 +43,14 @@ export declare interface TCliContextStore {
43
43
 
44
44
  export declare interface TCliEventData {
45
45
  argv: string[];
46
+ pathParams: string[];
46
47
  type: 'CLI';
47
48
  }
48
49
 
49
50
  export declare interface TWooksCliOptions {
50
51
  onError?(e: Error): void;
51
- onUnknownParams?(pathParams: string[]): void;
52
+ onNotFound?: TWooksHandler<unknown>;
53
+ onUnknownCommand?: ((params: string[]) => unknown);
52
54
  logger?: TConsoleBase;
53
55
  eventOptions?: TEventOptions;
54
56
  }
@@ -81,14 +83,16 @@ export declare function useFlags(): {
81
83
  [name: string]: string | boolean;
82
84
  };
83
85
 
86
+ export declare function usePathParams(): string[];
87
+
84
88
  export declare class WooksCli extends WooksAdapterBase {
85
89
  protected opts?: TWooksCliOptions | undefined;
86
90
  protected logger: TConsoleBase;
87
91
  constructor(opts?: TWooksCliOptions | undefined, wooks?: Wooks | WooksAdapterBase);
88
- cli<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
92
+ cli<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathHandle<ParamsType>;
89
93
  run(_argv?: string[]): Promise<void>;
90
94
  onError(e: Error): void;
91
- onUnknownParams(pathParams: string[]): void;
95
+ onUnknownCommand(pathParams: string[]): void;
92
96
  error(e: string | Error): void;
93
97
  }
94
98
 
package/dist/index.mjs CHANGED
@@ -26,6 +26,8 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
26
26
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
27
27
  PERFORMANCE OF THIS SOFTWARE.
28
28
  ***************************************************************************** */
29
+ /* global Reflect, Promise */
30
+
29
31
 
30
32
  function __awaiter(thisArg, _arguments, P, generator) {
31
33
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
@@ -50,14 +52,16 @@ class WooksCli extends WooksAdapterBase {
50
52
  return this.on('CLI', path, handler);
51
53
  }
52
54
  run(_argv) {
53
- var _a;
55
+ var _a, _b;
54
56
  return __awaiter(this, void 0, void 0, function* () {
55
57
  const argv = process.argv.slice(2) || _argv;
56
- const firstFlagIndex = argv.findIndex(a => a.startsWith('-')) + 1;
57
- const pathParams = (firstFlagIndex ? argv.slice(0, firstFlagIndex - 1) : argv);
58
- const path = '/' + pathParams.map(v => encodeURIComponent(v)).join('/');
59
- const { restoreCtx, clearCtx } = createCliContext({ argv }, this.mergeEventOptions((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions));
60
- const handlers = this.wooks.lookup('CLI', path);
58
+ const firstFlagIndex = argv.findIndex((a) => a.startsWith('-')) + 1;
59
+ const pathParams = (firstFlagIndex
60
+ ? argv.slice(0, firstFlagIndex - 1)
61
+ : argv).map((v) => encodeURIComponent(v));
62
+ const path = '/' + pathParams.join('/');
63
+ const { restoreCtx, clearCtx } = createCliContext({ argv, pathParams }, this.mergeEventOptions((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions));
64
+ const handlers = this.wooks.lookup('CLI', path) || ((_b = this.opts) === null || _b === void 0 ? void 0 : _b.onNotFound) && [this.opts.onNotFound] || null;
61
65
  if (handlers) {
62
66
  try {
63
67
  for (const handler of handlers) {
@@ -67,7 +71,9 @@ class WooksCli extends WooksAdapterBase {
67
71
  console.log(response);
68
72
  }
69
73
  else if (Array.isArray(response)) {
70
- response.forEach(r => console.log(typeof r === 'string' ? r : JSON.stringify(r, null, ' ')));
74
+ response.forEach((r) => console.log(typeof r === 'string'
75
+ ? r
76
+ : JSON.stringify(r, null, ' ')));
71
77
  }
72
78
  else if (response instanceof Error) {
73
79
  console.error('' + response.message + '');
@@ -85,7 +91,8 @@ class WooksCli extends WooksAdapterBase {
85
91
  clearCtx();
86
92
  }
87
93
  else {
88
- this.onUnknownParams(pathParams);
94
+ this.onUnknownCommand(pathParams);
95
+ clearCtx();
89
96
  }
90
97
  });
91
98
  }
@@ -99,13 +106,16 @@ class WooksCli extends WooksAdapterBase {
99
106
  process.exit(1);
100
107
  }
101
108
  }
102
- onUnknownParams(pathParams) {
109
+ onUnknownCommand(pathParams) {
103
110
  var _a;
104
- if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onUnknownParams) {
105
- this.opts.onUnknownParams(pathParams);
111
+ if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onUnknownCommand) {
112
+ this.opts.onUnknownCommand(pathParams);
106
113
  }
107
114
  else {
108
- this.error('' + 'Unknown command parameters: ' + '' + pathParams.join(' '));
115
+ this.error('' +
116
+ 'Unknown command: ' +
117
+ '' +
118
+ pathParams.join(' '));
109
119
  process.exit(1);
110
120
  }
111
121
  }
@@ -134,4 +144,10 @@ function useFlag(name) {
134
144
  return useFlags()[name];
135
145
  }
136
146
 
137
- export { WooksCli, cliShortcuts, createCliApp, createCliContext, useCliContext, useFlag, useFlags };
147
+ function usePathParams() {
148
+ const { store } = useCliContext();
149
+ const event = store('event');
150
+ return event.get('pathParams');
151
+ }
152
+
153
+ export { WooksCli, cliShortcuts, createCliApp, createCliContext, useCliContext, useFlag, useFlags, usePathParams };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wooksjs/event-cli",
3
- "version": "0.2.18",
3
+ "version": "0.2.20",
4
4
  "description": "@wooksjs/event-cli",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -31,12 +31,12 @@
31
31
  "url": "https://github.com/wooksjs/wooksjs/issues"
32
32
  },
33
33
  "peerDependencies": {
34
- "wooks": "0.2.18",
35
- "@wooksjs/event-core": "0.2.18"
34
+ "wooks": "0.2.20",
35
+ "@wooksjs/event-core": "0.2.20"
36
36
  },
37
37
  "dependencies": {
38
38
  "minimist": "^1.2.6",
39
- "@prostojs/logger": "^0.3.4"
39
+ "@prostojs/logger": "^0.3.6"
40
40
  },
41
41
  "homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/event-cli#readme"
42
42
  }