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