convoker 0.2.0 → 0.3.0

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.
@@ -87,9 +87,9 @@ export {
87
87
  o as C,
88
88
  i as H,
89
89
  n as I,
90
- c as M,
90
+ d as M,
91
91
  a as T,
92
92
  u as U,
93
- d as a,
93
+ c as a,
94
94
  l as e
95
95
  };
@@ -1,6 +1,6 @@
1
1
  import { D as y } from "./color-CiruG_zQ.js";
2
- import { v as p } from "./standard-schema-CFxVDMhv.js";
3
- import { I as f } from "./error-CBR2veuf.js";
2
+ import { v as p } from "./standard-schema-BHKzvwIS.js";
3
+ import { I as f } from "./error-CyKscMUD.js";
4
4
  import { i as $, a as v, b } from "./utils-DdmSEjLc.js";
5
5
  import { r as m, a as g, c as d, b as x, d as D } from "./__vite-browser-external-DQYBmsno.js";
6
6
  let l = y;
@@ -1,4 +1,4 @@
1
- import { v as o } from "./standard-schema-CFxVDMhv.js";
1
+ import { v as o } from "./standard-schema-BHKzvwIS.js";
2
2
  async function a(r, t) {
3
3
  async function i(e) {
4
4
  if (typeof r == "string")
@@ -1,4 +1,4 @@
1
- import { I as i } from "./error-CBR2veuf.js";
1
+ import { I as i } from "./error-CyKscMUD.js";
2
2
  async function o(a, t) {
3
3
  const s = await a["~standard"].validate(t);
4
4
  if (s.issues) {
package/dist/command.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Command action function.
3
3
  */
4
- export declare type ActionFn<T extends Input> = (input: InferInput<T>) => void | Promise<void>;
4
+ export declare type ActionFn<T extends Input> = (input: InferInput<T>) => any | Promise<any>;
5
5
 
6
6
  /**
7
7
  * A basic input type.
@@ -57,6 +57,10 @@ export declare class Command<T extends Input = Input> {
57
57
  * The action function of this command.
58
58
  */
59
59
  $fn: ActionFn<T> | undefined;
60
+ /**
61
+ * The middlewares associated with this command.
62
+ */
63
+ $middlewares: MiddlewareFn<T>[];
60
64
  /**
61
65
  * The error handler of this command.
62
66
  */
@@ -92,6 +96,12 @@ export declare class Command<T extends Input = Input> {
92
96
  * @returns this
93
97
  */
94
98
  input<TInput extends Input>(input: TInput): Command<TInput>;
99
+ /**
100
+ * Adds a chain of middlewares.
101
+ * @param fns The middlewares to use.
102
+ * @returns this
103
+ */
104
+ use(...fns: MiddlewareFn<T>[]): this;
95
105
  /**
96
106
  * Sets the action function for this command.
97
107
  * @param fn The action.
@@ -135,6 +145,11 @@ export declare class Command<T extends Input = Input> {
135
145
  */
136
146
  parse(argv: string[]): Promise<ParseResult<T>>;
137
147
  private buildInputMap;
148
+ /**
149
+ * Allows surpassing the amount of arguments specified.
150
+ * @returns this
151
+ */
152
+ allowSurpassArgLimit(): this;
138
153
  /**
139
154
  * Gets the full command path (name including parents).
140
155
  * @returns The full command path.
@@ -227,6 +242,11 @@ declare interface Input {
227
242
  */
228
243
  declare type Kind = BasicKind | StandardSchemaV1<any, any>;
229
244
 
245
+ /**
246
+ * Command middleware function.
247
+ */
248
+ export declare type MiddlewareFn<T extends Input = Input> = (input: InferInput<T>, next: () => Promise<any>) => any | Promise<any>;
249
+
230
250
  /**
231
251
  * An option.
232
252
  */
package/dist/command.js CHANGED
@@ -1,217 +1,225 @@
1
1
  import { D as V, b, c as v, g as O } from "./chunks/color-CiruG_zQ.js";
2
- import { s as q } from "./chunks/index-D7JQKzRX.js";
3
- import { m as R, i as A, b as W, a as j } from "./chunks/utils-DdmSEjLc.js";
4
- import { T as H, M as z, a as N, C as J, H as P, U as K } from "./chunks/error-CBR2veuf.js";
5
- import { P as B, c as G, O as _ } from "./chunks/input-BfYvlWdG.js";
2
+ import { s as R } from "./chunks/index-BluQjWvw.js";
3
+ import { m as z, i as A, b as j, a as P } from "./chunks/utils-DdmSEjLc.js";
4
+ import { M as U, a as J, T as N, C as K, H as L, U as x } from "./chunks/error-CyKscMUD.js";
5
+ import { P as q, c as G, O as F } from "./chunks/input-WNu16aNE.js";
6
6
  async function Q() {
7
- if (W && process.stdout?.writable) {
8
- const { Writable: n } = await import("./chunks/__vite-browser-external-DQYBmsno.js").then((e) => e._);
9
- return n.toWeb(process.stdout);
7
+ if (j && process.stdout?.writable) {
8
+ const { Writable: r } = await import("./chunks/__vite-browser-external-DQYBmsno.js").then((t) => t._);
9
+ return r.toWeb(process.stdout);
10
10
  }
11
- return A && Deno.stdout?.writable ? Deno.stdout.writable : j && Bun.stdout ? Bun.stdout : new WritableStream({
12
- write(n) {
13
- console.log(String(n));
11
+ return A && Deno.stdout?.writable ? Deno.stdout.writable : P && Bun.stdout ? Bun.stdout : new WritableStream({
12
+ write(r) {
13
+ console.log(String(r));
14
14
  }
15
15
  });
16
16
  }
17
17
  async function X() {
18
- if (W && process.stderr?.writable) {
19
- const { Writable: n } = await import("./chunks/__vite-browser-external-DQYBmsno.js").then((e) => e._);
20
- return n.toWeb(process.stderr);
18
+ if (j && process.stderr?.writable) {
19
+ const { Writable: r } = await import("./chunks/__vite-browser-external-DQYBmsno.js").then((t) => t._);
20
+ return r.toWeb(process.stderr);
21
21
  }
22
- return A && Deno.stderr?.writable ? Deno.stderr.writable : j && Bun.stderr ? Bun.stderr : new WritableStream({
23
- write(n) {
24
- console.error(String(n));
22
+ return A && Deno.stderr?.writable ? Deno.stderr.writable : P && Bun.stderr ? Bun.stderr : new WritableStream({
23
+ write(r) {
24
+ console.error(String(r));
25
25
  }
26
26
  });
27
27
  }
28
28
  async function Y() {
29
- if (W && process.stdin?.readable) {
30
- const { Readable: n } = await import("./chunks/__vite-browser-external-DQYBmsno.js").then((e) => e._);
31
- return n.toWeb(process.stdin);
29
+ if (j && process.stdin?.readable) {
30
+ const { Readable: r } = await import("./chunks/__vite-browser-external-DQYBmsno.js").then((t) => t._);
31
+ return r.toWeb(process.stdin);
32
32
  }
33
- return A && Deno.stdin?.readable ? Deno.stdin.readable : j ? Bun.stdin.stream() : new ReadableStream({
34
- start(n) {
35
- n.close();
33
+ return A && Deno.stdin?.readable ? Deno.stdin.readable : P ? Bun.stdin.stream() : new ReadableStream({
34
+ start(r) {
35
+ r.close();
36
36
  }
37
37
  });
38
38
  }
39
39
  let m = V, g;
40
- function I(n) {
41
- m = n;
40
+ function H(r) {
41
+ m = r;
42
42
  }
43
- async function L(n) {
44
- g = R(
43
+ async function _(r) {
44
+ g = z(
45
45
  {
46
46
  format: "text",
47
47
  stdout: await Q(),
48
48
  stderr: await X(),
49
49
  stdin: await Y()
50
50
  },
51
- n
51
+ r
52
52
  );
53
53
  }
54
54
  async function Z() {
55
- await L({});
55
+ await _({});
56
56
  }
57
- function S(n, ...e) {
58
- const t = (/* @__PURE__ */ new Date()).toISOString(), r = e.map((i) => typeof i == "string" ? i : JSON.stringify(i, null, 2)).join(" ");
57
+ function S(r, ...t) {
58
+ const e = (/* @__PURE__ */ new Date()).toISOString(), n = t.map((i) => typeof i == "string" ? i : JSON.stringify(i, null, 2)).join(" ");
59
59
  switch (g.format) {
60
60
  case "json":
61
- return JSON.stringify({ timestamp: t, level: n, message: r }) + `
61
+ return JSON.stringify({ timestamp: e, level: r, message: n }) + `
62
62
  `;
63
63
  case "xml":
64
64
  return `<log>
65
- <timestamp>${t}</timestamp>
66
- <level>${n}</level>
67
- <message>${r}</message>
65
+ <timestamp>${e}</timestamp>
66
+ <level>${r}</level>
67
+ <message>${n}</message>
68
68
  </log>
69
69
  `;
70
70
  case "yaml":
71
- return `- timestamp: ${t}
72
- level: ${n}
73
- message: "${r.replace(/"/g, '\\"')}"
71
+ return `- timestamp: ${e}
72
+ level: ${r}
73
+ message: "${n.replace(/"/g, '\\"')}"
74
74
  `;
75
75
  case "csv":
76
- return `"${t}","${n}","${r.replace(/"/g, '""')}"
76
+ return `"${e}","${r}","${n.replace(/"/g, '""')}"
77
77
  `;
78
78
  case "text":
79
79
  default:
80
- return `[${t}] [${n.toUpperCase()}] ${r}
80
+ return `[${e}] [${r.toUpperCase()}] ${n}
81
81
  `;
82
82
  }
83
83
  }
84
- function E(n, e) {
85
- switch (n) {
84
+ function E(r, t) {
85
+ switch (r) {
86
86
  case "trace":
87
- return m.secondary ? m.secondary(e) : e;
87
+ return m.secondary ? m.secondary(t) : t;
88
88
  case "info":
89
- return m.info ? m.info(e) : e;
89
+ return m.info ? m.info(t) : t;
90
90
  case "warn":
91
- return m.warning ? m.warning(e) : e;
91
+ return m.warning ? m.warning(t) : t;
92
92
  case "error":
93
- return m.error ? m.error(e) : e;
93
+ return m.error ? m.error(t) : t;
94
94
  case "fatal":
95
- return m.error ? m.error(m.styles?.bold?.(e) ?? e) : e;
95
+ return m.error ? m.error(m.styles?.bold?.(t) ?? t) : t;
96
96
  default:
97
- return e;
97
+ return t;
98
98
  }
99
99
  }
100
- async function k(n, e) {
101
- const t = n.getWriter();
100
+ async function k(r, t) {
101
+ const e = r.getWriter();
102
102
  try {
103
- await t.write(e);
103
+ await e.write(t);
104
104
  } finally {
105
- t.releaseLock();
105
+ e.releaseLock();
106
106
  }
107
107
  }
108
- async function x(...n) {
109
- const e = S("trace", ...n), t = E("trace", e);
110
- await k(g.stdout, t);
108
+ async function ee(...r) {
109
+ const t = S("trace", ...r), e = E("trace", t);
110
+ await k(g.stdout, e);
111
111
  }
112
- async function tt(...n) {
113
- const e = S("info", ...n), t = E("info", e);
114
- await k(g.stdout, t);
112
+ async function te(...r) {
113
+ const t = S("info", ...r), e = E("info", t);
114
+ await k(g.stdout, e);
115
115
  }
116
- async function et(...n) {
117
- const e = S("warn", ...n), t = E("warn", e);
118
- await k(g.stdout, t);
116
+ async function ne(...r) {
117
+ const t = S("warn", ...r), e = E("warn", t);
118
+ await k(g.stdout, e);
119
119
  }
120
- async function nt(...n) {
121
- const e = S("error", ...n), t = E("error", e);
122
- await k(g.stderr, t);
120
+ async function re(...r) {
121
+ const t = S("error", ...r), e = E("error", t);
122
+ await k(g.stderr, e);
123
123
  }
124
- async function rt(...n) {
125
- const e = S("fatal", ...n), t = E("fatal", e);
126
- await k(g.stderr, t), A ? Deno.exit(-1) : (W || j) && process.exit(-1);
124
+ async function se(...r) {
125
+ const t = S("fatal", ...r), e = E("fatal", t);
126
+ await k(g.stderr, e), A ? Deno.exit(-1) : (j || P) && process.exit(-1);
127
127
  }
128
- const ct = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
128
+ const de = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
129
129
  __proto__: null,
130
- error: nt,
131
- fatal: rt,
132
- info: tt,
133
- setConfig: L,
134
- setTheme: I,
130
+ error: re,
131
+ fatal: se,
132
+ info: te,
133
+ setConfig: _,
134
+ setTheme: H,
135
135
  setup: Z,
136
- trace: x,
137
- warn: et
136
+ trace: ee,
137
+ warn: ne
138
138
  }, Symbol.toStringTag, { value: "Module" }));
139
- class F {
139
+ class B {
140
140
  /**
141
141
  * Creates a new command.
142
142
  * @param names The names (aliases).
143
143
  * @param desc The description.
144
144
  * @param version The version.
145
145
  */
146
- constructor(e, t, r) {
147
- this.$children = /* @__PURE__ */ new Map(), this.$allowUnknownOptions = !1, this.$allowSurpassArgLimit = !1, this.$input = {}, this.$fn = void 0, this.$errorFn = void 0, this.$names = Array.isArray(e) ? e : [e], this.$description = t, this.$version = r;
146
+ constructor(t, e, n) {
147
+ this.$children = /* @__PURE__ */ new Map(), this.$allowUnknownOptions = !1, this.$allowSurpassArgLimit = !1, this.$input = {}, this.$fn = void 0, this.$middlewares = [], this.$errorFn = void 0, this.$names = Array.isArray(t) ? t : [t], this.$description = e, this.$version = n;
148
148
  }
149
149
  /**
150
150
  * Adds a set of aliases to this command.
151
151
  * @param aliases The aliases to add.
152
152
  * @returns this
153
153
  */
154
- alias(...e) {
155
- return this.$names.concat(e), this.$parent?.add(this), this;
154
+ alias(...t) {
155
+ return this.$names.concat(t), this.$parent?.add(this), this;
156
156
  }
157
157
  /**
158
158
  * Adds a description to this command.
159
159
  * @param desc The description.
160
160
  * @returns this
161
161
  */
162
- description(e) {
163
- return this.$description = e, this;
162
+ description(t) {
163
+ return this.$description = t, this;
164
164
  }
165
165
  /**
166
166
  * Adds a version to this command.
167
167
  * @param version The version.
168
168
  * @returns this
169
169
  */
170
- version(e) {
171
- return this.$version = e, this;
170
+ version(t) {
171
+ return this.$version = t, this;
172
172
  }
173
173
  /**
174
174
  * Sets the input for this command.
175
175
  * @param version The input.
176
176
  * @returns this
177
177
  */
178
- input(e) {
179
- return this.$input = e, this;
178
+ input(t) {
179
+ return this.$input = t, this;
180
+ }
181
+ /**
182
+ * Adds a chain of middlewares.
183
+ * @param fns The middlewares to use.
184
+ * @returns this
185
+ */
186
+ use(...t) {
187
+ return this.$middlewares.push(...t), this;
180
188
  }
181
189
  /**
182
190
  * Sets the action function for this command.
183
191
  * @param fn The action.
184
192
  * @returns this
185
193
  */
186
- action(e) {
187
- return this.$fn = e, this;
194
+ action(t) {
195
+ return this.$fn = t, this;
188
196
  }
189
197
  /**
190
198
  * Sets the error function for this command.
191
199
  * @param fn The error handler.
192
200
  * @returns this
193
201
  */
194
- error(e) {
195
- return this.$errorFn = e, this;
202
+ error(t) {
203
+ return this.$errorFn = t, this;
196
204
  }
197
205
  /**
198
206
  * Adds an existing command to this.
199
207
  * @param command The command.
200
208
  * @returns this
201
209
  */
202
- add(e) {
203
- e.$parent = this;
204
- const t = { command: e, alias: e.$names[0] };
205
- for (let r = 0; r < e.$names.length; r++)
206
- r === 0 && this.$children.set(e.$names[r], { command: e }), this.$children.set(e.$names[r], t);
210
+ add(t) {
211
+ t.$parent = this;
212
+ const e = { command: t, alias: t.$names[0] };
213
+ for (let n = 0; n < t.$names.length; n++)
214
+ n === 0 && this.$children.set(t.$names[n], { command: t }), this.$children.set(t.$names[n], e);
207
215
  return this;
208
216
  }
209
- subCommand(e, t, r) {
210
- if (typeof t == "function") {
211
- const a = new F(e);
212
- return t(a), this.add(a), this;
217
+ subCommand(t, e, n) {
218
+ if (typeof e == "function") {
219
+ const a = new B(t);
220
+ return e(a), this.add(a), this;
213
221
  }
214
- const i = new F(e, t, r);
222
+ const i = new B(t, e, n);
215
223
  return this.add(i), i;
216
224
  }
217
225
  /**
@@ -226,132 +234,136 @@ class F {
226
234
  * @param argv The arguments to parse.
227
235
  * @returns A parse result.
228
236
  */
229
- async parse(e) {
230
- let t = this, r = !1;
231
- const i = {}, a = [], d = {}, s = [], c = t.buildInputMap();
232
- function u(f, o) {
233
- const l = c.get(f);
234
- return l ? l.value : (!t.$allowUnknownOptions && !o && s.push(new K(t, f)), null);
237
+ async parse(t) {
238
+ let e = this, n = !1;
239
+ const i = {}, a = [], f = {}, s = [], c = e.buildInputMap();
240
+ function d(u, o) {
241
+ const l = c.get(u);
242
+ return l ? l.value : (!e.$allowUnknownOptions && !o && s.push(new x(e, u)), null);
235
243
  }
236
- function h(f, o, l) {
237
- o.$kind === "boolean" ? d[f] = "true" : l !== void 0 && (d[f] = l);
244
+ function h(u, o, l) {
245
+ o.$kind === "boolean" ? f[u] = "true" : l !== void 0 && (f[u] = l);
238
246
  }
239
- let p = !1, C = !1;
240
- for (let f = 0; f < e.length; f++) {
241
- const o = e[f];
247
+ let p = !1, W = !1;
248
+ for (let u = 0; u < t.length; u++) {
249
+ const o = t[u];
242
250
  if (o.startsWith("--")) {
243
251
  const [l, w] = o.slice(2).split("=");
244
252
  let y = !1;
245
- l === "help" ? (C = !0, y = !0) : l === "version" && (p = !0, y = !0);
246
- const $ = u(l, y);
253
+ l === "help" ? (W = !0, y = !0) : l === "version" && (p = !0, y = !0);
254
+ const $ = d(l, y);
247
255
  $ && (w === void 0 ? h(
248
256
  l,
249
257
  $,
250
- $.$kind === "boolean" ? void 0 : e[++f]
258
+ $.$kind === "boolean" ? void 0 : t[++u]
251
259
  ) : h(l, $, w));
252
260
  } else if (o.startsWith("-")) {
253
261
  const [l, w] = o.slice(1).split("="), y = l.split("");
254
262
  let $ = w;
255
263
  for (const D of y) {
256
- let T = !1;
257
- D === "h" ? (C = !0, T = !0) : D === "V" && (p = !0, T = !0);
258
- const U = u(D, T);
259
- U && (U.$kind !== "boolean" && $ === void 0 && ($ = e[++f]), h(D, U, $), $ = void 0);
264
+ let C = !1;
265
+ D === "h" ? (W = !0, C = !0) : D === "V" && (p = !0, C = !0);
266
+ const T = d(D, C);
267
+ T && (T.$kind !== "boolean" && $ === void 0 && ($ = t[++u]), h(D, T, $), $ = void 0);
260
268
  }
261
269
  } else
262
- t.$children.has(o) && !r ? (t = t.$children.get(o).command, t.$theme && (q(t.$theme), I(t.$theme))) : (r = !0, a.push(o));
270
+ e.$children.has(o) && !n ? (e = e.$children.get(o).command, e.$theme && (R(e.$theme), H(e.$theme))) : (n = !0, a.push(o));
263
271
  }
264
272
  let M = 0;
265
- for (const f in t.$input) {
266
- const o = t.$input[f];
273
+ for (const u in e.$input) {
274
+ const o = e.$input[u];
267
275
  let l;
268
- if (o instanceof B) {
269
- if (o.$list) {
270
- if (l = a.slice(M), M = a.length, !t.$allowSurpassArgLimit && l.length === 0)
271
- throw new H(t);
272
- } else if (l = a[M++], M > a.length && !t.$allowSurpassArgLimit)
273
- throw new H(t);
274
- } else
276
+ if (o instanceof q)
277
+ o.$list ? (l = a.slice(M), M = a.length, !e.$allowSurpassArgLimit && l.length === 0 && o.$required && s.push(new U(e, u, o))) : (l = a[M++], l === void 0 && o.$required && s.push(new U(e, u, o)));
278
+ else
275
279
  for (const w of o.$names)
276
- if (d[w] !== void 0) {
277
- l = o.$list ? d[w] : d[w].split(o.$separator ?? ",");
280
+ if (f[w] !== void 0) {
281
+ l = o.$list ? f[w].split(o.$separator ?? ",") : f[w];
278
282
  break;
279
283
  }
280
- l !== void 0 ? i[f] = await G(o.$kind, l) : o.$default !== void 0 ? i[f] = o.$default : o.$required && (o instanceof _ ? s.push(new z(t, f, o)) : s.push(new N(t, f, o)));
284
+ l !== void 0 ? i[u] = await G(o.$kind, l) : o.$default !== void 0 ? i[u] = o.$default : o.$required && (o instanceof F ? s.push(new J(e, u, o)) : s.push(new U(e, u, o)));
281
285
  }
282
- return {
286
+ const I = a.slice(M);
287
+ return !e.$allowSurpassArgLimit && I.length > 0 && s.push(new N(e)), {
283
288
  input: i,
284
- command: t,
289
+ command: e,
285
290
  errors: s,
286
291
  isVersion: p,
287
- isHelp: C
292
+ isHelp: W
288
293
  };
289
294
  }
290
- buildInputMap(e) {
291
- const t = /* @__PURE__ */ new Map();
292
- let r = 0;
295
+ buildInputMap(t) {
296
+ const e = /* @__PURE__ */ new Map();
297
+ let n = 0;
293
298
  for (const i in this.$input) {
294
299
  const a = this.$input[i];
295
- if (a instanceof B)
296
- t.set(r++, { value: a, key: i });
300
+ if (a instanceof q)
301
+ e.set(n++, { value: a, key: i });
297
302
  else
298
- for (const d of a.$names)
299
- t.set(d, { value: a, key: i });
303
+ for (const f of a.$names)
304
+ e.set(f, { value: a, key: i });
300
305
  }
301
- if (!e)
306
+ if (!t)
302
307
  for (const [i, a] of this.$parent?.buildInputMap() ?? [])
303
- t.set(i, a);
308
+ e.set(i, a);
304
309
  for (const [, { command: i }] of this.$children)
305
- for (const [a, d] of i.buildInputMap(!0))
306
- t.set(a, d);
307
- return t;
310
+ for (const [a, f] of i.buildInputMap(!0))
311
+ e.set(a, f);
312
+ return e;
313
+ }
314
+ /**
315
+ * Allows surpassing the amount of arguments specified.
316
+ * @returns this
317
+ */
318
+ allowSurpassArgLimit() {
319
+ return this.$allowSurpassArgLimit = !0, this;
308
320
  }
309
321
  /**
310
322
  * Gets the full command path (name including parents).
311
323
  * @returns The full command path.
312
324
  */
313
325
  fullCommandPath() {
314
- const e = [];
315
- let t = this;
316
- for (; t; )
317
- e.unshift(t.$names[0]), t = t.$parent;
318
- return e.join(" ");
326
+ const t = [];
327
+ let e = this;
328
+ for (; e; )
329
+ t.unshift(e.$names[0]), e = e.$parent;
330
+ return t.join(" ");
319
331
  }
320
332
  /**
321
333
  * The default error screen.
322
334
  * @param errors The errors.
323
335
  */
324
- defaultErrorScreen(e) {
325
- let t = !1;
326
- const r = [];
327
- for (const s of e)
328
- s instanceof J ? (s instanceof P || s.print(), t = !0) : r.push(s);
329
- if (r.length) throw r[0];
330
- if (!t) return;
336
+ defaultErrorScreen(t) {
337
+ let e = !1;
338
+ const n = [];
339
+ for (const s of t)
340
+ s instanceof K ? (s instanceof L || s.print(), e = !0) : n.push(s);
341
+ if (n.length) throw n[0];
342
+ if (!e) return;
331
343
  const i = (s, c) => s.padEnd(c, " ");
332
344
  console.log(
333
345
  `${b("usage:")} ${v(this.fullCommandPath())} ${O("[options] [arguments]")}`
334
346
  ), this.$description && console.log(`${this.$description}`), this.$version && console.log(`${b("version")} ${this.$version}`);
335
- const a = Object.entries(this.$input).filter(([, s]) => s instanceof _).map(([s, c]) => ({ key: s, entry: c }));
347
+ const a = Object.entries(this.$input).filter(([, s]) => s instanceof F).map(([s, c]) => ({ key: s, entry: c }));
336
348
  if (a.length > 0) {
337
349
  console.log(b("options:"));
338
350
  const s = Math.max(
339
351
  ...a.map(({ entry: c }) => c.$names.join(", ").length)
340
352
  );
341
353
  for (const { entry: c } of a) {
342
- const u = c.$names.map((p) => p.length === 1 ? `-${p}` : `--${p}`).join(", "), h = ` ${v(i(u, s + 4))}${O(c.$description ?? "")}`;
354
+ const d = c.$names.map((p) => p.length === 1 ? `-${p}` : `--${p}`).join(", "), h = ` ${v(i(d, s + 4))}${O(c.$description ?? "")}`;
343
355
  console.log(h);
344
356
  }
345
357
  }
346
- const d = Object.entries(this.$input).filter(([, s]) => s instanceof B).map(([s, c]) => ({
358
+ const f = Object.entries(this.$input).filter(([, s]) => s instanceof q).map(([s, c]) => ({
347
359
  key: s,
348
360
  entry: c
349
361
  }));
350
- if (d.length > 0) {
362
+ if (f.length > 0) {
351
363
  console.log(b("arguments:"));
352
- const s = Math.max(...d.map(({ key: c }) => c.length));
353
- for (const { key: c, entry: u } of d) {
354
- const h = u.$required ? `<${c}>` : `[${c}]`, p = ` ${v(i(h, s + 4))}${O(u.$description ?? "")}`;
364
+ const s = Math.max(...f.map(({ key: c }) => c.length));
365
+ for (const { key: c, entry: d } of f) {
366
+ const h = d.$required ? `<${c}>` : `[${c}]`, p = ` ${v(i(h, s + 4))}${O(d.$description ?? "")}`;
355
367
  console.log(p);
356
368
  }
357
369
  }
@@ -359,14 +371,14 @@ class F {
359
371
  console.log(b("sub commands:"));
360
372
  const s = Array.from(
361
373
  new Map(
362
- [...this.$children.values()].map((u) => [
363
- u.command.$names[0],
364
- u.command
374
+ [...this.$children.values()].map((d) => [
375
+ d.command.$names[0],
376
+ d.command
365
377
  ])
366
378
  ).values()
367
- ), c = Math.max(...s.map((u) => u.$names[0].length));
368
- for (const u of s) {
369
- const h = ` ${v(i(u.$names[0], c + 4))}${O(u.$description) ?? ""}`;
379
+ ), c = Math.max(...s.map((d) => d.$names[0].length));
380
+ for (const d of s) {
381
+ const h = ` ${v(i(d.$names[0], c + 4))}${O(d.$description) ?? ""}`;
370
382
  console.log(h);
371
383
  }
372
384
  console.log(), console.log(
@@ -380,40 +392,75 @@ class F {
380
392
  * @param input The parsed input, if possible.
381
393
  * @returns this
382
394
  */
383
- async handleErrors(e, t) {
384
- let r = this;
385
- for (; !r.$errorFn && r.$parent; )
386
- r = r.$parent;
387
- return r.$errorFn ? await r.$errorFn(r, e, t ?? {}) : this.defaultErrorScreen(e), this;
395
+ async handleErrors(t, e) {
396
+ let n = this;
397
+ for (; !n.$errorFn && n.$parent; )
398
+ n = n.$parent;
399
+ return n.$errorFn ? await n.$errorFn(n, t, e ?? {}) : this.defaultErrorScreen(t), this;
388
400
  }
389
401
  /**
390
402
  * Runs a command.
391
403
  * @param argv The arguments to run the command with. Defaults to your runtime's `argv` equivalent.
392
404
  * @returns this
393
405
  */
394
- async run(e) {
395
- e || (e = typeof Bun < "u" ? Bun.argv.slice(2) : typeof Deno < "u" ? Deno.args : process.argv.slice(2));
396
- const t = await this.parse(e);
397
- if (t.isHelp)
398
- return t.command.handleErrors([new P(t.command)]), this;
399
- if (t.isVersion)
406
+ async run(t) {
407
+ t || (t = typeof Bun < "u" ? Bun.argv.slice(2) : typeof Deno < "u" ? Deno.args : process.argv.slice(2));
408
+ const e = await this.parse(t);
409
+ if (e.isHelp)
410
+ return e.command.handleErrors([new L(e.command)]), this;
411
+ if (e.isVersion)
400
412
  return console.log(
401
- `${t.command.fullCommandPath()} version ${t.command.$version}`
413
+ `${e.command.fullCommandPath()} version ${e.command.$version}`
402
414
  ), this;
403
415
  try {
404
- t.errors.length > 0 ? await t.command.handleErrors(t.errors, t.input) : t.command.$fn ? await t.command.$fn(t.input) : await t.command.handleErrors(
405
- [new P(t.command), ...t.errors],
406
- t.input
407
- );
408
- } catch (r) {
409
- r instanceof Error || console.warn(
416
+ if (e.errors.length > 0)
417
+ await e.command.handleErrors(e.errors, e.input);
418
+ else if (!e.command.$fn)
419
+ await e.command.handleErrors(
420
+ [new L(e.command), ...e.errors],
421
+ e.input
422
+ );
423
+ else {
424
+ const n = oe(e.command);
425
+ n.length > 0 ? await ie(n)(e.input, async () => {
426
+ await e.command.$fn?.(e.input);
427
+ }) : await e.command.$fn(e.input);
428
+ }
429
+ } catch (n) {
430
+ n instanceof Error || console.warn(
410
431
  "[convoker] an error that is not instance of `Error` was thrown. this may cause undefined behavior."
411
- ), await t.command.handleErrors([r]);
432
+ ), await e.command.handleErrors([n]);
412
433
  }
413
434
  return this;
414
435
  }
415
436
  }
437
+ function oe(r) {
438
+ const t = [];
439
+ let e = r;
440
+ for (; e; )
441
+ e.$middlewares.length && t.unshift(...e.$middlewares), e = e.$parent;
442
+ return t;
443
+ }
444
+ function ie(r) {
445
+ return (t, e) => {
446
+ let n = -1;
447
+ const i = (a) => {
448
+ if (a <= n)
449
+ return Promise.reject(new Error("next() called multiple times"));
450
+ n = a;
451
+ const f = r[a];
452
+ if (!f)
453
+ return e ? e() : Promise.resolve();
454
+ try {
455
+ return Promise.resolve(f(t, () => i(a + 1)));
456
+ } catch (s) {
457
+ return Promise.reject(s);
458
+ }
459
+ };
460
+ return i(0);
461
+ };
462
+ }
416
463
  export {
417
- F as Command,
418
- ct as l
464
+ B as Command,
465
+ de as l
419
466
  };
package/dist/error.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Command action function.
3
3
  */
4
- declare type ActionFn<T extends Input> = (input: InferInput<T>) => void | Promise<void>;
4
+ declare type ActionFn<T extends Input> = (input: InferInput<T>) => any | Promise<any>;
5
5
 
6
6
  /**
7
7
  * A basic input type.
@@ -62,6 +62,10 @@ declare class Command<T extends Input = Input> {
62
62
  * The action function of this command.
63
63
  */
64
64
  $fn: ActionFn<T> | undefined;
65
+ /**
66
+ * The middlewares associated with this command.
67
+ */
68
+ $middlewares: MiddlewareFn<T>[];
65
69
  /**
66
70
  * The error handler of this command.
67
71
  */
@@ -97,6 +101,12 @@ declare class Command<T extends Input = Input> {
97
101
  * @returns this
98
102
  */
99
103
  input<TInput extends Input>(input: TInput): Command<TInput>;
104
+ /**
105
+ * Adds a chain of middlewares.
106
+ * @param fns The middlewares to use.
107
+ * @returns this
108
+ */
109
+ use(...fns: MiddlewareFn<T>[]): this;
100
110
  /**
101
111
  * Sets the action function for this command.
102
112
  * @param fn The action.
@@ -140,6 +150,11 @@ declare class Command<T extends Input = Input> {
140
150
  */
141
151
  parse(argv: string[]): Promise<ParseResult<T>>;
142
152
  private buildInputMap;
153
+ /**
154
+ * Allows surpassing the amount of arguments specified.
155
+ * @returns this
156
+ */
157
+ allowSurpassArgLimit(): this;
143
158
  /**
144
159
  * Gets the full command path (name including parents).
145
160
  * @returns The full command path.
@@ -283,6 +298,11 @@ declare type Kind = BasicKind | StandardSchemaV1<any, any>;
283
298
  */
284
299
  declare type Kind_2 = BasicKind_2 | StandardSchemaV1_2<any, any>;
285
300
 
301
+ /**
302
+ * Command middleware function.
303
+ */
304
+ declare type MiddlewareFn<T extends Input = Input> = (input: InferInput<T>, next: () => Promise<any>) => any | Promise<any>;
305
+
286
306
  export declare class MissingRequiredArgumentError extends ConvokerError {
287
307
  /**
288
308
  * The argument key.
package/dist/error.js CHANGED
@@ -1,4 +1,4 @@
1
- import { C as n, H as s, I as a, a as e, M as i, T as t, U as E } from "./chunks/error-CBR2veuf.js";
1
+ import { C as n, H as s, I as a, M as e, a as i, T as t, U as E } from "./chunks/error-CyKscMUD.js";
2
2
  export {
3
3
  n as ConvokerError,
4
4
  s as HelpAskedError,
package/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@ import { Theme as Theme_2 } from '../../../../../../../../../../src/color';
4
4
  /**
5
5
  * Command action function.
6
6
  */
7
- export declare type ActionFn<T extends Input> = (input: InferInput<T>) => void | Promise<void>;
7
+ export declare type ActionFn<T extends Input> = (input: InferInput<T>) => any | Promise<any>;
8
8
 
9
9
  /**
10
10
  * Creates a new positional argument.
@@ -190,6 +190,10 @@ export declare class Command<T extends Input = Input> {
190
190
  * The action function of this command.
191
191
  */
192
192
  $fn: ActionFn<T> | undefined;
193
+ /**
194
+ * The middlewares associated with this command.
195
+ */
196
+ $middlewares: MiddlewareFn<T>[];
193
197
  /**
194
198
  * The error handler of this command.
195
199
  */
@@ -225,6 +229,12 @@ export declare class Command<T extends Input = Input> {
225
229
  * @returns this
226
230
  */
227
231
  input<TInput extends Input>(input: TInput): Command<TInput>;
232
+ /**
233
+ * Adds a chain of middlewares.
234
+ * @param fns The middlewares to use.
235
+ * @returns this
236
+ */
237
+ use(...fns: MiddlewareFn<T>[]): this;
228
238
  /**
229
239
  * Sets the action function for this command.
230
240
  * @param fn The action.
@@ -268,6 +278,11 @@ export declare class Command<T extends Input = Input> {
268
278
  */
269
279
  parse(argv: string[]): Promise<ParseResult<T>>;
270
280
  private buildInputMap;
281
+ /**
282
+ * Allows surpassing the amount of arguments specified.
283
+ * @returns this
284
+ */
285
+ allowSurpassArgLimit(): this;
271
286
  /**
272
287
  * Gets the full command path (name including parents).
273
288
  * @returns The full command path.
@@ -577,6 +592,11 @@ declare const magenta: (input: any) => string;
577
592
 
578
593
  declare const magentaBright: (input: any) => string;
579
594
 
595
+ /**
596
+ * Command middleware function.
597
+ */
598
+ export declare type MiddlewareFn<T extends Input = Input> = (input: InferInput<T>, next: () => Promise<any>) => any | Promise<any>;
599
+
580
600
  declare class MissingRequiredArgumentError extends ConvokerError {
581
601
  /**
582
602
  * The argument key.
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import { Command as m, l as e } from "./command.js";
2
- import { e as a } from "./chunks/error-CBR2veuf.js";
3
- import { i as f } from "./chunks/input-BfYvlWdG.js";
2
+ import { e as a } from "./chunks/error-CyKscMUD.js";
3
+ import { i as f } from "./chunks/input-WNu16aNE.js";
4
4
  import { a as s } from "./chunks/color-CiruG_zQ.js";
5
- import { i } from "./chunks/index-D7JQKzRX.js";
5
+ import { i } from "./chunks/index-BluQjWvw.js";
6
6
  export {
7
7
  m as Command,
8
8
  s as color,
package/dist/input.js CHANGED
@@ -1,5 +1,5 @@
1
- import "./chunks/standard-schema-CFxVDMhv.js";
2
- import { O as t, P as i, a as n, c as p, o as r, p as e } from "./chunks/input-BfYvlWdG.js";
1
+ import "./chunks/standard-schema-BHKzvwIS.js";
2
+ import { O as t, P as i, a as n, c as p, o as r, p as e } from "./chunks/input-WNu16aNE.js";
3
3
  export {
4
4
  t as Option,
5
5
  i as Positional,
package/dist/prompt.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import "./chunks/color-CiruG_zQ.js";
2
- import "./chunks/standard-schema-CFxVDMhv.js";
3
- import "./chunks/error-CBR2veuf.js";
2
+ import "./chunks/standard-schema-BHKzvwIS.js";
3
+ import "./chunks/error-CyKscMUD.js";
4
4
  import "./chunks/utils-DdmSEjLc.js";
5
5
  import { d as m } from "./chunks/__vite-browser-external-DQYBmsno.js";
6
- import { c as i, e as c, m as d, p as f, b as l, a as x, s as h, t as w } from "./chunks/index-D7JQKzRX.js";
6
+ import { c as i, e as c, m as d, p as f, b as l, a as x, s as h, t as w } from "./chunks/index-BluQjWvw.js";
7
7
  export {
8
8
  i as confirm,
9
9
  c as editor,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "convoker",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "A simple, type safe CLI framework for TypeScript.",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",