@moostjs/event-cli 0.5.21 → 0.5.23
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/LICENSE +21 -0
- package/dist/index.cjs +306 -178
- package/dist/index.d.ts +20 -156
- package/dist/index.mjs +239 -167
- package/package.json +16 -9
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 moostjs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
CHANGED
|
@@ -1,204 +1,332 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region rolldown:runtime
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
2
23
|
|
|
3
|
-
|
|
4
|
-
|
|
24
|
+
//#endregion
|
|
25
|
+
const moost = __toESM(require("moost"));
|
|
26
|
+
const __wooksjs_event_cli = __toESM(require("@wooksjs/event-cli"));
|
|
5
27
|
|
|
28
|
+
//#region packages/event-cli/src/meta-types.ts
|
|
6
29
|
function getCliMate() {
|
|
7
|
-
|
|
30
|
+
return (0, moost.getMoostMate)();
|
|
8
31
|
}
|
|
9
32
|
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region packages/event-cli/src/decorators/cli.decorator.ts
|
|
10
35
|
function Cli(path) {
|
|
11
|
-
|
|
36
|
+
return getCliMate().decorate("handlers", {
|
|
37
|
+
path: path?.replace(/\s+/g, "/"),
|
|
38
|
+
type: "CLI"
|
|
39
|
+
}, true);
|
|
12
40
|
}
|
|
13
41
|
function CliAlias(alias) {
|
|
14
|
-
|
|
42
|
+
return getCliMate().decorate("cliAliases", alias, true);
|
|
15
43
|
}
|
|
16
44
|
function CliExample(cmd, description) {
|
|
17
|
-
|
|
45
|
+
return getCliMate().decorate("cliExamples", {
|
|
46
|
+
cmd,
|
|
47
|
+
description
|
|
48
|
+
}, true);
|
|
18
49
|
}
|
|
19
50
|
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region packages/event-cli/src/utils.ts
|
|
20
53
|
function formatParams(keys) {
|
|
21
|
-
|
|
22
|
-
|
|
54
|
+
const names = [keys].flat();
|
|
55
|
+
return names.map((n) => n.length === 1 ? `-${n}` : `--${n}`);
|
|
23
56
|
}
|
|
24
57
|
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region packages/event-cli/src/decorators/option.decorator.ts
|
|
25
60
|
function CliOption(...keys) {
|
|
26
|
-
|
|
27
|
-
|
|
61
|
+
const mate = getCliMate();
|
|
62
|
+
return mate.apply(mate.decorate("cliOptionsKeys", keys, false), (0, moost.Resolve)(() => (0, __wooksjs_event_cli.useCliOption)(keys[0]), formatParams(keys).join(", ")));
|
|
28
63
|
}
|
|
29
64
|
function CliGlobalOption(option) {
|
|
30
|
-
|
|
31
|
-
|
|
65
|
+
const mate = getCliMate();
|
|
66
|
+
return mate.decorate("cliOptions", option, true);
|
|
32
67
|
}
|
|
33
68
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
else if (cliAppOpts) {
|
|
46
|
-
this.cliApp = eventCli.createCliApp({
|
|
47
|
-
...cliAppOpts,
|
|
48
|
-
onNotFound: this.onNotFound.bind(this),
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
this.cliApp = eventCli.createCliApp({
|
|
53
|
-
onNotFound: this.onNotFound.bind(this),
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
if (!opts?.debug) {
|
|
57
|
-
moost.setInfactLoggingOptions({
|
|
58
|
-
newInstance: false,
|
|
59
|
-
error: false,
|
|
60
|
-
warn: false,
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
async onNotFound() {
|
|
65
|
-
const pathParams = eventCli.useCliContext().store('event').get('pathParams') || [];
|
|
66
|
-
const response = await moost.defineMoostEventHandler({
|
|
67
|
-
loggerTitle: LOGGER_TITLE,
|
|
68
|
-
getIterceptorHandler: () => this.moost?.getGlobalInterceptorHandler(),
|
|
69
|
-
getControllerInstance: () => this.moost,
|
|
70
|
-
callControllerMethod: () => undefined,
|
|
71
|
-
logErrors: this.opts?.debug,
|
|
72
|
-
targetPath: '',
|
|
73
|
-
handlerType: '__SYSTEM__',
|
|
74
|
-
})();
|
|
75
|
-
if (response === undefined) {
|
|
76
|
-
this.cliApp.onUnknownCommand(pathParams);
|
|
77
|
-
}
|
|
78
|
-
return response;
|
|
79
|
-
}
|
|
80
|
-
onInit(moost) {
|
|
81
|
-
this.moost = moost;
|
|
82
|
-
const boolean = Object.entries(this.optionTypes)
|
|
83
|
-
.filter(([_key, val]) => val.length === 1 && val[0] === Boolean)
|
|
84
|
-
.map(([key, _val]) => key);
|
|
85
|
-
void this.cliApp.run(undefined, {
|
|
86
|
-
boolean,
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
bindHandler(opts) {
|
|
90
|
-
let fn;
|
|
91
|
-
for (const handler of opts.handlers) {
|
|
92
|
-
if (handler.type !== 'CLI') {
|
|
93
|
-
continue;
|
|
94
|
-
}
|
|
95
|
-
const path = typeof handler.path === 'string'
|
|
96
|
-
? handler.path
|
|
97
|
-
: typeof opts.method === 'string'
|
|
98
|
-
? opts.method
|
|
99
|
-
: '';
|
|
100
|
-
const prefix = opts.prefix.replace(/\s+/g, '/') || '';
|
|
101
|
-
const makePath = (p) => `${prefix}/${p}`
|
|
102
|
-
.replace(/\/\/+/g, '/')
|
|
103
|
-
.replace(/\/\\:/g, '\\:')
|
|
104
|
-
.replace(/^\/+/g, '');
|
|
105
|
-
const targetPath = makePath(path);
|
|
106
|
-
fn = moost.defineMoostEventHandler({
|
|
107
|
-
contextType: CONTEXT_TYPE,
|
|
108
|
-
loggerTitle: LOGGER_TITLE,
|
|
109
|
-
getIterceptorHandler: opts.getIterceptorHandler,
|
|
110
|
-
getControllerInstance: opts.getInstance,
|
|
111
|
-
controllerMethod: opts.method,
|
|
112
|
-
resolveArgs: opts.resolveArgs,
|
|
113
|
-
logErrors: this.opts?.debug,
|
|
114
|
-
targetPath,
|
|
115
|
-
handlerType: handler.type,
|
|
116
|
-
});
|
|
117
|
-
const meta = getCliMate().read(opts.fakeInstance, opts.method);
|
|
118
|
-
const classMeta = getCliMate().read(opts.fakeInstance);
|
|
119
|
-
const cliOptions = new Map();
|
|
120
|
-
[
|
|
121
|
-
...(this.opts?.globalCliOptions?.length ? this.opts.globalCliOptions : []),
|
|
122
|
-
...(classMeta?.cliOptions || []),
|
|
123
|
-
...(meta?.params
|
|
124
|
-
? meta.params
|
|
125
|
-
.filter((param) => param.cliOptionsKeys?.length)
|
|
126
|
-
.map((param) => ({
|
|
127
|
-
keys: param.cliOptionsKeys,
|
|
128
|
-
value: typeof param.value === 'string' ? param.value : '',
|
|
129
|
-
description: param.description || '',
|
|
130
|
-
type: param.type,
|
|
131
|
-
}))
|
|
132
|
-
: []),
|
|
133
|
-
].forEach(o => cliOptions.set(o.keys[0], o));
|
|
134
|
-
const aliases = [];
|
|
135
|
-
if (meta?.cliAliases) {
|
|
136
|
-
for (const alias of meta.cliAliases) {
|
|
137
|
-
const targetPath = makePath(alias);
|
|
138
|
-
aliases.push(targetPath);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
const cliOptionsArray = Array.from(cliOptions.values());
|
|
142
|
-
cliOptionsArray.forEach(o => {
|
|
143
|
-
for (const key of o.keys) {
|
|
144
|
-
if (!this.optionTypes[key]) {
|
|
145
|
-
this.optionTypes[key] = [];
|
|
146
|
-
}
|
|
147
|
-
if (!this.optionTypes[key].includes(o.type)) {
|
|
148
|
-
this.optionTypes[key].push(o.type);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
const args = {};
|
|
153
|
-
meta?.params
|
|
154
|
-
.filter(p => p.paramSource === 'ROUTE' && p.description)
|
|
155
|
-
.forEach(p => (args[p.paramName] = p.description));
|
|
156
|
-
const routerBinding = this.cliApp.cli(targetPath, {
|
|
157
|
-
description: meta?.description || '',
|
|
158
|
-
options: cliOptionsArray,
|
|
159
|
-
args,
|
|
160
|
-
aliases,
|
|
161
|
-
examples: meta?.cliExamples || [],
|
|
162
|
-
handler: fn,
|
|
163
|
-
onRegister: (path, aliasType, route) => {
|
|
164
|
-
opts.register(handler, path, route?.getArgs() || routerBinding.getArgs());
|
|
165
|
-
if (this.opts?.debug) {
|
|
166
|
-
opts.logHandler(`${'[36m'}(${aliasTypes[aliasType]})${'[32m'}${path}`);
|
|
167
|
-
}
|
|
168
|
-
},
|
|
169
|
-
});
|
|
170
|
-
opts.register(handler, targetPath, routerBinding.getArgs());
|
|
171
|
-
}
|
|
172
|
-
}
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region packages/event-cli/src/event-cli.ts
|
|
71
|
+
function _define_property$1(obj, key, value) {
|
|
72
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
73
|
+
value,
|
|
74
|
+
enumerable: true,
|
|
75
|
+
configurable: true,
|
|
76
|
+
writable: true
|
|
77
|
+
});
|
|
78
|
+
else obj[key] = value;
|
|
79
|
+
return obj;
|
|
173
80
|
}
|
|
174
|
-
const
|
|
81
|
+
const LOGGER_TITLE = "moost-cli";
|
|
82
|
+
const CONTEXT_TYPE = "CLI";
|
|
83
|
+
var MoostCli = class {
|
|
84
|
+
async onNotFound() {
|
|
85
|
+
const pathParams = (0, __wooksjs_event_cli.useCliContext)().store("event").get("pathParams") || [];
|
|
86
|
+
const response = await (0, moost.defineMoostEventHandler)({
|
|
87
|
+
loggerTitle: LOGGER_TITLE,
|
|
88
|
+
getIterceptorHandler: () => this.moost?.getGlobalInterceptorHandler(),
|
|
89
|
+
getControllerInstance: () => this.moost,
|
|
90
|
+
callControllerMethod: () => undefined,
|
|
91
|
+
logErrors: this.opts?.debug,
|
|
92
|
+
targetPath: "",
|
|
93
|
+
handlerType: "__SYSTEM__"
|
|
94
|
+
})();
|
|
95
|
+
if (response === undefined) this.cliApp.onUnknownCommand(pathParams);
|
|
96
|
+
return response;
|
|
97
|
+
}
|
|
98
|
+
onInit(moost$1) {
|
|
99
|
+
this.moost = moost$1;
|
|
100
|
+
const boolean = Object.entries(this.optionTypes).filter(([_key, val]) => val.length === 1 && val[0] === Boolean).map(([key, _val]) => key);
|
|
101
|
+
this.cliApp.run(undefined, { boolean });
|
|
102
|
+
}
|
|
103
|
+
bindHandler(opts) {
|
|
104
|
+
let fn;
|
|
105
|
+
for (const handler of opts.handlers) {
|
|
106
|
+
if (handler.type !== "CLI") continue;
|
|
107
|
+
const path = typeof handler.path === "string" ? handler.path : typeof opts.method === "string" ? opts.method : "";
|
|
108
|
+
const prefix = opts.prefix.replace(/\s+/g, "/") || "";
|
|
109
|
+
const makePath = (p) => `${prefix}/${p}`.replace(/\/\/+/g, "/").replace(/\/\\:/g, "\\:").replace(/^\/+/g, "");
|
|
110
|
+
const targetPath = makePath(path);
|
|
111
|
+
fn = (0, moost.defineMoostEventHandler)({
|
|
112
|
+
contextType: CONTEXT_TYPE,
|
|
113
|
+
loggerTitle: LOGGER_TITLE,
|
|
114
|
+
getIterceptorHandler: opts.getIterceptorHandler,
|
|
115
|
+
getControllerInstance: opts.getInstance,
|
|
116
|
+
controllerMethod: opts.method,
|
|
117
|
+
resolveArgs: opts.resolveArgs,
|
|
118
|
+
logErrors: this.opts?.debug,
|
|
119
|
+
targetPath,
|
|
120
|
+
handlerType: handler.type
|
|
121
|
+
});
|
|
122
|
+
const meta = getCliMate().read(opts.fakeInstance, opts.method);
|
|
123
|
+
const classMeta = getCliMate().read(opts.fakeInstance);
|
|
124
|
+
const cliOptions = new Map();
|
|
125
|
+
[
|
|
126
|
+
...this.opts?.globalCliOptions?.length ? this.opts.globalCliOptions : [],
|
|
127
|
+
...classMeta?.cliOptions || [],
|
|
128
|
+
...meta?.params ? meta.params.filter((param) => param.cliOptionsKeys?.length).map((param) => ({
|
|
129
|
+
keys: param.cliOptionsKeys,
|
|
130
|
+
value: typeof param.value === "string" ? param.value : "",
|
|
131
|
+
description: param.description || "",
|
|
132
|
+
type: param.type
|
|
133
|
+
})) : []
|
|
134
|
+
].forEach((o) => cliOptions.set(o.keys[0], o));
|
|
135
|
+
const aliases = [];
|
|
136
|
+
if (meta?.cliAliases) for (const alias of meta.cliAliases) {
|
|
137
|
+
const targetPath$1 = makePath(alias);
|
|
138
|
+
aliases.push(targetPath$1);
|
|
139
|
+
}
|
|
140
|
+
const cliOptionsArray = Array.from(cliOptions.values());
|
|
141
|
+
cliOptionsArray.forEach((o) => {
|
|
142
|
+
for (const key of o.keys) {
|
|
143
|
+
if (!this.optionTypes[key]) this.optionTypes[key] = [];
|
|
144
|
+
if (!this.optionTypes[key].includes(o.type)) this.optionTypes[key].push(o.type);
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
const args = {};
|
|
148
|
+
meta?.params.filter((p) => p.paramSource === "ROUTE" && p.description).forEach((p) => args[p.paramName] = p.description);
|
|
149
|
+
const routerBinding = this.cliApp.cli(targetPath, {
|
|
150
|
+
description: meta?.description || "",
|
|
151
|
+
options: cliOptionsArray,
|
|
152
|
+
args,
|
|
153
|
+
aliases,
|
|
154
|
+
examples: meta?.cliExamples || [],
|
|
155
|
+
handler: fn,
|
|
156
|
+
onRegister: (path$1, aliasType, route) => {
|
|
157
|
+
opts.register(handler, path$1, route?.getArgs() || routerBinding.getArgs());
|
|
158
|
+
if (this.opts?.debug) opts.logHandler(`${"\x1B[36m"}(${aliasTypes[aliasType]})${"\x1B[32m"}${path$1}`);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
opts.register(handler, targetPath, routerBinding.getArgs());
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
constructor(opts) {
|
|
165
|
+
_define_property$1(this, "opts", void 0);
|
|
166
|
+
_define_property$1(this, "name", void 0);
|
|
167
|
+
_define_property$1(this, "cliApp", void 0);
|
|
168
|
+
_define_property$1(this, "optionTypes", void 0);
|
|
169
|
+
_define_property$1(this, "moost", void 0);
|
|
170
|
+
this.opts = opts;
|
|
171
|
+
this.name = "cli";
|
|
172
|
+
this.optionTypes = {};
|
|
173
|
+
const cliAppOpts = opts?.wooksCli;
|
|
174
|
+
if (cliAppOpts && cliAppOpts instanceof __wooksjs_event_cli.WooksCli) this.cliApp = cliAppOpts;
|
|
175
|
+
else if (cliAppOpts) this.cliApp = (0, __wooksjs_event_cli.createCliApp)({
|
|
176
|
+
...cliAppOpts,
|
|
177
|
+
onNotFound: this.onNotFound.bind(this)
|
|
178
|
+
});
|
|
179
|
+
else this.cliApp = (0, __wooksjs_event_cli.createCliApp)({ onNotFound: this.onNotFound.bind(this) });
|
|
180
|
+
if (!opts?.debug) (0, moost.setInfactLoggingOptions)({
|
|
181
|
+
newInstance: false,
|
|
182
|
+
error: false,
|
|
183
|
+
warn: false
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
const aliasTypes = [
|
|
188
|
+
"CLI",
|
|
189
|
+
"CLI-alias",
|
|
190
|
+
"CLI-alias*",
|
|
191
|
+
"CLI-alias*"
|
|
192
|
+
];
|
|
175
193
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
if (!getMethod()) {
|
|
187
|
-
eventCli.useCommandLookupHelp(opts.lookupLevel);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
194
|
+
//#endregion
|
|
195
|
+
//#region packages/event-cli/src/interceptors/help.interceptor.ts
|
|
196
|
+
const cliHelpInterceptor = (opts) => (0, moost.defineInterceptorFn)(() => {
|
|
197
|
+
try {
|
|
198
|
+
if ((0, __wooksjs_event_cli.useAutoHelp)(opts?.helpOptions, opts?.colors)) return "";
|
|
199
|
+
} catch (error) {}
|
|
200
|
+
if (opts?.lookupLevel) {
|
|
201
|
+
const { getMethod } = (0, moost.useControllerContext)();
|
|
202
|
+
if (!getMethod()) (0, __wooksjs_event_cli.useCommandLookupHelp)(opts.lookupLevel);
|
|
203
|
+
}
|
|
190
204
|
}, moost.TInterceptorPriority.BEFORE_ALL);
|
|
191
|
-
const CliHelpInterceptor = (...opts) => moost.Intercept(cliHelpInterceptor(...opts));
|
|
205
|
+
const CliHelpInterceptor = (...opts) => (0, moost.Intercept)(cliHelpInterceptor(...opts));
|
|
192
206
|
|
|
193
|
-
|
|
207
|
+
//#endregion
|
|
208
|
+
//#region packages/event-cli/src/quick-cli.ts
|
|
209
|
+
function _define_property(obj, key, value) {
|
|
210
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
211
|
+
value,
|
|
212
|
+
enumerable: true,
|
|
213
|
+
configurable: true,
|
|
214
|
+
writable: true
|
|
215
|
+
});
|
|
216
|
+
else obj[key] = value;
|
|
217
|
+
return obj;
|
|
218
|
+
}
|
|
219
|
+
var CliApp = class extends moost.Moost {
|
|
220
|
+
/**
|
|
221
|
+
* Registers one or more CLI controllers.
|
|
222
|
+
*
|
|
223
|
+
* (Shortcut for `registerControllers` method.)
|
|
224
|
+
*
|
|
225
|
+
* @param {...(object|Function|[string, object|Function])} controllers - List of controllers.
|
|
226
|
+
* Each controller can be an object, a class, or a tuple with a name and the controller.
|
|
227
|
+
* @returns {this} The CliApp instance for method chaining.
|
|
228
|
+
*/ controllers(...controllers) {
|
|
229
|
+
return this.registerControllers(...controllers);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Configures the CLI help options.
|
|
233
|
+
*
|
|
234
|
+
* This method sets the help options for the CLI. It ensures that colored output is enabled
|
|
235
|
+
* (unless explicitly disabled) and that the lookup level defaults to 3 if not provided.
|
|
236
|
+
*
|
|
237
|
+
* @param {THelpOptions} helpOpts - Help options configuration.
|
|
238
|
+
* @param {boolean} helpOpts.colors - Enable colored output.
|
|
239
|
+
* @param {number} helpOpts.lookupLevel - Level for help lookup.
|
|
240
|
+
* @returns {this} The CliApp instance for method chaining.
|
|
241
|
+
*/ useHelp(helpOpts) {
|
|
242
|
+
this._helpOpts = helpOpts;
|
|
243
|
+
if (this._helpOpts.colors !== false) this._helpOpts.colors = true;
|
|
244
|
+
if (typeof this._helpOpts.lookupLevel !== "number") this._helpOpts.lookupLevel = 3;
|
|
245
|
+
return this;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Sets the global CLI options.
|
|
249
|
+
*
|
|
250
|
+
* @param {TMoostCliOpts['globalCliOptions']} globalOpts - Global options for the CLI.
|
|
251
|
+
* @returns {this} The CliApp instance for method chaining.
|
|
252
|
+
*/ useOptions(globalOpts) {
|
|
253
|
+
this._globalOpts = globalOpts;
|
|
254
|
+
return this;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Starts the CLI application.
|
|
258
|
+
*
|
|
259
|
+
* This method creates a MoostCli instance using the configured help and global options,
|
|
260
|
+
* attaches it via the adapter, applies the CLI help interceptor (if help options are set),
|
|
261
|
+
* and then initializes the application.
|
|
262
|
+
*
|
|
263
|
+
* @returns {any} The result of the initialization process.
|
|
264
|
+
*/ start() {
|
|
265
|
+
const cli = new MoostCli({
|
|
266
|
+
wooksCli: { cliHelp: this._helpOpts ? {
|
|
267
|
+
name: this._helpOpts.name,
|
|
268
|
+
title: this._helpOpts.title,
|
|
269
|
+
maxWidth: this._helpOpts.maxWidth,
|
|
270
|
+
maxLeft: this._helpOpts.maxLeft,
|
|
271
|
+
mark: this._helpOpts.mark
|
|
272
|
+
} : undefined },
|
|
273
|
+
globalCliOptions: this._globalOpts
|
|
274
|
+
});
|
|
275
|
+
this.adapter(cli);
|
|
276
|
+
if (this._helpOpts) this.applyGlobalInterceptors(cliHelpInterceptor({
|
|
277
|
+
colors: this._helpOpts.colors,
|
|
278
|
+
lookupLevel: this._helpOpts.lookupLevel
|
|
279
|
+
}));
|
|
280
|
+
return this.init();
|
|
281
|
+
}
|
|
282
|
+
constructor(...args) {
|
|
283
|
+
super(...args), _define_property(this, "_helpOpts", void 0), _define_property(this, "_globalOpts", void 0);
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
//#endregion
|
|
288
|
+
exports.Cli = Cli
|
|
289
|
+
exports.CliAlias = CliAlias
|
|
290
|
+
exports.CliApp = CliApp
|
|
291
|
+
exports.CliExample = CliExample
|
|
292
|
+
exports.CliGlobalOption = CliGlobalOption
|
|
293
|
+
exports.CliHelpInterceptor = CliHelpInterceptor
|
|
294
|
+
exports.CliOption = CliOption
|
|
295
|
+
Object.defineProperty(exports, 'Controller', {
|
|
296
|
+
enumerable: true,
|
|
297
|
+
get: function () {
|
|
298
|
+
return moost.Controller;
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
Object.defineProperty(exports, 'Description', {
|
|
302
|
+
enumerable: true,
|
|
303
|
+
get: function () {
|
|
304
|
+
return moost.Description;
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
Object.defineProperty(exports, 'Intercept', {
|
|
194
308
|
enumerable: true,
|
|
195
|
-
get: function () {
|
|
309
|
+
get: function () {
|
|
310
|
+
return moost.Intercept;
|
|
311
|
+
}
|
|
196
312
|
});
|
|
197
|
-
exports.
|
|
198
|
-
exports
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
exports
|
|
313
|
+
exports.MoostCli = MoostCli
|
|
314
|
+
Object.defineProperty(exports, 'Optional', {
|
|
315
|
+
enumerable: true,
|
|
316
|
+
get: function () {
|
|
317
|
+
return moost.Optional;
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
Object.defineProperty(exports, 'Param', {
|
|
321
|
+
enumerable: true,
|
|
322
|
+
get: function () {
|
|
323
|
+
return moost.Param;
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
exports.cliHelpInterceptor = cliHelpInterceptor
|
|
327
|
+
Object.defineProperty(exports, 'useCliContext', {
|
|
328
|
+
enumerable: true,
|
|
329
|
+
get: function () {
|
|
330
|
+
return __wooksjs_event_cli.useCliContext;
|
|
331
|
+
}
|
|
332
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -2,71 +2,13 @@ import { WooksCli, TWooksCliOptions } from '@wooksjs/event-cli';
|
|
|
2
2
|
export { useCliContext } from '@wooksjs/event-cli';
|
|
3
3
|
import * as moost from 'moost';
|
|
4
4
|
import { TMoostAdapter, Moost, TMoostAdapterOptions } from 'moost';
|
|
5
|
+
export { Controller, Description, Intercept, Optional, Param, TInterceptorPriority, defineInterceptorFn } from 'moost';
|
|
5
6
|
|
|
6
|
-
/**
|
|
7
|
-
* ## Define CLI Command
|
|
8
|
-
* ### @MethodDecorator
|
|
9
|
-
*
|
|
10
|
-
* Command path segments may be separated by / or space.
|
|
11
|
-
*
|
|
12
|
-
* For example the folowing path are interpreted the same:
|
|
13
|
-
* - "command test use:dev :name"
|
|
14
|
-
* - "command/test/use:dev/:name"
|
|
15
|
-
*
|
|
16
|
-
* Where name will become an argument
|
|
17
|
-
*
|
|
18
|
-
* @param path - command path
|
|
19
|
-
* @returns
|
|
20
|
-
*/
|
|
21
7
|
declare function Cli(path?: string): MethodDecorator;
|
|
22
|
-
/**
|
|
23
|
-
* ## Define CLI Command Alias
|
|
24
|
-
* ### @MethodDecorator
|
|
25
|
-
*
|
|
26
|
-
* Use it to define alias for @Cli('...') command
|
|
27
|
-
*
|
|
28
|
-
* @param path - command alias path
|
|
29
|
-
* @returns
|
|
30
|
-
*/
|
|
31
8
|
declare function CliAlias(alias: string): MethodDecorator;
|
|
32
|
-
/**
|
|
33
|
-
* ## Define CLI Example
|
|
34
|
-
* ### @MethodDecorator
|
|
35
|
-
*
|
|
36
|
-
* Use it to define example for Cli Help display
|
|
37
|
-
*
|
|
38
|
-
* @param path - command alias path
|
|
39
|
-
* @returns
|
|
40
|
-
*/
|
|
41
9
|
declare function CliExample(cmd: string, description?: string): MethodDecorator;
|
|
42
10
|
|
|
43
|
-
/**
|
|
44
|
-
* ## Define CLI Option
|
|
45
|
-
* ### @ParameterDecorator
|
|
46
|
-
* Use together with @Description('...') to document cli option
|
|
47
|
-
*
|
|
48
|
-
* ```ts
|
|
49
|
-
* │ @Cli('command')
|
|
50
|
-
* │ command(
|
|
51
|
-
* │ @Description('Test option...')
|
|
52
|
-
* │ @CliOption('test', 't')
|
|
53
|
-
* │ test: boolean,
|
|
54
|
-
* │ ) {
|
|
55
|
-
* │ return `test=${ test }`
|
|
56
|
-
* │ }
|
|
57
|
-
* ```
|
|
58
|
-
*
|
|
59
|
-
* @param keys list of keys (short and long alternatives)
|
|
60
|
-
* @returns
|
|
61
|
-
*/
|
|
62
11
|
declare function CliOption(...keys: string[]): ParameterDecorator;
|
|
63
|
-
/**
|
|
64
|
-
* ## Define Global CLI Option
|
|
65
|
-
* ### @ClassDecorator
|
|
66
|
-
* The option described here will appear in every command instructions
|
|
67
|
-
* @param option keys and description of CLI option
|
|
68
|
-
* @returns
|
|
69
|
-
*/
|
|
70
12
|
declare function CliGlobalOption(option: {
|
|
71
13
|
keys: string[];
|
|
72
14
|
description?: string;
|
|
@@ -79,53 +21,14 @@ interface TCliHandlerMeta {
|
|
|
79
21
|
path: string;
|
|
80
22
|
}
|
|
81
23
|
interface TMoostCliOpts {
|
|
82
|
-
/**
|
|
83
|
-
* WooksCli options or instance
|
|
84
|
-
*/
|
|
85
24
|
wooksCli?: WooksCli | TWooksCliOptions;
|
|
86
|
-
/**
|
|
87
|
-
* more internal logs are printed when true
|
|
88
|
-
*/
|
|
89
25
|
debug?: boolean;
|
|
90
|
-
/**
|
|
91
|
-
* Array of cli options applicable to every cli command
|
|
92
|
-
*/
|
|
93
26
|
globalCliOptions?: Array<{
|
|
94
27
|
keys: string[];
|
|
95
28
|
description?: string;
|
|
96
29
|
type?: TFunction;
|
|
97
30
|
}>;
|
|
98
31
|
}
|
|
99
|
-
/**
|
|
100
|
-
* ## Moost Cli Adapter
|
|
101
|
-
*
|
|
102
|
-
* Moost Adapter for CLI events
|
|
103
|
-
*
|
|
104
|
-
* ```ts
|
|
105
|
-
* │ // Quick example
|
|
106
|
-
* │ import { MoostCli, Cli, CliOption, cliHelpInterceptor } from '@moostjs/event-cli'
|
|
107
|
-
* │ import { Moost, Param } from 'moost'
|
|
108
|
-
* │
|
|
109
|
-
* │ class MyApp extends Moost {
|
|
110
|
-
* │ @Cli('command/:arg')
|
|
111
|
-
* │ command(
|
|
112
|
-
* │ @Param('arg')
|
|
113
|
-
* │ arg: string,
|
|
114
|
-
* │ @CliOption('test', 't')
|
|
115
|
-
* │ test: boolean,
|
|
116
|
-
* │ ) {
|
|
117
|
-
* │ return `command run with flag arg=${ arg }, test=${ test }`
|
|
118
|
-
* │ }
|
|
119
|
-
* │ }
|
|
120
|
-
* │
|
|
121
|
-
* │ const app = new MyApp()
|
|
122
|
-
* │ app.applyGlobalInterceptors(cliHelpInterceptor())
|
|
123
|
-
* │
|
|
124
|
-
* │ const cli = new MoostCli()
|
|
125
|
-
* │ app.adapter(cli)
|
|
126
|
-
* │ app.init()
|
|
127
|
-
* ```
|
|
128
|
-
*/
|
|
129
32
|
declare class MoostCli implements TMoostAdapter<TCliHandlerMeta> {
|
|
130
33
|
protected opts?: TMoostCliOpts | undefined;
|
|
131
34
|
readonly name = "cli";
|
|
@@ -138,72 +41,33 @@ declare class MoostCli implements TMoostAdapter<TCliHandlerMeta> {
|
|
|
138
41
|
bindHandler<T extends object = object>(opts: TMoostAdapterOptions<TCliHandlerMeta, T>): void;
|
|
139
42
|
}
|
|
140
43
|
|
|
141
|
-
/**
|
|
142
|
-
* ### Interceptor Factory for CliHelpRenderer
|
|
143
|
-
*
|
|
144
|
-
* By default intercepts cli calls with flag --help
|
|
145
|
-
* and prints help.
|
|
146
|
-
*
|
|
147
|
-
* ```js
|
|
148
|
-
* new Moost().applyGlobalInterceptors(cliHelpInterceptor({ colors: true }))
|
|
149
|
-
* ```
|
|
150
|
-
* @param opts {} { helpOptions: ['help', 'h'], colors: true } cli options to invoke help renderer
|
|
151
|
-
* @returns TInterceptorFn
|
|
152
|
-
*/
|
|
153
44
|
declare const cliHelpInterceptor: (opts?: {
|
|
154
|
-
/**
|
|
155
|
-
* CLI Options that invoke help
|
|
156
|
-
* ```js
|
|
157
|
-
* helpOptions: ['help', 'h']
|
|
158
|
-
* ```
|
|
159
|
-
*/
|
|
160
45
|
helpOptions?: string[];
|
|
161
|
-
/**
|
|
162
|
-
* Enable colored help
|
|
163
|
-
*/
|
|
164
46
|
colors?: boolean;
|
|
165
|
-
/**
|
|
166
|
-
* Enable lookup for a command
|
|
167
|
-
*/
|
|
168
47
|
lookupLevel?: number;
|
|
169
48
|
}) => moost.TInterceptorFn;
|
|
170
|
-
/**
|
|
171
|
-
* ## @Decorator
|
|
172
|
-
* ### Interceptor Factory for CliHelpRenderer
|
|
173
|
-
*
|
|
174
|
-
* By default intercepts cli calls with flag --help
|
|
175
|
-
* and prints help.
|
|
176
|
-
*
|
|
177
|
-
* ```ts
|
|
178
|
-
* // default configuration
|
|
179
|
-
* • @CliHelpInterceptor({ helpOptions: 'help', colors: true })
|
|
180
|
-
*
|
|
181
|
-
* // additional option -h to invoke help renderer
|
|
182
|
-
* • @CliHelpInterceptor({ helpOptions: ['help', 'h'], colors: true })
|
|
183
|
-
*
|
|
184
|
-
* // redefine cli option to invoke help renderer
|
|
185
|
-
* • @CliHelpInterceptor({ helpOptions: ['usage'] })
|
|
186
|
-
* ```
|
|
187
|
-
*
|
|
188
|
-
* @param opts {} { helpOptions: ['help', 'h'], colors: true } cli options to invoke help renderer
|
|
189
|
-
* @returns Decorator
|
|
190
|
-
*/
|
|
191
49
|
declare const CliHelpInterceptor: (opts?: {
|
|
192
|
-
/**
|
|
193
|
-
* CLI Options that invoke help
|
|
194
|
-
* ```js
|
|
195
|
-
* helpOptions: ['help', 'h']
|
|
196
|
-
* ```
|
|
197
|
-
*/
|
|
198
50
|
helpOptions?: string[] | undefined;
|
|
199
|
-
/**
|
|
200
|
-
* Enable colored help
|
|
201
|
-
*/
|
|
202
51
|
colors?: boolean | undefined;
|
|
203
|
-
/**
|
|
204
|
-
* Enable lookup for a command
|
|
205
|
-
*/
|
|
206
52
|
lookupLevel?: number | undefined;
|
|
207
53
|
} | undefined) => ClassDecorator & MethodDecorator;
|
|
208
54
|
|
|
209
|
-
|
|
55
|
+
type THelpOptions = {
|
|
56
|
+
name?: string;
|
|
57
|
+
title?: string;
|
|
58
|
+
maxWidth?: number;
|
|
59
|
+
maxLeft?: number;
|
|
60
|
+
mark?: string;
|
|
61
|
+
colors?: boolean;
|
|
62
|
+
lookupLevel?: number;
|
|
63
|
+
};
|
|
64
|
+
declare class CliApp extends Moost {
|
|
65
|
+
protected _helpOpts?: THelpOptions;
|
|
66
|
+
protected _globalOpts?: TMoostCliOpts['globalCliOptions'];
|
|
67
|
+
controllers(...controllers: Array<object | Function | [string, object | Function]>): this;
|
|
68
|
+
useHelp(helpOpts: THelpOptions): this;
|
|
69
|
+
useOptions(globalOpts: TMoostCliOpts['globalCliOptions']): this;
|
|
70
|
+
start(): Promise<void>;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { Cli, CliAlias, CliApp, CliExample, CliGlobalOption, CliHelpInterceptor, CliOption, MoostCli, type TCliHandlerMeta, type TMoostCliOpts, cliHelpInterceptor };
|
package/dist/index.mjs
CHANGED
|
@@ -1,192 +1,264 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
export { useCliContext } from '@wooksjs/event-cli';
|
|
1
|
+
import { Controller, Description, Intercept, Moost, Optional, Param, Resolve, TInterceptorPriority, defineInterceptorFn, defineMoostEventHandler, getMoostMate, setInfactLoggingOptions, useControllerContext } from "moost";
|
|
2
|
+
import { WooksCli, createCliApp, useAutoHelp, useCliContext, useCliOption, useCommandLookupHelp } from "@wooksjs/event-cli";
|
|
4
3
|
|
|
4
|
+
//#region packages/event-cli/src/meta-types.ts
|
|
5
5
|
function getCliMate() {
|
|
6
|
-
|
|
6
|
+
return getMoostMate();
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region packages/event-cli/src/decorators/cli.decorator.ts
|
|
9
11
|
function Cli(path) {
|
|
10
|
-
|
|
12
|
+
return getCliMate().decorate("handlers", {
|
|
13
|
+
path: path?.replace(/\s+/g, "/"),
|
|
14
|
+
type: "CLI"
|
|
15
|
+
}, true);
|
|
11
16
|
}
|
|
12
17
|
function CliAlias(alias) {
|
|
13
|
-
|
|
18
|
+
return getCliMate().decorate("cliAliases", alias, true);
|
|
14
19
|
}
|
|
15
20
|
function CliExample(cmd, description) {
|
|
16
|
-
|
|
21
|
+
return getCliMate().decorate("cliExamples", {
|
|
22
|
+
cmd,
|
|
23
|
+
description
|
|
24
|
+
}, true);
|
|
17
25
|
}
|
|
18
26
|
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region packages/event-cli/src/utils.ts
|
|
19
29
|
function formatParams(keys) {
|
|
20
|
-
|
|
21
|
-
|
|
30
|
+
const names = [keys].flat();
|
|
31
|
+
return names.map((n) => n.length === 1 ? `-${n}` : `--${n}`);
|
|
22
32
|
}
|
|
23
33
|
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region packages/event-cli/src/decorators/option.decorator.ts
|
|
24
36
|
function CliOption(...keys) {
|
|
25
|
-
|
|
26
|
-
|
|
37
|
+
const mate = getCliMate();
|
|
38
|
+
return mate.apply(mate.decorate("cliOptionsKeys", keys, false), Resolve(() => useCliOption(keys[0]), formatParams(keys).join(", ")));
|
|
27
39
|
}
|
|
28
40
|
function CliGlobalOption(option) {
|
|
29
|
-
|
|
30
|
-
|
|
41
|
+
const mate = getCliMate();
|
|
42
|
+
return mate.decorate("cliOptions", option, true);
|
|
31
43
|
}
|
|
32
44
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
else if (cliAppOpts) {
|
|
45
|
-
this.cliApp = createCliApp({
|
|
46
|
-
...cliAppOpts,
|
|
47
|
-
onNotFound: this.onNotFound.bind(this),
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
this.cliApp = createCliApp({
|
|
52
|
-
onNotFound: this.onNotFound.bind(this),
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
if (!opts?.debug) {
|
|
56
|
-
setInfactLoggingOptions({
|
|
57
|
-
newInstance: false,
|
|
58
|
-
error: false,
|
|
59
|
-
warn: false,
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
async onNotFound() {
|
|
64
|
-
const pathParams = useCliContext().store('event').get('pathParams') || [];
|
|
65
|
-
const response = await defineMoostEventHandler({
|
|
66
|
-
loggerTitle: LOGGER_TITLE,
|
|
67
|
-
getIterceptorHandler: () => this.moost?.getGlobalInterceptorHandler(),
|
|
68
|
-
getControllerInstance: () => this.moost,
|
|
69
|
-
callControllerMethod: () => undefined,
|
|
70
|
-
logErrors: this.opts?.debug,
|
|
71
|
-
targetPath: '',
|
|
72
|
-
handlerType: '__SYSTEM__',
|
|
73
|
-
})();
|
|
74
|
-
if (response === undefined) {
|
|
75
|
-
this.cliApp.onUnknownCommand(pathParams);
|
|
76
|
-
}
|
|
77
|
-
return response;
|
|
78
|
-
}
|
|
79
|
-
onInit(moost) {
|
|
80
|
-
this.moost = moost;
|
|
81
|
-
const boolean = Object.entries(this.optionTypes)
|
|
82
|
-
.filter(([_key, val]) => val.length === 1 && val[0] === Boolean)
|
|
83
|
-
.map(([key, _val]) => key);
|
|
84
|
-
void this.cliApp.run(undefined, {
|
|
85
|
-
boolean,
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
bindHandler(opts) {
|
|
89
|
-
let fn;
|
|
90
|
-
for (const handler of opts.handlers) {
|
|
91
|
-
if (handler.type !== 'CLI') {
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
const path = typeof handler.path === 'string'
|
|
95
|
-
? handler.path
|
|
96
|
-
: typeof opts.method === 'string'
|
|
97
|
-
? opts.method
|
|
98
|
-
: '';
|
|
99
|
-
const prefix = opts.prefix.replace(/\s+/g, '/') || '';
|
|
100
|
-
const makePath = (p) => `${prefix}/${p}`
|
|
101
|
-
.replace(/\/\/+/g, '/')
|
|
102
|
-
.replace(/\/\\:/g, '\\:')
|
|
103
|
-
.replace(/^\/+/g, '');
|
|
104
|
-
const targetPath = makePath(path);
|
|
105
|
-
fn = defineMoostEventHandler({
|
|
106
|
-
contextType: CONTEXT_TYPE,
|
|
107
|
-
loggerTitle: LOGGER_TITLE,
|
|
108
|
-
getIterceptorHandler: opts.getIterceptorHandler,
|
|
109
|
-
getControllerInstance: opts.getInstance,
|
|
110
|
-
controllerMethod: opts.method,
|
|
111
|
-
resolveArgs: opts.resolveArgs,
|
|
112
|
-
logErrors: this.opts?.debug,
|
|
113
|
-
targetPath,
|
|
114
|
-
handlerType: handler.type,
|
|
115
|
-
});
|
|
116
|
-
const meta = getCliMate().read(opts.fakeInstance, opts.method);
|
|
117
|
-
const classMeta = getCliMate().read(opts.fakeInstance);
|
|
118
|
-
const cliOptions = new Map();
|
|
119
|
-
[
|
|
120
|
-
...(this.opts?.globalCliOptions?.length ? this.opts.globalCliOptions : []),
|
|
121
|
-
...(classMeta?.cliOptions || []),
|
|
122
|
-
...(meta?.params
|
|
123
|
-
? meta.params
|
|
124
|
-
.filter((param) => param.cliOptionsKeys?.length)
|
|
125
|
-
.map((param) => ({
|
|
126
|
-
keys: param.cliOptionsKeys,
|
|
127
|
-
value: typeof param.value === 'string' ? param.value : '',
|
|
128
|
-
description: param.description || '',
|
|
129
|
-
type: param.type,
|
|
130
|
-
}))
|
|
131
|
-
: []),
|
|
132
|
-
].forEach(o => cliOptions.set(o.keys[0], o));
|
|
133
|
-
const aliases = [];
|
|
134
|
-
if (meta?.cliAliases) {
|
|
135
|
-
for (const alias of meta.cliAliases) {
|
|
136
|
-
const targetPath = makePath(alias);
|
|
137
|
-
aliases.push(targetPath);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
const cliOptionsArray = Array.from(cliOptions.values());
|
|
141
|
-
cliOptionsArray.forEach(o => {
|
|
142
|
-
for (const key of o.keys) {
|
|
143
|
-
if (!this.optionTypes[key]) {
|
|
144
|
-
this.optionTypes[key] = [];
|
|
145
|
-
}
|
|
146
|
-
if (!this.optionTypes[key].includes(o.type)) {
|
|
147
|
-
this.optionTypes[key].push(o.type);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
const args = {};
|
|
152
|
-
meta?.params
|
|
153
|
-
.filter(p => p.paramSource === 'ROUTE' && p.description)
|
|
154
|
-
.forEach(p => (args[p.paramName] = p.description));
|
|
155
|
-
const routerBinding = this.cliApp.cli(targetPath, {
|
|
156
|
-
description: meta?.description || '',
|
|
157
|
-
options: cliOptionsArray,
|
|
158
|
-
args,
|
|
159
|
-
aliases,
|
|
160
|
-
examples: meta?.cliExamples || [],
|
|
161
|
-
handler: fn,
|
|
162
|
-
onRegister: (path, aliasType, route) => {
|
|
163
|
-
opts.register(handler, path, route?.getArgs() || routerBinding.getArgs());
|
|
164
|
-
if (this.opts?.debug) {
|
|
165
|
-
opts.logHandler(`${'[36m'}(${aliasTypes[aliasType]})${'[32m'}${path}`);
|
|
166
|
-
}
|
|
167
|
-
},
|
|
168
|
-
});
|
|
169
|
-
opts.register(handler, targetPath, routerBinding.getArgs());
|
|
170
|
-
}
|
|
171
|
-
}
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region packages/event-cli/src/event-cli.ts
|
|
47
|
+
function _define_property$1(obj, key, value) {
|
|
48
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
49
|
+
value,
|
|
50
|
+
enumerable: true,
|
|
51
|
+
configurable: true,
|
|
52
|
+
writable: true
|
|
53
|
+
});
|
|
54
|
+
else obj[key] = value;
|
|
55
|
+
return obj;
|
|
172
56
|
}
|
|
173
|
-
const
|
|
57
|
+
const LOGGER_TITLE = "moost-cli";
|
|
58
|
+
const CONTEXT_TYPE = "CLI";
|
|
59
|
+
var MoostCli = class {
|
|
60
|
+
async onNotFound() {
|
|
61
|
+
const pathParams = useCliContext().store("event").get("pathParams") || [];
|
|
62
|
+
const response = await defineMoostEventHandler({
|
|
63
|
+
loggerTitle: LOGGER_TITLE,
|
|
64
|
+
getIterceptorHandler: () => this.moost?.getGlobalInterceptorHandler(),
|
|
65
|
+
getControllerInstance: () => this.moost,
|
|
66
|
+
callControllerMethod: () => undefined,
|
|
67
|
+
logErrors: this.opts?.debug,
|
|
68
|
+
targetPath: "",
|
|
69
|
+
handlerType: "__SYSTEM__"
|
|
70
|
+
})();
|
|
71
|
+
if (response === undefined) this.cliApp.onUnknownCommand(pathParams);
|
|
72
|
+
return response;
|
|
73
|
+
}
|
|
74
|
+
onInit(moost) {
|
|
75
|
+
this.moost = moost;
|
|
76
|
+
const boolean = Object.entries(this.optionTypes).filter(([_key, val]) => val.length === 1 && val[0] === Boolean).map(([key, _val]) => key);
|
|
77
|
+
this.cliApp.run(undefined, { boolean });
|
|
78
|
+
}
|
|
79
|
+
bindHandler(opts) {
|
|
80
|
+
let fn;
|
|
81
|
+
for (const handler of opts.handlers) {
|
|
82
|
+
if (handler.type !== "CLI") continue;
|
|
83
|
+
const path = typeof handler.path === "string" ? handler.path : typeof opts.method === "string" ? opts.method : "";
|
|
84
|
+
const prefix = opts.prefix.replace(/\s+/g, "/") || "";
|
|
85
|
+
const makePath = (p) => `${prefix}/${p}`.replace(/\/\/+/g, "/").replace(/\/\\:/g, "\\:").replace(/^\/+/g, "");
|
|
86
|
+
const targetPath = makePath(path);
|
|
87
|
+
fn = defineMoostEventHandler({
|
|
88
|
+
contextType: CONTEXT_TYPE,
|
|
89
|
+
loggerTitle: LOGGER_TITLE,
|
|
90
|
+
getIterceptorHandler: opts.getIterceptorHandler,
|
|
91
|
+
getControllerInstance: opts.getInstance,
|
|
92
|
+
controllerMethod: opts.method,
|
|
93
|
+
resolveArgs: opts.resolveArgs,
|
|
94
|
+
logErrors: this.opts?.debug,
|
|
95
|
+
targetPath,
|
|
96
|
+
handlerType: handler.type
|
|
97
|
+
});
|
|
98
|
+
const meta = getCliMate().read(opts.fakeInstance, opts.method);
|
|
99
|
+
const classMeta = getCliMate().read(opts.fakeInstance);
|
|
100
|
+
const cliOptions = new Map();
|
|
101
|
+
[
|
|
102
|
+
...this.opts?.globalCliOptions?.length ? this.opts.globalCliOptions : [],
|
|
103
|
+
...classMeta?.cliOptions || [],
|
|
104
|
+
...meta?.params ? meta.params.filter((param) => param.cliOptionsKeys?.length).map((param) => ({
|
|
105
|
+
keys: param.cliOptionsKeys,
|
|
106
|
+
value: typeof param.value === "string" ? param.value : "",
|
|
107
|
+
description: param.description || "",
|
|
108
|
+
type: param.type
|
|
109
|
+
})) : []
|
|
110
|
+
].forEach((o) => cliOptions.set(o.keys[0], o));
|
|
111
|
+
const aliases = [];
|
|
112
|
+
if (meta?.cliAliases) for (const alias of meta.cliAliases) {
|
|
113
|
+
const targetPath$1 = makePath(alias);
|
|
114
|
+
aliases.push(targetPath$1);
|
|
115
|
+
}
|
|
116
|
+
const cliOptionsArray = Array.from(cliOptions.values());
|
|
117
|
+
cliOptionsArray.forEach((o) => {
|
|
118
|
+
for (const key of o.keys) {
|
|
119
|
+
if (!this.optionTypes[key]) this.optionTypes[key] = [];
|
|
120
|
+
if (!this.optionTypes[key].includes(o.type)) this.optionTypes[key].push(o.type);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
const args = {};
|
|
124
|
+
meta?.params.filter((p) => p.paramSource === "ROUTE" && p.description).forEach((p) => args[p.paramName] = p.description);
|
|
125
|
+
const routerBinding = this.cliApp.cli(targetPath, {
|
|
126
|
+
description: meta?.description || "",
|
|
127
|
+
options: cliOptionsArray,
|
|
128
|
+
args,
|
|
129
|
+
aliases,
|
|
130
|
+
examples: meta?.cliExamples || [],
|
|
131
|
+
handler: fn,
|
|
132
|
+
onRegister: (path$1, aliasType, route) => {
|
|
133
|
+
opts.register(handler, path$1, route?.getArgs() || routerBinding.getArgs());
|
|
134
|
+
if (this.opts?.debug) opts.logHandler(`${"\x1B[36m"}(${aliasTypes[aliasType]})${"\x1B[32m"}${path$1}`);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
opts.register(handler, targetPath, routerBinding.getArgs());
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
constructor(opts) {
|
|
141
|
+
_define_property$1(this, "opts", void 0);
|
|
142
|
+
_define_property$1(this, "name", void 0);
|
|
143
|
+
_define_property$1(this, "cliApp", void 0);
|
|
144
|
+
_define_property$1(this, "optionTypes", void 0);
|
|
145
|
+
_define_property$1(this, "moost", void 0);
|
|
146
|
+
this.opts = opts;
|
|
147
|
+
this.name = "cli";
|
|
148
|
+
this.optionTypes = {};
|
|
149
|
+
const cliAppOpts = opts?.wooksCli;
|
|
150
|
+
if (cliAppOpts && cliAppOpts instanceof WooksCli) this.cliApp = cliAppOpts;
|
|
151
|
+
else if (cliAppOpts) this.cliApp = createCliApp({
|
|
152
|
+
...cliAppOpts,
|
|
153
|
+
onNotFound: this.onNotFound.bind(this)
|
|
154
|
+
});
|
|
155
|
+
else this.cliApp = createCliApp({ onNotFound: this.onNotFound.bind(this) });
|
|
156
|
+
if (!opts?.debug) setInfactLoggingOptions({
|
|
157
|
+
newInstance: false,
|
|
158
|
+
error: false,
|
|
159
|
+
warn: false
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
const aliasTypes = [
|
|
164
|
+
"CLI",
|
|
165
|
+
"CLI-alias",
|
|
166
|
+
"CLI-alias*",
|
|
167
|
+
"CLI-alias*"
|
|
168
|
+
];
|
|
174
169
|
|
|
170
|
+
//#endregion
|
|
171
|
+
//#region packages/event-cli/src/interceptors/help.interceptor.ts
|
|
175
172
|
const cliHelpInterceptor = (opts) => defineInterceptorFn(() => {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
if (opts?.lookupLevel) {
|
|
184
|
-
const { getMethod } = useControllerContext();
|
|
185
|
-
if (!getMethod()) {
|
|
186
|
-
useCommandLookupHelp(opts.lookupLevel);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
173
|
+
try {
|
|
174
|
+
if (useAutoHelp(opts?.helpOptions, opts?.colors)) return "";
|
|
175
|
+
} catch (error) {}
|
|
176
|
+
if (opts?.lookupLevel) {
|
|
177
|
+
const { getMethod } = useControllerContext();
|
|
178
|
+
if (!getMethod()) useCommandLookupHelp(opts.lookupLevel);
|
|
179
|
+
}
|
|
189
180
|
}, TInterceptorPriority.BEFORE_ALL);
|
|
190
181
|
const CliHelpInterceptor = (...opts) => Intercept(cliHelpInterceptor(...opts));
|
|
191
182
|
|
|
192
|
-
|
|
183
|
+
//#endregion
|
|
184
|
+
//#region packages/event-cli/src/quick-cli.ts
|
|
185
|
+
function _define_property(obj, key, value) {
|
|
186
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
187
|
+
value,
|
|
188
|
+
enumerable: true,
|
|
189
|
+
configurable: true,
|
|
190
|
+
writable: true
|
|
191
|
+
});
|
|
192
|
+
else obj[key] = value;
|
|
193
|
+
return obj;
|
|
194
|
+
}
|
|
195
|
+
var CliApp = class extends Moost {
|
|
196
|
+
/**
|
|
197
|
+
* Registers one or more CLI controllers.
|
|
198
|
+
*
|
|
199
|
+
* (Shortcut for `registerControllers` method.)
|
|
200
|
+
*
|
|
201
|
+
* @param {...(object|Function|[string, object|Function])} controllers - List of controllers.
|
|
202
|
+
* Each controller can be an object, a class, or a tuple with a name and the controller.
|
|
203
|
+
* @returns {this} The CliApp instance for method chaining.
|
|
204
|
+
*/ controllers(...controllers) {
|
|
205
|
+
return this.registerControllers(...controllers);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Configures the CLI help options.
|
|
209
|
+
*
|
|
210
|
+
* This method sets the help options for the CLI. It ensures that colored output is enabled
|
|
211
|
+
* (unless explicitly disabled) and that the lookup level defaults to 3 if not provided.
|
|
212
|
+
*
|
|
213
|
+
* @param {THelpOptions} helpOpts - Help options configuration.
|
|
214
|
+
* @param {boolean} helpOpts.colors - Enable colored output.
|
|
215
|
+
* @param {number} helpOpts.lookupLevel - Level for help lookup.
|
|
216
|
+
* @returns {this} The CliApp instance for method chaining.
|
|
217
|
+
*/ useHelp(helpOpts) {
|
|
218
|
+
this._helpOpts = helpOpts;
|
|
219
|
+
if (this._helpOpts.colors !== false) this._helpOpts.colors = true;
|
|
220
|
+
if (typeof this._helpOpts.lookupLevel !== "number") this._helpOpts.lookupLevel = 3;
|
|
221
|
+
return this;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Sets the global CLI options.
|
|
225
|
+
*
|
|
226
|
+
* @param {TMoostCliOpts['globalCliOptions']} globalOpts - Global options for the CLI.
|
|
227
|
+
* @returns {this} The CliApp instance for method chaining.
|
|
228
|
+
*/ useOptions(globalOpts) {
|
|
229
|
+
this._globalOpts = globalOpts;
|
|
230
|
+
return this;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Starts the CLI application.
|
|
234
|
+
*
|
|
235
|
+
* This method creates a MoostCli instance using the configured help and global options,
|
|
236
|
+
* attaches it via the adapter, applies the CLI help interceptor (if help options are set),
|
|
237
|
+
* and then initializes the application.
|
|
238
|
+
*
|
|
239
|
+
* @returns {any} The result of the initialization process.
|
|
240
|
+
*/ start() {
|
|
241
|
+
const cli = new MoostCli({
|
|
242
|
+
wooksCli: { cliHelp: this._helpOpts ? {
|
|
243
|
+
name: this._helpOpts.name,
|
|
244
|
+
title: this._helpOpts.title,
|
|
245
|
+
maxWidth: this._helpOpts.maxWidth,
|
|
246
|
+
maxLeft: this._helpOpts.maxLeft,
|
|
247
|
+
mark: this._helpOpts.mark
|
|
248
|
+
} : undefined },
|
|
249
|
+
globalCliOptions: this._globalOpts
|
|
250
|
+
});
|
|
251
|
+
this.adapter(cli);
|
|
252
|
+
if (this._helpOpts) this.applyGlobalInterceptors(cliHelpInterceptor({
|
|
253
|
+
colors: this._helpOpts.colors,
|
|
254
|
+
lookupLevel: this._helpOpts.lookupLevel
|
|
255
|
+
}));
|
|
256
|
+
return this.init();
|
|
257
|
+
}
|
|
258
|
+
constructor(...args) {
|
|
259
|
+
super(...args), _define_property(this, "_helpOpts", void 0), _define_property(this, "_globalOpts", void 0);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
//#endregion
|
|
264
|
+
export { Cli, CliAlias, CliApp, CliExample, CliGlobalOption, CliHelpInterceptor, CliOption, Controller, Description, Intercept, MoostCli, Optional, Param, cliHelpInterceptor, useCliContext };
|
package/package.json
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moostjs/event-cli",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.23",
|
|
4
4
|
"description": "@moostjs/event-cli",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
8
9
|
"sideEffects": false,
|
|
9
10
|
"exports": {
|
|
10
11
|
"./package.json": "./package.json",
|
|
11
12
|
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
12
14
|
"import": "./dist/index.mjs",
|
|
13
|
-
"require": "./dist/index.cjs"
|
|
14
|
-
"types": "./dist/index.d.ts"
|
|
15
|
+
"require": "./dist/index.cjs"
|
|
15
16
|
}
|
|
16
17
|
},
|
|
17
18
|
"files": [
|
|
@@ -36,11 +37,17 @@
|
|
|
36
37
|
"url": "https://github.com/moostjs/moostjs/issues"
|
|
37
38
|
},
|
|
38
39
|
"homepage": "https://github.com/moostjs/moostjs/tree/main/packages/event-cli#readme",
|
|
39
|
-
"peerDependencies": {},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
41
|
+
"@wooksjs/event-cli": "^0.5.25",
|
|
42
|
+
"@wooksjs/event-core": "^0.5.25",
|
|
43
|
+
"wooks": "^0.5.25",
|
|
44
|
+
"moost": "^0.5.23"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"vitest": "^3.0.5"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"pub": "pnpm publish --access public",
|
|
51
|
+
"test": "vitest"
|
|
45
52
|
}
|
|
46
|
-
}
|
|
53
|
+
}
|