bob-core 2.0.0-beta.4 → 2.0.0-beta.6

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