calabasas 0.0.1

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.
Files changed (2) hide show
  1. package/dist/index.js +4133 -0
  2. package/package.json +51 -0
package/dist/index.js ADDED
@@ -0,0 +1,4133 @@
1
+ #!/usr/bin/env node
2
+ import { createRequire } from "node:module";
3
+ var __create = Object.create;
4
+ var __getProtoOf = Object.getPrototypeOf;
5
+ var __defProp = Object.defineProperty;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __toESM = (mod, isNodeMode, target) => {
9
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
10
+ const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
11
+ for (let key of __getOwnPropNames(mod))
12
+ if (!__hasOwnProp.call(to, key))
13
+ __defProp(to, key, {
14
+ get: () => mod[key],
15
+ enumerable: true
16
+ });
17
+ return to;
18
+ };
19
+ var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
20
+ var __export = (target, all) => {
21
+ for (var name in all)
22
+ __defProp(target, name, {
23
+ get: all[name],
24
+ enumerable: true,
25
+ configurable: true,
26
+ set: (newValue) => all[name] = () => newValue
27
+ });
28
+ };
29
+ var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
30
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
31
+
32
+ // ../../node_modules/.bun/commander@13.1.0/node_modules/commander/lib/error.js
33
+ var require_error = __commonJS((exports) => {
34
+ class CommanderError extends Error {
35
+ constructor(exitCode, code, message) {
36
+ super(message);
37
+ Error.captureStackTrace(this, this.constructor);
38
+ this.name = this.constructor.name;
39
+ this.code = code;
40
+ this.exitCode = exitCode;
41
+ this.nestedError = undefined;
42
+ }
43
+ }
44
+
45
+ class InvalidArgumentError extends CommanderError {
46
+ constructor(message) {
47
+ super(1, "commander.invalidArgument", message);
48
+ Error.captureStackTrace(this, this.constructor);
49
+ this.name = this.constructor.name;
50
+ }
51
+ }
52
+ exports.CommanderError = CommanderError;
53
+ exports.InvalidArgumentError = InvalidArgumentError;
54
+ });
55
+
56
+ // ../../node_modules/.bun/commander@13.1.0/node_modules/commander/lib/argument.js
57
+ var require_argument = __commonJS((exports) => {
58
+ var { InvalidArgumentError } = require_error();
59
+
60
+ class Argument {
61
+ constructor(name, description) {
62
+ this.description = description || "";
63
+ this.variadic = false;
64
+ this.parseArg = undefined;
65
+ this.defaultValue = undefined;
66
+ this.defaultValueDescription = undefined;
67
+ this.argChoices = undefined;
68
+ switch (name[0]) {
69
+ case "<":
70
+ this.required = true;
71
+ this._name = name.slice(1, -1);
72
+ break;
73
+ case "[":
74
+ this.required = false;
75
+ this._name = name.slice(1, -1);
76
+ break;
77
+ default:
78
+ this.required = true;
79
+ this._name = name;
80
+ break;
81
+ }
82
+ if (this._name.length > 3 && this._name.slice(-3) === "...") {
83
+ this.variadic = true;
84
+ this._name = this._name.slice(0, -3);
85
+ }
86
+ }
87
+ name() {
88
+ return this._name;
89
+ }
90
+ _concatValue(value, previous) {
91
+ if (previous === this.defaultValue || !Array.isArray(previous)) {
92
+ return [value];
93
+ }
94
+ return previous.concat(value);
95
+ }
96
+ default(value, description) {
97
+ this.defaultValue = value;
98
+ this.defaultValueDescription = description;
99
+ return this;
100
+ }
101
+ argParser(fn) {
102
+ this.parseArg = fn;
103
+ return this;
104
+ }
105
+ choices(values) {
106
+ this.argChoices = values.slice();
107
+ this.parseArg = (arg, previous) => {
108
+ if (!this.argChoices.includes(arg)) {
109
+ throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
110
+ }
111
+ if (this.variadic) {
112
+ return this._concatValue(arg, previous);
113
+ }
114
+ return arg;
115
+ };
116
+ return this;
117
+ }
118
+ argRequired() {
119
+ this.required = true;
120
+ return this;
121
+ }
122
+ argOptional() {
123
+ this.required = false;
124
+ return this;
125
+ }
126
+ }
127
+ function humanReadableArgName(arg) {
128
+ const nameOutput = arg.name() + (arg.variadic === true ? "..." : "");
129
+ return arg.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
130
+ }
131
+ exports.Argument = Argument;
132
+ exports.humanReadableArgName = humanReadableArgName;
133
+ });
134
+
135
+ // ../../node_modules/.bun/commander@13.1.0/node_modules/commander/lib/help.js
136
+ var require_help = __commonJS((exports) => {
137
+ var { humanReadableArgName } = require_argument();
138
+
139
+ class Help {
140
+ constructor() {
141
+ this.helpWidth = undefined;
142
+ this.minWidthToWrap = 40;
143
+ this.sortSubcommands = false;
144
+ this.sortOptions = false;
145
+ this.showGlobalOptions = false;
146
+ }
147
+ prepareContext(contextOptions) {
148
+ this.helpWidth = this.helpWidth ?? contextOptions.helpWidth ?? 80;
149
+ }
150
+ visibleCommands(cmd) {
151
+ const visibleCommands = cmd.commands.filter((cmd2) => !cmd2._hidden);
152
+ const helpCommand = cmd._getHelpCommand();
153
+ if (helpCommand && !helpCommand._hidden) {
154
+ visibleCommands.push(helpCommand);
155
+ }
156
+ if (this.sortSubcommands) {
157
+ visibleCommands.sort((a, b) => {
158
+ return a.name().localeCompare(b.name());
159
+ });
160
+ }
161
+ return visibleCommands;
162
+ }
163
+ compareOptions(a, b) {
164
+ const getSortKey = (option) => {
165
+ return option.short ? option.short.replace(/^-/, "") : option.long.replace(/^--/, "");
166
+ };
167
+ return getSortKey(a).localeCompare(getSortKey(b));
168
+ }
169
+ visibleOptions(cmd) {
170
+ const visibleOptions = cmd.options.filter((option) => !option.hidden);
171
+ const helpOption = cmd._getHelpOption();
172
+ if (helpOption && !helpOption.hidden) {
173
+ const removeShort = helpOption.short && cmd._findOption(helpOption.short);
174
+ const removeLong = helpOption.long && cmd._findOption(helpOption.long);
175
+ if (!removeShort && !removeLong) {
176
+ visibleOptions.push(helpOption);
177
+ } else if (helpOption.long && !removeLong) {
178
+ visibleOptions.push(cmd.createOption(helpOption.long, helpOption.description));
179
+ } else if (helpOption.short && !removeShort) {
180
+ visibleOptions.push(cmd.createOption(helpOption.short, helpOption.description));
181
+ }
182
+ }
183
+ if (this.sortOptions) {
184
+ visibleOptions.sort(this.compareOptions);
185
+ }
186
+ return visibleOptions;
187
+ }
188
+ visibleGlobalOptions(cmd) {
189
+ if (!this.showGlobalOptions)
190
+ return [];
191
+ const globalOptions = [];
192
+ for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
193
+ const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
194
+ globalOptions.push(...visibleOptions);
195
+ }
196
+ if (this.sortOptions) {
197
+ globalOptions.sort(this.compareOptions);
198
+ }
199
+ return globalOptions;
200
+ }
201
+ visibleArguments(cmd) {
202
+ if (cmd._argsDescription) {
203
+ cmd.registeredArguments.forEach((argument) => {
204
+ argument.description = argument.description || cmd._argsDescription[argument.name()] || "";
205
+ });
206
+ }
207
+ if (cmd.registeredArguments.find((argument) => argument.description)) {
208
+ return cmd.registeredArguments;
209
+ }
210
+ return [];
211
+ }
212
+ subcommandTerm(cmd) {
213
+ const args = cmd.registeredArguments.map((arg) => humanReadableArgName(arg)).join(" ");
214
+ return cmd._name + (cmd._aliases[0] ? "|" + cmd._aliases[0] : "") + (cmd.options.length ? " [options]" : "") + (args ? " " + args : "");
215
+ }
216
+ optionTerm(option) {
217
+ return option.flags;
218
+ }
219
+ argumentTerm(argument) {
220
+ return argument.name();
221
+ }
222
+ longestSubcommandTermLength(cmd, helper) {
223
+ return helper.visibleCommands(cmd).reduce((max, command) => {
224
+ return Math.max(max, this.displayWidth(helper.styleSubcommandTerm(helper.subcommandTerm(command))));
225
+ }, 0);
226
+ }
227
+ longestOptionTermLength(cmd, helper) {
228
+ return helper.visibleOptions(cmd).reduce((max, option) => {
229
+ return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
230
+ }, 0);
231
+ }
232
+ longestGlobalOptionTermLength(cmd, helper) {
233
+ return helper.visibleGlobalOptions(cmd).reduce((max, option) => {
234
+ return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
235
+ }, 0);
236
+ }
237
+ longestArgumentTermLength(cmd, helper) {
238
+ return helper.visibleArguments(cmd).reduce((max, argument) => {
239
+ return Math.max(max, this.displayWidth(helper.styleArgumentTerm(helper.argumentTerm(argument))));
240
+ }, 0);
241
+ }
242
+ commandUsage(cmd) {
243
+ let cmdName = cmd._name;
244
+ if (cmd._aliases[0]) {
245
+ cmdName = cmdName + "|" + cmd._aliases[0];
246
+ }
247
+ let ancestorCmdNames = "";
248
+ for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
249
+ ancestorCmdNames = ancestorCmd.name() + " " + ancestorCmdNames;
250
+ }
251
+ return ancestorCmdNames + cmdName + " " + cmd.usage();
252
+ }
253
+ commandDescription(cmd) {
254
+ return cmd.description();
255
+ }
256
+ subcommandDescription(cmd) {
257
+ return cmd.summary() || cmd.description();
258
+ }
259
+ optionDescription(option) {
260
+ const extraInfo = [];
261
+ if (option.argChoices) {
262
+ extraInfo.push(`choices: ${option.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
263
+ }
264
+ if (option.defaultValue !== undefined) {
265
+ const showDefault = option.required || option.optional || option.isBoolean() && typeof option.defaultValue === "boolean";
266
+ if (showDefault) {
267
+ extraInfo.push(`default: ${option.defaultValueDescription || JSON.stringify(option.defaultValue)}`);
268
+ }
269
+ }
270
+ if (option.presetArg !== undefined && option.optional) {
271
+ extraInfo.push(`preset: ${JSON.stringify(option.presetArg)}`);
272
+ }
273
+ if (option.envVar !== undefined) {
274
+ extraInfo.push(`env: ${option.envVar}`);
275
+ }
276
+ if (extraInfo.length > 0) {
277
+ return `${option.description} (${extraInfo.join(", ")})`;
278
+ }
279
+ return option.description;
280
+ }
281
+ argumentDescription(argument) {
282
+ const extraInfo = [];
283
+ if (argument.argChoices) {
284
+ extraInfo.push(`choices: ${argument.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
285
+ }
286
+ if (argument.defaultValue !== undefined) {
287
+ extraInfo.push(`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`);
288
+ }
289
+ if (extraInfo.length > 0) {
290
+ const extraDescription = `(${extraInfo.join(", ")})`;
291
+ if (argument.description) {
292
+ return `${argument.description} ${extraDescription}`;
293
+ }
294
+ return extraDescription;
295
+ }
296
+ return argument.description;
297
+ }
298
+ formatHelp(cmd, helper) {
299
+ const termWidth = helper.padWidth(cmd, helper);
300
+ const helpWidth = helper.helpWidth ?? 80;
301
+ function callFormatItem(term, description) {
302
+ return helper.formatItem(term, termWidth, description, helper);
303
+ }
304
+ let output = [
305
+ `${helper.styleTitle("Usage:")} ${helper.styleUsage(helper.commandUsage(cmd))}`,
306
+ ""
307
+ ];
308
+ const commandDescription = helper.commandDescription(cmd);
309
+ if (commandDescription.length > 0) {
310
+ output = output.concat([
311
+ helper.boxWrap(helper.styleCommandDescription(commandDescription), helpWidth),
312
+ ""
313
+ ]);
314
+ }
315
+ const argumentList = helper.visibleArguments(cmd).map((argument) => {
316
+ return callFormatItem(helper.styleArgumentTerm(helper.argumentTerm(argument)), helper.styleArgumentDescription(helper.argumentDescription(argument)));
317
+ });
318
+ if (argumentList.length > 0) {
319
+ output = output.concat([
320
+ helper.styleTitle("Arguments:"),
321
+ ...argumentList,
322
+ ""
323
+ ]);
324
+ }
325
+ const optionList = helper.visibleOptions(cmd).map((option) => {
326
+ return callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option)));
327
+ });
328
+ if (optionList.length > 0) {
329
+ output = output.concat([
330
+ helper.styleTitle("Options:"),
331
+ ...optionList,
332
+ ""
333
+ ]);
334
+ }
335
+ if (helper.showGlobalOptions) {
336
+ const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
337
+ return callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option)));
338
+ });
339
+ if (globalOptionList.length > 0) {
340
+ output = output.concat([
341
+ helper.styleTitle("Global Options:"),
342
+ ...globalOptionList,
343
+ ""
344
+ ]);
345
+ }
346
+ }
347
+ const commandList = helper.visibleCommands(cmd).map((cmd2) => {
348
+ return callFormatItem(helper.styleSubcommandTerm(helper.subcommandTerm(cmd2)), helper.styleSubcommandDescription(helper.subcommandDescription(cmd2)));
349
+ });
350
+ if (commandList.length > 0) {
351
+ output = output.concat([
352
+ helper.styleTitle("Commands:"),
353
+ ...commandList,
354
+ ""
355
+ ]);
356
+ }
357
+ return output.join(`
358
+ `);
359
+ }
360
+ displayWidth(str) {
361
+ return stripColor(str).length;
362
+ }
363
+ styleTitle(str) {
364
+ return str;
365
+ }
366
+ styleUsage(str) {
367
+ return str.split(" ").map((word) => {
368
+ if (word === "[options]")
369
+ return this.styleOptionText(word);
370
+ if (word === "[command]")
371
+ return this.styleSubcommandText(word);
372
+ if (word[0] === "[" || word[0] === "<")
373
+ return this.styleArgumentText(word);
374
+ return this.styleCommandText(word);
375
+ }).join(" ");
376
+ }
377
+ styleCommandDescription(str) {
378
+ return this.styleDescriptionText(str);
379
+ }
380
+ styleOptionDescription(str) {
381
+ return this.styleDescriptionText(str);
382
+ }
383
+ styleSubcommandDescription(str) {
384
+ return this.styleDescriptionText(str);
385
+ }
386
+ styleArgumentDescription(str) {
387
+ return this.styleDescriptionText(str);
388
+ }
389
+ styleDescriptionText(str) {
390
+ return str;
391
+ }
392
+ styleOptionTerm(str) {
393
+ return this.styleOptionText(str);
394
+ }
395
+ styleSubcommandTerm(str) {
396
+ return str.split(" ").map((word) => {
397
+ if (word === "[options]")
398
+ return this.styleOptionText(word);
399
+ if (word[0] === "[" || word[0] === "<")
400
+ return this.styleArgumentText(word);
401
+ return this.styleSubcommandText(word);
402
+ }).join(" ");
403
+ }
404
+ styleArgumentTerm(str) {
405
+ return this.styleArgumentText(str);
406
+ }
407
+ styleOptionText(str) {
408
+ return str;
409
+ }
410
+ styleArgumentText(str) {
411
+ return str;
412
+ }
413
+ styleSubcommandText(str) {
414
+ return str;
415
+ }
416
+ styleCommandText(str) {
417
+ return str;
418
+ }
419
+ padWidth(cmd, helper) {
420
+ return Math.max(helper.longestOptionTermLength(cmd, helper), helper.longestGlobalOptionTermLength(cmd, helper), helper.longestSubcommandTermLength(cmd, helper), helper.longestArgumentTermLength(cmd, helper));
421
+ }
422
+ preformatted(str) {
423
+ return /\n[^\S\r\n]/.test(str);
424
+ }
425
+ formatItem(term, termWidth, description, helper) {
426
+ const itemIndent = 2;
427
+ const itemIndentStr = " ".repeat(itemIndent);
428
+ if (!description)
429
+ return itemIndentStr + term;
430
+ const paddedTerm = term.padEnd(termWidth + term.length - helper.displayWidth(term));
431
+ const spacerWidth = 2;
432
+ const helpWidth = this.helpWidth ?? 80;
433
+ const remainingWidth = helpWidth - termWidth - spacerWidth - itemIndent;
434
+ let formattedDescription;
435
+ if (remainingWidth < this.minWidthToWrap || helper.preformatted(description)) {
436
+ formattedDescription = description;
437
+ } else {
438
+ const wrappedDescription = helper.boxWrap(description, remainingWidth);
439
+ formattedDescription = wrappedDescription.replace(/\n/g, `
440
+ ` + " ".repeat(termWidth + spacerWidth));
441
+ }
442
+ return itemIndentStr + paddedTerm + " ".repeat(spacerWidth) + formattedDescription.replace(/\n/g, `
443
+ ${itemIndentStr}`);
444
+ }
445
+ boxWrap(str, width) {
446
+ if (width < this.minWidthToWrap)
447
+ return str;
448
+ const rawLines = str.split(/\r\n|\n/);
449
+ const chunkPattern = /[\s]*[^\s]+/g;
450
+ const wrappedLines = [];
451
+ rawLines.forEach((line) => {
452
+ const chunks = line.match(chunkPattern);
453
+ if (chunks === null) {
454
+ wrappedLines.push("");
455
+ return;
456
+ }
457
+ let sumChunks = [chunks.shift()];
458
+ let sumWidth = this.displayWidth(sumChunks[0]);
459
+ chunks.forEach((chunk) => {
460
+ const visibleWidth = this.displayWidth(chunk);
461
+ if (sumWidth + visibleWidth <= width) {
462
+ sumChunks.push(chunk);
463
+ sumWidth += visibleWidth;
464
+ return;
465
+ }
466
+ wrappedLines.push(sumChunks.join(""));
467
+ const nextChunk = chunk.trimStart();
468
+ sumChunks = [nextChunk];
469
+ sumWidth = this.displayWidth(nextChunk);
470
+ });
471
+ wrappedLines.push(sumChunks.join(""));
472
+ });
473
+ return wrappedLines.join(`
474
+ `);
475
+ }
476
+ }
477
+ function stripColor(str) {
478
+ const sgrPattern = /\x1b\[\d*(;\d*)*m/g;
479
+ return str.replace(sgrPattern, "");
480
+ }
481
+ exports.Help = Help;
482
+ exports.stripColor = stripColor;
483
+ });
484
+
485
+ // ../../node_modules/.bun/commander@13.1.0/node_modules/commander/lib/option.js
486
+ var require_option = __commonJS((exports) => {
487
+ var { InvalidArgumentError } = require_error();
488
+
489
+ class Option {
490
+ constructor(flags, description) {
491
+ this.flags = flags;
492
+ this.description = description || "";
493
+ this.required = flags.includes("<");
494
+ this.optional = flags.includes("[");
495
+ this.variadic = /\w\.\.\.[>\]]$/.test(flags);
496
+ this.mandatory = false;
497
+ const optionFlags = splitOptionFlags(flags);
498
+ this.short = optionFlags.shortFlag;
499
+ this.long = optionFlags.longFlag;
500
+ this.negate = false;
501
+ if (this.long) {
502
+ this.negate = this.long.startsWith("--no-");
503
+ }
504
+ this.defaultValue = undefined;
505
+ this.defaultValueDescription = undefined;
506
+ this.presetArg = undefined;
507
+ this.envVar = undefined;
508
+ this.parseArg = undefined;
509
+ this.hidden = false;
510
+ this.argChoices = undefined;
511
+ this.conflictsWith = [];
512
+ this.implied = undefined;
513
+ }
514
+ default(value, description) {
515
+ this.defaultValue = value;
516
+ this.defaultValueDescription = description;
517
+ return this;
518
+ }
519
+ preset(arg) {
520
+ this.presetArg = arg;
521
+ return this;
522
+ }
523
+ conflicts(names) {
524
+ this.conflictsWith = this.conflictsWith.concat(names);
525
+ return this;
526
+ }
527
+ implies(impliedOptionValues) {
528
+ let newImplied = impliedOptionValues;
529
+ if (typeof impliedOptionValues === "string") {
530
+ newImplied = { [impliedOptionValues]: true };
531
+ }
532
+ this.implied = Object.assign(this.implied || {}, newImplied);
533
+ return this;
534
+ }
535
+ env(name) {
536
+ this.envVar = name;
537
+ return this;
538
+ }
539
+ argParser(fn) {
540
+ this.parseArg = fn;
541
+ return this;
542
+ }
543
+ makeOptionMandatory(mandatory = true) {
544
+ this.mandatory = !!mandatory;
545
+ return this;
546
+ }
547
+ hideHelp(hide = true) {
548
+ this.hidden = !!hide;
549
+ return this;
550
+ }
551
+ _concatValue(value, previous) {
552
+ if (previous === this.defaultValue || !Array.isArray(previous)) {
553
+ return [value];
554
+ }
555
+ return previous.concat(value);
556
+ }
557
+ choices(values) {
558
+ this.argChoices = values.slice();
559
+ this.parseArg = (arg, previous) => {
560
+ if (!this.argChoices.includes(arg)) {
561
+ throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
562
+ }
563
+ if (this.variadic) {
564
+ return this._concatValue(arg, previous);
565
+ }
566
+ return arg;
567
+ };
568
+ return this;
569
+ }
570
+ name() {
571
+ if (this.long) {
572
+ return this.long.replace(/^--/, "");
573
+ }
574
+ return this.short.replace(/^-/, "");
575
+ }
576
+ attributeName() {
577
+ if (this.negate) {
578
+ return camelcase(this.name().replace(/^no-/, ""));
579
+ }
580
+ return camelcase(this.name());
581
+ }
582
+ is(arg) {
583
+ return this.short === arg || this.long === arg;
584
+ }
585
+ isBoolean() {
586
+ return !this.required && !this.optional && !this.negate;
587
+ }
588
+ }
589
+
590
+ class DualOptions {
591
+ constructor(options) {
592
+ this.positiveOptions = new Map;
593
+ this.negativeOptions = new Map;
594
+ this.dualOptions = new Set;
595
+ options.forEach((option) => {
596
+ if (option.negate) {
597
+ this.negativeOptions.set(option.attributeName(), option);
598
+ } else {
599
+ this.positiveOptions.set(option.attributeName(), option);
600
+ }
601
+ });
602
+ this.negativeOptions.forEach((value, key) => {
603
+ if (this.positiveOptions.has(key)) {
604
+ this.dualOptions.add(key);
605
+ }
606
+ });
607
+ }
608
+ valueFromOption(value, option) {
609
+ const optionKey = option.attributeName();
610
+ if (!this.dualOptions.has(optionKey))
611
+ return true;
612
+ const preset = this.negativeOptions.get(optionKey).presetArg;
613
+ const negativeValue = preset !== undefined ? preset : false;
614
+ return option.negate === (negativeValue === value);
615
+ }
616
+ }
617
+ function camelcase(str) {
618
+ return str.split("-").reduce((str2, word) => {
619
+ return str2 + word[0].toUpperCase() + word.slice(1);
620
+ });
621
+ }
622
+ function splitOptionFlags(flags) {
623
+ let shortFlag;
624
+ let longFlag;
625
+ const shortFlagExp = /^-[^-]$/;
626
+ const longFlagExp = /^--[^-]/;
627
+ const flagParts = flags.split(/[ |,]+/).concat("guard");
628
+ if (shortFlagExp.test(flagParts[0]))
629
+ shortFlag = flagParts.shift();
630
+ if (longFlagExp.test(flagParts[0]))
631
+ longFlag = flagParts.shift();
632
+ if (!shortFlag && shortFlagExp.test(flagParts[0]))
633
+ shortFlag = flagParts.shift();
634
+ if (!shortFlag && longFlagExp.test(flagParts[0])) {
635
+ shortFlag = longFlag;
636
+ longFlag = flagParts.shift();
637
+ }
638
+ if (flagParts[0].startsWith("-")) {
639
+ const unsupportedFlag = flagParts[0];
640
+ const baseError = `option creation failed due to '${unsupportedFlag}' in option flags '${flags}'`;
641
+ if (/^-[^-][^-]/.test(unsupportedFlag))
642
+ throw new Error(`${baseError}
643
+ - a short flag is a single dash and a single character
644
+ - either use a single dash and a single character (for a short flag)
645
+ - or use a double dash for a long option (and can have two, like '--ws, --workspace')`);
646
+ if (shortFlagExp.test(unsupportedFlag))
647
+ throw new Error(`${baseError}
648
+ - too many short flags`);
649
+ if (longFlagExp.test(unsupportedFlag))
650
+ throw new Error(`${baseError}
651
+ - too many long flags`);
652
+ throw new Error(`${baseError}
653
+ - unrecognised flag format`);
654
+ }
655
+ if (shortFlag === undefined && longFlag === undefined)
656
+ throw new Error(`option creation failed due to no flags found in '${flags}'.`);
657
+ return { shortFlag, longFlag };
658
+ }
659
+ exports.Option = Option;
660
+ exports.DualOptions = DualOptions;
661
+ });
662
+
663
+ // ../../node_modules/.bun/commander@13.1.0/node_modules/commander/lib/suggestSimilar.js
664
+ var require_suggestSimilar = __commonJS((exports) => {
665
+ var maxDistance = 3;
666
+ function editDistance(a, b) {
667
+ if (Math.abs(a.length - b.length) > maxDistance)
668
+ return Math.max(a.length, b.length);
669
+ const d = [];
670
+ for (let i = 0;i <= a.length; i++) {
671
+ d[i] = [i];
672
+ }
673
+ for (let j = 0;j <= b.length; j++) {
674
+ d[0][j] = j;
675
+ }
676
+ for (let j = 1;j <= b.length; j++) {
677
+ for (let i = 1;i <= a.length; i++) {
678
+ let cost = 1;
679
+ if (a[i - 1] === b[j - 1]) {
680
+ cost = 0;
681
+ } else {
682
+ cost = 1;
683
+ }
684
+ d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
685
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
686
+ d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
687
+ }
688
+ }
689
+ }
690
+ return d[a.length][b.length];
691
+ }
692
+ function suggestSimilar(word, candidates) {
693
+ if (!candidates || candidates.length === 0)
694
+ return "";
695
+ candidates = Array.from(new Set(candidates));
696
+ const searchingOptions = word.startsWith("--");
697
+ if (searchingOptions) {
698
+ word = word.slice(2);
699
+ candidates = candidates.map((candidate) => candidate.slice(2));
700
+ }
701
+ let similar = [];
702
+ let bestDistance = maxDistance;
703
+ const minSimilarity = 0.4;
704
+ candidates.forEach((candidate) => {
705
+ if (candidate.length <= 1)
706
+ return;
707
+ const distance = editDistance(word, candidate);
708
+ const length = Math.max(word.length, candidate.length);
709
+ const similarity = (length - distance) / length;
710
+ if (similarity > minSimilarity) {
711
+ if (distance < bestDistance) {
712
+ bestDistance = distance;
713
+ similar = [candidate];
714
+ } else if (distance === bestDistance) {
715
+ similar.push(candidate);
716
+ }
717
+ }
718
+ });
719
+ similar.sort((a, b) => a.localeCompare(b));
720
+ if (searchingOptions) {
721
+ similar = similar.map((candidate) => `--${candidate}`);
722
+ }
723
+ if (similar.length > 1) {
724
+ return `
725
+ (Did you mean one of ${similar.join(", ")}?)`;
726
+ }
727
+ if (similar.length === 1) {
728
+ return `
729
+ (Did you mean ${similar[0]}?)`;
730
+ }
731
+ return "";
732
+ }
733
+ exports.suggestSimilar = suggestSimilar;
734
+ });
735
+
736
+ // ../../node_modules/.bun/commander@13.1.0/node_modules/commander/lib/command.js
737
+ var require_command = __commonJS((exports) => {
738
+ var EventEmitter = __require("node:events").EventEmitter;
739
+ var childProcess = __require("node:child_process");
740
+ var path = __require("node:path");
741
+ var fs = __require("node:fs");
742
+ var process2 = __require("node:process");
743
+ var { Argument, humanReadableArgName } = require_argument();
744
+ var { CommanderError } = require_error();
745
+ var { Help, stripColor } = require_help();
746
+ var { Option, DualOptions } = require_option();
747
+ var { suggestSimilar } = require_suggestSimilar();
748
+
749
+ class Command extends EventEmitter {
750
+ constructor(name) {
751
+ super();
752
+ this.commands = [];
753
+ this.options = [];
754
+ this.parent = null;
755
+ this._allowUnknownOption = false;
756
+ this._allowExcessArguments = false;
757
+ this.registeredArguments = [];
758
+ this._args = this.registeredArguments;
759
+ this.args = [];
760
+ this.rawArgs = [];
761
+ this.processedArgs = [];
762
+ this._scriptPath = null;
763
+ this._name = name || "";
764
+ this._optionValues = {};
765
+ this._optionValueSources = {};
766
+ this._storeOptionsAsProperties = false;
767
+ this._actionHandler = null;
768
+ this._executableHandler = false;
769
+ this._executableFile = null;
770
+ this._executableDir = null;
771
+ this._defaultCommandName = null;
772
+ this._exitCallback = null;
773
+ this._aliases = [];
774
+ this._combineFlagAndOptionalValue = true;
775
+ this._description = "";
776
+ this._summary = "";
777
+ this._argsDescription = undefined;
778
+ this._enablePositionalOptions = false;
779
+ this._passThroughOptions = false;
780
+ this._lifeCycleHooks = {};
781
+ this._showHelpAfterError = false;
782
+ this._showSuggestionAfterError = true;
783
+ this._savedState = null;
784
+ this._outputConfiguration = {
785
+ writeOut: (str) => process2.stdout.write(str),
786
+ writeErr: (str) => process2.stderr.write(str),
787
+ outputError: (str, write) => write(str),
788
+ getOutHelpWidth: () => process2.stdout.isTTY ? process2.stdout.columns : undefined,
789
+ getErrHelpWidth: () => process2.stderr.isTTY ? process2.stderr.columns : undefined,
790
+ getOutHasColors: () => useColor() ?? (process2.stdout.isTTY && process2.stdout.hasColors?.()),
791
+ getErrHasColors: () => useColor() ?? (process2.stderr.isTTY && process2.stderr.hasColors?.()),
792
+ stripColor: (str) => stripColor(str)
793
+ };
794
+ this._hidden = false;
795
+ this._helpOption = undefined;
796
+ this._addImplicitHelpCommand = undefined;
797
+ this._helpCommand = undefined;
798
+ this._helpConfiguration = {};
799
+ }
800
+ copyInheritedSettings(sourceCommand) {
801
+ this._outputConfiguration = sourceCommand._outputConfiguration;
802
+ this._helpOption = sourceCommand._helpOption;
803
+ this._helpCommand = sourceCommand._helpCommand;
804
+ this._helpConfiguration = sourceCommand._helpConfiguration;
805
+ this._exitCallback = sourceCommand._exitCallback;
806
+ this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
807
+ this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
808
+ this._allowExcessArguments = sourceCommand._allowExcessArguments;
809
+ this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
810
+ this._showHelpAfterError = sourceCommand._showHelpAfterError;
811
+ this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;
812
+ return this;
813
+ }
814
+ _getCommandAndAncestors() {
815
+ const result = [];
816
+ for (let command = this;command; command = command.parent) {
817
+ result.push(command);
818
+ }
819
+ return result;
820
+ }
821
+ command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
822
+ let desc = actionOptsOrExecDesc;
823
+ let opts = execOpts;
824
+ if (typeof desc === "object" && desc !== null) {
825
+ opts = desc;
826
+ desc = null;
827
+ }
828
+ opts = opts || {};
829
+ const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
830
+ const cmd = this.createCommand(name);
831
+ if (desc) {
832
+ cmd.description(desc);
833
+ cmd._executableHandler = true;
834
+ }
835
+ if (opts.isDefault)
836
+ this._defaultCommandName = cmd._name;
837
+ cmd._hidden = !!(opts.noHelp || opts.hidden);
838
+ cmd._executableFile = opts.executableFile || null;
839
+ if (args)
840
+ cmd.arguments(args);
841
+ this._registerCommand(cmd);
842
+ cmd.parent = this;
843
+ cmd.copyInheritedSettings(this);
844
+ if (desc)
845
+ return this;
846
+ return cmd;
847
+ }
848
+ createCommand(name) {
849
+ return new Command(name);
850
+ }
851
+ createHelp() {
852
+ return Object.assign(new Help, this.configureHelp());
853
+ }
854
+ configureHelp(configuration) {
855
+ if (configuration === undefined)
856
+ return this._helpConfiguration;
857
+ this._helpConfiguration = configuration;
858
+ return this;
859
+ }
860
+ configureOutput(configuration) {
861
+ if (configuration === undefined)
862
+ return this._outputConfiguration;
863
+ Object.assign(this._outputConfiguration, configuration);
864
+ return this;
865
+ }
866
+ showHelpAfterError(displayHelp = true) {
867
+ if (typeof displayHelp !== "string")
868
+ displayHelp = !!displayHelp;
869
+ this._showHelpAfterError = displayHelp;
870
+ return this;
871
+ }
872
+ showSuggestionAfterError(displaySuggestion = true) {
873
+ this._showSuggestionAfterError = !!displaySuggestion;
874
+ return this;
875
+ }
876
+ addCommand(cmd, opts) {
877
+ if (!cmd._name) {
878
+ throw new Error(`Command passed to .addCommand() must have a name
879
+ - specify the name in Command constructor or using .name()`);
880
+ }
881
+ opts = opts || {};
882
+ if (opts.isDefault)
883
+ this._defaultCommandName = cmd._name;
884
+ if (opts.noHelp || opts.hidden)
885
+ cmd._hidden = true;
886
+ this._registerCommand(cmd);
887
+ cmd.parent = this;
888
+ cmd._checkForBrokenPassThrough();
889
+ return this;
890
+ }
891
+ createArgument(name, description) {
892
+ return new Argument(name, description);
893
+ }
894
+ argument(name, description, fn, defaultValue) {
895
+ const argument = this.createArgument(name, description);
896
+ if (typeof fn === "function") {
897
+ argument.default(defaultValue).argParser(fn);
898
+ } else {
899
+ argument.default(fn);
900
+ }
901
+ this.addArgument(argument);
902
+ return this;
903
+ }
904
+ arguments(names) {
905
+ names.trim().split(/ +/).forEach((detail) => {
906
+ this.argument(detail);
907
+ });
908
+ return this;
909
+ }
910
+ addArgument(argument) {
911
+ const previousArgument = this.registeredArguments.slice(-1)[0];
912
+ if (previousArgument && previousArgument.variadic) {
913
+ throw new Error(`only the last argument can be variadic '${previousArgument.name()}'`);
914
+ }
915
+ if (argument.required && argument.defaultValue !== undefined && argument.parseArg === undefined) {
916
+ throw new Error(`a default value for a required argument is never used: '${argument.name()}'`);
917
+ }
918
+ this.registeredArguments.push(argument);
919
+ return this;
920
+ }
921
+ helpCommand(enableOrNameAndArgs, description) {
922
+ if (typeof enableOrNameAndArgs === "boolean") {
923
+ this._addImplicitHelpCommand = enableOrNameAndArgs;
924
+ return this;
925
+ }
926
+ enableOrNameAndArgs = enableOrNameAndArgs ?? "help [command]";
927
+ const [, helpName, helpArgs] = enableOrNameAndArgs.match(/([^ ]+) *(.*)/);
928
+ const helpDescription = description ?? "display help for command";
929
+ const helpCommand = this.createCommand(helpName);
930
+ helpCommand.helpOption(false);
931
+ if (helpArgs)
932
+ helpCommand.arguments(helpArgs);
933
+ if (helpDescription)
934
+ helpCommand.description(helpDescription);
935
+ this._addImplicitHelpCommand = true;
936
+ this._helpCommand = helpCommand;
937
+ return this;
938
+ }
939
+ addHelpCommand(helpCommand, deprecatedDescription) {
940
+ if (typeof helpCommand !== "object") {
941
+ this.helpCommand(helpCommand, deprecatedDescription);
942
+ return this;
943
+ }
944
+ this._addImplicitHelpCommand = true;
945
+ this._helpCommand = helpCommand;
946
+ return this;
947
+ }
948
+ _getHelpCommand() {
949
+ const hasImplicitHelpCommand = this._addImplicitHelpCommand ?? (this.commands.length && !this._actionHandler && !this._findCommand("help"));
950
+ if (hasImplicitHelpCommand) {
951
+ if (this._helpCommand === undefined) {
952
+ this.helpCommand(undefined, undefined);
953
+ }
954
+ return this._helpCommand;
955
+ }
956
+ return null;
957
+ }
958
+ hook(event, listener) {
959
+ const allowedValues = ["preSubcommand", "preAction", "postAction"];
960
+ if (!allowedValues.includes(event)) {
961
+ throw new Error(`Unexpected value for event passed to hook : '${event}'.
962
+ Expecting one of '${allowedValues.join("', '")}'`);
963
+ }
964
+ if (this._lifeCycleHooks[event]) {
965
+ this._lifeCycleHooks[event].push(listener);
966
+ } else {
967
+ this._lifeCycleHooks[event] = [listener];
968
+ }
969
+ return this;
970
+ }
971
+ exitOverride(fn) {
972
+ if (fn) {
973
+ this._exitCallback = fn;
974
+ } else {
975
+ this._exitCallback = (err) => {
976
+ if (err.code !== "commander.executeSubCommandAsync") {
977
+ throw err;
978
+ } else {}
979
+ };
980
+ }
981
+ return this;
982
+ }
983
+ _exit(exitCode, code, message) {
984
+ if (this._exitCallback) {
985
+ this._exitCallback(new CommanderError(exitCode, code, message));
986
+ }
987
+ process2.exit(exitCode);
988
+ }
989
+ action(fn) {
990
+ const listener = (args) => {
991
+ const expectedArgsCount = this.registeredArguments.length;
992
+ const actionArgs = args.slice(0, expectedArgsCount);
993
+ if (this._storeOptionsAsProperties) {
994
+ actionArgs[expectedArgsCount] = this;
995
+ } else {
996
+ actionArgs[expectedArgsCount] = this.opts();
997
+ }
998
+ actionArgs.push(this);
999
+ return fn.apply(this, actionArgs);
1000
+ };
1001
+ this._actionHandler = listener;
1002
+ return this;
1003
+ }
1004
+ createOption(flags, description) {
1005
+ return new Option(flags, description);
1006
+ }
1007
+ _callParseArg(target, value, previous, invalidArgumentMessage) {
1008
+ try {
1009
+ return target.parseArg(value, previous);
1010
+ } catch (err) {
1011
+ if (err.code === "commander.invalidArgument") {
1012
+ const message = `${invalidArgumentMessage} ${err.message}`;
1013
+ this.error(message, { exitCode: err.exitCode, code: err.code });
1014
+ }
1015
+ throw err;
1016
+ }
1017
+ }
1018
+ _registerOption(option) {
1019
+ const matchingOption = option.short && this._findOption(option.short) || option.long && this._findOption(option.long);
1020
+ if (matchingOption) {
1021
+ const matchingFlag = option.long && this._findOption(option.long) ? option.long : option.short;
1022
+ throw new Error(`Cannot add option '${option.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'
1023
+ - already used by option '${matchingOption.flags}'`);
1024
+ }
1025
+ this.options.push(option);
1026
+ }
1027
+ _registerCommand(command) {
1028
+ const knownBy = (cmd) => {
1029
+ return [cmd.name()].concat(cmd.aliases());
1030
+ };
1031
+ const alreadyUsed = knownBy(command).find((name) => this._findCommand(name));
1032
+ if (alreadyUsed) {
1033
+ const existingCmd = knownBy(this._findCommand(alreadyUsed)).join("|");
1034
+ const newCmd = knownBy(command).join("|");
1035
+ throw new Error(`cannot add command '${newCmd}' as already have command '${existingCmd}'`);
1036
+ }
1037
+ this.commands.push(command);
1038
+ }
1039
+ addOption(option) {
1040
+ this._registerOption(option);
1041
+ const oname = option.name();
1042
+ const name = option.attributeName();
1043
+ if (option.negate) {
1044
+ const positiveLongFlag = option.long.replace(/^--no-/, "--");
1045
+ if (!this._findOption(positiveLongFlag)) {
1046
+ this.setOptionValueWithSource(name, option.defaultValue === undefined ? true : option.defaultValue, "default");
1047
+ }
1048
+ } else if (option.defaultValue !== undefined) {
1049
+ this.setOptionValueWithSource(name, option.defaultValue, "default");
1050
+ }
1051
+ const handleOptionValue = (val, invalidValueMessage, valueSource) => {
1052
+ if (val == null && option.presetArg !== undefined) {
1053
+ val = option.presetArg;
1054
+ }
1055
+ const oldValue = this.getOptionValue(name);
1056
+ if (val !== null && option.parseArg) {
1057
+ val = this._callParseArg(option, val, oldValue, invalidValueMessage);
1058
+ } else if (val !== null && option.variadic) {
1059
+ val = option._concatValue(val, oldValue);
1060
+ }
1061
+ if (val == null) {
1062
+ if (option.negate) {
1063
+ val = false;
1064
+ } else if (option.isBoolean() || option.optional) {
1065
+ val = true;
1066
+ } else {
1067
+ val = "";
1068
+ }
1069
+ }
1070
+ this.setOptionValueWithSource(name, val, valueSource);
1071
+ };
1072
+ this.on("option:" + oname, (val) => {
1073
+ const invalidValueMessage = `error: option '${option.flags}' argument '${val}' is invalid.`;
1074
+ handleOptionValue(val, invalidValueMessage, "cli");
1075
+ });
1076
+ if (option.envVar) {
1077
+ this.on("optionEnv:" + oname, (val) => {
1078
+ const invalidValueMessage = `error: option '${option.flags}' value '${val}' from env '${option.envVar}' is invalid.`;
1079
+ handleOptionValue(val, invalidValueMessage, "env");
1080
+ });
1081
+ }
1082
+ return this;
1083
+ }
1084
+ _optionEx(config, flags, description, fn, defaultValue) {
1085
+ if (typeof flags === "object" && flags instanceof Option) {
1086
+ throw new Error("To add an Option object use addOption() instead of option() or requiredOption()");
1087
+ }
1088
+ const option = this.createOption(flags, description);
1089
+ option.makeOptionMandatory(!!config.mandatory);
1090
+ if (typeof fn === "function") {
1091
+ option.default(defaultValue).argParser(fn);
1092
+ } else if (fn instanceof RegExp) {
1093
+ const regex = fn;
1094
+ fn = (val, def) => {
1095
+ const m = regex.exec(val);
1096
+ return m ? m[0] : def;
1097
+ };
1098
+ option.default(defaultValue).argParser(fn);
1099
+ } else {
1100
+ option.default(fn);
1101
+ }
1102
+ return this.addOption(option);
1103
+ }
1104
+ option(flags, description, parseArg, defaultValue) {
1105
+ return this._optionEx({}, flags, description, parseArg, defaultValue);
1106
+ }
1107
+ requiredOption(flags, description, parseArg, defaultValue) {
1108
+ return this._optionEx({ mandatory: true }, flags, description, parseArg, defaultValue);
1109
+ }
1110
+ combineFlagAndOptionalValue(combine = true) {
1111
+ this._combineFlagAndOptionalValue = !!combine;
1112
+ return this;
1113
+ }
1114
+ allowUnknownOption(allowUnknown = true) {
1115
+ this._allowUnknownOption = !!allowUnknown;
1116
+ return this;
1117
+ }
1118
+ allowExcessArguments(allowExcess = true) {
1119
+ this._allowExcessArguments = !!allowExcess;
1120
+ return this;
1121
+ }
1122
+ enablePositionalOptions(positional = true) {
1123
+ this._enablePositionalOptions = !!positional;
1124
+ return this;
1125
+ }
1126
+ passThroughOptions(passThrough = true) {
1127
+ this._passThroughOptions = !!passThrough;
1128
+ this._checkForBrokenPassThrough();
1129
+ return this;
1130
+ }
1131
+ _checkForBrokenPassThrough() {
1132
+ if (this.parent && this._passThroughOptions && !this.parent._enablePositionalOptions) {
1133
+ throw new Error(`passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`);
1134
+ }
1135
+ }
1136
+ storeOptionsAsProperties(storeAsProperties = true) {
1137
+ if (this.options.length) {
1138
+ throw new Error("call .storeOptionsAsProperties() before adding options");
1139
+ }
1140
+ if (Object.keys(this._optionValues).length) {
1141
+ throw new Error("call .storeOptionsAsProperties() before setting option values");
1142
+ }
1143
+ this._storeOptionsAsProperties = !!storeAsProperties;
1144
+ return this;
1145
+ }
1146
+ getOptionValue(key) {
1147
+ if (this._storeOptionsAsProperties) {
1148
+ return this[key];
1149
+ }
1150
+ return this._optionValues[key];
1151
+ }
1152
+ setOptionValue(key, value) {
1153
+ return this.setOptionValueWithSource(key, value, undefined);
1154
+ }
1155
+ setOptionValueWithSource(key, value, source) {
1156
+ if (this._storeOptionsAsProperties) {
1157
+ this[key] = value;
1158
+ } else {
1159
+ this._optionValues[key] = value;
1160
+ }
1161
+ this._optionValueSources[key] = source;
1162
+ return this;
1163
+ }
1164
+ getOptionValueSource(key) {
1165
+ return this._optionValueSources[key];
1166
+ }
1167
+ getOptionValueSourceWithGlobals(key) {
1168
+ let source;
1169
+ this._getCommandAndAncestors().forEach((cmd) => {
1170
+ if (cmd.getOptionValueSource(key) !== undefined) {
1171
+ source = cmd.getOptionValueSource(key);
1172
+ }
1173
+ });
1174
+ return source;
1175
+ }
1176
+ _prepareUserArgs(argv, parseOptions) {
1177
+ if (argv !== undefined && !Array.isArray(argv)) {
1178
+ throw new Error("first parameter to parse must be array or undefined");
1179
+ }
1180
+ parseOptions = parseOptions || {};
1181
+ if (argv === undefined && parseOptions.from === undefined) {
1182
+ if (process2.versions?.electron) {
1183
+ parseOptions.from = "electron";
1184
+ }
1185
+ const execArgv = process2.execArgv ?? [];
1186
+ if (execArgv.includes("-e") || execArgv.includes("--eval") || execArgv.includes("-p") || execArgv.includes("--print")) {
1187
+ parseOptions.from = "eval";
1188
+ }
1189
+ }
1190
+ if (argv === undefined) {
1191
+ argv = process2.argv;
1192
+ }
1193
+ this.rawArgs = argv.slice();
1194
+ let userArgs;
1195
+ switch (parseOptions.from) {
1196
+ case undefined:
1197
+ case "node":
1198
+ this._scriptPath = argv[1];
1199
+ userArgs = argv.slice(2);
1200
+ break;
1201
+ case "electron":
1202
+ if (process2.defaultApp) {
1203
+ this._scriptPath = argv[1];
1204
+ userArgs = argv.slice(2);
1205
+ } else {
1206
+ userArgs = argv.slice(1);
1207
+ }
1208
+ break;
1209
+ case "user":
1210
+ userArgs = argv.slice(0);
1211
+ break;
1212
+ case "eval":
1213
+ userArgs = argv.slice(1);
1214
+ break;
1215
+ default:
1216
+ throw new Error(`unexpected parse option { from: '${parseOptions.from}' }`);
1217
+ }
1218
+ if (!this._name && this._scriptPath)
1219
+ this.nameFromFilename(this._scriptPath);
1220
+ this._name = this._name || "program";
1221
+ return userArgs;
1222
+ }
1223
+ parse(argv, parseOptions) {
1224
+ this._prepareForParse();
1225
+ const userArgs = this._prepareUserArgs(argv, parseOptions);
1226
+ this._parseCommand([], userArgs);
1227
+ return this;
1228
+ }
1229
+ async parseAsync(argv, parseOptions) {
1230
+ this._prepareForParse();
1231
+ const userArgs = this._prepareUserArgs(argv, parseOptions);
1232
+ await this._parseCommand([], userArgs);
1233
+ return this;
1234
+ }
1235
+ _prepareForParse() {
1236
+ if (this._savedState === null) {
1237
+ this.saveStateBeforeParse();
1238
+ } else {
1239
+ this.restoreStateBeforeParse();
1240
+ }
1241
+ }
1242
+ saveStateBeforeParse() {
1243
+ this._savedState = {
1244
+ _name: this._name,
1245
+ _optionValues: { ...this._optionValues },
1246
+ _optionValueSources: { ...this._optionValueSources }
1247
+ };
1248
+ }
1249
+ restoreStateBeforeParse() {
1250
+ if (this._storeOptionsAsProperties)
1251
+ throw new Error(`Can not call parse again when storeOptionsAsProperties is true.
1252
+ - either make a new Command for each call to parse, or stop storing options as properties`);
1253
+ this._name = this._savedState._name;
1254
+ this._scriptPath = null;
1255
+ this.rawArgs = [];
1256
+ this._optionValues = { ...this._savedState._optionValues };
1257
+ this._optionValueSources = { ...this._savedState._optionValueSources };
1258
+ this.args = [];
1259
+ this.processedArgs = [];
1260
+ }
1261
+ _checkForMissingExecutable(executableFile, executableDir, subcommandName) {
1262
+ if (fs.existsSync(executableFile))
1263
+ return;
1264
+ const executableDirMessage = executableDir ? `searched for local subcommand relative to directory '${executableDir}'` : "no directory for search for local subcommand, use .executableDir() to supply a custom directory";
1265
+ const executableMissing = `'${executableFile}' does not exist
1266
+ - if '${subcommandName}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
1267
+ - if the default executable name is not suitable, use the executableFile option to supply a custom name or path
1268
+ - ${executableDirMessage}`;
1269
+ throw new Error(executableMissing);
1270
+ }
1271
+ _executeSubCommand(subcommand, args) {
1272
+ args = args.slice();
1273
+ let launchWithNode = false;
1274
+ const sourceExt = [".js", ".ts", ".tsx", ".mjs", ".cjs"];
1275
+ function findFile(baseDir, baseName) {
1276
+ const localBin = path.resolve(baseDir, baseName);
1277
+ if (fs.existsSync(localBin))
1278
+ return localBin;
1279
+ if (sourceExt.includes(path.extname(baseName)))
1280
+ return;
1281
+ const foundExt = sourceExt.find((ext) => fs.existsSync(`${localBin}${ext}`));
1282
+ if (foundExt)
1283
+ return `${localBin}${foundExt}`;
1284
+ return;
1285
+ }
1286
+ this._checkForMissingMandatoryOptions();
1287
+ this._checkForConflictingOptions();
1288
+ let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;
1289
+ let executableDir = this._executableDir || "";
1290
+ if (this._scriptPath) {
1291
+ let resolvedScriptPath;
1292
+ try {
1293
+ resolvedScriptPath = fs.realpathSync(this._scriptPath);
1294
+ } catch {
1295
+ resolvedScriptPath = this._scriptPath;
1296
+ }
1297
+ executableDir = path.resolve(path.dirname(resolvedScriptPath), executableDir);
1298
+ }
1299
+ if (executableDir) {
1300
+ let localFile = findFile(executableDir, executableFile);
1301
+ if (!localFile && !subcommand._executableFile && this._scriptPath) {
1302
+ const legacyName = path.basename(this._scriptPath, path.extname(this._scriptPath));
1303
+ if (legacyName !== this._name) {
1304
+ localFile = findFile(executableDir, `${legacyName}-${subcommand._name}`);
1305
+ }
1306
+ }
1307
+ executableFile = localFile || executableFile;
1308
+ }
1309
+ launchWithNode = sourceExt.includes(path.extname(executableFile));
1310
+ let proc;
1311
+ if (process2.platform !== "win32") {
1312
+ if (launchWithNode) {
1313
+ args.unshift(executableFile);
1314
+ args = incrementNodeInspectorPort(process2.execArgv).concat(args);
1315
+ proc = childProcess.spawn(process2.argv[0], args, { stdio: "inherit" });
1316
+ } else {
1317
+ proc = childProcess.spawn(executableFile, args, { stdio: "inherit" });
1318
+ }
1319
+ } else {
1320
+ this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
1321
+ args.unshift(executableFile);
1322
+ args = incrementNodeInspectorPort(process2.execArgv).concat(args);
1323
+ proc = childProcess.spawn(process2.execPath, args, { stdio: "inherit" });
1324
+ }
1325
+ if (!proc.killed) {
1326
+ const signals = ["SIGUSR1", "SIGUSR2", "SIGTERM", "SIGINT", "SIGHUP"];
1327
+ signals.forEach((signal) => {
1328
+ process2.on(signal, () => {
1329
+ if (proc.killed === false && proc.exitCode === null) {
1330
+ proc.kill(signal);
1331
+ }
1332
+ });
1333
+ });
1334
+ }
1335
+ const exitCallback = this._exitCallback;
1336
+ proc.on("close", (code) => {
1337
+ code = code ?? 1;
1338
+ if (!exitCallback) {
1339
+ process2.exit(code);
1340
+ } else {
1341
+ exitCallback(new CommanderError(code, "commander.executeSubCommandAsync", "(close)"));
1342
+ }
1343
+ });
1344
+ proc.on("error", (err) => {
1345
+ if (err.code === "ENOENT") {
1346
+ this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
1347
+ } else if (err.code === "EACCES") {
1348
+ throw new Error(`'${executableFile}' not executable`);
1349
+ }
1350
+ if (!exitCallback) {
1351
+ process2.exit(1);
1352
+ } else {
1353
+ const wrappedError = new CommanderError(1, "commander.executeSubCommandAsync", "(error)");
1354
+ wrappedError.nestedError = err;
1355
+ exitCallback(wrappedError);
1356
+ }
1357
+ });
1358
+ this.runningCommand = proc;
1359
+ }
1360
+ _dispatchSubcommand(commandName, operands, unknown) {
1361
+ const subCommand = this._findCommand(commandName);
1362
+ if (!subCommand)
1363
+ this.help({ error: true });
1364
+ subCommand._prepareForParse();
1365
+ let promiseChain;
1366
+ promiseChain = this._chainOrCallSubCommandHook(promiseChain, subCommand, "preSubcommand");
1367
+ promiseChain = this._chainOrCall(promiseChain, () => {
1368
+ if (subCommand._executableHandler) {
1369
+ this._executeSubCommand(subCommand, operands.concat(unknown));
1370
+ } else {
1371
+ return subCommand._parseCommand(operands, unknown);
1372
+ }
1373
+ });
1374
+ return promiseChain;
1375
+ }
1376
+ _dispatchHelpCommand(subcommandName) {
1377
+ if (!subcommandName) {
1378
+ this.help();
1379
+ }
1380
+ const subCommand = this._findCommand(subcommandName);
1381
+ if (subCommand && !subCommand._executableHandler) {
1382
+ subCommand.help();
1383
+ }
1384
+ return this._dispatchSubcommand(subcommandName, [], [this._getHelpOption()?.long ?? this._getHelpOption()?.short ?? "--help"]);
1385
+ }
1386
+ _checkNumberOfArguments() {
1387
+ this.registeredArguments.forEach((arg, i) => {
1388
+ if (arg.required && this.args[i] == null) {
1389
+ this.missingArgument(arg.name());
1390
+ }
1391
+ });
1392
+ if (this.registeredArguments.length > 0 && this.registeredArguments[this.registeredArguments.length - 1].variadic) {
1393
+ return;
1394
+ }
1395
+ if (this.args.length > this.registeredArguments.length) {
1396
+ this._excessArguments(this.args);
1397
+ }
1398
+ }
1399
+ _processArguments() {
1400
+ const myParseArg = (argument, value, previous) => {
1401
+ let parsedValue = value;
1402
+ if (value !== null && argument.parseArg) {
1403
+ const invalidValueMessage = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'.`;
1404
+ parsedValue = this._callParseArg(argument, value, previous, invalidValueMessage);
1405
+ }
1406
+ return parsedValue;
1407
+ };
1408
+ this._checkNumberOfArguments();
1409
+ const processedArgs = [];
1410
+ this.registeredArguments.forEach((declaredArg, index) => {
1411
+ let value = declaredArg.defaultValue;
1412
+ if (declaredArg.variadic) {
1413
+ if (index < this.args.length) {
1414
+ value = this.args.slice(index);
1415
+ if (declaredArg.parseArg) {
1416
+ value = value.reduce((processed, v) => {
1417
+ return myParseArg(declaredArg, v, processed);
1418
+ }, declaredArg.defaultValue);
1419
+ }
1420
+ } else if (value === undefined) {
1421
+ value = [];
1422
+ }
1423
+ } else if (index < this.args.length) {
1424
+ value = this.args[index];
1425
+ if (declaredArg.parseArg) {
1426
+ value = myParseArg(declaredArg, value, declaredArg.defaultValue);
1427
+ }
1428
+ }
1429
+ processedArgs[index] = value;
1430
+ });
1431
+ this.processedArgs = processedArgs;
1432
+ }
1433
+ _chainOrCall(promise, fn) {
1434
+ if (promise && promise.then && typeof promise.then === "function") {
1435
+ return promise.then(() => fn());
1436
+ }
1437
+ return fn();
1438
+ }
1439
+ _chainOrCallHooks(promise, event) {
1440
+ let result = promise;
1441
+ const hooks = [];
1442
+ this._getCommandAndAncestors().reverse().filter((cmd) => cmd._lifeCycleHooks[event] !== undefined).forEach((hookedCommand) => {
1443
+ hookedCommand._lifeCycleHooks[event].forEach((callback) => {
1444
+ hooks.push({ hookedCommand, callback });
1445
+ });
1446
+ });
1447
+ if (event === "postAction") {
1448
+ hooks.reverse();
1449
+ }
1450
+ hooks.forEach((hookDetail) => {
1451
+ result = this._chainOrCall(result, () => {
1452
+ return hookDetail.callback(hookDetail.hookedCommand, this);
1453
+ });
1454
+ });
1455
+ return result;
1456
+ }
1457
+ _chainOrCallSubCommandHook(promise, subCommand, event) {
1458
+ let result = promise;
1459
+ if (this._lifeCycleHooks[event] !== undefined) {
1460
+ this._lifeCycleHooks[event].forEach((hook) => {
1461
+ result = this._chainOrCall(result, () => {
1462
+ return hook(this, subCommand);
1463
+ });
1464
+ });
1465
+ }
1466
+ return result;
1467
+ }
1468
+ _parseCommand(operands, unknown) {
1469
+ const parsed = this.parseOptions(unknown);
1470
+ this._parseOptionsEnv();
1471
+ this._parseOptionsImplied();
1472
+ operands = operands.concat(parsed.operands);
1473
+ unknown = parsed.unknown;
1474
+ this.args = operands.concat(unknown);
1475
+ if (operands && this._findCommand(operands[0])) {
1476
+ return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
1477
+ }
1478
+ if (this._getHelpCommand() && operands[0] === this._getHelpCommand().name()) {
1479
+ return this._dispatchHelpCommand(operands[1]);
1480
+ }
1481
+ if (this._defaultCommandName) {
1482
+ this._outputHelpIfRequested(unknown);
1483
+ return this._dispatchSubcommand(this._defaultCommandName, operands, unknown);
1484
+ }
1485
+ if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) {
1486
+ this.help({ error: true });
1487
+ }
1488
+ this._outputHelpIfRequested(parsed.unknown);
1489
+ this._checkForMissingMandatoryOptions();
1490
+ this._checkForConflictingOptions();
1491
+ const checkForUnknownOptions = () => {
1492
+ if (parsed.unknown.length > 0) {
1493
+ this.unknownOption(parsed.unknown[0]);
1494
+ }
1495
+ };
1496
+ const commandEvent = `command:${this.name()}`;
1497
+ if (this._actionHandler) {
1498
+ checkForUnknownOptions();
1499
+ this._processArguments();
1500
+ let promiseChain;
1501
+ promiseChain = this._chainOrCallHooks(promiseChain, "preAction");
1502
+ promiseChain = this._chainOrCall(promiseChain, () => this._actionHandler(this.processedArgs));
1503
+ if (this.parent) {
1504
+ promiseChain = this._chainOrCall(promiseChain, () => {
1505
+ this.parent.emit(commandEvent, operands, unknown);
1506
+ });
1507
+ }
1508
+ promiseChain = this._chainOrCallHooks(promiseChain, "postAction");
1509
+ return promiseChain;
1510
+ }
1511
+ if (this.parent && this.parent.listenerCount(commandEvent)) {
1512
+ checkForUnknownOptions();
1513
+ this._processArguments();
1514
+ this.parent.emit(commandEvent, operands, unknown);
1515
+ } else if (operands.length) {
1516
+ if (this._findCommand("*")) {
1517
+ return this._dispatchSubcommand("*", operands, unknown);
1518
+ }
1519
+ if (this.listenerCount("command:*")) {
1520
+ this.emit("command:*", operands, unknown);
1521
+ } else if (this.commands.length) {
1522
+ this.unknownCommand();
1523
+ } else {
1524
+ checkForUnknownOptions();
1525
+ this._processArguments();
1526
+ }
1527
+ } else if (this.commands.length) {
1528
+ checkForUnknownOptions();
1529
+ this.help({ error: true });
1530
+ } else {
1531
+ checkForUnknownOptions();
1532
+ this._processArguments();
1533
+ }
1534
+ }
1535
+ _findCommand(name) {
1536
+ if (!name)
1537
+ return;
1538
+ return this.commands.find((cmd) => cmd._name === name || cmd._aliases.includes(name));
1539
+ }
1540
+ _findOption(arg) {
1541
+ return this.options.find((option) => option.is(arg));
1542
+ }
1543
+ _checkForMissingMandatoryOptions() {
1544
+ this._getCommandAndAncestors().forEach((cmd) => {
1545
+ cmd.options.forEach((anOption) => {
1546
+ if (anOption.mandatory && cmd.getOptionValue(anOption.attributeName()) === undefined) {
1547
+ cmd.missingMandatoryOptionValue(anOption);
1548
+ }
1549
+ });
1550
+ });
1551
+ }
1552
+ _checkForConflictingLocalOptions() {
1553
+ const definedNonDefaultOptions = this.options.filter((option) => {
1554
+ const optionKey = option.attributeName();
1555
+ if (this.getOptionValue(optionKey) === undefined) {
1556
+ return false;
1557
+ }
1558
+ return this.getOptionValueSource(optionKey) !== "default";
1559
+ });
1560
+ const optionsWithConflicting = definedNonDefaultOptions.filter((option) => option.conflictsWith.length > 0);
1561
+ optionsWithConflicting.forEach((option) => {
1562
+ const conflictingAndDefined = definedNonDefaultOptions.find((defined) => option.conflictsWith.includes(defined.attributeName()));
1563
+ if (conflictingAndDefined) {
1564
+ this._conflictingOption(option, conflictingAndDefined);
1565
+ }
1566
+ });
1567
+ }
1568
+ _checkForConflictingOptions() {
1569
+ this._getCommandAndAncestors().forEach((cmd) => {
1570
+ cmd._checkForConflictingLocalOptions();
1571
+ });
1572
+ }
1573
+ parseOptions(argv) {
1574
+ const operands = [];
1575
+ const unknown = [];
1576
+ let dest = operands;
1577
+ const args = argv.slice();
1578
+ function maybeOption(arg) {
1579
+ return arg.length > 1 && arg[0] === "-";
1580
+ }
1581
+ let activeVariadicOption = null;
1582
+ while (args.length) {
1583
+ const arg = args.shift();
1584
+ if (arg === "--") {
1585
+ if (dest === unknown)
1586
+ dest.push(arg);
1587
+ dest.push(...args);
1588
+ break;
1589
+ }
1590
+ if (activeVariadicOption && !maybeOption(arg)) {
1591
+ this.emit(`option:${activeVariadicOption.name()}`, arg);
1592
+ continue;
1593
+ }
1594
+ activeVariadicOption = null;
1595
+ if (maybeOption(arg)) {
1596
+ const option = this._findOption(arg);
1597
+ if (option) {
1598
+ if (option.required) {
1599
+ const value = args.shift();
1600
+ if (value === undefined)
1601
+ this.optionMissingArgument(option);
1602
+ this.emit(`option:${option.name()}`, value);
1603
+ } else if (option.optional) {
1604
+ let value = null;
1605
+ if (args.length > 0 && !maybeOption(args[0])) {
1606
+ value = args.shift();
1607
+ }
1608
+ this.emit(`option:${option.name()}`, value);
1609
+ } else {
1610
+ this.emit(`option:${option.name()}`);
1611
+ }
1612
+ activeVariadicOption = option.variadic ? option : null;
1613
+ continue;
1614
+ }
1615
+ }
1616
+ if (arg.length > 2 && arg[0] === "-" && arg[1] !== "-") {
1617
+ const option = this._findOption(`-${arg[1]}`);
1618
+ if (option) {
1619
+ if (option.required || option.optional && this._combineFlagAndOptionalValue) {
1620
+ this.emit(`option:${option.name()}`, arg.slice(2));
1621
+ } else {
1622
+ this.emit(`option:${option.name()}`);
1623
+ args.unshift(`-${arg.slice(2)}`);
1624
+ }
1625
+ continue;
1626
+ }
1627
+ }
1628
+ if (/^--[^=]+=/.test(arg)) {
1629
+ const index = arg.indexOf("=");
1630
+ const option = this._findOption(arg.slice(0, index));
1631
+ if (option && (option.required || option.optional)) {
1632
+ this.emit(`option:${option.name()}`, arg.slice(index + 1));
1633
+ continue;
1634
+ }
1635
+ }
1636
+ if (maybeOption(arg)) {
1637
+ dest = unknown;
1638
+ }
1639
+ if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
1640
+ if (this._findCommand(arg)) {
1641
+ operands.push(arg);
1642
+ if (args.length > 0)
1643
+ unknown.push(...args);
1644
+ break;
1645
+ } else if (this._getHelpCommand() && arg === this._getHelpCommand().name()) {
1646
+ operands.push(arg);
1647
+ if (args.length > 0)
1648
+ operands.push(...args);
1649
+ break;
1650
+ } else if (this._defaultCommandName) {
1651
+ unknown.push(arg);
1652
+ if (args.length > 0)
1653
+ unknown.push(...args);
1654
+ break;
1655
+ }
1656
+ }
1657
+ if (this._passThroughOptions) {
1658
+ dest.push(arg);
1659
+ if (args.length > 0)
1660
+ dest.push(...args);
1661
+ break;
1662
+ }
1663
+ dest.push(arg);
1664
+ }
1665
+ return { operands, unknown };
1666
+ }
1667
+ opts() {
1668
+ if (this._storeOptionsAsProperties) {
1669
+ const result = {};
1670
+ const len = this.options.length;
1671
+ for (let i = 0;i < len; i++) {
1672
+ const key = this.options[i].attributeName();
1673
+ result[key] = key === this._versionOptionName ? this._version : this[key];
1674
+ }
1675
+ return result;
1676
+ }
1677
+ return this._optionValues;
1678
+ }
1679
+ optsWithGlobals() {
1680
+ return this._getCommandAndAncestors().reduce((combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()), {});
1681
+ }
1682
+ error(message, errorOptions) {
1683
+ this._outputConfiguration.outputError(`${message}
1684
+ `, this._outputConfiguration.writeErr);
1685
+ if (typeof this._showHelpAfterError === "string") {
1686
+ this._outputConfiguration.writeErr(`${this._showHelpAfterError}
1687
+ `);
1688
+ } else if (this._showHelpAfterError) {
1689
+ this._outputConfiguration.writeErr(`
1690
+ `);
1691
+ this.outputHelp({ error: true });
1692
+ }
1693
+ const config = errorOptions || {};
1694
+ const exitCode = config.exitCode || 1;
1695
+ const code = config.code || "commander.error";
1696
+ this._exit(exitCode, code, message);
1697
+ }
1698
+ _parseOptionsEnv() {
1699
+ this.options.forEach((option) => {
1700
+ if (option.envVar && option.envVar in process2.env) {
1701
+ const optionKey = option.attributeName();
1702
+ if (this.getOptionValue(optionKey) === undefined || ["default", "config", "env"].includes(this.getOptionValueSource(optionKey))) {
1703
+ if (option.required || option.optional) {
1704
+ this.emit(`optionEnv:${option.name()}`, process2.env[option.envVar]);
1705
+ } else {
1706
+ this.emit(`optionEnv:${option.name()}`);
1707
+ }
1708
+ }
1709
+ }
1710
+ });
1711
+ }
1712
+ _parseOptionsImplied() {
1713
+ const dualHelper = new DualOptions(this.options);
1714
+ const hasCustomOptionValue = (optionKey) => {
1715
+ return this.getOptionValue(optionKey) !== undefined && !["default", "implied"].includes(this.getOptionValueSource(optionKey));
1716
+ };
1717
+ this.options.filter((option) => option.implied !== undefined && hasCustomOptionValue(option.attributeName()) && dualHelper.valueFromOption(this.getOptionValue(option.attributeName()), option)).forEach((option) => {
1718
+ Object.keys(option.implied).filter((impliedKey) => !hasCustomOptionValue(impliedKey)).forEach((impliedKey) => {
1719
+ this.setOptionValueWithSource(impliedKey, option.implied[impliedKey], "implied");
1720
+ });
1721
+ });
1722
+ }
1723
+ missingArgument(name) {
1724
+ const message = `error: missing required argument '${name}'`;
1725
+ this.error(message, { code: "commander.missingArgument" });
1726
+ }
1727
+ optionMissingArgument(option) {
1728
+ const message = `error: option '${option.flags}' argument missing`;
1729
+ this.error(message, { code: "commander.optionMissingArgument" });
1730
+ }
1731
+ missingMandatoryOptionValue(option) {
1732
+ const message = `error: required option '${option.flags}' not specified`;
1733
+ this.error(message, { code: "commander.missingMandatoryOptionValue" });
1734
+ }
1735
+ _conflictingOption(option, conflictingOption) {
1736
+ const findBestOptionFromValue = (option2) => {
1737
+ const optionKey = option2.attributeName();
1738
+ const optionValue = this.getOptionValue(optionKey);
1739
+ const negativeOption = this.options.find((target) => target.negate && optionKey === target.attributeName());
1740
+ const positiveOption = this.options.find((target) => !target.negate && optionKey === target.attributeName());
1741
+ if (negativeOption && (negativeOption.presetArg === undefined && optionValue === false || negativeOption.presetArg !== undefined && optionValue === negativeOption.presetArg)) {
1742
+ return negativeOption;
1743
+ }
1744
+ return positiveOption || option2;
1745
+ };
1746
+ const getErrorMessage = (option2) => {
1747
+ const bestOption = findBestOptionFromValue(option2);
1748
+ const optionKey = bestOption.attributeName();
1749
+ const source = this.getOptionValueSource(optionKey);
1750
+ if (source === "env") {
1751
+ return `environment variable '${bestOption.envVar}'`;
1752
+ }
1753
+ return `option '${bestOption.flags}'`;
1754
+ };
1755
+ const message = `error: ${getErrorMessage(option)} cannot be used with ${getErrorMessage(conflictingOption)}`;
1756
+ this.error(message, { code: "commander.conflictingOption" });
1757
+ }
1758
+ unknownOption(flag) {
1759
+ if (this._allowUnknownOption)
1760
+ return;
1761
+ let suggestion = "";
1762
+ if (flag.startsWith("--") && this._showSuggestionAfterError) {
1763
+ let candidateFlags = [];
1764
+ let command = this;
1765
+ do {
1766
+ const moreFlags = command.createHelp().visibleOptions(command).filter((option) => option.long).map((option) => option.long);
1767
+ candidateFlags = candidateFlags.concat(moreFlags);
1768
+ command = command.parent;
1769
+ } while (command && !command._enablePositionalOptions);
1770
+ suggestion = suggestSimilar(flag, candidateFlags);
1771
+ }
1772
+ const message = `error: unknown option '${flag}'${suggestion}`;
1773
+ this.error(message, { code: "commander.unknownOption" });
1774
+ }
1775
+ _excessArguments(receivedArgs) {
1776
+ if (this._allowExcessArguments)
1777
+ return;
1778
+ const expected = this.registeredArguments.length;
1779
+ const s = expected === 1 ? "" : "s";
1780
+ const forSubcommand = this.parent ? ` for '${this.name()}'` : "";
1781
+ const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;
1782
+ this.error(message, { code: "commander.excessArguments" });
1783
+ }
1784
+ unknownCommand() {
1785
+ const unknownName = this.args[0];
1786
+ let suggestion = "";
1787
+ if (this._showSuggestionAfterError) {
1788
+ const candidateNames = [];
1789
+ this.createHelp().visibleCommands(this).forEach((command) => {
1790
+ candidateNames.push(command.name());
1791
+ if (command.alias())
1792
+ candidateNames.push(command.alias());
1793
+ });
1794
+ suggestion = suggestSimilar(unknownName, candidateNames);
1795
+ }
1796
+ const message = `error: unknown command '${unknownName}'${suggestion}`;
1797
+ this.error(message, { code: "commander.unknownCommand" });
1798
+ }
1799
+ version(str, flags, description) {
1800
+ if (str === undefined)
1801
+ return this._version;
1802
+ this._version = str;
1803
+ flags = flags || "-V, --version";
1804
+ description = description || "output the version number";
1805
+ const versionOption = this.createOption(flags, description);
1806
+ this._versionOptionName = versionOption.attributeName();
1807
+ this._registerOption(versionOption);
1808
+ this.on("option:" + versionOption.name(), () => {
1809
+ this._outputConfiguration.writeOut(`${str}
1810
+ `);
1811
+ this._exit(0, "commander.version", str);
1812
+ });
1813
+ return this;
1814
+ }
1815
+ description(str, argsDescription) {
1816
+ if (str === undefined && argsDescription === undefined)
1817
+ return this._description;
1818
+ this._description = str;
1819
+ if (argsDescription) {
1820
+ this._argsDescription = argsDescription;
1821
+ }
1822
+ return this;
1823
+ }
1824
+ summary(str) {
1825
+ if (str === undefined)
1826
+ return this._summary;
1827
+ this._summary = str;
1828
+ return this;
1829
+ }
1830
+ alias(alias) {
1831
+ if (alias === undefined)
1832
+ return this._aliases[0];
1833
+ let command = this;
1834
+ if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
1835
+ command = this.commands[this.commands.length - 1];
1836
+ }
1837
+ if (alias === command._name)
1838
+ throw new Error("Command alias can't be the same as its name");
1839
+ const matchingCommand = this.parent?._findCommand(alias);
1840
+ if (matchingCommand) {
1841
+ const existingCmd = [matchingCommand.name()].concat(matchingCommand.aliases()).join("|");
1842
+ throw new Error(`cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`);
1843
+ }
1844
+ command._aliases.push(alias);
1845
+ return this;
1846
+ }
1847
+ aliases(aliases) {
1848
+ if (aliases === undefined)
1849
+ return this._aliases;
1850
+ aliases.forEach((alias) => this.alias(alias));
1851
+ return this;
1852
+ }
1853
+ usage(str) {
1854
+ if (str === undefined) {
1855
+ if (this._usage)
1856
+ return this._usage;
1857
+ const args = this.registeredArguments.map((arg) => {
1858
+ return humanReadableArgName(arg);
1859
+ });
1860
+ return [].concat(this.options.length || this._helpOption !== null ? "[options]" : [], this.commands.length ? "[command]" : [], this.registeredArguments.length ? args : []).join(" ");
1861
+ }
1862
+ this._usage = str;
1863
+ return this;
1864
+ }
1865
+ name(str) {
1866
+ if (str === undefined)
1867
+ return this._name;
1868
+ this._name = str;
1869
+ return this;
1870
+ }
1871
+ nameFromFilename(filename) {
1872
+ this._name = path.basename(filename, path.extname(filename));
1873
+ return this;
1874
+ }
1875
+ executableDir(path2) {
1876
+ if (path2 === undefined)
1877
+ return this._executableDir;
1878
+ this._executableDir = path2;
1879
+ return this;
1880
+ }
1881
+ helpInformation(contextOptions) {
1882
+ const helper = this.createHelp();
1883
+ const context = this._getOutputContext(contextOptions);
1884
+ helper.prepareContext({
1885
+ error: context.error,
1886
+ helpWidth: context.helpWidth,
1887
+ outputHasColors: context.hasColors
1888
+ });
1889
+ const text = helper.formatHelp(this, helper);
1890
+ if (context.hasColors)
1891
+ return text;
1892
+ return this._outputConfiguration.stripColor(text);
1893
+ }
1894
+ _getOutputContext(contextOptions) {
1895
+ contextOptions = contextOptions || {};
1896
+ const error = !!contextOptions.error;
1897
+ let baseWrite;
1898
+ let hasColors;
1899
+ let helpWidth;
1900
+ if (error) {
1901
+ baseWrite = (str) => this._outputConfiguration.writeErr(str);
1902
+ hasColors = this._outputConfiguration.getErrHasColors();
1903
+ helpWidth = this._outputConfiguration.getErrHelpWidth();
1904
+ } else {
1905
+ baseWrite = (str) => this._outputConfiguration.writeOut(str);
1906
+ hasColors = this._outputConfiguration.getOutHasColors();
1907
+ helpWidth = this._outputConfiguration.getOutHelpWidth();
1908
+ }
1909
+ const write = (str) => {
1910
+ if (!hasColors)
1911
+ str = this._outputConfiguration.stripColor(str);
1912
+ return baseWrite(str);
1913
+ };
1914
+ return { error, write, hasColors, helpWidth };
1915
+ }
1916
+ outputHelp(contextOptions) {
1917
+ let deprecatedCallback;
1918
+ if (typeof contextOptions === "function") {
1919
+ deprecatedCallback = contextOptions;
1920
+ contextOptions = undefined;
1921
+ }
1922
+ const outputContext = this._getOutputContext(contextOptions);
1923
+ const eventContext = {
1924
+ error: outputContext.error,
1925
+ write: outputContext.write,
1926
+ command: this
1927
+ };
1928
+ this._getCommandAndAncestors().reverse().forEach((command) => command.emit("beforeAllHelp", eventContext));
1929
+ this.emit("beforeHelp", eventContext);
1930
+ let helpInformation = this.helpInformation({ error: outputContext.error });
1931
+ if (deprecatedCallback) {
1932
+ helpInformation = deprecatedCallback(helpInformation);
1933
+ if (typeof helpInformation !== "string" && !Buffer.isBuffer(helpInformation)) {
1934
+ throw new Error("outputHelp callback must return a string or a Buffer");
1935
+ }
1936
+ }
1937
+ outputContext.write(helpInformation);
1938
+ if (this._getHelpOption()?.long) {
1939
+ this.emit(this._getHelpOption().long);
1940
+ }
1941
+ this.emit("afterHelp", eventContext);
1942
+ this._getCommandAndAncestors().forEach((command) => command.emit("afterAllHelp", eventContext));
1943
+ }
1944
+ helpOption(flags, description) {
1945
+ if (typeof flags === "boolean") {
1946
+ if (flags) {
1947
+ this._helpOption = this._helpOption ?? undefined;
1948
+ } else {
1949
+ this._helpOption = null;
1950
+ }
1951
+ return this;
1952
+ }
1953
+ flags = flags ?? "-h, --help";
1954
+ description = description ?? "display help for command";
1955
+ this._helpOption = this.createOption(flags, description);
1956
+ return this;
1957
+ }
1958
+ _getHelpOption() {
1959
+ if (this._helpOption === undefined) {
1960
+ this.helpOption(undefined, undefined);
1961
+ }
1962
+ return this._helpOption;
1963
+ }
1964
+ addHelpOption(option) {
1965
+ this._helpOption = option;
1966
+ return this;
1967
+ }
1968
+ help(contextOptions) {
1969
+ this.outputHelp(contextOptions);
1970
+ let exitCode = Number(process2.exitCode ?? 0);
1971
+ if (exitCode === 0 && contextOptions && typeof contextOptions !== "function" && contextOptions.error) {
1972
+ exitCode = 1;
1973
+ }
1974
+ this._exit(exitCode, "commander.help", "(outputHelp)");
1975
+ }
1976
+ addHelpText(position, text) {
1977
+ const allowedValues = ["beforeAll", "before", "after", "afterAll"];
1978
+ if (!allowedValues.includes(position)) {
1979
+ throw new Error(`Unexpected value for position to addHelpText.
1980
+ Expecting one of '${allowedValues.join("', '")}'`);
1981
+ }
1982
+ const helpEvent = `${position}Help`;
1983
+ this.on(helpEvent, (context) => {
1984
+ let helpStr;
1985
+ if (typeof text === "function") {
1986
+ helpStr = text({ error: context.error, command: context.command });
1987
+ } else {
1988
+ helpStr = text;
1989
+ }
1990
+ if (helpStr) {
1991
+ context.write(`${helpStr}
1992
+ `);
1993
+ }
1994
+ });
1995
+ return this;
1996
+ }
1997
+ _outputHelpIfRequested(args) {
1998
+ const helpOption = this._getHelpOption();
1999
+ const helpRequested = helpOption && args.find((arg) => helpOption.is(arg));
2000
+ if (helpRequested) {
2001
+ this.outputHelp();
2002
+ this._exit(0, "commander.helpDisplayed", "(outputHelp)");
2003
+ }
2004
+ }
2005
+ }
2006
+ function incrementNodeInspectorPort(args) {
2007
+ return args.map((arg) => {
2008
+ if (!arg.startsWith("--inspect")) {
2009
+ return arg;
2010
+ }
2011
+ let debugOption;
2012
+ let debugHost = "127.0.0.1";
2013
+ let debugPort = "9229";
2014
+ let match;
2015
+ if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
2016
+ debugOption = match[1];
2017
+ } else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
2018
+ debugOption = match[1];
2019
+ if (/^\d+$/.test(match[3])) {
2020
+ debugPort = match[3];
2021
+ } else {
2022
+ debugHost = match[3];
2023
+ }
2024
+ } else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) {
2025
+ debugOption = match[1];
2026
+ debugHost = match[3];
2027
+ debugPort = match[4];
2028
+ }
2029
+ if (debugOption && debugPort !== "0") {
2030
+ return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
2031
+ }
2032
+ return arg;
2033
+ });
2034
+ }
2035
+ function useColor() {
2036
+ if (process2.env.NO_COLOR || process2.env.FORCE_COLOR === "0" || process2.env.FORCE_COLOR === "false")
2037
+ return false;
2038
+ if (process2.env.FORCE_COLOR || process2.env.CLICOLOR_FORCE !== undefined)
2039
+ return true;
2040
+ return;
2041
+ }
2042
+ exports.Command = Command;
2043
+ exports.useColor = useColor;
2044
+ });
2045
+
2046
+ // ../../node_modules/.bun/commander@13.1.0/node_modules/commander/index.js
2047
+ var require_commander = __commonJS((exports) => {
2048
+ var { Argument } = require_argument();
2049
+ var { Command } = require_command();
2050
+ var { CommanderError, InvalidArgumentError } = require_error();
2051
+ var { Help } = require_help();
2052
+ var { Option } = require_option();
2053
+ exports.program = new Command;
2054
+ exports.createCommand = (name) => new Command(name);
2055
+ exports.createOption = (flags, description) => new Option(flags, description);
2056
+ exports.createArgument = (name, description) => new Argument(name, description);
2057
+ exports.Command = Command;
2058
+ exports.Option = Option;
2059
+ exports.Argument = Argument;
2060
+ exports.Help = Help;
2061
+ exports.CommanderError = CommanderError;
2062
+ exports.InvalidArgumentError = InvalidArgumentError;
2063
+ exports.InvalidOptionArgumentError = InvalidArgumentError;
2064
+ });
2065
+
2066
+ // ../../node_modules/.bun/is-docker@3.0.0/node_modules/is-docker/index.js
2067
+ import fs2 from "node:fs";
2068
+ function hasDockerEnv() {
2069
+ try {
2070
+ fs2.statSync("/.dockerenv");
2071
+ return true;
2072
+ } catch {
2073
+ return false;
2074
+ }
2075
+ }
2076
+ function hasDockerCGroup() {
2077
+ try {
2078
+ return fs2.readFileSync("/proc/self/cgroup", "utf8").includes("docker");
2079
+ } catch {
2080
+ return false;
2081
+ }
2082
+ }
2083
+ function isDocker() {
2084
+ if (isDockerCached === undefined) {
2085
+ isDockerCached = hasDockerEnv() || hasDockerCGroup();
2086
+ }
2087
+ return isDockerCached;
2088
+ }
2089
+ var isDockerCached;
2090
+ var init_is_docker = () => {};
2091
+
2092
+ // ../../node_modules/.bun/is-inside-container@1.0.0/node_modules/is-inside-container/index.js
2093
+ import fs3 from "node:fs";
2094
+ function isInsideContainer() {
2095
+ if (cachedResult === undefined) {
2096
+ cachedResult = hasContainerEnv() || isDocker();
2097
+ }
2098
+ return cachedResult;
2099
+ }
2100
+ var cachedResult, hasContainerEnv = () => {
2101
+ try {
2102
+ fs3.statSync("/run/.containerenv");
2103
+ return true;
2104
+ } catch {
2105
+ return false;
2106
+ }
2107
+ };
2108
+ var init_is_inside_container = __esm(() => {
2109
+ init_is_docker();
2110
+ });
2111
+
2112
+ // ../../node_modules/.bun/is-wsl@3.1.0/node_modules/is-wsl/index.js
2113
+ import process2 from "node:process";
2114
+ import os2 from "node:os";
2115
+ import fs4 from "node:fs";
2116
+ var isWsl = () => {
2117
+ if (process2.platform !== "linux") {
2118
+ return false;
2119
+ }
2120
+ if (os2.release().toLowerCase().includes("microsoft")) {
2121
+ if (isInsideContainer()) {
2122
+ return false;
2123
+ }
2124
+ return true;
2125
+ }
2126
+ try {
2127
+ return fs4.readFileSync("/proc/version", "utf8").toLowerCase().includes("microsoft") ? !isInsideContainer() : false;
2128
+ } catch {
2129
+ return false;
2130
+ }
2131
+ }, is_wsl_default;
2132
+ var init_is_wsl = __esm(() => {
2133
+ init_is_inside_container();
2134
+ is_wsl_default = process2.env.__IS_WSL_TEST__ ? isWsl : isWsl();
2135
+ });
2136
+
2137
+ // ../../node_modules/.bun/wsl-utils@0.1.0/node_modules/wsl-utils/index.js
2138
+ import process3 from "node:process";
2139
+ import fs5, { constants as fsConstants } from "node:fs/promises";
2140
+ var wslDrivesMountPoint, powerShellPathFromWsl = async () => {
2141
+ const mountPoint = await wslDrivesMountPoint();
2142
+ return `${mountPoint}c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe`;
2143
+ }, powerShellPath = async () => {
2144
+ if (is_wsl_default) {
2145
+ return powerShellPathFromWsl();
2146
+ }
2147
+ return `${process3.env.SYSTEMROOT || process3.env.windir || String.raw`C:\Windows`}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`;
2148
+ };
2149
+ var init_wsl_utils = __esm(() => {
2150
+ init_is_wsl();
2151
+ init_is_wsl();
2152
+ wslDrivesMountPoint = (() => {
2153
+ const defaultMountPoint = "/mnt/";
2154
+ let mountPoint;
2155
+ return async function() {
2156
+ if (mountPoint) {
2157
+ return mountPoint;
2158
+ }
2159
+ const configFilePath = "/etc/wsl.conf";
2160
+ let isConfigFileExists = false;
2161
+ try {
2162
+ await fs5.access(configFilePath, fsConstants.F_OK);
2163
+ isConfigFileExists = true;
2164
+ } catch {}
2165
+ if (!isConfigFileExists) {
2166
+ return defaultMountPoint;
2167
+ }
2168
+ const configContent = await fs5.readFile(configFilePath, { encoding: "utf8" });
2169
+ const configMountPoint = /(?<!#.*)root\s*=\s*(?<mountPoint>.*)/g.exec(configContent);
2170
+ if (!configMountPoint) {
2171
+ return defaultMountPoint;
2172
+ }
2173
+ mountPoint = configMountPoint.groups.mountPoint.trim();
2174
+ mountPoint = mountPoint.endsWith("/") ? mountPoint : `${mountPoint}/`;
2175
+ return mountPoint;
2176
+ };
2177
+ })();
2178
+ });
2179
+
2180
+ // ../../node_modules/.bun/define-lazy-prop@3.0.0/node_modules/define-lazy-prop/index.js
2181
+ function defineLazyProperty(object, propertyName, valueGetter) {
2182
+ const define = (value) => Object.defineProperty(object, propertyName, { value, enumerable: true, writable: true });
2183
+ Object.defineProperty(object, propertyName, {
2184
+ configurable: true,
2185
+ enumerable: true,
2186
+ get() {
2187
+ const result = valueGetter();
2188
+ define(result);
2189
+ return result;
2190
+ },
2191
+ set(value) {
2192
+ define(value);
2193
+ }
2194
+ });
2195
+ return object;
2196
+ }
2197
+
2198
+ // ../../node_modules/.bun/default-browser-id@5.0.1/node_modules/default-browser-id/index.js
2199
+ import { promisify } from "node:util";
2200
+ import process4 from "node:process";
2201
+ import { execFile } from "node:child_process";
2202
+ async function defaultBrowserId() {
2203
+ if (process4.platform !== "darwin") {
2204
+ throw new Error("macOS only");
2205
+ }
2206
+ const { stdout } = await execFileAsync("defaults", ["read", "com.apple.LaunchServices/com.apple.launchservices.secure", "LSHandlers"]);
2207
+ const match = /LSHandlerRoleAll = "(?!-)(?<id>[^"]+?)";\s+?LSHandlerURLScheme = (?:http|https);/.exec(stdout);
2208
+ const browserId = match?.groups.id ?? "com.apple.Safari";
2209
+ if (browserId === "com.apple.safari") {
2210
+ return "com.apple.Safari";
2211
+ }
2212
+ return browserId;
2213
+ }
2214
+ var execFileAsync;
2215
+ var init_default_browser_id = __esm(() => {
2216
+ execFileAsync = promisify(execFile);
2217
+ });
2218
+
2219
+ // ../../node_modules/.bun/run-applescript@7.1.0/node_modules/run-applescript/index.js
2220
+ import process5 from "node:process";
2221
+ import { promisify as promisify2 } from "node:util";
2222
+ import { execFile as execFile2, execFileSync } from "node:child_process";
2223
+ async function runAppleScript(script, { humanReadableOutput = true, signal } = {}) {
2224
+ if (process5.platform !== "darwin") {
2225
+ throw new Error("macOS only");
2226
+ }
2227
+ const outputArguments = humanReadableOutput ? [] : ["-ss"];
2228
+ const execOptions = {};
2229
+ if (signal) {
2230
+ execOptions.signal = signal;
2231
+ }
2232
+ const { stdout } = await execFileAsync2("osascript", ["-e", script, outputArguments], execOptions);
2233
+ return stdout.trim();
2234
+ }
2235
+ var execFileAsync2;
2236
+ var init_run_applescript = __esm(() => {
2237
+ execFileAsync2 = promisify2(execFile2);
2238
+ });
2239
+
2240
+ // ../../node_modules/.bun/bundle-name@4.1.0/node_modules/bundle-name/index.js
2241
+ async function bundleName(bundleId) {
2242
+ return runAppleScript(`tell application "Finder" to set app_path to application file id "${bundleId}" as string
2243
+ tell application "System Events" to get value of property list item "CFBundleName" of property list file (app_path & ":Contents:Info.plist")`);
2244
+ }
2245
+ var init_bundle_name = __esm(() => {
2246
+ init_run_applescript();
2247
+ });
2248
+
2249
+ // ../../node_modules/.bun/default-browser@5.5.0/node_modules/default-browser/windows.js
2250
+ import { promisify as promisify3 } from "node:util";
2251
+ import { execFile as execFile3 } from "node:child_process";
2252
+ async function defaultBrowser(_execFileAsync = execFileAsync3) {
2253
+ const { stdout } = await _execFileAsync("reg", [
2254
+ "QUERY",
2255
+ " HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice",
2256
+ "/v",
2257
+ "ProgId"
2258
+ ]);
2259
+ const match = /ProgId\s*REG_SZ\s*(?<id>\S+)/.exec(stdout);
2260
+ if (!match) {
2261
+ throw new UnknownBrowserError(`Cannot find Windows browser in stdout: ${JSON.stringify(stdout)}`);
2262
+ }
2263
+ const { id } = match.groups;
2264
+ const dotIndex = id.lastIndexOf(".");
2265
+ const hyphenIndex = id.lastIndexOf("-");
2266
+ const baseIdByDot = dotIndex === -1 ? undefined : id.slice(0, dotIndex);
2267
+ const baseIdByHyphen = hyphenIndex === -1 ? undefined : id.slice(0, hyphenIndex);
2268
+ return windowsBrowserProgIds[id] ?? windowsBrowserProgIds[baseIdByDot] ?? windowsBrowserProgIds[baseIdByHyphen] ?? { name: id, id };
2269
+ }
2270
+ var execFileAsync3, windowsBrowserProgIds, _windowsBrowserProgIdMap, UnknownBrowserError;
2271
+ var init_windows = __esm(() => {
2272
+ execFileAsync3 = promisify3(execFile3);
2273
+ windowsBrowserProgIds = {
2274
+ MSEdgeHTM: { name: "Edge", id: "com.microsoft.edge" },
2275
+ MSEdgeBHTML: { name: "Edge Beta", id: "com.microsoft.edge.beta" },
2276
+ MSEdgeDHTML: { name: "Edge Dev", id: "com.microsoft.edge.dev" },
2277
+ AppXq0fevzme2pys62n3e0fbqa7peapykr8v: { name: "Edge", id: "com.microsoft.edge.old" },
2278
+ ChromeHTML: { name: "Chrome", id: "com.google.chrome" },
2279
+ ChromeBHTML: { name: "Chrome Beta", id: "com.google.chrome.beta" },
2280
+ ChromeDHTML: { name: "Chrome Dev", id: "com.google.chrome.dev" },
2281
+ ChromiumHTM: { name: "Chromium", id: "org.chromium.Chromium" },
2282
+ BraveHTML: { name: "Brave", id: "com.brave.Browser" },
2283
+ BraveBHTML: { name: "Brave Beta", id: "com.brave.Browser.beta" },
2284
+ BraveDHTML: { name: "Brave Dev", id: "com.brave.Browser.dev" },
2285
+ BraveSSHTM: { name: "Brave Nightly", id: "com.brave.Browser.nightly" },
2286
+ FirefoxURL: { name: "Firefox", id: "org.mozilla.firefox" },
2287
+ OperaStable: { name: "Opera", id: "com.operasoftware.Opera" },
2288
+ VivaldiHTM: { name: "Vivaldi", id: "com.vivaldi.Vivaldi" },
2289
+ "IE.HTTP": { name: "Internet Explorer", id: "com.microsoft.ie" }
2290
+ };
2291
+ _windowsBrowserProgIdMap = new Map(Object.entries(windowsBrowserProgIds));
2292
+ UnknownBrowserError = class UnknownBrowserError extends Error {
2293
+ };
2294
+ });
2295
+
2296
+ // ../../node_modules/.bun/default-browser@5.5.0/node_modules/default-browser/index.js
2297
+ import { promisify as promisify4 } from "node:util";
2298
+ import process6 from "node:process";
2299
+ import { execFile as execFile4 } from "node:child_process";
2300
+ async function defaultBrowser2() {
2301
+ if (process6.platform === "darwin") {
2302
+ const id = await defaultBrowserId();
2303
+ const name = await bundleName(id);
2304
+ return { name, id };
2305
+ }
2306
+ if (process6.platform === "linux") {
2307
+ const { stdout } = await execFileAsync4("xdg-mime", ["query", "default", "x-scheme-handler/http"]);
2308
+ const id = stdout.trim();
2309
+ const name = titleize(id.replace(/.desktop$/, "").replace("-", " "));
2310
+ return { name, id };
2311
+ }
2312
+ if (process6.platform === "win32") {
2313
+ return defaultBrowser();
2314
+ }
2315
+ throw new Error("Only macOS, Linux, and Windows are supported");
2316
+ }
2317
+ var execFileAsync4, titleize = (string) => string.toLowerCase().replaceAll(/(?:^|\s|-)\S/g, (x) => x.toUpperCase());
2318
+ var init_default_browser = __esm(() => {
2319
+ init_default_browser_id();
2320
+ init_bundle_name();
2321
+ init_windows();
2322
+ execFileAsync4 = promisify4(execFile4);
2323
+ });
2324
+
2325
+ // ../../node_modules/.bun/open@10.2.0/node_modules/open/index.js
2326
+ var exports_open = {};
2327
+ __export(exports_open, {
2328
+ openApp: () => openApp,
2329
+ default: () => open_default,
2330
+ apps: () => apps
2331
+ });
2332
+ import process7 from "node:process";
2333
+ import { Buffer as Buffer2 } from "node:buffer";
2334
+ import path2 from "node:path";
2335
+ import { fileURLToPath } from "node:url";
2336
+ import { promisify as promisify5 } from "node:util";
2337
+ import childProcess from "node:child_process";
2338
+ import fs6, { constants as fsConstants2 } from "node:fs/promises";
2339
+ async function getWindowsDefaultBrowserFromWsl() {
2340
+ const powershellPath = await powerShellPath();
2341
+ const rawCommand = String.raw`(Get-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice").ProgId`;
2342
+ const encodedCommand = Buffer2.from(rawCommand, "utf16le").toString("base64");
2343
+ const { stdout } = await execFile5(powershellPath, [
2344
+ "-NoProfile",
2345
+ "-NonInteractive",
2346
+ "-ExecutionPolicy",
2347
+ "Bypass",
2348
+ "-EncodedCommand",
2349
+ encodedCommand
2350
+ ], { encoding: "utf8" });
2351
+ const progId = stdout.trim();
2352
+ const browserMap = {
2353
+ ChromeHTML: "com.google.chrome",
2354
+ BraveHTML: "com.brave.Browser",
2355
+ MSEdgeHTM: "com.microsoft.edge",
2356
+ FirefoxURL: "org.mozilla.firefox"
2357
+ };
2358
+ return browserMap[progId] ? { id: browserMap[progId] } : {};
2359
+ }
2360
+ function detectArchBinary(binary) {
2361
+ if (typeof binary === "string" || Array.isArray(binary)) {
2362
+ return binary;
2363
+ }
2364
+ const { [arch]: archBinary } = binary;
2365
+ if (!archBinary) {
2366
+ throw new Error(`${arch} is not supported`);
2367
+ }
2368
+ return archBinary;
2369
+ }
2370
+ function detectPlatformBinary({ [platform]: platformBinary }, { wsl }) {
2371
+ if (wsl && is_wsl_default) {
2372
+ return detectArchBinary(wsl);
2373
+ }
2374
+ if (!platformBinary) {
2375
+ throw new Error(`${platform} is not supported`);
2376
+ }
2377
+ return detectArchBinary(platformBinary);
2378
+ }
2379
+ var execFile5, __dirname2, localXdgOpenPath, platform, arch, pTryEach = async (array, mapper) => {
2380
+ let latestError;
2381
+ for (const item of array) {
2382
+ try {
2383
+ return await mapper(item);
2384
+ } catch (error) {
2385
+ latestError = error;
2386
+ }
2387
+ }
2388
+ throw latestError;
2389
+ }, baseOpen = async (options) => {
2390
+ options = {
2391
+ wait: false,
2392
+ background: false,
2393
+ newInstance: false,
2394
+ allowNonzeroExitCode: false,
2395
+ ...options
2396
+ };
2397
+ if (Array.isArray(options.app)) {
2398
+ return pTryEach(options.app, (singleApp) => baseOpen({
2399
+ ...options,
2400
+ app: singleApp
2401
+ }));
2402
+ }
2403
+ let { name: app, arguments: appArguments = [] } = options.app ?? {};
2404
+ appArguments = [...appArguments];
2405
+ if (Array.isArray(app)) {
2406
+ return pTryEach(app, (appName) => baseOpen({
2407
+ ...options,
2408
+ app: {
2409
+ name: appName,
2410
+ arguments: appArguments
2411
+ }
2412
+ }));
2413
+ }
2414
+ if (app === "browser" || app === "browserPrivate") {
2415
+ const ids = {
2416
+ "com.google.chrome": "chrome",
2417
+ "google-chrome.desktop": "chrome",
2418
+ "com.brave.Browser": "brave",
2419
+ "org.mozilla.firefox": "firefox",
2420
+ "firefox.desktop": "firefox",
2421
+ "com.microsoft.msedge": "edge",
2422
+ "com.microsoft.edge": "edge",
2423
+ "com.microsoft.edgemac": "edge",
2424
+ "microsoft-edge.desktop": "edge"
2425
+ };
2426
+ const flags = {
2427
+ chrome: "--incognito",
2428
+ brave: "--incognito",
2429
+ firefox: "--private-window",
2430
+ edge: "--inPrivate"
2431
+ };
2432
+ const browser = is_wsl_default ? await getWindowsDefaultBrowserFromWsl() : await defaultBrowser2();
2433
+ if (browser.id in ids) {
2434
+ const browserName = ids[browser.id];
2435
+ if (app === "browserPrivate") {
2436
+ appArguments.push(flags[browserName]);
2437
+ }
2438
+ return baseOpen({
2439
+ ...options,
2440
+ app: {
2441
+ name: apps[browserName],
2442
+ arguments: appArguments
2443
+ }
2444
+ });
2445
+ }
2446
+ throw new Error(`${browser.name} is not supported as a default browser`);
2447
+ }
2448
+ let command;
2449
+ const cliArguments = [];
2450
+ const childProcessOptions = {};
2451
+ if (platform === "darwin") {
2452
+ command = "open";
2453
+ if (options.wait) {
2454
+ cliArguments.push("--wait-apps");
2455
+ }
2456
+ if (options.background) {
2457
+ cliArguments.push("--background");
2458
+ }
2459
+ if (options.newInstance) {
2460
+ cliArguments.push("--new");
2461
+ }
2462
+ if (app) {
2463
+ cliArguments.push("-a", app);
2464
+ }
2465
+ } else if (platform === "win32" || is_wsl_default && !isInsideContainer() && !app) {
2466
+ command = await powerShellPath();
2467
+ cliArguments.push("-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-EncodedCommand");
2468
+ if (!is_wsl_default) {
2469
+ childProcessOptions.windowsVerbatimArguments = true;
2470
+ }
2471
+ const encodedArguments = ["Start"];
2472
+ if (options.wait) {
2473
+ encodedArguments.push("-Wait");
2474
+ }
2475
+ if (app) {
2476
+ encodedArguments.push(`"\`"${app}\`""`);
2477
+ if (options.target) {
2478
+ appArguments.push(options.target);
2479
+ }
2480
+ } else if (options.target) {
2481
+ encodedArguments.push(`"${options.target}"`);
2482
+ }
2483
+ if (appArguments.length > 0) {
2484
+ appArguments = appArguments.map((argument) => `"\`"${argument}\`""`);
2485
+ encodedArguments.push("-ArgumentList", appArguments.join(","));
2486
+ }
2487
+ options.target = Buffer2.from(encodedArguments.join(" "), "utf16le").toString("base64");
2488
+ } else {
2489
+ if (app) {
2490
+ command = app;
2491
+ } else {
2492
+ const isBundled = !__dirname2 || __dirname2 === "/";
2493
+ let exeLocalXdgOpen = false;
2494
+ try {
2495
+ await fs6.access(localXdgOpenPath, fsConstants2.X_OK);
2496
+ exeLocalXdgOpen = true;
2497
+ } catch {}
2498
+ const useSystemXdgOpen = process7.versions.electron ?? (platform === "android" || isBundled || !exeLocalXdgOpen);
2499
+ command = useSystemXdgOpen ? "xdg-open" : localXdgOpenPath;
2500
+ }
2501
+ if (appArguments.length > 0) {
2502
+ cliArguments.push(...appArguments);
2503
+ }
2504
+ if (!options.wait) {
2505
+ childProcessOptions.stdio = "ignore";
2506
+ childProcessOptions.detached = true;
2507
+ }
2508
+ }
2509
+ if (platform === "darwin" && appArguments.length > 0) {
2510
+ cliArguments.push("--args", ...appArguments);
2511
+ }
2512
+ if (options.target) {
2513
+ cliArguments.push(options.target);
2514
+ }
2515
+ const subprocess = childProcess.spawn(command, cliArguments, childProcessOptions);
2516
+ if (options.wait) {
2517
+ return new Promise((resolve, reject) => {
2518
+ subprocess.once("error", reject);
2519
+ subprocess.once("close", (exitCode) => {
2520
+ if (!options.allowNonzeroExitCode && exitCode > 0) {
2521
+ reject(new Error(`Exited with code ${exitCode}`));
2522
+ return;
2523
+ }
2524
+ resolve(subprocess);
2525
+ });
2526
+ });
2527
+ }
2528
+ subprocess.unref();
2529
+ return subprocess;
2530
+ }, open = (target, options) => {
2531
+ if (typeof target !== "string") {
2532
+ throw new TypeError("Expected a `target`");
2533
+ }
2534
+ return baseOpen({
2535
+ ...options,
2536
+ target
2537
+ });
2538
+ }, openApp = (name, options) => {
2539
+ if (typeof name !== "string" && !Array.isArray(name)) {
2540
+ throw new TypeError("Expected a valid `name`");
2541
+ }
2542
+ const { arguments: appArguments = [] } = options ?? {};
2543
+ if (appArguments !== undefined && appArguments !== null && !Array.isArray(appArguments)) {
2544
+ throw new TypeError("Expected `appArguments` as Array type");
2545
+ }
2546
+ return baseOpen({
2547
+ ...options,
2548
+ app: {
2549
+ name,
2550
+ arguments: appArguments
2551
+ }
2552
+ });
2553
+ }, apps, open_default;
2554
+ var init_open = __esm(() => {
2555
+ init_wsl_utils();
2556
+ init_default_browser();
2557
+ init_is_inside_container();
2558
+ execFile5 = promisify5(childProcess.execFile);
2559
+ __dirname2 = path2.dirname(fileURLToPath(import.meta.url));
2560
+ localXdgOpenPath = path2.join(__dirname2, "xdg-open");
2561
+ ({ platform, arch } = process7);
2562
+ apps = {};
2563
+ defineLazyProperty(apps, "chrome", () => detectPlatformBinary({
2564
+ darwin: "google chrome",
2565
+ win32: "chrome",
2566
+ linux: ["google-chrome", "google-chrome-stable", "chromium"]
2567
+ }, {
2568
+ wsl: {
2569
+ ia32: "/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe",
2570
+ x64: ["/mnt/c/Program Files/Google/Chrome/Application/chrome.exe", "/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe"]
2571
+ }
2572
+ }));
2573
+ defineLazyProperty(apps, "brave", () => detectPlatformBinary({
2574
+ darwin: "brave browser",
2575
+ win32: "brave",
2576
+ linux: ["brave-browser", "brave"]
2577
+ }, {
2578
+ wsl: {
2579
+ ia32: "/mnt/c/Program Files (x86)/BraveSoftware/Brave-Browser/Application/brave.exe",
2580
+ x64: ["/mnt/c/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe", "/mnt/c/Program Files (x86)/BraveSoftware/Brave-Browser/Application/brave.exe"]
2581
+ }
2582
+ }));
2583
+ defineLazyProperty(apps, "firefox", () => detectPlatformBinary({
2584
+ darwin: "firefox",
2585
+ win32: String.raw`C:\Program Files\Mozilla Firefox\firefox.exe`,
2586
+ linux: "firefox"
2587
+ }, {
2588
+ wsl: "/mnt/c/Program Files/Mozilla Firefox/firefox.exe"
2589
+ }));
2590
+ defineLazyProperty(apps, "edge", () => detectPlatformBinary({
2591
+ darwin: "microsoft edge",
2592
+ win32: "msedge",
2593
+ linux: ["microsoft-edge", "microsoft-edge-dev"]
2594
+ }, {
2595
+ wsl: "/mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe"
2596
+ }));
2597
+ defineLazyProperty(apps, "browser", () => "browser");
2598
+ defineLazyProperty(apps, "browserPrivate", () => "browserPrivate");
2599
+ open_default = open;
2600
+ });
2601
+
2602
+ // ../../node_modules/.bun/commander@13.1.0/node_modules/commander/esm.mjs
2603
+ var import__ = __toESM(require_commander(), 1);
2604
+ var {
2605
+ program,
2606
+ createCommand,
2607
+ createArgument,
2608
+ createOption,
2609
+ CommanderError,
2610
+ InvalidArgumentError,
2611
+ InvalidOptionArgumentError,
2612
+ Command,
2613
+ Argument,
2614
+ Option,
2615
+ Help
2616
+ } = import__.default;
2617
+
2618
+ // src/commands/login.ts
2619
+ import * as http from "http";
2620
+
2621
+ // src/lib/config.ts
2622
+ import * as fs from "fs";
2623
+ import * as path from "path";
2624
+ import * as os from "os";
2625
+ var CONFIG_DIR = path.join(os.homedir(), ".calabasas");
2626
+ var CONFIG_FILE = path.join(CONFIG_DIR, "config.json");
2627
+ var DEFAULT_API_URL = "https://notable-monitor-41.convex.site";
2628
+ function getConfig() {
2629
+ try {
2630
+ if (!fs.existsSync(CONFIG_FILE)) {
2631
+ return {};
2632
+ }
2633
+ const content = fs.readFileSync(CONFIG_FILE, "utf-8");
2634
+ return JSON.parse(content);
2635
+ } catch {
2636
+ return {};
2637
+ }
2638
+ }
2639
+ function saveConfig(config) {
2640
+ const existing = getConfig();
2641
+ const merged = { ...existing, ...config };
2642
+ if (!fs.existsSync(CONFIG_DIR)) {
2643
+ fs.mkdirSync(CONFIG_DIR, { recursive: true });
2644
+ }
2645
+ fs.writeFileSync(CONFIG_FILE, JSON.stringify(merged, null, 2));
2646
+ }
2647
+ function clearConfig() {
2648
+ if (fs.existsSync(CONFIG_FILE)) {
2649
+ fs.unlinkSync(CONFIG_FILE);
2650
+ }
2651
+ }
2652
+ function isAuthenticated() {
2653
+ const config = getConfig();
2654
+ return !!config.apiKey;
2655
+ }
2656
+ function getApiUrl() {
2657
+ const config = getConfig();
2658
+ return config.apiUrl || DEFAULT_API_URL;
2659
+ }
2660
+
2661
+ // src/commands/login.ts
2662
+ var CALLBACK_PORT = 9876;
2663
+ async function login() {
2664
+ const existing = getConfig();
2665
+ if (existing.apiKey) {
2666
+ console.log("You are already logged in.");
2667
+ console.log(`API Key: ${existing.apiKey.slice(0, 8)}...`);
2668
+ console.log("");
2669
+ console.log("Run `calabasas logout` to clear credentials.");
2670
+ return;
2671
+ }
2672
+ console.log(`Opening browser for authentication...
2673
+ `);
2674
+ const server = http.createServer((req, res) => {
2675
+ const url = new URL(req.url || "", `http://localhost:${CALLBACK_PORT}`);
2676
+ if (url.pathname === "/callback") {
2677
+ const apiKey = url.searchParams.get("key");
2678
+ if (apiKey) {
2679
+ saveConfig({ apiKey });
2680
+ res.writeHead(200, { "Content-Type": "text/html" });
2681
+ res.end(`
2682
+ <!DOCTYPE html>
2683
+ <html>
2684
+ <head>
2685
+ <title>Calabasas CLI</title>
2686
+ <style>
2687
+ body { font-family: system-ui; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background: #0a0a0a; color: white; }
2688
+ .container { text-align: center; }
2689
+ h1 { color: #22c55e; }
2690
+ </style>
2691
+ </head>
2692
+ <body>
2693
+ <div class="container">
2694
+ <h1>✓ Authenticated!</h1>
2695
+ <p>You can close this window and return to the terminal.</p>
2696
+ </div>
2697
+ </body>
2698
+ </html>
2699
+ `);
2700
+ console.log("✅ Logged in successfully!");
2701
+ console.log("");
2702
+ console.log("You can now use:");
2703
+ console.log(" calabasas bot add - Add a Discord bot");
2704
+ console.log(" calabasas bot list - List your bots");
2705
+ console.log(" calabasas generate - Generate event handlers");
2706
+ setTimeout(() => {
2707
+ server.close();
2708
+ process.exit(0);
2709
+ }, 500);
2710
+ } else {
2711
+ res.writeHead(400, { "Content-Type": "text/html" });
2712
+ res.end(`
2713
+ <!DOCTYPE html>
2714
+ <html>
2715
+ <head><title>Error</title></head>
2716
+ <body>
2717
+ <h1>Authentication failed</h1>
2718
+ <p>No API key received.</p>
2719
+ </body>
2720
+ </html>
2721
+ `);
2722
+ }
2723
+ } else {
2724
+ res.writeHead(404);
2725
+ res.end("Not found");
2726
+ }
2727
+ });
2728
+ server.listen(CALLBACK_PORT, async () => {
2729
+ const authUrl = `http://localhost:3002/auth/cli?callback=http://localhost:${CALLBACK_PORT}/callback`;
2730
+ console.log("If the browser doesn't open automatically, visit:");
2731
+ console.log(` ${authUrl}
2732
+ `);
2733
+ const open2 = await Promise.resolve().then(() => (init_open(), exports_open)).catch(() => null);
2734
+ if (open2) {
2735
+ open2.default(authUrl);
2736
+ } else {
2737
+ const { exec } = await import("child_process");
2738
+ const cmd = process.platform === "darwin" ? "open" : process.platform === "win32" ? "start" : "xdg-open";
2739
+ exec(`${cmd} "${authUrl}"`);
2740
+ }
2741
+ });
2742
+ setTimeout(() => {
2743
+ console.log(`
2744
+ Authentication timed out. Please try again.`);
2745
+ server.close();
2746
+ process.exit(1);
2747
+ }, 5 * 60 * 1000);
2748
+ }
2749
+
2750
+ // src/commands/logout.ts
2751
+ async function logout() {
2752
+ clearConfig();
2753
+ console.log("✅ Logged out successfully. Credentials cleared.");
2754
+ }
2755
+
2756
+ // src/commands/init.ts
2757
+ import * as fs7 from "fs";
2758
+ import * as path3 from "path";
2759
+ var CONFIG_TEMPLATE = `// Calabasas configuration
2760
+ // Documentation: https://calabasas.dev/docs/config
2761
+
2762
+ import { defineCalabasas } from "calabasas";
2763
+
2764
+ export default defineCalabasas({
2765
+ // Managed sync - Discord data synced automatically to your tables
2766
+ sync: {
2767
+ guilds: true, // Sync server info (calabasasGuilds table)
2768
+ channels: true, // Sync channels (calabasasChannels table)
2769
+ roles: true, // Sync roles (calabasasRoles table)
2770
+ members: false, // Sync members (calabasasMembers table) - requires privileged intent
2771
+ },
2772
+
2773
+ // Custom event handlers - events forwarded to your discord:receive mutation
2774
+ events: {
2775
+ // Full event data
2776
+ // messageCreate: true,
2777
+
2778
+ // Or specify fields to receive
2779
+ // messageCreate: ["id", "content", "author", "channelId"],
2780
+ // interactionCreate: true,
2781
+ },
2782
+ });
2783
+ `;
2784
+ var DEFINE_CALABASAS_TEMPLATE = `// Type definitions for calabasas.config.ts
2785
+
2786
+ export type SyncConfig = {
2787
+ guilds?: boolean;
2788
+ channels?: boolean;
2789
+ roles?: boolean;
2790
+ members?: boolean;
2791
+ };
2792
+
2793
+ export type EventConfig = {
2794
+ [eventType: string]: true | string[];
2795
+ };
2796
+
2797
+ export type CalabasasConfig = {
2798
+ sync?: SyncConfig;
2799
+ events?: EventConfig;
2800
+ };
2801
+
2802
+ export function defineCalabasas(config: CalabasasConfig): CalabasasConfig {
2803
+ return config;
2804
+ }
2805
+ `;
2806
+ async function init() {
2807
+ const convexDir = path3.resolve(process.cwd(), "convex");
2808
+ const configPath = path3.join(convexDir, "calabasas.config.ts");
2809
+ if (!fs7.existsSync(convexDir)) {
2810
+ console.error("Error: convex/ directory not found.");
2811
+ console.error("");
2812
+ console.error("Make sure you're in a Convex project directory.");
2813
+ console.error("Run `npx convex dev` to initialize a new Convex project.");
2814
+ process.exit(1);
2815
+ }
2816
+ if (fs7.existsSync(configPath)) {
2817
+ console.error("Error: convex/calabasas.config.ts already exists.");
2818
+ console.error("");
2819
+ console.error("Delete the existing file if you want to start fresh:");
2820
+ console.error(" rm convex/calabasas.config.ts");
2821
+ process.exit(1);
2822
+ }
2823
+ fs7.writeFileSync(configPath, CONFIG_TEMPLATE);
2824
+ console.log("Created convex/calabasas.config.ts");
2825
+ const typesDir = path3.join(convexDir, "lib");
2826
+ const typesPath = path3.join(typesDir, "calabasas.ts");
2827
+ if (!fs7.existsSync(typesDir)) {
2828
+ fs7.mkdirSync(typesDir, { recursive: true });
2829
+ }
2830
+ if (!fs7.existsSync(typesPath)) {
2831
+ fs7.writeFileSync(typesPath, DEFINE_CALABASAS_TEMPLATE);
2832
+ console.log("Created convex/lib/calabasas.ts");
2833
+ }
2834
+ console.log("");
2835
+ console.log("Next steps:");
2836
+ console.log("1. Edit convex/calabasas.config.ts to configure sync and events");
2837
+ console.log("2. Run `calabasas generate` to generate type-safe handlers");
2838
+ console.log("3. Run `calabasas push` to sync config with Calabasas");
2839
+ }
2840
+
2841
+ // src/commands/push.ts
2842
+ import * as fs8 from "fs";
2843
+ import * as path4 from "path";
2844
+ function parseConfigFile(configPath) {
2845
+ const configContent = fs8.readFileSync(configPath, "utf-8");
2846
+ const syncMatch = configContent.match(/sync:\s*\{([^}]+)\}/);
2847
+ const eventsMatch = configContent.match(/events:\s*\{([^}]+)\}/);
2848
+ const sync = {};
2849
+ const events = {};
2850
+ if (syncMatch) {
2851
+ const syncContent = syncMatch[1];
2852
+ if (/guilds:\s*true/.test(syncContent))
2853
+ sync.guilds = true;
2854
+ if (/channels:\s*true/.test(syncContent))
2855
+ sync.channels = true;
2856
+ if (/roles:\s*true/.test(syncContent))
2857
+ sync.roles = true;
2858
+ if (/members:\s*true/.test(syncContent))
2859
+ sync.members = true;
2860
+ }
2861
+ if (eventsMatch) {
2862
+ const eventsContent = eventsMatch[1];
2863
+ const eventRegex = /(\w+):\s*(?:true|(\[[^\]]+\]))/g;
2864
+ let match;
2865
+ while ((match = eventRegex.exec(eventsContent)) !== null) {
2866
+ const eventName = match[1];
2867
+ if (match[2]) {
2868
+ const fields = match[2].replace(/[\[\]"'\s]/g, "").split(",").filter(Boolean);
2869
+ events[eventName] = fields;
2870
+ } else {
2871
+ events[eventName] = true;
2872
+ }
2873
+ }
2874
+ }
2875
+ return { sync, events };
2876
+ }
2877
+ async function selectBot(apiKey) {
2878
+ const response = await fetch(`${getApiUrl()}/api/cli/bots`, {
2879
+ method: "GET",
2880
+ headers: {
2881
+ "Content-Type": "application/json",
2882
+ Authorization: `Bearer ${apiKey}`
2883
+ }
2884
+ });
2885
+ if (!response.ok) {
2886
+ console.error("Error fetching bots:", await response.text());
2887
+ return null;
2888
+ }
2889
+ const { bots } = await response.json();
2890
+ if (!bots || bots.length === 0) {
2891
+ console.error("No bots found. Add a bot first with `calabasas bot add`");
2892
+ return null;
2893
+ }
2894
+ if (bots.length === 1) {
2895
+ return bots[0]._id;
2896
+ }
2897
+ console.log("Select a bot:");
2898
+ bots.forEach((bot, index) => {
2899
+ console.log(` ${index + 1}. ${bot.name} (${bot._id})`);
2900
+ });
2901
+ const readline = await import("readline");
2902
+ const rl = readline.createInterface({
2903
+ input: process.stdin,
2904
+ output: process.stdout
2905
+ });
2906
+ return new Promise((resolve3) => {
2907
+ rl.question("Enter number: ", (answer) => {
2908
+ rl.close();
2909
+ const index = parseInt(answer, 10) - 1;
2910
+ if (index >= 0 && index < bots.length) {
2911
+ resolve3(bots[index]._id);
2912
+ } else {
2913
+ console.error("Invalid selection");
2914
+ resolve3(null);
2915
+ }
2916
+ });
2917
+ });
2918
+ }
2919
+ async function push(options) {
2920
+ const configPath = path4.resolve(process.cwd(), options.config);
2921
+ if (!fs8.existsSync(configPath)) {
2922
+ console.error(`Error: Config file not found: ${configPath}`);
2923
+ console.error("");
2924
+ console.error("Run `calabasas init` to create a calabasas.config.ts file.");
2925
+ process.exit(1);
2926
+ }
2927
+ if (!isAuthenticated()) {
2928
+ console.error("Error: Not authenticated. Run `calabasas login` first.");
2929
+ process.exit(1);
2930
+ }
2931
+ const config = getConfig();
2932
+ const apiKey = config.apiKey;
2933
+ let botId = options.bot;
2934
+ if (!botId) {
2935
+ botId = await selectBot(apiKey) ?? undefined;
2936
+ if (!botId) {
2937
+ process.exit(1);
2938
+ }
2939
+ }
2940
+ console.log(`Pushing config from ${options.config}...`);
2941
+ const calabasasConfig = parseConfigFile(configPath);
2942
+ const eventConfigs = Object.entries(calabasasConfig.events ?? {}).map(([eventType, fields]) => ({
2943
+ eventType,
2944
+ fields: fields === true ? true : fields
2945
+ }));
2946
+ const response = await fetch(`${getApiUrl()}/api/cli/sync-config`, {
2947
+ method: "POST",
2948
+ headers: {
2949
+ "Content-Type": "application/json",
2950
+ Authorization: `Bearer ${apiKey}`
2951
+ },
2952
+ body: JSON.stringify({
2953
+ botId,
2954
+ syncGuilds: calabasasConfig.sync?.guilds ?? false,
2955
+ syncChannels: calabasasConfig.sync?.channels ?? false,
2956
+ syncRoles: calabasasConfig.sync?.roles ?? false,
2957
+ syncMembers: calabasasConfig.sync?.members ?? false,
2958
+ eventConfigs
2959
+ })
2960
+ });
2961
+ if (!response.ok) {
2962
+ const error = await response.text();
2963
+ console.error("Error pushing config:", error);
2964
+ process.exit(1);
2965
+ }
2966
+ const result = await response.json();
2967
+ console.log("");
2968
+ console.log("Config pushed successfully!");
2969
+ console.log("");
2970
+ console.log("Sync settings:");
2971
+ console.log(` Guilds: ${calabasasConfig.sync?.guilds ? "enabled" : "disabled"}`);
2972
+ console.log(` Channels: ${calabasasConfig.sync?.channels ? "enabled" : "disabled"}`);
2973
+ console.log(` Roles: ${calabasasConfig.sync?.roles ? "enabled" : "disabled"}`);
2974
+ console.log(` Members: ${calabasasConfig.sync?.members ? "enabled" : "disabled"}`);
2975
+ console.log("");
2976
+ console.log(`Event handlers: ${eventConfigs.length} configured`);
2977
+ eventConfigs.forEach((ec) => {
2978
+ const fieldsStr = ec.fields === true ? "(all fields)" : `(${ec.fields.join(", ")})`;
2979
+ console.log(` - ${ec.eventType} ${fieldsStr}`);
2980
+ });
2981
+ }
2982
+
2983
+ // src/commands/generate.ts
2984
+ import * as fs9 from "fs";
2985
+ import * as path5 from "path";
2986
+
2987
+ // src/lib/generators/schema.ts
2988
+ function generateSchemaFile(sync) {
2989
+ const tables = [];
2990
+ if (sync.guilds) {
2991
+ tables.push(`export const calabasasGuilds = defineTable({
2992
+ discordId: v.string(),
2993
+ name: v.string(),
2994
+ icon: v.optional(v.string()),
2995
+ ownerId: v.string(),
2996
+ memberCount: v.optional(v.number()),
2997
+ features: v.array(v.string()),
2998
+ premiumTier: v.number(),
2999
+ syncedAt: v.number(),
3000
+ })
3001
+ .index("by_discord_id", ["discordId"]);`);
3002
+ }
3003
+ if (sync.channels) {
3004
+ tables.push(`export const calabasasChannels = defineTable({
3005
+ discordId: v.string(),
3006
+ guildDiscordId: v.string(),
3007
+ name: v.string(),
3008
+ type: v.number(),
3009
+ position: v.number(),
3010
+ parentId: v.optional(v.string()),
3011
+ topic: v.optional(v.string()),
3012
+ nsfw: v.optional(v.boolean()),
3013
+ syncedAt: v.number(),
3014
+ })
3015
+ .index("by_discord_id", ["discordId"])
3016
+ .index("by_guild", ["guildDiscordId"]);`);
3017
+ }
3018
+ if (sync.roles) {
3019
+ tables.push(`export const calabasasRoles = defineTable({
3020
+ discordId: v.string(),
3021
+ guildDiscordId: v.string(),
3022
+ name: v.string(),
3023
+ color: v.number(),
3024
+ position: v.number(),
3025
+ permissions: v.string(),
3026
+ hoist: v.boolean(),
3027
+ mentionable: v.boolean(),
3028
+ syncedAt: v.number(),
3029
+ })
3030
+ .index("by_discord_id", ["discordId"])
3031
+ .index("by_guild", ["guildDiscordId"]);`);
3032
+ }
3033
+ if (sync.members) {
3034
+ tables.push(`export const calabasasMembers = defineTable({
3035
+ discordUserId: v.string(),
3036
+ guildDiscordId: v.string(),
3037
+ username: v.string(),
3038
+ displayName: v.optional(v.string()),
3039
+ avatar: v.optional(v.string()),
3040
+ nick: v.optional(v.string()),
3041
+ roles: v.array(v.string()),
3042
+ joinedAt: v.string(),
3043
+ bot: v.optional(v.boolean()),
3044
+ syncedAt: v.number(),
3045
+ })
3046
+ .index("by_user_guild", ["discordUserId", "guildDiscordId"])
3047
+ .index("by_guild", ["guildDiscordId"]);`);
3048
+ }
3049
+ const tableNames = [];
3050
+ if (sync.guilds)
3051
+ tableNames.push("calabasasGuilds");
3052
+ if (sync.channels)
3053
+ tableNames.push("calabasasChannels");
3054
+ if (sync.roles)
3055
+ tableNames.push("calabasasRoles");
3056
+ if (sync.members)
3057
+ tableNames.push("calabasasMembers");
3058
+ const tablesExport = tableNames.length > 0 ? `export const calabasasTables = {
3059
+ ${tableNames.join(`,
3060
+ `)},
3061
+ };` : `export const calabasasTables = {};`;
3062
+ return `// This file is auto-generated by \`calabasas generate\`
3063
+ // Do not edit manually
3064
+
3065
+ import { defineTable } from "convex/server";
3066
+ import { v } from "convex/values";
3067
+
3068
+ ${tables.join(`
3069
+
3070
+ `)}
3071
+
3072
+ // Spread this into your schema.ts defineSchema()
3073
+ ${tablesExport}
3074
+ `;
3075
+ }
3076
+
3077
+ // src/lib/generators/sync.ts
3078
+ var GUILD_VALIDATOR = `v.object({
3079
+ id: v.string(),
3080
+ name: v.string(),
3081
+ icon: v.optional(v.string()),
3082
+ owner_id: v.string(),
3083
+ member_count: v.optional(v.number()),
3084
+ features: v.array(v.string()),
3085
+ premium_tier: v.number(),
3086
+ })`;
3087
+ var CHANNEL_VALIDATOR = `v.object({
3088
+ id: v.string(),
3089
+ guild_id: v.optional(v.string()),
3090
+ name: v.string(),
3091
+ type: v.number(),
3092
+ position: v.optional(v.number()),
3093
+ parent_id: v.optional(v.string()),
3094
+ topic: v.optional(v.string()),
3095
+ nsfw: v.optional(v.boolean()),
3096
+ })`;
3097
+ var ROLE_VALIDATOR = `v.object({
3098
+ id: v.string(),
3099
+ guild_id: v.string(),
3100
+ name: v.string(),
3101
+ color: v.number(),
3102
+ position: v.number(),
3103
+ permissions: v.string(),
3104
+ hoist: v.boolean(),
3105
+ mentionable: v.boolean(),
3106
+ })`;
3107
+ var MEMBER_VALIDATOR = `v.object({
3108
+ user: v.object({
3109
+ id: v.string(),
3110
+ username: v.string(),
3111
+ global_name: v.optional(v.string()),
3112
+ avatar: v.optional(v.string()),
3113
+ bot: v.optional(v.boolean()),
3114
+ }),
3115
+ guild_id: v.string(),
3116
+ nick: v.optional(v.string()),
3117
+ roles: v.array(v.string()),
3118
+ joined_at: v.string(),
3119
+ })`;
3120
+ function generateGuildMutation() {
3121
+ return `export const syncGuild = internalMutation({
3122
+ args: {
3123
+ guild: guildValidator,
3124
+ operation: v.union(v.literal("upsert"), v.literal("delete")),
3125
+ },
3126
+ returns: v.null(),
3127
+ handler: async (ctx, { guild, operation }) => {
3128
+ if (operation === "delete") {
3129
+ const existing = await ctx.db
3130
+ .query("calabasasGuilds")
3131
+ .withIndex("by_discord_id", (q) => q.eq("discordId", guild.id))
3132
+ .unique();
3133
+ if (existing) await ctx.db.delete(existing._id);
3134
+ return null;
3135
+ }
3136
+
3137
+ const existing = await ctx.db
3138
+ .query("calabasasGuilds")
3139
+ .withIndex("by_discord_id", (q) => q.eq("discordId", guild.id))
3140
+ .unique();
3141
+
3142
+ const data = {
3143
+ discordId: guild.id,
3144
+ name: guild.name,
3145
+ icon: guild.icon ?? undefined,
3146
+ ownerId: guild.owner_id,
3147
+ memberCount: guild.member_count ?? undefined,
3148
+ features: guild.features,
3149
+ premiumTier: guild.premium_tier,
3150
+ syncedAt: Date.now(),
3151
+ };
3152
+
3153
+ if (existing) {
3154
+ await ctx.db.patch(existing._id, data);
3155
+ } else {
3156
+ await ctx.db.insert("calabasasGuilds", data);
3157
+ }
3158
+ return null;
3159
+ },
3160
+ });`;
3161
+ }
3162
+ function generateChannelMutation() {
3163
+ return `export const syncChannel = internalMutation({
3164
+ args: {
3165
+ channel: channelValidator,
3166
+ operation: v.union(v.literal("upsert"), v.literal("delete")),
3167
+ },
3168
+ returns: v.null(),
3169
+ handler: async (ctx, { channel, operation }) => {
3170
+ if (operation === "delete") {
3171
+ const existing = await ctx.db
3172
+ .query("calabasasChannels")
3173
+ .withIndex("by_discord_id", (q) => q.eq("discordId", channel.id))
3174
+ .unique();
3175
+ if (existing) await ctx.db.delete(existing._id);
3176
+ return null;
3177
+ }
3178
+
3179
+ const existing = await ctx.db
3180
+ .query("calabasasChannels")
3181
+ .withIndex("by_discord_id", (q) => q.eq("discordId", channel.id))
3182
+ .unique();
3183
+
3184
+ const data = {
3185
+ discordId: channel.id,
3186
+ guildDiscordId: channel.guild_id ?? "",
3187
+ name: channel.name,
3188
+ type: channel.type,
3189
+ position: channel.position ?? 0,
3190
+ parentId: channel.parent_id ?? undefined,
3191
+ topic: channel.topic ?? undefined,
3192
+ nsfw: channel.nsfw ?? undefined,
3193
+ syncedAt: Date.now(),
3194
+ };
3195
+
3196
+ if (existing) {
3197
+ await ctx.db.patch(existing._id, data);
3198
+ } else {
3199
+ await ctx.db.insert("calabasasChannels", data);
3200
+ }
3201
+ return null;
3202
+ },
3203
+ });`;
3204
+ }
3205
+ function generateRoleMutation() {
3206
+ return `export const syncRole = internalMutation({
3207
+ args: {
3208
+ role: roleValidator,
3209
+ operation: v.union(v.literal("upsert"), v.literal("delete")),
3210
+ },
3211
+ returns: v.null(),
3212
+ handler: async (ctx, { role, operation }) => {
3213
+ if (operation === "delete") {
3214
+ const existing = await ctx.db
3215
+ .query("calabasasRoles")
3216
+ .withIndex("by_discord_id", (q) => q.eq("discordId", role.id))
3217
+ .unique();
3218
+ if (existing) await ctx.db.delete(existing._id);
3219
+ return null;
3220
+ }
3221
+
3222
+ const existing = await ctx.db
3223
+ .query("calabasasRoles")
3224
+ .withIndex("by_discord_id", (q) => q.eq("discordId", role.id))
3225
+ .unique();
3226
+
3227
+ const data = {
3228
+ discordId: role.id,
3229
+ guildDiscordId: role.guild_id,
3230
+ name: role.name,
3231
+ color: role.color,
3232
+ position: role.position,
3233
+ permissions: role.permissions,
3234
+ hoist: role.hoist,
3235
+ mentionable: role.mentionable,
3236
+ syncedAt: Date.now(),
3237
+ };
3238
+
3239
+ if (existing) {
3240
+ await ctx.db.patch(existing._id, data);
3241
+ } else {
3242
+ await ctx.db.insert("calabasasRoles", data);
3243
+ }
3244
+ return null;
3245
+ },
3246
+ });`;
3247
+ }
3248
+ function generateMemberMutation() {
3249
+ return `export const syncMember = internalMutation({
3250
+ args: {
3251
+ member: memberValidator,
3252
+ operation: v.union(v.literal("upsert"), v.literal("delete")),
3253
+ },
3254
+ returns: v.null(),
3255
+ handler: async (ctx, { member, operation }) => {
3256
+ if (operation === "delete") {
3257
+ const existing = await ctx.db
3258
+ .query("calabasasMembers")
3259
+ .withIndex("by_user_guild", (q) =>
3260
+ q.eq("discordUserId", member.user.id).eq("guildDiscordId", member.guild_id)
3261
+ )
3262
+ .unique();
3263
+ if (existing) await ctx.db.delete(existing._id);
3264
+ return null;
3265
+ }
3266
+
3267
+ const existing = await ctx.db
3268
+ .query("calabasasMembers")
3269
+ .withIndex("by_user_guild", (q) =>
3270
+ q.eq("discordUserId", member.user.id).eq("guildDiscordId", member.guild_id)
3271
+ )
3272
+ .unique();
3273
+
3274
+ const data = {
3275
+ discordUserId: member.user.id,
3276
+ guildDiscordId: member.guild_id,
3277
+ username: member.user.username,
3278
+ displayName: member.user.global_name ?? undefined,
3279
+ avatar: member.user.avatar ?? undefined,
3280
+ nick: member.nick ?? undefined,
3281
+ roles: member.roles,
3282
+ joinedAt: member.joined_at,
3283
+ bot: member.user.bot ?? undefined,
3284
+ syncedAt: Date.now(),
3285
+ };
3286
+
3287
+ if (existing) {
3288
+ await ctx.db.patch(existing._id, data);
3289
+ } else {
3290
+ await ctx.db.insert("calabasasMembers", data);
3291
+ }
3292
+ return null;
3293
+ },
3294
+ });`;
3295
+ }
3296
+ function generateHttpHandler(type, validator) {
3297
+ return `export const handle${type.charAt(0).toUpperCase() + type.slice(1)}Sync = httpAction(async (ctx, request) => {
3298
+ const body = await request.json();
3299
+ const { secret, ${type}, operation } = body;
3300
+
3301
+ const expectedSecret = process.env.CALABASAS_SECRET;
3302
+ if (!expectedSecret || secret !== expectedSecret) {
3303
+ return new Response(JSON.stringify({ error: "Invalid secret" }), {
3304
+ status: 401,
3305
+ headers: { "Content-Type": "application/json" },
3306
+ });
3307
+ }
3308
+
3309
+ await ctx.runMutation(internal.calabasasSync.sync${type.charAt(0).toUpperCase() + type.slice(1)}, { ${type}, operation });
3310
+
3311
+ return new Response(JSON.stringify({ success: true }), {
3312
+ status: 200,
3313
+ headers: { "Content-Type": "application/json" },
3314
+ });
3315
+ });`;
3316
+ }
3317
+ function generateSyncFile(sync) {
3318
+ const validators = [];
3319
+ const mutations = [];
3320
+ const httpHandlers = [];
3321
+ const routesList = [];
3322
+ if (sync.guilds) {
3323
+ validators.push(`const guildValidator = ${GUILD_VALIDATOR};`);
3324
+ mutations.push(generateGuildMutation());
3325
+ httpHandlers.push(generateHttpHandler("guild", "guildValidator"));
3326
+ routesList.push("guild");
3327
+ }
3328
+ if (sync.channels) {
3329
+ validators.push(`const channelValidator = ${CHANNEL_VALIDATOR};`);
3330
+ mutations.push(generateChannelMutation());
3331
+ httpHandlers.push(generateHttpHandler("channel", "channelValidator"));
3332
+ routesList.push("channel");
3333
+ }
3334
+ if (sync.roles) {
3335
+ validators.push(`const roleValidator = ${ROLE_VALIDATOR};`);
3336
+ mutations.push(generateRoleMutation());
3337
+ httpHandlers.push(generateHttpHandler("role", "roleValidator"));
3338
+ routesList.push("role");
3339
+ }
3340
+ if (sync.members) {
3341
+ validators.push(`const memberValidator = ${MEMBER_VALIDATOR};`);
3342
+ mutations.push(generateMemberMutation());
3343
+ httpHandlers.push(generateHttpHandler("member", "memberValidator"));
3344
+ routesList.push("member");
3345
+ }
3346
+ if (mutations.length === 0) {
3347
+ return `// This file is auto-generated by \`calabasas generate\`
3348
+ // Do not edit manually
3349
+ // No sync handlers configured - enable sync options in calabasas.config.ts
3350
+
3351
+ export {};
3352
+ `;
3353
+ }
3354
+ const routeRegistrations = routesList.map((type) => ` {
3355
+ path: "/api/calabasas/sync/${type}",
3356
+ method: "POST" as const,
3357
+ handler: handle${type.charAt(0).toUpperCase() + type.slice(1)}Sync,
3358
+ }`).join(`,
3359
+ `);
3360
+ return `// This file is auto-generated by \`calabasas generate\`
3361
+ // Do not edit manually
3362
+
3363
+ import { httpAction, internalMutation } from "./_generated/server";
3364
+ import { internal } from "./_generated/api";
3365
+ import { v } from "convex/values";
3366
+
3367
+ // Validators
3368
+ ${validators.join(`
3369
+
3370
+ `)}
3371
+
3372
+ // Internal mutations for syncing data
3373
+ ${mutations.join(`
3374
+
3375
+ `)}
3376
+
3377
+ // HTTP handlers for sync endpoints
3378
+ ${httpHandlers.join(`
3379
+
3380
+ `)}
3381
+
3382
+ /**
3383
+ * Register these routes in your http.ts file:
3384
+ *
3385
+ * import { calabasasSyncRoutes } from "./calabasas.sync.generated";
3386
+ *
3387
+ * // Add routes to your HTTP router
3388
+ * calabasasSyncRoutes.forEach(route => {
3389
+ * http.route(route);
3390
+ * });
3391
+ */
3392
+ export const calabasasSyncRoutes = [
3393
+ ${routeRegistrations}
3394
+ ];
3395
+ `;
3396
+ }
3397
+
3398
+ // src/commands/generate.ts
3399
+ var DISCORD_EVENTS = {
3400
+ messageCreate: {
3401
+ intent: "GuildMessages",
3402
+ fields: {
3403
+ id: "v.string()",
3404
+ channelId: "v.string()",
3405
+ guildId: "v.optional(v.string())",
3406
+ author: "v.object({ id: v.string(), username: v.string(), discriminator: v.string(), avatar: v.optional(v.string()), bot: v.optional(v.boolean()) })",
3407
+ content: "v.string()",
3408
+ timestamp: "v.string()",
3409
+ editedTimestamp: "v.optional(v.string())",
3410
+ tts: "v.boolean()",
3411
+ mentionEveryone: "v.boolean()",
3412
+ attachments: "v.array(v.object({ id: v.string(), filename: v.string(), size: v.number(), url: v.string() }))",
3413
+ embeds: "v.array(v.any())"
3414
+ }
3415
+ },
3416
+ messageUpdate: {
3417
+ intent: "GuildMessages",
3418
+ fields: {
3419
+ id: "v.string()",
3420
+ channelId: "v.string()",
3421
+ guildId: "v.optional(v.string())",
3422
+ content: "v.optional(v.string())",
3423
+ editedTimestamp: "v.optional(v.string())"
3424
+ }
3425
+ },
3426
+ messageDelete: {
3427
+ intent: "GuildMessages",
3428
+ fields: {
3429
+ id: "v.string()",
3430
+ channelId: "v.string()",
3431
+ guildId: "v.optional(v.string())"
3432
+ }
3433
+ },
3434
+ guildMemberAdd: {
3435
+ intent: "GuildMembers",
3436
+ fields: {
3437
+ guildId: "v.string()",
3438
+ user: "v.object({ id: v.string(), username: v.string(), discriminator: v.string(), avatar: v.optional(v.string()) })",
3439
+ nick: "v.optional(v.string())",
3440
+ roles: "v.array(v.string())",
3441
+ joinedAt: "v.string()"
3442
+ }
3443
+ },
3444
+ guildMemberRemove: {
3445
+ intent: "GuildMembers",
3446
+ fields: {
3447
+ guildId: "v.string()",
3448
+ user: "v.object({ id: v.string(), username: v.string(), discriminator: v.string(), avatar: v.optional(v.string()) })"
3449
+ }
3450
+ },
3451
+ guildMemberUpdate: {
3452
+ intent: "GuildMembers",
3453
+ fields: {
3454
+ guildId: "v.string()",
3455
+ user: "v.object({ id: v.string(), username: v.string(), discriminator: v.string(), avatar: v.optional(v.string()) })",
3456
+ nick: "v.optional(v.string())",
3457
+ roles: "v.array(v.string())"
3458
+ }
3459
+ },
3460
+ interactionCreate: {
3461
+ intent: null,
3462
+ fields: {
3463
+ id: "v.string()",
3464
+ applicationId: "v.string()",
3465
+ type: "v.number()",
3466
+ guildId: "v.optional(v.string())",
3467
+ channelId: "v.optional(v.string())",
3468
+ member: "v.optional(v.any())",
3469
+ user: "v.optional(v.object({ id: v.string(), username: v.string(), discriminator: v.string(), avatar: v.optional(v.string()) }))",
3470
+ token: "v.string()",
3471
+ data: "v.optional(v.any())"
3472
+ }
3473
+ },
3474
+ voiceStateUpdate: {
3475
+ intent: "GuildVoiceStates",
3476
+ fields: {
3477
+ guildId: "v.optional(v.string())",
3478
+ channelId: "v.optional(v.string())",
3479
+ userId: "v.string()",
3480
+ sessionId: "v.string()",
3481
+ deaf: "v.boolean()",
3482
+ mute: "v.boolean()",
3483
+ selfDeaf: "v.boolean()",
3484
+ selfMute: "v.boolean()"
3485
+ }
3486
+ },
3487
+ presenceUpdate: {
3488
+ intent: "GuildPresences",
3489
+ fields: {
3490
+ user: "v.object({ id: v.string() })",
3491
+ guildId: "v.string()",
3492
+ status: "v.string()",
3493
+ activities: "v.array(v.any())"
3494
+ }
3495
+ },
3496
+ typingStart: {
3497
+ intent: "GuildMessageTyping",
3498
+ fields: {
3499
+ channelId: "v.string()",
3500
+ guildId: "v.optional(v.string())",
3501
+ userId: "v.string()",
3502
+ timestamp: "v.number()"
3503
+ }
3504
+ },
3505
+ messageReactionAdd: {
3506
+ intent: "GuildMessageReactions",
3507
+ fields: {
3508
+ userId: "v.string()",
3509
+ channelId: "v.string()",
3510
+ messageId: "v.string()",
3511
+ guildId: "v.optional(v.string())",
3512
+ emoji: "v.object({ id: v.optional(v.string()), name: v.optional(v.string()) })"
3513
+ }
3514
+ },
3515
+ messageReactionRemove: {
3516
+ intent: "GuildMessageReactions",
3517
+ fields: {
3518
+ userId: "v.string()",
3519
+ channelId: "v.string()",
3520
+ messageId: "v.string()",
3521
+ guildId: "v.optional(v.string())",
3522
+ emoji: "v.object({ id: v.optional(v.string()), name: v.optional(v.string()) })"
3523
+ }
3524
+ }
3525
+ };
3526
+ function toScreamingSnake(camelCase) {
3527
+ return camelCase.replace(/([A-Z])/g, "_$1").toUpperCase();
3528
+ }
3529
+ function generateValidatorForEvent(eventName, fields) {
3530
+ const event = DISCORD_EVENTS[eventName];
3531
+ const fieldsToUse = fields ?? Object.keys(event.fields);
3532
+ const fieldEntries = fieldsToUse.filter((key) => (key in event.fields)).map((key) => {
3533
+ const validator = event.fields[key];
3534
+ return ` ${key}: ${validator},`;
3535
+ }).join(`
3536
+ `);
3537
+ return `v.object({
3538
+ ${fieldEntries}
3539
+ })`;
3540
+ }
3541
+ function generateTypeForEvent(eventName, fields) {
3542
+ const event = DISCORD_EVENTS[eventName];
3543
+ const fieldsToUse = fields ?? Object.keys(event.fields);
3544
+ const fieldEntries = fieldsToUse.filter((key) => (key in event.fields)).map((key) => {
3545
+ const validator = event.fields[key];
3546
+ const tsType = validatorToType(validator);
3547
+ return ` ${key}: ${tsType};`;
3548
+ }).join(`
3549
+ `);
3550
+ return `{
3551
+ ${fieldEntries}
3552
+ }`;
3553
+ }
3554
+ function validatorToType(validator) {
3555
+ if (validator.startsWith("v.string()"))
3556
+ return "string";
3557
+ if (validator.startsWith("v.number()"))
3558
+ return "number";
3559
+ if (validator.startsWith("v.boolean()"))
3560
+ return "boolean";
3561
+ if (validator.startsWith("v.any()"))
3562
+ return "unknown";
3563
+ if (validator.startsWith("v.optional(")) {
3564
+ const inner = validator.slice(11, -1);
3565
+ return `${validatorToType(inner)} | undefined`;
3566
+ }
3567
+ if (validator.startsWith("v.array(")) {
3568
+ const inner = validator.slice(8, -1);
3569
+ return `Array<${validatorToType(inner)}>`;
3570
+ }
3571
+ if (validator.startsWith("v.object(")) {
3572
+ return "Record<string, unknown>";
3573
+ }
3574
+ return "unknown";
3575
+ }
3576
+ function parseConfigFile2(configPath) {
3577
+ const configContent = fs9.readFileSync(configPath, "utf-8");
3578
+ const syncMatch = configContent.match(/sync:\s*\{([^}]+)\}/);
3579
+ const eventsMatch = configContent.match(/events:\s*\{([^}]+)\}/);
3580
+ const sync = {};
3581
+ const events = {};
3582
+ if (syncMatch) {
3583
+ const syncContent = syncMatch[1];
3584
+ if (/guilds:\s*true/.test(syncContent))
3585
+ sync.guilds = true;
3586
+ if (/channels:\s*true/.test(syncContent))
3587
+ sync.channels = true;
3588
+ if (/roles:\s*true/.test(syncContent))
3589
+ sync.roles = true;
3590
+ if (/members:\s*true/.test(syncContent))
3591
+ sync.members = true;
3592
+ }
3593
+ if (eventsMatch) {
3594
+ const eventsContent = eventsMatch[1];
3595
+ const eventRegex = /(\w+):\s*(?:true|(\[[^\]]+\]))/g;
3596
+ let match;
3597
+ while ((match = eventRegex.exec(eventsContent)) !== null) {
3598
+ const eventName = match[1];
3599
+ if (match[2]) {
3600
+ const fields = match[2].replace(/[\[\]"'\s]/g, "").split(",").filter(Boolean);
3601
+ events[eventName] = fields;
3602
+ } else {
3603
+ events[eventName] = true;
3604
+ }
3605
+ }
3606
+ }
3607
+ return { sync, events };
3608
+ }
3609
+ function generateEventHandlersFile(events) {
3610
+ const configuredEvents = Object.entries(events).filter(([name]) => (name in DISCORD_EVENTS));
3611
+ if (configuredEvents.length === 0) {
3612
+ return generateAllEventsFile();
3613
+ }
3614
+ const eventNames = configuredEvents.map(([name]) => name);
3615
+ const screamingEvents = eventNames.map(toScreamingSnake);
3616
+ const code = `// This file is auto-generated by \`calabasas generate\`
3617
+ // Do not edit manually
3618
+
3619
+ import { mutation } from "./_generated/server";
3620
+ import { v } from "convex/values";
3621
+ import type { MutationCtx } from "./_generated/server";
3622
+
3623
+ // Event type validators
3624
+ ${configuredEvents.map(([name, config]) => {
3625
+ const fields = config === true ? undefined : config;
3626
+ return `const ${name}Validator = ${generateValidatorForEvent(name, fields)};`;
3627
+ }).join(`
3628
+
3629
+ `)}
3630
+
3631
+ // Event type definitions
3632
+ ${configuredEvents.map(([name, config]) => {
3633
+ const fields = config === true ? undefined : config;
3634
+ return `export type ${name.charAt(0).toUpperCase() + name.slice(1)}Event = ${generateTypeForEvent(name, fields)};`;
3635
+ }).join(`
3636
+
3637
+ `)}
3638
+
3639
+ // Union of all event types
3640
+ export type DiscordEventType = ${screamingEvents.map((e) => `"${e}"`).join(" | ")};
3641
+
3642
+ // Event data union
3643
+ export type DiscordEventData = ${eventNames.map((name) => `${name.charAt(0).toUpperCase() + name.slice(1)}Event`).join(" | ")};
3644
+
3645
+ // Handler function types
3646
+ export type EventHandlers = {
3647
+ ${eventNames.map((name) => ` ${name}?: (ctx: MutationCtx, event: ${name.charAt(0).toUpperCase() + name.slice(1)}Event) => Promise<void>;`).join(`
3648
+ `)}
3649
+ };
3650
+
3651
+ // Data validator union
3652
+ const eventDataValidator = v.union(
3653
+ ${eventNames.map((name) => ` ${name}Validator,`).join(`
3654
+ `)}
3655
+ );
3656
+
3657
+ /**
3658
+ * Create a Discord event handler mutation.
3659
+ *
3660
+ * @example
3661
+ * \`\`\`ts
3662
+ * export const receive = handleDiscordEvent({
3663
+ * messageCreate: async (ctx, event) => {
3664
+ * await ctx.db.insert("messages", {
3665
+ * discordId: event.id,
3666
+ * content: event.content,
3667
+ * });
3668
+ * },
3669
+ * });
3670
+ * \`\`\`
3671
+ */
3672
+ export function handleDiscordEvent(handlers: EventHandlers) {
3673
+ return mutation({
3674
+ args: {
3675
+ secret: v.string(),
3676
+ type: v.union(${screamingEvents.map((e) => `v.literal("${e}")`).join(", ")}),
3677
+ data: eventDataValidator,
3678
+ },
3679
+ returns: v.null(),
3680
+ handler: async (ctx, { secret, type, data }) => {
3681
+ // Validate shared secret
3682
+ const expectedSecret = process.env.CALABASAS_SECRET;
3683
+ if (!expectedSecret || secret !== expectedSecret) {
3684
+ throw new Error("Invalid secret");
3685
+ }
3686
+
3687
+ ${eventNames.map((name) => ` if (type === "${toScreamingSnake(name)}" && handlers.${name}) {
3688
+ await handlers.${name}(ctx, data as ${name.charAt(0).toUpperCase() + name.slice(1)}Event);
3689
+ }`).join(`
3690
+
3691
+ `)}
3692
+
3693
+ return null;
3694
+ },
3695
+ });
3696
+ }
3697
+ `;
3698
+ return code;
3699
+ }
3700
+ function generateAllEventsFile() {
3701
+ const eventNames = Object.keys(DISCORD_EVENTS);
3702
+ const screamingEvents = eventNames.map(toScreamingSnake);
3703
+ return `// This file is auto-generated by \`calabasas generate\`
3704
+ // Do not edit manually
3705
+
3706
+ import { mutation } from "./_generated/server";
3707
+ import { v } from "convex/values";
3708
+ import type { MutationCtx } from "./_generated/server";
3709
+
3710
+ // Event type validators
3711
+ ${eventNames.map((name) => `const ${name}Validator = ${generateValidatorForEvent(name)};`).join(`
3712
+
3713
+ `)}
3714
+
3715
+ // Event type definitions
3716
+ ${eventNames.map((name) => `export type ${name.charAt(0).toUpperCase() + name.slice(1)}Event = ${generateTypeForEvent(name)};`).join(`
3717
+
3718
+ `)}
3719
+
3720
+ // Union of all event types
3721
+ export type DiscordEventType = ${screamingEvents.map((e) => `"${e}"`).join(" | ")};
3722
+
3723
+ // Event data union
3724
+ export type DiscordEventData = ${eventNames.map((name) => `${name.charAt(0).toUpperCase() + name.slice(1)}Event`).join(" | ")};
3725
+
3726
+ // Handler function types
3727
+ export type EventHandlers = {
3728
+ ${eventNames.map((name) => ` ${name}?: (ctx: MutationCtx, event: ${name.charAt(0).toUpperCase() + name.slice(1)}Event) => Promise<void>;`).join(`
3729
+ `)}
3730
+ };
3731
+
3732
+ // Data validator union
3733
+ const eventDataValidator = v.union(
3734
+ ${eventNames.map((name) => ` ${name}Validator,`).join(`
3735
+ `)}
3736
+ );
3737
+
3738
+ /**
3739
+ * Create a Discord event handler mutation.
3740
+ *
3741
+ * @example
3742
+ * \`\`\`ts
3743
+ * export const receive = handleDiscordEvent({
3744
+ * messageCreate: async (ctx, event) => {
3745
+ * await ctx.db.insert("messages", {
3746
+ * discordId: event.id,
3747
+ * content: event.content,
3748
+ * });
3749
+ * },
3750
+ * });
3751
+ * \`\`\`
3752
+ */
3753
+ export function handleDiscordEvent(handlers: EventHandlers) {
3754
+ return mutation({
3755
+ args: {
3756
+ secret: v.string(),
3757
+ type: v.union(${screamingEvents.map((e) => `v.literal("${e}")`).join(", ")}),
3758
+ data: eventDataValidator,
3759
+ },
3760
+ returns: v.null(),
3761
+ handler: async (ctx, { secret, type, data }) => {
3762
+ // Validate shared secret
3763
+ const expectedSecret = process.env.CALABASAS_SECRET;
3764
+ if (!expectedSecret || secret !== expectedSecret) {
3765
+ throw new Error("Invalid secret");
3766
+ }
3767
+
3768
+ ${eventNames.map((name) => ` if (type === "${toScreamingSnake(name)}" && handlers.${name}) {
3769
+ await handlers.${name}(ctx, data as ${name.charAt(0).toUpperCase() + name.slice(1)}Event);
3770
+ }`).join(`
3771
+
3772
+ `)}
3773
+
3774
+ return null;
3775
+ },
3776
+ });
3777
+ }
3778
+ `;
3779
+ }
3780
+ async function generate(options) {
3781
+ const convexDir = path5.resolve(process.cwd(), "convex");
3782
+ const configPath = path5.join(convexDir, "calabasas.config.ts");
3783
+ let config = { sync: {}, events: {} };
3784
+ if (fs9.existsSync(configPath)) {
3785
+ console.log("Reading convex/calabasas.config.ts...");
3786
+ config = parseConfigFile2(configPath);
3787
+ } else {
3788
+ console.log("No calabasas.config.ts found, generating with default settings...");
3789
+ console.log("Run `calabasas init` to create a config file.");
3790
+ console.log("");
3791
+ }
3792
+ const eventHandlersPath = path5.resolve(process.cwd(), options.output);
3793
+ const eventHandlersCode = generateEventHandlersFile(config.events ?? {});
3794
+ fs9.writeFileSync(eventHandlersPath, eventHandlersCode);
3795
+ console.log(`Generated ${options.output}`);
3796
+ const syncConfig = config.sync ?? {};
3797
+ const hasSyncEnabled = syncConfig.guilds || syncConfig.channels || syncConfig.roles || syncConfig.members;
3798
+ if (hasSyncEnabled) {
3799
+ const schemaPath = path5.join(convexDir, "calabasas.schema.generated.ts");
3800
+ const schemaCode = generateSchemaFile(syncConfig);
3801
+ fs9.writeFileSync(schemaPath, schemaCode);
3802
+ console.log("Generated convex/calabasas.schema.generated.ts");
3803
+ const syncPath = path5.join(convexDir, "calabasas.sync.generated.ts");
3804
+ const syncCode = generateSyncFile(syncConfig);
3805
+ fs9.writeFileSync(syncPath, syncCode);
3806
+ console.log("Generated convex/calabasas.sync.generated.ts");
3807
+ }
3808
+ console.log("");
3809
+ console.log("Next steps:");
3810
+ if (hasSyncEnabled) {
3811
+ console.log("1. Add tables to your convex/schema.ts:");
3812
+ console.log(' import { calabasasTables } from "./calabasas.schema.generated";');
3813
+ console.log("");
3814
+ console.log(" export default defineSchema({");
3815
+ console.log(" ...calabasasTables,");
3816
+ console.log(" // your other tables...");
3817
+ console.log(" });");
3818
+ console.log("");
3819
+ console.log("2. Register sync routes in your convex/http.ts:");
3820
+ console.log(' import { calabasasSyncRoutes } from "./calabasas.sync.generated";');
3821
+ console.log("");
3822
+ console.log(" calabasasSyncRoutes.forEach(route => http.route(route));");
3823
+ console.log("");
3824
+ console.log("3. Add CALABASAS_SECRET to your Convex environment variables");
3825
+ console.log("4. Create your event handler in convex/discord.ts");
3826
+ console.log("5. Run `calabasas push` to sync config with Calabasas");
3827
+ } else {
3828
+ console.log("1. Add CALABASAS_SECRET to your Convex environment variables");
3829
+ console.log("2. Create your handler in convex/discord.ts:");
3830
+ console.log("");
3831
+ console.log(' import { handleDiscordEvent } from "./discord.generated";');
3832
+ console.log("");
3833
+ console.log(" export const receive = handleDiscordEvent({");
3834
+ console.log(" messageCreate: async (ctx, event) => {");
3835
+ console.log(" console.log('New message:', event.content);");
3836
+ console.log(" },");
3837
+ console.log(" });");
3838
+ }
3839
+ }
3840
+
3841
+ // src/commands/bot.ts
3842
+ import * as readline from "readline";
3843
+ var INTENTS = [
3844
+ { name: "Guilds", value: 1 << 0, description: "Guild create/update/delete, roles, channels" },
3845
+ { name: "Guild Members", value: 1 << 1, description: "Member add/remove/update (Privileged)", privileged: true },
3846
+ { name: "Guild Moderation", value: 1 << 2, description: "Bans, audit log" },
3847
+ { name: "Guild Emojis", value: 1 << 3, description: "Emoji/sticker updates" },
3848
+ { name: "Guild Integrations", value: 1 << 4, description: "Integration updates" },
3849
+ { name: "Guild Webhooks", value: 1 << 5, description: "Webhook updates" },
3850
+ { name: "Guild Invites", value: 1 << 6, description: "Invite create/delete" },
3851
+ { name: "Guild Voice States", value: 1 << 7, description: "Voice state updates" },
3852
+ { name: "Guild Presences", value: 1 << 8, description: "Presence updates (Privileged)", privileged: true },
3853
+ { name: "Guild Messages", value: 1 << 9, description: "Message create/update/delete in guilds" },
3854
+ { name: "Guild Message Reactions", value: 1 << 10, description: "Reaction add/remove in guilds" },
3855
+ { name: "Guild Message Typing", value: 1 << 11, description: "Typing start in guilds" },
3856
+ { name: "Direct Messages", value: 1 << 12, description: "DM create/update/delete" },
3857
+ { name: "Direct Message Reactions", value: 1 << 13, description: "Reaction add/remove in DMs" },
3858
+ { name: "Direct Message Typing", value: 1 << 14, description: "Typing start in DMs" },
3859
+ { name: "Message Content", value: 1 << 15, description: "Access message content (Privileged)", privileged: true },
3860
+ { name: "Guild Scheduled Events", value: 1 << 16, description: "Scheduled event updates" },
3861
+ { name: "Auto Moderation Config", value: 1 << 20, description: "Auto mod rule changes" },
3862
+ { name: "Auto Moderation Execution", value: 1 << 21, description: "Auto mod action execution" }
3863
+ ];
3864
+ function encrypt(text, key) {
3865
+ const keyBytes = new TextEncoder().encode(key);
3866
+ const textBytes = new TextEncoder().encode(text);
3867
+ const encrypted = new Uint8Array(textBytes.length);
3868
+ for (let i = 0;i < textBytes.length; i++) {
3869
+ encrypted[i] = textBytes[i] ^ keyBytes[i % keyBytes.length];
3870
+ }
3871
+ return btoa(String.fromCharCode(...encrypted));
3872
+ }
3873
+ function generateSecret() {
3874
+ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
3875
+ let result = "";
3876
+ for (let i = 0;i < 32; i++) {
3877
+ result += chars.charAt(Math.floor(Math.random() * chars.length));
3878
+ }
3879
+ return result;
3880
+ }
3881
+ function prompt(question) {
3882
+ const rl = readline.createInterface({
3883
+ input: process.stdin,
3884
+ output: process.stdout
3885
+ });
3886
+ return new Promise((resolve4) => {
3887
+ rl.question(question, (answer) => {
3888
+ rl.close();
3889
+ resolve4(answer);
3890
+ });
3891
+ });
3892
+ }
3893
+ async function selectIntents() {
3894
+ const selected = new Set([0, 9]);
3895
+ let cursor = 0;
3896
+ return new Promise((resolve4) => {
3897
+ const render = () => {
3898
+ process.stdout.write("\x1B[2J\x1B[H");
3899
+ console.log(`Select Gateway Intents (use arrow keys, space to toggle, enter to confirm)
3900
+ `);
3901
+ console.log(` ⚠️ Privileged intents require approval in Discord Developer Portal
3902
+ `);
3903
+ INTENTS.forEach((intent, i) => {
3904
+ const isSelected = selected.has(i);
3905
+ const isCursor = cursor === i;
3906
+ const checkbox = isSelected ? "[✓]" : "[ ]";
3907
+ const pointer = isCursor ? "→" : " ";
3908
+ const privilegedTag = intent.privileged ? " (Privileged)" : "";
3909
+ const color = intent.privileged ? "\x1B[33m" : "";
3910
+ const reset = "\x1B[0m";
3911
+ console.log(`${pointer} ${checkbox} ${color}${intent.name}${privilegedTag}${reset}`);
3912
+ if (isCursor) {
3913
+ console.log(` ${intent.description}`);
3914
+ }
3915
+ });
3916
+ const total = Array.from(selected).reduce((sum, i) => sum + INTENTS[i].value, 0);
3917
+ console.log(`
3918
+ Selected intents value: ${total}`);
3919
+ };
3920
+ render();
3921
+ if (process.stdin.isTTY) {
3922
+ process.stdin.setRawMode(true);
3923
+ }
3924
+ process.stdin.resume();
3925
+ process.stdin.setEncoding("utf8");
3926
+ const onKeypress = (key) => {
3927
+ if (key === "\x03") {
3928
+ process.stdin.setRawMode(false);
3929
+ process.exit(0);
3930
+ }
3931
+ if (key === "\r" || key === `
3932
+ `) {
3933
+ process.stdin.removeListener("data", onKeypress);
3934
+ if (process.stdin.isTTY) {
3935
+ process.stdin.setRawMode(false);
3936
+ }
3937
+ const total = Array.from(selected).reduce((sum, i) => sum + INTENTS[i].value, 0);
3938
+ console.log(`
3939
+ `);
3940
+ resolve4(total);
3941
+ return;
3942
+ }
3943
+ if (key === " ") {
3944
+ if (selected.has(cursor)) {
3945
+ selected.delete(cursor);
3946
+ } else {
3947
+ selected.add(cursor);
3948
+ }
3949
+ render();
3950
+ return;
3951
+ }
3952
+ if (key === "\x1B[A" || key === "k") {
3953
+ cursor = Math.max(0, cursor - 1);
3954
+ render();
3955
+ return;
3956
+ }
3957
+ if (key === "\x1B[B" || key === "j") {
3958
+ cursor = Math.min(INTENTS.length - 1, cursor + 1);
3959
+ render();
3960
+ return;
3961
+ }
3962
+ };
3963
+ process.stdin.on("data", onKeypress);
3964
+ });
3965
+ }
3966
+ async function apiRequest(method, path6, body) {
3967
+ const config = getConfig();
3968
+ const url = `${getApiUrl()}${path6}`;
3969
+ const response = await fetch(url, {
3970
+ method,
3971
+ headers: {
3972
+ "Content-Type": "application/json",
3973
+ Authorization: `Bearer ${config.apiKey}`
3974
+ },
3975
+ body: body ? JSON.stringify(body) : undefined
3976
+ });
3977
+ const data = await response.json();
3978
+ return { ok: response.ok, data, status: response.status };
3979
+ }
3980
+ async function botAdd() {
3981
+ if (!isAuthenticated()) {
3982
+ console.log("Not logged in. Run `calabasas login` first.");
3983
+ return;
3984
+ }
3985
+ console.log(`Add a new Discord bot
3986
+ `);
3987
+ const name = await prompt("Bot name: ");
3988
+ if (!name.trim()) {
3989
+ console.log("Bot name is required.");
3990
+ return;
3991
+ }
3992
+ const discordAppId = await prompt("Discord Application ID: ");
3993
+ if (!discordAppId.trim()) {
3994
+ console.log("Discord Application ID is required.");
3995
+ return;
3996
+ }
3997
+ const token = await prompt("Bot token: ");
3998
+ if (!token.trim()) {
3999
+ console.log("Bot token is required.");
4000
+ return;
4001
+ }
4002
+ const convexUrl = await prompt("Your Convex URL (e.g., https://your-project.convex.cloud): ");
4003
+ if (!convexUrl.trim()) {
4004
+ console.log("Convex URL is required.");
4005
+ return;
4006
+ }
4007
+ const intents = await selectIntents();
4008
+ console.log("Creating bot...");
4009
+ const encryptionKey = "calabasas-dev-key-change-in-production";
4010
+ const encryptedToken = encrypt(token, encryptionKey);
4011
+ const sharedSecret = generateSecret();
4012
+ const { ok, data, status } = await apiRequest("POST", "/api/cli/bots", {
4013
+ name: name.trim(),
4014
+ discordAppId: discordAppId.trim(),
4015
+ encryptedToken,
4016
+ intents,
4017
+ convexUrl: convexUrl.trim(),
4018
+ sharedSecret
4019
+ });
4020
+ if (!ok) {
4021
+ console.log(`
4022
+ Failed to create bot: ${data.error || `HTTP ${status}`}`);
4023
+ return;
4024
+ }
4025
+ console.log(`
4026
+ ✅ Bot created successfully!`);
4027
+ console.log("");
4028
+ console.log("Shared secret (add this to your Convex env as CALABASAS_SECRET):");
4029
+ console.log(` ${sharedSecret}`);
4030
+ console.log("");
4031
+ console.log("Next steps:");
4032
+ console.log("1. Run `calabasas generate` to generate the event handler");
4033
+ console.log("2. Create your handler in convex/discord.ts");
4034
+ console.log("3. Add CALABASAS_SECRET to your Convex environment variables");
4035
+ }
4036
+ async function botList() {
4037
+ if (!isAuthenticated()) {
4038
+ console.log("Not logged in. Run `calabasas login` first.");
4039
+ return;
4040
+ }
4041
+ const { ok, data, status } = await apiRequest("GET", "/api/cli/bots");
4042
+ if (!ok) {
4043
+ console.log(`Failed to list bots: ${data.error || `HTTP ${status}`}`);
4044
+ return;
4045
+ }
4046
+ const bots = data.bots;
4047
+ if (bots.length === 0) {
4048
+ console.log("No bots found. Run `calabasas bot add` to create one.");
4049
+ return;
4050
+ }
4051
+ console.log(`Your Discord bots:
4052
+ `);
4053
+ for (const bot of bots) {
4054
+ const statusIcon = bot.status === "connected" ? "\uD83D\uDFE2" : bot.status === "connecting" ? "\uD83D\uDFE1" : bot.status === "error" ? "\uD83D\uDD34" : "⚪";
4055
+ console.log(`${statusIcon} ${bot.name}`);
4056
+ console.log(` ID: ${bot._id}`);
4057
+ console.log(` Discord App: ${bot.discordAppId}`);
4058
+ console.log(` Status: ${bot.status}`);
4059
+ console.log(` Convex: ${bot.convexUrl}`);
4060
+ console.log("");
4061
+ }
4062
+ }
4063
+ async function botRemove(botId) {
4064
+ if (!isAuthenticated()) {
4065
+ console.log("Not logged in. Run `calabasas login` first.");
4066
+ return;
4067
+ }
4068
+ const confirm = await prompt(`Are you sure you want to remove bot ${botId}? (y/N): `);
4069
+ if (confirm.toLowerCase() !== "y") {
4070
+ console.log("Cancelled.");
4071
+ return;
4072
+ }
4073
+ const { ok, data, status } = await apiRequest("DELETE", `/api/cli/bots?id=${botId}`);
4074
+ if (!ok) {
4075
+ console.log(`Failed to remove bot: ${data.error || `HTTP ${status}`}`);
4076
+ return;
4077
+ }
4078
+ console.log("✅ Bot removed successfully!");
4079
+ }
4080
+ async function botEdit(botId, options) {
4081
+ if (!isAuthenticated()) {
4082
+ console.log("Not logged in. Run `calabasas login` first.");
4083
+ return;
4084
+ }
4085
+ const updates = { botId };
4086
+ if (options.name) {
4087
+ updates.name = options.name;
4088
+ }
4089
+ if (options.url) {
4090
+ updates.convexUrl = options.url;
4091
+ }
4092
+ if (options.token) {
4093
+ const encryptionKey = "calabasas-dev-key-change-in-production";
4094
+ updates.encryptedToken = encrypt(options.token, encryptionKey);
4095
+ }
4096
+ if (options.intents) {
4097
+ updates.intents = parseInt(options.intents, 10);
4098
+ }
4099
+ if (Object.keys(updates).length === 1) {
4100
+ console.log("No updates specified. Use --name, --url, --token, or --intents");
4101
+ console.log("");
4102
+ console.log("Examples:");
4103
+ console.log(' calabasas bot edit <id> --name "New Name"');
4104
+ console.log(" calabasas bot edit <id> --url https://new-project.convex.cloud");
4105
+ console.log(" calabasas bot edit <id> --token <new-token>");
4106
+ console.log(" calabasas bot edit <id> --intents 513");
4107
+ return;
4108
+ }
4109
+ console.log("Updating bot...");
4110
+ const { ok, data, status } = await apiRequest("PATCH", "/api/cli/bots", updates);
4111
+ if (!ok) {
4112
+ console.log(`Failed to update bot: ${data.error || `HTTP ${status}`}`);
4113
+ return;
4114
+ }
4115
+ console.log("✅ Bot updated successfully!");
4116
+ console.log("");
4117
+ console.log("Note: The Gateway will automatically reconnect with the new settings.");
4118
+ }
4119
+
4120
+ // src/index.ts
4121
+ var program2 = new Command;
4122
+ program2.name("calabasas").description("CLI for Calabasas - Discord Gateway as a Service for Convex").version("0.0.1");
4123
+ program2.command("login").description("Authenticate with Calabasas using your API key").action(login);
4124
+ program2.command("logout").description("Clear stored credentials").action(logout);
4125
+ program2.command("init").description("Initialize Calabasas config in your Convex project").action(init);
4126
+ program2.command("push").description("Push your calabasas.config.ts to Calabasas").option("-c, --config <path>", "Path to config file", "convex/calabasas.config.ts").option("-b, --bot <botId>", "Bot ID to configure (prompts if not specified)").action(push);
4127
+ program2.command("generate").description("Generate discord.generated.ts with type-safe handlers").option("-o, --output <path>", "Output path", "convex/discord.generated.ts").action(generate);
4128
+ var bot = program2.command("bot").description("Manage Discord bots");
4129
+ bot.command("add").description("Add a new Discord bot").action(botAdd);
4130
+ bot.command("list").alias("ls").description("List your Discord bots").action(botList);
4131
+ bot.command("remove <botId>").alias("rm").description("Remove a Discord bot").action(botRemove);
4132
+ bot.command("edit <botId>").description("Edit a Discord bot").option("-n, --name <name>", "New bot name").option("-u, --url <url>", "New Convex URL").option("-t, --token <token>", "New bot token").option("-i, --intents <intents>", "New intents value").action(botEdit);
4133
+ program2.parse();