@wooksjs/event-cli 0.4.10 → 0.4.11

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,7 +7,10 @@ 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
  }
@@ -20,51 +23,19 @@ function useCliContext() {
20
23
  return eventCore.useEventContext('CLI');
21
24
  }
22
25
 
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
26
  const cliShortcuts = {
56
27
  cli: 'CLI',
57
28
  };
58
29
  class WooksCli extends wooks.WooksAdapterBase {
59
30
  constructor(opts, wooks) {
60
- super(wooks, opts === null || opts === void 0 ? void 0 : opts.logger, opts === null || opts === void 0 ? void 0 : opts.router);
31
+ super(wooks, opts?.logger, opts?.router);
61
32
  this.opts = opts;
62
33
  this.alreadyComputedAliases = false;
63
- this.logger = (opts === null || opts === void 0 ? void 0 : opts.logger) || this.getLogger('wooks-cli');
34
+ this.logger = opts?.logger || this.getLogger('wooks-cli');
64
35
  this.cliHelp =
65
- (opts === null || opts === void 0 ? void 0 : opts.cliHelp) instanceof cliHelp.CliHelpRenderer
36
+ opts?.cliHelp instanceof cliHelp.CliHelpRenderer
66
37
  ? opts.cliHelp
67
- : new cliHelp.CliHelpRenderer(opts === null || opts === void 0 ? void 0 : opts.cliHelp);
38
+ : new cliHelp.CliHelpRenderer(opts?.cliHelp);
68
39
  }
69
40
  /**
70
41
  * ### Register CLI Command
@@ -103,7 +74,6 @@ class WooksCli extends wooks.WooksAdapterBase {
103
74
  * @returns
104
75
  */
105
76
  cli(path, _options) {
106
- var _a;
107
77
  const options = typeof _options === 'function' ? { handler: _options } : _options;
108
78
  const handler = typeof _options === 'function' ? _options : _options.handler;
109
79
  const makePath = (s) => '/' + s.replace(/\s+/g, '/');
@@ -124,7 +94,9 @@ class WooksCli extends wooks.WooksAdapterBase {
124
94
  }
125
95
  // register helpCli entry
126
96
  const command = routed.getStaticPart().replace(/\//g, ' ').trim();
127
- const args = Object.assign({}, (options.args || {}));
97
+ const args = {
98
+ ...(options.args || {}),
99
+ };
128
100
  for (const arg of routed.getArgs()) {
129
101
  if (!args[arg]) {
130
102
  args[arg] = '';
@@ -132,7 +104,7 @@ class WooksCli extends wooks.WooksAdapterBase {
132
104
  }
133
105
  this.cliHelp.addEntry({
134
106
  command,
135
- aliases: (_a = options.aliases) === null || _a === void 0 ? void 0 : _a.map(alias => alias.replace(/\\:/g, ':')), // unescape ":" character
107
+ aliases: options.aliases?.map(alias => alias.replace(/\\:/g, ':')), // unescape ":" character
136
108
  args,
137
109
  description: options.description,
138
110
  examples: options.examples,
@@ -172,62 +144,58 @@ class WooksCli extends wooks.WooksAdapterBase {
172
144
  *
173
145
  * @param _argv optionally overwrite `process.argv.slice(2)` with your `argv` array
174
146
  */
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
- }
147
+ async run(_argv, _opts) {
148
+ const argv = _argv || process.argv.slice(2);
149
+ const parsedFlags = minimist(argv, _opts);
150
+ const pathParams = parsedFlags._;
151
+ const path = '/' +
152
+ pathParams.map((v) => encodeURI(v).replace(/\//g, '%2F')).join('/');
153
+ const { restoreCtx, clearCtx, store } = createCliContext({ opts: _opts, argv, pathParams, cliHelp: this.cliHelp, command: path.replace(/\//g, ' ').trim() }, this.mergeEventOptions(this.opts?.eventOptions));
154
+ store('flags').value = parsedFlags;
155
+ this.computeAliases();
156
+ const { handlers: foundHandlers, firstStatic } = this.wooks.lookup('CLI', path);
157
+ if (typeof firstStatic === 'string') {
158
+ // overwriting command with firstStatic to properly search for help
159
+ store('event').set('command', firstStatic.replace(/\//g, ' ').trim());
160
+ }
161
+ const handlers = foundHandlers ||
162
+ (this.opts?.onNotFound && [this.opts.onNotFound]) ||
163
+ null;
164
+ if (handlers) {
165
+ try {
166
+ for (const handler of handlers) {
167
+ restoreCtx();
168
+ const response = await handler();
169
+ if (typeof response === 'string') {
170
+ console.log(response);
171
+ }
172
+ else if (Array.isArray(response)) {
173
+ response.forEach((r) => console.log(typeof r === 'string'
174
+ ? r
175
+ : JSON.stringify(r, null, ' ')));
176
+ }
177
+ else if (response instanceof Error) {
178
+ this.onError(response);
179
+ }
180
+ else if (response) {
181
+ if (response) {
182
+ console.log(JSON.stringify(response, null, ' '));
214
183
  }
215
184
  }
216
185
  }
217
- catch (e) {
218
- this.onError(e);
219
- }
220
- clearCtx();
221
186
  }
222
- else {
223
- this.onUnknownCommand(pathParams);
224
- clearCtx();
187
+ catch (e) {
188
+ this.onError(e);
225
189
  }
226
- });
190
+ clearCtx();
191
+ }
192
+ else {
193
+ this.onUnknownCommand(pathParams);
194
+ clearCtx();
195
+ }
227
196
  }
228
197
  onError(e) {
229
- var _a;
230
- if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onError) {
198
+ if (this.opts?.onError) {
231
199
  this.opts.onError(e);
232
200
  }
233
201
  else {
@@ -240,12 +208,11 @@ class WooksCli extends wooks.WooksAdapterBase {
240
208
  * @param pathParams `string[]` containing command
241
209
  */
242
210
  onUnknownCommand(pathParams) {
243
- var _a;
244
211
  const raiseError = () => {
245
212
  this.error('' + 'Unknown command: ' + pathParams.join(' '));
246
213
  process.exit(1);
247
214
  };
248
- if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onUnknownCommand) {
215
+ if (this.opts?.onUnknownCommand) {
249
216
  this.opts.onUnknownCommand(pathParams, raiseError);
250
217
  }
251
218
  else {
@@ -287,7 +254,7 @@ function createCliApp(opts, wooks) {
287
254
  function useCliHelp() {
288
255
  const event = useCliContext().store('event');
289
256
  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; };
257
+ const getEntry = () => getCliHelp().match(event.get('command'))?.main;
291
258
  return {
292
259
  getCliHelp,
293
260
  getEntry,
@@ -428,9 +395,8 @@ function useCliOptions() {
428
395
  * @returns value of a CLI option
429
396
  */
430
397
  function useCliOption(name) {
431
- var _a;
432
398
  try {
433
- const options = ((_a = useCliHelp().getEntry()) === null || _a === void 0 ? void 0 : _a.options) || [];
399
+ const options = useCliHelp().getEntry()?.options || [];
434
400
  const opt = options.find(o => o.keys.includes(name));
435
401
  if (opt) {
436
402
  for (const key of opt.keys) {
package/dist/index.mjs CHANGED
@@ -5,7 +5,10 @@ import minimist from 'minimist';
5
5
 
6
6
  function createCliContext(data, options) {
7
7
  return createEventContext({
8
- event: Object.assign(Object.assign({}, data), { type: 'CLI' }),
8
+ event: {
9
+ ...data,
10
+ type: 'CLI',
11
+ },
9
12
  options,
10
13
  });
11
14
  }
@@ -18,51 +21,19 @@ function useCliContext() {
18
21
  return useEventContext('CLI');
19
22
  }
20
23
 
21
- /******************************************************************************
22
- Copyright (c) Microsoft Corporation.
23
-
24
- Permission to use, copy, modify, and/or distribute this software for any
25
- purpose with or without fee is hereby granted.
26
-
27
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
28
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
29
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
30
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
31
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
32
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
33
- PERFORMANCE OF THIS SOFTWARE.
34
- ***************************************************************************** */
35
- /* global Reflect, Promise, SuppressedError, Symbol */
36
-
37
-
38
- function __awaiter(thisArg, _arguments, P, generator) {
39
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
40
- return new (P || (P = Promise))(function (resolve, reject) {
41
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
42
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
43
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
44
- step((generator = generator.apply(thisArg, _arguments || [])).next());
45
- });
46
- }
47
-
48
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
49
- var e = new Error(message);
50
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
51
- };
52
-
53
24
  const cliShortcuts = {
54
25
  cli: 'CLI',
55
26
  };
56
27
  class WooksCli extends WooksAdapterBase {
57
28
  constructor(opts, wooks) {
58
- super(wooks, opts === null || opts === void 0 ? void 0 : opts.logger, opts === null || opts === void 0 ? void 0 : opts.router);
29
+ super(wooks, opts?.logger, opts?.router);
59
30
  this.opts = opts;
60
31
  this.alreadyComputedAliases = false;
61
- this.logger = (opts === null || opts === void 0 ? void 0 : opts.logger) || this.getLogger('wooks-cli');
32
+ this.logger = opts?.logger || this.getLogger('wooks-cli');
62
33
  this.cliHelp =
63
- (opts === null || opts === void 0 ? void 0 : opts.cliHelp) instanceof CliHelpRenderer
34
+ opts?.cliHelp instanceof CliHelpRenderer
64
35
  ? opts.cliHelp
65
- : new CliHelpRenderer(opts === null || opts === void 0 ? void 0 : opts.cliHelp);
36
+ : new CliHelpRenderer(opts?.cliHelp);
66
37
  }
67
38
  /**
68
39
  * ### Register CLI Command
@@ -101,7 +72,6 @@ class WooksCli extends WooksAdapterBase {
101
72
  * @returns
102
73
  */
103
74
  cli(path, _options) {
104
- var _a;
105
75
  const options = typeof _options === 'function' ? { handler: _options } : _options;
106
76
  const handler = typeof _options === 'function' ? _options : _options.handler;
107
77
  const makePath = (s) => '/' + s.replace(/\s+/g, '/');
@@ -122,7 +92,9 @@ class WooksCli extends WooksAdapterBase {
122
92
  }
123
93
  // register helpCli entry
124
94
  const command = routed.getStaticPart().replace(/\//g, ' ').trim();
125
- const args = Object.assign({}, (options.args || {}));
95
+ const args = {
96
+ ...(options.args || {}),
97
+ };
126
98
  for (const arg of routed.getArgs()) {
127
99
  if (!args[arg]) {
128
100
  args[arg] = '';
@@ -130,7 +102,7 @@ class WooksCli extends WooksAdapterBase {
130
102
  }
131
103
  this.cliHelp.addEntry({
132
104
  command,
133
- aliases: (_a = options.aliases) === null || _a === void 0 ? void 0 : _a.map(alias => alias.replace(/\\:/g, ':')), // unescape ":" character
105
+ aliases: options.aliases?.map(alias => alias.replace(/\\:/g, ':')), // unescape ":" character
134
106
  args,
135
107
  description: options.description,
136
108
  examples: options.examples,
@@ -170,62 +142,58 @@ class WooksCli extends WooksAdapterBase {
170
142
  *
171
143
  * @param _argv optionally overwrite `process.argv.slice(2)` with your `argv` array
172
144
  */
173
- run(_argv, _opts) {
174
- var _a, _b;
175
- return __awaiter(this, void 0, void 0, function* () {
176
- const argv = _argv || process.argv.slice(2);
177
- const parsedFlags = minimist(argv, _opts);
178
- const pathParams = parsedFlags._;
179
- const path = '/' +
180
- pathParams.map((v) => encodeURI(v).replace(/\//g, '%2F')).join('/');
181
- 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));
182
- store('flags').value = parsedFlags;
183
- this.computeAliases();
184
- const { handlers: foundHandlers, firstStatic } = this.wooks.lookup('CLI', path);
185
- if (typeof firstStatic === 'string') {
186
- // overwriting command with firstStatic to properly search for help
187
- store('event').set('command', firstStatic.replace(/\//g, ' ').trim());
188
- }
189
- const handlers = foundHandlers ||
190
- (((_b = this.opts) === null || _b === void 0 ? void 0 : _b.onNotFound) && [this.opts.onNotFound]) ||
191
- null;
192
- if (handlers) {
193
- try {
194
- for (const handler of handlers) {
195
- restoreCtx();
196
- const response = yield handler();
197
- if (typeof response === 'string') {
198
- console.log(response);
199
- }
200
- else if (Array.isArray(response)) {
201
- response.forEach((r) => console.log(typeof r === 'string'
202
- ? r
203
- : JSON.stringify(r, null, ' ')));
204
- }
205
- else if (response instanceof Error) {
206
- this.onError(response);
207
- }
208
- else if (response) {
209
- if (response) {
210
- console.log(JSON.stringify(response, null, ' '));
211
- }
145
+ async run(_argv, _opts) {
146
+ const argv = _argv || process.argv.slice(2);
147
+ const parsedFlags = minimist(argv, _opts);
148
+ const pathParams = parsedFlags._;
149
+ const path = '/' +
150
+ pathParams.map((v) => encodeURI(v).replace(/\//g, '%2F')).join('/');
151
+ const { restoreCtx, clearCtx, store } = createCliContext({ opts: _opts, argv, pathParams, cliHelp: this.cliHelp, command: path.replace(/\//g, ' ').trim() }, this.mergeEventOptions(this.opts?.eventOptions));
152
+ store('flags').value = parsedFlags;
153
+ this.computeAliases();
154
+ const { handlers: foundHandlers, firstStatic } = this.wooks.lookup('CLI', path);
155
+ if (typeof firstStatic === 'string') {
156
+ // overwriting command with firstStatic to properly search for help
157
+ store('event').set('command', firstStatic.replace(/\//g, ' ').trim());
158
+ }
159
+ const handlers = foundHandlers ||
160
+ (this.opts?.onNotFound && [this.opts.onNotFound]) ||
161
+ null;
162
+ if (handlers) {
163
+ try {
164
+ for (const handler of handlers) {
165
+ restoreCtx();
166
+ const response = await handler();
167
+ if (typeof response === 'string') {
168
+ console.log(response);
169
+ }
170
+ else if (Array.isArray(response)) {
171
+ response.forEach((r) => console.log(typeof r === 'string'
172
+ ? r
173
+ : JSON.stringify(r, null, ' ')));
174
+ }
175
+ else if (response instanceof Error) {
176
+ this.onError(response);
177
+ }
178
+ else if (response) {
179
+ if (response) {
180
+ console.log(JSON.stringify(response, null, ' '));
212
181
  }
213
182
  }
214
183
  }
215
- catch (e) {
216
- this.onError(e);
217
- }
218
- clearCtx();
219
184
  }
220
- else {
221
- this.onUnknownCommand(pathParams);
222
- clearCtx();
185
+ catch (e) {
186
+ this.onError(e);
223
187
  }
224
- });
188
+ clearCtx();
189
+ }
190
+ else {
191
+ this.onUnknownCommand(pathParams);
192
+ clearCtx();
193
+ }
225
194
  }
226
195
  onError(e) {
227
- var _a;
228
- if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onError) {
196
+ if (this.opts?.onError) {
229
197
  this.opts.onError(e);
230
198
  }
231
199
  else {
@@ -238,12 +206,11 @@ class WooksCli extends WooksAdapterBase {
238
206
  * @param pathParams `string[]` containing command
239
207
  */
240
208
  onUnknownCommand(pathParams) {
241
- var _a;
242
209
  const raiseError = () => {
243
210
  this.error('' + 'Unknown command: ' + pathParams.join(' '));
244
211
  process.exit(1);
245
212
  };
246
- if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onUnknownCommand) {
213
+ if (this.opts?.onUnknownCommand) {
247
214
  this.opts.onUnknownCommand(pathParams, raiseError);
248
215
  }
249
216
  else {
@@ -285,7 +252,7 @@ function createCliApp(opts, wooks) {
285
252
  function useCliHelp() {
286
253
  const event = useCliContext().store('event');
287
254
  const getCliHelp = () => event.get('cliHelp');
288
- const getEntry = () => { var _a; return (_a = getCliHelp().match(event.get('command'))) === null || _a === void 0 ? void 0 : _a.main; };
255
+ const getEntry = () => getCliHelp().match(event.get('command'))?.main;
289
256
  return {
290
257
  getCliHelp,
291
258
  getEntry,
@@ -426,9 +393,8 @@ function useCliOptions() {
426
393
  * @returns value of a CLI option
427
394
  */
428
395
  function useCliOption(name) {
429
- var _a;
430
396
  try {
431
- const options = ((_a = useCliHelp().getEntry()) === null || _a === void 0 ? void 0 : _a.options) || [];
397
+ const options = useCliHelp().getEntry()?.options || [];
432
398
  const opt = options.find(o => o.keys.includes(name));
433
399
  if (opt) {
434
400
  for (const key of opt.keys) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wooksjs/event-cli",
3
- "version": "0.4.10",
3
+ "version": "0.4.11",
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.4.10",
35
- "@wooksjs/event-core": "0.4.10"
34
+ "wooks": "0.4.11",
35
+ "@wooksjs/event-core": "0.4.11"
36
36
  },
37
37
  "dependencies": {
38
38
  "minimist": "^1.2.6",