@moostjs/event-cli 0.2.29 → 0.2.31
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 +32 -142
- package/dist/index.d.ts +6 -51
- package/dist/index.mjs +34 -142
- package/package.json +5 -6
package/dist/index.cjs
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
var moost = require('moost');
|
|
4
4
|
var eventCli = require('@wooksjs/event-cli');
|
|
5
|
-
var cliHelp = require('@prostojs/cli-help');
|
|
6
5
|
|
|
7
6
|
/******************************************************************************
|
|
8
7
|
Copyright (c) Microsoft Corporation.
|
|
@@ -35,34 +34,6 @@ function getCliMate() {
|
|
|
35
34
|
return moost.getMoostMate();
|
|
36
35
|
}
|
|
37
36
|
|
|
38
|
-
/**
|
|
39
|
-
* ### setCliHelpForEvent
|
|
40
|
-
* Used internally to set CliHelpRenderer instance for an event state
|
|
41
|
-
* @param cliHelp CliHelpRenderer
|
|
42
|
-
*/
|
|
43
|
-
function setCliHelpForEvent(cliHelp) {
|
|
44
|
-
eventCli.useCliContext().store('event').set('cliHelp', cliHelp);
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* ## useCliHelp
|
|
48
|
-
* ### Composable
|
|
49
|
-
* ```js
|
|
50
|
-
* // example of printing cli instructions
|
|
51
|
-
* const { print } = useCliHelp()
|
|
52
|
-
* print(true)
|
|
53
|
-
* ```
|
|
54
|
-
* @returns
|
|
55
|
-
*/
|
|
56
|
-
function useCliHelp() {
|
|
57
|
-
const event = eventCli.useCliContext().store('event');
|
|
58
|
-
const getCliHelp = () => event.get('cliHelp');
|
|
59
|
-
return {
|
|
60
|
-
getCliHelp,
|
|
61
|
-
render: (width, withColors) => getCliHelp().render(event.get('pathParams').join(' '), width, withColors),
|
|
62
|
-
print: (withColors) => getCliHelp().print(event.get('pathParams').join(' '), withColors),
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
|
|
66
37
|
const LOGGER_TITLE = 'moost-cli';
|
|
67
38
|
const CONTEXT_TYPE = 'CLI';
|
|
68
39
|
/**
|
|
@@ -110,16 +81,6 @@ class MoostCli {
|
|
|
110
81
|
onNotFound: this.onNotFound.bind(this),
|
|
111
82
|
});
|
|
112
83
|
}
|
|
113
|
-
const cliHelpOpts = opts === null || opts === void 0 ? void 0 : opts.cliHelp;
|
|
114
|
-
if (cliHelpOpts && cliHelpOpts instanceof cliHelp.CliHelpRenderer) {
|
|
115
|
-
this.cliHelp = cliHelpOpts;
|
|
116
|
-
}
|
|
117
|
-
else if (cliHelpOpts) {
|
|
118
|
-
this.cliHelp = new cliHelp.CliHelpRenderer(cliHelpOpts);
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
this.cliHelp = new cliHelp.CliHelpRenderer();
|
|
122
|
-
}
|
|
123
84
|
if (!(opts === null || opts === void 0 ? void 0 : opts.debug)) {
|
|
124
85
|
moost.getMoostInfact().silent(true);
|
|
125
86
|
}
|
|
@@ -134,9 +95,6 @@ class MoostCli {
|
|
|
134
95
|
getControllerInstance: () => this.moost,
|
|
135
96
|
callControllerMethod: () => undefined,
|
|
136
97
|
logErrors: (_a = this.opts) === null || _a === void 0 ? void 0 : _a.debug,
|
|
137
|
-
hooks: {
|
|
138
|
-
init: () => setCliHelpForEvent(this.cliHelp),
|
|
139
|
-
},
|
|
140
98
|
})();
|
|
141
99
|
if (typeof response === 'undefined') {
|
|
142
100
|
this.cliApp.onUnknownCommand(pathParams);
|
|
@@ -145,22 +103,11 @@ class MoostCli {
|
|
|
145
103
|
});
|
|
146
104
|
}
|
|
147
105
|
onInit(moost) {
|
|
148
|
-
var _a;
|
|
149
106
|
this.moost = moost;
|
|
150
|
-
for (const [alias, entry] of Object.entries(this.cliHelp.getComputedAliases())) {
|
|
151
|
-
if (entry.custom) {
|
|
152
|
-
const vars = Object.keys(entry.args || {}).map(k => ':' + k).join('/');
|
|
153
|
-
const path = '/' + alias.replace(/\s+/g, '/').replace(/:/g, '\\:') + (vars ? '/' + vars : '');
|
|
154
|
-
this.cliApp.cli(path, entry.custom.fn);
|
|
155
|
-
if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.debug) {
|
|
156
|
-
entry.custom.log(`${'[36m'}(CLI-alias*)${'[32m'}${path}`);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
107
|
void this.cliApp.run();
|
|
161
108
|
}
|
|
162
109
|
bindHandler(opts) {
|
|
163
|
-
var _a, _b, _c, _d, _e
|
|
110
|
+
var _a, _b, _c, _d, _e;
|
|
164
111
|
let fn;
|
|
165
112
|
for (const handler of opts.handlers) {
|
|
166
113
|
if (handler.type !== 'CLI')
|
|
@@ -170,11 +117,12 @@ class MoostCli {
|
|
|
170
117
|
: typeof opts.method === 'string'
|
|
171
118
|
? opts.method
|
|
172
119
|
: '';
|
|
173
|
-
const
|
|
120
|
+
const prefix = opts.prefix.replace(/\s+/g, '/') || '';
|
|
121
|
+
const makePath = (p) => `${prefix}/${p}`
|
|
174
122
|
.replace(/\/\/+/g, '/')
|
|
175
123
|
// avoid interpreting "cmd:tail" as "cmd/:tail"
|
|
176
|
-
.replace(/\/\\:/g, '\\:')
|
|
177
|
-
|
|
124
|
+
.replace(/\/\\:/g, '\\:')
|
|
125
|
+
.replace(/^\/+/g, '');
|
|
178
126
|
if (!fn) {
|
|
179
127
|
fn = moost.defineMoostEventHandler({
|
|
180
128
|
contextType: CONTEXT_TYPE,
|
|
@@ -184,58 +132,48 @@ class MoostCli {
|
|
|
184
132
|
controllerMethod: opts.method,
|
|
185
133
|
resolveArgs: opts.resolveArgs,
|
|
186
134
|
logErrors: (_a = this.opts) === null || _a === void 0 ? void 0 : _a.debug,
|
|
187
|
-
hooks: {
|
|
188
|
-
init: () => setCliHelpForEvent(this.cliHelp),
|
|
189
|
-
},
|
|
190
135
|
});
|
|
191
136
|
}
|
|
192
137
|
const targetPath = makePath(path);
|
|
193
|
-
const { getArgs, getStaticPart } = this.cliApp.cli(targetPath, fn);
|
|
194
138
|
const meta = getCliMate().read(opts.fakeInstance, opts.method);
|
|
195
139
|
const classMeta = getCliMate().read(opts.fakeInstance);
|
|
196
|
-
const args = {};
|
|
197
|
-
getArgs().forEach(a => {
|
|
198
|
-
var _a;
|
|
199
|
-
const argParam = (_a = meta === null || meta === void 0 ? void 0 : meta.params) === null || _a === void 0 ? void 0 : _a.find(p => p.label === a && p.description);
|
|
200
|
-
args[a] = (argParam === null || argParam === void 0 ? void 0 : argParam.description) || '';
|
|
201
|
-
});
|
|
202
|
-
cliCommand = getStaticPart().replace(/\//g, ' ').trim();
|
|
203
140
|
const cliOptions = new Map();
|
|
204
141
|
[
|
|
205
142
|
...(((_c = (_b = this.opts) === null || _b === void 0 ? void 0 : _b.globalCliOptions) === null || _c === void 0 ? void 0 : _c.length) ? this.opts.globalCliOptions : []),
|
|
206
143
|
...((classMeta === null || classMeta === void 0 ? void 0 : classMeta.cliOptions) || []),
|
|
207
|
-
...(((_d = meta === null || meta === void 0 ? void 0 : meta.params) === null || _d === void 0 ? void 0 : _d.filter(param => !!param.
|
|
208
|
-
keys: param.
|
|
144
|
+
...(((_d = meta === null || meta === void 0 ? void 0 : meta.params) === null || _d === void 0 ? void 0 : _d.filter(param => !!param.cliOptionsKeys && param.cliOptionsKeys.length > 0).map(param => ({
|
|
145
|
+
keys: param.cliOptionsKeys,
|
|
209
146
|
value: typeof param.value === 'string' ? param.value : '',
|
|
210
147
|
description: param.description || '',
|
|
211
148
|
}))) || []),
|
|
212
149
|
].forEach(o => cliOptions.set(o.keys[0], o));
|
|
213
|
-
if ((_e = this.opts) === null || _e === void 0 ? void 0 : _e.debug) {
|
|
214
|
-
opts.logHandler(`${'[36m'}(CLI)${'[32m'}${targetPath}`);
|
|
215
|
-
}
|
|
216
150
|
const aliases = [];
|
|
217
151
|
if (meta === null || meta === void 0 ? void 0 : meta.cliAliases) {
|
|
218
152
|
for (const alias of meta.cliAliases) {
|
|
219
153
|
const targetPath = makePath(alias);
|
|
220
|
-
|
|
221
|
-
aliases.push(getStaticPart().replace(/\//g, ' ').trim());
|
|
222
|
-
if ((_f = this.opts) === null || _f === void 0 ? void 0 : _f.debug) {
|
|
223
|
-
opts.logHandler(`${'[36m'}(CLI-alias)${'[32m'}${targetPath}`);
|
|
224
|
-
}
|
|
154
|
+
aliases.push(targetPath);
|
|
225
155
|
}
|
|
226
156
|
}
|
|
227
|
-
|
|
157
|
+
const args = {};
|
|
158
|
+
(_e = meta === null || meta === void 0 ? void 0 : meta.params) === null || _e === void 0 ? void 0 : _e.filter(p => p.isRouteParam && p.description).forEach(p => args[p.isRouteParam] = p.description);
|
|
159
|
+
this.cliApp.cli(targetPath, {
|
|
228
160
|
description: (meta === null || meta === void 0 ? void 0 : meta.description) || '',
|
|
229
|
-
command: cliCommand,
|
|
230
161
|
options: Array.from(cliOptions.values()),
|
|
231
162
|
args,
|
|
232
|
-
aliases
|
|
233
|
-
custom: { fn, log: opts.logHandler },
|
|
163
|
+
aliases,
|
|
234
164
|
examples: (meta === null || meta === void 0 ? void 0 : meta.cliExamples) || [],
|
|
165
|
+
handler: fn,
|
|
166
|
+
onRegister: (path, aliasType) => {
|
|
167
|
+
var _a;
|
|
168
|
+
if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.debug) {
|
|
169
|
+
opts.logHandler(`${'[36m'}(${aliasTypes[aliasType]})${'[32m'}${path}`);
|
|
170
|
+
}
|
|
171
|
+
},
|
|
235
172
|
});
|
|
236
173
|
}
|
|
237
174
|
}
|
|
238
|
-
}
|
|
175
|
+
}
|
|
176
|
+
const aliasTypes = ['CLI', 'CLI-alias', 'CLI-alias*', 'CLI-alias*'];
|
|
239
177
|
|
|
240
178
|
function formatParams(keys) {
|
|
241
179
|
const names = [keys].flat();
|
|
@@ -250,7 +188,7 @@ function formatParams(keys) {
|
|
|
250
188
|
* ```ts
|
|
251
189
|
* │ @Cli('command')
|
|
252
190
|
* │ command(
|
|
253
|
-
* │ @Description('Test
|
|
191
|
+
* │ @Description('Test option...')
|
|
254
192
|
* │ @CliOption('test', 't')
|
|
255
193
|
* │ test: boolean,
|
|
256
194
|
* │ ) {
|
|
@@ -263,23 +201,7 @@ function formatParams(keys) {
|
|
|
263
201
|
*/
|
|
264
202
|
function CliOption(...keys) {
|
|
265
203
|
const mate = getCliMate();
|
|
266
|
-
return mate.apply(mate.decorate('
|
|
267
|
-
const flags = eventCli.useFlags();
|
|
268
|
-
const names = keys;
|
|
269
|
-
const vals = [];
|
|
270
|
-
for (const name of names) {
|
|
271
|
-
if (flags[name]) {
|
|
272
|
-
vals.push(flags[name]);
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
if (vals.length > 1) {
|
|
276
|
-
throw new Error(`Options ${formatParams(names).join(' and ')} are synonyms. Please use only one of them.`);
|
|
277
|
-
}
|
|
278
|
-
if (vals.length === 0) {
|
|
279
|
-
return false;
|
|
280
|
-
}
|
|
281
|
-
return vals[0];
|
|
282
|
-
}, formatParams(keys).join(', ')));
|
|
204
|
+
return mate.apply(mate.decorate('cliOptionsKeys', keys, false), moost.Resolve(() => eventCli.useCliOption(keys[0]), formatParams(keys).join(', ')));
|
|
283
205
|
}
|
|
284
206
|
/**
|
|
285
207
|
* ## Define Global CLI Option
|
|
@@ -302,6 +224,7 @@ function CliGlobalOption(option) {
|
|
|
302
224
|
* For example the folowing path are interpreted the same:
|
|
303
225
|
* - "command test use:dev :name"
|
|
304
226
|
* - "command/test/use:dev/:name"
|
|
227
|
+
*
|
|
305
228
|
* Where name will become an argument
|
|
306
229
|
*
|
|
307
230
|
* @param path - command path
|
|
@@ -349,49 +272,18 @@ function CliExample(cmd, description) {
|
|
|
349
272
|
*/
|
|
350
273
|
const cliHelpInterceptor = (opts) => {
|
|
351
274
|
return moost.defineInterceptorFn(() => {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
try {
|
|
356
|
-
useCliHelp().print(opts === null || opts === void 0 ? void 0 : opts.colors);
|
|
357
|
-
return '';
|
|
358
|
-
}
|
|
359
|
-
catch (e) {
|
|
360
|
-
//
|
|
361
|
-
}
|
|
275
|
+
try {
|
|
276
|
+
if (eventCli.useAutoHelp(opts === null || opts === void 0 ? void 0 : opts.helpOptions, opts === null || opts === void 0 ? void 0 : opts.colors)) {
|
|
277
|
+
return '';
|
|
362
278
|
}
|
|
363
279
|
}
|
|
364
|
-
|
|
280
|
+
catch (e) {
|
|
281
|
+
//
|
|
282
|
+
}
|
|
283
|
+
if (opts === null || opts === void 0 ? void 0 : opts.lookupLevel) {
|
|
365
284
|
const { getMethod } = moost.useControllerContext();
|
|
366
285
|
if (!getMethod()) {
|
|
367
|
-
|
|
368
|
-
const cliHelp = useCliHelp().getCliHelp();
|
|
369
|
-
const cmd = cliHelp.getCliName();
|
|
370
|
-
let data;
|
|
371
|
-
for (let i = 0; i < Math.min(parts.length, 4); i++) {
|
|
372
|
-
const pathParams = parts.slice(0, i ? -i : parts.length).join(' ');
|
|
373
|
-
try {
|
|
374
|
-
data = cliHelp.match(pathParams);
|
|
375
|
-
break;
|
|
376
|
-
}
|
|
377
|
-
catch (e) {
|
|
378
|
-
if (opts === null || opts === void 0 ? void 0 : opts.helpWithIncompleteCmd) {
|
|
379
|
-
const variants = cliHelp.lookup(pathParams);
|
|
380
|
-
if (variants.length) {
|
|
381
|
-
throw new Error(`Wrong command, did you mean:\n${variants.slice(0, 7).map(c => ` $ ${cmd} ${c.main.command}`).join('\n')}`);
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
if (data) {
|
|
387
|
-
const { main, children } = data;
|
|
388
|
-
if ((opts === null || opts === void 0 ? void 0 : opts.helpWithArgs) && main.args && Object.keys(main.args).length) {
|
|
389
|
-
throw new Error(`Arguments expected: ${Object.keys(main.args).map(l => `<${l}>`).join(', ')}`);
|
|
390
|
-
}
|
|
391
|
-
else if ((opts === null || opts === void 0 ? void 0 : opts.helpWithIncompleteCmd) && children && children.length) {
|
|
392
|
-
throw new Error(`Wrong command, did you mean:\n${children.slice(0, 7).map(c => ` $ ${cmd} ${c.command}`).join('\n')}`);
|
|
393
|
-
}
|
|
394
|
-
}
|
|
286
|
+
eventCli.useCommandLookupHelp(opts.lookupLevel);
|
|
395
287
|
}
|
|
396
288
|
}
|
|
397
289
|
}, moost.TInterceptorPriority.BEFORE_ALL);
|
|
@@ -427,5 +319,3 @@ exports.CliHelpInterceptor = CliHelpInterceptor;
|
|
|
427
319
|
exports.CliOption = CliOption;
|
|
428
320
|
exports.MoostCli = MoostCli;
|
|
429
321
|
exports.cliHelpInterceptor = cliHelpInterceptor;
|
|
430
|
-
exports.setCliHelpForEvent = setCliHelpForEvent;
|
|
431
|
-
exports.useCliHelp = useCliHelp;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import { CliHelpRenderer } from '@prostojs/cli-help';
|
|
2
1
|
import { Moost } from 'moost';
|
|
3
|
-
import { TCliHelpOptions } from '@prostojs/cli-help';
|
|
4
2
|
import { TInterceptorFn } from 'moost';
|
|
5
3
|
import { TMoostAdapter } from 'moost';
|
|
6
4
|
import { TMoostAdapterOptions } from 'moost';
|
|
7
5
|
import { TWooksCliOptions } from '@wooksjs/event-cli';
|
|
8
|
-
import { TWooksHandler } from 'wooks';
|
|
9
6
|
import { WooksCli } from '@wooksjs/event-cli';
|
|
10
7
|
|
|
11
8
|
/**
|
|
@@ -17,6 +14,7 @@ import { WooksCli } from '@wooksjs/event-cli';
|
|
|
17
14
|
* For example the folowing path are interpreted the same:
|
|
18
15
|
* - "command test use:dev :name"
|
|
19
16
|
* - "command/test/use:dev/:name"
|
|
17
|
+
*
|
|
20
18
|
* Where name will become an argument
|
|
21
19
|
*
|
|
22
20
|
* @param path - command path
|
|
@@ -93,14 +91,9 @@ export declare const CliHelpInterceptor: (opts?: {
|
|
|
93
91
|
*/
|
|
94
92
|
colors?: boolean | undefined;
|
|
95
93
|
/**
|
|
96
|
-
* Enable
|
|
97
|
-
*/
|
|
98
|
-
helpWithArgs?: boolean | undefined;
|
|
99
|
-
/**
|
|
100
|
-
* Enable help message when command is incomplete
|
|
101
|
-
* and it is possible to suggest related commands
|
|
94
|
+
* Enable lookup for a command
|
|
102
95
|
*/
|
|
103
|
-
|
|
96
|
+
lookupLevel?: number | undefined;
|
|
104
97
|
} | undefined) => ClassDecorator & MethodDecorator;
|
|
105
98
|
|
|
106
99
|
/**
|
|
@@ -128,21 +121,11 @@ export declare const cliHelpInterceptor: (opts?: {
|
|
|
128
121
|
*/
|
|
129
122
|
colors?: boolean;
|
|
130
123
|
/**
|
|
131
|
-
* Enable
|
|
132
|
-
*/
|
|
133
|
-
helpWithArgs?: boolean;
|
|
134
|
-
/**
|
|
135
|
-
* Enable help message when command is incomplete
|
|
136
|
-
* and it is possible to suggest related commands
|
|
124
|
+
* Enable lookup for a command
|
|
137
125
|
*/
|
|
138
|
-
|
|
126
|
+
lookupLevel?: number;
|
|
139
127
|
}) => TInterceptorFn;
|
|
140
128
|
|
|
141
|
-
declare type CliHelpRendererWithFn = CliHelpRenderer<{
|
|
142
|
-
fn: TWooksHandler;
|
|
143
|
-
log: ((eventName: string) => void);
|
|
144
|
-
}>;
|
|
145
|
-
|
|
146
129
|
/**
|
|
147
130
|
* ## Define CLI Option
|
|
148
131
|
* ### @ParameterDecorator
|
|
@@ -151,7 +134,7 @@ declare type CliHelpRendererWithFn = CliHelpRenderer<{
|
|
|
151
134
|
* ```ts
|
|
152
135
|
* │ @Cli('command')
|
|
153
136
|
* │ command(
|
|
154
|
-
* │ @Description('Test
|
|
137
|
+
* │ @Description('Test option...')
|
|
155
138
|
* │ @CliOption('test', 't')
|
|
156
139
|
* │ test: boolean,
|
|
157
140
|
* │ ) {
|
|
@@ -197,7 +180,6 @@ export declare function CliOption(...keys: string[]): ParameterDecorator;
|
|
|
197
180
|
export declare class MoostCli implements TMoostAdapter<TCliHandlerMeta> {
|
|
198
181
|
protected opts?: TMoostCliOpts | undefined;
|
|
199
182
|
protected cliApp: WooksCli;
|
|
200
|
-
protected cliHelp: CliHelpRendererWithFn;
|
|
201
183
|
constructor(opts?: TMoostCliOpts | undefined);
|
|
202
184
|
onNotFound(): Promise<unknown>;
|
|
203
185
|
protected moost?: Moost;
|
|
@@ -205,13 +187,6 @@ export declare class MoostCli implements TMoostAdapter<TCliHandlerMeta> {
|
|
|
205
187
|
bindHandler<T extends object = object>(opts: TMoostAdapterOptions<TCliHandlerMeta, T>): void | Promise<void>;
|
|
206
188
|
}
|
|
207
189
|
|
|
208
|
-
/**
|
|
209
|
-
* ### setCliHelpForEvent
|
|
210
|
-
* Used internally to set CliHelpRenderer instance for an event state
|
|
211
|
-
* @param cliHelp CliHelpRenderer
|
|
212
|
-
*/
|
|
213
|
-
export declare function setCliHelpForEvent(cliHelp: CliHelpRendererWithFn): void;
|
|
214
|
-
|
|
215
190
|
export declare interface TCliHandlerMeta {
|
|
216
191
|
path: string;
|
|
217
192
|
}
|
|
@@ -221,10 +196,6 @@ export declare interface TMoostCliOpts {
|
|
|
221
196
|
* WooksCli options or instance
|
|
222
197
|
*/
|
|
223
198
|
wooksCli?: WooksCli | TWooksCliOptions;
|
|
224
|
-
/**
|
|
225
|
-
* CliHelpRenderer options or instance
|
|
226
|
-
*/
|
|
227
|
-
cliHelp?: CliHelpRendererWithFn | TCliHelpOptions;
|
|
228
199
|
/**
|
|
229
200
|
* more internal logs are printed when true
|
|
230
201
|
*/
|
|
@@ -238,20 +209,4 @@ export declare interface TMoostCliOpts {
|
|
|
238
209
|
}[];
|
|
239
210
|
}
|
|
240
211
|
|
|
241
|
-
/**
|
|
242
|
-
* ## useCliHelp
|
|
243
|
-
* ### Composable
|
|
244
|
-
* ```js
|
|
245
|
-
* // example of printing cli instructions
|
|
246
|
-
* const { print } = useCliHelp()
|
|
247
|
-
* print(true)
|
|
248
|
-
* ```
|
|
249
|
-
* @returns
|
|
250
|
-
*/
|
|
251
|
-
export declare function useCliHelp(): {
|
|
252
|
-
getCliHelp: () => CliHelpRendererWithFn;
|
|
253
|
-
render: (width?: number, withColors?: boolean) => string[];
|
|
254
|
-
print: (withColors?: boolean) => void;
|
|
255
|
-
};
|
|
256
|
-
|
|
257
212
|
export { }
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { getMoostMate, getMoostInfact, defineMoostEventHandler, Resolve, defineInterceptorFn, useControllerContext, TInterceptorPriority, Intercept } from 'moost';
|
|
2
|
-
import {
|
|
3
|
-
import { CliHelpRenderer } from '@prostojs/cli-help';
|
|
2
|
+
import { WooksCli, createCliApp, useCliContext, useCliOption, useAutoHelp, useCommandLookupHelp } from '@wooksjs/event-cli';
|
|
4
3
|
|
|
5
4
|
/******************************************************************************
|
|
6
5
|
Copyright (c) Microsoft Corporation.
|
|
@@ -33,34 +32,6 @@ function getCliMate() {
|
|
|
33
32
|
return getMoostMate();
|
|
34
33
|
}
|
|
35
34
|
|
|
36
|
-
/**
|
|
37
|
-
* ### setCliHelpForEvent
|
|
38
|
-
* Used internally to set CliHelpRenderer instance for an event state
|
|
39
|
-
* @param cliHelp CliHelpRenderer
|
|
40
|
-
*/
|
|
41
|
-
function setCliHelpForEvent(cliHelp) {
|
|
42
|
-
useCliContext().store('event').set('cliHelp', cliHelp);
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* ## useCliHelp
|
|
46
|
-
* ### Composable
|
|
47
|
-
* ```js
|
|
48
|
-
* // example of printing cli instructions
|
|
49
|
-
* const { print } = useCliHelp()
|
|
50
|
-
* print(true)
|
|
51
|
-
* ```
|
|
52
|
-
* @returns
|
|
53
|
-
*/
|
|
54
|
-
function useCliHelp() {
|
|
55
|
-
const event = useCliContext().store('event');
|
|
56
|
-
const getCliHelp = () => event.get('cliHelp');
|
|
57
|
-
return {
|
|
58
|
-
getCliHelp,
|
|
59
|
-
render: (width, withColors) => getCliHelp().render(event.get('pathParams').join(' '), width, withColors),
|
|
60
|
-
print: (withColors) => getCliHelp().print(event.get('pathParams').join(' '), withColors),
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
|
|
64
35
|
const LOGGER_TITLE = 'moost-cli';
|
|
65
36
|
const CONTEXT_TYPE = 'CLI';
|
|
66
37
|
/**
|
|
@@ -108,16 +79,6 @@ class MoostCli {
|
|
|
108
79
|
onNotFound: this.onNotFound.bind(this),
|
|
109
80
|
});
|
|
110
81
|
}
|
|
111
|
-
const cliHelpOpts = opts === null || opts === void 0 ? void 0 : opts.cliHelp;
|
|
112
|
-
if (cliHelpOpts && cliHelpOpts instanceof CliHelpRenderer) {
|
|
113
|
-
this.cliHelp = cliHelpOpts;
|
|
114
|
-
}
|
|
115
|
-
else if (cliHelpOpts) {
|
|
116
|
-
this.cliHelp = new CliHelpRenderer(cliHelpOpts);
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
119
|
-
this.cliHelp = new CliHelpRenderer();
|
|
120
|
-
}
|
|
121
82
|
if (!(opts === null || opts === void 0 ? void 0 : opts.debug)) {
|
|
122
83
|
getMoostInfact().silent(true);
|
|
123
84
|
}
|
|
@@ -132,9 +93,6 @@ class MoostCli {
|
|
|
132
93
|
getControllerInstance: () => this.moost,
|
|
133
94
|
callControllerMethod: () => undefined,
|
|
134
95
|
logErrors: (_a = this.opts) === null || _a === void 0 ? void 0 : _a.debug,
|
|
135
|
-
hooks: {
|
|
136
|
-
init: () => setCliHelpForEvent(this.cliHelp),
|
|
137
|
-
},
|
|
138
96
|
})();
|
|
139
97
|
if (typeof response === 'undefined') {
|
|
140
98
|
this.cliApp.onUnknownCommand(pathParams);
|
|
@@ -143,22 +101,11 @@ class MoostCli {
|
|
|
143
101
|
});
|
|
144
102
|
}
|
|
145
103
|
onInit(moost) {
|
|
146
|
-
var _a;
|
|
147
104
|
this.moost = moost;
|
|
148
|
-
for (const [alias, entry] of Object.entries(this.cliHelp.getComputedAliases())) {
|
|
149
|
-
if (entry.custom) {
|
|
150
|
-
const vars = Object.keys(entry.args || {}).map(k => ':' + k).join('/');
|
|
151
|
-
const path = '/' + alias.replace(/\s+/g, '/').replace(/:/g, '\\:') + (vars ? '/' + vars : '');
|
|
152
|
-
this.cliApp.cli(path, entry.custom.fn);
|
|
153
|
-
if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.debug) {
|
|
154
|
-
entry.custom.log(`${'[36m'}(CLI-alias*)${'[32m'}${path}`);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
105
|
void this.cliApp.run();
|
|
159
106
|
}
|
|
160
107
|
bindHandler(opts) {
|
|
161
|
-
var _a, _b, _c, _d, _e
|
|
108
|
+
var _a, _b, _c, _d, _e;
|
|
162
109
|
let fn;
|
|
163
110
|
for (const handler of opts.handlers) {
|
|
164
111
|
if (handler.type !== 'CLI')
|
|
@@ -168,11 +115,12 @@ class MoostCli {
|
|
|
168
115
|
: typeof opts.method === 'string'
|
|
169
116
|
? opts.method
|
|
170
117
|
: '';
|
|
171
|
-
const
|
|
118
|
+
const prefix = opts.prefix.replace(/\s+/g, '/') || '';
|
|
119
|
+
const makePath = (p) => `${prefix}/${p}`
|
|
172
120
|
.replace(/\/\/+/g, '/')
|
|
173
121
|
// avoid interpreting "cmd:tail" as "cmd/:tail"
|
|
174
|
-
.replace(/\/\\:/g, '\\:')
|
|
175
|
-
|
|
122
|
+
.replace(/\/\\:/g, '\\:')
|
|
123
|
+
.replace(/^\/+/g, '');
|
|
176
124
|
if (!fn) {
|
|
177
125
|
fn = defineMoostEventHandler({
|
|
178
126
|
contextType: CONTEXT_TYPE,
|
|
@@ -182,58 +130,48 @@ class MoostCli {
|
|
|
182
130
|
controllerMethod: opts.method,
|
|
183
131
|
resolveArgs: opts.resolveArgs,
|
|
184
132
|
logErrors: (_a = this.opts) === null || _a === void 0 ? void 0 : _a.debug,
|
|
185
|
-
hooks: {
|
|
186
|
-
init: () => setCliHelpForEvent(this.cliHelp),
|
|
187
|
-
},
|
|
188
133
|
});
|
|
189
134
|
}
|
|
190
135
|
const targetPath = makePath(path);
|
|
191
|
-
const { getArgs, getStaticPart } = this.cliApp.cli(targetPath, fn);
|
|
192
136
|
const meta = getCliMate().read(opts.fakeInstance, opts.method);
|
|
193
137
|
const classMeta = getCliMate().read(opts.fakeInstance);
|
|
194
|
-
const args = {};
|
|
195
|
-
getArgs().forEach(a => {
|
|
196
|
-
var _a;
|
|
197
|
-
const argParam = (_a = meta === null || meta === void 0 ? void 0 : meta.params) === null || _a === void 0 ? void 0 : _a.find(p => p.label === a && p.description);
|
|
198
|
-
args[a] = (argParam === null || argParam === void 0 ? void 0 : argParam.description) || '';
|
|
199
|
-
});
|
|
200
|
-
cliCommand = getStaticPart().replace(/\//g, ' ').trim();
|
|
201
138
|
const cliOptions = new Map();
|
|
202
139
|
[
|
|
203
140
|
...(((_c = (_b = this.opts) === null || _b === void 0 ? void 0 : _b.globalCliOptions) === null || _c === void 0 ? void 0 : _c.length) ? this.opts.globalCliOptions : []),
|
|
204
141
|
...((classMeta === null || classMeta === void 0 ? void 0 : classMeta.cliOptions) || []),
|
|
205
|
-
...(((_d = meta === null || meta === void 0 ? void 0 : meta.params) === null || _d === void 0 ? void 0 : _d.filter(param => !!param.
|
|
206
|
-
keys: param.
|
|
142
|
+
...(((_d = meta === null || meta === void 0 ? void 0 : meta.params) === null || _d === void 0 ? void 0 : _d.filter(param => !!param.cliOptionsKeys && param.cliOptionsKeys.length > 0).map(param => ({
|
|
143
|
+
keys: param.cliOptionsKeys,
|
|
207
144
|
value: typeof param.value === 'string' ? param.value : '',
|
|
208
145
|
description: param.description || '',
|
|
209
146
|
}))) || []),
|
|
210
147
|
].forEach(o => cliOptions.set(o.keys[0], o));
|
|
211
|
-
if ((_e = this.opts) === null || _e === void 0 ? void 0 : _e.debug) {
|
|
212
|
-
opts.logHandler(`${'[36m'}(CLI)${'[32m'}${targetPath}`);
|
|
213
|
-
}
|
|
214
148
|
const aliases = [];
|
|
215
149
|
if (meta === null || meta === void 0 ? void 0 : meta.cliAliases) {
|
|
216
150
|
for (const alias of meta.cliAliases) {
|
|
217
151
|
const targetPath = makePath(alias);
|
|
218
|
-
|
|
219
|
-
aliases.push(getStaticPart().replace(/\//g, ' ').trim());
|
|
220
|
-
if ((_f = this.opts) === null || _f === void 0 ? void 0 : _f.debug) {
|
|
221
|
-
opts.logHandler(`${'[36m'}(CLI-alias)${'[32m'}${targetPath}`);
|
|
222
|
-
}
|
|
152
|
+
aliases.push(targetPath);
|
|
223
153
|
}
|
|
224
154
|
}
|
|
225
|
-
|
|
155
|
+
const args = {};
|
|
156
|
+
(_e = meta === null || meta === void 0 ? void 0 : meta.params) === null || _e === void 0 ? void 0 : _e.filter(p => p.isRouteParam && p.description).forEach(p => args[p.isRouteParam] = p.description);
|
|
157
|
+
this.cliApp.cli(targetPath, {
|
|
226
158
|
description: (meta === null || meta === void 0 ? void 0 : meta.description) || '',
|
|
227
|
-
command: cliCommand,
|
|
228
159
|
options: Array.from(cliOptions.values()),
|
|
229
160
|
args,
|
|
230
|
-
aliases
|
|
231
|
-
custom: { fn, log: opts.logHandler },
|
|
161
|
+
aliases,
|
|
232
162
|
examples: (meta === null || meta === void 0 ? void 0 : meta.cliExamples) || [],
|
|
163
|
+
handler: fn,
|
|
164
|
+
onRegister: (path, aliasType) => {
|
|
165
|
+
var _a;
|
|
166
|
+
if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.debug) {
|
|
167
|
+
opts.logHandler(`${'[36m'}(${aliasTypes[aliasType]})${'[32m'}${path}`);
|
|
168
|
+
}
|
|
169
|
+
},
|
|
233
170
|
});
|
|
234
171
|
}
|
|
235
172
|
}
|
|
236
|
-
}
|
|
173
|
+
}
|
|
174
|
+
const aliasTypes = ['CLI', 'CLI-alias', 'CLI-alias*', 'CLI-alias*'];
|
|
237
175
|
|
|
238
176
|
function formatParams(keys) {
|
|
239
177
|
const names = [keys].flat();
|
|
@@ -248,7 +186,7 @@ function formatParams(keys) {
|
|
|
248
186
|
* ```ts
|
|
249
187
|
* │ @Cli('command')
|
|
250
188
|
* │ command(
|
|
251
|
-
* │ @Description('Test
|
|
189
|
+
* │ @Description('Test option...')
|
|
252
190
|
* │ @CliOption('test', 't')
|
|
253
191
|
* │ test: boolean,
|
|
254
192
|
* │ ) {
|
|
@@ -261,23 +199,7 @@ function formatParams(keys) {
|
|
|
261
199
|
*/
|
|
262
200
|
function CliOption(...keys) {
|
|
263
201
|
const mate = getCliMate();
|
|
264
|
-
return mate.apply(mate.decorate('
|
|
265
|
-
const flags = useFlags();
|
|
266
|
-
const names = keys;
|
|
267
|
-
const vals = [];
|
|
268
|
-
for (const name of names) {
|
|
269
|
-
if (flags[name]) {
|
|
270
|
-
vals.push(flags[name]);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
if (vals.length > 1) {
|
|
274
|
-
throw new Error(`Options ${formatParams(names).join(' and ')} are synonyms. Please use only one of them.`);
|
|
275
|
-
}
|
|
276
|
-
if (vals.length === 0) {
|
|
277
|
-
return false;
|
|
278
|
-
}
|
|
279
|
-
return vals[0];
|
|
280
|
-
}, formatParams(keys).join(', ')));
|
|
202
|
+
return mate.apply(mate.decorate('cliOptionsKeys', keys, false), Resolve(() => useCliOption(keys[0]), formatParams(keys).join(', ')));
|
|
281
203
|
}
|
|
282
204
|
/**
|
|
283
205
|
* ## Define Global CLI Option
|
|
@@ -300,6 +222,7 @@ function CliGlobalOption(option) {
|
|
|
300
222
|
* For example the folowing path are interpreted the same:
|
|
301
223
|
* - "command test use:dev :name"
|
|
302
224
|
* - "command/test/use:dev/:name"
|
|
225
|
+
*
|
|
303
226
|
* Where name will become an argument
|
|
304
227
|
*
|
|
305
228
|
* @param path - command path
|
|
@@ -347,49 +270,18 @@ function CliExample(cmd, description) {
|
|
|
347
270
|
*/
|
|
348
271
|
const cliHelpInterceptor = (opts) => {
|
|
349
272
|
return defineInterceptorFn(() => {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
try {
|
|
354
|
-
useCliHelp().print(opts === null || opts === void 0 ? void 0 : opts.colors);
|
|
355
|
-
return '';
|
|
356
|
-
}
|
|
357
|
-
catch (e) {
|
|
358
|
-
//
|
|
359
|
-
}
|
|
273
|
+
try {
|
|
274
|
+
if (useAutoHelp(opts === null || opts === void 0 ? void 0 : opts.helpOptions, opts === null || opts === void 0 ? void 0 : opts.colors)) {
|
|
275
|
+
return '';
|
|
360
276
|
}
|
|
361
277
|
}
|
|
362
|
-
|
|
278
|
+
catch (e) {
|
|
279
|
+
//
|
|
280
|
+
}
|
|
281
|
+
if (opts === null || opts === void 0 ? void 0 : opts.lookupLevel) {
|
|
363
282
|
const { getMethod } = useControllerContext();
|
|
364
283
|
if (!getMethod()) {
|
|
365
|
-
|
|
366
|
-
const cliHelp = useCliHelp().getCliHelp();
|
|
367
|
-
const cmd = cliHelp.getCliName();
|
|
368
|
-
let data;
|
|
369
|
-
for (let i = 0; i < Math.min(parts.length, 4); i++) {
|
|
370
|
-
const pathParams = parts.slice(0, i ? -i : parts.length).join(' ');
|
|
371
|
-
try {
|
|
372
|
-
data = cliHelp.match(pathParams);
|
|
373
|
-
break;
|
|
374
|
-
}
|
|
375
|
-
catch (e) {
|
|
376
|
-
if (opts === null || opts === void 0 ? void 0 : opts.helpWithIncompleteCmd) {
|
|
377
|
-
const variants = cliHelp.lookup(pathParams);
|
|
378
|
-
if (variants.length) {
|
|
379
|
-
throw new Error(`Wrong command, did you mean:\n${variants.slice(0, 7).map(c => ` $ ${cmd} ${c.main.command}`).join('\n')}`);
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
if (data) {
|
|
385
|
-
const { main, children } = data;
|
|
386
|
-
if ((opts === null || opts === void 0 ? void 0 : opts.helpWithArgs) && main.args && Object.keys(main.args).length) {
|
|
387
|
-
throw new Error(`Arguments expected: ${Object.keys(main.args).map(l => `<${l}>`).join(', ')}`);
|
|
388
|
-
}
|
|
389
|
-
else if ((opts === null || opts === void 0 ? void 0 : opts.helpWithIncompleteCmd) && children && children.length) {
|
|
390
|
-
throw new Error(`Wrong command, did you mean:\n${children.slice(0, 7).map(c => ` $ ${cmd} ${c.command}`).join('\n')}`);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
284
|
+
useCommandLookupHelp(opts.lookupLevel);
|
|
393
285
|
}
|
|
394
286
|
}
|
|
395
287
|
}, TInterceptorPriority.BEFORE_ALL);
|
|
@@ -417,4 +309,4 @@ const cliHelpInterceptor = (opts) => {
|
|
|
417
309
|
*/
|
|
418
310
|
const CliHelpInterceptor = (...opts) => Intercept(cliHelpInterceptor(...opts));
|
|
419
311
|
|
|
420
|
-
export { Cli, CliAlias, CliExample, CliGlobalOption, CliHelpInterceptor, CliOption, MoostCli, cliHelpInterceptor
|
|
312
|
+
export { Cli, CliAlias, CliExample, CliGlobalOption, CliHelpInterceptor, CliOption, MoostCli, cliHelpInterceptor };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moostjs/event-cli",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.31",
|
|
4
4
|
"description": "@moostjs/event-cli",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -28,12 +28,11 @@
|
|
|
28
28
|
},
|
|
29
29
|
"homepage": "https://github.com/moostjs/moostjs/tree/main/packages/event-cli#readme",
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"moost": "0.2.
|
|
32
|
-
"wooks": "^0.2
|
|
33
|
-
"@wooksjs/event-core": "^0.2
|
|
31
|
+
"moost": "0.2.31",
|
|
32
|
+
"wooks": "^0.3.2",
|
|
33
|
+
"@wooksjs/event-core": "^0.3.2"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@wooksjs/event-cli": "^0.2
|
|
37
|
-
"@prostojs/cli-help": "^0.0.9"
|
|
36
|
+
"@wooksjs/event-cli": "^0.3.2"
|
|
38
37
|
}
|
|
39
38
|
}
|