bob-core 2.0.0-beta.4 → 2.0.0-beta.6
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-CXW_8mWb.cjs → package-BKcK1zXi.cjs} +1 -1
- package/dist/cjs/src/Command.d.ts +11 -6
- package/dist/cjs/src/CommandIO.d.ts +9 -0
- package/dist/cjs/src/CommandParser.d.ts +17 -1
- package/dist/cjs/src/CommandRegistry.d.ts +3 -1
- package/dist/cjs/src/CommandSignatureParser.d.ts +1 -47
- package/dist/cjs/src/CommandWithSignature.d.ts +1 -10
- package/dist/cjs/src/errors/index.d.ts +0 -2
- package/dist/cjs/src/lib/optionHelpers.d.ts +1 -1
- package/dist/cjs/src/lib/types.d.ts +2 -2
- package/dist/esm/index.js +560 -652
- package/dist/esm/{package-ye8xt3By.js → package-B_VxJm2q.js} +1 -1
- package/dist/esm/src/Command.d.ts +11 -6
- package/dist/esm/src/CommandIO.d.ts +9 -0
- package/dist/esm/src/CommandParser.d.ts +17 -1
- package/dist/esm/src/CommandRegistry.d.ts +3 -1
- package/dist/esm/src/CommandSignatureParser.d.ts +1 -47
- package/dist/esm/src/CommandWithSignature.d.ts +1 -10
- package/dist/esm/src/errors/index.d.ts +0 -2
- package/dist/esm/src/lib/optionHelpers.d.ts +1 -1
- package/dist/esm/src/lib/types.d.ts +2 -2
- package/package.json +1 -1
- package/dist/cjs/src/errors/MissingSignatureArgument.d.ts +0 -8
- package/dist/cjs/src/errors/MissingSignatureOption.d.ts +0 -8
- package/dist/esm/src/errors/MissingSignatureArgument.d.ts +0 -8
- package/dist/esm/src/errors/MissingSignatureOption.d.ts +0 -8
package/dist/esm/index.js
CHANGED
|
@@ -1,174 +1,141 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import * as
|
|
5
|
-
import
|
|
6
|
-
import * as
|
|
7
|
-
class
|
|
1
|
+
import q from "minimist";
|
|
2
|
+
import l from "chalk";
|
|
3
|
+
import y from "prompts";
|
|
4
|
+
import * as D from "node:fs";
|
|
5
|
+
import R from "path";
|
|
6
|
+
import * as P from "string-similarity";
|
|
7
|
+
class f extends Error {
|
|
8
8
|
}
|
|
9
|
-
class
|
|
10
|
-
constructor(
|
|
11
|
-
let
|
|
12
|
-
|
|
9
|
+
class U extends f {
|
|
10
|
+
constructor(e) {
|
|
11
|
+
let t = `Argument "${e.param}" value is invalid.`;
|
|
12
|
+
e.reason ? t += ` Reason: ${e.reason}` : t += ` Value: "${e.value}"`, super(t), this.param = e;
|
|
13
13
|
}
|
|
14
|
-
pretty(
|
|
15
|
-
|
|
14
|
+
pretty(e) {
|
|
15
|
+
e.log(l` {white.bgRed ERROR } Argument {bold.yellow ${this.param.param}} value is invalid. `), (this.param.value || this.param.reason) && e.log(""), this.param.value && e.log(l` {blue Value}: ${this.param.value}`), this.param.reason && e.log(l` {yellow Reason}: ${this.param.reason}`);
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
|
-
class
|
|
19
|
-
constructor(
|
|
20
|
-
let
|
|
21
|
-
|
|
18
|
+
class A extends f {
|
|
19
|
+
constructor(e) {
|
|
20
|
+
let t = `Option "${e.option}" value is invalid.`;
|
|
21
|
+
e.reason ? t += ` Reason: ${e.reason}` : t += ` Value: "${e.value}"`, super(t), this.param = e;
|
|
22
22
|
}
|
|
23
|
-
pretty(
|
|
24
|
-
|
|
23
|
+
pretty(e) {
|
|
24
|
+
e.log(l` {white.bgRed ERROR } Option {bold.yellow ${this.param.option}} value is invalid. `), (this.param.value || this.param.reason) && e.log(""), this.param.value && e.log(l` {blue Value}: ${this.param.value}`), this.param.reason && e.log(l` {yellow Reason}: ${this.param.reason}`);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
-
function
|
|
28
|
-
if (
|
|
29
|
-
if (
|
|
30
|
-
if (Array.isArray(
|
|
31
|
-
if (
|
|
32
|
-
if (
|
|
33
|
-
}
|
|
34
|
-
throw new Error("Invalid option type: " +
|
|
27
|
+
function x(i) {
|
|
28
|
+
if (i === "string" || i === "secret" || i === "number") return null;
|
|
29
|
+
if (i === "boolean") return !1;
|
|
30
|
+
if (Array.isArray(i) && i.length === 1) {
|
|
31
|
+
if (i[0] === "string") return [];
|
|
32
|
+
if (i[0] === "number") return [];
|
|
33
|
+
}
|
|
34
|
+
throw new Error("Invalid option type: " + i);
|
|
35
35
|
}
|
|
36
|
-
function
|
|
37
|
-
return
|
|
36
|
+
function k(i) {
|
|
37
|
+
return !Array.isArray(i) && typeof i == "object" && i.type ? i.default !== void 0 ? i.default : x(i.type) : x(i);
|
|
38
38
|
}
|
|
39
|
-
function
|
|
40
|
-
return typeof
|
|
41
|
-
type:
|
|
42
|
-
default:
|
|
39
|
+
function g(i) {
|
|
40
|
+
return typeof i == "string" || Array.isArray(i) ? {
|
|
41
|
+
type: i,
|
|
42
|
+
default: k(i),
|
|
43
43
|
description: "",
|
|
44
44
|
alias: [],
|
|
45
45
|
required: !1,
|
|
46
46
|
variadic: !1
|
|
47
47
|
} : {
|
|
48
|
-
type:
|
|
49
|
-
default:
|
|
50
|
-
description:
|
|
51
|
-
alias:
|
|
52
|
-
required:
|
|
53
|
-
variadic:
|
|
48
|
+
type: i.type,
|
|
49
|
+
default: i.default ?? k(i.type),
|
|
50
|
+
description: i.description ?? "",
|
|
51
|
+
alias: i.alias ? Array.isArray(i.alias) ? i.alias : [i.alias] : [],
|
|
52
|
+
required: i.required ?? !1,
|
|
53
|
+
variadic: i.variadic ?? !1
|
|
54
54
|
};
|
|
55
55
|
}
|
|
56
|
-
class
|
|
57
|
-
constructor(
|
|
58
|
-
super(`Invalid option ${
|
|
59
|
-
}
|
|
60
|
-
pretty(
|
|
61
|
-
const
|
|
62
|
-
if (
|
|
63
|
-
|
|
64
|
-
for (const [n,
|
|
65
|
-
const
|
|
66
|
-
|
|
56
|
+
class b extends f {
|
|
57
|
+
constructor(e, t = {}) {
|
|
58
|
+
super(`Invalid option ${e} in not recognized`), this.option = e, this.optionsSchema = t;
|
|
59
|
+
}
|
|
60
|
+
pretty(e) {
|
|
61
|
+
const t = Object.entries(this.optionsSchema);
|
|
62
|
+
if (t.length > 0) {
|
|
63
|
+
e.log(l`\n{yellow Available options}:`);
|
|
64
|
+
for (const [n, s] of t) {
|
|
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
|
+
e.log(l` {green ${m}} ${p} ${r.description || "\b"} {white (${u})}`);
|
|
67
67
|
}
|
|
68
|
-
|
|
68
|
+
e.log("");
|
|
69
69
|
}
|
|
70
|
-
|
|
70
|
+
e.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 V extends f {
|
|
74
|
+
constructor(e) {
|
|
75
|
+
super(`Command "${e}" not found.`), this.command = e;
|
|
76
76
|
}
|
|
77
|
-
pretty(
|
|
78
|
-
|
|
77
|
+
pretty(e) {
|
|
78
|
+
e.log(l`{bgRed ERROR } Command {yellow ${this.command}} not found.`);
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
|
-
class
|
|
82
|
-
constructor(
|
|
83
|
-
super(`
|
|
84
|
-
}
|
|
85
|
-
pretty(t) {
|
|
86
|
-
const e = Object.entries(this.argumentsSchema);
|
|
87
|
-
if (e.length) {
|
|
88
|
-
t.log(a`\n{yellow Available arguments}:`);
|
|
89
|
-
for (const [n, i] of e) {
|
|
90
|
-
const s = f(i), o = Array.isArray(s.type) ? `[${s.type[0]}]` : s.type, l = o ? a`{white (${o})}` : "", u = " ".repeat(20 - n.length);
|
|
91
|
-
t.log(a` {green ${n}} ${u} ${s.description ?? "\b"} ${l}`);
|
|
92
|
-
}
|
|
93
|
-
t.log("");
|
|
94
|
-
}
|
|
95
|
-
t.log(a`{white.bgRed ERROR } Argument {bold.yellow ${this.argument}} is missing in the signature.`);
|
|
81
|
+
class E extends f {
|
|
82
|
+
constructor(e) {
|
|
83
|
+
super(`Argument "${e}" is required.`), this.argument = e;
|
|
96
84
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
constructor(t, e) {
|
|
100
|
-
super(`Missing ${t} in the command signature`), this.option = t, this.optionsSchema = e;
|
|
101
|
-
}
|
|
102
|
-
pretty(t) {
|
|
103
|
-
const e = Object.entries(this.optionsSchema);
|
|
104
|
-
if (e.length) {
|
|
105
|
-
t.log(a`{yellow Available options}:`);
|
|
106
|
-
for (const [n, i] of e) {
|
|
107
|
-
const s = f(i), o = Array.isArray(s.type) ? `[${s.type[0]}]` : s.type, l = o ? a`{white (${o})}` : "", u = " ".repeat(20 - n.length);
|
|
108
|
-
t.log(a` {green ${n}} ${u} ${s.description ?? "\b"} ${l}`);
|
|
109
|
-
}
|
|
110
|
-
t.log("");
|
|
111
|
-
}
|
|
112
|
-
t.log(a`{white.bgRed ERROR } Option {bold.yellow ${this.option}} is missing in the signature.`);
|
|
85
|
+
pretty(e) {
|
|
86
|
+
e.log(l`{white.bgRed ERROR } Argument {bold.yellow ${this.argument}} is required.`);
|
|
113
87
|
}
|
|
114
88
|
}
|
|
115
|
-
class
|
|
116
|
-
constructor(
|
|
117
|
-
super(`Argument "${
|
|
89
|
+
class I extends f {
|
|
90
|
+
constructor(e) {
|
|
91
|
+
super(`Argument "${e}" is required.`), this.option = e;
|
|
118
92
|
}
|
|
119
|
-
pretty(
|
|
120
|
-
|
|
93
|
+
pretty(e) {
|
|
94
|
+
e.log(l`{white.bgRed ERROR } Option {bold.yellow ${this.option}} is required.`);
|
|
121
95
|
}
|
|
122
96
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
super(`Argument "${t}" is required.`), this.option = t;
|
|
126
|
-
}
|
|
127
|
-
pretty(t) {
|
|
128
|
-
t.log(a`{white.bgRed ERROR } Option {bold.yellow ${this.option}} is required.`);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
function $(r, t, e, n) {
|
|
132
|
-
if (r == null)
|
|
97
|
+
function v(i, e, t, n) {
|
|
98
|
+
if (i == null)
|
|
133
99
|
return n ?? null;
|
|
134
|
-
if (
|
|
135
|
-
return String(
|
|
136
|
-
if (
|
|
137
|
-
const
|
|
138
|
-
if (isNaN(
|
|
139
|
-
throw new
|
|
140
|
-
option:
|
|
141
|
-
reason: `Expected a number, got "${
|
|
100
|
+
if (e === "string")
|
|
101
|
+
return String(i);
|
|
102
|
+
if (e === "number") {
|
|
103
|
+
const s = Number(i);
|
|
104
|
+
if (isNaN(s))
|
|
105
|
+
throw new A({
|
|
106
|
+
option: t,
|
|
107
|
+
reason: `Expected a number, got "${i}"`
|
|
142
108
|
});
|
|
143
|
-
return
|
|
144
|
-
}
|
|
145
|
-
if (
|
|
146
|
-
return typeof
|
|
147
|
-
if (Array.isArray(
|
|
148
|
-
const
|
|
149
|
-
if (
|
|
150
|
-
return
|
|
151
|
-
if (
|
|
152
|
-
return
|
|
153
|
-
const
|
|
154
|
-
if (isNaN(
|
|
155
|
-
throw new
|
|
156
|
-
option:
|
|
109
|
+
return s;
|
|
110
|
+
}
|
|
111
|
+
if (e === "boolean")
|
|
112
|
+
return typeof i == "boolean" ? i : i === "true" || i === "1" ? !0 : i === "false" || i === "0" ? !1 : !!i;
|
|
113
|
+
if (Array.isArray(e)) {
|
|
114
|
+
const s = e[0], r = Array.isArray(i) ? i : [i];
|
|
115
|
+
if (s === "string")
|
|
116
|
+
return r.map((o) => String(o));
|
|
117
|
+
if (s === "number")
|
|
118
|
+
return r.map((o) => {
|
|
119
|
+
const u = Number(o);
|
|
120
|
+
if (isNaN(u))
|
|
121
|
+
throw new A({
|
|
122
|
+
option: t,
|
|
157
123
|
reason: `Expected array of numbers, got "${o}" in array`
|
|
158
124
|
});
|
|
159
|
-
return
|
|
125
|
+
return u;
|
|
160
126
|
});
|
|
161
127
|
}
|
|
162
|
-
return
|
|
128
|
+
return i;
|
|
163
129
|
}
|
|
164
|
-
class
|
|
130
|
+
class F {
|
|
165
131
|
options;
|
|
166
132
|
parsedOptions = null;
|
|
167
133
|
arguments;
|
|
168
134
|
parsedArguments = null;
|
|
169
135
|
io;
|
|
170
|
-
|
|
171
|
-
|
|
136
|
+
shouldPromptForMissingOptions = !0;
|
|
137
|
+
constructor(e) {
|
|
138
|
+
this.options = e.options, this.arguments = e.arguments, this.io = e.io;
|
|
172
139
|
}
|
|
173
140
|
// === PUBLIC METHODS ===
|
|
174
141
|
/**
|
|
@@ -178,9 +145,9 @@ class k {
|
|
|
178
145
|
* @throws {InvalidOption} If an unknown option is provided
|
|
179
146
|
* @throws {BadCommandOption} If a value cannot be converted to the expected type
|
|
180
147
|
*/
|
|
181
|
-
init(
|
|
182
|
-
const { _:
|
|
183
|
-
return this.validateUnknownOptions(n), this.parsedOptions = this.handleOptions(n), this.parsedArguments = this.handleArguments(
|
|
148
|
+
init(e) {
|
|
149
|
+
const { _: t, ...n } = q(e);
|
|
150
|
+
return this.validateUnknownOptions(n), this.parsedOptions = this.handleOptions(n), this.parsedArguments = this.handleArguments(t), {
|
|
184
151
|
options: this.parsedOptions,
|
|
185
152
|
arguments: this.parsedArguments
|
|
186
153
|
};
|
|
@@ -190,12 +157,32 @@ class k {
|
|
|
190
157
|
* @throws {Error} If validation fails
|
|
191
158
|
*/
|
|
192
159
|
async validate() {
|
|
193
|
-
for (const
|
|
194
|
-
if (
|
|
195
|
-
throw new
|
|
196
|
-
for (const
|
|
197
|
-
|
|
198
|
-
|
|
160
|
+
for (const e in this.options)
|
|
161
|
+
if (g(this.options[e]).required && (this.parsedOptions?.[e] === void 0 || this.parsedOptions?.[e] === null))
|
|
162
|
+
throw new I(e);
|
|
163
|
+
for (const e in this.arguments) {
|
|
164
|
+
const t = g(this.arguments[e]), n = this.parsedArguments?.[e];
|
|
165
|
+
if (t.required && n == null) {
|
|
166
|
+
if (this.shouldPromptForMissingOptions) {
|
|
167
|
+
const s = await this.promptForArgument(e, t);
|
|
168
|
+
if (s && this.parsedArguments) {
|
|
169
|
+
this.parsedArguments[e] = v(s, t.type, e);
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
throw new E(e);
|
|
174
|
+
}
|
|
175
|
+
if (t.required && t.variadic && Array.isArray(n) && n.length === 0) {
|
|
176
|
+
if (this.shouldPromptForMissingOptions) {
|
|
177
|
+
const s = await this.promptForArgument(e, t);
|
|
178
|
+
if (s && this.parsedArguments) {
|
|
179
|
+
this.parsedArguments[e] = v(s, t.type, e);
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
throw new E(e);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
199
186
|
}
|
|
200
187
|
/**
|
|
201
188
|
* Retrieves a parsed option value by name
|
|
@@ -203,10 +190,17 @@ class k {
|
|
|
203
190
|
* @returns The typed option value
|
|
204
191
|
* @throws {Error} If init() has not been called yet
|
|
205
192
|
*/
|
|
206
|
-
option(
|
|
193
|
+
option(e) {
|
|
207
194
|
if (!this.parsedOptions)
|
|
208
195
|
throw new Error("Options have not been parsed yet. Call init() first.");
|
|
209
|
-
return this.parsedOptions[
|
|
196
|
+
return this.parsedOptions[e];
|
|
197
|
+
}
|
|
198
|
+
setOption(e, t) {
|
|
199
|
+
if (!this.parsedOptions)
|
|
200
|
+
throw new Error("Options have not been parsed yet. Call init() first.");
|
|
201
|
+
if (!(e in this.options))
|
|
202
|
+
throw new b(e, this.options);
|
|
203
|
+
this.parsedOptions[e] = t;
|
|
210
204
|
}
|
|
211
205
|
/**
|
|
212
206
|
* Retrieves a parsed argument value by name
|
|
@@ -214,105 +208,111 @@ class k {
|
|
|
214
208
|
* @returns The typed argument value
|
|
215
209
|
* @throws {Error} If init() has not been called yet
|
|
216
210
|
*/
|
|
217
|
-
argument(
|
|
211
|
+
argument(e) {
|
|
212
|
+
if (!this.parsedArguments)
|
|
213
|
+
throw new Error("Arguments have not been parsed yet. Call init() first.");
|
|
214
|
+
return this.parsedArguments[e];
|
|
215
|
+
}
|
|
216
|
+
setArgument(e, t) {
|
|
218
217
|
if (!this.parsedArguments)
|
|
219
218
|
throw new Error("Arguments have not been parsed yet. Call init() first.");
|
|
220
|
-
|
|
219
|
+
if (!(e in this.arguments))
|
|
220
|
+
throw new b(e, this.arguments);
|
|
221
|
+
this.parsedArguments[e] = t;
|
|
221
222
|
}
|
|
222
223
|
// === PRIVATE HELPERS ===
|
|
223
224
|
/**
|
|
224
225
|
* Validates that all provided options are recognized
|
|
225
226
|
* @throws {InvalidOption} If an unknown option is found
|
|
226
227
|
*/
|
|
227
|
-
validateUnknownOptions(
|
|
228
|
-
const
|
|
228
|
+
validateUnknownOptions(e) {
|
|
229
|
+
const t = /* @__PURE__ */ new Set();
|
|
229
230
|
for (const n in this.options) {
|
|
230
|
-
|
|
231
|
-
const
|
|
232
|
-
for (const
|
|
233
|
-
|
|
231
|
+
t.add(n);
|
|
232
|
+
const s = g(this.options[n]);
|
|
233
|
+
for (const r of s.alias)
|
|
234
|
+
t.add(r);
|
|
234
235
|
}
|
|
235
|
-
for (const n in
|
|
236
|
-
if (!
|
|
237
|
-
throw new
|
|
236
|
+
for (const n in e)
|
|
237
|
+
if (!t.has(n))
|
|
238
|
+
throw new b(n, this.options);
|
|
238
239
|
}
|
|
239
240
|
/**
|
|
240
241
|
* Processes named options from minimist output
|
|
241
242
|
*/
|
|
242
|
-
handleOptions(
|
|
243
|
-
const
|
|
243
|
+
handleOptions(e) {
|
|
244
|
+
const t = {};
|
|
244
245
|
for (const n in this.options) {
|
|
245
|
-
const
|
|
246
|
-
|
|
246
|
+
const s = g(this.options[n]);
|
|
247
|
+
t[n] = this.resolveOptionValue(
|
|
247
248
|
n,
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
"option"
|
|
249
|
+
s,
|
|
250
|
+
e
|
|
251
251
|
);
|
|
252
252
|
}
|
|
253
|
-
return
|
|
253
|
+
return t;
|
|
254
254
|
}
|
|
255
255
|
/**
|
|
256
256
|
* Processes positional arguments from minimist output
|
|
257
257
|
*/
|
|
258
|
-
handleArguments(
|
|
259
|
-
const
|
|
260
|
-
for (const
|
|
261
|
-
const
|
|
262
|
-
if (
|
|
263
|
-
|
|
258
|
+
handleArguments(e) {
|
|
259
|
+
const t = {}, n = [...e];
|
|
260
|
+
for (const s in this.arguments) {
|
|
261
|
+
const r = g(this.arguments[s]);
|
|
262
|
+
if (r.variadic) {
|
|
263
|
+
t[s] = this.handleVariadicArgument(s, r, n);
|
|
264
264
|
continue;
|
|
265
265
|
}
|
|
266
|
-
|
|
266
|
+
t[s] = this.resolveArgumentValue(s, r, n.shift());
|
|
267
267
|
}
|
|
268
|
-
return
|
|
268
|
+
return t;
|
|
269
269
|
}
|
|
270
270
|
/**
|
|
271
271
|
* Handles variadic arguments that consume all remaining positional values
|
|
272
272
|
*/
|
|
273
|
-
handleVariadicArgument(
|
|
274
|
-
return n.length ?
|
|
273
|
+
handleVariadicArgument(e, t, n) {
|
|
274
|
+
return n.length ? v(n, t.type, e, t.default) : t.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(e, t, n) {
|
|
281
|
+
return n === void 0 ? t.default : v(n, t.type, e, t.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(e, t, n) {
|
|
288
288
|
let s;
|
|
289
|
-
const
|
|
290
|
-
for (const
|
|
291
|
-
if (
|
|
292
|
-
s = n[
|
|
289
|
+
const r = [e, ...t.alias];
|
|
290
|
+
for (const o of r)
|
|
291
|
+
if (o in n) {
|
|
292
|
+
s = n[o];
|
|
293
293
|
break;
|
|
294
294
|
}
|
|
295
295
|
if (s === void 0) {
|
|
296
|
-
if (
|
|
297
|
-
throw new
|
|
298
|
-
option:
|
|
299
|
-
reason:
|
|
296
|
+
if (t.required)
|
|
297
|
+
throw new A({
|
|
298
|
+
option: e,
|
|
299
|
+
reason: "Required option is missing"
|
|
300
300
|
});
|
|
301
|
-
return
|
|
301
|
+
return t.default;
|
|
302
302
|
}
|
|
303
|
-
return
|
|
303
|
+
return v(s, t.type, e, t.default);
|
|
304
304
|
}
|
|
305
305
|
optionDefinitions() {
|
|
306
|
-
const
|
|
307
|
-
for (const
|
|
308
|
-
t
|
|
309
|
-
return
|
|
306
|
+
const e = {};
|
|
307
|
+
for (const t in this.options)
|
|
308
|
+
e[t] = g(this.options[t]);
|
|
309
|
+
return e;
|
|
310
310
|
}
|
|
311
311
|
argumentDefinitions() {
|
|
312
|
-
const
|
|
313
|
-
for (const
|
|
314
|
-
t
|
|
315
|
-
return
|
|
312
|
+
const e = {};
|
|
313
|
+
for (const t in this.arguments)
|
|
314
|
+
e[t] = g(this.arguments[t]);
|
|
315
|
+
return e;
|
|
316
316
|
}
|
|
317
317
|
availableOptions() {
|
|
318
318
|
return Object.keys(this.options);
|
|
@@ -320,152 +320,213 @@ class k {
|
|
|
320
320
|
availableArguments() {
|
|
321
321
|
return Object.keys(this.arguments);
|
|
322
322
|
}
|
|
323
|
+
/**
|
|
324
|
+
* Disables prompting for missing argument values
|
|
325
|
+
* Useful for non-interactive environments
|
|
326
|
+
*/
|
|
327
|
+
disablePrompting() {
|
|
328
|
+
return this.shouldPromptForMissingOptions = !1, this;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Prompts the user to provide a missing argument value via CommandIO
|
|
332
|
+
* Used by validate() when shouldPromptForMissingArgs is enabled
|
|
333
|
+
* @param argumentName - The name of the missing argument
|
|
334
|
+
* @param argDef - The argument's definition for type and description
|
|
335
|
+
* @returns The user-provided value, or null if none given
|
|
336
|
+
*/
|
|
337
|
+
async promptForArgument(e, t) {
|
|
338
|
+
if (!Array.isArray(t.type) && !["string", "number", "secret"].includes(t.type))
|
|
339
|
+
return null;
|
|
340
|
+
let n = l`{yellow.bold ${e}} is required`;
|
|
341
|
+
return t.description && (n += l`: {gray (${t.description})}`), n += `
|
|
342
|
+
`, Array.isArray(t.type) ? (n += l`Please provide one or more values, separated by commas:\n`, await this.io.askForList(
|
|
343
|
+
n,
|
|
344
|
+
void 0,
|
|
345
|
+
{
|
|
346
|
+
validate: (s) => s.length ? !0 : "Please enter at least one value"
|
|
347
|
+
}
|
|
348
|
+
)) : await this.io.askForInput(
|
|
349
|
+
n,
|
|
350
|
+
void 0,
|
|
351
|
+
{
|
|
352
|
+
type: t.type === "number" ? "number" : t.type === "secret" ? "password" : "text",
|
|
353
|
+
validate: (s) => {
|
|
354
|
+
if (t.type === "number") {
|
|
355
|
+
const r = Number(s);
|
|
356
|
+
if (isNaN(r))
|
|
357
|
+
return "Please enter a valid number";
|
|
358
|
+
} else if (t.type === "string" && (typeof s != "string" || !s.length))
|
|
359
|
+
return "Please enter a valid text";
|
|
360
|
+
return !0;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
);
|
|
364
|
+
}
|
|
323
365
|
}
|
|
324
|
-
function O(
|
|
325
|
-
return new Array(
|
|
366
|
+
function O(i) {
|
|
367
|
+
return new Array(i + 5).join(" ");
|
|
326
368
|
}
|
|
327
|
-
class
|
|
369
|
+
class H {
|
|
328
370
|
type = "boolean";
|
|
329
371
|
option = "help";
|
|
330
372
|
alias = ["h"];
|
|
331
373
|
default = !1;
|
|
332
|
-
description =
|
|
374
|
+
description = l`Display help for the given command. When no command is given display help for the {green list} command`;
|
|
333
375
|
async handler() {
|
|
334
|
-
const
|
|
335
|
-
const
|
|
376
|
+
const e = this.parser.argumentDefinitions(), t = this.parser.optionDefinitions(), n = Object.entries(e), s = Object.entries(t), r = s.map(([a, d]) => {
|
|
377
|
+
const c = Array.isArray(d.alias) ? d.alias : d.alias ? [d.alias] : [];
|
|
336
378
|
return {
|
|
337
|
-
name:
|
|
338
|
-
...
|
|
339
|
-
optionWithAlias: `--${
|
|
379
|
+
name: a,
|
|
380
|
+
...d,
|
|
381
|
+
optionWithAlias: `--${a}${c.map((h) => `, -${h}`).join("")}`
|
|
340
382
|
};
|
|
341
|
-
}), o = n.filter(([,
|
|
342
|
-
this.io.log(
|
|
343
|
-
const
|
|
383
|
+
}), o = n.filter(([, a]) => a.required);
|
|
384
|
+
this.io.log(l`{yellow Description}:`), this.io.log(l` ${this.description}\n`), this.io.log(l`{yellow Usage}:`), this.io.log(l` ${this.command} ${o.length > 0 ? o.map(([a]) => `<${a}>`).join(" ") : "\b"} [options]`);
|
|
385
|
+
const u = Math.max(...r.map((a) => a.optionWithAlias.length), 0), m = Math.max(...n.map(([a]) => a.length), 0), p = m > u ? m : u;
|
|
344
386
|
if (n.length > 0) {
|
|
345
|
-
this.io.log(
|
|
346
|
-
for (const [
|
|
347
|
-
const
|
|
348
|
-
let
|
|
349
|
-
if (
|
|
350
|
-
const
|
|
351
|
-
|
|
387
|
+
this.io.log(l`\n{yellow Arguments}:`);
|
|
388
|
+
for (const [a, d] of n) {
|
|
389
|
+
const c = O(p - a.length);
|
|
390
|
+
let h = l` {green ${a}} ${c} ${d.description ?? "\b"}`;
|
|
391
|
+
if (d.default !== void 0 && !d.required) {
|
|
392
|
+
const N = (Array.isArray(d.type) ? `[${d.type[0]}]` : d.type) === "array" || Array.isArray(d.type) ? JSON.stringify(d.default) : d.default;
|
|
393
|
+
h += l` {yellow [default: ${N}]}`;
|
|
352
394
|
}
|
|
353
|
-
|
|
395
|
+
d.variadic && (h += l` {white (variadic)}`), this.io.log(h);
|
|
354
396
|
}
|
|
355
397
|
}
|
|
356
|
-
if (
|
|
357
|
-
this.io.log(
|
|
358
|
-
for (const
|
|
359
|
-
const
|
|
360
|
-
let
|
|
361
|
-
if (
|
|
362
|
-
const
|
|
363
|
-
|
|
398
|
+
if (s.length > 0) {
|
|
399
|
+
this.io.log(l`\n{yellow Options}:`);
|
|
400
|
+
for (const a of r) {
|
|
401
|
+
const d = O(p - a.optionWithAlias.length);
|
|
402
|
+
let c = l`{green ${a.optionWithAlias}} ${d} ${a.description ?? "\b"}`;
|
|
403
|
+
if (a.type) {
|
|
404
|
+
const h = Array.isArray(a.type) ? `[${a.type[0]}]` : a.type;
|
|
405
|
+
c += l` {white (${h})}`;
|
|
364
406
|
}
|
|
365
|
-
if (
|
|
366
|
-
const
|
|
367
|
-
|
|
407
|
+
if (a.default !== void 0 && !a.required) {
|
|
408
|
+
const $ = (Array.isArray(a.type) ? `[${a.type[0]}]` : a.type) === "array" || Array.isArray(a.type) ? JSON.stringify(a.default) : a.default;
|
|
409
|
+
c += l` {yellow [default: ${$}]}`;
|
|
368
410
|
}
|
|
369
|
-
this.io.log(
|
|
411
|
+
this.io.log(c);
|
|
370
412
|
}
|
|
371
413
|
}
|
|
372
414
|
if (this.commandsExamples.length > 0) {
|
|
373
|
-
this.io.log(
|
|
374
|
-
let
|
|
375
|
-
|
|
376
|
-
for (const [
|
|
377
|
-
|
|
378
|
-
`), this.io.log(
|
|
415
|
+
this.io.log(l`\n{yellow Examples}:`);
|
|
416
|
+
let a = process.argv[0].split("/").pop();
|
|
417
|
+
a === "node" && (a += " " + process.argv[1].split("/").pop());
|
|
418
|
+
for (const [d, c] of this.commandsExamples.entries())
|
|
419
|
+
d > 0 && this.io.log(""), this.io.log(` ${c.description}
|
|
420
|
+
`), this.io.log(l` {green ${a} ${c.command}}`);
|
|
379
421
|
}
|
|
380
422
|
return -1;
|
|
381
423
|
}
|
|
382
424
|
}
|
|
383
|
-
class
|
|
425
|
+
class S {
|
|
384
426
|
logger;
|
|
385
|
-
constructor(
|
|
386
|
-
this.logger =
|
|
427
|
+
constructor(e) {
|
|
428
|
+
this.logger = e;
|
|
387
429
|
}
|
|
388
430
|
/**
|
|
389
431
|
* Logger methods
|
|
390
432
|
*/
|
|
391
|
-
log(...
|
|
392
|
-
this.logger.log(...
|
|
433
|
+
log(...e) {
|
|
434
|
+
this.logger.log(...e);
|
|
393
435
|
}
|
|
394
|
-
info(...
|
|
395
|
-
this.logger.info(...
|
|
436
|
+
info(...e) {
|
|
437
|
+
this.logger.info(...e);
|
|
396
438
|
}
|
|
397
|
-
warn(...
|
|
398
|
-
this.logger.warn(...
|
|
439
|
+
warn(...e) {
|
|
440
|
+
this.logger.warn(...e);
|
|
399
441
|
}
|
|
400
|
-
error(...
|
|
401
|
-
this.logger.error(...
|
|
442
|
+
error(...e) {
|
|
443
|
+
this.logger.error(...e);
|
|
402
444
|
}
|
|
403
|
-
debug(...
|
|
404
|
-
this.logger.debug(...
|
|
445
|
+
debug(...e) {
|
|
446
|
+
this.logger.debug(...e);
|
|
405
447
|
}
|
|
406
448
|
/**
|
|
407
449
|
* Prompt utils
|
|
408
450
|
*/
|
|
409
|
-
async askForConfirmation(
|
|
410
|
-
return (await
|
|
451
|
+
async askForConfirmation(e = "Do you want to continue?", t) {
|
|
452
|
+
return (await y({
|
|
411
453
|
type: "confirm",
|
|
412
454
|
name: "value",
|
|
413
|
-
message:
|
|
414
|
-
initial:
|
|
455
|
+
message: e,
|
|
456
|
+
initial: t ?? !1
|
|
415
457
|
})).value;
|
|
416
458
|
}
|
|
417
|
-
async askForInput(
|
|
418
|
-
return (await
|
|
459
|
+
async askForInput(e, t, n) {
|
|
460
|
+
return (await y({
|
|
419
461
|
type: "text",
|
|
420
462
|
name: "value",
|
|
421
|
-
message:
|
|
422
|
-
initial:
|
|
463
|
+
message: e,
|
|
464
|
+
initial: t,
|
|
465
|
+
...n
|
|
466
|
+
}))?.value ?? null;
|
|
467
|
+
}
|
|
468
|
+
async askForDate(e, t, n) {
|
|
469
|
+
return (await y({
|
|
470
|
+
type: "date",
|
|
471
|
+
name: "value",
|
|
472
|
+
message: e,
|
|
473
|
+
initial: t,
|
|
474
|
+
...n
|
|
475
|
+
}))?.value ?? null;
|
|
476
|
+
}
|
|
477
|
+
async askForList(e, t, n) {
|
|
478
|
+
return (await y({
|
|
479
|
+
type: "list",
|
|
480
|
+
name: "value",
|
|
481
|
+
message: e,
|
|
482
|
+
initial: t,
|
|
423
483
|
...n
|
|
424
484
|
}))?.value ?? null;
|
|
425
485
|
}
|
|
426
|
-
async askForToggle(
|
|
427
|
-
return (await
|
|
486
|
+
async askForToggle(e, t, n) {
|
|
487
|
+
return (await y({
|
|
428
488
|
type: "toggle",
|
|
429
489
|
name: "value",
|
|
430
|
-
message:
|
|
431
|
-
initial:
|
|
490
|
+
message: e,
|
|
491
|
+
initial: t,
|
|
432
492
|
...n
|
|
433
493
|
}))?.value ?? null;
|
|
434
494
|
}
|
|
435
|
-
async askForSelect(
|
|
436
|
-
if (
|
|
495
|
+
async askForSelect(e, t, n) {
|
|
496
|
+
if (t.length === 0)
|
|
437
497
|
throw new Error("No options provided");
|
|
438
|
-
const
|
|
439
|
-
for (const o of
|
|
440
|
-
typeof o == "string" ?
|
|
441
|
-
return (await
|
|
498
|
+
const s = [];
|
|
499
|
+
for (const o of t)
|
|
500
|
+
typeof o == "string" ? s.push({ title: o, value: o }) : s.push(o);
|
|
501
|
+
return (await y({
|
|
442
502
|
type: "select",
|
|
443
503
|
name: "value",
|
|
444
|
-
message:
|
|
445
|
-
choices:
|
|
504
|
+
message: e,
|
|
505
|
+
choices: s,
|
|
446
506
|
...n
|
|
447
507
|
}))?.value ?? null;
|
|
448
508
|
}
|
|
449
|
-
newLoader(
|
|
450
|
-
let
|
|
451
|
-
const
|
|
452
|
-
|
|
453
|
-
}, n),
|
|
454
|
-
clearInterval(
|
|
509
|
+
newLoader(e = "", t = ["⠙", "⠘", "⠰", "⠴", "⠤", "⠦", "⠆", "⠃", "⠋", "⠉"], n = 100) {
|
|
510
|
+
let s = e, r = null, o = 0;
|
|
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" + t[o++] + " " + s)), o = o % t.length;
|
|
513
|
+
}, n), m = () => {
|
|
514
|
+
clearInterval(u), process.stdout.write(new TextEncoder().encode("\r" + " ".repeat(s.length + 5) + "\r"));
|
|
455
515
|
};
|
|
456
516
|
return {
|
|
457
|
-
[Symbol.dispose]:
|
|
458
|
-
[Symbol.asyncDispose]:
|
|
459
|
-
updateText: (
|
|
460
|
-
|
|
517
|
+
[Symbol.dispose]: m,
|
|
518
|
+
[Symbol.asyncDispose]: m,
|
|
519
|
+
updateText: (p) => {
|
|
520
|
+
r = s, s = p;
|
|
461
521
|
},
|
|
462
|
-
stop:
|
|
522
|
+
stop: m
|
|
463
523
|
};
|
|
464
524
|
}
|
|
465
525
|
}
|
|
466
|
-
class
|
|
526
|
+
class C {
|
|
467
527
|
_command;
|
|
468
528
|
description = "";
|
|
529
|
+
group;
|
|
469
530
|
commandsExamples = [];
|
|
470
531
|
get command() {
|
|
471
532
|
return this._command;
|
|
@@ -473,81 +534,89 @@ class R {
|
|
|
473
534
|
ctx;
|
|
474
535
|
io;
|
|
475
536
|
logger;
|
|
476
|
-
_handler;
|
|
477
537
|
parser;
|
|
538
|
+
disablePromptingFlag = !1;
|
|
539
|
+
_preHandler;
|
|
540
|
+
_handler;
|
|
478
541
|
tmp;
|
|
479
542
|
defaultOptions() {
|
|
480
|
-
return [new
|
|
543
|
+
return [new H()];
|
|
481
544
|
}
|
|
482
|
-
newCommandParser(
|
|
483
|
-
return new
|
|
484
|
-
io:
|
|
485
|
-
options:
|
|
486
|
-
arguments:
|
|
545
|
+
newCommandParser(e) {
|
|
546
|
+
return new F({
|
|
547
|
+
io: e.io,
|
|
548
|
+
options: e.options,
|
|
549
|
+
arguments: e.arguments
|
|
487
550
|
});
|
|
488
551
|
}
|
|
489
|
-
newCommandIO(
|
|
490
|
-
return new
|
|
552
|
+
newCommandIO(e) {
|
|
553
|
+
return new S(e.logger);
|
|
491
554
|
}
|
|
492
|
-
constructor(
|
|
493
|
-
this._command =
|
|
555
|
+
constructor(e, t) {
|
|
556
|
+
this._command = e, this.description = t?.description ?? "", this.group = t?.group;
|
|
494
557
|
const n = this.defaultOptions();
|
|
495
558
|
if (n.length > 0) {
|
|
496
559
|
this.tmp = {
|
|
497
560
|
options: {},
|
|
498
561
|
arguments: {}
|
|
499
562
|
};
|
|
500
|
-
for (const
|
|
501
|
-
this.tmp.options[
|
|
563
|
+
for (const s of n)
|
|
564
|
+
this.tmp.options[s.option] = s;
|
|
502
565
|
}
|
|
503
566
|
}
|
|
504
|
-
|
|
505
|
-
return this.
|
|
567
|
+
disablePrompting() {
|
|
568
|
+
return this.disablePromptingFlag = !0, this;
|
|
506
569
|
}
|
|
507
|
-
|
|
570
|
+
preHandler(e) {
|
|
571
|
+
return this._preHandler = e, this;
|
|
572
|
+
}
|
|
573
|
+
handler(e) {
|
|
574
|
+
return this._handler = e, this;
|
|
575
|
+
}
|
|
576
|
+
options(e) {
|
|
508
577
|
return this.tmp = {
|
|
509
578
|
options: {
|
|
510
579
|
...this.tmp?.options ?? {},
|
|
511
|
-
...
|
|
580
|
+
...e
|
|
512
581
|
},
|
|
513
582
|
arguments: this.tmp?.arguments ?? {}
|
|
514
583
|
}, this;
|
|
515
584
|
}
|
|
516
|
-
arguments(
|
|
585
|
+
arguments(e) {
|
|
517
586
|
return this.tmp = {
|
|
518
587
|
options: this.tmp?.options ?? {},
|
|
519
588
|
arguments: {
|
|
520
589
|
...this.tmp?.arguments ?? {},
|
|
521
|
-
...
|
|
590
|
+
...e
|
|
522
591
|
}
|
|
523
592
|
}, this;
|
|
524
593
|
}
|
|
525
|
-
async run(
|
|
594
|
+
async run(e) {
|
|
526
595
|
if (!this._handler && !this.handle)
|
|
527
596
|
throw new Error(`No handler defined for command ${this.command || "(unknown)"}`);
|
|
528
|
-
let
|
|
529
|
-
if (this.ctx =
|
|
530
|
-
logger:
|
|
531
|
-
}),
|
|
532
|
-
const
|
|
597
|
+
let t;
|
|
598
|
+
if (this.ctx = e.ctx, this.logger = e.logger, this.io = this.newCommandIO({
|
|
599
|
+
logger: e.logger
|
|
600
|
+
}), e && "args" in e) {
|
|
601
|
+
const r = this.tmp?.options ?? {};
|
|
533
602
|
for (const o of this.defaultOptions())
|
|
534
|
-
o.option in
|
|
603
|
+
o.option in r || (r[o.option] = o);
|
|
535
604
|
this.parser = this.newCommandParser({
|
|
536
605
|
io: this.io,
|
|
537
|
-
options:
|
|
606
|
+
options: r,
|
|
538
607
|
arguments: this.tmp?.arguments ?? {}
|
|
539
|
-
}),
|
|
608
|
+
}), t = this.parser.init(e.args);
|
|
540
609
|
for (const o of this.defaultOptions())
|
|
541
|
-
if (
|
|
542
|
-
const
|
|
543
|
-
if (
|
|
544
|
-
return
|
|
610
|
+
if (t.options[o.option] === !0) {
|
|
611
|
+
const u = await o.handler.call(this);
|
|
612
|
+
if (u && u !== 0)
|
|
613
|
+
return u;
|
|
545
614
|
}
|
|
546
|
-
await this.parser.validate();
|
|
615
|
+
this.disablePromptingFlag && this.parser.disablePrompting(), await this.parser.validate();
|
|
547
616
|
} else
|
|
548
|
-
|
|
549
|
-
options:
|
|
550
|
-
arguments:
|
|
617
|
+
t = {
|
|
618
|
+
options: e.options,
|
|
619
|
+
arguments: e.arguments
|
|
551
620
|
};
|
|
552
621
|
const n = this.preHandle ? await this.preHandle() : null;
|
|
553
622
|
if (n && n !== 0)
|
|
@@ -556,105 +625,42 @@ class R {
|
|
|
556
625
|
this._handler = this.handle.bind(this);
|
|
557
626
|
else if (!this._handler)
|
|
558
627
|
throw new Error(`No handler defined for command ${this.command || "(unknown)"}`);
|
|
559
|
-
return await this._handler(
|
|
628
|
+
return await this._handler(e.ctx, t) ?? 0;
|
|
560
629
|
}
|
|
561
630
|
}
|
|
562
|
-
class
|
|
631
|
+
class w extends F {
|
|
563
632
|
command;
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
t.helperDefinitions,
|
|
570
|
-
t.defaultOptions
|
|
633
|
+
constructor(e) {
|
|
634
|
+
const t = w.parseSignature(
|
|
635
|
+
e.signature,
|
|
636
|
+
e.helperDefinitions,
|
|
637
|
+
e.defaultOptions
|
|
571
638
|
);
|
|
572
|
-
super({ io:
|
|
639
|
+
super({ io: e.io, options: t.options, arguments: t.arguments }), this.command = t.command;
|
|
573
640
|
}
|
|
574
641
|
/**
|
|
575
642
|
* Parses command signature string into command name and parameter schemas
|
|
576
643
|
* Example: "migrate {name} {--force}" -> { command: "migrate", arguments: {name: ...}, options: {force: ...} }
|
|
577
644
|
*/
|
|
578
|
-
static parseSignature(
|
|
579
|
-
const [
|
|
580
|
-
for (const
|
|
581
|
-
const { name:
|
|
582
|
-
|
|
583
|
-
|
|
645
|
+
static parseSignature(e, t, n) {
|
|
646
|
+
const [s, ...r] = e.split(/\{(.*?)\}/g).map((m) => m.trim()).filter(Boolean), o = {}, u = {};
|
|
647
|
+
for (const m of r) {
|
|
648
|
+
const { name: p, isOption: a, definition: d } = w.parseParamSignature(
|
|
649
|
+
m,
|
|
650
|
+
t
|
|
584
651
|
);
|
|
585
|
-
|
|
652
|
+
a ? o[p] = d : u[p] = d;
|
|
586
653
|
}
|
|
587
|
-
for (const
|
|
588
|
-
o[
|
|
589
|
-
type:
|
|
590
|
-
required:
|
|
591
|
-
alias:
|
|
592
|
-
variadic:
|
|
593
|
-
description:
|
|
594
|
-
default:
|
|
654
|
+
for (const m of n)
|
|
655
|
+
o[m.option] = {
|
|
656
|
+
type: m.type,
|
|
657
|
+
required: m.required,
|
|
658
|
+
alias: m.alias,
|
|
659
|
+
variadic: m.variadic ?? !1,
|
|
660
|
+
description: m.description,
|
|
661
|
+
default: m.default ?? null
|
|
595
662
|
};
|
|
596
|
-
return { command:
|
|
597
|
-
}
|
|
598
|
-
// === PUBLIC API ===
|
|
599
|
-
/**
|
|
600
|
-
* Retrieves an option value by name, with signature validation
|
|
601
|
-
*/
|
|
602
|
-
option(t) {
|
|
603
|
-
if (!this.optionsSchema[t])
|
|
604
|
-
throw new A(t, this.optionsSchema);
|
|
605
|
-
return super.option(t);
|
|
606
|
-
}
|
|
607
|
-
/**
|
|
608
|
-
* Sets an option value programmatically
|
|
609
|
-
*/
|
|
610
|
-
setOption(t, e) {
|
|
611
|
-
if (!this.optionsSchema[t])
|
|
612
|
-
throw new A(t, this.optionsSchema);
|
|
613
|
-
this.parsedOptions && (this.parsedOptions[t] = e);
|
|
614
|
-
}
|
|
615
|
-
/**
|
|
616
|
-
* Retrieves the description/help text for an option
|
|
617
|
-
*/
|
|
618
|
-
optionHelp(t) {
|
|
619
|
-
if (!this.optionsSchema[t])
|
|
620
|
-
throw new A(t, this.optionsSchema);
|
|
621
|
-
return f(this.optionsSchema[t]).description;
|
|
622
|
-
}
|
|
623
|
-
/**
|
|
624
|
-
* Retrieves the description/help text for an argument
|
|
625
|
-
*/
|
|
626
|
-
argumentHelp(t) {
|
|
627
|
-
if (!this.argumentsSchema[t])
|
|
628
|
-
throw new v(t, this.argumentsSchema);
|
|
629
|
-
return f(this.argumentsSchema[t]).description;
|
|
630
|
-
}
|
|
631
|
-
/**
|
|
632
|
-
* Retrieves an argument value by name, with signature validation
|
|
633
|
-
*/
|
|
634
|
-
argument(t) {
|
|
635
|
-
if (!this.argumentsSchema[t])
|
|
636
|
-
throw new v(t, this.argumentsSchema);
|
|
637
|
-
return super.argument(t);
|
|
638
|
-
}
|
|
639
|
-
/**
|
|
640
|
-
* Sets an argument value programmatically
|
|
641
|
-
*/
|
|
642
|
-
setArgument(t, e) {
|
|
643
|
-
if (!this.argumentsSchema[t])
|
|
644
|
-
throw new v(t, this.argumentsSchema);
|
|
645
|
-
this.parsedArguments && (this.parsedArguments[t] = e);
|
|
646
|
-
}
|
|
647
|
-
/**
|
|
648
|
-
* Returns all argument definitions from the signature
|
|
649
|
-
*/
|
|
650
|
-
getArgumentSignatures() {
|
|
651
|
-
return this.argumentsSchema;
|
|
652
|
-
}
|
|
653
|
-
/**
|
|
654
|
-
* Returns all option definitions from the signature
|
|
655
|
-
*/
|
|
656
|
-
getOptionSignatures() {
|
|
657
|
-
return this.optionsSchema;
|
|
663
|
+
return { command: s, options: o, arguments: u };
|
|
658
664
|
}
|
|
659
665
|
/**
|
|
660
666
|
* Parses a single parameter signature like "{name}" or "{--force}" or "{files*}"
|
|
@@ -670,9 +676,9 @@ class g extends k {
|
|
|
670
676
|
* - {--opt=} -> string option
|
|
671
677
|
* - {--opt|o} -> option with alias
|
|
672
678
|
*/
|
|
673
|
-
static parseParamSignature(
|
|
674
|
-
let n =
|
|
675
|
-
const
|
|
679
|
+
static parseParamSignature(e, t) {
|
|
680
|
+
let n = e, s = !1;
|
|
681
|
+
const r = {
|
|
676
682
|
required: !0,
|
|
677
683
|
type: "string",
|
|
678
684
|
description: void 0,
|
|
@@ -680,73 +686,28 @@ class g extends k {
|
|
|
680
686
|
variadic: !1
|
|
681
687
|
};
|
|
682
688
|
if (n.includes(":")) {
|
|
683
|
-
const [o,
|
|
684
|
-
n = o.trim(),
|
|
689
|
+
const [o, u] = n.split(":");
|
|
690
|
+
n = o.trim(), r.description = u.trim();
|
|
685
691
|
}
|
|
686
692
|
if (n.includes("=")) {
|
|
687
|
-
const [o,
|
|
688
|
-
n = o.trim(),
|
|
689
|
-
} else n.startsWith("--") && (
|
|
693
|
+
const [o, u] = n.split("=");
|
|
694
|
+
n = o.trim(), r.default = u.trim(), r.required = !1, r.default.length ? r.default === "true" ? (r.default = !0, r.type = "boolean") : r.default === "false" && (r.default = !1, r.type = "boolean") : r.default = null;
|
|
695
|
+
} else n.startsWith("--") && (r.required = !1, r.default = !1, r.type = "boolean");
|
|
690
696
|
if (n.includes("|")) {
|
|
691
|
-
const [o, ...
|
|
692
|
-
n = o.trim(),
|
|
693
|
-
}
|
|
694
|
-
return n.startsWith("--") && (i = !0, n = n.slice(2)), s.default === "*" && (s.default = [], s.type = ["string"]), n.endsWith("?") && (s.required = !1, n = n.slice(0, -1)), n.endsWith("*") && (s.type = ["string"], s.variadic = !0, s.default = [], n = n.slice(0, -1)), s.description = s.description ?? e[n] ?? e[`--${n}`], { name: n, isOption: i, definition: s };
|
|
695
|
-
}
|
|
696
|
-
/**
|
|
697
|
-
* Validates that all required arguments are present
|
|
698
|
-
* If missing, prompts the user via CommandIO to provide them
|
|
699
|
-
* @throws {MissingRequiredArgumentValue} If a required argument cannot be obtained
|
|
700
|
-
*/
|
|
701
|
-
async validate() {
|
|
702
|
-
for (const t in this.argumentsSchema) {
|
|
703
|
-
const e = f(this.argumentsSchema[t]), n = this.argument(t);
|
|
704
|
-
if (!n && e.required) {
|
|
705
|
-
const i = await this.promptForArgument(t, e);
|
|
706
|
-
if (i)
|
|
707
|
-
this.setArgument(t, i);
|
|
708
|
-
else
|
|
709
|
-
throw new C(t);
|
|
710
|
-
}
|
|
711
|
-
if (e.variadic && e.required && typeof n == "object" && !n?.length)
|
|
712
|
-
throw new C(t);
|
|
697
|
+
const [o, ...u] = n.split("|");
|
|
698
|
+
n = o.trim(), r.alias = u.map((m) => m.trim());
|
|
713
699
|
}
|
|
714
|
-
|
|
715
|
-
/**
|
|
716
|
-
* Prompts the user to provide a missing argument value via CommandIO
|
|
717
|
-
*/
|
|
718
|
-
async promptForArgument(t, e) {
|
|
719
|
-
if (e.type !== "string")
|
|
720
|
-
return null;
|
|
721
|
-
let n = a`{yellow.bold ${t}} is required`;
|
|
722
|
-
return e.description && (n += a`: {gray (${e.description})}`), n += `
|
|
723
|
-
`, await this.io.askForInput(
|
|
724
|
-
n,
|
|
725
|
-
e.default,
|
|
726
|
-
{
|
|
727
|
-
validate: (i) => i?.trim()?.length ? !0 : `${t} cannot be empty`
|
|
728
|
-
}
|
|
729
|
-
);
|
|
730
|
-
}
|
|
731
|
-
optionValues() {
|
|
732
|
-
if (!this.parsedOptions)
|
|
733
|
-
throw new Error("Options have not been parsed yet. Call init() first.");
|
|
734
|
-
return this.parsedOptions;
|
|
735
|
-
}
|
|
736
|
-
argumentValues() {
|
|
737
|
-
if (!this.parsedArguments)
|
|
738
|
-
throw new Error("Arguments have not been parsed yet. Call init() first.");
|
|
739
|
-
return this.parsedArguments;
|
|
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 ?? t[n] ?? t[`--${n}`], { name: n, isOption: s, definition: r };
|
|
740
701
|
}
|
|
741
702
|
}
|
|
742
|
-
class
|
|
703
|
+
class z extends C {
|
|
743
704
|
helperDefinitions = {};
|
|
744
705
|
get command() {
|
|
745
706
|
return this.parser ? this.parser.command : this.signature.split(" ")[0];
|
|
746
707
|
}
|
|
747
|
-
newCommandParser(
|
|
748
|
-
return new
|
|
749
|
-
io:
|
|
708
|
+
newCommandParser(e) {
|
|
709
|
+
return new w({
|
|
710
|
+
io: e.io,
|
|
750
711
|
signature: this.signature,
|
|
751
712
|
helperDefinitions: this.helperDefinitions,
|
|
752
713
|
defaultOptions: this.defaultOptions()
|
|
@@ -755,113 +716,66 @@ class K extends R {
|
|
|
755
716
|
constructor() {
|
|
756
717
|
super("");
|
|
757
718
|
}
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
this.parser.setOption(t, e);
|
|
761
|
-
}
|
|
762
|
-
setArgument(t, e) {
|
|
763
|
-
this.parser instanceof g && this.parser.setArgument(t, e);
|
|
719
|
+
option(e, t = null) {
|
|
720
|
+
return this.parser instanceof w ? this.parser.option(e) ?? t : t;
|
|
764
721
|
}
|
|
765
|
-
|
|
766
|
-
return this.parser instanceof
|
|
767
|
-
}
|
|
768
|
-
optionBoolean(t, e = !1) {
|
|
769
|
-
return this.parser instanceof g ? this.parser.option(t) ?? e : e;
|
|
770
|
-
}
|
|
771
|
-
optionArray(t, e = []) {
|
|
772
|
-
if (this.parser instanceof g) {
|
|
773
|
-
const n = this.parser.option(t);
|
|
774
|
-
if (!Array.isArray(n))
|
|
775
|
-
throw new Error(`Option ${t} is not an array`);
|
|
776
|
-
if (n.length)
|
|
777
|
-
return n;
|
|
778
|
-
}
|
|
779
|
-
return e;
|
|
780
|
-
}
|
|
781
|
-
optionNumber(t, e = null) {
|
|
782
|
-
if (this.parser instanceof g) {
|
|
783
|
-
const n = this.parser.option(t);
|
|
784
|
-
return n ? typeof n == "number" ? n : parseInt(n) : e;
|
|
785
|
-
}
|
|
786
|
-
return e;
|
|
787
|
-
}
|
|
788
|
-
argument(t, e = null) {
|
|
789
|
-
return this.parser instanceof g ? this.parser.argument(t) ?? e : e;
|
|
790
|
-
}
|
|
791
|
-
argumentArray(t, e = []) {
|
|
792
|
-
if (this.parser instanceof g) {
|
|
793
|
-
const n = this.parser.argument(t);
|
|
794
|
-
if (!Array.isArray(n))
|
|
795
|
-
throw new Error(`Argument ${t} is not an array`);
|
|
796
|
-
if (n?.length)
|
|
797
|
-
return n;
|
|
798
|
-
}
|
|
799
|
-
return e;
|
|
800
|
-
}
|
|
801
|
-
argumentBoolean(t, e = !1) {
|
|
802
|
-
return this.parser instanceof g ? this.parser.argument(t) ?? e : e;
|
|
803
|
-
}
|
|
804
|
-
argumentNumber(t, e = null) {
|
|
805
|
-
if (this.parser instanceof g) {
|
|
806
|
-
const n = this.parser.argument(t);
|
|
807
|
-
return n ? typeof n == "number" ? n : parseInt(n) : e;
|
|
808
|
-
}
|
|
809
|
-
return e;
|
|
722
|
+
argument(e, t = null) {
|
|
723
|
+
return this.parser instanceof w ? this.parser.argument(e) ?? t : t;
|
|
810
724
|
}
|
|
811
725
|
// Prompt utils
|
|
812
|
-
async askForConfirmation(...
|
|
813
|
-
return this.io.askForConfirmation(...
|
|
726
|
+
async askForConfirmation(...e) {
|
|
727
|
+
return this.io.askForConfirmation(...e);
|
|
814
728
|
}
|
|
815
|
-
async askForInput(...
|
|
816
|
-
return this.io.askForInput(...
|
|
729
|
+
async askForInput(...e) {
|
|
730
|
+
return this.io.askForInput(...e);
|
|
817
731
|
}
|
|
818
|
-
async askForSelect(...
|
|
819
|
-
return this.io.askForSelect(...
|
|
732
|
+
async askForSelect(...e) {
|
|
733
|
+
return this.io.askForSelect(...e);
|
|
820
734
|
}
|
|
821
|
-
newLoader(...
|
|
822
|
-
return this.io.newLoader(...
|
|
735
|
+
newLoader(...e) {
|
|
736
|
+
return this.io.newLoader(...e);
|
|
823
737
|
}
|
|
824
738
|
}
|
|
825
|
-
class
|
|
739
|
+
class L {
|
|
826
740
|
level;
|
|
827
|
-
constructor(
|
|
828
|
-
this.level =
|
|
741
|
+
constructor(e = {}) {
|
|
742
|
+
this.level = e.level ?? "info";
|
|
829
743
|
}
|
|
830
|
-
shouldLog(
|
|
831
|
-
const
|
|
832
|
-
return
|
|
744
|
+
shouldLog(e) {
|
|
745
|
+
const t = ["debug", "info", "warn", "error"], n = t.indexOf(this.level);
|
|
746
|
+
return t.indexOf(e) >= n;
|
|
833
747
|
}
|
|
834
|
-
setLevel(
|
|
835
|
-
this.level =
|
|
748
|
+
setLevel(e) {
|
|
749
|
+
this.level = e;
|
|
836
750
|
}
|
|
837
751
|
getLevel() {
|
|
838
752
|
return this.level;
|
|
839
753
|
}
|
|
840
|
-
log(...
|
|
841
|
-
console.log(...
|
|
754
|
+
log(...e) {
|
|
755
|
+
console.log(...e);
|
|
842
756
|
}
|
|
843
|
-
info(...
|
|
844
|
-
this.shouldLog("info") && console.log(...
|
|
757
|
+
info(...e) {
|
|
758
|
+
this.shouldLog("info") && console.log(...e);
|
|
845
759
|
}
|
|
846
|
-
warn(...
|
|
847
|
-
this.shouldLog("warn") && console.warn(...
|
|
760
|
+
warn(...e) {
|
|
761
|
+
this.shouldLog("warn") && console.warn(...e);
|
|
848
762
|
}
|
|
849
|
-
error(...
|
|
850
|
-
this.shouldLog("error") && console.error(...
|
|
763
|
+
error(...e) {
|
|
764
|
+
this.shouldLog("error") && console.error(...e);
|
|
851
765
|
}
|
|
852
|
-
debug(...
|
|
853
|
-
this.shouldLog("debug") && console.log(...
|
|
766
|
+
debug(...e) {
|
|
767
|
+
this.shouldLog("debug") && console.log(...e);
|
|
854
768
|
}
|
|
855
769
|
}
|
|
856
|
-
class
|
|
770
|
+
class j {
|
|
857
771
|
commands = {};
|
|
858
772
|
io;
|
|
859
773
|
logger;
|
|
860
774
|
get CommandIOClass() {
|
|
861
|
-
return
|
|
775
|
+
return S;
|
|
862
776
|
}
|
|
863
|
-
constructor(
|
|
864
|
-
this.logger =
|
|
777
|
+
constructor(e) {
|
|
778
|
+
this.logger = e ?? new L(), this.io = new this.CommandIOClass(this.logger);
|
|
865
779
|
}
|
|
866
780
|
getAvailableCommands() {
|
|
867
781
|
return Object.keys(this.commands);
|
|
@@ -869,192 +783,186 @@ class M {
|
|
|
869
783
|
getCommands() {
|
|
870
784
|
return Object.values(this.commands);
|
|
871
785
|
}
|
|
872
|
-
commandResolver = async (
|
|
873
|
-
let
|
|
874
|
-
if (!
|
|
875
|
-
throw new Error(`The command at path ${
|
|
876
|
-
|
|
877
|
-
let n;
|
|
878
|
-
if (typeof e == "function")
|
|
879
|
-
n = new e();
|
|
880
|
-
else if (e instanceof R)
|
|
881
|
-
n = e;
|
|
882
|
-
else
|
|
883
|
-
throw new Error(`The command at path ${t} is not a valid command class.`);
|
|
884
|
-
return n;
|
|
786
|
+
commandResolver = async (e) => {
|
|
787
|
+
let t = (await import(e)).default;
|
|
788
|
+
if (!t)
|
|
789
|
+
throw new Error(`The command at path ${e} does not have a default export.`);
|
|
790
|
+
return t?.default && (t = t.default), typeof t == "function" ? new t() : t instanceof C ? t : null;
|
|
885
791
|
};
|
|
886
|
-
setCommandResolver(
|
|
887
|
-
this.commandResolver =
|
|
792
|
+
setCommandResolver(e) {
|
|
793
|
+
this.commandResolver = e;
|
|
794
|
+
}
|
|
795
|
+
fileCheck = (e) => !0;
|
|
796
|
+
setFileCheck(e) {
|
|
797
|
+
return this.fileCheck = e, this;
|
|
888
798
|
}
|
|
889
|
-
registerCommand(
|
|
890
|
-
const n =
|
|
799
|
+
registerCommand(e, t = !1) {
|
|
800
|
+
const n = e.command;
|
|
891
801
|
if (!n)
|
|
892
802
|
throw new Error("Command signature is invalid, it must have a command name.");
|
|
893
|
-
if (!
|
|
803
|
+
if (!t && this.commands[n])
|
|
894
804
|
throw new Error(`Command ${n} already registered.`);
|
|
895
|
-
this.commands[n] =
|
|
805
|
+
this.commands[n] = e;
|
|
896
806
|
}
|
|
897
|
-
async loadCommandsPath(
|
|
898
|
-
for await (const
|
|
807
|
+
async loadCommandsPath(e) {
|
|
808
|
+
for await (const t of this.listCommandsFiles(e))
|
|
899
809
|
try {
|
|
900
|
-
const n = await this.commandResolver(
|
|
901
|
-
this.registerCommand(n);
|
|
810
|
+
const n = await this.commandResolver(t);
|
|
811
|
+
n instanceof C && this.registerCommand(n);
|
|
902
812
|
} catch (n) {
|
|
903
|
-
throw new Error(`Command ${
|
|
813
|
+
throw new Error(`Command ${t} failed to load. ${n}`, {
|
|
904
814
|
cause: n
|
|
905
815
|
});
|
|
906
816
|
}
|
|
907
817
|
}
|
|
908
|
-
async runCommand(
|
|
909
|
-
const
|
|
910
|
-
if (!
|
|
911
|
-
const o = await this.suggestCommand(
|
|
912
|
-
return o ? await this.runCommand(
|
|
818
|
+
async runCommand(e, t, ...n) {
|
|
819
|
+
const s = typeof t == "string" ? this.commands[t] : t, r = typeof t == "string" ? t : s.command;
|
|
820
|
+
if (!s) {
|
|
821
|
+
const o = await this.suggestCommand(r);
|
|
822
|
+
return o ? await this.runCommand(e, o, ...n) : 1;
|
|
913
823
|
}
|
|
914
|
-
return await
|
|
915
|
-
ctx:
|
|
824
|
+
return await s.run({
|
|
825
|
+
ctx: e,
|
|
916
826
|
logger: this.logger,
|
|
917
827
|
args: n
|
|
918
828
|
}) ?? 0;
|
|
919
829
|
}
|
|
920
|
-
async suggestCommand(
|
|
921
|
-
const
|
|
830
|
+
async suggestCommand(e) {
|
|
831
|
+
const t = this.getAvailableCommands(), { bestMatch: n, bestMatchIndex: s, ratings: r } = P.findBestMatch(e, t), o = r.filter((u) => u.rating > 0.3).map((u) => u.target);
|
|
922
832
|
if (n.rating > 0 && o.length <= 1 || n.rating > 0.7 && o.length > 1) {
|
|
923
|
-
const
|
|
924
|
-
return await this.askRunSimilarCommand(
|
|
833
|
+
const u = t[s];
|
|
834
|
+
return await this.askRunSimilarCommand(e, u) ? u : null;
|
|
925
835
|
}
|
|
926
836
|
if (o.length) {
|
|
927
|
-
this.io.error(
|
|
928
|
-
const
|
|
929
|
-
|
|
837
|
+
this.io.error(l`{bgRed ERROR } Command {yellow ${e}} not found.\n`);
|
|
838
|
+
const u = await this.io.askForSelect(
|
|
839
|
+
l`{green Did you mean to run one of these commands instead?}`,
|
|
930
840
|
o
|
|
931
841
|
);
|
|
932
|
-
if (
|
|
933
|
-
return
|
|
842
|
+
if (u)
|
|
843
|
+
return u;
|
|
934
844
|
}
|
|
935
|
-
throw new
|
|
845
|
+
throw new V(e);
|
|
936
846
|
}
|
|
937
|
-
async askRunSimilarCommand(
|
|
938
|
-
return this.io.error(
|
|
847
|
+
async askRunSimilarCommand(e, t) {
|
|
848
|
+
return this.io.error(l`{bgRed ERROR } Command {yellow ${e}} not found.\n`), this.io.askForConfirmation(l`{green Do you want to run {yellow ${t}} instead?} `);
|
|
939
849
|
}
|
|
940
|
-
async *listCommandsFiles(
|
|
941
|
-
const
|
|
942
|
-
for (const n of
|
|
943
|
-
const
|
|
850
|
+
async *listCommandsFiles(e) {
|
|
851
|
+
const t = D.readdirSync(e, { withFileTypes: !0 });
|
|
852
|
+
for (const n of t) {
|
|
853
|
+
const s = R.resolve(e, n.name);
|
|
944
854
|
if (n.isDirectory())
|
|
945
|
-
yield* this.listCommandsFiles(
|
|
855
|
+
yield* this.listCommandsFiles(R.resolve(e, n.name));
|
|
946
856
|
else {
|
|
947
|
-
if (!
|
|
857
|
+
if (!s.endsWith(".ts") && !s.endsWith(".js") && !s.endsWith(".mjs") && !s.endsWith(".cjs"))
|
|
948
858
|
continue;
|
|
949
|
-
yield
|
|
859
|
+
yield s;
|
|
950
860
|
}
|
|
951
861
|
}
|
|
952
862
|
}
|
|
953
863
|
}
|
|
954
|
-
class
|
|
955
|
-
constructor(
|
|
864
|
+
class W extends C {
|
|
865
|
+
constructor(e) {
|
|
956
866
|
super("help", {
|
|
957
|
-
description:
|
|
958
|
-
}), this.opts =
|
|
867
|
+
description: l.bold("Show help information about the CLI and its commands")
|
|
868
|
+
}), this.opts = e;
|
|
959
869
|
}
|
|
960
870
|
async handle() {
|
|
961
|
-
const
|
|
962
|
-
this.io.log(
|
|
871
|
+
const e = this.opts.commandRegistry.getCommands(), t = this.opts.cliName ?? "Bob CLI", n = this.opts.cliVersion ?? "0.0.0", s = (await import("./package-B_VxJm2q.js"))?.default?.version ?? "0.0.0";
|
|
872
|
+
this.io.log(l`${t} {green ${n}} (core: {yellow ${s}})
|
|
963
873
|
|
|
964
874
|
{yellow Usage}:
|
|
965
875
|
command [options] [arguments]
|
|
966
876
|
|
|
967
877
|
{yellow Available commands}:
|
|
968
878
|
`);
|
|
969
|
-
const
|
|
970
|
-
for (const
|
|
971
|
-
const
|
|
972
|
-
o[
|
|
879
|
+
const r = Math.max(...e.map((m) => m.command.length)) ?? 0, o = {};
|
|
880
|
+
for (const m of e) {
|
|
881
|
+
const p = m.group ?? m.command.split(":")[0];
|
|
882
|
+
o[p] || (o[p] = []), o[p].push(m);
|
|
973
883
|
}
|
|
974
|
-
const
|
|
975
|
-
for (const [
|
|
976
|
-
const
|
|
977
|
-
|
|
978
|
-
const
|
|
979
|
-
for (const
|
|
980
|
-
let
|
|
981
|
-
|
|
884
|
+
const u = Object.entries(o).sort(([m], [p]) => m.toLowerCase().localeCompare(p.toLowerCase())).sort(([, m], [, p]) => m.length - p.length);
|
|
885
|
+
for (const [m, p] of u) {
|
|
886
|
+
const a = p.length > 1;
|
|
887
|
+
a && this.io.log(l`{yellow ${m}}:`);
|
|
888
|
+
const d = p.sort((c, h) => c.command.toLowerCase().localeCompare(h.command.toLowerCase()));
|
|
889
|
+
for (const c of d) {
|
|
890
|
+
let h = O(r - c.command.length);
|
|
891
|
+
a && (h = h.slice(2)), this.io.log(l`${a ? " " : ""}{green ${c.command}} ${h} ${c.description}`);
|
|
982
892
|
}
|
|
983
893
|
}
|
|
984
894
|
}
|
|
985
895
|
}
|
|
986
|
-
class
|
|
896
|
+
class T {
|
|
987
897
|
logger;
|
|
988
|
-
constructor(
|
|
989
|
-
this.logger =
|
|
898
|
+
constructor(e) {
|
|
899
|
+
this.logger = e;
|
|
990
900
|
}
|
|
991
|
-
handle(
|
|
992
|
-
if (
|
|
993
|
-
return
|
|
994
|
-
throw
|
|
901
|
+
handle(e) {
|
|
902
|
+
if (e instanceof f)
|
|
903
|
+
return e.pretty(this.logger), -1;
|
|
904
|
+
throw e;
|
|
995
905
|
}
|
|
996
906
|
}
|
|
997
|
-
class
|
|
907
|
+
class J {
|
|
998
908
|
ctx;
|
|
999
909
|
logger;
|
|
1000
910
|
commandRegistry;
|
|
1001
911
|
exceptionHandler;
|
|
1002
912
|
helpCommand;
|
|
1003
|
-
newCommandRegistry(
|
|
1004
|
-
return new
|
|
913
|
+
newCommandRegistry(e) {
|
|
914
|
+
return new j(e.logger);
|
|
1005
915
|
}
|
|
1006
|
-
newHelpCommand(
|
|
1007
|
-
return new
|
|
916
|
+
newHelpCommand(e) {
|
|
917
|
+
return new W(e);
|
|
1008
918
|
}
|
|
1009
|
-
newExceptionHandler(
|
|
1010
|
-
return new
|
|
919
|
+
newExceptionHandler(e) {
|
|
920
|
+
return new T(e.logger);
|
|
1011
921
|
}
|
|
1012
|
-
constructor(
|
|
1013
|
-
this.ctx =
|
|
922
|
+
constructor(e = {}) {
|
|
923
|
+
this.ctx = e.ctx, this.logger = e.logger ?? new L(), this.commandRegistry = this.newCommandRegistry({
|
|
1014
924
|
logger: this.logger
|
|
1015
925
|
}), this.exceptionHandler = this.newExceptionHandler({
|
|
1016
926
|
logger: this.logger
|
|
1017
927
|
}), this.helpCommand = this.newHelpCommand({
|
|
1018
|
-
cliName:
|
|
1019
|
-
cliVersion:
|
|
928
|
+
cliName: e.name,
|
|
929
|
+
cliVersion: e.version,
|
|
1020
930
|
commandRegistry: this.commandRegistry
|
|
1021
931
|
});
|
|
1022
932
|
}
|
|
1023
|
-
setCommandResolver(
|
|
1024
|
-
this.commandRegistry.setCommandResolver(
|
|
933
|
+
setCommandResolver(e) {
|
|
934
|
+
this.commandRegistry.setCommandResolver(e);
|
|
1025
935
|
}
|
|
1026
|
-
async withCommands(...
|
|
1027
|
-
for (const
|
|
1028
|
-
typeof
|
|
936
|
+
async withCommands(...e) {
|
|
937
|
+
for (const t of e)
|
|
938
|
+
typeof t == "string" ? await this.commandRegistry.loadCommandsPath(t) : typeof t == "function" ? this.registerCommand(new t()) : this.registerCommand(t);
|
|
1029
939
|
}
|
|
1030
|
-
async runCommand(
|
|
1031
|
-
return
|
|
940
|
+
async runCommand(e, ...t) {
|
|
941
|
+
return e ? await this.commandRegistry.runCommand(this.ctx, e, ...t).catch(this.exceptionHandler.handle.bind(this.exceptionHandler)) : await this.runHelpCommand();
|
|
1032
942
|
}
|
|
1033
943
|
async runHelpCommand() {
|
|
1034
944
|
return await this.runCommand(this.helpCommand);
|
|
1035
945
|
}
|
|
1036
|
-
registerCommand(
|
|
1037
|
-
this.commandRegistry.registerCommand(
|
|
946
|
+
registerCommand(e) {
|
|
947
|
+
this.commandRegistry.registerCommand(e);
|
|
1038
948
|
}
|
|
1039
949
|
}
|
|
1040
950
|
export {
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
v as MissingSignatureArgument,
|
|
1059
|
-
A as MissingSignatureOption
|
|
951
|
+
A as BadCommandOption,
|
|
952
|
+
U as BadCommandParameter,
|
|
953
|
+
f as BobError,
|
|
954
|
+
J as Cli,
|
|
955
|
+
C as Command,
|
|
956
|
+
S as CommandIO,
|
|
957
|
+
V as CommandNotFoundError,
|
|
958
|
+
F as CommandParser,
|
|
959
|
+
j as CommandRegistry,
|
|
960
|
+
w as CommandSignatureParser,
|
|
961
|
+
z as CommandWithSignature,
|
|
962
|
+
T as ExceptionHandler,
|
|
963
|
+
H as HelpOption,
|
|
964
|
+
b as InvalidOption,
|
|
965
|
+
L as Logger,
|
|
966
|
+
E as MissingRequiredArgumentValue,
|
|
967
|
+
I as MissingRequiredOptionValue
|
|
1060
968
|
};
|