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