@navios/commander 1.0.0 → 1.2.0
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/CHANGELOG.md +35 -0
- package/README.md +22 -0
- package/dist/src/commander.factory.d.mts +59 -0
- package/dist/src/commander.factory.d.mts.map +1 -1
- package/dist/src/commands/help.command.d.mts.map +1 -1
- package/dist/src/decorators/command.decorator.d.mts +9 -2
- package/dist/src/decorators/command.decorator.d.mts.map +1 -1
- package/dist/src/legacy-compat/decorators/cli-module.decorator.d.mts +26 -0
- package/dist/src/legacy-compat/decorators/cli-module.decorator.d.mts.map +1 -0
- package/dist/src/legacy-compat/decorators/command.decorator.d.mts +34 -0
- package/dist/src/legacy-compat/decorators/command.decorator.d.mts.map +1 -0
- package/dist/src/legacy-compat/decorators/index.d.mts +3 -0
- package/dist/src/legacy-compat/decorators/index.d.mts.map +1 -0
- package/dist/src/legacy-compat/index.d.mts +28 -0
- package/dist/src/legacy-compat/index.d.mts.map +1 -0
- package/dist/src/overrides/help.command.d.mts +18 -0
- package/dist/src/overrides/help.command.d.mts.map +1 -0
- package/dist/src/tokens/help-command.token.d.mts +4 -0
- package/dist/src/tokens/help-command.token.d.mts.map +1 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/lib/cli-module.decorator-CkP22084.cjs +71 -0
- package/lib/cli-module.decorator-CkP22084.cjs.map +1 -0
- package/lib/cli-module.decorator-DGuGfpex.d.mts +411 -0
- package/lib/cli-module.decorator-DGuGfpex.d.mts.map +1 -0
- package/lib/cli-module.decorator-DVl8009Q.d.cts +213 -0
- package/lib/cli-module.decorator-DVl8009Q.d.cts.map +1 -0
- package/lib/cli-module.decorator-UGbtkRYc.mjs +66 -0
- package/lib/cli-module.decorator-UGbtkRYc.mjs.map +1 -0
- package/lib/command.decorator-DVLSAqYZ.mjs +135 -0
- package/lib/command.decorator-DVLSAqYZ.mjs.map +1 -0
- package/lib/command.decorator-UrNJmQN0.cjs +176 -0
- package/lib/command.decorator-UrNJmQN0.cjs.map +1 -0
- package/lib/help-command.token-C0Kgj60o.mjs +427 -0
- package/lib/help-command.token-C0Kgj60o.mjs.map +1 -0
- package/lib/help-command.token-CMWYI6em.cjs +438 -0
- package/lib/help-command.token-CMWYI6em.cjs.map +1 -0
- package/lib/help.command-DQyv6ali.cjs +317 -0
- package/lib/help.command-DQyv6ali.cjs.map +1 -0
- package/lib/help.command-dtZbhq0w.mjs +318 -0
- package/lib/help.command-dtZbhq0w.mjs.map +1 -0
- package/lib/index.cjs +45 -627
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +139 -281
- package/lib/index.d.cts.map +1 -1
- package/lib/index.d.mts +62 -402
- package/lib/index.d.mts.map +1 -1
- package/lib/index.mjs +30 -612
- package/lib/index.mjs.map +1 -1
- package/lib/legacy-compat/index.cjs +108 -0
- package/lib/legacy-compat/index.cjs.map +1 -0
- package/lib/legacy-compat/index.d.cts +63 -0
- package/lib/legacy-compat/index.d.cts.map +1 -0
- package/lib/legacy-compat/index.d.mts +63 -0
- package/lib/legacy-compat/index.d.mts.map +1 -0
- package/lib/legacy-compat/index.mjs +101 -0
- package/lib/legacy-compat/index.mjs.map +1 -0
- package/package.json +20 -3
- package/src/commander.factory.mts +107 -8
- package/src/commands/help.command.mts +4 -3
- package/src/decorators/command.decorator.mts +17 -8
- package/src/legacy-compat/decorators/cli-module.decorator.mts +46 -0
- package/src/legacy-compat/decorators/command.decorator.mts +46 -0
- package/src/legacy-compat/decorators/index.mts +2 -0
- package/src/legacy-compat/index.mts +31 -0
- package/src/overrides/help.command.mts +40 -0
- package/src/tokens/help-command.token.mts +5 -0
- package/tsconfig.json +0 -3
- package/tsconfig.spec.json +1 -1
- package/tsdown.config.mts +1 -1
- package/dist/src/commander.application.d.mts +0 -147
- package/dist/src/commander.application.d.mts.map +0 -1
- package/dist/src/metadata/cli-module.metadata.d.mts +0 -60
- package/dist/src/metadata/cli-module.metadata.d.mts.map +0 -1
- package/dist/src/services/module-loader.service.d.mts +0 -74
- package/dist/src/services/module-loader.service.d.mts.map +0 -1
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
import { Injectable, InjectionToken } from "@navios/core";
|
|
2
|
+
|
|
3
|
+
//#region src/services/command-registry.service.mts
|
|
4
|
+
function applyDecs2203RFactory() {
|
|
5
|
+
function createAddInitializerMethod(initializers, decoratorFinishedRef) {
|
|
6
|
+
return function addInitializer(initializer) {
|
|
7
|
+
assertNotFinished(decoratorFinishedRef, "addInitializer");
|
|
8
|
+
assertCallable(initializer, "An initializer");
|
|
9
|
+
initializers.push(initializer);
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value) {
|
|
13
|
+
var kindStr;
|
|
14
|
+
switch (kind) {
|
|
15
|
+
case 1:
|
|
16
|
+
kindStr = "accessor";
|
|
17
|
+
break;
|
|
18
|
+
case 2:
|
|
19
|
+
kindStr = "method";
|
|
20
|
+
break;
|
|
21
|
+
case 3:
|
|
22
|
+
kindStr = "getter";
|
|
23
|
+
break;
|
|
24
|
+
case 4:
|
|
25
|
+
kindStr = "setter";
|
|
26
|
+
break;
|
|
27
|
+
default: kindStr = "field";
|
|
28
|
+
}
|
|
29
|
+
var ctx = {
|
|
30
|
+
kind: kindStr,
|
|
31
|
+
name: isPrivate ? "#" + name : name,
|
|
32
|
+
static: isStatic,
|
|
33
|
+
private: isPrivate,
|
|
34
|
+
metadata
|
|
35
|
+
};
|
|
36
|
+
var decoratorFinishedRef = { v: false };
|
|
37
|
+
ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef);
|
|
38
|
+
var get, set;
|
|
39
|
+
if (kind === 0) if (isPrivate) {
|
|
40
|
+
get = desc.get;
|
|
41
|
+
set = desc.set;
|
|
42
|
+
} else {
|
|
43
|
+
get = function() {
|
|
44
|
+
return this[name];
|
|
45
|
+
};
|
|
46
|
+
set = function(v) {
|
|
47
|
+
this[name] = v;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
else if (kind === 2) get = function() {
|
|
51
|
+
return desc.value;
|
|
52
|
+
};
|
|
53
|
+
else {
|
|
54
|
+
if (kind === 1 || kind === 3) get = function() {
|
|
55
|
+
return desc.get.call(this);
|
|
56
|
+
};
|
|
57
|
+
if (kind === 1 || kind === 4) set = function(v) {
|
|
58
|
+
desc.set.call(this, v);
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
ctx.access = get && set ? {
|
|
62
|
+
get,
|
|
63
|
+
set
|
|
64
|
+
} : get ? { get } : { set };
|
|
65
|
+
try {
|
|
66
|
+
return dec(value, ctx);
|
|
67
|
+
} finally {
|
|
68
|
+
decoratorFinishedRef.v = true;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function assertNotFinished(decoratorFinishedRef, fnName) {
|
|
72
|
+
if (decoratorFinishedRef.v) throw new Error("attempted to call " + fnName + " after decoration was finished");
|
|
73
|
+
}
|
|
74
|
+
function assertCallable(fn, hint) {
|
|
75
|
+
if (typeof fn !== "function") throw new TypeError(hint + " must be a function");
|
|
76
|
+
}
|
|
77
|
+
function assertValidReturnValue(kind, value) {
|
|
78
|
+
var type = typeof value;
|
|
79
|
+
if (kind === 1) {
|
|
80
|
+
if (type !== "object" || value === null) throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");
|
|
81
|
+
if (value.get !== void 0) assertCallable(value.get, "accessor.get");
|
|
82
|
+
if (value.set !== void 0) assertCallable(value.set, "accessor.set");
|
|
83
|
+
if (value.init !== void 0) assertCallable(value.init, "accessor.init");
|
|
84
|
+
} else if (type !== "function") {
|
|
85
|
+
var hint;
|
|
86
|
+
if (kind === 0) hint = "field";
|
|
87
|
+
else if (kind === 10) hint = "class";
|
|
88
|
+
else hint = "method";
|
|
89
|
+
throw new TypeError(hint + " decorators must return a function or void 0");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata) {
|
|
93
|
+
var decs = decInfo[0];
|
|
94
|
+
var desc, init, value;
|
|
95
|
+
if (isPrivate) if (kind === 0 || kind === 1) desc = {
|
|
96
|
+
get: decInfo[3],
|
|
97
|
+
set: decInfo[4]
|
|
98
|
+
};
|
|
99
|
+
else if (kind === 3) desc = { get: decInfo[3] };
|
|
100
|
+
else if (kind === 4) desc = { set: decInfo[3] };
|
|
101
|
+
else desc = { value: decInfo[3] };
|
|
102
|
+
else if (kind !== 0) desc = Object.getOwnPropertyDescriptor(base, name);
|
|
103
|
+
if (kind === 1) value = {
|
|
104
|
+
get: desc.get,
|
|
105
|
+
set: desc.set
|
|
106
|
+
};
|
|
107
|
+
else if (kind === 2) value = desc.value;
|
|
108
|
+
else if (kind === 3) value = desc.get;
|
|
109
|
+
else if (kind === 4) value = desc.set;
|
|
110
|
+
var newValue, get, set;
|
|
111
|
+
if (typeof decs === "function") {
|
|
112
|
+
newValue = memberDec(decs, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
|
|
113
|
+
if (newValue !== void 0) {
|
|
114
|
+
assertValidReturnValue(kind, newValue);
|
|
115
|
+
if (kind === 0) init = newValue;
|
|
116
|
+
else if (kind === 1) {
|
|
117
|
+
init = newValue.init;
|
|
118
|
+
get = newValue.get || value.get;
|
|
119
|
+
set = newValue.set || value.set;
|
|
120
|
+
value = {
|
|
121
|
+
get,
|
|
122
|
+
set
|
|
123
|
+
};
|
|
124
|
+
} else value = newValue;
|
|
125
|
+
}
|
|
126
|
+
} else for (var i = decs.length - 1; i >= 0; i--) {
|
|
127
|
+
var dec = decs[i];
|
|
128
|
+
newValue = memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
|
|
129
|
+
if (newValue !== void 0) {
|
|
130
|
+
assertValidReturnValue(kind, newValue);
|
|
131
|
+
var newInit;
|
|
132
|
+
if (kind === 0) newInit = newValue;
|
|
133
|
+
else if (kind === 1) {
|
|
134
|
+
newInit = newValue.init;
|
|
135
|
+
get = newValue.get || value.get;
|
|
136
|
+
set = newValue.set || value.set;
|
|
137
|
+
value = {
|
|
138
|
+
get,
|
|
139
|
+
set
|
|
140
|
+
};
|
|
141
|
+
} else value = newValue;
|
|
142
|
+
if (newInit !== void 0) if (init === void 0) init = newInit;
|
|
143
|
+
else if (typeof init === "function") init = [init, newInit];
|
|
144
|
+
else init.push(newInit);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (kind === 0 || kind === 1) {
|
|
148
|
+
if (init === void 0) init = function(instance, init$1) {
|
|
149
|
+
return init$1;
|
|
150
|
+
};
|
|
151
|
+
else if (typeof init !== "function") {
|
|
152
|
+
var ownInitializers = init;
|
|
153
|
+
init = function(instance, init$1) {
|
|
154
|
+
var value$1 = init$1;
|
|
155
|
+
for (var i$1 = 0; i$1 < ownInitializers.length; i$1++) value$1 = ownInitializers[i$1].call(instance, value$1);
|
|
156
|
+
return value$1;
|
|
157
|
+
};
|
|
158
|
+
} else {
|
|
159
|
+
var originalInitializer = init;
|
|
160
|
+
init = function(instance, init$1) {
|
|
161
|
+
return originalInitializer.call(instance, init$1);
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
ret.push(init);
|
|
165
|
+
}
|
|
166
|
+
if (kind !== 0) {
|
|
167
|
+
if (kind === 1) {
|
|
168
|
+
desc.get = value.get;
|
|
169
|
+
desc.set = value.set;
|
|
170
|
+
} else if (kind === 2) desc.value = value;
|
|
171
|
+
else if (kind === 3) desc.get = value;
|
|
172
|
+
else if (kind === 4) desc.set = value;
|
|
173
|
+
if (isPrivate) if (kind === 1) {
|
|
174
|
+
ret.push(function(instance, args) {
|
|
175
|
+
return value.get.call(instance, args);
|
|
176
|
+
});
|
|
177
|
+
ret.push(function(instance, args) {
|
|
178
|
+
return value.set.call(instance, args);
|
|
179
|
+
});
|
|
180
|
+
} else if (kind === 2) ret.push(value);
|
|
181
|
+
else ret.push(function(instance, args) {
|
|
182
|
+
return value.call(instance, args);
|
|
183
|
+
});
|
|
184
|
+
else Object.defineProperty(base, name, desc);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function applyMemberDecs(Class, decInfos, metadata) {
|
|
188
|
+
var ret = [];
|
|
189
|
+
var protoInitializers;
|
|
190
|
+
var staticInitializers;
|
|
191
|
+
var existingProtoNonFields = /* @__PURE__ */ new Map();
|
|
192
|
+
var existingStaticNonFields = /* @__PURE__ */ new Map();
|
|
193
|
+
for (var i = 0; i < decInfos.length; i++) {
|
|
194
|
+
var decInfo = decInfos[i];
|
|
195
|
+
if (!Array.isArray(decInfo)) continue;
|
|
196
|
+
var kind = decInfo[1];
|
|
197
|
+
var name = decInfo[2];
|
|
198
|
+
var isPrivate = decInfo.length > 3;
|
|
199
|
+
var isStatic = kind >= 5;
|
|
200
|
+
var base;
|
|
201
|
+
var initializers;
|
|
202
|
+
if (isStatic) {
|
|
203
|
+
base = Class;
|
|
204
|
+
kind = kind - 5;
|
|
205
|
+
staticInitializers = staticInitializers || [];
|
|
206
|
+
initializers = staticInitializers;
|
|
207
|
+
} else {
|
|
208
|
+
base = Class.prototype;
|
|
209
|
+
protoInitializers = protoInitializers || [];
|
|
210
|
+
initializers = protoInitializers;
|
|
211
|
+
}
|
|
212
|
+
if (kind !== 0 && !isPrivate) {
|
|
213
|
+
var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields;
|
|
214
|
+
var existingKind = existingNonFields.get(name) || 0;
|
|
215
|
+
if (existingKind === true || existingKind === 3 && kind !== 4 || existingKind === 4 && kind !== 3) throw new Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: " + name);
|
|
216
|
+
else if (!existingKind && kind > 2) existingNonFields.set(name, kind);
|
|
217
|
+
else existingNonFields.set(name, true);
|
|
218
|
+
}
|
|
219
|
+
applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata);
|
|
220
|
+
}
|
|
221
|
+
pushInitializers(ret, protoInitializers);
|
|
222
|
+
pushInitializers(ret, staticInitializers);
|
|
223
|
+
return ret;
|
|
224
|
+
}
|
|
225
|
+
function pushInitializers(ret, initializers) {
|
|
226
|
+
if (initializers) ret.push(function(instance) {
|
|
227
|
+
for (var i = 0; i < initializers.length; i++) initializers[i].call(instance);
|
|
228
|
+
return instance;
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
function applyClassDecs(targetClass, classDecs, metadata) {
|
|
232
|
+
if (classDecs.length > 0) {
|
|
233
|
+
var initializers = [];
|
|
234
|
+
var newClass = targetClass;
|
|
235
|
+
var name = targetClass.name;
|
|
236
|
+
for (var i = classDecs.length - 1; i >= 0; i--) {
|
|
237
|
+
var decoratorFinishedRef = { v: false };
|
|
238
|
+
try {
|
|
239
|
+
var nextNewClass = classDecs[i](newClass, {
|
|
240
|
+
kind: "class",
|
|
241
|
+
name,
|
|
242
|
+
addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef),
|
|
243
|
+
metadata
|
|
244
|
+
});
|
|
245
|
+
} finally {
|
|
246
|
+
decoratorFinishedRef.v = true;
|
|
247
|
+
}
|
|
248
|
+
if (nextNewClass !== void 0) {
|
|
249
|
+
assertValidReturnValue(10, nextNewClass);
|
|
250
|
+
newClass = nextNewClass;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return [defineMetadata(newClass, metadata), function() {
|
|
254
|
+
for (var i$1 = 0; i$1 < initializers.length; i$1++) initializers[i$1].call(newClass);
|
|
255
|
+
}];
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
function defineMetadata(Class, metadata) {
|
|
259
|
+
return Object.defineProperty(Class, Symbol.metadata || Symbol.for("Symbol.metadata"), {
|
|
260
|
+
configurable: true,
|
|
261
|
+
enumerable: true,
|
|
262
|
+
value: metadata
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
return function applyDecs2203R(targetClass, memberDecs, classDecs, parentClass) {
|
|
266
|
+
if (parentClass !== void 0) var parentMetadata = parentClass[Symbol.metadata || Symbol.for("Symbol.metadata")];
|
|
267
|
+
var metadata = Object.create(parentMetadata === void 0 ? null : parentMetadata);
|
|
268
|
+
var e = applyMemberDecs(targetClass, memberDecs, metadata);
|
|
269
|
+
if (!classDecs.length) defineMetadata(targetClass, metadata);
|
|
270
|
+
return {
|
|
271
|
+
e,
|
|
272
|
+
get c() {
|
|
273
|
+
return applyClassDecs(targetClass, classDecs, metadata);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentClass) {
|
|
279
|
+
return (_apply_decs_2203_r = applyDecs2203RFactory())(targetClass, memberDecs, classDecs, parentClass);
|
|
280
|
+
}
|
|
281
|
+
var _dec, _initClass;
|
|
282
|
+
let _CommandRegistryService;
|
|
283
|
+
_dec = Injectable();
|
|
284
|
+
var CommandRegistryService = class {
|
|
285
|
+
static {
|
|
286
|
+
({c: [_CommandRegistryService, _initClass]} = _apply_decs_2203_r(this, [], [_dec]));
|
|
287
|
+
}
|
|
288
|
+
commands = /* @__PURE__ */ new Map();
|
|
289
|
+
/**
|
|
290
|
+
* Register a command with its metadata.
|
|
291
|
+
*
|
|
292
|
+
* @param path - The command path (e.g., 'greet', 'user:create')
|
|
293
|
+
* @param command - The registered command data
|
|
294
|
+
* @throws Error if a command with the same path is already registered
|
|
295
|
+
*/ register(path, command) {
|
|
296
|
+
if (this.commands.has(path)) throw new Error(`[Navios Commander] Duplicate command path: ${path}`);
|
|
297
|
+
this.commands.set(path, command);
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Get a command by its path.
|
|
301
|
+
*
|
|
302
|
+
* @param path - The command path
|
|
303
|
+
* @returns The registered command or undefined if not found
|
|
304
|
+
*/ getByPath(path) {
|
|
305
|
+
return this.commands.get(path);
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Get all registered commands.
|
|
309
|
+
*
|
|
310
|
+
* @returns Map of path to registered command
|
|
311
|
+
*/ getAll() {
|
|
312
|
+
return new Map(this.commands);
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Get all registered commands as an array of path and class pairs.
|
|
316
|
+
* Useful for listing available commands.
|
|
317
|
+
*
|
|
318
|
+
* @returns Array of objects containing path and class
|
|
319
|
+
*/ getAllAsArray() {
|
|
320
|
+
const result = [];
|
|
321
|
+
for (const [path, { class: cls }] of this.commands) result.push({
|
|
322
|
+
path,
|
|
323
|
+
class: cls
|
|
324
|
+
});
|
|
325
|
+
return result;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Formats help text listing all available commands with descriptions.
|
|
329
|
+
*
|
|
330
|
+
* @returns Formatted string listing all commands
|
|
331
|
+
*/ formatCommandList() {
|
|
332
|
+
const lines = ["Available commands:", ""];
|
|
333
|
+
for (const [path, { metadata }] of this.commands) {
|
|
334
|
+
const description = metadata.description;
|
|
335
|
+
if (description) lines.push(` ${path.padEnd(20)} ${description}`);
|
|
336
|
+
else lines.push(` ${path}`);
|
|
337
|
+
}
|
|
338
|
+
return lines.join("\n");
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Formats help text for a specific command.
|
|
342
|
+
*
|
|
343
|
+
* @param commandPath - The command path to show help for
|
|
344
|
+
* @returns Formatted string with command help
|
|
345
|
+
*/ formatCommandHelp(commandPath) {
|
|
346
|
+
const command = this.commands.get(commandPath);
|
|
347
|
+
if (!command) return `Unknown command: ${commandPath}\n\n${this.formatCommandList()}`;
|
|
348
|
+
const { metadata } = command;
|
|
349
|
+
const lines = [];
|
|
350
|
+
lines.push(`Usage: ${metadata.path} [options]`);
|
|
351
|
+
lines.push("");
|
|
352
|
+
if (metadata.description) {
|
|
353
|
+
lines.push(metadata.description);
|
|
354
|
+
lines.push("");
|
|
355
|
+
}
|
|
356
|
+
if (metadata.optionsSchema) {
|
|
357
|
+
lines.push("Options:");
|
|
358
|
+
try {
|
|
359
|
+
const shape = metadata.optionsSchema.def.shape;
|
|
360
|
+
if (shape && typeof shape === "object") for (const [key, fieldSchema] of Object.entries(shape)) {
|
|
361
|
+
const optionFlag = `--${key.replace(/([A-Z])/g, "-$1").toLowerCase()}`;
|
|
362
|
+
const fieldType = this.getSchemaTypeName(fieldSchema);
|
|
363
|
+
lines.push(` ${optionFlag.padEnd(20)} ${fieldType}`);
|
|
364
|
+
}
|
|
365
|
+
} catch {}
|
|
366
|
+
}
|
|
367
|
+
return lines.join("\n");
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Gets a human-readable type name from a Zod schema.
|
|
371
|
+
*/ getSchemaTypeName(schema) {
|
|
372
|
+
try {
|
|
373
|
+
let currentSchema = schema;
|
|
374
|
+
let typeName = currentSchema?.def?.type;
|
|
375
|
+
let isOptional = false;
|
|
376
|
+
let defaultValue;
|
|
377
|
+
while (typeName === "optional" || typeName === "default") {
|
|
378
|
+
if (typeName === "optional") isOptional = true;
|
|
379
|
+
if (typeName === "default") {
|
|
380
|
+
isOptional = true;
|
|
381
|
+
defaultValue = currentSchema?.def?.defaultValue?.();
|
|
382
|
+
}
|
|
383
|
+
currentSchema = currentSchema?.def?.innerType;
|
|
384
|
+
typeName = currentSchema?.def?.type;
|
|
385
|
+
}
|
|
386
|
+
let result = `<${typeName || "unknown"}>`;
|
|
387
|
+
if (defaultValue !== void 0) result += ` (default: ${JSON.stringify(defaultValue)})`;
|
|
388
|
+
else if (isOptional) result += " (optional)";
|
|
389
|
+
const description = this.getSchemaMeta(schema)?.description;
|
|
390
|
+
if (description) result += ` - ${description}`;
|
|
391
|
+
return result;
|
|
392
|
+
} catch {
|
|
393
|
+
return "<unknown>";
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Gets metadata from a Zod schema, traversing innerType if needed.
|
|
398
|
+
* Zod v4 stores meta at the outermost layer when .meta() is called last,
|
|
399
|
+
* or in innerType when .meta() is called before .optional()/.default().
|
|
400
|
+
*/ getSchemaMeta(schema) {
|
|
401
|
+
try {
|
|
402
|
+
const directMeta = schema.meta?.();
|
|
403
|
+
if (directMeta) return directMeta;
|
|
404
|
+
const innerType = schema.def?.innerType;
|
|
405
|
+
if (innerType) return this.getSchemaMeta(innerType);
|
|
406
|
+
return;
|
|
407
|
+
} catch {
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Clear all registered commands.
|
|
413
|
+
*/ clear() {
|
|
414
|
+
this.commands.clear();
|
|
415
|
+
}
|
|
416
|
+
static {
|
|
417
|
+
_initClass();
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
//#endregion
|
|
422
|
+
//#region src/tokens/help-command.token.mts
|
|
423
|
+
const HelpCommandToken = InjectionToken.create("HelpCommand");
|
|
424
|
+
|
|
425
|
+
//#endregion
|
|
426
|
+
export { _CommandRegistryService as n, HelpCommandToken as t };
|
|
427
|
+
//# sourceMappingURL=help-command.token-C0Kgj60o.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"help-command.token-C0Kgj60o.mjs","names":["Injectable","CommandRegistryService","commands","Map","register","path","command","has","Error","set","getByPath","get","getAll","getAllAsArray","result","class","cls","push","formatCommandList","lines","metadata","description","padEnd","join","formatCommandHelp","commandPath","optionsSchema","shape","def","key","fieldSchema","Object","entries","kebabKey","replace","toLowerCase","optionFlag","fieldType","getSchemaTypeName","schema","currentSchema","typeName","type","isOptional","defaultValue","innerType","undefined","JSON","stringify","getSchemaMeta","directMeta","meta","clear","InjectionToken","HelpCommandToken","create"],"sources":["../src/services/command-registry.service.mts","../src/tokens/help-command.token.mts"],"sourcesContent":["import type { ClassType } from '@navios/core'\n\nimport { Injectable } from '@navios/core'\n\nimport type { CommandMetadata } from '../metadata/index.mjs'\n\n/**\n * Represents a registered command with its metadata and module information.\n *\n * @public\n */\nexport interface RegisteredCommand {\n /**\n * The command class\n */\n class: ClassType\n /**\n * The command metadata from @Command decorator\n */\n metadata: CommandMetadata\n /**\n * Name of the module this command belongs to\n */\n moduleName: string\n}\n\n/**\n * Service for registering and looking up CLI commands.\n * Used internally by the CLI adapter to manage discovered commands.\n *\n * @public\n */\n@Injectable()\nexport class CommandRegistryService {\n private commands = new Map<string, RegisteredCommand>()\n\n /**\n * Register a command with its metadata.\n *\n * @param path - The command path (e.g., 'greet', 'user:create')\n * @param command - The registered command data\n * @throws Error if a command with the same path is already registered\n */\n register(path: string, command: RegisteredCommand): void {\n if (this.commands.has(path)) {\n throw new Error(`[Navios Commander] Duplicate command path: ${path}`)\n }\n this.commands.set(path, command)\n }\n\n /**\n * Get a command by its path.\n *\n * @param path - The command path\n * @returns The registered command or undefined if not found\n */\n getByPath(path: string): RegisteredCommand | undefined {\n return this.commands.get(path)\n }\n\n /**\n * Get all registered commands.\n *\n * @returns Map of path to registered command\n */\n getAll(): Map<string, RegisteredCommand> {\n return new Map(this.commands)\n }\n\n /**\n * Get all registered commands as an array of path and class pairs.\n * Useful for listing available commands.\n *\n * @returns Array of objects containing path and class\n */\n getAllAsArray(): Array<{ path: string; class: ClassType }> {\n const result: Array<{ path: string; class: ClassType }> = []\n for (const [path, { class: cls }] of this.commands) {\n result.push({ path, class: cls })\n }\n return result\n }\n\n /**\n * Formats help text listing all available commands with descriptions.\n *\n * @returns Formatted string listing all commands\n */\n formatCommandList(): string {\n const lines = ['Available commands:', '']\n for (const [path, { metadata }] of this.commands) {\n const description = metadata.description\n if (description) {\n lines.push(` ${path.padEnd(20)} ${description}`)\n } else {\n lines.push(` ${path}`)\n }\n }\n return lines.join('\\n')\n }\n\n /**\n * Formats help text for a specific command.\n *\n * @param commandPath - The command path to show help for\n * @returns Formatted string with command help\n */\n formatCommandHelp(commandPath: string): string {\n const command = this.commands.get(commandPath)\n if (!command) {\n return `Unknown command: ${commandPath}\\n\\n${this.formatCommandList()}`\n }\n\n const { metadata } = command\n const lines: string[] = []\n\n lines.push(`Usage: ${metadata.path} [options]`)\n lines.push('')\n\n if (metadata.description) {\n lines.push(metadata.description)\n lines.push('')\n }\n\n // Extract options from schema if available\n if (metadata.optionsSchema) {\n lines.push('Options:')\n try {\n const shape = metadata.optionsSchema.def.shape\n if (shape && typeof shape === 'object') {\n for (const [key, fieldSchema] of Object.entries(shape)) {\n const kebabKey = key.replace(/([A-Z])/g, '-$1').toLowerCase()\n const optionFlag = `--${kebabKey}`\n const fieldType = this.getSchemaTypeName(fieldSchema as any)\n lines.push(` ${optionFlag.padEnd(20)} ${fieldType}`)\n }\n }\n } catch {\n // Schema introspection failed, skip options\n }\n }\n\n return lines.join('\\n')\n }\n\n /**\n * Gets a human-readable type name from a Zod schema.\n */\n private getSchemaTypeName(schema: any): string {\n try {\n let currentSchema = schema\n let typeName = currentSchema?.def?.type\n let isOptional = false\n let defaultValue: any\n\n // Unwrap optional/default wrappers\n while (typeName === 'optional' || typeName === 'default') {\n if (typeName === 'optional') {\n isOptional = true\n }\n if (typeName === 'default') {\n isOptional = true\n defaultValue = currentSchema?.def?.defaultValue?.()\n }\n currentSchema = currentSchema?.def?.innerType\n typeName = currentSchema?.def?.type\n }\n\n let result = `<${typeName || 'unknown'}>`\n if (defaultValue !== undefined) {\n result += ` (default: ${JSON.stringify(defaultValue)})`\n } else if (isOptional) {\n result += ' (optional)'\n }\n\n // Get description from meta() if available\n const description = this.getSchemaMeta(schema)?.description\n if (description) {\n result += ` - ${description}`\n }\n\n return result\n } catch {\n return '<unknown>'\n }\n }\n\n /**\n * Gets metadata from a Zod schema, traversing innerType if needed.\n * Zod v4 stores meta at the outermost layer when .meta() is called last,\n * or in innerType when .meta() is called before .optional()/.default().\n */\n private getSchemaMeta(schema: any): Record<string, unknown> | undefined {\n try {\n // First check direct meta (when .meta() is called last in chain)\n const directMeta = schema.meta?.()\n if (directMeta) return directMeta\n\n // Check innerType for wrapped schemas (optional, default, etc.)\n const innerType = schema.def?.innerType\n if (innerType) {\n return this.getSchemaMeta(innerType)\n }\n\n return undefined\n } catch {\n return undefined\n }\n }\n\n /**\n * Clear all registered commands.\n */\n clear(): void {\n this.commands.clear()\n }\n}\n","import { InjectionToken } from '@navios/core'\n\nimport type { HelpCommand } from '../overrides/help.command.mjs'\n\nexport const HelpCommandToken = InjectionToken.create<HelpCommand>('HelpCommand')\n"],"mappings":"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"}
|