@wooksjs/event-cli 0.2.5 → 0.2.7

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
@@ -38,13 +38,6 @@ function __awaiter(thisArg, _arguments, P, generator) {
38
38
  });
39
39
  }
40
40
 
41
- const banner = () => `[${"@wooksjs/event-cli"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
42
-
43
- /* istanbul ignore file */
44
- function logError(error) {
45
- console.error('' + '' + banner() + error + '');
46
- }
47
-
48
41
  const cliShortcuts = {
49
42
  cli: 'CLI',
50
43
  };
@@ -60,7 +53,8 @@ class WooksCli extends wooks.WooksAdapterBase {
60
53
  return __awaiter(this, void 0, void 0, function* () {
61
54
  const argv = process.argv.slice(2) || _argv;
62
55
  const firstFlagIndex = argv.findIndex(a => a.startsWith('-')) + 1;
63
- const path = '/' + (firstFlagIndex ? argv.slice(0, firstFlagIndex - 1) : argv).map(v => encodeURIComponent(v)).join('/');
56
+ const pathParams = (firstFlagIndex ? argv.slice(0, firstFlagIndex - 1) : argv);
57
+ const path = '/' + pathParams.map(v => encodeURIComponent(v)).join('/');
64
58
  const { restoreCtx, clearCtx } = createCliContext({ argv });
65
59
  const handlers = this.wooks.lookup('CLI', path);
66
60
  if (handlers) {
@@ -74,23 +68,54 @@ class WooksCli extends wooks.WooksAdapterBase {
74
68
  else if (Array.isArray(response)) {
75
69
  response.forEach(r => console.log(typeof r === 'string' ? r : JSON.stringify(r, null, ' ')));
76
70
  }
77
- else {
78
- console.log(JSON.stringify(response, null, ' '));
71
+ else if (response instanceof Error) {
72
+ console.error('' + response.message + '');
73
+ }
74
+ else if (response) {
75
+ if (response) {
76
+ console.log(JSON.stringify(response, null, ' '));
77
+ }
79
78
  }
80
79
  }
81
80
  }
82
81
  catch (e) {
83
- logError(e.message);
84
- process.exit(1);
82
+ this.onError(e);
85
83
  }
86
84
  clearCtx();
87
85
  }
88
86
  else {
89
- logError('Unknown command parameters');
90
- process.exit(1);
87
+ this.onUnknownParams(pathParams);
91
88
  }
92
89
  });
93
90
  }
91
+ onError(e) {
92
+ var _a;
93
+ if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onError) {
94
+ this.opts.onError(e);
95
+ }
96
+ else {
97
+ this.error(e.message);
98
+ process.exit(1);
99
+ }
100
+ }
101
+ onUnknownParams(pathParams) {
102
+ var _a;
103
+ if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onUnknownParams) {
104
+ this.opts.onUnknownParams(pathParams);
105
+ }
106
+ else {
107
+ this.error('' + 'Unknown command parameters: ' + '' + pathParams.join(' '));
108
+ process.exit(1);
109
+ }
110
+ }
111
+ error(e) {
112
+ if (typeof e === 'string') {
113
+ console.error('' + e + '');
114
+ }
115
+ else {
116
+ console.error('' + e.message + '');
117
+ }
118
+ }
94
119
  }
95
120
  function createCliApp(opts, wooks) {
96
121
  return new WooksCli(opts, wooks);
package/dist/index.d.ts CHANGED
@@ -48,6 +48,8 @@ declare interface TCliEventData {
48
48
  }
49
49
 
50
50
  export declare interface TWooksCliOptions {
51
+ onError?(e: Error): void;
52
+ onUnknownParams?(pathParams: string[]): void;
51
53
  }
52
54
 
53
55
  export declare function useCliContext<T extends object>(): {
@@ -83,6 +85,9 @@ export declare class WooksCli extends WooksAdapterBase {
83
85
  constructor(opts?: TWooksCliOptions | undefined, wooks?: Wooks | WooksAdapterBase);
84
86
  cli<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathBuilder<ParamsType>;
85
87
  run(_argv?: string[]): Promise<void>;
88
+ onError(e: Error): void;
89
+ onUnknownParams(pathParams: string[]): void;
90
+ error(e: string | Error): void;
86
91
  }
87
92
 
88
93
  export { }
package/dist/index.mjs CHANGED
@@ -36,13 +36,6 @@ function __awaiter(thisArg, _arguments, P, generator) {
36
36
  });
37
37
  }
38
38
 
39
- const banner = () => `[${"@wooksjs/event-cli"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
40
-
41
- /* istanbul ignore file */
42
- function logError(error) {
43
- console.error('' + '' + banner() + error + '');
44
- }
45
-
46
39
  const cliShortcuts = {
47
40
  cli: 'CLI',
48
41
  };
@@ -58,7 +51,8 @@ class WooksCli extends WooksAdapterBase {
58
51
  return __awaiter(this, void 0, void 0, function* () {
59
52
  const argv = process.argv.slice(2) || _argv;
60
53
  const firstFlagIndex = argv.findIndex(a => a.startsWith('-')) + 1;
61
- const path = '/' + (firstFlagIndex ? argv.slice(0, firstFlagIndex - 1) : argv).map(v => encodeURIComponent(v)).join('/');
54
+ const pathParams = (firstFlagIndex ? argv.slice(0, firstFlagIndex - 1) : argv);
55
+ const path = '/' + pathParams.map(v => encodeURIComponent(v)).join('/');
62
56
  const { restoreCtx, clearCtx } = createCliContext({ argv });
63
57
  const handlers = this.wooks.lookup('CLI', path);
64
58
  if (handlers) {
@@ -72,23 +66,54 @@ class WooksCli extends WooksAdapterBase {
72
66
  else if (Array.isArray(response)) {
73
67
  response.forEach(r => console.log(typeof r === 'string' ? r : JSON.stringify(r, null, ' ')));
74
68
  }
75
- else {
76
- console.log(JSON.stringify(response, null, ' '));
69
+ else if (response instanceof Error) {
70
+ console.error('' + response.message + '');
71
+ }
72
+ else if (response) {
73
+ if (response) {
74
+ console.log(JSON.stringify(response, null, ' '));
75
+ }
77
76
  }
78
77
  }
79
78
  }
80
79
  catch (e) {
81
- logError(e.message);
82
- process.exit(1);
80
+ this.onError(e);
83
81
  }
84
82
  clearCtx();
85
83
  }
86
84
  else {
87
- logError('Unknown command parameters');
88
- process.exit(1);
85
+ this.onUnknownParams(pathParams);
89
86
  }
90
87
  });
91
88
  }
89
+ onError(e) {
90
+ var _a;
91
+ if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onError) {
92
+ this.opts.onError(e);
93
+ }
94
+ else {
95
+ this.error(e.message);
96
+ process.exit(1);
97
+ }
98
+ }
99
+ onUnknownParams(pathParams) {
100
+ var _a;
101
+ if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onUnknownParams) {
102
+ this.opts.onUnknownParams(pathParams);
103
+ }
104
+ else {
105
+ this.error('' + 'Unknown command parameters: ' + '' + pathParams.join(' '));
106
+ process.exit(1);
107
+ }
108
+ }
109
+ error(e) {
110
+ if (typeof e === 'string') {
111
+ console.error('' + e + '');
112
+ }
113
+ else {
114
+ console.error('' + e.message + '');
115
+ }
116
+ }
92
117
  }
93
118
  function createCliApp(opts, wooks) {
94
119
  return new WooksCli(opts, wooks);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wooksjs/event-cli",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
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.2.5",
35
- "@wooksjs/event-core": "0.2.5"
34
+ "wooks": "0.2.7",
35
+ "@wooksjs/event-core": "0.2.7"
36
36
  },
37
37
  "dependencies": {
38
38
  "minimist": "^1.2.6"