bob-core 1.4.0 → 2.0.0-beta.10
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/README.md +316 -175
- package/dist/cjs/index.js +18 -6
- package/dist/cjs/package-D82wayAe.cjs +1 -0
- package/dist/cjs/src/Cli.d.ts +21 -12
- package/dist/cjs/src/Command.d.ts +51 -35
- package/dist/cjs/src/CommandIO.d.ts +21 -1
- package/dist/cjs/src/CommandParser.d.ts +92 -44
- package/dist/cjs/src/CommandRegistry.d.ts +12 -7
- package/dist/cjs/src/CommandSignatureParser.d.ts +40 -0
- package/dist/cjs/src/CommandWithSignature.d.ts +28 -0
- package/dist/cjs/src/ExceptionHandler.d.ts +3 -0
- package/dist/cjs/src/Logger.d.ts +16 -0
- package/dist/cjs/src/commands/HelpCommand.d.ts +1 -3
- package/dist/cjs/src/contracts/CommandOption.d.ts +4 -5
- package/dist/cjs/src/contracts/LoggerContract.d.ts +13 -0
- package/dist/cjs/src/contracts/index.d.ts +1 -0
- package/dist/cjs/src/errors/BadCommandOption.d.ts +2 -1
- package/dist/cjs/src/errors/BadCommandParameter.d.ts +2 -1
- package/dist/cjs/src/errors/BobError.d.ts +2 -1
- package/dist/cjs/src/errors/CommandNotFoundError.d.ts +2 -1
- package/dist/cjs/src/errors/InvalidOption.d.ts +5 -4
- package/dist/cjs/src/errors/MissingRequiredArgumentValue.d.ts +4 -4
- package/dist/cjs/src/errors/MissingRequiredOptionValue.d.ts +7 -0
- package/dist/cjs/src/errors/index.d.ts +4 -0
- package/dist/cjs/src/index.d.ts +8 -1
- package/dist/cjs/src/lib/optionHelpers.d.ts +5 -0
- package/dist/cjs/src/lib/types.d.ts +33 -0
- package/dist/cjs/src/lib/valueConverter.d.ts +10 -0
- package/dist/cjs/src/options/HelpOption.d.ts +3 -1
- package/dist/cjs/src/testFixtures.d.ts +11 -0
- package/dist/esm/index.js +1108 -475
- package/dist/esm/package-O5nP1XlU.js +31 -0
- package/dist/esm/src/Cli.d.ts +21 -12
- package/dist/esm/src/Command.d.ts +51 -35
- package/dist/esm/src/CommandIO.d.ts +21 -1
- package/dist/esm/src/CommandParser.d.ts +92 -44
- package/dist/esm/src/CommandRegistry.d.ts +12 -7
- package/dist/esm/src/CommandSignatureParser.d.ts +40 -0
- package/dist/esm/src/CommandWithSignature.d.ts +28 -0
- package/dist/esm/src/ExceptionHandler.d.ts +3 -0
- package/dist/esm/src/Logger.d.ts +16 -0
- package/dist/esm/src/commands/HelpCommand.d.ts +1 -3
- package/dist/esm/src/contracts/CommandOption.d.ts +4 -5
- package/dist/esm/src/contracts/LoggerContract.d.ts +13 -0
- package/dist/esm/src/contracts/index.d.ts +1 -0
- package/dist/esm/src/errors/BadCommandOption.d.ts +2 -1
- package/dist/esm/src/errors/BadCommandParameter.d.ts +2 -1
- package/dist/esm/src/errors/BobError.d.ts +2 -1
- package/dist/esm/src/errors/CommandNotFoundError.d.ts +2 -1
- package/dist/esm/src/errors/InvalidOption.d.ts +5 -4
- package/dist/esm/src/errors/MissingRequiredArgumentValue.d.ts +4 -4
- package/dist/esm/src/errors/MissingRequiredOptionValue.d.ts +7 -0
- package/dist/esm/src/errors/index.d.ts +4 -0
- package/dist/esm/src/index.d.ts +8 -1
- package/dist/esm/src/lib/optionHelpers.d.ts +5 -0
- package/dist/esm/src/lib/types.d.ts +33 -0
- package/dist/esm/src/lib/valueConverter.d.ts +10 -0
- package/dist/esm/src/options/HelpOption.d.ts +3 -1
- package/dist/esm/src/testFixtures.d.ts +11 -0
- package/package.json +17 -4
- package/dist/cjs/package-6sByjm31.cjs +0 -1
- package/dist/cjs/src/errors/MissingSignatureArgument.d.ts +0 -8
- package/dist/cjs/src/errors/MissingSignatureOption.d.ts +0 -8
- package/dist/esm/package-eljsfLkU.js +0 -31
- 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,437 +1,1058 @@
|
|
|
1
|
-
import b from "
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import b from "prompts";
|
|
2
|
+
import X from "minimist";
|
|
3
|
+
import * as Z from "node:fs";
|
|
4
|
+
import I from "path";
|
|
5
|
+
import * as ee from "string-similarity";
|
|
6
|
+
class z {
|
|
7
|
+
logger;
|
|
8
|
+
constructor(e) {
|
|
9
|
+
this.logger = e;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Logger methods
|
|
13
|
+
*/
|
|
14
|
+
log(...e) {
|
|
15
|
+
this.logger.log(...e);
|
|
16
|
+
}
|
|
17
|
+
info(...e) {
|
|
18
|
+
this.logger.info(...e);
|
|
19
|
+
}
|
|
20
|
+
warn(...e) {
|
|
21
|
+
this.logger.warn(...e);
|
|
22
|
+
}
|
|
23
|
+
error(...e) {
|
|
24
|
+
this.logger.error(...e);
|
|
25
|
+
}
|
|
26
|
+
debug(...e) {
|
|
27
|
+
this.logger.debug(...e);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Prompt utils
|
|
31
|
+
*/
|
|
32
|
+
async askForConfirmation(e = "Do you want to continue?", t) {
|
|
33
|
+
return (await b({
|
|
34
|
+
type: "confirm",
|
|
35
|
+
name: "value",
|
|
36
|
+
message: e,
|
|
37
|
+
initial: t ?? !1
|
|
38
|
+
})).value;
|
|
39
|
+
}
|
|
40
|
+
async askForInput(e, t, n) {
|
|
41
|
+
return (await b({
|
|
42
|
+
type: "text",
|
|
43
|
+
name: "value",
|
|
44
|
+
message: e,
|
|
45
|
+
initial: t,
|
|
46
|
+
...n
|
|
47
|
+
}))?.value ?? null;
|
|
48
|
+
}
|
|
49
|
+
async askForDate(e, t, n) {
|
|
50
|
+
return (await b({
|
|
51
|
+
type: "date",
|
|
52
|
+
name: "value",
|
|
53
|
+
message: e,
|
|
54
|
+
initial: t,
|
|
55
|
+
...n
|
|
56
|
+
}))?.value ?? null;
|
|
57
|
+
}
|
|
58
|
+
async askForList(e, t, n) {
|
|
59
|
+
return (await b({
|
|
60
|
+
type: "list",
|
|
61
|
+
name: "value",
|
|
62
|
+
message: e,
|
|
63
|
+
initial: t,
|
|
64
|
+
...n
|
|
65
|
+
}))?.value ?? null;
|
|
66
|
+
}
|
|
67
|
+
async askForToggle(e, t, n) {
|
|
68
|
+
return (await b({
|
|
69
|
+
type: "toggle",
|
|
70
|
+
name: "value",
|
|
71
|
+
message: e,
|
|
72
|
+
initial: t,
|
|
73
|
+
...n
|
|
74
|
+
}))?.value ?? null;
|
|
75
|
+
}
|
|
76
|
+
async askForSelect(e, t, n) {
|
|
77
|
+
if (t.length === 0)
|
|
78
|
+
throw new Error("No options provided");
|
|
79
|
+
const s = [];
|
|
80
|
+
for (const o of t)
|
|
81
|
+
typeof o == "string" ? s.push({ title: o, value: o }) : s.push(o);
|
|
82
|
+
return (await b({
|
|
83
|
+
type: "select",
|
|
84
|
+
name: "value",
|
|
85
|
+
message: e,
|
|
86
|
+
choices: s,
|
|
87
|
+
...n
|
|
88
|
+
}))?.value ?? null;
|
|
89
|
+
}
|
|
90
|
+
newLoader(e = "", t = ["⠙", "⠘", "⠰", "⠴", "⠤", "⠦", "⠆", "⠃", "⠋", "⠉"], n = 100) {
|
|
91
|
+
let s = e, i = null, o = 0;
|
|
92
|
+
const u = setInterval(function() {
|
|
93
|
+
i && (process.stdout.write(new TextEncoder().encode("\r" + " ".repeat(i.length + 5) + "\r")), i = null), process.stdout.write(new TextEncoder().encode("\r" + t[o++] + " " + s)), o = o % t.length;
|
|
94
|
+
}, n), m = () => {
|
|
95
|
+
clearInterval(u), process.stdout.write(new TextEncoder().encode("\r" + " ".repeat(s.length + 5) + "\r"));
|
|
96
|
+
};
|
|
97
|
+
return {
|
|
98
|
+
[Symbol.dispose]: m,
|
|
99
|
+
[Symbol.asyncDispose]: m,
|
|
100
|
+
updateText: (h) => {
|
|
101
|
+
i = s, s = h;
|
|
102
|
+
},
|
|
103
|
+
stop: m
|
|
104
|
+
};
|
|
105
|
+
}
|
|
8
106
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
107
|
+
const E = 10, L = (r = 0) => (e) => `\x1B[${e + r}m`, M = (r = 0) => (e) => `\x1B[${38 + r};5;${e}m`, q = (r = 0) => (e, t, n) => `\x1B[${38 + r};2;${e};${t};${n}m`, c = {
|
|
108
|
+
modifier: {
|
|
109
|
+
reset: [0, 0],
|
|
110
|
+
// 21 isn't widely supported and 22 does the same thing
|
|
111
|
+
bold: [1, 22],
|
|
112
|
+
dim: [2, 22],
|
|
113
|
+
italic: [3, 23],
|
|
114
|
+
underline: [4, 24],
|
|
115
|
+
overline: [53, 55],
|
|
116
|
+
inverse: [7, 27],
|
|
117
|
+
hidden: [8, 28],
|
|
118
|
+
strikethrough: [9, 29]
|
|
119
|
+
},
|
|
120
|
+
color: {
|
|
121
|
+
black: [30, 39],
|
|
122
|
+
red: [31, 39],
|
|
123
|
+
green: [32, 39],
|
|
124
|
+
yellow: [33, 39],
|
|
125
|
+
blue: [34, 39],
|
|
126
|
+
magenta: [35, 39],
|
|
127
|
+
cyan: [36, 39],
|
|
128
|
+
white: [37, 39],
|
|
129
|
+
// Bright color
|
|
130
|
+
blackBright: [90, 39],
|
|
131
|
+
gray: [90, 39],
|
|
132
|
+
// Alias of `blackBright`
|
|
133
|
+
grey: [90, 39],
|
|
134
|
+
// Alias of `blackBright`
|
|
135
|
+
redBright: [91, 39],
|
|
136
|
+
greenBright: [92, 39],
|
|
137
|
+
yellowBright: [93, 39],
|
|
138
|
+
blueBright: [94, 39],
|
|
139
|
+
magentaBright: [95, 39],
|
|
140
|
+
cyanBright: [96, 39],
|
|
141
|
+
whiteBright: [97, 39]
|
|
142
|
+
},
|
|
143
|
+
bgColor: {
|
|
144
|
+
bgBlack: [40, 49],
|
|
145
|
+
bgRed: [41, 49],
|
|
146
|
+
bgGreen: [42, 49],
|
|
147
|
+
bgYellow: [43, 49],
|
|
148
|
+
bgBlue: [44, 49],
|
|
149
|
+
bgMagenta: [45, 49],
|
|
150
|
+
bgCyan: [46, 49],
|
|
151
|
+
bgWhite: [47, 49],
|
|
152
|
+
// Bright color
|
|
153
|
+
bgBlackBright: [100, 49],
|
|
154
|
+
bgGray: [100, 49],
|
|
155
|
+
// Alias of `bgBlackBright`
|
|
156
|
+
bgGrey: [100, 49],
|
|
157
|
+
// Alias of `bgBlackBright`
|
|
158
|
+
bgRedBright: [101, 49],
|
|
159
|
+
bgGreenBright: [102, 49],
|
|
160
|
+
bgYellowBright: [103, 49],
|
|
161
|
+
bgBlueBright: [104, 49],
|
|
162
|
+
bgMagentaBright: [105, 49],
|
|
163
|
+
bgCyanBright: [106, 49],
|
|
164
|
+
bgWhiteBright: [107, 49]
|
|
12
165
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
166
|
+
};
|
|
167
|
+
Object.keys(c.modifier);
|
|
168
|
+
const te = Object.keys(c.color), ne = Object.keys(c.bgColor);
|
|
169
|
+
[...te, ...ne];
|
|
170
|
+
function re() {
|
|
171
|
+
const r = /* @__PURE__ */ new Map();
|
|
172
|
+
for (const [e, t] of Object.entries(c)) {
|
|
173
|
+
for (const [n, s] of Object.entries(t))
|
|
174
|
+
c[n] = {
|
|
175
|
+
open: `\x1B[${s[0]}m`,
|
|
176
|
+
close: `\x1B[${s[1]}m`
|
|
177
|
+
}, t[n] = c[n], r.set(s[0], s[1]);
|
|
178
|
+
Object.defineProperty(c, e, {
|
|
179
|
+
value: t,
|
|
180
|
+
enumerable: !1
|
|
181
|
+
});
|
|
16
182
|
}
|
|
183
|
+
return Object.defineProperty(c, "codes", {
|
|
184
|
+
value: r,
|
|
185
|
+
enumerable: !1
|
|
186
|
+
}), c.color.close = "\x1B[39m", c.bgColor.close = "\x1B[49m", c.color.ansi = L(), c.color.ansi256 = M(), c.color.ansi16m = q(), c.bgColor.ansi = L(E), c.bgColor.ansi256 = M(E), c.bgColor.ansi16m = q(E), Object.defineProperties(c, {
|
|
187
|
+
rgbToAnsi256: {
|
|
188
|
+
value(e, t, n) {
|
|
189
|
+
return e === t && t === n ? e < 8 ? 16 : e > 248 ? 231 : Math.round((e - 8) / 247 * 24) + 232 : 16 + 36 * Math.round(e / 255 * 5) + 6 * Math.round(t / 255 * 5) + Math.round(n / 255 * 5);
|
|
190
|
+
},
|
|
191
|
+
enumerable: !1
|
|
192
|
+
},
|
|
193
|
+
hexToRgb: {
|
|
194
|
+
value(e) {
|
|
195
|
+
const t = /[a-f\d]{6}|[a-f\d]{3}/i.exec(e.toString(16));
|
|
196
|
+
if (!t)
|
|
197
|
+
return [0, 0, 0];
|
|
198
|
+
let [n] = t;
|
|
199
|
+
n.length === 3 && (n = [...n].map((i) => i + i).join(""));
|
|
200
|
+
const s = Number.parseInt(n, 16);
|
|
201
|
+
return [
|
|
202
|
+
/* eslint-disable no-bitwise */
|
|
203
|
+
s >> 16 & 255,
|
|
204
|
+
s >> 8 & 255,
|
|
205
|
+
s & 255
|
|
206
|
+
/* eslint-enable no-bitwise */
|
|
207
|
+
];
|
|
208
|
+
},
|
|
209
|
+
enumerable: !1
|
|
210
|
+
},
|
|
211
|
+
hexToAnsi256: {
|
|
212
|
+
value: (e) => c.rgbToAnsi256(...c.hexToRgb(e)),
|
|
213
|
+
enumerable: !1
|
|
214
|
+
},
|
|
215
|
+
ansi256ToAnsi: {
|
|
216
|
+
value(e) {
|
|
217
|
+
if (e < 8)
|
|
218
|
+
return 30 + e;
|
|
219
|
+
if (e < 16)
|
|
220
|
+
return 90 + (e - 8);
|
|
221
|
+
let t, n, s;
|
|
222
|
+
if (e >= 232)
|
|
223
|
+
t = ((e - 232) * 10 + 8) / 255, n = t, s = t;
|
|
224
|
+
else {
|
|
225
|
+
e -= 16;
|
|
226
|
+
const u = e % 36;
|
|
227
|
+
t = Math.floor(e / 36) / 5, n = Math.floor(u / 6) / 5, s = u % 6 / 5;
|
|
228
|
+
}
|
|
229
|
+
const i = Math.max(t, n, s) * 2;
|
|
230
|
+
if (i === 0)
|
|
231
|
+
return 30;
|
|
232
|
+
let o = 30 + (Math.round(s) << 2 | Math.round(n) << 1 | Math.round(t));
|
|
233
|
+
return i === 2 && (o += 60), o;
|
|
234
|
+
},
|
|
235
|
+
enumerable: !1
|
|
236
|
+
},
|
|
237
|
+
rgbToAnsi: {
|
|
238
|
+
value: (e, t, n) => c.ansi256ToAnsi(c.rgbToAnsi256(e, t, n)),
|
|
239
|
+
enumerable: !1
|
|
240
|
+
},
|
|
241
|
+
hexToAnsi: {
|
|
242
|
+
value: (e) => c.ansi256ToAnsi(c.hexToAnsi256(e)),
|
|
243
|
+
enumerable: !1
|
|
244
|
+
}
|
|
245
|
+
}), c;
|
|
17
246
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
247
|
+
const f = re(), D = (() => {
|
|
248
|
+
if (!("navigator" in globalThis))
|
|
249
|
+
return 0;
|
|
250
|
+
if (globalThis.navigator.userAgentData) {
|
|
251
|
+
const r = navigator.userAgentData.brands.find(({ brand: e }) => e === "Chromium");
|
|
252
|
+
if (r && r.version > 93)
|
|
253
|
+
return 3;
|
|
254
|
+
}
|
|
255
|
+
return /\b(Chrome|Chromium)\//.test(globalThis.navigator.userAgent) ? 1 : 0;
|
|
256
|
+
})(), V = D !== 0 && {
|
|
257
|
+
level: D
|
|
258
|
+
}, se = {
|
|
259
|
+
stdout: V,
|
|
260
|
+
stderr: V
|
|
261
|
+
};
|
|
262
|
+
function ie(r, e, t) {
|
|
263
|
+
let n = r.indexOf(e);
|
|
264
|
+
if (n === -1)
|
|
265
|
+
return r;
|
|
266
|
+
const s = e.length;
|
|
267
|
+
let i = 0, o = "";
|
|
268
|
+
do
|
|
269
|
+
o += r.slice(i, n) + e + t, i = n + s, n = r.indexOf(e, i);
|
|
270
|
+
while (n !== -1);
|
|
271
|
+
return o += r.slice(i), o;
|
|
272
|
+
}
|
|
273
|
+
function oe(r, e, t, n) {
|
|
274
|
+
let s = 0, i = "";
|
|
275
|
+
do {
|
|
276
|
+
const o = r[n - 1] === "\r";
|
|
277
|
+
i += r.slice(s, o ? n - 1 : n) + e + (o ? `\r
|
|
278
|
+
` : `
|
|
279
|
+
`) + t, s = n + 1, n = r.indexOf(`
|
|
280
|
+
`, s);
|
|
281
|
+
} while (n !== -1);
|
|
282
|
+
return i += r.slice(s), i;
|
|
283
|
+
}
|
|
284
|
+
const { stdout: H, stderr: W } = se, S = Symbol("GENERATOR"), $ = Symbol("STYLER"), O = Symbol("IS_EMPTY"), G = [
|
|
285
|
+
"ansi",
|
|
286
|
+
"ansi",
|
|
287
|
+
"ansi256",
|
|
288
|
+
"ansi16m"
|
|
289
|
+
], A = /* @__PURE__ */ Object.create(null), ae = (r, e = {}) => {
|
|
290
|
+
if (e.level && !(Number.isInteger(e.level) && e.level >= 0 && e.level <= 3))
|
|
291
|
+
throw new Error("The `level` option should be an integer from 0 to 3");
|
|
292
|
+
const t = H ? H.level : 0;
|
|
293
|
+
r.level = e.level === void 0 ? t : e.level;
|
|
294
|
+
}, le = (r) => {
|
|
295
|
+
const e = (...t) => t.join(" ");
|
|
296
|
+
return ae(e, r), Object.setPrototypeOf(e, R.prototype), e;
|
|
297
|
+
};
|
|
298
|
+
function R(r) {
|
|
299
|
+
return le(r);
|
|
300
|
+
}
|
|
301
|
+
Object.setPrototypeOf(R.prototype, Function.prototype);
|
|
302
|
+
for (const [r, e] of Object.entries(f))
|
|
303
|
+
A[r] = {
|
|
304
|
+
get() {
|
|
305
|
+
const t = x(this, N(e.open, e.close, this[$]), this[O]);
|
|
306
|
+
return Object.defineProperty(this, r, { value: t }), t;
|
|
31
307
|
}
|
|
32
|
-
|
|
308
|
+
};
|
|
309
|
+
A.visible = {
|
|
310
|
+
get() {
|
|
311
|
+
const r = x(this, this[$], !0);
|
|
312
|
+
return Object.defineProperty(this, "visible", { value: r }), r;
|
|
33
313
|
}
|
|
314
|
+
};
|
|
315
|
+
const T = (r, e, t, ...n) => r === "rgb" ? e === "ansi16m" ? f[t].ansi16m(...n) : e === "ansi256" ? f[t].ansi256(f.rgbToAnsi256(...n)) : f[t].ansi(f.rgbToAnsi(...n)) : r === "hex" ? T("rgb", e, t, ...f.hexToRgb(...n)) : f[t][r](...n), ue = ["rgb", "hex", "ansi256"];
|
|
316
|
+
for (const r of ue) {
|
|
317
|
+
A[r] = {
|
|
318
|
+
get() {
|
|
319
|
+
const { level: t } = this;
|
|
320
|
+
return function(...n) {
|
|
321
|
+
const s = N(T(r, G[t], "color", ...n), f.color.close, this[$]);
|
|
322
|
+
return x(this, s, this[O]);
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
const e = "bg" + r[0].toUpperCase() + r.slice(1);
|
|
327
|
+
A[e] = {
|
|
328
|
+
get() {
|
|
329
|
+
const { level: t } = this;
|
|
330
|
+
return function(...n) {
|
|
331
|
+
const s = N(T(r, G[t], "bgColor", ...n), f.bgColor.close, this[$]);
|
|
332
|
+
return x(this, s, this[O]);
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
};
|
|
34
336
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
t(o` {green ${e.name}} ${s} ${e.help ?? "\b"} ${n}`);
|
|
46
|
-
}
|
|
47
|
-
t("");
|
|
337
|
+
const me = Object.defineProperties(() => {
|
|
338
|
+
}, {
|
|
339
|
+
...A,
|
|
340
|
+
level: {
|
|
341
|
+
enumerable: !0,
|
|
342
|
+
get() {
|
|
343
|
+
return this[S].level;
|
|
344
|
+
},
|
|
345
|
+
set(r) {
|
|
346
|
+
this[S].level = r;
|
|
48
347
|
}
|
|
49
|
-
t(o`{white.bgRed ERROR } Argument {bold.yellow ${this.argument}} is missing in the signature.`);
|
|
50
348
|
}
|
|
349
|
+
}), N = (r, e, t) => {
|
|
350
|
+
let n, s;
|
|
351
|
+
return t === void 0 ? (n = r, s = e) : (n = t.openAll + r, s = e + t.closeAll), {
|
|
352
|
+
open: r,
|
|
353
|
+
close: e,
|
|
354
|
+
openAll: n,
|
|
355
|
+
closeAll: s,
|
|
356
|
+
parent: t
|
|
357
|
+
};
|
|
358
|
+
}, x = (r, e, t) => {
|
|
359
|
+
const n = (...s) => de(n, s.length === 1 ? "" + s[0] : s.join(" "));
|
|
360
|
+
return Object.setPrototypeOf(n, me), n[S] = r, n[$] = e, n[O] = t, n;
|
|
361
|
+
}, de = (r, e) => {
|
|
362
|
+
if (r.level <= 0 || !e)
|
|
363
|
+
return r[O] ? "" : e;
|
|
364
|
+
let t = r[$];
|
|
365
|
+
if (t === void 0)
|
|
366
|
+
return e;
|
|
367
|
+
const { openAll: n, closeAll: s } = t;
|
|
368
|
+
if (e.includes("\x1B"))
|
|
369
|
+
for (; t !== void 0; )
|
|
370
|
+
e = ie(e, t.close, t.open), t = t.parent;
|
|
371
|
+
const i = e.indexOf(`
|
|
372
|
+
`);
|
|
373
|
+
return i !== -1 && (e = oe(e, s, n, i)), n + e + s;
|
|
374
|
+
};
|
|
375
|
+
Object.defineProperties(R.prototype, A);
|
|
376
|
+
const a = R();
|
|
377
|
+
R({ level: W ? W.level : 0 });
|
|
378
|
+
class w extends Error {
|
|
379
|
+
}
|
|
380
|
+
function _(r) {
|
|
381
|
+
if (r === "string" || r === "number") return null;
|
|
382
|
+
if (r === "boolean") return !1;
|
|
383
|
+
if (Array.isArray(r) && r.length === 1) {
|
|
384
|
+
if (r[0] === "string") return [];
|
|
385
|
+
if (r[0] === "number") return [];
|
|
386
|
+
}
|
|
387
|
+
throw new Error("Invalid option type: " + r);
|
|
388
|
+
}
|
|
389
|
+
function U(r) {
|
|
390
|
+
return !Array.isArray(r) && typeof r == "object" && r.type ? r.default !== void 0 ? r.default : _(r.type) : _(r);
|
|
51
391
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
392
|
+
function y(r) {
|
|
393
|
+
return typeof r == "string" || Array.isArray(r) ? {
|
|
394
|
+
alias: [],
|
|
395
|
+
default: U(r),
|
|
396
|
+
description: "",
|
|
397
|
+
required: !1,
|
|
398
|
+
secret: !1,
|
|
399
|
+
type: r,
|
|
400
|
+
variadic: !1
|
|
401
|
+
} : {
|
|
402
|
+
alias: r.alias ? Array.isArray(r.alias) ? r.alias : [r.alias] : [],
|
|
403
|
+
default: r.default ?? U(r.type),
|
|
404
|
+
description: r.description ?? "",
|
|
405
|
+
required: r.required ?? !1,
|
|
406
|
+
secret: r.secret ?? !1,
|
|
407
|
+
type: r.type,
|
|
408
|
+
variadic: r.variadic ?? !1
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
class k extends w {
|
|
412
|
+
constructor(e, t = {}) {
|
|
413
|
+
super(`Invalid option ${e} in not recognized`), this.option = e, this.optionsSchema = t;
|
|
414
|
+
}
|
|
415
|
+
pretty(e) {
|
|
416
|
+
const t = Object.entries(this.optionsSchema);
|
|
417
|
+
if (t.length > 0) {
|
|
418
|
+
e.log(`
|
|
419
|
+
${a.yellow("Available options")}:`);
|
|
420
|
+
for (const [n, s] of t) {
|
|
421
|
+
const i = y(s), o = typeof i.alias == "string" ? [i.alias] : i.alias, u = Array.isArray(i.type) ? `[${i.type[0]}]` : i.type, m = `--${n}${o.length > 0 ? o.map((l) => `, -${l}`).join("") : ""}`, h = " ".repeat(30 - m.length);
|
|
422
|
+
e.log(` ${a.green(m)} ${h} ${i.description || "\b"} ${a.white(`(${u})`)}`);
|
|
63
423
|
}
|
|
64
|
-
|
|
424
|
+
e.log("");
|
|
65
425
|
}
|
|
66
|
-
|
|
426
|
+
e.log(`${a.white.bgRed(" ERROR ")} Option ${a.bold.yellow(this.option)} is not recognized.`);
|
|
67
427
|
}
|
|
68
428
|
}
|
|
69
|
-
class
|
|
70
|
-
constructor(
|
|
71
|
-
|
|
72
|
-
const [i, ...m] = e.split(/\{(.*?)\}/g).map((p) => p.trim()).filter(Boolean), { _: a, ...l } = b(r);
|
|
73
|
-
this.command = i, this.parseSignature(m), this.parseDefaultOptions(), this.handleArguments(a), this.handleOptions(l);
|
|
429
|
+
class Y extends w {
|
|
430
|
+
constructor(e) {
|
|
431
|
+
super(`Argument "${e}" is required.`), this.argument = e;
|
|
74
432
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
return
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
433
|
+
pretty(e) {
|
|
434
|
+
e.log(`${a.white.bgRed(" ERROR ")} Argument ${a.bold.yellow(this.argument)} is required.`);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
class ce extends w {
|
|
438
|
+
constructor(e) {
|
|
439
|
+
super(`Argument "${e}" is required.`), this.option = e;
|
|
440
|
+
}
|
|
441
|
+
pretty(e) {
|
|
442
|
+
e.log(`${a.white.bgRed(" ERROR ")} Option ${a.bold.yellow(this.option)} is required.`);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
class $e extends w {
|
|
446
|
+
constructor(e) {
|
|
447
|
+
let t = `Argument "${e.param}" value is invalid.`;
|
|
448
|
+
e.reason ? t += ` Reason: ${e.reason}` : t += ` Value: "${e.value}"`, super(t), this.param = e;
|
|
449
|
+
}
|
|
450
|
+
pretty(e) {
|
|
451
|
+
e.log(` ${a.white.bgRed(" ERROR ")} Argument ${a.bold.yellow(this.param.param)} value is invalid. `), (this.param.value || this.param.reason) && e.log(""), this.param.value && e.log(` ${a.blue("Value")}: ${this.param.value}`), this.param.reason && e.log(` ${a.yellow("Reason")}: ${this.param.reason}`);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
class B extends w {
|
|
455
|
+
constructor(e) {
|
|
456
|
+
let t = `Option "${e.option}" value is invalid.`;
|
|
457
|
+
e.reason ? t += ` Reason: ${e.reason}` : t += ` Value: "${e.value}"`, super(t), this.param = e;
|
|
458
|
+
}
|
|
459
|
+
pretty(e) {
|
|
460
|
+
e.log(` ${a.white.bgRed(" ERROR ")} Option ${a.bold.yellow(this.param.option)} value is invalid. `), (this.param.value || this.param.reason) && e.log(""), this.param.value && e.log(` ${a.blue("Value")}: ${this.param.value}`), this.param.reason && e.log(` ${a.yellow("Reason")}: ${this.param.reason}`);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
class he extends w {
|
|
464
|
+
constructor(e) {
|
|
465
|
+
super(`Command "${e}" not found.`), this.command = e;
|
|
466
|
+
}
|
|
467
|
+
pretty(e) {
|
|
468
|
+
e.log(`${a.bgRed(" ERROR ")} Command ${a.yellow(this.command)} not found.`);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
function C(r, e, t, n) {
|
|
472
|
+
if (r == null)
|
|
473
|
+
return n ?? null;
|
|
474
|
+
if (e === "string")
|
|
475
|
+
return String(r);
|
|
476
|
+
if (e === "number") {
|
|
477
|
+
const s = Number(r);
|
|
478
|
+
if (isNaN(s))
|
|
479
|
+
throw new B({
|
|
480
|
+
option: t,
|
|
481
|
+
reason: `Expected a number, got "${r}"`
|
|
482
|
+
});
|
|
483
|
+
return s;
|
|
484
|
+
}
|
|
485
|
+
if (e === "boolean")
|
|
486
|
+
return typeof r == "boolean" ? r : r === "true" || r === "1" ? !0 : r === "false" || r === "0" ? !1 : !!r;
|
|
487
|
+
if (Array.isArray(e)) {
|
|
488
|
+
const s = e[0], i = Array.isArray(r) ? r : [r];
|
|
489
|
+
if (s === "string")
|
|
490
|
+
return i.map((o) => String(o));
|
|
491
|
+
if (s === "number")
|
|
492
|
+
return i.map((o) => {
|
|
493
|
+
const u = Number(o);
|
|
494
|
+
if (isNaN(u))
|
|
495
|
+
throw new B({
|
|
496
|
+
option: t,
|
|
497
|
+
reason: `Expected array of numbers, got "${o}" in array`
|
|
498
|
+
});
|
|
499
|
+
return u;
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
return r;
|
|
503
|
+
}
|
|
504
|
+
class J {
|
|
505
|
+
options;
|
|
506
|
+
parsedOptions = null;
|
|
507
|
+
arguments;
|
|
508
|
+
parsedArguments = null;
|
|
509
|
+
io;
|
|
510
|
+
shouldPromptForMissingOptions = !0;
|
|
511
|
+
constructor(e) {
|
|
512
|
+
this.options = e.options, this.arguments = e.arguments, this.io = e.io;
|
|
513
|
+
}
|
|
514
|
+
// === PUBLIC METHODS ===
|
|
515
|
+
/**
|
|
516
|
+
* Parses raw command-line arguments into structured options and arguments
|
|
517
|
+
* @param args - Raw command line arguments (typically from process.argv.slice(2))
|
|
518
|
+
* @returns Object containing parsed options and arguments
|
|
519
|
+
* @throws {InvalidOption} If an unknown option is provided
|
|
520
|
+
* @throws {BadCommandOption} If a value cannot be converted to the expected type
|
|
521
|
+
*/
|
|
522
|
+
init(e) {
|
|
523
|
+
const { _: t, ...n } = X(e);
|
|
524
|
+
return this.validateUnknownOptions(n), this.parsedOptions = this.handleOptions(n), this.parsedArguments = this.handleArguments(t), {
|
|
525
|
+
options: this.parsedOptions,
|
|
526
|
+
arguments: this.parsedArguments
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Validates the parsed options and arguments
|
|
531
|
+
* @throws {Error} If validation fails
|
|
532
|
+
*/
|
|
533
|
+
async validate() {
|
|
534
|
+
for (const e in this.options)
|
|
535
|
+
if (y(this.options[e]).required && (this.parsedOptions?.[e] === void 0 || this.parsedOptions?.[e] === null))
|
|
536
|
+
throw new ce(e);
|
|
537
|
+
for (const e in this.arguments) {
|
|
538
|
+
const t = y(this.arguments[e]), n = this.parsedArguments?.[e];
|
|
539
|
+
if (t.required && n == null) {
|
|
540
|
+
if (this.shouldPromptForMissingOptions) {
|
|
541
|
+
const s = await this.promptForArgument(e, t);
|
|
542
|
+
if (s && this.parsedArguments) {
|
|
543
|
+
this.parsedArguments[e] = C(s, t.type, e);
|
|
544
|
+
continue;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
throw new Y(e);
|
|
548
|
+
}
|
|
549
|
+
if (t.required && t.variadic && Array.isArray(n) && n.length === 0) {
|
|
550
|
+
if (this.shouldPromptForMissingOptions) {
|
|
551
|
+
const s = await this.promptForArgument(e, t);
|
|
552
|
+
if (s && this.parsedArguments) {
|
|
553
|
+
this.parsedArguments[e] = C(s, t.type, e);
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
throw new Y(e);
|
|
128
558
|
}
|
|
129
559
|
}
|
|
130
560
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
561
|
+
/**
|
|
562
|
+
* Retrieves a parsed option value by name
|
|
563
|
+
* @param name - The option name
|
|
564
|
+
* @returns The typed option value
|
|
565
|
+
* @throws {Error} If init() has not been called yet
|
|
566
|
+
*/
|
|
567
|
+
option(e) {
|
|
568
|
+
if (!this.parsedOptions)
|
|
569
|
+
throw new Error("Options have not been parsed yet. Call init() first.");
|
|
570
|
+
return this.parsedOptions[e];
|
|
571
|
+
}
|
|
572
|
+
setOption(e, t) {
|
|
573
|
+
if (!this.parsedOptions)
|
|
574
|
+
throw new Error("Options have not been parsed yet. Call init() first.");
|
|
575
|
+
if (!(e in this.options))
|
|
576
|
+
throw new k(e, this.options);
|
|
577
|
+
this.parsedOptions[e] = t;
|
|
140
578
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
579
|
+
/**
|
|
580
|
+
* Retrieves a parsed argument value by name
|
|
581
|
+
* @param name - The argument name
|
|
582
|
+
* @returns The typed argument value
|
|
583
|
+
* @throws {Error} If init() has not been called yet
|
|
584
|
+
*/
|
|
585
|
+
argument(e) {
|
|
586
|
+
if (!this.parsedArguments)
|
|
587
|
+
throw new Error("Arguments have not been parsed yet. Call init() first.");
|
|
588
|
+
return this.parsedArguments[e];
|
|
589
|
+
}
|
|
590
|
+
setArgument(e, t) {
|
|
591
|
+
if (!this.parsedArguments)
|
|
592
|
+
throw new Error("Arguments have not been parsed yet. Call init() first.");
|
|
593
|
+
if (!(e in this.arguments))
|
|
594
|
+
throw new k(e, this.arguments);
|
|
595
|
+
this.parsedArguments[e] = t;
|
|
596
|
+
}
|
|
597
|
+
// === PRIVATE HELPERS ===
|
|
598
|
+
/**
|
|
599
|
+
* Validates that all provided options are recognized
|
|
600
|
+
* @throws {InvalidOption} If an unknown option is found
|
|
601
|
+
*/
|
|
602
|
+
validateUnknownOptions(e) {
|
|
603
|
+
const t = /* @__PURE__ */ new Set();
|
|
604
|
+
for (const n in this.options) {
|
|
605
|
+
t.add(n);
|
|
606
|
+
const s = y(this.options[n]);
|
|
607
|
+
for (const i of s.alias)
|
|
608
|
+
t.add(i);
|
|
150
609
|
}
|
|
610
|
+
for (const n in e)
|
|
611
|
+
if (!t.has(n))
|
|
612
|
+
throw new k(n, this.options);
|
|
151
613
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
variadic: !1,
|
|
161
|
-
help: t.description,
|
|
162
|
-
defaultValue: t.defaultValue ?? null,
|
|
163
|
-
isOption: !0
|
|
164
|
-
}, this.options[t.option] = t.defaultValue, t.alias)
|
|
165
|
-
for (const e of t.alias)
|
|
166
|
-
this.optionAliases[e] = t.option;
|
|
614
|
+
/**
|
|
615
|
+
* Processes named options from minimist output
|
|
616
|
+
*/
|
|
617
|
+
handleOptions(e) {
|
|
618
|
+
const t = {};
|
|
619
|
+
for (const n in this.options) {
|
|
620
|
+
const s = y(this.options[n]);
|
|
621
|
+
t[n] = this.resolveOptionValue(n, s, e);
|
|
167
622
|
}
|
|
623
|
+
return t;
|
|
168
624
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
variadic
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
e.name = n.trim(), e.help = s.trim();
|
|
625
|
+
/**
|
|
626
|
+
* Processes positional arguments from minimist output
|
|
627
|
+
*/
|
|
628
|
+
handleArguments(e) {
|
|
629
|
+
const t = {}, n = [...e];
|
|
630
|
+
for (const s in this.arguments) {
|
|
631
|
+
const i = y(this.arguments[s]);
|
|
632
|
+
if (i.variadic) {
|
|
633
|
+
t[s] = this.handleVariadicArgument(s, i, n);
|
|
634
|
+
continue;
|
|
635
|
+
}
|
|
636
|
+
t[s] = this.resolveArgumentValue(s, i, n.shift());
|
|
182
637
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
638
|
+
return t;
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* Handles variadic arguments that consume all remaining positional values
|
|
642
|
+
*/
|
|
643
|
+
handleVariadicArgument(e, t, n) {
|
|
644
|
+
return n.length ? C(n, t.type, e, t.default) : t.default;
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* Resolves a single positional argument value with defaults and type conversion
|
|
648
|
+
* Note: Does not validate required arguments - validation happens in subclass validate() methods
|
|
649
|
+
*/
|
|
650
|
+
resolveArgumentValue(e, t, n) {
|
|
651
|
+
return n === void 0 ? t.default : C(n, t.type, e, t.default);
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Resolves an option value from the parsed option values object
|
|
655
|
+
* Handles alias resolution, defaults, and type conversion
|
|
656
|
+
*/
|
|
657
|
+
resolveOptionValue(e, t, n) {
|
|
658
|
+
let s;
|
|
659
|
+
const i = [e, ...t.alias];
|
|
660
|
+
for (const o of i)
|
|
661
|
+
if (o in n) {
|
|
662
|
+
s = n[o];
|
|
663
|
+
break;
|
|
664
|
+
}
|
|
665
|
+
if (s === void 0) {
|
|
666
|
+
if (t.required)
|
|
667
|
+
throw new B({
|
|
668
|
+
option: e,
|
|
669
|
+
reason: "Required option is missing"
|
|
670
|
+
});
|
|
671
|
+
return t.default;
|
|
191
672
|
}
|
|
192
|
-
return
|
|
673
|
+
return C(s, t.type, e, t.default);
|
|
193
674
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
675
|
+
optionDefinitions() {
|
|
676
|
+
const e = {};
|
|
677
|
+
for (const t in this.options)
|
|
678
|
+
e[t] = y(this.options[t]);
|
|
679
|
+
return e;
|
|
680
|
+
}
|
|
681
|
+
argumentDefinitions() {
|
|
682
|
+
const e = {};
|
|
683
|
+
for (const t in this.arguments)
|
|
684
|
+
e[t] = y(this.arguments[t]);
|
|
685
|
+
return e;
|
|
686
|
+
}
|
|
687
|
+
availableOptions() {
|
|
688
|
+
return Object.keys(this.options);
|
|
689
|
+
}
|
|
690
|
+
availableArguments() {
|
|
691
|
+
return Object.keys(this.arguments);
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* Disables prompting for missing argument values
|
|
695
|
+
* Useful for non-interactive environments
|
|
696
|
+
*/
|
|
697
|
+
disablePrompting() {
|
|
698
|
+
return this.shouldPromptForMissingOptions = !1, this;
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* Prompts the user to provide a missing argument value via CommandIO
|
|
702
|
+
* Used by validate() when shouldPromptForMissingArgs is enabled
|
|
703
|
+
* @param argumentName - The name of the missing argument
|
|
704
|
+
* @param argDef - The argument's definition for type and description
|
|
705
|
+
* @returns The user-provided value, or null if none given
|
|
706
|
+
*/
|
|
707
|
+
async promptForArgument(e, t) {
|
|
708
|
+
if (!Array.isArray(t.type) && !["string", "number", "secret"].includes(t.type))
|
|
709
|
+
return null;
|
|
710
|
+
let n = `${a.yellow.bold(e)} is required`;
|
|
711
|
+
return t.description && (n += `: ${a.gray(`(${t.description})`)}`), n += ` ${a.green(`(${t.type}${t.variadic == !0 ? "[]" : ""})`)}
|
|
712
|
+
`, Array.isArray(t.type) ? (n += `Please provide one or more values, separated by commas:
|
|
713
|
+
`, await this.io.askForList(n, void 0, {
|
|
714
|
+
separator: ",",
|
|
715
|
+
validate: (s) => {
|
|
716
|
+
if (!s.length)
|
|
717
|
+
return "Please enter at least one value";
|
|
718
|
+
if (t.type[0] === "number") {
|
|
719
|
+
for (const i of s.split(","))
|
|
720
|
+
if (isNaN(Number(i)))
|
|
721
|
+
return "Please enter only valid numbers";
|
|
207
722
|
}
|
|
208
|
-
|
|
209
|
-
this.setArgument(t, s);
|
|
210
|
-
else
|
|
211
|
-
throw new v(n);
|
|
723
|
+
return !0;
|
|
212
724
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
725
|
+
})) : await this.io.askForInput(n, void 0, {
|
|
726
|
+
type: t.type === "number" ? "number" : t.secret ? "password" : "text",
|
|
727
|
+
validate: (s) => {
|
|
728
|
+
if (s == null || typeof s == "string" && !s.length)
|
|
729
|
+
return "This value is required";
|
|
730
|
+
if (t.type === "number") {
|
|
731
|
+
const i = Number(s);
|
|
732
|
+
if (isNaN(i))
|
|
733
|
+
return "Please enter a valid number";
|
|
734
|
+
} else if (t.type === "string" && (typeof s != "string" || !s.length))
|
|
735
|
+
return "Please enter a valid text";
|
|
736
|
+
return !0;
|
|
737
|
+
}
|
|
738
|
+
});
|
|
216
739
|
}
|
|
217
740
|
}
|
|
218
|
-
function
|
|
219
|
-
return new Array(
|
|
741
|
+
function j(r) {
|
|
742
|
+
return new Array(r + 5).join(" ");
|
|
220
743
|
}
|
|
221
|
-
class
|
|
744
|
+
class pe {
|
|
745
|
+
type = "boolean";
|
|
222
746
|
option = "help";
|
|
223
747
|
alias = ["h"];
|
|
224
|
-
|
|
225
|
-
description =
|
|
748
|
+
default = !1;
|
|
749
|
+
description = `Display help for the given command. When no command is given display help for the ${a.green("list")} command`;
|
|
226
750
|
async handler() {
|
|
227
|
-
const
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
751
|
+
const e = this.parser.argumentDefinitions(), t = this.parser.optionDefinitions(), n = Object.entries(e), s = Object.entries(t), i = s.map(([l, d]) => {
|
|
752
|
+
const p = Array.isArray(d.alias) ? d.alias : d.alias ? [d.alias] : [];
|
|
753
|
+
return {
|
|
754
|
+
name: l,
|
|
755
|
+
...d,
|
|
756
|
+
optionWithAlias: `--${l}${p.map((g) => `, -${g}`).join("")}`
|
|
757
|
+
};
|
|
758
|
+
}), o = n.filter(([, l]) => l.required);
|
|
759
|
+
this.io.log(a.yellow("Description:")), this.io.log(` ${this.description}
|
|
760
|
+
`), this.io.log(a.yellow("Usage:")), this.io.log(` ${this.command} ${o.length > 0 ? o.map(([l]) => `<${l}>`).join(" ") : "\b"} [options]`);
|
|
761
|
+
const u = Math.max(...i.map((l) => l.optionWithAlias.length), 0), m = Math.max(...n.map(([l]) => l.length), 0), h = m > u ? m : u;
|
|
762
|
+
if (n.length > 0) {
|
|
763
|
+
this.io.log(`
|
|
764
|
+
${a.yellow("Arguments")}:`);
|
|
765
|
+
for (const [l, d] of n) {
|
|
766
|
+
const p = j(h - l.length);
|
|
767
|
+
let g = ` ${a.green(l)} ${p} ${d.description ?? "\b"}`;
|
|
768
|
+
if (d.default !== void 0 && !d.required) {
|
|
769
|
+
const Q = (Array.isArray(d.type) ? `[${d.type[0]}]` : d.type) === "array" || Array.isArray(d.type) ? JSON.stringify(d.default) : d.default;
|
|
770
|
+
g += ` ${a.yellow(`[default: ${Q}]`)}`;
|
|
241
771
|
}
|
|
242
|
-
|
|
772
|
+
d.variadic && (g += ` ${a.white("(variadic)")}`), this.io.log(g);
|
|
243
773
|
}
|
|
244
774
|
}
|
|
245
|
-
if (
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
775
|
+
if (s.length > 0) {
|
|
776
|
+
this.io.log(`
|
|
777
|
+
${a.yellow("Options")}:`);
|
|
778
|
+
for (const l of i) {
|
|
779
|
+
const d = j(h - l.optionWithAlias.length);
|
|
780
|
+
let p = `${a.green(l.optionWithAlias)} ${d} ${l.description ?? "\b"}`;
|
|
781
|
+
if (l.type) {
|
|
782
|
+
const g = Array.isArray(l.type) ? `[${l.type[0]}]` : l.type;
|
|
783
|
+
p += ` ${a.white(`(${g})`)}`;
|
|
784
|
+
}
|
|
785
|
+
if (l.default !== void 0 && !l.required) {
|
|
786
|
+
const P = (Array.isArray(l.type) ? `[${l.type[0]}]` : l.type) === "array" || Array.isArray(l.type) ? JSON.stringify(l.default) : l.default;
|
|
787
|
+
p += ` ${a.yellow(`[default: ${P}]`)}`;
|
|
253
788
|
}
|
|
254
|
-
|
|
789
|
+
this.io.log(p);
|
|
255
790
|
}
|
|
256
791
|
}
|
|
257
792
|
if (this.commandsExamples.length > 0) {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
793
|
+
this.io.log(`
|
|
794
|
+
${a.yellow("Examples")}:`);
|
|
795
|
+
let l = process.argv[0].split("/").pop();
|
|
796
|
+
l === "node" && (l += " " + process.argv[1].split("/").pop());
|
|
797
|
+
for (const [d, p] of this.commandsExamples.entries())
|
|
798
|
+
d > 0 && this.io.log(""), this.io.log(` ${p.description}
|
|
799
|
+
`), this.io.log(` ${a.green(`${l} ${p.command}`)}`);
|
|
264
800
|
}
|
|
265
801
|
return -1;
|
|
266
802
|
}
|
|
267
803
|
}
|
|
268
|
-
class
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
name: "value",
|
|
276
|
-
message: t,
|
|
277
|
-
initial: e ?? !1
|
|
278
|
-
})).value;
|
|
804
|
+
class F {
|
|
805
|
+
_command;
|
|
806
|
+
description = "";
|
|
807
|
+
group;
|
|
808
|
+
commandsExamples = [];
|
|
809
|
+
get command() {
|
|
810
|
+
return this._command;
|
|
279
811
|
}
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
812
|
+
ctx;
|
|
813
|
+
io;
|
|
814
|
+
logger;
|
|
815
|
+
parser;
|
|
816
|
+
disablePromptingFlag = !1;
|
|
817
|
+
_preHandler;
|
|
818
|
+
_handler;
|
|
819
|
+
tmp;
|
|
820
|
+
defaultOptions() {
|
|
821
|
+
return [new pe()];
|
|
288
822
|
}
|
|
289
|
-
|
|
290
|
-
return
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
...n
|
|
296
|
-
}))?.value ?? null;
|
|
823
|
+
newCommandParser(e) {
|
|
824
|
+
return new J({
|
|
825
|
+
io: e.io,
|
|
826
|
+
options: e.options,
|
|
827
|
+
arguments: e.arguments
|
|
828
|
+
});
|
|
297
829
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
throw new Error("No options provided");
|
|
301
|
-
const s = [];
|
|
302
|
-
for (const i of e)
|
|
303
|
-
typeof i == "string" ? s.push({ title: i, value: i }) : s.push(i);
|
|
304
|
-
return (await f({
|
|
305
|
-
type: "select",
|
|
306
|
-
name: "value",
|
|
307
|
-
message: t,
|
|
308
|
-
choices: s,
|
|
309
|
-
...n
|
|
310
|
-
}))?.value ?? null;
|
|
830
|
+
newCommandIO(e) {
|
|
831
|
+
return new z(e.logger);
|
|
311
832
|
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
}, n), a = () => {
|
|
317
|
-
clearInterval(m), process.stdout.write(new TextEncoder().encode("\r" + " ".repeat(s.length + 5) + "\r"));
|
|
833
|
+
constructor(e, t) {
|
|
834
|
+
this._command = e, this.description = t?.description ?? "", this.group = t?.group, this.tmp = {
|
|
835
|
+
options: t?.options ?? {},
|
|
836
|
+
arguments: t?.arguments ?? {}
|
|
318
837
|
};
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
838
|
+
const n = this.defaultOptions();
|
|
839
|
+
if (n.length > 0)
|
|
840
|
+
for (const s of n)
|
|
841
|
+
this.tmp.options[s.option] = s;
|
|
842
|
+
}
|
|
843
|
+
disablePrompting() {
|
|
844
|
+
return this.disablePromptingFlag = !0, this;
|
|
845
|
+
}
|
|
846
|
+
preHandler(e) {
|
|
847
|
+
return this._preHandler = e, this;
|
|
848
|
+
}
|
|
849
|
+
handler(e) {
|
|
850
|
+
return this._handler = e, this;
|
|
851
|
+
}
|
|
852
|
+
options(e) {
|
|
853
|
+
return this.tmp = {
|
|
854
|
+
options: {
|
|
855
|
+
...this.tmp?.options ?? {},
|
|
856
|
+
...e
|
|
324
857
|
},
|
|
325
|
-
|
|
326
|
-
};
|
|
858
|
+
arguments: this.tmp?.arguments ?? {}
|
|
859
|
+
}, this;
|
|
860
|
+
}
|
|
861
|
+
arguments(e) {
|
|
862
|
+
return this.tmp = {
|
|
863
|
+
options: this.tmp?.options ?? {},
|
|
864
|
+
arguments: {
|
|
865
|
+
...this.tmp?.arguments ?? {},
|
|
866
|
+
...e
|
|
867
|
+
}
|
|
868
|
+
}, this;
|
|
869
|
+
}
|
|
870
|
+
async run(e) {
|
|
871
|
+
if (!this._handler && !this.handle)
|
|
872
|
+
throw new Error(`No handler defined for command ${this.command || "(unknown)"}`);
|
|
873
|
+
let t;
|
|
874
|
+
if (this.ctx = e.ctx, this.logger = e.logger, this.io = this.newCommandIO({
|
|
875
|
+
logger: e.logger
|
|
876
|
+
}), e && "args" in e) {
|
|
877
|
+
const i = this.tmp?.options ?? {};
|
|
878
|
+
for (const o of this.defaultOptions())
|
|
879
|
+
o.option in i || (i[o.option] = o);
|
|
880
|
+
this.parser = this.newCommandParser({
|
|
881
|
+
io: this.io,
|
|
882
|
+
options: i,
|
|
883
|
+
arguments: this.tmp?.arguments ?? {}
|
|
884
|
+
}), t = this.parser.init(e.args);
|
|
885
|
+
for (const o of this.defaultOptions())
|
|
886
|
+
if (t.options[o.option] === !0) {
|
|
887
|
+
const u = await o.handler.call(this);
|
|
888
|
+
if (u && u !== 0)
|
|
889
|
+
return u;
|
|
890
|
+
}
|
|
891
|
+
this.disablePromptingFlag && this.parser.disablePrompting(), await this.parser.validate();
|
|
892
|
+
} else
|
|
893
|
+
t = {
|
|
894
|
+
options: e.options,
|
|
895
|
+
arguments: e.arguments
|
|
896
|
+
};
|
|
897
|
+
const n = this.preHandle ? await this.preHandle() : null;
|
|
898
|
+
if (n && n !== 0)
|
|
899
|
+
return n;
|
|
900
|
+
if (!this._handler && this.handle)
|
|
901
|
+
this._handler = this.handle.bind(this);
|
|
902
|
+
else if (!this._handler)
|
|
903
|
+
throw new Error(`No handler defined for command ${this.command || "(unknown)"}`);
|
|
904
|
+
return await this._handler(e.ctx, t) ?? 0;
|
|
327
905
|
}
|
|
328
906
|
}
|
|
329
|
-
class
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
907
|
+
class v extends J {
|
|
908
|
+
command;
|
|
909
|
+
constructor(e) {
|
|
910
|
+
const t = v.parseSignature(e.signature, e.helperDefinitions, e.defaultOptions);
|
|
911
|
+
super({
|
|
912
|
+
io: e.io,
|
|
913
|
+
options: t.options,
|
|
914
|
+
arguments: t.arguments
|
|
915
|
+
}), this.command = t.command;
|
|
337
916
|
}
|
|
338
|
-
|
|
339
|
-
|
|
917
|
+
/**
|
|
918
|
+
* Parses command signature string into command name and parameter schemas
|
|
919
|
+
* Example: "migrate {name} {--force}" -> { command: "migrate", arguments: {name: ...}, options: {force: ...} }
|
|
920
|
+
*/
|
|
921
|
+
static parseSignature(e, t, n) {
|
|
922
|
+
const [s, ...i] = e.split(/\{(.*?)\}/g).map((m) => m.trim()).filter(Boolean), o = {}, u = {};
|
|
923
|
+
for (const m of i) {
|
|
924
|
+
const { name: h, isOption: l, definition: d } = v.parseParamSignature(m, t);
|
|
925
|
+
l ? o[h] = d : u[h] = d;
|
|
926
|
+
}
|
|
927
|
+
for (const m of n)
|
|
928
|
+
o[m.option] = {
|
|
929
|
+
type: m.type,
|
|
930
|
+
required: m.required,
|
|
931
|
+
alias: m.alias,
|
|
932
|
+
variadic: m.variadic ?? !1,
|
|
933
|
+
description: m.description,
|
|
934
|
+
default: m.default ?? null
|
|
935
|
+
};
|
|
936
|
+
return {
|
|
937
|
+
command: s,
|
|
938
|
+
options: o,
|
|
939
|
+
arguments: u
|
|
940
|
+
};
|
|
340
941
|
}
|
|
341
|
-
|
|
342
|
-
|
|
942
|
+
/**
|
|
943
|
+
* Parses a single parameter signature like "{name}" or "{--force}" or "{files*}"
|
|
944
|
+
* Extracts name, type, default value, aliases, description, etc.
|
|
945
|
+
*
|
|
946
|
+
* Signature syntax:
|
|
947
|
+
* - {arg} -> required string argument
|
|
948
|
+
* - {arg?} -> optional argument
|
|
949
|
+
* - {arg=default} -> argument with default value
|
|
950
|
+
* - {arg*} -> variadic argument (array)
|
|
951
|
+
* - {arg:desc} -> argument with description
|
|
952
|
+
* - {--opt} -> boolean option
|
|
953
|
+
* - {--opt=} -> string option
|
|
954
|
+
* - {--opt|o} -> option with alias
|
|
955
|
+
*/
|
|
956
|
+
static parseParamSignature(e, t) {
|
|
957
|
+
let n = e, s = !1;
|
|
958
|
+
const i = {
|
|
959
|
+
required: !0,
|
|
960
|
+
type: "string",
|
|
961
|
+
description: void 0,
|
|
962
|
+
default: null,
|
|
963
|
+
variadic: !1
|
|
964
|
+
};
|
|
965
|
+
if (n.includes(":")) {
|
|
966
|
+
const [o, u] = n.split(":");
|
|
967
|
+
n = o.trim(), i.description = u.trim();
|
|
968
|
+
}
|
|
969
|
+
if (n.includes("=")) {
|
|
970
|
+
const [o, u] = n.split("=");
|
|
971
|
+
n = o.trim(), i.default = u.trim(), i.required = !1, typeof i.default == "string" && !i.default.length ? i.default = null : i.default === "true" ? (i.default = !0, i.type = "boolean") : i.default === "false" && (i.default = !1, i.type = "boolean");
|
|
972
|
+
} else n.startsWith("--") && (i.required = !1, i.default = !1, i.type = "boolean");
|
|
973
|
+
if (n.includes("|")) {
|
|
974
|
+
const [o, ...u] = n.split("|");
|
|
975
|
+
n = o.trim(), i.alias = u.map((m) => m.trim());
|
|
976
|
+
}
|
|
977
|
+
return n.startsWith("--") && (s = !0, n = n.slice(2)), i.default === "*" && (i.default = [], i.type = ["string"]), n.endsWith("?") && (i.required = !1, n = n.slice(0, -1)), n.endsWith("*") && (i.type = ["string"], i.variadic = !0, i.default = [], n = n.slice(0, -1)), i.description = i.description ?? t[n] ?? t[`--${n}`], { name: n, isOption: s, definition: i };
|
|
343
978
|
}
|
|
979
|
+
}
|
|
980
|
+
class Ae extends F {
|
|
981
|
+
helperDefinitions = {};
|
|
344
982
|
get command() {
|
|
345
983
|
return this.parser ? this.parser.command : this.signature.split(" ")[0];
|
|
346
984
|
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
if (i && i !== 0)
|
|
355
|
-
return i;
|
|
356
|
-
}
|
|
357
|
-
await this.parser.validate();
|
|
358
|
-
const s = this.preHandle ? await this.preHandle() : null;
|
|
359
|
-
return s && s !== 0 ? s : await this.handle() ?? 0;
|
|
985
|
+
newCommandParser(e) {
|
|
986
|
+
return new v({
|
|
987
|
+
io: e.io,
|
|
988
|
+
signature: this.signature,
|
|
989
|
+
helperDefinitions: this.helperDefinitions,
|
|
990
|
+
defaultOptions: this.defaultOptions()
|
|
991
|
+
});
|
|
360
992
|
}
|
|
361
|
-
|
|
362
|
-
|
|
993
|
+
constructor() {
|
|
994
|
+
super("");
|
|
363
995
|
}
|
|
364
|
-
|
|
365
|
-
this.parser.
|
|
996
|
+
option(e, t = null) {
|
|
997
|
+
return this.parser instanceof v ? this.parser.option(e) ?? t : t;
|
|
366
998
|
}
|
|
367
|
-
|
|
368
|
-
return this.parser.
|
|
999
|
+
argument(e, t = null) {
|
|
1000
|
+
return this.parser instanceof v ? this.parser.argument(e) ?? t : t;
|
|
369
1001
|
}
|
|
370
|
-
|
|
371
|
-
|
|
1002
|
+
// Prompt utils
|
|
1003
|
+
async askForConfirmation(...e) {
|
|
1004
|
+
return this.io.askForConfirmation(...e);
|
|
372
1005
|
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
if (!Array.isArray(n))
|
|
376
|
-
throw new Error(`Option ${t} is not an array`);
|
|
377
|
-
return n.length ? n : e;
|
|
1006
|
+
async askForInput(...e) {
|
|
1007
|
+
return this.io.askForInput(...e);
|
|
378
1008
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
return n ? typeof n == "number" ? n : parseInt(n) : e;
|
|
1009
|
+
async askForSelect(...e) {
|
|
1010
|
+
return this.io.askForSelect(...e);
|
|
382
1011
|
}
|
|
383
|
-
|
|
384
|
-
return this.
|
|
1012
|
+
newLoader(...e) {
|
|
1013
|
+
return this.io.newLoader(...e);
|
|
385
1014
|
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
1015
|
+
}
|
|
1016
|
+
class K {
|
|
1017
|
+
level;
|
|
1018
|
+
constructor(e = {}) {
|
|
1019
|
+
this.level = e.level ?? "info";
|
|
391
1020
|
}
|
|
392
|
-
|
|
393
|
-
|
|
1021
|
+
shouldLog(e) {
|
|
1022
|
+
const t = ["debug", "info", "warn", "error"], n = t.indexOf(this.level);
|
|
1023
|
+
return t.indexOf(e) >= n;
|
|
394
1024
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
return n ? typeof n == "number" ? n : parseInt(n) : e;
|
|
1025
|
+
setLevel(e) {
|
|
1026
|
+
this.level = e;
|
|
398
1027
|
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
*/
|
|
402
|
-
async askForConfirmation(...t) {
|
|
403
|
-
return this.io.askForConfirmation(...t);
|
|
1028
|
+
getLevel() {
|
|
1029
|
+
return this.level;
|
|
404
1030
|
}
|
|
405
|
-
|
|
406
|
-
|
|
1031
|
+
log(...e) {
|
|
1032
|
+
console.log(...e);
|
|
407
1033
|
}
|
|
408
|
-
|
|
409
|
-
|
|
1034
|
+
info(...e) {
|
|
1035
|
+
this.shouldLog("info") && console.log(...e);
|
|
410
1036
|
}
|
|
411
|
-
|
|
412
|
-
|
|
1037
|
+
warn(...e) {
|
|
1038
|
+
this.shouldLog("warn") && console.warn(...e);
|
|
413
1039
|
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
constructor(t) {
|
|
417
|
-
super(`Command "${t}" not found.`), this.command = t;
|
|
1040
|
+
error(...e) {
|
|
1041
|
+
this.shouldLog("error") && console.error(...e);
|
|
418
1042
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
t(o`{bgRed ERROR } Command {yellow ${this.command}} not found.`);
|
|
1043
|
+
debug(...e) {
|
|
1044
|
+
this.shouldLog("debug") && console.log(...e);
|
|
422
1045
|
}
|
|
423
1046
|
}
|
|
424
|
-
class
|
|
1047
|
+
class ge {
|
|
425
1048
|
commands = {};
|
|
426
1049
|
io;
|
|
427
|
-
|
|
428
|
-
return "Command";
|
|
429
|
-
}
|
|
1050
|
+
logger;
|
|
430
1051
|
get CommandIOClass() {
|
|
431
|
-
return
|
|
1052
|
+
return z;
|
|
432
1053
|
}
|
|
433
|
-
constructor() {
|
|
434
|
-
this.io = new this.CommandIOClass();
|
|
1054
|
+
constructor(e) {
|
|
1055
|
+
this.logger = e ?? new K(), this.io = new this.CommandIOClass(this.logger);
|
|
435
1056
|
}
|
|
436
1057
|
getAvailableCommands() {
|
|
437
1058
|
return Object.keys(this.commands);
|
|
@@ -439,176 +1060,188 @@ class F {
|
|
|
439
1060
|
getCommands() {
|
|
440
1061
|
return Object.values(this.commands);
|
|
441
1062
|
}
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
let
|
|
445
|
-
|
|
1063
|
+
importFile = async (e) => (await import(e)).default;
|
|
1064
|
+
commandResolver = async (e) => {
|
|
1065
|
+
let t = await this.importFile(e);
|
|
1066
|
+
if (!t)
|
|
1067
|
+
throw new Error(`The command at path ${e} does not have a default export.`);
|
|
1068
|
+
return t && typeof t == "object" && "default" in t && (t = t.default), typeof t == "function" ? new t() : t instanceof F ? t : null;
|
|
446
1069
|
};
|
|
447
|
-
|
|
448
|
-
this.commandResolver =
|
|
1070
|
+
withCommandResolver(e) {
|
|
1071
|
+
return this.commandResolver = e, this;
|
|
1072
|
+
}
|
|
1073
|
+
withFileImporter(e) {
|
|
1074
|
+
return this.importFile = e, this;
|
|
449
1075
|
}
|
|
450
|
-
registerCommand(
|
|
451
|
-
const n =
|
|
1076
|
+
registerCommand(e, t = !1) {
|
|
1077
|
+
const n = e.command;
|
|
452
1078
|
if (!n)
|
|
453
1079
|
throw new Error("Command signature is invalid, it must have a command name.");
|
|
454
|
-
if (!
|
|
1080
|
+
if (!t && this.commands[n])
|
|
455
1081
|
throw new Error(`Command ${n} already registered.`);
|
|
456
|
-
this.commands[n] =
|
|
1082
|
+
this.commands[n] = e;
|
|
457
1083
|
}
|
|
458
|
-
async loadCommandsPath(
|
|
459
|
-
for await (const
|
|
1084
|
+
async loadCommandsPath(e) {
|
|
1085
|
+
for await (const t of this.listCommandsFiles(e))
|
|
460
1086
|
try {
|
|
461
|
-
const n = await this.commandResolver(
|
|
462
|
-
this.registerCommand(n);
|
|
1087
|
+
const n = await this.commandResolver(t);
|
|
1088
|
+
n instanceof F && this.registerCommand(n);
|
|
463
1089
|
} catch (n) {
|
|
464
|
-
throw new Error(`Command ${
|
|
1090
|
+
throw new Error(`Command ${t} failed to load. ${n}`, {
|
|
1091
|
+
cause: n
|
|
1092
|
+
});
|
|
465
1093
|
}
|
|
466
1094
|
}
|
|
467
|
-
async runCommand(
|
|
468
|
-
const s = typeof
|
|
1095
|
+
async runCommand(e, t, ...n) {
|
|
1096
|
+
const s = typeof t == "string" ? this.commands[t] : t, i = typeof t == "string" ? t : s.command;
|
|
469
1097
|
if (!s) {
|
|
470
|
-
const
|
|
471
|
-
return
|
|
1098
|
+
const o = await this.suggestCommand(i);
|
|
1099
|
+
return o ? await this.runCommand(e, o, ...n) : 1;
|
|
472
1100
|
}
|
|
473
|
-
return await s.run(
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
1101
|
+
return await s.run({
|
|
1102
|
+
ctx: e,
|
|
1103
|
+
logger: this.logger,
|
|
1104
|
+
args: n
|
|
1105
|
+
}) ?? 0;
|
|
1106
|
+
}
|
|
1107
|
+
async suggestCommand(e) {
|
|
1108
|
+
const t = this.getAvailableCommands(), { bestMatch: n, bestMatchIndex: s, ratings: i } = ee.findBestMatch(e, t), o = i.filter((u) => u.rating > 0.3).map((u) => u.target);
|
|
1109
|
+
if (n.rating > 0 && o.length <= 1 || n.rating > 0.7 && o.length > 1) {
|
|
1110
|
+
const u = t[s];
|
|
1111
|
+
return await this.askRunSimilarCommand(e, u) ? u : null;
|
|
480
1112
|
}
|
|
481
|
-
if (
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
if (m)
|
|
488
|
-
return m;
|
|
1113
|
+
if (o.length) {
|
|
1114
|
+
this.io.error(`${a.bgRed(" ERROR ")} Command ${a.yellow(e)} not found.
|
|
1115
|
+
`);
|
|
1116
|
+
const u = await this.io.askForSelect(a.green("Did you mean to run one of these commands instead?"), o);
|
|
1117
|
+
if (u)
|
|
1118
|
+
return u;
|
|
489
1119
|
}
|
|
490
|
-
throw new
|
|
1120
|
+
throw new he(e);
|
|
491
1121
|
}
|
|
492
|
-
async askRunSimilarCommand(
|
|
493
|
-
return
|
|
1122
|
+
async askRunSimilarCommand(e, t) {
|
|
1123
|
+
return this.io.error(`${a.bgRed(" ERROR ")} Command ${a.yellow(e)} not found.
|
|
1124
|
+
`), this.io.askForConfirmation(`${a.green(`Do you want to run ${a.yellow(t)} instead?`)} `);
|
|
494
1125
|
}
|
|
495
|
-
async *listCommandsFiles(
|
|
496
|
-
const
|
|
497
|
-
for (const n of
|
|
498
|
-
const s =
|
|
1126
|
+
async *listCommandsFiles(e) {
|
|
1127
|
+
const t = Z.readdirSync(e, { withFileTypes: !0 });
|
|
1128
|
+
for (const n of t) {
|
|
1129
|
+
const s = I.resolve(e, n.name);
|
|
499
1130
|
if (n.isDirectory())
|
|
500
|
-
yield* this.listCommandsFiles(
|
|
1131
|
+
yield* this.listCommandsFiles(I.resolve(e, n.name));
|
|
501
1132
|
else {
|
|
502
|
-
if (!s.endsWith(
|
|
1133
|
+
if (!s.endsWith(".ts") && !s.endsWith(".js") && !s.endsWith(".mjs") && !s.endsWith(".cjs"))
|
|
503
1134
|
continue;
|
|
504
1135
|
yield s;
|
|
505
1136
|
}
|
|
506
1137
|
}
|
|
507
1138
|
}
|
|
508
1139
|
}
|
|
509
|
-
class
|
|
510
|
-
|
|
511
|
-
|
|
1140
|
+
class fe {
|
|
1141
|
+
logger;
|
|
1142
|
+
constructor(e) {
|
|
1143
|
+
this.logger = e;
|
|
1144
|
+
}
|
|
1145
|
+
handle(e) {
|
|
1146
|
+
if (e instanceof w)
|
|
1147
|
+
return e.pretty(this.logger), -1;
|
|
1148
|
+
throw e;
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
class ye extends F {
|
|
1152
|
+
constructor(e) {
|
|
1153
|
+
super("help", {
|
|
1154
|
+
description: a.bold("Show help information about the CLI and its commands")
|
|
1155
|
+
}), this.opts = e;
|
|
512
1156
|
}
|
|
513
|
-
signature = "help";
|
|
514
|
-
description = "Show help";
|
|
515
1157
|
async handle() {
|
|
516
|
-
const
|
|
517
|
-
|
|
1158
|
+
const e = this.opts.commandRegistry.getCommands(), t = this.opts.cliName ?? "Bob CLI", n = this.opts.cliVersion ?? "0.0.0", s = (await import("./package-O5nP1XlU.js"))?.default?.version ?? "0.0.0";
|
|
1159
|
+
this.io.log(`${t} ${a.green(n)} (core: ${a.yellow(s)})
|
|
518
1160
|
|
|
519
|
-
{yellow
|
|
1161
|
+
${a.yellow("Usage")}:
|
|
520
1162
|
command [options] [arguments]
|
|
521
1163
|
|
|
522
|
-
{yellow
|
|
1164
|
+
${a.yellow("Available commands")}:
|
|
523
1165
|
`);
|
|
524
|
-
const
|
|
525
|
-
for (const
|
|
526
|
-
const
|
|
527
|
-
|
|
1166
|
+
const i = Math.max(...e.map((m) => m.command.length)) ?? 0, o = {};
|
|
1167
|
+
for (const m of e) {
|
|
1168
|
+
const h = m.group ?? m.command.split(":")[0];
|
|
1169
|
+
o[h] || (o[h] = []), o[h].push(m);
|
|
528
1170
|
}
|
|
529
|
-
const
|
|
530
|
-
for (const [
|
|
531
|
-
const
|
|
532
|
-
|
|
533
|
-
const
|
|
534
|
-
for (const
|
|
535
|
-
let g =
|
|
536
|
-
|
|
1171
|
+
const u = Object.entries(o).sort(([m], [h]) => m.toLowerCase().localeCompare(h.toLowerCase())).sort(([, m], [, h]) => m.length - h.length);
|
|
1172
|
+
for (const [m, h] of u) {
|
|
1173
|
+
const l = h.length > 1;
|
|
1174
|
+
l && this.io.log(a.yellow(`${m}:`));
|
|
1175
|
+
const d = h.sort((p, g) => p.command.toLowerCase().localeCompare(g.command.toLowerCase()));
|
|
1176
|
+
for (const p of d) {
|
|
1177
|
+
let g = j(i - p.command.length);
|
|
1178
|
+
l && (g = g.slice(2)), this.io.log(`${l ? " " : ""}${a.green(p.command)} ${g} ${p.description}`);
|
|
537
1179
|
}
|
|
538
1180
|
}
|
|
539
1181
|
}
|
|
540
1182
|
}
|
|
541
|
-
class
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
t.reason ? e += ` Reason: ${t.reason}` : e += ` Value: "${t.value}"`, super(e), this.param = t;
|
|
545
|
-
}
|
|
546
|
-
pretty() {
|
|
547
|
-
const t = console.log;
|
|
548
|
-
t(o` {white.bgRed ERROR } Argument {bold.yellow ${this.param.param}} value is invalid. `), (this.param.value || this.param.reason) && t(""), this.param.value && t(o` {blue Value}: ${this.param.value}`), this.param.reason && t(o` {yellow Reason}: ${this.param.reason}`);
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
class M extends c {
|
|
552
|
-
constructor(t) {
|
|
553
|
-
let e = `Option "${t.option}" value is invalid.`;
|
|
554
|
-
t.reason ? e += ` Reason: ${t.reason}` : e += ` Value: "${t.value}"`, super(e), this.param = t;
|
|
555
|
-
}
|
|
556
|
-
pretty() {
|
|
557
|
-
const t = console.log;
|
|
558
|
-
t(o` {white.bgRed ERROR } Option {bold.yellow ${this.param.option}} value is invalid. `), (this.param.value || this.param.reason) && t(""), this.param.value && t(o` {blue Value}: ${this.param.value}`), this.param.reason && t(o` {yellow Reason}: ${this.param.reason}`);
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
class H {
|
|
562
|
-
handle(t) {
|
|
563
|
-
if (t instanceof c)
|
|
564
|
-
return t.pretty(), -1;
|
|
565
|
-
throw t;
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
class N {
|
|
1183
|
+
class Ce {
|
|
1184
|
+
ctx;
|
|
1185
|
+
logger;
|
|
569
1186
|
commandRegistry;
|
|
570
1187
|
exceptionHandler;
|
|
571
|
-
ctx;
|
|
572
1188
|
helpCommand;
|
|
573
|
-
|
|
574
|
-
return
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
return
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
return
|
|
581
|
-
}
|
|
582
|
-
constructor(
|
|
583
|
-
this.ctx =
|
|
584
|
-
|
|
585
|
-
|
|
1189
|
+
newCommandRegistry(e) {
|
|
1190
|
+
return new ge(e.logger);
|
|
1191
|
+
}
|
|
1192
|
+
newHelpCommand(e) {
|
|
1193
|
+
return new ye(e);
|
|
1194
|
+
}
|
|
1195
|
+
newExceptionHandler(e) {
|
|
1196
|
+
return new fe(e.logger);
|
|
1197
|
+
}
|
|
1198
|
+
constructor(e = {}) {
|
|
1199
|
+
this.ctx = e.ctx, this.logger = e.logger ?? new K(), this.commandRegistry = this.newCommandRegistry({
|
|
1200
|
+
logger: this.logger
|
|
1201
|
+
}), this.exceptionHandler = this.newExceptionHandler({
|
|
1202
|
+
logger: this.logger
|
|
1203
|
+
}), this.helpCommand = this.newHelpCommand({
|
|
1204
|
+
cliName: e.name,
|
|
1205
|
+
cliVersion: e.version,
|
|
586
1206
|
commandRegistry: this.commandRegistry
|
|
587
1207
|
});
|
|
588
1208
|
}
|
|
589
|
-
|
|
590
|
-
this.commandRegistry.
|
|
1209
|
+
withCommandResolver(e) {
|
|
1210
|
+
return this.commandRegistry.withCommandResolver(e), this;
|
|
1211
|
+
}
|
|
1212
|
+
withFileImporter(e) {
|
|
1213
|
+
return this.commandRegistry.withFileImporter(e), this;
|
|
591
1214
|
}
|
|
592
|
-
async withCommands(...
|
|
593
|
-
for (const
|
|
594
|
-
typeof
|
|
1215
|
+
async withCommands(...e) {
|
|
1216
|
+
for (const t of e)
|
|
1217
|
+
typeof t == "string" ? await this.commandRegistry.loadCommandsPath(t) : typeof t == "function" ? this.registerCommand(new t()) : this.registerCommand(t);
|
|
595
1218
|
}
|
|
596
|
-
async runCommand(
|
|
597
|
-
return
|
|
1219
|
+
async runCommand(e, ...t) {
|
|
1220
|
+
return e ? await this.commandRegistry.runCommand(this.ctx ?? {}, e, ...t).catch(this.exceptionHandler.handle.bind(this.exceptionHandler)) : await this.runHelpCommand();
|
|
598
1221
|
}
|
|
599
1222
|
async runHelpCommand() {
|
|
600
1223
|
return await this.runCommand(this.helpCommand);
|
|
601
1224
|
}
|
|
602
|
-
registerCommand(
|
|
603
|
-
this.commandRegistry.registerCommand(
|
|
1225
|
+
registerCommand(e) {
|
|
1226
|
+
this.commandRegistry.registerCommand(e);
|
|
604
1227
|
}
|
|
605
1228
|
}
|
|
606
1229
|
export {
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
1230
|
+
B as BadCommandOption,
|
|
1231
|
+
$e as BadCommandParameter,
|
|
1232
|
+
w as BobError,
|
|
1233
|
+
Ce as Cli,
|
|
1234
|
+
F as Command,
|
|
1235
|
+
z as CommandIO,
|
|
1236
|
+
he as CommandNotFoundError,
|
|
1237
|
+
J as CommandParser,
|
|
1238
|
+
ge as CommandRegistry,
|
|
1239
|
+
v as CommandSignatureParser,
|
|
1240
|
+
Ae as CommandWithSignature,
|
|
1241
|
+
fe as ExceptionHandler,
|
|
1242
|
+
pe as HelpOption,
|
|
1243
|
+
k as InvalidOption,
|
|
1244
|
+
K as Logger,
|
|
1245
|
+
Y as MissingRequiredArgumentValue,
|
|
1246
|
+
ce as MissingRequiredOptionValue
|
|
614
1247
|
};
|