bob-core 2.0.0-beta.7 → 2.0.0-beta.8
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/cjs/index.js +4 -4
- package/dist/cjs/{package-COb9yxnx.cjs → package-BaL-l6OV.cjs} +1 -1
- package/dist/cjs/src/Cli.d.ts +3 -2
- package/dist/cjs/src/CommandRegistry.d.ts +2 -4
- package/dist/esm/index.js +299 -300
- package/dist/esm/{package-D2VbJdgN.js → package-ODN3iB6L.js} +1 -1
- package/dist/esm/src/Cli.d.ts +3 -2
- package/dist/esm/src/CommandRegistry.d.ts +2 -4
- package/package.json +1 -1
package/dist/esm/index.js
CHANGED
|
@@ -3,25 +3,25 @@ import l from "chalk";
|
|
|
3
3
|
import y from "prompts";
|
|
4
4
|
import * as D from "node:fs";
|
|
5
5
|
import R from "path";
|
|
6
|
-
import * as
|
|
6
|
+
import * as I from "string-similarity";
|
|
7
7
|
class f extends Error {
|
|
8
8
|
}
|
|
9
9
|
class U extends f {
|
|
10
|
-
constructor(
|
|
11
|
-
let
|
|
12
|
-
|
|
10
|
+
constructor(t) {
|
|
11
|
+
let e = `Argument "${t.param}" value is invalid.`;
|
|
12
|
+
t.reason ? e += ` Reason: ${t.reason}` : e += ` Value: "${t.value}"`, super(e), this.param = t;
|
|
13
13
|
}
|
|
14
|
-
pretty(
|
|
15
|
-
|
|
14
|
+
pretty(t) {
|
|
15
|
+
t.log(l` {white.bgRed ERROR } Argument {bold.yellow ${this.param.param}} value is invalid. `), (this.param.value || this.param.reason) && t.log(""), this.param.value && t.log(l` {blue Value}: ${this.param.value}`), this.param.reason && t.log(l` {yellow Reason}: ${this.param.reason}`);
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
class A extends f {
|
|
19
|
-
constructor(
|
|
20
|
-
let
|
|
21
|
-
|
|
19
|
+
constructor(t) {
|
|
20
|
+
let e = `Option "${t.option}" value is invalid.`;
|
|
21
|
+
t.reason ? e += ` Reason: ${t.reason}` : e += ` Value: "${t.value}"`, super(e), this.param = t;
|
|
22
22
|
}
|
|
23
|
-
pretty(
|
|
24
|
-
|
|
23
|
+
pretty(t) {
|
|
24
|
+
t.log(l` {white.bgRed ERROR } Option {bold.yellow ${this.param.option}} value is invalid. `), (this.param.value || this.param.reason) && t.log(""), this.param.value && t.log(l` {blue Value}: ${this.param.value}`), this.param.reason && t.log(l` {yellow Reason}: ${this.param.reason}`);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
function x(i) {
|
|
@@ -33,20 +33,20 @@ function x(i) {
|
|
|
33
33
|
}
|
|
34
34
|
throw new Error("Invalid option type: " + i);
|
|
35
35
|
}
|
|
36
|
-
function
|
|
36
|
+
function F(i) {
|
|
37
37
|
return !Array.isArray(i) && typeof i == "object" && i.type ? i.default !== void 0 ? i.default : x(i.type) : x(i);
|
|
38
38
|
}
|
|
39
39
|
function g(i) {
|
|
40
40
|
return typeof i == "string" || Array.isArray(i) ? {
|
|
41
41
|
type: i,
|
|
42
|
-
default:
|
|
42
|
+
default: F(i),
|
|
43
43
|
description: "",
|
|
44
44
|
alias: [],
|
|
45
45
|
required: !1,
|
|
46
46
|
variadic: !1
|
|
47
47
|
} : {
|
|
48
48
|
type: i.type,
|
|
49
|
-
default: i.default ??
|
|
49
|
+
default: i.default ?? F(i.type),
|
|
50
50
|
description: i.description ?? "",
|
|
51
51
|
alias: i.alias ? Array.isArray(i.alias) ? i.alias : [i.alias] : [],
|
|
52
52
|
required: i.required ?? !1,
|
|
@@ -54,64 +54,64 @@ function g(i) {
|
|
|
54
54
|
};
|
|
55
55
|
}
|
|
56
56
|
class b extends f {
|
|
57
|
-
constructor(
|
|
58
|
-
super(`Invalid option ${
|
|
59
|
-
}
|
|
60
|
-
pretty(
|
|
61
|
-
const
|
|
62
|
-
if (
|
|
63
|
-
|
|
64
|
-
for (const [n, s] of
|
|
57
|
+
constructor(t, e = {}) {
|
|
58
|
+
super(`Invalid option ${t} in not recognized`), this.option = t, this.optionsSchema = e;
|
|
59
|
+
}
|
|
60
|
+
pretty(t) {
|
|
61
|
+
const e = Object.entries(this.optionsSchema);
|
|
62
|
+
if (e.length > 0) {
|
|
63
|
+
t.log(l`\n{yellow Available options}:`);
|
|
64
|
+
for (const [n, s] of e) {
|
|
65
65
|
const r = g(s), o = typeof r.alias == "string" ? [r.alias] : r.alias, u = Array.isArray(r.type) ? `[${r.type[0]}]` : r.type, m = `--${n}${o.length > 0 ? o.map((a) => `, -${a}`).join("") : ""}`, p = " ".repeat(30 - m.length);
|
|
66
|
-
|
|
66
|
+
t.log(l` {green ${m}} ${p} ${r.description || "\b"} {white (${u})}`);
|
|
67
67
|
}
|
|
68
|
-
|
|
68
|
+
t.log("");
|
|
69
69
|
}
|
|
70
|
-
|
|
70
|
+
t.log(l`{white.bgRed ERROR } Option {bold.yellow ${this.option}} is not recognized.`);
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
|
-
class
|
|
74
|
-
constructor(
|
|
75
|
-
super(`Command "${
|
|
73
|
+
class P extends f {
|
|
74
|
+
constructor(t) {
|
|
75
|
+
super(`Command "${t}" not found.`), this.command = t;
|
|
76
76
|
}
|
|
77
|
-
pretty(
|
|
78
|
-
|
|
77
|
+
pretty(t) {
|
|
78
|
+
t.log(l`{bgRed ERROR } Command {yellow ${this.command}} not found.`);
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
class E extends f {
|
|
82
|
-
constructor(
|
|
83
|
-
super(`Argument "${
|
|
82
|
+
constructor(t) {
|
|
83
|
+
super(`Argument "${t}" is required.`), this.argument = t;
|
|
84
84
|
}
|
|
85
|
-
pretty(
|
|
86
|
-
|
|
85
|
+
pretty(t) {
|
|
86
|
+
t.log(l`{white.bgRed ERROR } Argument {bold.yellow ${this.argument}} is required.`);
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
class V extends f {
|
|
90
|
-
constructor(
|
|
91
|
-
super(`Argument "${
|
|
90
|
+
constructor(t) {
|
|
91
|
+
super(`Argument "${t}" is required.`), this.option = t;
|
|
92
92
|
}
|
|
93
|
-
pretty(
|
|
94
|
-
|
|
93
|
+
pretty(t) {
|
|
94
|
+
t.log(l`{white.bgRed ERROR } Option {bold.yellow ${this.option}} is required.`);
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
|
-
function v(i,
|
|
97
|
+
function v(i, t, e, n) {
|
|
98
98
|
if (i == null)
|
|
99
99
|
return n ?? null;
|
|
100
|
-
if (
|
|
100
|
+
if (t === "string")
|
|
101
101
|
return String(i);
|
|
102
|
-
if (
|
|
102
|
+
if (t === "number") {
|
|
103
103
|
const s = Number(i);
|
|
104
104
|
if (isNaN(s))
|
|
105
105
|
throw new A({
|
|
106
|
-
option:
|
|
106
|
+
option: e,
|
|
107
107
|
reason: `Expected a number, got "${i}"`
|
|
108
108
|
});
|
|
109
109
|
return s;
|
|
110
110
|
}
|
|
111
|
-
if (
|
|
111
|
+
if (t === "boolean")
|
|
112
112
|
return typeof i == "boolean" ? i : i === "true" || i === "1" ? !0 : i === "false" || i === "0" ? !1 : !!i;
|
|
113
|
-
if (Array.isArray(
|
|
114
|
-
const s =
|
|
113
|
+
if (Array.isArray(t)) {
|
|
114
|
+
const s = t[0], r = Array.isArray(i) ? i : [i];
|
|
115
115
|
if (s === "string")
|
|
116
116
|
return r.map((o) => String(o));
|
|
117
117
|
if (s === "number")
|
|
@@ -119,7 +119,7 @@ function v(i, e, t, n) {
|
|
|
119
119
|
const u = Number(o);
|
|
120
120
|
if (isNaN(u))
|
|
121
121
|
throw new A({
|
|
122
|
-
option:
|
|
122
|
+
option: e,
|
|
123
123
|
reason: `Expected array of numbers, got "${o}" in array`
|
|
124
124
|
});
|
|
125
125
|
return u;
|
|
@@ -127,15 +127,15 @@ function v(i, e, t, n) {
|
|
|
127
127
|
}
|
|
128
128
|
return i;
|
|
129
129
|
}
|
|
130
|
-
class
|
|
130
|
+
class k {
|
|
131
131
|
options;
|
|
132
132
|
parsedOptions = null;
|
|
133
133
|
arguments;
|
|
134
134
|
parsedArguments = null;
|
|
135
135
|
io;
|
|
136
136
|
shouldPromptForMissingOptions = !0;
|
|
137
|
-
constructor(
|
|
138
|
-
this.options =
|
|
137
|
+
constructor(t) {
|
|
138
|
+
this.options = t.options, this.arguments = t.arguments, this.io = t.io;
|
|
139
139
|
}
|
|
140
140
|
// === PUBLIC METHODS ===
|
|
141
141
|
/**
|
|
@@ -145,9 +145,9 @@ class F {
|
|
|
145
145
|
* @throws {InvalidOption} If an unknown option is provided
|
|
146
146
|
* @throws {BadCommandOption} If a value cannot be converted to the expected type
|
|
147
147
|
*/
|
|
148
|
-
init(
|
|
149
|
-
const { _:
|
|
150
|
-
return this.validateUnknownOptions(n), this.parsedOptions = this.handleOptions(n), this.parsedArguments = this.handleArguments(
|
|
148
|
+
init(t) {
|
|
149
|
+
const { _: e, ...n } = q(t);
|
|
150
|
+
return this.validateUnknownOptions(n), this.parsedOptions = this.handleOptions(n), this.parsedArguments = this.handleArguments(e), {
|
|
151
151
|
options: this.parsedOptions,
|
|
152
152
|
arguments: this.parsedArguments
|
|
153
153
|
};
|
|
@@ -157,30 +157,30 @@ class F {
|
|
|
157
157
|
* @throws {Error} If validation fails
|
|
158
158
|
*/
|
|
159
159
|
async validate() {
|
|
160
|
-
for (const
|
|
161
|
-
if (g(this.options[
|
|
162
|
-
throw new V(
|
|
163
|
-
for (const
|
|
164
|
-
const
|
|
165
|
-
if (
|
|
160
|
+
for (const t in this.options)
|
|
161
|
+
if (g(this.options[t]).required && (this.parsedOptions?.[t] === void 0 || this.parsedOptions?.[t] === null))
|
|
162
|
+
throw new V(t);
|
|
163
|
+
for (const t in this.arguments) {
|
|
164
|
+
const e = g(this.arguments[t]), n = this.parsedArguments?.[t];
|
|
165
|
+
if (e.required && n == null) {
|
|
166
166
|
if (this.shouldPromptForMissingOptions) {
|
|
167
|
-
const s = await this.promptForArgument(
|
|
167
|
+
const s = await this.promptForArgument(t, e);
|
|
168
168
|
if (s && this.parsedArguments) {
|
|
169
|
-
this.parsedArguments[
|
|
169
|
+
this.parsedArguments[t] = v(s, e.type, t);
|
|
170
170
|
continue;
|
|
171
171
|
}
|
|
172
172
|
}
|
|
173
|
-
throw new E(
|
|
173
|
+
throw new E(t);
|
|
174
174
|
}
|
|
175
|
-
if (
|
|
175
|
+
if (e.required && e.variadic && Array.isArray(n) && n.length === 0) {
|
|
176
176
|
if (this.shouldPromptForMissingOptions) {
|
|
177
|
-
const s = await this.promptForArgument(
|
|
177
|
+
const s = await this.promptForArgument(t, e);
|
|
178
178
|
if (s && this.parsedArguments) {
|
|
179
|
-
this.parsedArguments[
|
|
179
|
+
this.parsedArguments[t] = v(s, e.type, t);
|
|
180
180
|
continue;
|
|
181
181
|
}
|
|
182
182
|
}
|
|
183
|
-
throw new E(
|
|
183
|
+
throw new E(t);
|
|
184
184
|
}
|
|
185
185
|
}
|
|
186
186
|
}
|
|
@@ -190,17 +190,17 @@ class F {
|
|
|
190
190
|
* @returns The typed option value
|
|
191
191
|
* @throws {Error} If init() has not been called yet
|
|
192
192
|
*/
|
|
193
|
-
option(
|
|
193
|
+
option(t) {
|
|
194
194
|
if (!this.parsedOptions)
|
|
195
195
|
throw new Error("Options have not been parsed yet. Call init() first.");
|
|
196
|
-
return this.parsedOptions[
|
|
196
|
+
return this.parsedOptions[t];
|
|
197
197
|
}
|
|
198
|
-
setOption(
|
|
198
|
+
setOption(t, e) {
|
|
199
199
|
if (!this.parsedOptions)
|
|
200
200
|
throw new Error("Options have not been parsed yet. Call init() first.");
|
|
201
|
-
if (!(
|
|
202
|
-
throw new b(
|
|
203
|
-
this.parsedOptions[
|
|
201
|
+
if (!(t in this.options))
|
|
202
|
+
throw new b(t, this.options);
|
|
203
|
+
this.parsedOptions[t] = e;
|
|
204
204
|
}
|
|
205
205
|
/**
|
|
206
206
|
* Retrieves a parsed argument value by name
|
|
@@ -208,111 +208,111 @@ class F {
|
|
|
208
208
|
* @returns The typed argument value
|
|
209
209
|
* @throws {Error} If init() has not been called yet
|
|
210
210
|
*/
|
|
211
|
-
argument(
|
|
211
|
+
argument(t) {
|
|
212
212
|
if (!this.parsedArguments)
|
|
213
213
|
throw new Error("Arguments have not been parsed yet. Call init() first.");
|
|
214
|
-
return this.parsedArguments[
|
|
214
|
+
return this.parsedArguments[t];
|
|
215
215
|
}
|
|
216
|
-
setArgument(
|
|
216
|
+
setArgument(t, e) {
|
|
217
217
|
if (!this.parsedArguments)
|
|
218
218
|
throw new Error("Arguments have not been parsed yet. Call init() first.");
|
|
219
|
-
if (!(
|
|
220
|
-
throw new b(
|
|
221
|
-
this.parsedArguments[
|
|
219
|
+
if (!(t in this.arguments))
|
|
220
|
+
throw new b(t, this.arguments);
|
|
221
|
+
this.parsedArguments[t] = e;
|
|
222
222
|
}
|
|
223
223
|
// === PRIVATE HELPERS ===
|
|
224
224
|
/**
|
|
225
225
|
* Validates that all provided options are recognized
|
|
226
226
|
* @throws {InvalidOption} If an unknown option is found
|
|
227
227
|
*/
|
|
228
|
-
validateUnknownOptions(
|
|
229
|
-
const
|
|
228
|
+
validateUnknownOptions(t) {
|
|
229
|
+
const e = /* @__PURE__ */ new Set();
|
|
230
230
|
for (const n in this.options) {
|
|
231
|
-
|
|
231
|
+
e.add(n);
|
|
232
232
|
const s = g(this.options[n]);
|
|
233
233
|
for (const r of s.alias)
|
|
234
|
-
|
|
234
|
+
e.add(r);
|
|
235
235
|
}
|
|
236
|
-
for (const n in
|
|
237
|
-
if (!
|
|
236
|
+
for (const n in t)
|
|
237
|
+
if (!e.has(n))
|
|
238
238
|
throw new b(n, this.options);
|
|
239
239
|
}
|
|
240
240
|
/**
|
|
241
241
|
* Processes named options from minimist output
|
|
242
242
|
*/
|
|
243
|
-
handleOptions(
|
|
244
|
-
const
|
|
243
|
+
handleOptions(t) {
|
|
244
|
+
const e = {};
|
|
245
245
|
for (const n in this.options) {
|
|
246
246
|
const s = g(this.options[n]);
|
|
247
|
-
|
|
247
|
+
e[n] = this.resolveOptionValue(
|
|
248
248
|
n,
|
|
249
249
|
s,
|
|
250
|
-
|
|
250
|
+
t
|
|
251
251
|
);
|
|
252
252
|
}
|
|
253
|
-
return
|
|
253
|
+
return e;
|
|
254
254
|
}
|
|
255
255
|
/**
|
|
256
256
|
* Processes positional arguments from minimist output
|
|
257
257
|
*/
|
|
258
|
-
handleArguments(
|
|
259
|
-
const
|
|
258
|
+
handleArguments(t) {
|
|
259
|
+
const e = {}, n = [...t];
|
|
260
260
|
for (const s in this.arguments) {
|
|
261
261
|
const r = g(this.arguments[s]);
|
|
262
262
|
if (r.variadic) {
|
|
263
|
-
|
|
263
|
+
e[s] = this.handleVariadicArgument(s, r, n);
|
|
264
264
|
continue;
|
|
265
265
|
}
|
|
266
|
-
|
|
266
|
+
e[s] = this.resolveArgumentValue(s, r, n.shift());
|
|
267
267
|
}
|
|
268
|
-
return
|
|
268
|
+
return e;
|
|
269
269
|
}
|
|
270
270
|
/**
|
|
271
271
|
* Handles variadic arguments that consume all remaining positional values
|
|
272
272
|
*/
|
|
273
|
-
handleVariadicArgument(
|
|
274
|
-
return n.length ? v(n,
|
|
273
|
+
handleVariadicArgument(t, e, n) {
|
|
274
|
+
return n.length ? v(n, e.type, t, e.default) : e.default;
|
|
275
275
|
}
|
|
276
276
|
/**
|
|
277
277
|
* Resolves a single positional argument value with defaults and type conversion
|
|
278
278
|
* Note: Does not validate required arguments - validation happens in subclass validate() methods
|
|
279
279
|
*/
|
|
280
|
-
resolveArgumentValue(
|
|
281
|
-
return n === void 0 ?
|
|
280
|
+
resolveArgumentValue(t, e, n) {
|
|
281
|
+
return n === void 0 ? e.default : v(n, e.type, t, e.default);
|
|
282
282
|
}
|
|
283
283
|
/**
|
|
284
284
|
* Resolves an option value from the parsed option values object
|
|
285
285
|
* Handles alias resolution, defaults, and type conversion
|
|
286
286
|
*/
|
|
287
|
-
resolveOptionValue(
|
|
287
|
+
resolveOptionValue(t, e, n) {
|
|
288
288
|
let s;
|
|
289
|
-
const r = [
|
|
289
|
+
const r = [t, ...e.alias];
|
|
290
290
|
for (const o of r)
|
|
291
291
|
if (o in n) {
|
|
292
292
|
s = n[o];
|
|
293
293
|
break;
|
|
294
294
|
}
|
|
295
295
|
if (s === void 0) {
|
|
296
|
-
if (
|
|
296
|
+
if (e.required)
|
|
297
297
|
throw new A({
|
|
298
|
-
option:
|
|
298
|
+
option: t,
|
|
299
299
|
reason: "Required option is missing"
|
|
300
300
|
});
|
|
301
|
-
return
|
|
301
|
+
return e.default;
|
|
302
302
|
}
|
|
303
|
-
return v(s,
|
|
303
|
+
return v(s, e.type, t, e.default);
|
|
304
304
|
}
|
|
305
305
|
optionDefinitions() {
|
|
306
|
-
const
|
|
307
|
-
for (const
|
|
308
|
-
e
|
|
309
|
-
return
|
|
306
|
+
const t = {};
|
|
307
|
+
for (const e in this.options)
|
|
308
|
+
t[e] = g(this.options[e]);
|
|
309
|
+
return t;
|
|
310
310
|
}
|
|
311
311
|
argumentDefinitions() {
|
|
312
|
-
const
|
|
313
|
-
for (const
|
|
314
|
-
e
|
|
315
|
-
return
|
|
312
|
+
const t = {};
|
|
313
|
+
for (const e in this.arguments)
|
|
314
|
+
t[e] = g(this.arguments[e]);
|
|
315
|
+
return t;
|
|
316
316
|
}
|
|
317
317
|
availableOptions() {
|
|
318
318
|
return Object.keys(this.options);
|
|
@@ -334,12 +334,12 @@ class F {
|
|
|
334
334
|
* @param argDef - The argument's definition for type and description
|
|
335
335
|
* @returns The user-provided value, or null if none given
|
|
336
336
|
*/
|
|
337
|
-
async promptForArgument(
|
|
338
|
-
if (!Array.isArray(
|
|
337
|
+
async promptForArgument(t, e) {
|
|
338
|
+
if (!Array.isArray(e.type) && !["string", "number", "secret"].includes(e.type))
|
|
339
339
|
return null;
|
|
340
|
-
let n = l`{yellow.bold ${
|
|
341
|
-
return
|
|
342
|
-
`, Array.isArray(
|
|
340
|
+
let n = l`{yellow.bold ${t}} is required`;
|
|
341
|
+
return e.description && (n += l`: {gray (${e.description})}`), n += `
|
|
342
|
+
`, Array.isArray(e.type) ? (n += l`Please provide one or more values, separated by commas:\n`, await this.io.askForList(
|
|
343
343
|
n,
|
|
344
344
|
void 0,
|
|
345
345
|
{
|
|
@@ -349,13 +349,13 @@ class F {
|
|
|
349
349
|
n,
|
|
350
350
|
void 0,
|
|
351
351
|
{
|
|
352
|
-
type:
|
|
352
|
+
type: e.type === "number" ? "number" : e.type === "secret" ? "password" : "text",
|
|
353
353
|
validate: (s) => {
|
|
354
|
-
if (
|
|
354
|
+
if (e.type === "number") {
|
|
355
355
|
const r = Number(s);
|
|
356
356
|
if (isNaN(r))
|
|
357
357
|
return "Please enter a valid number";
|
|
358
|
-
} else if (
|
|
358
|
+
} else if (e.type === "string" && (typeof s != "string" || !s.length))
|
|
359
359
|
return "Please enter a valid text";
|
|
360
360
|
return !0;
|
|
361
361
|
}
|
|
@@ -373,7 +373,7 @@ class H {
|
|
|
373
373
|
default = !1;
|
|
374
374
|
description = l`Display help for the given command. When no command is given display help for the {green list} command`;
|
|
375
375
|
async handler() {
|
|
376
|
-
const
|
|
376
|
+
const t = this.parser.argumentDefinitions(), e = this.parser.optionDefinitions(), n = Object.entries(t), s = Object.entries(e), r = s.map(([a, d]) => {
|
|
377
377
|
const h = Array.isArray(d.alias) ? d.alias : d.alias ? [d.alias] : [];
|
|
378
378
|
return {
|
|
379
379
|
name: a,
|
|
@@ -424,92 +424,92 @@ class H {
|
|
|
424
424
|
}
|
|
425
425
|
class S {
|
|
426
426
|
logger;
|
|
427
|
-
constructor(
|
|
428
|
-
this.logger =
|
|
427
|
+
constructor(t) {
|
|
428
|
+
this.logger = t;
|
|
429
429
|
}
|
|
430
430
|
/**
|
|
431
431
|
* Logger methods
|
|
432
432
|
*/
|
|
433
|
-
log(...
|
|
434
|
-
this.logger.log(...
|
|
433
|
+
log(...t) {
|
|
434
|
+
this.logger.log(...t);
|
|
435
435
|
}
|
|
436
|
-
info(...
|
|
437
|
-
this.logger.info(...
|
|
436
|
+
info(...t) {
|
|
437
|
+
this.logger.info(...t);
|
|
438
438
|
}
|
|
439
|
-
warn(...
|
|
440
|
-
this.logger.warn(...
|
|
439
|
+
warn(...t) {
|
|
440
|
+
this.logger.warn(...t);
|
|
441
441
|
}
|
|
442
|
-
error(...
|
|
443
|
-
this.logger.error(...
|
|
442
|
+
error(...t) {
|
|
443
|
+
this.logger.error(...t);
|
|
444
444
|
}
|
|
445
|
-
debug(...
|
|
446
|
-
this.logger.debug(...
|
|
445
|
+
debug(...t) {
|
|
446
|
+
this.logger.debug(...t);
|
|
447
447
|
}
|
|
448
448
|
/**
|
|
449
449
|
* Prompt utils
|
|
450
450
|
*/
|
|
451
|
-
async askForConfirmation(
|
|
451
|
+
async askForConfirmation(t = "Do you want to continue?", e) {
|
|
452
452
|
return (await y({
|
|
453
453
|
type: "confirm",
|
|
454
454
|
name: "value",
|
|
455
|
-
message:
|
|
456
|
-
initial:
|
|
455
|
+
message: t,
|
|
456
|
+
initial: e ?? !1
|
|
457
457
|
})).value;
|
|
458
458
|
}
|
|
459
|
-
async askForInput(
|
|
459
|
+
async askForInput(t, e, n) {
|
|
460
460
|
return (await y({
|
|
461
461
|
type: "text",
|
|
462
462
|
name: "value",
|
|
463
|
-
message:
|
|
464
|
-
initial:
|
|
463
|
+
message: t,
|
|
464
|
+
initial: e,
|
|
465
465
|
...n
|
|
466
466
|
}))?.value ?? null;
|
|
467
467
|
}
|
|
468
|
-
async askForDate(
|
|
468
|
+
async askForDate(t, e, n) {
|
|
469
469
|
return (await y({
|
|
470
470
|
type: "date",
|
|
471
471
|
name: "value",
|
|
472
|
-
message:
|
|
473
|
-
initial:
|
|
472
|
+
message: t,
|
|
473
|
+
initial: e,
|
|
474
474
|
...n
|
|
475
475
|
}))?.value ?? null;
|
|
476
476
|
}
|
|
477
|
-
async askForList(
|
|
477
|
+
async askForList(t, e, n) {
|
|
478
478
|
return (await y({
|
|
479
479
|
type: "list",
|
|
480
480
|
name: "value",
|
|
481
|
-
message:
|
|
482
|
-
initial:
|
|
481
|
+
message: t,
|
|
482
|
+
initial: e,
|
|
483
483
|
...n
|
|
484
484
|
}))?.value ?? null;
|
|
485
485
|
}
|
|
486
|
-
async askForToggle(
|
|
486
|
+
async askForToggle(t, e, n) {
|
|
487
487
|
return (await y({
|
|
488
488
|
type: "toggle",
|
|
489
489
|
name: "value",
|
|
490
|
-
message:
|
|
491
|
-
initial:
|
|
490
|
+
message: t,
|
|
491
|
+
initial: e,
|
|
492
492
|
...n
|
|
493
493
|
}))?.value ?? null;
|
|
494
494
|
}
|
|
495
|
-
async askForSelect(
|
|
496
|
-
if (
|
|
495
|
+
async askForSelect(t, e, n) {
|
|
496
|
+
if (e.length === 0)
|
|
497
497
|
throw new Error("No options provided");
|
|
498
498
|
const s = [];
|
|
499
|
-
for (const o of
|
|
499
|
+
for (const o of e)
|
|
500
500
|
typeof o == "string" ? s.push({ title: o, value: o }) : s.push(o);
|
|
501
501
|
return (await y({
|
|
502
502
|
type: "select",
|
|
503
503
|
name: "value",
|
|
504
|
-
message:
|
|
504
|
+
message: t,
|
|
505
505
|
choices: s,
|
|
506
506
|
...n
|
|
507
507
|
}))?.value ?? null;
|
|
508
508
|
}
|
|
509
|
-
newLoader(
|
|
510
|
-
let s =
|
|
509
|
+
newLoader(t = "", e = ["⠙", "⠘", "⠰", "⠴", "⠤", "⠦", "⠆", "⠃", "⠋", "⠉"], n = 100) {
|
|
510
|
+
let s = t, r = null, o = 0;
|
|
511
511
|
const u = setInterval(function() {
|
|
512
|
-
r && (process.stdout.write(new TextEncoder().encode("\r" + " ".repeat(r.length + 5) + "\r")), r = null), process.stdout.write(new TextEncoder().encode("\r" +
|
|
512
|
+
r && (process.stdout.write(new TextEncoder().encode("\r" + " ".repeat(r.length + 5) + "\r")), r = null), process.stdout.write(new TextEncoder().encode("\r" + e[o++] + " " + s)), o = o % e.length;
|
|
513
513
|
}, n), m = () => {
|
|
514
514
|
clearInterval(u), process.stdout.write(new TextEncoder().encode("\r" + " ".repeat(s.length + 5) + "\r"));
|
|
515
515
|
};
|
|
@@ -542,18 +542,18 @@ class C {
|
|
|
542
542
|
defaultOptions() {
|
|
543
543
|
return [new H()];
|
|
544
544
|
}
|
|
545
|
-
newCommandParser(
|
|
546
|
-
return new
|
|
547
|
-
io:
|
|
548
|
-
options:
|
|
549
|
-
arguments:
|
|
545
|
+
newCommandParser(t) {
|
|
546
|
+
return new k({
|
|
547
|
+
io: t.io,
|
|
548
|
+
options: t.options,
|
|
549
|
+
arguments: t.arguments
|
|
550
550
|
});
|
|
551
551
|
}
|
|
552
|
-
newCommandIO(
|
|
553
|
-
return new S(
|
|
552
|
+
newCommandIO(t) {
|
|
553
|
+
return new S(t.logger);
|
|
554
554
|
}
|
|
555
|
-
constructor(
|
|
556
|
-
this._command =
|
|
555
|
+
constructor(t, e) {
|
|
556
|
+
this._command = t, this.description = e?.description ?? "", this.group = e?.group;
|
|
557
557
|
const n = this.defaultOptions();
|
|
558
558
|
if (n.length > 0) {
|
|
559
559
|
this.tmp = {
|
|
@@ -567,37 +567,37 @@ class C {
|
|
|
567
567
|
disablePrompting() {
|
|
568
568
|
return this.disablePromptingFlag = !0, this;
|
|
569
569
|
}
|
|
570
|
-
preHandler(
|
|
571
|
-
return this._preHandler =
|
|
570
|
+
preHandler(t) {
|
|
571
|
+
return this._preHandler = t, this;
|
|
572
572
|
}
|
|
573
|
-
handler(
|
|
574
|
-
return this._handler =
|
|
573
|
+
handler(t) {
|
|
574
|
+
return this._handler = t, this;
|
|
575
575
|
}
|
|
576
|
-
options(
|
|
576
|
+
options(t) {
|
|
577
577
|
return this.tmp = {
|
|
578
578
|
options: {
|
|
579
579
|
...this.tmp?.options ?? {},
|
|
580
|
-
...
|
|
580
|
+
...t
|
|
581
581
|
},
|
|
582
582
|
arguments: this.tmp?.arguments ?? {}
|
|
583
583
|
}, this;
|
|
584
584
|
}
|
|
585
|
-
arguments(
|
|
585
|
+
arguments(t) {
|
|
586
586
|
return this.tmp = {
|
|
587
587
|
options: this.tmp?.options ?? {},
|
|
588
588
|
arguments: {
|
|
589
589
|
...this.tmp?.arguments ?? {},
|
|
590
|
-
...
|
|
590
|
+
...t
|
|
591
591
|
}
|
|
592
592
|
}, this;
|
|
593
593
|
}
|
|
594
|
-
async run(
|
|
594
|
+
async run(t) {
|
|
595
595
|
if (!this._handler && !this.handle)
|
|
596
596
|
throw new Error(`No handler defined for command ${this.command || "(unknown)"}`);
|
|
597
|
-
let
|
|
598
|
-
if (this.ctx =
|
|
599
|
-
logger:
|
|
600
|
-
}),
|
|
597
|
+
let e;
|
|
598
|
+
if (this.ctx = t.ctx, this.logger = t.logger, this.io = this.newCommandIO({
|
|
599
|
+
logger: t.logger
|
|
600
|
+
}), t && "args" in t) {
|
|
601
601
|
const r = this.tmp?.options ?? {};
|
|
602
602
|
for (const o of this.defaultOptions())
|
|
603
603
|
o.option in r || (r[o.option] = o);
|
|
@@ -605,18 +605,18 @@ class C {
|
|
|
605
605
|
io: this.io,
|
|
606
606
|
options: r,
|
|
607
607
|
arguments: this.tmp?.arguments ?? {}
|
|
608
|
-
}),
|
|
608
|
+
}), e = this.parser.init(t.args);
|
|
609
609
|
for (const o of this.defaultOptions())
|
|
610
|
-
if (
|
|
610
|
+
if (e.options[o.option] === !0) {
|
|
611
611
|
const u = await o.handler.call(this);
|
|
612
612
|
if (u && u !== 0)
|
|
613
613
|
return u;
|
|
614
614
|
}
|
|
615
615
|
this.disablePromptingFlag && this.parser.disablePrompting(), await this.parser.validate();
|
|
616
616
|
} else
|
|
617
|
-
|
|
618
|
-
options:
|
|
619
|
-
arguments:
|
|
617
|
+
e = {
|
|
618
|
+
options: t.options,
|
|
619
|
+
arguments: t.arguments
|
|
620
620
|
};
|
|
621
621
|
const n = this.preHandle ? await this.preHandle() : null;
|
|
622
622
|
if (n && n !== 0)
|
|
@@ -625,29 +625,29 @@ class C {
|
|
|
625
625
|
this._handler = this.handle.bind(this);
|
|
626
626
|
else if (!this._handler)
|
|
627
627
|
throw new Error(`No handler defined for command ${this.command || "(unknown)"}`);
|
|
628
|
-
return await this._handler(
|
|
628
|
+
return await this._handler(t.ctx, e) ?? 0;
|
|
629
629
|
}
|
|
630
630
|
}
|
|
631
|
-
class w extends
|
|
631
|
+
class w extends k {
|
|
632
632
|
command;
|
|
633
|
-
constructor(
|
|
634
|
-
const
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
633
|
+
constructor(t) {
|
|
634
|
+
const e = w.parseSignature(
|
|
635
|
+
t.signature,
|
|
636
|
+
t.helperDefinitions,
|
|
637
|
+
t.defaultOptions
|
|
638
638
|
);
|
|
639
|
-
super({ io:
|
|
639
|
+
super({ io: t.io, options: e.options, arguments: e.arguments }), this.command = e.command;
|
|
640
640
|
}
|
|
641
641
|
/**
|
|
642
642
|
* Parses command signature string into command name and parameter schemas
|
|
643
643
|
* Example: "migrate {name} {--force}" -> { command: "migrate", arguments: {name: ...}, options: {force: ...} }
|
|
644
644
|
*/
|
|
645
|
-
static parseSignature(
|
|
646
|
-
const [s, ...r] =
|
|
645
|
+
static parseSignature(t, e, n) {
|
|
646
|
+
const [s, ...r] = t.split(/\{(.*?)\}/g).map((m) => m.trim()).filter(Boolean), o = {}, u = {};
|
|
647
647
|
for (const m of r) {
|
|
648
648
|
const { name: p, isOption: a, definition: d } = w.parseParamSignature(
|
|
649
649
|
m,
|
|
650
|
-
|
|
650
|
+
e
|
|
651
651
|
);
|
|
652
652
|
a ? o[p] = d : u[p] = d;
|
|
653
653
|
}
|
|
@@ -676,8 +676,8 @@ class w extends F {
|
|
|
676
676
|
* - {--opt=} -> string option
|
|
677
677
|
* - {--opt|o} -> option with alias
|
|
678
678
|
*/
|
|
679
|
-
static parseParamSignature(
|
|
680
|
-
let n =
|
|
679
|
+
static parseParamSignature(t, e) {
|
|
680
|
+
let n = t, s = !1;
|
|
681
681
|
const r = {
|
|
682
682
|
required: !0,
|
|
683
683
|
type: "string",
|
|
@@ -697,7 +697,7 @@ class w extends F {
|
|
|
697
697
|
const [o, ...u] = n.split("|");
|
|
698
698
|
n = o.trim(), r.alias = u.map((m) => m.trim());
|
|
699
699
|
}
|
|
700
|
-
return n.startsWith("--") && (s = !0, n = n.slice(2)), r.default === "*" && (r.default = [], r.type = ["string"]), n.endsWith("?") && (r.required = !1, n = n.slice(0, -1)), n.endsWith("*") && (r.type = ["string"], r.variadic = !0, r.default = [], n = n.slice(0, -1)), r.description = r.description ??
|
|
700
|
+
return n.startsWith("--") && (s = !0, n = n.slice(2)), r.default === "*" && (r.default = [], r.type = ["string"]), n.endsWith("?") && (r.required = !1, n = n.slice(0, -1)), n.endsWith("*") && (r.type = ["string"], r.variadic = !0, r.default = [], n = n.slice(0, -1)), r.description = r.description ?? e[n] ?? e[`--${n}`], { name: n, isOption: s, definition: r };
|
|
701
701
|
}
|
|
702
702
|
}
|
|
703
703
|
class z extends C {
|
|
@@ -705,9 +705,9 @@ class z extends C {
|
|
|
705
705
|
get command() {
|
|
706
706
|
return this.parser ? this.parser.command : this.signature.split(" ")[0];
|
|
707
707
|
}
|
|
708
|
-
newCommandParser(
|
|
708
|
+
newCommandParser(t) {
|
|
709
709
|
return new w({
|
|
710
|
-
io:
|
|
710
|
+
io: t.io,
|
|
711
711
|
signature: this.signature,
|
|
712
712
|
helperDefinitions: this.helperDefinitions,
|
|
713
713
|
defaultOptions: this.defaultOptions()
|
|
@@ -716,55 +716,55 @@ class z extends C {
|
|
|
716
716
|
constructor() {
|
|
717
717
|
super("");
|
|
718
718
|
}
|
|
719
|
-
option(
|
|
720
|
-
return this.parser instanceof w ? this.parser.option(
|
|
719
|
+
option(t, e = null) {
|
|
720
|
+
return this.parser instanceof w ? this.parser.option(t) ?? e : e;
|
|
721
721
|
}
|
|
722
|
-
argument(
|
|
723
|
-
return this.parser instanceof w ? this.parser.argument(
|
|
722
|
+
argument(t, e = null) {
|
|
723
|
+
return this.parser instanceof w ? this.parser.argument(t) ?? e : e;
|
|
724
724
|
}
|
|
725
725
|
// Prompt utils
|
|
726
|
-
async askForConfirmation(...
|
|
727
|
-
return this.io.askForConfirmation(...
|
|
726
|
+
async askForConfirmation(...t) {
|
|
727
|
+
return this.io.askForConfirmation(...t);
|
|
728
728
|
}
|
|
729
|
-
async askForInput(...
|
|
730
|
-
return this.io.askForInput(...
|
|
729
|
+
async askForInput(...t) {
|
|
730
|
+
return this.io.askForInput(...t);
|
|
731
731
|
}
|
|
732
|
-
async askForSelect(...
|
|
733
|
-
return this.io.askForSelect(...
|
|
732
|
+
async askForSelect(...t) {
|
|
733
|
+
return this.io.askForSelect(...t);
|
|
734
734
|
}
|
|
735
|
-
newLoader(...
|
|
736
|
-
return this.io.newLoader(...
|
|
735
|
+
newLoader(...t) {
|
|
736
|
+
return this.io.newLoader(...t);
|
|
737
737
|
}
|
|
738
738
|
}
|
|
739
739
|
class L {
|
|
740
740
|
level;
|
|
741
|
-
constructor(
|
|
742
|
-
this.level =
|
|
741
|
+
constructor(t = {}) {
|
|
742
|
+
this.level = t.level ?? "info";
|
|
743
743
|
}
|
|
744
|
-
shouldLog(
|
|
745
|
-
const
|
|
746
|
-
return
|
|
744
|
+
shouldLog(t) {
|
|
745
|
+
const e = ["debug", "info", "warn", "error"], n = e.indexOf(this.level);
|
|
746
|
+
return e.indexOf(t) >= n;
|
|
747
747
|
}
|
|
748
|
-
setLevel(
|
|
749
|
-
this.level =
|
|
748
|
+
setLevel(t) {
|
|
749
|
+
this.level = t;
|
|
750
750
|
}
|
|
751
751
|
getLevel() {
|
|
752
752
|
return this.level;
|
|
753
753
|
}
|
|
754
|
-
log(...
|
|
755
|
-
console.log(...
|
|
754
|
+
log(...t) {
|
|
755
|
+
console.log(...t);
|
|
756
756
|
}
|
|
757
|
-
info(...
|
|
758
|
-
this.shouldLog("info") && console.log(...
|
|
757
|
+
info(...t) {
|
|
758
|
+
this.shouldLog("info") && console.log(...t);
|
|
759
759
|
}
|
|
760
|
-
warn(...
|
|
761
|
-
this.shouldLog("warn") && console.warn(...
|
|
760
|
+
warn(...t) {
|
|
761
|
+
this.shouldLog("warn") && console.warn(...t);
|
|
762
762
|
}
|
|
763
|
-
error(...
|
|
764
|
-
this.shouldLog("error") && console.error(...
|
|
763
|
+
error(...t) {
|
|
764
|
+
this.shouldLog("error") && console.error(...t);
|
|
765
765
|
}
|
|
766
|
-
debug(...
|
|
767
|
-
this.shouldLog("debug") && console.log(...
|
|
766
|
+
debug(...t) {
|
|
767
|
+
this.shouldLog("debug") && console.log(...t);
|
|
768
768
|
}
|
|
769
769
|
}
|
|
770
770
|
class j {
|
|
@@ -774,8 +774,8 @@ class j {
|
|
|
774
774
|
get CommandIOClass() {
|
|
775
775
|
return S;
|
|
776
776
|
}
|
|
777
|
-
constructor(
|
|
778
|
-
this.logger =
|
|
777
|
+
constructor(t) {
|
|
778
|
+
this.logger = t ?? new L(), this.io = new this.CommandIOClass(this.logger);
|
|
779
779
|
}
|
|
780
780
|
getAvailableCommands() {
|
|
781
781
|
return Object.keys(this.commands);
|
|
@@ -783,62 +783,58 @@ class j {
|
|
|
783
783
|
getCommands() {
|
|
784
784
|
return Object.values(this.commands);
|
|
785
785
|
}
|
|
786
|
-
importFile = async (
|
|
787
|
-
commandResolver = async (
|
|
788
|
-
let
|
|
789
|
-
if (!
|
|
790
|
-
throw new Error(`The command at path ${
|
|
791
|
-
return
|
|
786
|
+
importFile = async (t) => (await import(t)).default;
|
|
787
|
+
commandResolver = async (t) => {
|
|
788
|
+
let e = await this.importFile(t);
|
|
789
|
+
if (!e)
|
|
790
|
+
throw new Error(`The command at path ${t} does not have a default export.`);
|
|
791
|
+
return e?.default && (e = e.default), typeof e == "function" ? new e() : e instanceof C ? e : null;
|
|
792
792
|
};
|
|
793
|
-
|
|
794
|
-
return this.commandResolver =
|
|
795
|
-
}
|
|
796
|
-
setFileImporter(e) {
|
|
797
|
-
return this.importFile = e, this;
|
|
793
|
+
withCommandResolver(t) {
|
|
794
|
+
return this.commandResolver = t, this;
|
|
798
795
|
}
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
return this.fileCheck = e, this;
|
|
796
|
+
withFileImporter(t) {
|
|
797
|
+
return this.importFile = t, this;
|
|
802
798
|
}
|
|
803
|
-
registerCommand(
|
|
804
|
-
const n =
|
|
799
|
+
registerCommand(t, e = !1) {
|
|
800
|
+
const n = t.command;
|
|
805
801
|
if (!n)
|
|
806
802
|
throw new Error("Command signature is invalid, it must have a command name.");
|
|
807
|
-
if (!
|
|
803
|
+
if (!e && this.commands[n])
|
|
808
804
|
throw new Error(`Command ${n} already registered.`);
|
|
809
|
-
this.commands[n] =
|
|
805
|
+
this.commands[n] = t;
|
|
810
806
|
}
|
|
811
|
-
async loadCommandsPath(
|
|
812
|
-
for await (const
|
|
807
|
+
async loadCommandsPath(t) {
|
|
808
|
+
for await (const e of this.listCommandsFiles(t))
|
|
813
809
|
try {
|
|
814
|
-
const n = await this.commandResolver(
|
|
810
|
+
const n = await this.commandResolver(e);
|
|
815
811
|
n instanceof C && this.registerCommand(n);
|
|
816
812
|
} catch (n) {
|
|
817
|
-
throw new Error(`Command ${
|
|
813
|
+
throw new Error(`Command ${e} failed to load. ${n}`, {
|
|
818
814
|
cause: n
|
|
819
815
|
});
|
|
820
816
|
}
|
|
821
817
|
}
|
|
822
|
-
async runCommand(
|
|
823
|
-
const s = typeof
|
|
818
|
+
async runCommand(t, e, ...n) {
|
|
819
|
+
const s = typeof e == "string" ? this.commands[e] : e, r = typeof e == "string" ? e : s.command;
|
|
824
820
|
if (!s) {
|
|
825
821
|
const o = await this.suggestCommand(r);
|
|
826
|
-
return o ? await this.runCommand(
|
|
822
|
+
return o ? await this.runCommand(t, o, ...n) : 1;
|
|
827
823
|
}
|
|
828
824
|
return await s.run({
|
|
829
|
-
ctx:
|
|
825
|
+
ctx: t,
|
|
830
826
|
logger: this.logger,
|
|
831
827
|
args: n
|
|
832
828
|
}) ?? 0;
|
|
833
829
|
}
|
|
834
|
-
async suggestCommand(
|
|
835
|
-
const
|
|
830
|
+
async suggestCommand(t) {
|
|
831
|
+
const e = this.getAvailableCommands(), { bestMatch: n, bestMatchIndex: s, ratings: r } = I.findBestMatch(t, e), o = r.filter((u) => u.rating > 0.3).map((u) => u.target);
|
|
836
832
|
if (n.rating > 0 && o.length <= 1 || n.rating > 0.7 && o.length > 1) {
|
|
837
|
-
const u =
|
|
838
|
-
return await this.askRunSimilarCommand(
|
|
833
|
+
const u = e[s];
|
|
834
|
+
return await this.askRunSimilarCommand(t, u) ? u : null;
|
|
839
835
|
}
|
|
840
836
|
if (o.length) {
|
|
841
|
-
this.io.error(l`{bgRed ERROR } Command {yellow ${
|
|
837
|
+
this.io.error(l`{bgRed ERROR } Command {yellow ${t}} not found.\n`);
|
|
842
838
|
const u = await this.io.askForSelect(
|
|
843
839
|
l`{green Did you mean to run one of these commands instead?}`,
|
|
844
840
|
o
|
|
@@ -846,17 +842,17 @@ class j {
|
|
|
846
842
|
if (u)
|
|
847
843
|
return u;
|
|
848
844
|
}
|
|
849
|
-
throw new
|
|
845
|
+
throw new P(t);
|
|
850
846
|
}
|
|
851
|
-
async askRunSimilarCommand(
|
|
852
|
-
return this.io.error(l`{bgRed ERROR } Command {yellow ${
|
|
847
|
+
async askRunSimilarCommand(t, e) {
|
|
848
|
+
return this.io.error(l`{bgRed ERROR } Command {yellow ${t}} not found.\n`), this.io.askForConfirmation(l`{green Do you want to run {yellow ${e}} instead?} `);
|
|
853
849
|
}
|
|
854
|
-
async *listCommandsFiles(
|
|
855
|
-
const
|
|
856
|
-
for (const n of
|
|
857
|
-
const s = R.resolve(
|
|
850
|
+
async *listCommandsFiles(t) {
|
|
851
|
+
const e = D.readdirSync(t, { withFileTypes: !0 });
|
|
852
|
+
for (const n of e) {
|
|
853
|
+
const s = R.resolve(t, n.name);
|
|
858
854
|
if (n.isDirectory())
|
|
859
|
-
yield* this.listCommandsFiles(R.resolve(
|
|
855
|
+
yield* this.listCommandsFiles(R.resolve(t, n.name));
|
|
860
856
|
else {
|
|
861
857
|
if (!s.endsWith(".ts") && !s.endsWith(".js") && !s.endsWith(".mjs") && !s.endsWith(".cjs"))
|
|
862
858
|
continue;
|
|
@@ -866,22 +862,22 @@ class j {
|
|
|
866
862
|
}
|
|
867
863
|
}
|
|
868
864
|
class W extends C {
|
|
869
|
-
constructor(
|
|
865
|
+
constructor(t) {
|
|
870
866
|
super("help", {
|
|
871
867
|
description: l.bold("Show help information about the CLI and its commands")
|
|
872
|
-
}), this.opts =
|
|
868
|
+
}), this.opts = t;
|
|
873
869
|
}
|
|
874
870
|
async handle() {
|
|
875
|
-
const
|
|
876
|
-
this.io.log(l`${
|
|
871
|
+
const t = this.opts.commandRegistry.getCommands(), e = this.opts.cliName ?? "Bob CLI", n = this.opts.cliVersion ?? "0.0.0", s = (await import("./package-ODN3iB6L.js"))?.default?.version ?? "0.0.0";
|
|
872
|
+
this.io.log(l`${e} {green ${n}} (core: {yellow ${s}})
|
|
877
873
|
|
|
878
874
|
{yellow Usage}:
|
|
879
875
|
command [options] [arguments]
|
|
880
876
|
|
|
881
877
|
{yellow Available commands}:
|
|
882
878
|
`);
|
|
883
|
-
const r = Math.max(...
|
|
884
|
-
for (const m of
|
|
879
|
+
const r = Math.max(...t.map((m) => m.command.length)) ?? 0, o = {};
|
|
880
|
+
for (const m of t) {
|
|
885
881
|
const p = m.group ?? m.command.split(":")[0];
|
|
886
882
|
o[p] || (o[p] = []), o[p].push(m);
|
|
887
883
|
}
|
|
@@ -899,13 +895,13 @@ class W extends C {
|
|
|
899
895
|
}
|
|
900
896
|
class T {
|
|
901
897
|
logger;
|
|
902
|
-
constructor(
|
|
903
|
-
this.logger =
|
|
898
|
+
constructor(t) {
|
|
899
|
+
this.logger = t;
|
|
904
900
|
}
|
|
905
|
-
handle(
|
|
906
|
-
if (
|
|
907
|
-
return
|
|
908
|
-
throw
|
|
901
|
+
handle(t) {
|
|
902
|
+
if (t instanceof f)
|
|
903
|
+
return t.pretty(this.logger), -1;
|
|
904
|
+
throw t;
|
|
909
905
|
}
|
|
910
906
|
}
|
|
911
907
|
class J {
|
|
@@ -914,41 +910,44 @@ class J {
|
|
|
914
910
|
commandRegistry;
|
|
915
911
|
exceptionHandler;
|
|
916
912
|
helpCommand;
|
|
917
|
-
newCommandRegistry(
|
|
918
|
-
return new j(
|
|
913
|
+
newCommandRegistry(t) {
|
|
914
|
+
return new j(t.logger);
|
|
919
915
|
}
|
|
920
|
-
newHelpCommand(
|
|
921
|
-
return new W(
|
|
916
|
+
newHelpCommand(t) {
|
|
917
|
+
return new W(t);
|
|
922
918
|
}
|
|
923
|
-
newExceptionHandler(
|
|
924
|
-
return new T(
|
|
919
|
+
newExceptionHandler(t) {
|
|
920
|
+
return new T(t.logger);
|
|
925
921
|
}
|
|
926
|
-
constructor(
|
|
927
|
-
this.ctx =
|
|
922
|
+
constructor(t = {}) {
|
|
923
|
+
this.ctx = t.ctx, this.logger = t.logger ?? new L(), this.commandRegistry = this.newCommandRegistry({
|
|
928
924
|
logger: this.logger
|
|
929
925
|
}), this.exceptionHandler = this.newExceptionHandler({
|
|
930
926
|
logger: this.logger
|
|
931
927
|
}), this.helpCommand = this.newHelpCommand({
|
|
932
|
-
cliName:
|
|
933
|
-
cliVersion:
|
|
928
|
+
cliName: t.name,
|
|
929
|
+
cliVersion: t.version,
|
|
934
930
|
commandRegistry: this.commandRegistry
|
|
935
931
|
});
|
|
936
932
|
}
|
|
937
|
-
|
|
938
|
-
this.commandRegistry.
|
|
933
|
+
withCommandResolver(t) {
|
|
934
|
+
return this.commandRegistry.withCommandResolver(t), this;
|
|
935
|
+
}
|
|
936
|
+
withFileImporter(t) {
|
|
937
|
+
return this.commandRegistry.withFileImporter(t), this;
|
|
939
938
|
}
|
|
940
|
-
async withCommands(...
|
|
941
|
-
for (const
|
|
942
|
-
typeof
|
|
939
|
+
async withCommands(...t) {
|
|
940
|
+
for (const e of t)
|
|
941
|
+
typeof e == "string" ? await this.commandRegistry.loadCommandsPath(e) : typeof e == "function" ? this.registerCommand(new e()) : this.registerCommand(e);
|
|
943
942
|
}
|
|
944
|
-
async runCommand(
|
|
945
|
-
return
|
|
943
|
+
async runCommand(t, ...e) {
|
|
944
|
+
return t ? await this.commandRegistry.runCommand(this.ctx, t, ...e).catch(this.exceptionHandler.handle.bind(this.exceptionHandler)) : await this.runHelpCommand();
|
|
946
945
|
}
|
|
947
946
|
async runHelpCommand() {
|
|
948
947
|
return await this.runCommand(this.helpCommand);
|
|
949
948
|
}
|
|
950
|
-
registerCommand(
|
|
951
|
-
this.commandRegistry.registerCommand(
|
|
949
|
+
registerCommand(t) {
|
|
950
|
+
this.commandRegistry.registerCommand(t);
|
|
952
951
|
}
|
|
953
952
|
}
|
|
954
953
|
export {
|
|
@@ -958,8 +957,8 @@ export {
|
|
|
958
957
|
J as Cli,
|
|
959
958
|
C as Command,
|
|
960
959
|
S as CommandIO,
|
|
961
|
-
|
|
962
|
-
|
|
960
|
+
P as CommandNotFoundError,
|
|
961
|
+
k as CommandParser,
|
|
963
962
|
j as CommandRegistry,
|
|
964
963
|
w as CommandSignatureParser,
|
|
965
964
|
z as CommandWithSignature,
|