@hasna/todos 0.11.38 → 0.11.39

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 (53) hide show
  1. package/README.md +1 -11
  2. package/dist/capabilities.d.ts +32 -0
  3. package/dist/capabilities.d.ts.map +1 -0
  4. package/dist/cli/commands/cloud-commands.d.ts.map +1 -1
  5. package/dist/cli/commands/remote-commands.d.ts +3 -0
  6. package/dist/cli/commands/remote-commands.d.ts.map +1 -0
  7. package/dist/cli/index.js +16830 -29235
  8. package/dist/cli/remote-index.d.ts +3 -0
  9. package/dist/cli/remote-index.d.ts.map +1 -0
  10. package/dist/contracts.d.ts +58 -0
  11. package/dist/contracts.d.ts.map +1 -0
  12. package/dist/contracts.js +823 -0
  13. package/dist/db/builtin-templates.d.ts.map +1 -1
  14. package/dist/index.d.ts +14 -4
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +4712 -13636
  17. package/dist/json-contracts.d.ts +56 -0
  18. package/dist/json-contracts.d.ts.map +1 -0
  19. package/dist/lib/cloud-migration.d.ts +53 -0
  20. package/dist/lib/cloud-migration.d.ts.map +1 -0
  21. package/dist/lib/config.d.ts +17 -0
  22. package/dist/lib/config.d.ts.map +1 -1
  23. package/dist/lib/logger.d.ts +6 -12
  24. package/dist/lib/logger.d.ts.map +1 -1
  25. package/dist/mcp/index.d.ts.map +1 -1
  26. package/dist/mcp/index.js +4435 -16629
  27. package/dist/mcp/token-utils.d.ts.map +1 -1
  28. package/dist/mcp/tools/code-tools.d.ts.map +1 -1
  29. package/dist/mcp/tools/task-meta-tools.d.ts.map +1 -1
  30. package/dist/mcp.d.ts +42 -0
  31. package/dist/mcp.d.ts.map +1 -0
  32. package/dist/mcp.js +359 -0
  33. package/dist/registry.d.ts +33 -0
  34. package/dist/registry.d.ts.map +1 -0
  35. package/dist/registry.js +1382 -0
  36. package/dist/remote-cli/remote-index.js +3054 -0
  37. package/dist/remote.d.ts +5 -0
  38. package/dist/remote.d.ts.map +1 -0
  39. package/dist/remote.js +770 -0
  40. package/dist/sdk/client.d.ts +1 -1
  41. package/dist/sdk/client.d.ts.map +1 -1
  42. package/dist/sdk/index.js +635 -0
  43. package/dist/server/index.js +4 -4
  44. package/dist/storage/index.d.ts +4 -0
  45. package/dist/storage/index.d.ts.map +1 -0
  46. package/dist/storage/interfaces.d.ts +185 -0
  47. package/dist/storage/interfaces.d.ts.map +1 -0
  48. package/dist/storage/local-sqlite.d.ts +7 -0
  49. package/dist/storage/local-sqlite.d.ts.map +1 -0
  50. package/dist/storage.d.ts +4 -0
  51. package/dist/storage.d.ts.map +1 -0
  52. package/dist/storage.js +4984 -0
  53. package/package.json +22 -4
@@ -0,0 +1,3054 @@
1
+ #!/usr/bin/env bun
2
+ // @bun
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
+ function __accessProp(key) {
9
+ return this[key];
10
+ }
11
+ var __toESMCache_node;
12
+ var __toESMCache_esm;
13
+ var __toESM = (mod, isNodeMode, target) => {
14
+ var canCache = mod != null && typeof mod === "object";
15
+ if (canCache) {
16
+ var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
17
+ var cached = cache.get(mod);
18
+ if (cached)
19
+ return cached;
20
+ }
21
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
22
+ const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
23
+ for (let key of __getOwnPropNames(mod))
24
+ if (!__hasOwnProp.call(to, key))
25
+ __defProp(to, key, {
26
+ get: __accessProp.bind(mod, key),
27
+ enumerable: true
28
+ });
29
+ if (canCache)
30
+ cache.set(mod, to);
31
+ return to;
32
+ };
33
+ var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
34
+ var __require = import.meta.require;
35
+
36
+ // node_modules/commander/lib/error.js
37
+ var require_error = __commonJS((exports) => {
38
+ class CommanderError extends Error {
39
+ constructor(exitCode, code, message) {
40
+ super(message);
41
+ Error.captureStackTrace(this, this.constructor);
42
+ this.name = this.constructor.name;
43
+ this.code = code;
44
+ this.exitCode = exitCode;
45
+ this.nestedError = undefined;
46
+ }
47
+ }
48
+
49
+ class InvalidArgumentError extends CommanderError {
50
+ constructor(message) {
51
+ super(1, "commander.invalidArgument", message);
52
+ Error.captureStackTrace(this, this.constructor);
53
+ this.name = this.constructor.name;
54
+ }
55
+ }
56
+ exports.CommanderError = CommanderError;
57
+ exports.InvalidArgumentError = InvalidArgumentError;
58
+ });
59
+
60
+ // node_modules/commander/lib/argument.js
61
+ var require_argument = __commonJS((exports) => {
62
+ var { InvalidArgumentError } = require_error();
63
+
64
+ class Argument {
65
+ constructor(name, description) {
66
+ this.description = description || "";
67
+ this.variadic = false;
68
+ this.parseArg = undefined;
69
+ this.defaultValue = undefined;
70
+ this.defaultValueDescription = undefined;
71
+ this.argChoices = undefined;
72
+ switch (name[0]) {
73
+ case "<":
74
+ this.required = true;
75
+ this._name = name.slice(1, -1);
76
+ break;
77
+ case "[":
78
+ this.required = false;
79
+ this._name = name.slice(1, -1);
80
+ break;
81
+ default:
82
+ this.required = true;
83
+ this._name = name;
84
+ break;
85
+ }
86
+ if (this._name.length > 3 && this._name.slice(-3) === "...") {
87
+ this.variadic = true;
88
+ this._name = this._name.slice(0, -3);
89
+ }
90
+ }
91
+ name() {
92
+ return this._name;
93
+ }
94
+ _concatValue(value, previous) {
95
+ if (previous === this.defaultValue || !Array.isArray(previous)) {
96
+ return [value];
97
+ }
98
+ return previous.concat(value);
99
+ }
100
+ default(value, description) {
101
+ this.defaultValue = value;
102
+ this.defaultValueDescription = description;
103
+ return this;
104
+ }
105
+ argParser(fn) {
106
+ this.parseArg = fn;
107
+ return this;
108
+ }
109
+ choices(values) {
110
+ this.argChoices = values.slice();
111
+ this.parseArg = (arg, previous) => {
112
+ if (!this.argChoices.includes(arg)) {
113
+ throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
114
+ }
115
+ if (this.variadic) {
116
+ return this._concatValue(arg, previous);
117
+ }
118
+ return arg;
119
+ };
120
+ return this;
121
+ }
122
+ argRequired() {
123
+ this.required = true;
124
+ return this;
125
+ }
126
+ argOptional() {
127
+ this.required = false;
128
+ return this;
129
+ }
130
+ }
131
+ function humanReadableArgName(arg) {
132
+ const nameOutput = arg.name() + (arg.variadic === true ? "..." : "");
133
+ return arg.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
134
+ }
135
+ exports.Argument = Argument;
136
+ exports.humanReadableArgName = humanReadableArgName;
137
+ });
138
+
139
+ // node_modules/commander/lib/help.js
140
+ var require_help = __commonJS((exports) => {
141
+ var { humanReadableArgName } = require_argument();
142
+
143
+ class Help {
144
+ constructor() {
145
+ this.helpWidth = undefined;
146
+ this.minWidthToWrap = 40;
147
+ this.sortSubcommands = false;
148
+ this.sortOptions = false;
149
+ this.showGlobalOptions = false;
150
+ }
151
+ prepareContext(contextOptions) {
152
+ this.helpWidth = this.helpWidth ?? contextOptions.helpWidth ?? 80;
153
+ }
154
+ visibleCommands(cmd) {
155
+ const visibleCommands = cmd.commands.filter((cmd2) => !cmd2._hidden);
156
+ const helpCommand = cmd._getHelpCommand();
157
+ if (helpCommand && !helpCommand._hidden) {
158
+ visibleCommands.push(helpCommand);
159
+ }
160
+ if (this.sortSubcommands) {
161
+ visibleCommands.sort((a, b) => {
162
+ return a.name().localeCompare(b.name());
163
+ });
164
+ }
165
+ return visibleCommands;
166
+ }
167
+ compareOptions(a, b) {
168
+ const getSortKey = (option) => {
169
+ return option.short ? option.short.replace(/^-/, "") : option.long.replace(/^--/, "");
170
+ };
171
+ return getSortKey(a).localeCompare(getSortKey(b));
172
+ }
173
+ visibleOptions(cmd) {
174
+ const visibleOptions = cmd.options.filter((option) => !option.hidden);
175
+ const helpOption = cmd._getHelpOption();
176
+ if (helpOption && !helpOption.hidden) {
177
+ const removeShort = helpOption.short && cmd._findOption(helpOption.short);
178
+ const removeLong = helpOption.long && cmd._findOption(helpOption.long);
179
+ if (!removeShort && !removeLong) {
180
+ visibleOptions.push(helpOption);
181
+ } else if (helpOption.long && !removeLong) {
182
+ visibleOptions.push(cmd.createOption(helpOption.long, helpOption.description));
183
+ } else if (helpOption.short && !removeShort) {
184
+ visibleOptions.push(cmd.createOption(helpOption.short, helpOption.description));
185
+ }
186
+ }
187
+ if (this.sortOptions) {
188
+ visibleOptions.sort(this.compareOptions);
189
+ }
190
+ return visibleOptions;
191
+ }
192
+ visibleGlobalOptions(cmd) {
193
+ if (!this.showGlobalOptions)
194
+ return [];
195
+ const globalOptions = [];
196
+ for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
197
+ const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
198
+ globalOptions.push(...visibleOptions);
199
+ }
200
+ if (this.sortOptions) {
201
+ globalOptions.sort(this.compareOptions);
202
+ }
203
+ return globalOptions;
204
+ }
205
+ visibleArguments(cmd) {
206
+ if (cmd._argsDescription) {
207
+ cmd.registeredArguments.forEach((argument) => {
208
+ argument.description = argument.description || cmd._argsDescription[argument.name()] || "";
209
+ });
210
+ }
211
+ if (cmd.registeredArguments.find((argument) => argument.description)) {
212
+ return cmd.registeredArguments;
213
+ }
214
+ return [];
215
+ }
216
+ subcommandTerm(cmd) {
217
+ const args = cmd.registeredArguments.map((arg) => humanReadableArgName(arg)).join(" ");
218
+ return cmd._name + (cmd._aliases[0] ? "|" + cmd._aliases[0] : "") + (cmd.options.length ? " [options]" : "") + (args ? " " + args : "");
219
+ }
220
+ optionTerm(option) {
221
+ return option.flags;
222
+ }
223
+ argumentTerm(argument) {
224
+ return argument.name();
225
+ }
226
+ longestSubcommandTermLength(cmd, helper) {
227
+ return helper.visibleCommands(cmd).reduce((max, command) => {
228
+ return Math.max(max, this.displayWidth(helper.styleSubcommandTerm(helper.subcommandTerm(command))));
229
+ }, 0);
230
+ }
231
+ longestOptionTermLength(cmd, helper) {
232
+ return helper.visibleOptions(cmd).reduce((max, option) => {
233
+ return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
234
+ }, 0);
235
+ }
236
+ longestGlobalOptionTermLength(cmd, helper) {
237
+ return helper.visibleGlobalOptions(cmd).reduce((max, option) => {
238
+ return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
239
+ }, 0);
240
+ }
241
+ longestArgumentTermLength(cmd, helper) {
242
+ return helper.visibleArguments(cmd).reduce((max, argument) => {
243
+ return Math.max(max, this.displayWidth(helper.styleArgumentTerm(helper.argumentTerm(argument))));
244
+ }, 0);
245
+ }
246
+ commandUsage(cmd) {
247
+ let cmdName = cmd._name;
248
+ if (cmd._aliases[0]) {
249
+ cmdName = cmdName + "|" + cmd._aliases[0];
250
+ }
251
+ let ancestorCmdNames = "";
252
+ for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
253
+ ancestorCmdNames = ancestorCmd.name() + " " + ancestorCmdNames;
254
+ }
255
+ return ancestorCmdNames + cmdName + " " + cmd.usage();
256
+ }
257
+ commandDescription(cmd) {
258
+ return cmd.description();
259
+ }
260
+ subcommandDescription(cmd) {
261
+ return cmd.summary() || cmd.description();
262
+ }
263
+ optionDescription(option) {
264
+ const extraInfo = [];
265
+ if (option.argChoices) {
266
+ extraInfo.push(`choices: ${option.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
267
+ }
268
+ if (option.defaultValue !== undefined) {
269
+ const showDefault = option.required || option.optional || option.isBoolean() && typeof option.defaultValue === "boolean";
270
+ if (showDefault) {
271
+ extraInfo.push(`default: ${option.defaultValueDescription || JSON.stringify(option.defaultValue)}`);
272
+ }
273
+ }
274
+ if (option.presetArg !== undefined && option.optional) {
275
+ extraInfo.push(`preset: ${JSON.stringify(option.presetArg)}`);
276
+ }
277
+ if (option.envVar !== undefined) {
278
+ extraInfo.push(`env: ${option.envVar}`);
279
+ }
280
+ if (extraInfo.length > 0) {
281
+ return `${option.description} (${extraInfo.join(", ")})`;
282
+ }
283
+ return option.description;
284
+ }
285
+ argumentDescription(argument) {
286
+ const extraInfo = [];
287
+ if (argument.argChoices) {
288
+ extraInfo.push(`choices: ${argument.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
289
+ }
290
+ if (argument.defaultValue !== undefined) {
291
+ extraInfo.push(`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`);
292
+ }
293
+ if (extraInfo.length > 0) {
294
+ const extraDescription = `(${extraInfo.join(", ")})`;
295
+ if (argument.description) {
296
+ return `${argument.description} ${extraDescription}`;
297
+ }
298
+ return extraDescription;
299
+ }
300
+ return argument.description;
301
+ }
302
+ formatHelp(cmd, helper) {
303
+ const termWidth = helper.padWidth(cmd, helper);
304
+ const helpWidth = helper.helpWidth ?? 80;
305
+ function callFormatItem(term, description) {
306
+ return helper.formatItem(term, termWidth, description, helper);
307
+ }
308
+ let output = [
309
+ `${helper.styleTitle("Usage:")} ${helper.styleUsage(helper.commandUsage(cmd))}`,
310
+ ""
311
+ ];
312
+ const commandDescription = helper.commandDescription(cmd);
313
+ if (commandDescription.length > 0) {
314
+ output = output.concat([
315
+ helper.boxWrap(helper.styleCommandDescription(commandDescription), helpWidth),
316
+ ""
317
+ ]);
318
+ }
319
+ const argumentList = helper.visibleArguments(cmd).map((argument) => {
320
+ return callFormatItem(helper.styleArgumentTerm(helper.argumentTerm(argument)), helper.styleArgumentDescription(helper.argumentDescription(argument)));
321
+ });
322
+ if (argumentList.length > 0) {
323
+ output = output.concat([
324
+ helper.styleTitle("Arguments:"),
325
+ ...argumentList,
326
+ ""
327
+ ]);
328
+ }
329
+ const optionList = helper.visibleOptions(cmd).map((option) => {
330
+ return callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option)));
331
+ });
332
+ if (optionList.length > 0) {
333
+ output = output.concat([
334
+ helper.styleTitle("Options:"),
335
+ ...optionList,
336
+ ""
337
+ ]);
338
+ }
339
+ if (helper.showGlobalOptions) {
340
+ const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
341
+ return callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option)));
342
+ });
343
+ if (globalOptionList.length > 0) {
344
+ output = output.concat([
345
+ helper.styleTitle("Global Options:"),
346
+ ...globalOptionList,
347
+ ""
348
+ ]);
349
+ }
350
+ }
351
+ const commandList = helper.visibleCommands(cmd).map((cmd2) => {
352
+ return callFormatItem(helper.styleSubcommandTerm(helper.subcommandTerm(cmd2)), helper.styleSubcommandDescription(helper.subcommandDescription(cmd2)));
353
+ });
354
+ if (commandList.length > 0) {
355
+ output = output.concat([
356
+ helper.styleTitle("Commands:"),
357
+ ...commandList,
358
+ ""
359
+ ]);
360
+ }
361
+ return output.join(`
362
+ `);
363
+ }
364
+ displayWidth(str) {
365
+ return stripColor(str).length;
366
+ }
367
+ styleTitle(str) {
368
+ return str;
369
+ }
370
+ styleUsage(str) {
371
+ return str.split(" ").map((word) => {
372
+ if (word === "[options]")
373
+ return this.styleOptionText(word);
374
+ if (word === "[command]")
375
+ return this.styleSubcommandText(word);
376
+ if (word[0] === "[" || word[0] === "<")
377
+ return this.styleArgumentText(word);
378
+ return this.styleCommandText(word);
379
+ }).join(" ");
380
+ }
381
+ styleCommandDescription(str) {
382
+ return this.styleDescriptionText(str);
383
+ }
384
+ styleOptionDescription(str) {
385
+ return this.styleDescriptionText(str);
386
+ }
387
+ styleSubcommandDescription(str) {
388
+ return this.styleDescriptionText(str);
389
+ }
390
+ styleArgumentDescription(str) {
391
+ return this.styleDescriptionText(str);
392
+ }
393
+ styleDescriptionText(str) {
394
+ return str;
395
+ }
396
+ styleOptionTerm(str) {
397
+ return this.styleOptionText(str);
398
+ }
399
+ styleSubcommandTerm(str) {
400
+ return str.split(" ").map((word) => {
401
+ if (word === "[options]")
402
+ return this.styleOptionText(word);
403
+ if (word[0] === "[" || word[0] === "<")
404
+ return this.styleArgumentText(word);
405
+ return this.styleSubcommandText(word);
406
+ }).join(" ");
407
+ }
408
+ styleArgumentTerm(str) {
409
+ return this.styleArgumentText(str);
410
+ }
411
+ styleOptionText(str) {
412
+ return str;
413
+ }
414
+ styleArgumentText(str) {
415
+ return str;
416
+ }
417
+ styleSubcommandText(str) {
418
+ return str;
419
+ }
420
+ styleCommandText(str) {
421
+ return str;
422
+ }
423
+ padWidth(cmd, helper) {
424
+ return Math.max(helper.longestOptionTermLength(cmd, helper), helper.longestGlobalOptionTermLength(cmd, helper), helper.longestSubcommandTermLength(cmd, helper), helper.longestArgumentTermLength(cmd, helper));
425
+ }
426
+ preformatted(str) {
427
+ return /\n[^\S\r\n]/.test(str);
428
+ }
429
+ formatItem(term, termWidth, description, helper) {
430
+ const itemIndent = 2;
431
+ const itemIndentStr = " ".repeat(itemIndent);
432
+ if (!description)
433
+ return itemIndentStr + term;
434
+ const paddedTerm = term.padEnd(termWidth + term.length - helper.displayWidth(term));
435
+ const spacerWidth = 2;
436
+ const helpWidth = this.helpWidth ?? 80;
437
+ const remainingWidth = helpWidth - termWidth - spacerWidth - itemIndent;
438
+ let formattedDescription;
439
+ if (remainingWidth < this.minWidthToWrap || helper.preformatted(description)) {
440
+ formattedDescription = description;
441
+ } else {
442
+ const wrappedDescription = helper.boxWrap(description, remainingWidth);
443
+ formattedDescription = wrappedDescription.replace(/\n/g, `
444
+ ` + " ".repeat(termWidth + spacerWidth));
445
+ }
446
+ return itemIndentStr + paddedTerm + " ".repeat(spacerWidth) + formattedDescription.replace(/\n/g, `
447
+ ${itemIndentStr}`);
448
+ }
449
+ boxWrap(str, width) {
450
+ if (width < this.minWidthToWrap)
451
+ return str;
452
+ const rawLines = str.split(/\r\n|\n/);
453
+ const chunkPattern = /[\s]*[^\s]+/g;
454
+ const wrappedLines = [];
455
+ rawLines.forEach((line) => {
456
+ const chunks = line.match(chunkPattern);
457
+ if (chunks === null) {
458
+ wrappedLines.push("");
459
+ return;
460
+ }
461
+ let sumChunks = [chunks.shift()];
462
+ let sumWidth = this.displayWidth(sumChunks[0]);
463
+ chunks.forEach((chunk) => {
464
+ const visibleWidth = this.displayWidth(chunk);
465
+ if (sumWidth + visibleWidth <= width) {
466
+ sumChunks.push(chunk);
467
+ sumWidth += visibleWidth;
468
+ return;
469
+ }
470
+ wrappedLines.push(sumChunks.join(""));
471
+ const nextChunk = chunk.trimStart();
472
+ sumChunks = [nextChunk];
473
+ sumWidth = this.displayWidth(nextChunk);
474
+ });
475
+ wrappedLines.push(sumChunks.join(""));
476
+ });
477
+ return wrappedLines.join(`
478
+ `);
479
+ }
480
+ }
481
+ function stripColor(str) {
482
+ const sgrPattern = /\x1b\[\d*(;\d*)*m/g;
483
+ return str.replace(sgrPattern, "");
484
+ }
485
+ exports.Help = Help;
486
+ exports.stripColor = stripColor;
487
+ });
488
+
489
+ // node_modules/commander/lib/option.js
490
+ var require_option = __commonJS((exports) => {
491
+ var { InvalidArgumentError } = require_error();
492
+
493
+ class Option {
494
+ constructor(flags, description) {
495
+ this.flags = flags;
496
+ this.description = description || "";
497
+ this.required = flags.includes("<");
498
+ this.optional = flags.includes("[");
499
+ this.variadic = /\w\.\.\.[>\]]$/.test(flags);
500
+ this.mandatory = false;
501
+ const optionFlags = splitOptionFlags(flags);
502
+ this.short = optionFlags.shortFlag;
503
+ this.long = optionFlags.longFlag;
504
+ this.negate = false;
505
+ if (this.long) {
506
+ this.negate = this.long.startsWith("--no-");
507
+ }
508
+ this.defaultValue = undefined;
509
+ this.defaultValueDescription = undefined;
510
+ this.presetArg = undefined;
511
+ this.envVar = undefined;
512
+ this.parseArg = undefined;
513
+ this.hidden = false;
514
+ this.argChoices = undefined;
515
+ this.conflictsWith = [];
516
+ this.implied = undefined;
517
+ }
518
+ default(value, description) {
519
+ this.defaultValue = value;
520
+ this.defaultValueDescription = description;
521
+ return this;
522
+ }
523
+ preset(arg) {
524
+ this.presetArg = arg;
525
+ return this;
526
+ }
527
+ conflicts(names) {
528
+ this.conflictsWith = this.conflictsWith.concat(names);
529
+ return this;
530
+ }
531
+ implies(impliedOptionValues) {
532
+ let newImplied = impliedOptionValues;
533
+ if (typeof impliedOptionValues === "string") {
534
+ newImplied = { [impliedOptionValues]: true };
535
+ }
536
+ this.implied = Object.assign(this.implied || {}, newImplied);
537
+ return this;
538
+ }
539
+ env(name) {
540
+ this.envVar = name;
541
+ return this;
542
+ }
543
+ argParser(fn) {
544
+ this.parseArg = fn;
545
+ return this;
546
+ }
547
+ makeOptionMandatory(mandatory = true) {
548
+ this.mandatory = !!mandatory;
549
+ return this;
550
+ }
551
+ hideHelp(hide = true) {
552
+ this.hidden = !!hide;
553
+ return this;
554
+ }
555
+ _concatValue(value, previous) {
556
+ if (previous === this.defaultValue || !Array.isArray(previous)) {
557
+ return [value];
558
+ }
559
+ return previous.concat(value);
560
+ }
561
+ choices(values) {
562
+ this.argChoices = values.slice();
563
+ this.parseArg = (arg, previous) => {
564
+ if (!this.argChoices.includes(arg)) {
565
+ throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
566
+ }
567
+ if (this.variadic) {
568
+ return this._concatValue(arg, previous);
569
+ }
570
+ return arg;
571
+ };
572
+ return this;
573
+ }
574
+ name() {
575
+ if (this.long) {
576
+ return this.long.replace(/^--/, "");
577
+ }
578
+ return this.short.replace(/^-/, "");
579
+ }
580
+ attributeName() {
581
+ if (this.negate) {
582
+ return camelcase(this.name().replace(/^no-/, ""));
583
+ }
584
+ return camelcase(this.name());
585
+ }
586
+ is(arg) {
587
+ return this.short === arg || this.long === arg;
588
+ }
589
+ isBoolean() {
590
+ return !this.required && !this.optional && !this.negate;
591
+ }
592
+ }
593
+
594
+ class DualOptions {
595
+ constructor(options) {
596
+ this.positiveOptions = new Map;
597
+ this.negativeOptions = new Map;
598
+ this.dualOptions = new Set;
599
+ options.forEach((option) => {
600
+ if (option.negate) {
601
+ this.negativeOptions.set(option.attributeName(), option);
602
+ } else {
603
+ this.positiveOptions.set(option.attributeName(), option);
604
+ }
605
+ });
606
+ this.negativeOptions.forEach((value, key) => {
607
+ if (this.positiveOptions.has(key)) {
608
+ this.dualOptions.add(key);
609
+ }
610
+ });
611
+ }
612
+ valueFromOption(value, option) {
613
+ const optionKey = option.attributeName();
614
+ if (!this.dualOptions.has(optionKey))
615
+ return true;
616
+ const preset = this.negativeOptions.get(optionKey).presetArg;
617
+ const negativeValue = preset !== undefined ? preset : false;
618
+ return option.negate === (negativeValue === value);
619
+ }
620
+ }
621
+ function camelcase(str) {
622
+ return str.split("-").reduce((str2, word) => {
623
+ return str2 + word[0].toUpperCase() + word.slice(1);
624
+ });
625
+ }
626
+ function splitOptionFlags(flags) {
627
+ let shortFlag;
628
+ let longFlag;
629
+ const shortFlagExp = /^-[^-]$/;
630
+ const longFlagExp = /^--[^-]/;
631
+ const flagParts = flags.split(/[ |,]+/).concat("guard");
632
+ if (shortFlagExp.test(flagParts[0]))
633
+ shortFlag = flagParts.shift();
634
+ if (longFlagExp.test(flagParts[0]))
635
+ longFlag = flagParts.shift();
636
+ if (!shortFlag && shortFlagExp.test(flagParts[0]))
637
+ shortFlag = flagParts.shift();
638
+ if (!shortFlag && longFlagExp.test(flagParts[0])) {
639
+ shortFlag = longFlag;
640
+ longFlag = flagParts.shift();
641
+ }
642
+ if (flagParts[0].startsWith("-")) {
643
+ const unsupportedFlag = flagParts[0];
644
+ const baseError = `option creation failed due to '${unsupportedFlag}' in option flags '${flags}'`;
645
+ if (/^-[^-][^-]/.test(unsupportedFlag))
646
+ throw new Error(`${baseError}
647
+ - a short flag is a single dash and a single character
648
+ - either use a single dash and a single character (for a short flag)
649
+ - or use a double dash for a long option (and can have two, like '--ws, --workspace')`);
650
+ if (shortFlagExp.test(unsupportedFlag))
651
+ throw new Error(`${baseError}
652
+ - too many short flags`);
653
+ if (longFlagExp.test(unsupportedFlag))
654
+ throw new Error(`${baseError}
655
+ - too many long flags`);
656
+ throw new Error(`${baseError}
657
+ - unrecognised flag format`);
658
+ }
659
+ if (shortFlag === undefined && longFlag === undefined)
660
+ throw new Error(`option creation failed due to no flags found in '${flags}'.`);
661
+ return { shortFlag, longFlag };
662
+ }
663
+ exports.Option = Option;
664
+ exports.DualOptions = DualOptions;
665
+ });
666
+
667
+ // node_modules/commander/lib/suggestSimilar.js
668
+ var require_suggestSimilar = __commonJS((exports) => {
669
+ var maxDistance = 3;
670
+ function editDistance(a, b) {
671
+ if (Math.abs(a.length - b.length) > maxDistance)
672
+ return Math.max(a.length, b.length);
673
+ const d = [];
674
+ for (let i = 0;i <= a.length; i++) {
675
+ d[i] = [i];
676
+ }
677
+ for (let j = 0;j <= b.length; j++) {
678
+ d[0][j] = j;
679
+ }
680
+ for (let j = 1;j <= b.length; j++) {
681
+ for (let i = 1;i <= a.length; i++) {
682
+ let cost = 1;
683
+ if (a[i - 1] === b[j - 1]) {
684
+ cost = 0;
685
+ } else {
686
+ cost = 1;
687
+ }
688
+ d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
689
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
690
+ d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
691
+ }
692
+ }
693
+ }
694
+ return d[a.length][b.length];
695
+ }
696
+ function suggestSimilar(word, candidates) {
697
+ if (!candidates || candidates.length === 0)
698
+ return "";
699
+ candidates = Array.from(new Set(candidates));
700
+ const searchingOptions = word.startsWith("--");
701
+ if (searchingOptions) {
702
+ word = word.slice(2);
703
+ candidates = candidates.map((candidate) => candidate.slice(2));
704
+ }
705
+ let similar = [];
706
+ let bestDistance = maxDistance;
707
+ const minSimilarity = 0.4;
708
+ candidates.forEach((candidate) => {
709
+ if (candidate.length <= 1)
710
+ return;
711
+ const distance = editDistance(word, candidate);
712
+ const length = Math.max(word.length, candidate.length);
713
+ const similarity = (length - distance) / length;
714
+ if (similarity > minSimilarity) {
715
+ if (distance < bestDistance) {
716
+ bestDistance = distance;
717
+ similar = [candidate];
718
+ } else if (distance === bestDistance) {
719
+ similar.push(candidate);
720
+ }
721
+ }
722
+ });
723
+ similar.sort((a, b) => a.localeCompare(b));
724
+ if (searchingOptions) {
725
+ similar = similar.map((candidate) => `--${candidate}`);
726
+ }
727
+ if (similar.length > 1) {
728
+ return `
729
+ (Did you mean one of ${similar.join(", ")}?)`;
730
+ }
731
+ if (similar.length === 1) {
732
+ return `
733
+ (Did you mean ${similar[0]}?)`;
734
+ }
735
+ return "";
736
+ }
737
+ exports.suggestSimilar = suggestSimilar;
738
+ });
739
+
740
+ // node_modules/commander/lib/command.js
741
+ var require_command = __commonJS((exports) => {
742
+ var EventEmitter = __require("events").EventEmitter;
743
+ var childProcess = __require("child_process");
744
+ var path = __require("path");
745
+ var fs = __require("fs");
746
+ var process2 = __require("process");
747
+ var { Argument, humanReadableArgName } = require_argument();
748
+ var { CommanderError } = require_error();
749
+ var { Help, stripColor } = require_help();
750
+ var { Option, DualOptions } = require_option();
751
+ var { suggestSimilar } = require_suggestSimilar();
752
+
753
+ class Command extends EventEmitter {
754
+ constructor(name) {
755
+ super();
756
+ this.commands = [];
757
+ this.options = [];
758
+ this.parent = null;
759
+ this._allowUnknownOption = false;
760
+ this._allowExcessArguments = false;
761
+ this.registeredArguments = [];
762
+ this._args = this.registeredArguments;
763
+ this.args = [];
764
+ this.rawArgs = [];
765
+ this.processedArgs = [];
766
+ this._scriptPath = null;
767
+ this._name = name || "";
768
+ this._optionValues = {};
769
+ this._optionValueSources = {};
770
+ this._storeOptionsAsProperties = false;
771
+ this._actionHandler = null;
772
+ this._executableHandler = false;
773
+ this._executableFile = null;
774
+ this._executableDir = null;
775
+ this._defaultCommandName = null;
776
+ this._exitCallback = null;
777
+ this._aliases = [];
778
+ this._combineFlagAndOptionalValue = true;
779
+ this._description = "";
780
+ this._summary = "";
781
+ this._argsDescription = undefined;
782
+ this._enablePositionalOptions = false;
783
+ this._passThroughOptions = false;
784
+ this._lifeCycleHooks = {};
785
+ this._showHelpAfterError = false;
786
+ this._showSuggestionAfterError = true;
787
+ this._savedState = null;
788
+ this._outputConfiguration = {
789
+ writeOut: (str) => process2.stdout.write(str),
790
+ writeErr: (str) => process2.stderr.write(str),
791
+ outputError: (str, write) => write(str),
792
+ getOutHelpWidth: () => process2.stdout.isTTY ? process2.stdout.columns : undefined,
793
+ getErrHelpWidth: () => process2.stderr.isTTY ? process2.stderr.columns : undefined,
794
+ getOutHasColors: () => useColor() ?? (process2.stdout.isTTY && process2.stdout.hasColors?.()),
795
+ getErrHasColors: () => useColor() ?? (process2.stderr.isTTY && process2.stderr.hasColors?.()),
796
+ stripColor: (str) => stripColor(str)
797
+ };
798
+ this._hidden = false;
799
+ this._helpOption = undefined;
800
+ this._addImplicitHelpCommand = undefined;
801
+ this._helpCommand = undefined;
802
+ this._helpConfiguration = {};
803
+ }
804
+ copyInheritedSettings(sourceCommand) {
805
+ this._outputConfiguration = sourceCommand._outputConfiguration;
806
+ this._helpOption = sourceCommand._helpOption;
807
+ this._helpCommand = sourceCommand._helpCommand;
808
+ this._helpConfiguration = sourceCommand._helpConfiguration;
809
+ this._exitCallback = sourceCommand._exitCallback;
810
+ this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
811
+ this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
812
+ this._allowExcessArguments = sourceCommand._allowExcessArguments;
813
+ this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
814
+ this._showHelpAfterError = sourceCommand._showHelpAfterError;
815
+ this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;
816
+ return this;
817
+ }
818
+ _getCommandAndAncestors() {
819
+ const result = [];
820
+ for (let command = this;command; command = command.parent) {
821
+ result.push(command);
822
+ }
823
+ return result;
824
+ }
825
+ command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
826
+ let desc = actionOptsOrExecDesc;
827
+ let opts = execOpts;
828
+ if (typeof desc === "object" && desc !== null) {
829
+ opts = desc;
830
+ desc = null;
831
+ }
832
+ opts = opts || {};
833
+ const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
834
+ const cmd = this.createCommand(name);
835
+ if (desc) {
836
+ cmd.description(desc);
837
+ cmd._executableHandler = true;
838
+ }
839
+ if (opts.isDefault)
840
+ this._defaultCommandName = cmd._name;
841
+ cmd._hidden = !!(opts.noHelp || opts.hidden);
842
+ cmd._executableFile = opts.executableFile || null;
843
+ if (args)
844
+ cmd.arguments(args);
845
+ this._registerCommand(cmd);
846
+ cmd.parent = this;
847
+ cmd.copyInheritedSettings(this);
848
+ if (desc)
849
+ return this;
850
+ return cmd;
851
+ }
852
+ createCommand(name) {
853
+ return new Command(name);
854
+ }
855
+ createHelp() {
856
+ return Object.assign(new Help, this.configureHelp());
857
+ }
858
+ configureHelp(configuration) {
859
+ if (configuration === undefined)
860
+ return this._helpConfiguration;
861
+ this._helpConfiguration = configuration;
862
+ return this;
863
+ }
864
+ configureOutput(configuration) {
865
+ if (configuration === undefined)
866
+ return this._outputConfiguration;
867
+ Object.assign(this._outputConfiguration, configuration);
868
+ return this;
869
+ }
870
+ showHelpAfterError(displayHelp = true) {
871
+ if (typeof displayHelp !== "string")
872
+ displayHelp = !!displayHelp;
873
+ this._showHelpAfterError = displayHelp;
874
+ return this;
875
+ }
876
+ showSuggestionAfterError(displaySuggestion = true) {
877
+ this._showSuggestionAfterError = !!displaySuggestion;
878
+ return this;
879
+ }
880
+ addCommand(cmd, opts) {
881
+ if (!cmd._name) {
882
+ throw new Error(`Command passed to .addCommand() must have a name
883
+ - specify the name in Command constructor or using .name()`);
884
+ }
885
+ opts = opts || {};
886
+ if (opts.isDefault)
887
+ this._defaultCommandName = cmd._name;
888
+ if (opts.noHelp || opts.hidden)
889
+ cmd._hidden = true;
890
+ this._registerCommand(cmd);
891
+ cmd.parent = this;
892
+ cmd._checkForBrokenPassThrough();
893
+ return this;
894
+ }
895
+ createArgument(name, description) {
896
+ return new Argument(name, description);
897
+ }
898
+ argument(name, description, fn, defaultValue) {
899
+ const argument = this.createArgument(name, description);
900
+ if (typeof fn === "function") {
901
+ argument.default(defaultValue).argParser(fn);
902
+ } else {
903
+ argument.default(fn);
904
+ }
905
+ this.addArgument(argument);
906
+ return this;
907
+ }
908
+ arguments(names) {
909
+ names.trim().split(/ +/).forEach((detail) => {
910
+ this.argument(detail);
911
+ });
912
+ return this;
913
+ }
914
+ addArgument(argument) {
915
+ const previousArgument = this.registeredArguments.slice(-1)[0];
916
+ if (previousArgument && previousArgument.variadic) {
917
+ throw new Error(`only the last argument can be variadic '${previousArgument.name()}'`);
918
+ }
919
+ if (argument.required && argument.defaultValue !== undefined && argument.parseArg === undefined) {
920
+ throw new Error(`a default value for a required argument is never used: '${argument.name()}'`);
921
+ }
922
+ this.registeredArguments.push(argument);
923
+ return this;
924
+ }
925
+ helpCommand(enableOrNameAndArgs, description) {
926
+ if (typeof enableOrNameAndArgs === "boolean") {
927
+ this._addImplicitHelpCommand = enableOrNameAndArgs;
928
+ return this;
929
+ }
930
+ enableOrNameAndArgs = enableOrNameAndArgs ?? "help [command]";
931
+ const [, helpName, helpArgs] = enableOrNameAndArgs.match(/([^ ]+) *(.*)/);
932
+ const helpDescription = description ?? "display help for command";
933
+ const helpCommand = this.createCommand(helpName);
934
+ helpCommand.helpOption(false);
935
+ if (helpArgs)
936
+ helpCommand.arguments(helpArgs);
937
+ if (helpDescription)
938
+ helpCommand.description(helpDescription);
939
+ this._addImplicitHelpCommand = true;
940
+ this._helpCommand = helpCommand;
941
+ return this;
942
+ }
943
+ addHelpCommand(helpCommand, deprecatedDescription) {
944
+ if (typeof helpCommand !== "object") {
945
+ this.helpCommand(helpCommand, deprecatedDescription);
946
+ return this;
947
+ }
948
+ this._addImplicitHelpCommand = true;
949
+ this._helpCommand = helpCommand;
950
+ return this;
951
+ }
952
+ _getHelpCommand() {
953
+ const hasImplicitHelpCommand = this._addImplicitHelpCommand ?? (this.commands.length && !this._actionHandler && !this._findCommand("help"));
954
+ if (hasImplicitHelpCommand) {
955
+ if (this._helpCommand === undefined) {
956
+ this.helpCommand(undefined, undefined);
957
+ }
958
+ return this._helpCommand;
959
+ }
960
+ return null;
961
+ }
962
+ hook(event, listener) {
963
+ const allowedValues = ["preSubcommand", "preAction", "postAction"];
964
+ if (!allowedValues.includes(event)) {
965
+ throw new Error(`Unexpected value for event passed to hook : '${event}'.
966
+ Expecting one of '${allowedValues.join("', '")}'`);
967
+ }
968
+ if (this._lifeCycleHooks[event]) {
969
+ this._lifeCycleHooks[event].push(listener);
970
+ } else {
971
+ this._lifeCycleHooks[event] = [listener];
972
+ }
973
+ return this;
974
+ }
975
+ exitOverride(fn) {
976
+ if (fn) {
977
+ this._exitCallback = fn;
978
+ } else {
979
+ this._exitCallback = (err) => {
980
+ if (err.code !== "commander.executeSubCommandAsync") {
981
+ throw err;
982
+ } else {}
983
+ };
984
+ }
985
+ return this;
986
+ }
987
+ _exit(exitCode, code, message) {
988
+ if (this._exitCallback) {
989
+ this._exitCallback(new CommanderError(exitCode, code, message));
990
+ }
991
+ process2.exit(exitCode);
992
+ }
993
+ action(fn) {
994
+ const listener = (args) => {
995
+ const expectedArgsCount = this.registeredArguments.length;
996
+ const actionArgs = args.slice(0, expectedArgsCount);
997
+ if (this._storeOptionsAsProperties) {
998
+ actionArgs[expectedArgsCount] = this;
999
+ } else {
1000
+ actionArgs[expectedArgsCount] = this.opts();
1001
+ }
1002
+ actionArgs.push(this);
1003
+ return fn.apply(this, actionArgs);
1004
+ };
1005
+ this._actionHandler = listener;
1006
+ return this;
1007
+ }
1008
+ createOption(flags, description) {
1009
+ return new Option(flags, description);
1010
+ }
1011
+ _callParseArg(target, value, previous, invalidArgumentMessage) {
1012
+ try {
1013
+ return target.parseArg(value, previous);
1014
+ } catch (err) {
1015
+ if (err.code === "commander.invalidArgument") {
1016
+ const message = `${invalidArgumentMessage} ${err.message}`;
1017
+ this.error(message, { exitCode: err.exitCode, code: err.code });
1018
+ }
1019
+ throw err;
1020
+ }
1021
+ }
1022
+ _registerOption(option) {
1023
+ const matchingOption = option.short && this._findOption(option.short) || option.long && this._findOption(option.long);
1024
+ if (matchingOption) {
1025
+ const matchingFlag = option.long && this._findOption(option.long) ? option.long : option.short;
1026
+ throw new Error(`Cannot add option '${option.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'
1027
+ - already used by option '${matchingOption.flags}'`);
1028
+ }
1029
+ this.options.push(option);
1030
+ }
1031
+ _registerCommand(command) {
1032
+ const knownBy = (cmd) => {
1033
+ return [cmd.name()].concat(cmd.aliases());
1034
+ };
1035
+ const alreadyUsed = knownBy(command).find((name) => this._findCommand(name));
1036
+ if (alreadyUsed) {
1037
+ const existingCmd = knownBy(this._findCommand(alreadyUsed)).join("|");
1038
+ const newCmd = knownBy(command).join("|");
1039
+ throw new Error(`cannot add command '${newCmd}' as already have command '${existingCmd}'`);
1040
+ }
1041
+ this.commands.push(command);
1042
+ }
1043
+ addOption(option) {
1044
+ this._registerOption(option);
1045
+ const oname = option.name();
1046
+ const name = option.attributeName();
1047
+ if (option.negate) {
1048
+ const positiveLongFlag = option.long.replace(/^--no-/, "--");
1049
+ if (!this._findOption(positiveLongFlag)) {
1050
+ this.setOptionValueWithSource(name, option.defaultValue === undefined ? true : option.defaultValue, "default");
1051
+ }
1052
+ } else if (option.defaultValue !== undefined) {
1053
+ this.setOptionValueWithSource(name, option.defaultValue, "default");
1054
+ }
1055
+ const handleOptionValue = (val, invalidValueMessage, valueSource) => {
1056
+ if (val == null && option.presetArg !== undefined) {
1057
+ val = option.presetArg;
1058
+ }
1059
+ const oldValue = this.getOptionValue(name);
1060
+ if (val !== null && option.parseArg) {
1061
+ val = this._callParseArg(option, val, oldValue, invalidValueMessage);
1062
+ } else if (val !== null && option.variadic) {
1063
+ val = option._concatValue(val, oldValue);
1064
+ }
1065
+ if (val == null) {
1066
+ if (option.negate) {
1067
+ val = false;
1068
+ } else if (option.isBoolean() || option.optional) {
1069
+ val = true;
1070
+ } else {
1071
+ val = "";
1072
+ }
1073
+ }
1074
+ this.setOptionValueWithSource(name, val, valueSource);
1075
+ };
1076
+ this.on("option:" + oname, (val) => {
1077
+ const invalidValueMessage = `error: option '${option.flags}' argument '${val}' is invalid.`;
1078
+ handleOptionValue(val, invalidValueMessage, "cli");
1079
+ });
1080
+ if (option.envVar) {
1081
+ this.on("optionEnv:" + oname, (val) => {
1082
+ const invalidValueMessage = `error: option '${option.flags}' value '${val}' from env '${option.envVar}' is invalid.`;
1083
+ handleOptionValue(val, invalidValueMessage, "env");
1084
+ });
1085
+ }
1086
+ return this;
1087
+ }
1088
+ _optionEx(config, flags, description, fn, defaultValue) {
1089
+ if (typeof flags === "object" && flags instanceof Option) {
1090
+ throw new Error("To add an Option object use addOption() instead of option() or requiredOption()");
1091
+ }
1092
+ const option = this.createOption(flags, description);
1093
+ option.makeOptionMandatory(!!config.mandatory);
1094
+ if (typeof fn === "function") {
1095
+ option.default(defaultValue).argParser(fn);
1096
+ } else if (fn instanceof RegExp) {
1097
+ const regex = fn;
1098
+ fn = (val, def) => {
1099
+ const m = regex.exec(val);
1100
+ return m ? m[0] : def;
1101
+ };
1102
+ option.default(defaultValue).argParser(fn);
1103
+ } else {
1104
+ option.default(fn);
1105
+ }
1106
+ return this.addOption(option);
1107
+ }
1108
+ option(flags, description, parseArg, defaultValue) {
1109
+ return this._optionEx({}, flags, description, parseArg, defaultValue);
1110
+ }
1111
+ requiredOption(flags, description, parseArg, defaultValue) {
1112
+ return this._optionEx({ mandatory: true }, flags, description, parseArg, defaultValue);
1113
+ }
1114
+ combineFlagAndOptionalValue(combine = true) {
1115
+ this._combineFlagAndOptionalValue = !!combine;
1116
+ return this;
1117
+ }
1118
+ allowUnknownOption(allowUnknown = true) {
1119
+ this._allowUnknownOption = !!allowUnknown;
1120
+ return this;
1121
+ }
1122
+ allowExcessArguments(allowExcess = true) {
1123
+ this._allowExcessArguments = !!allowExcess;
1124
+ return this;
1125
+ }
1126
+ enablePositionalOptions(positional = true) {
1127
+ this._enablePositionalOptions = !!positional;
1128
+ return this;
1129
+ }
1130
+ passThroughOptions(passThrough = true) {
1131
+ this._passThroughOptions = !!passThrough;
1132
+ this._checkForBrokenPassThrough();
1133
+ return this;
1134
+ }
1135
+ _checkForBrokenPassThrough() {
1136
+ if (this.parent && this._passThroughOptions && !this.parent._enablePositionalOptions) {
1137
+ throw new Error(`passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`);
1138
+ }
1139
+ }
1140
+ storeOptionsAsProperties(storeAsProperties = true) {
1141
+ if (this.options.length) {
1142
+ throw new Error("call .storeOptionsAsProperties() before adding options");
1143
+ }
1144
+ if (Object.keys(this._optionValues).length) {
1145
+ throw new Error("call .storeOptionsAsProperties() before setting option values");
1146
+ }
1147
+ this._storeOptionsAsProperties = !!storeAsProperties;
1148
+ return this;
1149
+ }
1150
+ getOptionValue(key) {
1151
+ if (this._storeOptionsAsProperties) {
1152
+ return this[key];
1153
+ }
1154
+ return this._optionValues[key];
1155
+ }
1156
+ setOptionValue(key, value) {
1157
+ return this.setOptionValueWithSource(key, value, undefined);
1158
+ }
1159
+ setOptionValueWithSource(key, value, source) {
1160
+ if (this._storeOptionsAsProperties) {
1161
+ this[key] = value;
1162
+ } else {
1163
+ this._optionValues[key] = value;
1164
+ }
1165
+ this._optionValueSources[key] = source;
1166
+ return this;
1167
+ }
1168
+ getOptionValueSource(key) {
1169
+ return this._optionValueSources[key];
1170
+ }
1171
+ getOptionValueSourceWithGlobals(key) {
1172
+ let source;
1173
+ this._getCommandAndAncestors().forEach((cmd) => {
1174
+ if (cmd.getOptionValueSource(key) !== undefined) {
1175
+ source = cmd.getOptionValueSource(key);
1176
+ }
1177
+ });
1178
+ return source;
1179
+ }
1180
+ _prepareUserArgs(argv, parseOptions) {
1181
+ if (argv !== undefined && !Array.isArray(argv)) {
1182
+ throw new Error("first parameter to parse must be array or undefined");
1183
+ }
1184
+ parseOptions = parseOptions || {};
1185
+ if (argv === undefined && parseOptions.from === undefined) {
1186
+ if (process2.versions?.electron) {
1187
+ parseOptions.from = "electron";
1188
+ }
1189
+ const execArgv = process2.execArgv ?? [];
1190
+ if (execArgv.includes("-e") || execArgv.includes("--eval") || execArgv.includes("-p") || execArgv.includes("--print")) {
1191
+ parseOptions.from = "eval";
1192
+ }
1193
+ }
1194
+ if (argv === undefined) {
1195
+ argv = process2.argv;
1196
+ }
1197
+ this.rawArgs = argv.slice();
1198
+ let userArgs;
1199
+ switch (parseOptions.from) {
1200
+ case undefined:
1201
+ case "node":
1202
+ this._scriptPath = argv[1];
1203
+ userArgs = argv.slice(2);
1204
+ break;
1205
+ case "electron":
1206
+ if (process2.defaultApp) {
1207
+ this._scriptPath = argv[1];
1208
+ userArgs = argv.slice(2);
1209
+ } else {
1210
+ userArgs = argv.slice(1);
1211
+ }
1212
+ break;
1213
+ case "user":
1214
+ userArgs = argv.slice(0);
1215
+ break;
1216
+ case "eval":
1217
+ userArgs = argv.slice(1);
1218
+ break;
1219
+ default:
1220
+ throw new Error(`unexpected parse option { from: '${parseOptions.from}' }`);
1221
+ }
1222
+ if (!this._name && this._scriptPath)
1223
+ this.nameFromFilename(this._scriptPath);
1224
+ this._name = this._name || "program";
1225
+ return userArgs;
1226
+ }
1227
+ parse(argv, parseOptions) {
1228
+ this._prepareForParse();
1229
+ const userArgs = this._prepareUserArgs(argv, parseOptions);
1230
+ this._parseCommand([], userArgs);
1231
+ return this;
1232
+ }
1233
+ async parseAsync(argv, parseOptions) {
1234
+ this._prepareForParse();
1235
+ const userArgs = this._prepareUserArgs(argv, parseOptions);
1236
+ await this._parseCommand([], userArgs);
1237
+ return this;
1238
+ }
1239
+ _prepareForParse() {
1240
+ if (this._savedState === null) {
1241
+ this.saveStateBeforeParse();
1242
+ } else {
1243
+ this.restoreStateBeforeParse();
1244
+ }
1245
+ }
1246
+ saveStateBeforeParse() {
1247
+ this._savedState = {
1248
+ _name: this._name,
1249
+ _optionValues: { ...this._optionValues },
1250
+ _optionValueSources: { ...this._optionValueSources }
1251
+ };
1252
+ }
1253
+ restoreStateBeforeParse() {
1254
+ if (this._storeOptionsAsProperties)
1255
+ throw new Error(`Can not call parse again when storeOptionsAsProperties is true.
1256
+ - either make a new Command for each call to parse, or stop storing options as properties`);
1257
+ this._name = this._savedState._name;
1258
+ this._scriptPath = null;
1259
+ this.rawArgs = [];
1260
+ this._optionValues = { ...this._savedState._optionValues };
1261
+ this._optionValueSources = { ...this._savedState._optionValueSources };
1262
+ this.args = [];
1263
+ this.processedArgs = [];
1264
+ }
1265
+ _checkForMissingExecutable(executableFile, executableDir, subcommandName) {
1266
+ if (fs.existsSync(executableFile))
1267
+ return;
1268
+ 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";
1269
+ const executableMissing = `'${executableFile}' does not exist
1270
+ - if '${subcommandName}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
1271
+ - if the default executable name is not suitable, use the executableFile option to supply a custom name or path
1272
+ - ${executableDirMessage}`;
1273
+ throw new Error(executableMissing);
1274
+ }
1275
+ _executeSubCommand(subcommand, args) {
1276
+ args = args.slice();
1277
+ let launchWithNode = false;
1278
+ const sourceExt = [".js", ".ts", ".tsx", ".mjs", ".cjs"];
1279
+ function findFile(baseDir, baseName) {
1280
+ const localBin = path.resolve(baseDir, baseName);
1281
+ if (fs.existsSync(localBin))
1282
+ return localBin;
1283
+ if (sourceExt.includes(path.extname(baseName)))
1284
+ return;
1285
+ const foundExt = sourceExt.find((ext) => fs.existsSync(`${localBin}${ext}`));
1286
+ if (foundExt)
1287
+ return `${localBin}${foundExt}`;
1288
+ return;
1289
+ }
1290
+ this._checkForMissingMandatoryOptions();
1291
+ this._checkForConflictingOptions();
1292
+ let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;
1293
+ let executableDir = this._executableDir || "";
1294
+ if (this._scriptPath) {
1295
+ let resolvedScriptPath;
1296
+ try {
1297
+ resolvedScriptPath = fs.realpathSync(this._scriptPath);
1298
+ } catch {
1299
+ resolvedScriptPath = this._scriptPath;
1300
+ }
1301
+ executableDir = path.resolve(path.dirname(resolvedScriptPath), executableDir);
1302
+ }
1303
+ if (executableDir) {
1304
+ let localFile = findFile(executableDir, executableFile);
1305
+ if (!localFile && !subcommand._executableFile && this._scriptPath) {
1306
+ const legacyName = path.basename(this._scriptPath, path.extname(this._scriptPath));
1307
+ if (legacyName !== this._name) {
1308
+ localFile = findFile(executableDir, `${legacyName}-${subcommand._name}`);
1309
+ }
1310
+ }
1311
+ executableFile = localFile || executableFile;
1312
+ }
1313
+ launchWithNode = sourceExt.includes(path.extname(executableFile));
1314
+ let proc;
1315
+ if (process2.platform !== "win32") {
1316
+ if (launchWithNode) {
1317
+ args.unshift(executableFile);
1318
+ args = incrementNodeInspectorPort(process2.execArgv).concat(args);
1319
+ proc = childProcess.spawn(process2.argv[0], args, { stdio: "inherit" });
1320
+ } else {
1321
+ proc = childProcess.spawn(executableFile, args, { stdio: "inherit" });
1322
+ }
1323
+ } else {
1324
+ this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
1325
+ args.unshift(executableFile);
1326
+ args = incrementNodeInspectorPort(process2.execArgv).concat(args);
1327
+ proc = childProcess.spawn(process2.execPath, args, { stdio: "inherit" });
1328
+ }
1329
+ if (!proc.killed) {
1330
+ const signals = ["SIGUSR1", "SIGUSR2", "SIGTERM", "SIGINT", "SIGHUP"];
1331
+ signals.forEach((signal) => {
1332
+ process2.on(signal, () => {
1333
+ if (proc.killed === false && proc.exitCode === null) {
1334
+ proc.kill(signal);
1335
+ }
1336
+ });
1337
+ });
1338
+ }
1339
+ const exitCallback = this._exitCallback;
1340
+ proc.on("close", (code) => {
1341
+ code = code ?? 1;
1342
+ if (!exitCallback) {
1343
+ process2.exit(code);
1344
+ } else {
1345
+ exitCallback(new CommanderError(code, "commander.executeSubCommandAsync", "(close)"));
1346
+ }
1347
+ });
1348
+ proc.on("error", (err) => {
1349
+ if (err.code === "ENOENT") {
1350
+ this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
1351
+ } else if (err.code === "EACCES") {
1352
+ throw new Error(`'${executableFile}' not executable`);
1353
+ }
1354
+ if (!exitCallback) {
1355
+ process2.exit(1);
1356
+ } else {
1357
+ const wrappedError = new CommanderError(1, "commander.executeSubCommandAsync", "(error)");
1358
+ wrappedError.nestedError = err;
1359
+ exitCallback(wrappedError);
1360
+ }
1361
+ });
1362
+ this.runningCommand = proc;
1363
+ }
1364
+ _dispatchSubcommand(commandName, operands, unknown) {
1365
+ const subCommand = this._findCommand(commandName);
1366
+ if (!subCommand)
1367
+ this.help({ error: true });
1368
+ subCommand._prepareForParse();
1369
+ let promiseChain;
1370
+ promiseChain = this._chainOrCallSubCommandHook(promiseChain, subCommand, "preSubcommand");
1371
+ promiseChain = this._chainOrCall(promiseChain, () => {
1372
+ if (subCommand._executableHandler) {
1373
+ this._executeSubCommand(subCommand, operands.concat(unknown));
1374
+ } else {
1375
+ return subCommand._parseCommand(operands, unknown);
1376
+ }
1377
+ });
1378
+ return promiseChain;
1379
+ }
1380
+ _dispatchHelpCommand(subcommandName) {
1381
+ if (!subcommandName) {
1382
+ this.help();
1383
+ }
1384
+ const subCommand = this._findCommand(subcommandName);
1385
+ if (subCommand && !subCommand._executableHandler) {
1386
+ subCommand.help();
1387
+ }
1388
+ return this._dispatchSubcommand(subcommandName, [], [this._getHelpOption()?.long ?? this._getHelpOption()?.short ?? "--help"]);
1389
+ }
1390
+ _checkNumberOfArguments() {
1391
+ this.registeredArguments.forEach((arg, i) => {
1392
+ if (arg.required && this.args[i] == null) {
1393
+ this.missingArgument(arg.name());
1394
+ }
1395
+ });
1396
+ if (this.registeredArguments.length > 0 && this.registeredArguments[this.registeredArguments.length - 1].variadic) {
1397
+ return;
1398
+ }
1399
+ if (this.args.length > this.registeredArguments.length) {
1400
+ this._excessArguments(this.args);
1401
+ }
1402
+ }
1403
+ _processArguments() {
1404
+ const myParseArg = (argument, value, previous) => {
1405
+ let parsedValue = value;
1406
+ if (value !== null && argument.parseArg) {
1407
+ const invalidValueMessage = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'.`;
1408
+ parsedValue = this._callParseArg(argument, value, previous, invalidValueMessage);
1409
+ }
1410
+ return parsedValue;
1411
+ };
1412
+ this._checkNumberOfArguments();
1413
+ const processedArgs = [];
1414
+ this.registeredArguments.forEach((declaredArg, index) => {
1415
+ let value = declaredArg.defaultValue;
1416
+ if (declaredArg.variadic) {
1417
+ if (index < this.args.length) {
1418
+ value = this.args.slice(index);
1419
+ if (declaredArg.parseArg) {
1420
+ value = value.reduce((processed, v) => {
1421
+ return myParseArg(declaredArg, v, processed);
1422
+ }, declaredArg.defaultValue);
1423
+ }
1424
+ } else if (value === undefined) {
1425
+ value = [];
1426
+ }
1427
+ } else if (index < this.args.length) {
1428
+ value = this.args[index];
1429
+ if (declaredArg.parseArg) {
1430
+ value = myParseArg(declaredArg, value, declaredArg.defaultValue);
1431
+ }
1432
+ }
1433
+ processedArgs[index] = value;
1434
+ });
1435
+ this.processedArgs = processedArgs;
1436
+ }
1437
+ _chainOrCall(promise, fn) {
1438
+ if (promise && promise.then && typeof promise.then === "function") {
1439
+ return promise.then(() => fn());
1440
+ }
1441
+ return fn();
1442
+ }
1443
+ _chainOrCallHooks(promise, event) {
1444
+ let result = promise;
1445
+ const hooks = [];
1446
+ this._getCommandAndAncestors().reverse().filter((cmd) => cmd._lifeCycleHooks[event] !== undefined).forEach((hookedCommand) => {
1447
+ hookedCommand._lifeCycleHooks[event].forEach((callback) => {
1448
+ hooks.push({ hookedCommand, callback });
1449
+ });
1450
+ });
1451
+ if (event === "postAction") {
1452
+ hooks.reverse();
1453
+ }
1454
+ hooks.forEach((hookDetail) => {
1455
+ result = this._chainOrCall(result, () => {
1456
+ return hookDetail.callback(hookDetail.hookedCommand, this);
1457
+ });
1458
+ });
1459
+ return result;
1460
+ }
1461
+ _chainOrCallSubCommandHook(promise, subCommand, event) {
1462
+ let result = promise;
1463
+ if (this._lifeCycleHooks[event] !== undefined) {
1464
+ this._lifeCycleHooks[event].forEach((hook) => {
1465
+ result = this._chainOrCall(result, () => {
1466
+ return hook(this, subCommand);
1467
+ });
1468
+ });
1469
+ }
1470
+ return result;
1471
+ }
1472
+ _parseCommand(operands, unknown) {
1473
+ const parsed = this.parseOptions(unknown);
1474
+ this._parseOptionsEnv();
1475
+ this._parseOptionsImplied();
1476
+ operands = operands.concat(parsed.operands);
1477
+ unknown = parsed.unknown;
1478
+ this.args = operands.concat(unknown);
1479
+ if (operands && this._findCommand(operands[0])) {
1480
+ return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
1481
+ }
1482
+ if (this._getHelpCommand() && operands[0] === this._getHelpCommand().name()) {
1483
+ return this._dispatchHelpCommand(operands[1]);
1484
+ }
1485
+ if (this._defaultCommandName) {
1486
+ this._outputHelpIfRequested(unknown);
1487
+ return this._dispatchSubcommand(this._defaultCommandName, operands, unknown);
1488
+ }
1489
+ if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) {
1490
+ this.help({ error: true });
1491
+ }
1492
+ this._outputHelpIfRequested(parsed.unknown);
1493
+ this._checkForMissingMandatoryOptions();
1494
+ this._checkForConflictingOptions();
1495
+ const checkForUnknownOptions = () => {
1496
+ if (parsed.unknown.length > 0) {
1497
+ this.unknownOption(parsed.unknown[0]);
1498
+ }
1499
+ };
1500
+ const commandEvent = `command:${this.name()}`;
1501
+ if (this._actionHandler) {
1502
+ checkForUnknownOptions();
1503
+ this._processArguments();
1504
+ let promiseChain;
1505
+ promiseChain = this._chainOrCallHooks(promiseChain, "preAction");
1506
+ promiseChain = this._chainOrCall(promiseChain, () => this._actionHandler(this.processedArgs));
1507
+ if (this.parent) {
1508
+ promiseChain = this._chainOrCall(promiseChain, () => {
1509
+ this.parent.emit(commandEvent, operands, unknown);
1510
+ });
1511
+ }
1512
+ promiseChain = this._chainOrCallHooks(promiseChain, "postAction");
1513
+ return promiseChain;
1514
+ }
1515
+ if (this.parent && this.parent.listenerCount(commandEvent)) {
1516
+ checkForUnknownOptions();
1517
+ this._processArguments();
1518
+ this.parent.emit(commandEvent, operands, unknown);
1519
+ } else if (operands.length) {
1520
+ if (this._findCommand("*")) {
1521
+ return this._dispatchSubcommand("*", operands, unknown);
1522
+ }
1523
+ if (this.listenerCount("command:*")) {
1524
+ this.emit("command:*", operands, unknown);
1525
+ } else if (this.commands.length) {
1526
+ this.unknownCommand();
1527
+ } else {
1528
+ checkForUnknownOptions();
1529
+ this._processArguments();
1530
+ }
1531
+ } else if (this.commands.length) {
1532
+ checkForUnknownOptions();
1533
+ this.help({ error: true });
1534
+ } else {
1535
+ checkForUnknownOptions();
1536
+ this._processArguments();
1537
+ }
1538
+ }
1539
+ _findCommand(name) {
1540
+ if (!name)
1541
+ return;
1542
+ return this.commands.find((cmd) => cmd._name === name || cmd._aliases.includes(name));
1543
+ }
1544
+ _findOption(arg) {
1545
+ return this.options.find((option) => option.is(arg));
1546
+ }
1547
+ _checkForMissingMandatoryOptions() {
1548
+ this._getCommandAndAncestors().forEach((cmd) => {
1549
+ cmd.options.forEach((anOption) => {
1550
+ if (anOption.mandatory && cmd.getOptionValue(anOption.attributeName()) === undefined) {
1551
+ cmd.missingMandatoryOptionValue(anOption);
1552
+ }
1553
+ });
1554
+ });
1555
+ }
1556
+ _checkForConflictingLocalOptions() {
1557
+ const definedNonDefaultOptions = this.options.filter((option) => {
1558
+ const optionKey = option.attributeName();
1559
+ if (this.getOptionValue(optionKey) === undefined) {
1560
+ return false;
1561
+ }
1562
+ return this.getOptionValueSource(optionKey) !== "default";
1563
+ });
1564
+ const optionsWithConflicting = definedNonDefaultOptions.filter((option) => option.conflictsWith.length > 0);
1565
+ optionsWithConflicting.forEach((option) => {
1566
+ const conflictingAndDefined = definedNonDefaultOptions.find((defined) => option.conflictsWith.includes(defined.attributeName()));
1567
+ if (conflictingAndDefined) {
1568
+ this._conflictingOption(option, conflictingAndDefined);
1569
+ }
1570
+ });
1571
+ }
1572
+ _checkForConflictingOptions() {
1573
+ this._getCommandAndAncestors().forEach((cmd) => {
1574
+ cmd._checkForConflictingLocalOptions();
1575
+ });
1576
+ }
1577
+ parseOptions(argv) {
1578
+ const operands = [];
1579
+ const unknown = [];
1580
+ let dest = operands;
1581
+ const args = argv.slice();
1582
+ function maybeOption(arg) {
1583
+ return arg.length > 1 && arg[0] === "-";
1584
+ }
1585
+ let activeVariadicOption = null;
1586
+ while (args.length) {
1587
+ const arg = args.shift();
1588
+ if (arg === "--") {
1589
+ if (dest === unknown)
1590
+ dest.push(arg);
1591
+ dest.push(...args);
1592
+ break;
1593
+ }
1594
+ if (activeVariadicOption && !maybeOption(arg)) {
1595
+ this.emit(`option:${activeVariadicOption.name()}`, arg);
1596
+ continue;
1597
+ }
1598
+ activeVariadicOption = null;
1599
+ if (maybeOption(arg)) {
1600
+ const option = this._findOption(arg);
1601
+ if (option) {
1602
+ if (option.required) {
1603
+ const value = args.shift();
1604
+ if (value === undefined)
1605
+ this.optionMissingArgument(option);
1606
+ this.emit(`option:${option.name()}`, value);
1607
+ } else if (option.optional) {
1608
+ let value = null;
1609
+ if (args.length > 0 && !maybeOption(args[0])) {
1610
+ value = args.shift();
1611
+ }
1612
+ this.emit(`option:${option.name()}`, value);
1613
+ } else {
1614
+ this.emit(`option:${option.name()}`);
1615
+ }
1616
+ activeVariadicOption = option.variadic ? option : null;
1617
+ continue;
1618
+ }
1619
+ }
1620
+ if (arg.length > 2 && arg[0] === "-" && arg[1] !== "-") {
1621
+ const option = this._findOption(`-${arg[1]}`);
1622
+ if (option) {
1623
+ if (option.required || option.optional && this._combineFlagAndOptionalValue) {
1624
+ this.emit(`option:${option.name()}`, arg.slice(2));
1625
+ } else {
1626
+ this.emit(`option:${option.name()}`);
1627
+ args.unshift(`-${arg.slice(2)}`);
1628
+ }
1629
+ continue;
1630
+ }
1631
+ }
1632
+ if (/^--[^=]+=/.test(arg)) {
1633
+ const index = arg.indexOf("=");
1634
+ const option = this._findOption(arg.slice(0, index));
1635
+ if (option && (option.required || option.optional)) {
1636
+ this.emit(`option:${option.name()}`, arg.slice(index + 1));
1637
+ continue;
1638
+ }
1639
+ }
1640
+ if (maybeOption(arg)) {
1641
+ dest = unknown;
1642
+ }
1643
+ if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
1644
+ if (this._findCommand(arg)) {
1645
+ operands.push(arg);
1646
+ if (args.length > 0)
1647
+ unknown.push(...args);
1648
+ break;
1649
+ } else if (this._getHelpCommand() && arg === this._getHelpCommand().name()) {
1650
+ operands.push(arg);
1651
+ if (args.length > 0)
1652
+ operands.push(...args);
1653
+ break;
1654
+ } else if (this._defaultCommandName) {
1655
+ unknown.push(arg);
1656
+ if (args.length > 0)
1657
+ unknown.push(...args);
1658
+ break;
1659
+ }
1660
+ }
1661
+ if (this._passThroughOptions) {
1662
+ dest.push(arg);
1663
+ if (args.length > 0)
1664
+ dest.push(...args);
1665
+ break;
1666
+ }
1667
+ dest.push(arg);
1668
+ }
1669
+ return { operands, unknown };
1670
+ }
1671
+ opts() {
1672
+ if (this._storeOptionsAsProperties) {
1673
+ const result = {};
1674
+ const len = this.options.length;
1675
+ for (let i = 0;i < len; i++) {
1676
+ const key = this.options[i].attributeName();
1677
+ result[key] = key === this._versionOptionName ? this._version : this[key];
1678
+ }
1679
+ return result;
1680
+ }
1681
+ return this._optionValues;
1682
+ }
1683
+ optsWithGlobals() {
1684
+ return this._getCommandAndAncestors().reduce((combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()), {});
1685
+ }
1686
+ error(message, errorOptions) {
1687
+ this._outputConfiguration.outputError(`${message}
1688
+ `, this._outputConfiguration.writeErr);
1689
+ if (typeof this._showHelpAfterError === "string") {
1690
+ this._outputConfiguration.writeErr(`${this._showHelpAfterError}
1691
+ `);
1692
+ } else if (this._showHelpAfterError) {
1693
+ this._outputConfiguration.writeErr(`
1694
+ `);
1695
+ this.outputHelp({ error: true });
1696
+ }
1697
+ const config = errorOptions || {};
1698
+ const exitCode = config.exitCode || 1;
1699
+ const code = config.code || "commander.error";
1700
+ this._exit(exitCode, code, message);
1701
+ }
1702
+ _parseOptionsEnv() {
1703
+ this.options.forEach((option) => {
1704
+ if (option.envVar && option.envVar in process2.env) {
1705
+ const optionKey = option.attributeName();
1706
+ if (this.getOptionValue(optionKey) === undefined || ["default", "config", "env"].includes(this.getOptionValueSource(optionKey))) {
1707
+ if (option.required || option.optional) {
1708
+ this.emit(`optionEnv:${option.name()}`, process2.env[option.envVar]);
1709
+ } else {
1710
+ this.emit(`optionEnv:${option.name()}`);
1711
+ }
1712
+ }
1713
+ }
1714
+ });
1715
+ }
1716
+ _parseOptionsImplied() {
1717
+ const dualHelper = new DualOptions(this.options);
1718
+ const hasCustomOptionValue = (optionKey) => {
1719
+ return this.getOptionValue(optionKey) !== undefined && !["default", "implied"].includes(this.getOptionValueSource(optionKey));
1720
+ };
1721
+ this.options.filter((option) => option.implied !== undefined && hasCustomOptionValue(option.attributeName()) && dualHelper.valueFromOption(this.getOptionValue(option.attributeName()), option)).forEach((option) => {
1722
+ Object.keys(option.implied).filter((impliedKey) => !hasCustomOptionValue(impliedKey)).forEach((impliedKey) => {
1723
+ this.setOptionValueWithSource(impliedKey, option.implied[impliedKey], "implied");
1724
+ });
1725
+ });
1726
+ }
1727
+ missingArgument(name) {
1728
+ const message = `error: missing required argument '${name}'`;
1729
+ this.error(message, { code: "commander.missingArgument" });
1730
+ }
1731
+ optionMissingArgument(option) {
1732
+ const message = `error: option '${option.flags}' argument missing`;
1733
+ this.error(message, { code: "commander.optionMissingArgument" });
1734
+ }
1735
+ missingMandatoryOptionValue(option) {
1736
+ const message = `error: required option '${option.flags}' not specified`;
1737
+ this.error(message, { code: "commander.missingMandatoryOptionValue" });
1738
+ }
1739
+ _conflictingOption(option, conflictingOption) {
1740
+ const findBestOptionFromValue = (option2) => {
1741
+ const optionKey = option2.attributeName();
1742
+ const optionValue = this.getOptionValue(optionKey);
1743
+ const negativeOption = this.options.find((target) => target.negate && optionKey === target.attributeName());
1744
+ const positiveOption = this.options.find((target) => !target.negate && optionKey === target.attributeName());
1745
+ if (negativeOption && (negativeOption.presetArg === undefined && optionValue === false || negativeOption.presetArg !== undefined && optionValue === negativeOption.presetArg)) {
1746
+ return negativeOption;
1747
+ }
1748
+ return positiveOption || option2;
1749
+ };
1750
+ const getErrorMessage = (option2) => {
1751
+ const bestOption = findBestOptionFromValue(option2);
1752
+ const optionKey = bestOption.attributeName();
1753
+ const source = this.getOptionValueSource(optionKey);
1754
+ if (source === "env") {
1755
+ return `environment variable '${bestOption.envVar}'`;
1756
+ }
1757
+ return `option '${bestOption.flags}'`;
1758
+ };
1759
+ const message = `error: ${getErrorMessage(option)} cannot be used with ${getErrorMessage(conflictingOption)}`;
1760
+ this.error(message, { code: "commander.conflictingOption" });
1761
+ }
1762
+ unknownOption(flag) {
1763
+ if (this._allowUnknownOption)
1764
+ return;
1765
+ let suggestion = "";
1766
+ if (flag.startsWith("--") && this._showSuggestionAfterError) {
1767
+ let candidateFlags = [];
1768
+ let command = this;
1769
+ do {
1770
+ const moreFlags = command.createHelp().visibleOptions(command).filter((option) => option.long).map((option) => option.long);
1771
+ candidateFlags = candidateFlags.concat(moreFlags);
1772
+ command = command.parent;
1773
+ } while (command && !command._enablePositionalOptions);
1774
+ suggestion = suggestSimilar(flag, candidateFlags);
1775
+ }
1776
+ const message = `error: unknown option '${flag}'${suggestion}`;
1777
+ this.error(message, { code: "commander.unknownOption" });
1778
+ }
1779
+ _excessArguments(receivedArgs) {
1780
+ if (this._allowExcessArguments)
1781
+ return;
1782
+ const expected = this.registeredArguments.length;
1783
+ const s = expected === 1 ? "" : "s";
1784
+ const forSubcommand = this.parent ? ` for '${this.name()}'` : "";
1785
+ const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;
1786
+ this.error(message, { code: "commander.excessArguments" });
1787
+ }
1788
+ unknownCommand() {
1789
+ const unknownName = this.args[0];
1790
+ let suggestion = "";
1791
+ if (this._showSuggestionAfterError) {
1792
+ const candidateNames = [];
1793
+ this.createHelp().visibleCommands(this).forEach((command) => {
1794
+ candidateNames.push(command.name());
1795
+ if (command.alias())
1796
+ candidateNames.push(command.alias());
1797
+ });
1798
+ suggestion = suggestSimilar(unknownName, candidateNames);
1799
+ }
1800
+ const message = `error: unknown command '${unknownName}'${suggestion}`;
1801
+ this.error(message, { code: "commander.unknownCommand" });
1802
+ }
1803
+ version(str, flags, description) {
1804
+ if (str === undefined)
1805
+ return this._version;
1806
+ this._version = str;
1807
+ flags = flags || "-V, --version";
1808
+ description = description || "output the version number";
1809
+ const versionOption = this.createOption(flags, description);
1810
+ this._versionOptionName = versionOption.attributeName();
1811
+ this._registerOption(versionOption);
1812
+ this.on("option:" + versionOption.name(), () => {
1813
+ this._outputConfiguration.writeOut(`${str}
1814
+ `);
1815
+ this._exit(0, "commander.version", str);
1816
+ });
1817
+ return this;
1818
+ }
1819
+ description(str, argsDescription) {
1820
+ if (str === undefined && argsDescription === undefined)
1821
+ return this._description;
1822
+ this._description = str;
1823
+ if (argsDescription) {
1824
+ this._argsDescription = argsDescription;
1825
+ }
1826
+ return this;
1827
+ }
1828
+ summary(str) {
1829
+ if (str === undefined)
1830
+ return this._summary;
1831
+ this._summary = str;
1832
+ return this;
1833
+ }
1834
+ alias(alias) {
1835
+ if (alias === undefined)
1836
+ return this._aliases[0];
1837
+ let command = this;
1838
+ if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
1839
+ command = this.commands[this.commands.length - 1];
1840
+ }
1841
+ if (alias === command._name)
1842
+ throw new Error("Command alias can't be the same as its name");
1843
+ const matchingCommand = this.parent?._findCommand(alias);
1844
+ if (matchingCommand) {
1845
+ const existingCmd = [matchingCommand.name()].concat(matchingCommand.aliases()).join("|");
1846
+ throw new Error(`cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`);
1847
+ }
1848
+ command._aliases.push(alias);
1849
+ return this;
1850
+ }
1851
+ aliases(aliases) {
1852
+ if (aliases === undefined)
1853
+ return this._aliases;
1854
+ aliases.forEach((alias) => this.alias(alias));
1855
+ return this;
1856
+ }
1857
+ usage(str) {
1858
+ if (str === undefined) {
1859
+ if (this._usage)
1860
+ return this._usage;
1861
+ const args = this.registeredArguments.map((arg) => {
1862
+ return humanReadableArgName(arg);
1863
+ });
1864
+ return [].concat(this.options.length || this._helpOption !== null ? "[options]" : [], this.commands.length ? "[command]" : [], this.registeredArguments.length ? args : []).join(" ");
1865
+ }
1866
+ this._usage = str;
1867
+ return this;
1868
+ }
1869
+ name(str) {
1870
+ if (str === undefined)
1871
+ return this._name;
1872
+ this._name = str;
1873
+ return this;
1874
+ }
1875
+ nameFromFilename(filename) {
1876
+ this._name = path.basename(filename, path.extname(filename));
1877
+ return this;
1878
+ }
1879
+ executableDir(path2) {
1880
+ if (path2 === undefined)
1881
+ return this._executableDir;
1882
+ this._executableDir = path2;
1883
+ return this;
1884
+ }
1885
+ helpInformation(contextOptions) {
1886
+ const helper = this.createHelp();
1887
+ const context = this._getOutputContext(contextOptions);
1888
+ helper.prepareContext({
1889
+ error: context.error,
1890
+ helpWidth: context.helpWidth,
1891
+ outputHasColors: context.hasColors
1892
+ });
1893
+ const text = helper.formatHelp(this, helper);
1894
+ if (context.hasColors)
1895
+ return text;
1896
+ return this._outputConfiguration.stripColor(text);
1897
+ }
1898
+ _getOutputContext(contextOptions) {
1899
+ contextOptions = contextOptions || {};
1900
+ const error = !!contextOptions.error;
1901
+ let baseWrite;
1902
+ let hasColors;
1903
+ let helpWidth;
1904
+ if (error) {
1905
+ baseWrite = (str) => this._outputConfiguration.writeErr(str);
1906
+ hasColors = this._outputConfiguration.getErrHasColors();
1907
+ helpWidth = this._outputConfiguration.getErrHelpWidth();
1908
+ } else {
1909
+ baseWrite = (str) => this._outputConfiguration.writeOut(str);
1910
+ hasColors = this._outputConfiguration.getOutHasColors();
1911
+ helpWidth = this._outputConfiguration.getOutHelpWidth();
1912
+ }
1913
+ const write = (str) => {
1914
+ if (!hasColors)
1915
+ str = this._outputConfiguration.stripColor(str);
1916
+ return baseWrite(str);
1917
+ };
1918
+ return { error, write, hasColors, helpWidth };
1919
+ }
1920
+ outputHelp(contextOptions) {
1921
+ let deprecatedCallback;
1922
+ if (typeof contextOptions === "function") {
1923
+ deprecatedCallback = contextOptions;
1924
+ contextOptions = undefined;
1925
+ }
1926
+ const outputContext = this._getOutputContext(contextOptions);
1927
+ const eventContext = {
1928
+ error: outputContext.error,
1929
+ write: outputContext.write,
1930
+ command: this
1931
+ };
1932
+ this._getCommandAndAncestors().reverse().forEach((command) => command.emit("beforeAllHelp", eventContext));
1933
+ this.emit("beforeHelp", eventContext);
1934
+ let helpInformation = this.helpInformation({ error: outputContext.error });
1935
+ if (deprecatedCallback) {
1936
+ helpInformation = deprecatedCallback(helpInformation);
1937
+ if (typeof helpInformation !== "string" && !Buffer.isBuffer(helpInformation)) {
1938
+ throw new Error("outputHelp callback must return a string or a Buffer");
1939
+ }
1940
+ }
1941
+ outputContext.write(helpInformation);
1942
+ if (this._getHelpOption()?.long) {
1943
+ this.emit(this._getHelpOption().long);
1944
+ }
1945
+ this.emit("afterHelp", eventContext);
1946
+ this._getCommandAndAncestors().forEach((command) => command.emit("afterAllHelp", eventContext));
1947
+ }
1948
+ helpOption(flags, description) {
1949
+ if (typeof flags === "boolean") {
1950
+ if (flags) {
1951
+ this._helpOption = this._helpOption ?? undefined;
1952
+ } else {
1953
+ this._helpOption = null;
1954
+ }
1955
+ return this;
1956
+ }
1957
+ flags = flags ?? "-h, --help";
1958
+ description = description ?? "display help for command";
1959
+ this._helpOption = this.createOption(flags, description);
1960
+ return this;
1961
+ }
1962
+ _getHelpOption() {
1963
+ if (this._helpOption === undefined) {
1964
+ this.helpOption(undefined, undefined);
1965
+ }
1966
+ return this._helpOption;
1967
+ }
1968
+ addHelpOption(option) {
1969
+ this._helpOption = option;
1970
+ return this;
1971
+ }
1972
+ help(contextOptions) {
1973
+ this.outputHelp(contextOptions);
1974
+ let exitCode = Number(process2.exitCode ?? 0);
1975
+ if (exitCode === 0 && contextOptions && typeof contextOptions !== "function" && contextOptions.error) {
1976
+ exitCode = 1;
1977
+ }
1978
+ this._exit(exitCode, "commander.help", "(outputHelp)");
1979
+ }
1980
+ addHelpText(position, text) {
1981
+ const allowedValues = ["beforeAll", "before", "after", "afterAll"];
1982
+ if (!allowedValues.includes(position)) {
1983
+ throw new Error(`Unexpected value for position to addHelpText.
1984
+ Expecting one of '${allowedValues.join("', '")}'`);
1985
+ }
1986
+ const helpEvent = `${position}Help`;
1987
+ this.on(helpEvent, (context) => {
1988
+ let helpStr;
1989
+ if (typeof text === "function") {
1990
+ helpStr = text({ error: context.error, command: context.command });
1991
+ } else {
1992
+ helpStr = text;
1993
+ }
1994
+ if (helpStr) {
1995
+ context.write(`${helpStr}
1996
+ `);
1997
+ }
1998
+ });
1999
+ return this;
2000
+ }
2001
+ _outputHelpIfRequested(args) {
2002
+ const helpOption = this._getHelpOption();
2003
+ const helpRequested = helpOption && args.find((arg) => helpOption.is(arg));
2004
+ if (helpRequested) {
2005
+ this.outputHelp();
2006
+ this._exit(0, "commander.helpDisplayed", "(outputHelp)");
2007
+ }
2008
+ }
2009
+ }
2010
+ function incrementNodeInspectorPort(args) {
2011
+ return args.map((arg) => {
2012
+ if (!arg.startsWith("--inspect")) {
2013
+ return arg;
2014
+ }
2015
+ let debugOption;
2016
+ let debugHost = "127.0.0.1";
2017
+ let debugPort = "9229";
2018
+ let match;
2019
+ if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
2020
+ debugOption = match[1];
2021
+ } else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
2022
+ debugOption = match[1];
2023
+ if (/^\d+$/.test(match[3])) {
2024
+ debugPort = match[3];
2025
+ } else {
2026
+ debugHost = match[3];
2027
+ }
2028
+ } else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) {
2029
+ debugOption = match[1];
2030
+ debugHost = match[3];
2031
+ debugPort = match[4];
2032
+ }
2033
+ if (debugOption && debugPort !== "0") {
2034
+ return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
2035
+ }
2036
+ return arg;
2037
+ });
2038
+ }
2039
+ function useColor() {
2040
+ if (process2.env.NO_COLOR || process2.env.FORCE_COLOR === "0" || process2.env.FORCE_COLOR === "false")
2041
+ return false;
2042
+ if (process2.env.FORCE_COLOR || process2.env.CLICOLOR_FORCE !== undefined)
2043
+ return true;
2044
+ return;
2045
+ }
2046
+ exports.Command = Command;
2047
+ exports.useColor = useColor;
2048
+ });
2049
+
2050
+ // node_modules/commander/index.js
2051
+ var require_commander = __commonJS((exports) => {
2052
+ var { Argument } = require_argument();
2053
+ var { Command } = require_command();
2054
+ var { CommanderError, InvalidArgumentError } = require_error();
2055
+ var { Help } = require_help();
2056
+ var { Option } = require_option();
2057
+ exports.program = new Command;
2058
+ exports.createCommand = (name) => new Command(name);
2059
+ exports.createOption = (flags, description) => new Option(flags, description);
2060
+ exports.createArgument = (name, description) => new Argument(name, description);
2061
+ exports.Command = Command;
2062
+ exports.Option = Option;
2063
+ exports.Argument = Argument;
2064
+ exports.Help = Help;
2065
+ exports.CommanderError = CommanderError;
2066
+ exports.InvalidArgumentError = InvalidArgumentError;
2067
+ exports.InvalidOptionArgumentError = InvalidArgumentError;
2068
+ });
2069
+
2070
+ // node_modules/commander/esm.mjs
2071
+ var import__ = __toESM(require_commander(), 1);
2072
+ var {
2073
+ program,
2074
+ createCommand,
2075
+ createArgument,
2076
+ createOption,
2077
+ CommanderError,
2078
+ InvalidArgumentError,
2079
+ InvalidOptionArgumentError,
2080
+ Command,
2081
+ Argument,
2082
+ Option,
2083
+ Help
2084
+ } = import__.default;
2085
+
2086
+ // src/lib/package-version.ts
2087
+ import { existsSync, readFileSync } from "fs";
2088
+ import { dirname, join } from "path";
2089
+ import { fileURLToPath } from "url";
2090
+ function getPackageVersion(fromUrl = import.meta.url) {
2091
+ try {
2092
+ let dir = dirname(fileURLToPath(fromUrl));
2093
+ for (let i = 0;i < 5; i++) {
2094
+ const pkgPath = join(dir, "package.json");
2095
+ if (existsSync(pkgPath)) {
2096
+ return JSON.parse(readFileSync(pkgPath, "utf-8")).version || "0.0.0";
2097
+ }
2098
+ const parent = dirname(dir);
2099
+ if (parent === dir)
2100
+ break;
2101
+ dir = parent;
2102
+ }
2103
+ } catch {
2104
+ return "0.0.0";
2105
+ }
2106
+ return "0.0.0";
2107
+ }
2108
+
2109
+ // src/cli/commands/remote-commands.ts
2110
+ import chalk from "chalk";
2111
+
2112
+ // src/sdk/types.ts
2113
+ class TodosAPIError extends Error {
2114
+ status;
2115
+ statusText;
2116
+ body;
2117
+ constructor(message, status, statusText, body) {
2118
+ super(message);
2119
+ this.status = status;
2120
+ this.statusText = statusText;
2121
+ this.body = body;
2122
+ this.name = "TodosAPIError";
2123
+ }
2124
+ }
2125
+
2126
+ class TodosNotFoundError extends TodosAPIError {
2127
+ constructor(message, body) {
2128
+ super(message, 404, "Not Found", body);
2129
+ this.name = "TodosNotFoundError";
2130
+ }
2131
+ }
2132
+
2133
+ class TodosConflictError extends TodosAPIError {
2134
+ constructor(message, body) {
2135
+ super(message, 409, "Conflict", body);
2136
+ this.name = "TodosConflictError";
2137
+ }
2138
+ }
2139
+
2140
+ class TodosUnauthorizedError extends TodosAPIError {
2141
+ constructor(message, body) {
2142
+ super(message, 401, "Unauthorized", body);
2143
+ this.name = "TodosUnauthorizedError";
2144
+ }
2145
+ }
2146
+
2147
+ class TodosRateLimitError extends TodosAPIError {
2148
+ retryAfter;
2149
+ constructor(message, retryAfter, body) {
2150
+ super(message, 429, "Too Many Requests", body);
2151
+ this.retryAfter = retryAfter;
2152
+ this.name = "TodosRateLimitError";
2153
+ }
2154
+ }
2155
+
2156
+ class TodosTimeoutError extends Error {
2157
+ ms;
2158
+ constructor(ms) {
2159
+ super(`Request timed out after ${ms}ms`);
2160
+ this.ms = ms;
2161
+ this.name = "TodosTimeoutError";
2162
+ }
2163
+ }
2164
+
2165
+ // src/lib/config.ts
2166
+ import { existsSync as existsSync3 } from "fs";
2167
+ import { dirname as dirname2, join as join2 } from "path";
2168
+
2169
+ // src/lib/sync-utils.ts
2170
+ import { existsSync as existsSync2, mkdirSync, readFileSync as readFileSync2, readdirSync, statSync, writeFileSync } from "fs";
2171
+ var HOME = process.env["HOME"] || process.env["USERPROFILE"] || "~";
2172
+ function ensureDir(dir) {
2173
+ if (!existsSync2(dir))
2174
+ mkdirSync(dir, { recursive: true });
2175
+ }
2176
+ function readJsonFile(path) {
2177
+ try {
2178
+ return JSON.parse(readFileSync2(path, "utf-8"));
2179
+ } catch {
2180
+ return null;
2181
+ }
2182
+ }
2183
+ function writeJsonFile(path, data) {
2184
+ writeFileSync(path, JSON.stringify(data, null, 2) + `
2185
+ `);
2186
+ }
2187
+
2188
+ // src/lib/config.ts
2189
+ function getTodosGlobalDir() {
2190
+ const home = process.env["HOME"] || HOME;
2191
+ const newDir = join2(home, ".hasna", "todos");
2192
+ const legacyDir = join2(home, ".todos");
2193
+ const newConfig = join2(newDir, "config.json");
2194
+ const legacyConfig = join2(legacyDir, "config.json");
2195
+ if (!existsSync3(newConfig) && existsSync3(legacyConfig))
2196
+ return legacyDir;
2197
+ return newDir;
2198
+ }
2199
+ function getConfigPath() {
2200
+ return join2(getTodosGlobalDir(), "config.json");
2201
+ }
2202
+ var cached = null;
2203
+ function loadConfig() {
2204
+ if (cached)
2205
+ return cached;
2206
+ if (!existsSync3(getConfigPath())) {
2207
+ cached = {};
2208
+ return cached;
2209
+ }
2210
+ const config = readJsonFile(getConfigPath()) || {};
2211
+ if (typeof config.sync_agents === "string") {
2212
+ config.sync_agents = config.sync_agents.split(",").map((a) => a.trim()).filter(Boolean);
2213
+ }
2214
+ cached = config;
2215
+ return cached;
2216
+ }
2217
+ function saveConfig(config) {
2218
+ const configPath = getConfigPath();
2219
+ ensureDir(dirname2(configPath));
2220
+ writeJsonFile(configPath, config);
2221
+ cached = config;
2222
+ return config;
2223
+ }
2224
+ function updateConfig(patch) {
2225
+ return saveConfig({ ...loadConfig(), ...patch });
2226
+ }
2227
+ function normalizeApiUrl(value) {
2228
+ const trimmed = value?.trim();
2229
+ if (!trimmed)
2230
+ return null;
2231
+ return trimmed.replace(/\/+$/, "");
2232
+ }
2233
+ function normalizeMode(value) {
2234
+ return value === "local" || value === "remote" ? value : null;
2235
+ }
2236
+ function getRemoteApiConfig(env = process.env) {
2237
+ const config = loadConfig();
2238
+ const envApiUrl = normalizeApiUrl(env["TODOS_API_URL"]);
2239
+ const legacyEnvUrl = normalizeApiUrl(env["TODOS_URL"]);
2240
+ const configApiUrl = normalizeApiUrl(config.apiUrl);
2241
+ const apiUrl = envApiUrl ?? legacyEnvUrl ?? configApiUrl;
2242
+ const envMode = normalizeMode(env["TODOS_MODE"]);
2243
+ const configMode = normalizeMode(config.mode);
2244
+ const mode = envMode ?? configMode ?? (apiUrl ? "remote" : "local");
2245
+ const apiKey = env["TODOS_API_KEY"] || config.apiKey || null;
2246
+ return {
2247
+ mode,
2248
+ apiUrl: mode === "remote" ? apiUrl : null,
2249
+ apiKey,
2250
+ source: {
2251
+ mode: envMode ? "env" : configMode ? "config" : "derived",
2252
+ apiUrl: envApiUrl ? "TODOS_API_URL" : legacyEnvUrl ? "TODOS_URL" : configApiUrl ? "config" : "none",
2253
+ apiKey: env["TODOS_API_KEY"] ? "TODOS_API_KEY" : config.apiKey ? "config" : "none"
2254
+ }
2255
+ };
2256
+ }
2257
+
2258
+ // src/sdk/client.ts
2259
+ function buildQuery(params) {
2260
+ const search = new URLSearchParams;
2261
+ for (const [k, v] of Object.entries(params)) {
2262
+ if (v !== undefined)
2263
+ search.set(k, Array.isArray(v) ? v.join(",") : String(v));
2264
+ }
2265
+ const s = search.toString();
2266
+ return s ? `?${s}` : "";
2267
+ }
2268
+
2269
+ class TasksResource {
2270
+ client;
2271
+ constructor(client) {
2272
+ this.client = client;
2273
+ }
2274
+ async list(options) {
2275
+ return this.client._get("/api/tasks", buildQuery(options || {}));
2276
+ }
2277
+ async get(id, options) {
2278
+ return this.client._get(`/api/tasks/${id}`, buildQuery(options || {}));
2279
+ }
2280
+ async getWithRelations(id) {
2281
+ return this.client._get(`/api/tasks/${id}`, buildQuery({ format: "full" }));
2282
+ }
2283
+ async create(data) {
2284
+ return this.client._post("/api/tasks", data);
2285
+ }
2286
+ async update(id, data) {
2287
+ return this.client._patch(`/api/tasks/${id}`, data);
2288
+ }
2289
+ async delete(id) {
2290
+ return this.client._delete(`/api/tasks/${id}`);
2291
+ }
2292
+ async start(id, agentId) {
2293
+ return this.client._post(`/api/tasks/${id}/start`, { agent_id: agentId });
2294
+ }
2295
+ async complete(id, agentId) {
2296
+ return this.client._post(`/api/tasks/${id}/complete`, { agent_id: agentId });
2297
+ }
2298
+ async fail(id, options) {
2299
+ return this.client._post(`/api/tasks/${id}/fail`, options || {});
2300
+ }
2301
+ async logProgress(taskId, message, pctComplete, agentId) {
2302
+ return this.client._post(`/api/tasks/${taskId}/progress`, {
2303
+ message,
2304
+ pct_complete: pctComplete,
2305
+ agent_id: agentId
2306
+ });
2307
+ }
2308
+ async getProgress(id, options) {
2309
+ return this.client._get(`/api/tasks/${id}/progress`, buildQuery(options || {}));
2310
+ }
2311
+ async getHistory(id, options) {
2312
+ return this.client._get(`/api/tasks/${id}/history`, buildQuery(options || {}));
2313
+ }
2314
+ async getAttachments(id) {
2315
+ return this.client._get(`/api/tasks/${id}/attachments`);
2316
+ }
2317
+ async status(options) {
2318
+ return this.client._get("/api/tasks/status", buildQuery(options || {}));
2319
+ }
2320
+ async next(options) {
2321
+ return this.client._get("/api/tasks/next", buildQuery(options || {}));
2322
+ }
2323
+ async active(projectId) {
2324
+ return this.client._get("/api/tasks/active", projectId ? `?project_id=${projectId}` : "");
2325
+ }
2326
+ async stale(options) {
2327
+ return this.client._get("/api/tasks/stale", buildQuery(options || {}));
2328
+ }
2329
+ async changedSince(since, projectId, options) {
2330
+ return this.client._get("/api/tasks/changed", buildQuery({ since, project_id: projectId, fields: options?.fields }));
2331
+ }
2332
+ async context(options) {
2333
+ const q = buildQuery({
2334
+ agent_id: options?.agentId,
2335
+ project_id: options?.projectId,
2336
+ format: options?.format,
2337
+ fields: options?.fields
2338
+ });
2339
+ const url = `${this.client.baseUrl}/api/tasks/context${q}`;
2340
+ const res = await this.client._fetchRaw(url);
2341
+ if (!res.ok)
2342
+ return options?.format === "json" ? {} : "";
2343
+ if (options?.format === "json")
2344
+ return res.json();
2345
+ return res.text();
2346
+ }
2347
+ async export(options) {
2348
+ const q = buildQuery(options || {});
2349
+ const url = `${this.client.baseUrl}/api/tasks/export${q}`;
2350
+ const res = await this.client._fetchRaw(url);
2351
+ if (!res.ok)
2352
+ throw new TodosAPIError("Export failed", res.status, res.statusText, null);
2353
+ if (options?.format === "csv")
2354
+ return res.text();
2355
+ return res.json();
2356
+ }
2357
+ async bulk(ids, action) {
2358
+ return this.client._post("/api/tasks/bulk", { ids, action });
2359
+ }
2360
+ async claim(agentId, projectId) {
2361
+ return this.client._post("/api/tasks/claim", { agent_id: agentId, project_id: projectId });
2362
+ }
2363
+ async* subscribe(options = {}) {
2364
+ const q = buildQuery({
2365
+ agent_id: options.agentId,
2366
+ project_id: options.projectId,
2367
+ events: options.events?.join(",")
2368
+ });
2369
+ const url = `${this.client.baseUrl}/api/tasks/stream${q}`;
2370
+ const resp = await fetch(url);
2371
+ if (!resp.ok || !resp.body)
2372
+ throw new Error(`SSE connection failed: ${resp.status}`);
2373
+ const reader = resp.body.getReader();
2374
+ const decoder = new TextDecoder;
2375
+ let buffer = "";
2376
+ try {
2377
+ while (true) {
2378
+ const { done, value } = await reader.read();
2379
+ if (done)
2380
+ break;
2381
+ buffer += decoder.decode(value, { stream: true });
2382
+ const lines = buffer.split(`
2383
+ `);
2384
+ buffer = lines.pop() || "";
2385
+ for (const line of lines) {
2386
+ if (line.startsWith("data: ")) {
2387
+ try {
2388
+ const data = JSON.parse(line.slice(6));
2389
+ if (data.type !== "connected")
2390
+ yield data;
2391
+ } catch {}
2392
+ }
2393
+ }
2394
+ }
2395
+ } finally {
2396
+ reader.releaseLock();
2397
+ }
2398
+ }
2399
+ }
2400
+
2401
+ class AgentsResource {
2402
+ client;
2403
+ constructor(client) {
2404
+ this.client = client;
2405
+ }
2406
+ async list(options) {
2407
+ return this.client._get("/api/agents", buildQuery(options || {}));
2408
+ }
2409
+ async register(data) {
2410
+ return this.client._post("/api/agents", data);
2411
+ }
2412
+ async fullRegister(data) {
2413
+ return this.client._post("/api/agents", data);
2414
+ }
2415
+ async update(id, data) {
2416
+ return this.client._patch(`/api/agents/${id}`, data);
2417
+ }
2418
+ async delete(id) {
2419
+ return this.client._delete(`/api/agents/${id}`);
2420
+ }
2421
+ async bulkDelete(ids) {
2422
+ return this.client._post("/api/agents/bulk", { ids, action: "delete" });
2423
+ }
2424
+ async me(name) {
2425
+ return this.client._get("/api/agents/me", `?name=${encodeURIComponent(name)}`);
2426
+ }
2427
+ async queue(agentId) {
2428
+ return this.client._get(`/api/agents/${encodeURIComponent(agentId)}/queue`);
2429
+ }
2430
+ async team(agentId) {
2431
+ return this.client._get(`/api/agents/${encodeURIComponent(agentId)}/team`);
2432
+ }
2433
+ async orgChart() {
2434
+ return this.client._get("/api/org");
2435
+ }
2436
+ }
2437
+
2438
+ class ProjectsResource {
2439
+ client;
2440
+ constructor(client) {
2441
+ this.client = client;
2442
+ }
2443
+ async list(options) {
2444
+ return this.client._get("/api/projects", buildQuery(options || {}));
2445
+ }
2446
+ async create(data) {
2447
+ return this.client._post("/api/projects", data);
2448
+ }
2449
+ async delete(id) {
2450
+ return this.client._delete(`/api/projects/${id}`);
2451
+ }
2452
+ async bulkDelete(ids) {
2453
+ return this.client._post("/api/projects/bulk", { ids, action: "delete" });
2454
+ }
2455
+ }
2456
+
2457
+ class PlansResource {
2458
+ client;
2459
+ constructor(client) {
2460
+ this.client = client;
2461
+ }
2462
+ async list(projectId) {
2463
+ return this.client._get("/api/plans", projectId ? `?project_id=${projectId}` : "");
2464
+ }
2465
+ async create(data) {
2466
+ return this.client._post("/api/plans", data);
2467
+ }
2468
+ async get(id) {
2469
+ return this.client._get(`/api/plans/${id}`);
2470
+ }
2471
+ async update(id, data) {
2472
+ return this.client._patch(`/api/plans/${id}`, data);
2473
+ }
2474
+ async delete(id) {
2475
+ return this.client._delete(`/api/plans/${id}`);
2476
+ }
2477
+ async bulkDelete(ids) {
2478
+ return this.client._post("/api/plans/bulk", { ids, action: "delete" });
2479
+ }
2480
+ }
2481
+
2482
+ class OrgsResource {
2483
+ client;
2484
+ constructor(client) {
2485
+ this.client = client;
2486
+ }
2487
+ async list() {
2488
+ return this.client._get("/api/orgs");
2489
+ }
2490
+ async create(data) {
2491
+ return this.client._post("/api/orgs", data);
2492
+ }
2493
+ async update(id, data) {
2494
+ return this.client._patch(`/api/orgs/${id}`, data);
2495
+ }
2496
+ async delete(id) {
2497
+ return this.client._delete(`/api/orgs/${id}`);
2498
+ }
2499
+ }
2500
+
2501
+ class WebhooksResource {
2502
+ client;
2503
+ constructor(client) {
2504
+ this.client = client;
2505
+ }
2506
+ async list() {
2507
+ return this.client._get("/api/webhooks");
2508
+ }
2509
+ async create(data) {
2510
+ return this.client._post("/api/webhooks", data);
2511
+ }
2512
+ async delete(id) {
2513
+ return this.client._delete(`/api/webhooks/${id}`);
2514
+ }
2515
+ }
2516
+
2517
+ class TemplatesResource {
2518
+ client;
2519
+ constructor(client) {
2520
+ this.client = client;
2521
+ }
2522
+ async list() {
2523
+ return this.client._get("/api/templates");
2524
+ }
2525
+ async create(data) {
2526
+ return this.client._post("/api/templates", data);
2527
+ }
2528
+ async delete(id) {
2529
+ return this.client._delete(`/api/templates/${id}`);
2530
+ }
2531
+ }
2532
+
2533
+ class TodosClient {
2534
+ baseUrl;
2535
+ timeout;
2536
+ apiKey;
2537
+ maxRetries;
2538
+ retryDelay;
2539
+ tasks;
2540
+ agents;
2541
+ projects;
2542
+ plans;
2543
+ orgs;
2544
+ webhooks;
2545
+ templates;
2546
+ constructor(options = {}) {
2547
+ const remoteConfig = getRemoteApiConfig();
2548
+ this.baseUrl = normalizeApiUrl(options.baseUrl) || remoteConfig.apiUrl || normalizeApiUrl(process.env["TODOS_URL"]) || "http://localhost:19427";
2549
+ this.timeout = options.timeout ?? 1e4;
2550
+ this.apiKey = options.apiKey || remoteConfig.apiKey;
2551
+ this.maxRetries = options.maxRetries ?? 0;
2552
+ this.retryDelay = options.retryDelay ?? 1000;
2553
+ this.tasks = new TasksResource(this);
2554
+ this.agents = new AgentsResource(this);
2555
+ this.projects = new ProjectsResource(this);
2556
+ this.plans = new PlansResource(this);
2557
+ this.orgs = new OrgsResource(this);
2558
+ this.webhooks = new WebhooksResource(this);
2559
+ this.templates = new TemplatesResource(this);
2560
+ }
2561
+ static fromEnv(apiKey) {
2562
+ return new TodosClient({ apiKey });
2563
+ }
2564
+ async _fetchRaw(url, init) {
2565
+ const controller = new AbortController;
2566
+ const timer = setTimeout(() => controller.abort(), this.timeout);
2567
+ try {
2568
+ const headers = this._buildHeaders(init?.headers);
2569
+ return await fetch(url, { ...init, headers, signal: controller.signal });
2570
+ } finally {
2571
+ clearTimeout(timer);
2572
+ }
2573
+ }
2574
+ _buildHeaders(existing) {
2575
+ const headers = { "Content-Type": "application/json" };
2576
+ if (this.apiKey)
2577
+ headers["x-api-key"] = this.apiKey;
2578
+ if (existing) {
2579
+ if (existing instanceof Headers) {
2580
+ existing.forEach((v, k) => {
2581
+ headers[k] = v;
2582
+ });
2583
+ } else if (Array.isArray(existing)) {
2584
+ for (const [k, v] of existing)
2585
+ headers[k] = v;
2586
+ } else {
2587
+ Object.assign(headers, existing);
2588
+ }
2589
+ }
2590
+ return headers;
2591
+ }
2592
+ async _fetchWithRetry(path, init) {
2593
+ let lastError = null;
2594
+ const maxAttempts = this.maxRetries + 1;
2595
+ for (let attempt = 0;attempt < maxAttempts; attempt++) {
2596
+ try {
2597
+ return await this._fetch(path, init);
2598
+ } catch (e) {
2599
+ lastError = e;
2600
+ if (e instanceof TodosAPIError && e.status < 500 && e.status !== 429)
2601
+ throw e;
2602
+ if (e instanceof TodosUnauthorizedError || e instanceof TodosNotFoundError || e instanceof TodosConflictError)
2603
+ throw e;
2604
+ if (attempt < maxAttempts - 1) {
2605
+ const delay = this.retryDelay * Math.pow(2, attempt);
2606
+ if (e instanceof TodosRateLimitError) {
2607
+ await this._sleep(e.retryAfter * 1000);
2608
+ } else {
2609
+ await this._sleep(delay);
2610
+ }
2611
+ }
2612
+ }
2613
+ }
2614
+ throw lastError || new Error("Request failed after retries");
2615
+ }
2616
+ async _fetch(path, init) {
2617
+ const controller = new AbortController;
2618
+ const timer = setTimeout(() => controller.abort(), this.timeout);
2619
+ try {
2620
+ const url = `${this.baseUrl}${path}`;
2621
+ const headers = this._buildHeaders(init?.headers);
2622
+ const res = await fetch(url, { ...init, headers, signal: controller.signal });
2623
+ if (!res.ok) {
2624
+ const body = await res.json().catch(() => ({ error: res.statusText }));
2625
+ const message = body.error || `HTTP ${res.status}: ${res.statusText}`;
2626
+ if (res.status === 401)
2627
+ throw new TodosUnauthorizedError(message, body);
2628
+ if (res.status === 404)
2629
+ throw new TodosNotFoundError(message, body);
2630
+ if (res.status === 409)
2631
+ throw new TodosConflictError(message, body);
2632
+ if (res.status === 429) {
2633
+ const retryAfter = parseInt(res.headers.get("retry-after") || "60", 10);
2634
+ throw new TodosRateLimitError(message, retryAfter, body);
2635
+ }
2636
+ throw new TodosAPIError(message, res.status, res.statusText, body);
2637
+ }
2638
+ const contentLength = res.headers.get("content-length");
2639
+ if (contentLength === "0" || contentLength === "4")
2640
+ return null;
2641
+ return res.json();
2642
+ } catch (e) {
2643
+ if (e instanceof DOMException && e.name === "AbortError") {
2644
+ throw new TodosTimeoutError(this.timeout);
2645
+ }
2646
+ throw e;
2647
+ } finally {
2648
+ clearTimeout(timer);
2649
+ }
2650
+ }
2651
+ async _get(path, query = "") {
2652
+ return this._fetchWithRetry(`${path}${query}`);
2653
+ }
2654
+ async _post(path, body) {
2655
+ return this._fetchWithRetry(path, {
2656
+ method: "POST",
2657
+ body: body ? JSON.stringify(body) : undefined
2658
+ });
2659
+ }
2660
+ async _patch(path, body) {
2661
+ return this._fetchWithRetry(path, {
2662
+ method: "PATCH",
2663
+ body: JSON.stringify(body)
2664
+ });
2665
+ }
2666
+ async _delete(path) {
2667
+ return this._fetchWithRetry(path, { method: "DELETE" });
2668
+ }
2669
+ _sleep(ms) {
2670
+ return new Promise((resolve) => setTimeout(resolve, ms));
2671
+ }
2672
+ async getHealth() {
2673
+ return this._get("/api/health");
2674
+ }
2675
+ async isAlive() {
2676
+ try {
2677
+ await this._get("/api/stats");
2678
+ return true;
2679
+ } catch {
2680
+ return false;
2681
+ }
2682
+ }
2683
+ async getStats() {
2684
+ return this._get("/api/stats");
2685
+ }
2686
+ async getReport(options) {
2687
+ return this._get("/api/report", buildQuery({ days: options?.days, project_id: options?.projectId }));
2688
+ }
2689
+ async doctor() {
2690
+ return this._get("/api/doctor");
2691
+ }
2692
+ async activity(limit) {
2693
+ return this._get("/api/activity", limit ? `?limit=${limit}` : "");
2694
+ }
2695
+ async listTasks(filter = {}) {
2696
+ return this.tasks.list(filter);
2697
+ }
2698
+ async getTask(id, options) {
2699
+ return this.tasks.get(id, options);
2700
+ }
2701
+ async createTask(data) {
2702
+ return this.tasks.create(data);
2703
+ }
2704
+ async updateTask(id, data) {
2705
+ return this.tasks.update(id, data);
2706
+ }
2707
+ async deleteTask(id) {
2708
+ await this.tasks.delete(id);
2709
+ }
2710
+ async startTask(id, agentId) {
2711
+ return this.tasks.start(id, agentId);
2712
+ }
2713
+ async completeTask(id, agentId) {
2714
+ return this.tasks.complete(id, agentId);
2715
+ }
2716
+ async failTask(id, options = {}) {
2717
+ return this.tasks.fail(id, options);
2718
+ }
2719
+ async logProgress(taskId, message, pctComplete, agentId) {
2720
+ return this.tasks.logProgress(taskId, message, pctComplete, agentId);
2721
+ }
2722
+ async getStatus(projectId, agentId) {
2723
+ return this.tasks.status({ project_id: projectId, agent_id: agentId });
2724
+ }
2725
+ async getActiveWork(projectId) {
2726
+ const res = await this.tasks.active(projectId);
2727
+ return res.active;
2728
+ }
2729
+ async getTasksChangedSince(since, projectId) {
2730
+ return this.tasks.changedSince(since, projectId);
2731
+ }
2732
+ async getStaleTasks(minutes, projectId) {
2733
+ return this.tasks.stale({ minutes, project_id: projectId });
2734
+ }
2735
+ async getContext(options = {}) {
2736
+ return this.tasks.context(options);
2737
+ }
2738
+ async exportTasks(filter = {}) {
2739
+ return this.tasks.export(filter);
2740
+ }
2741
+ async claimNextTask(agentId, projectId) {
2742
+ return this.tasks.claim(agentId, projectId);
2743
+ }
2744
+ async getTaskHistory(id, options) {
2745
+ return this.tasks.getHistory(id, options);
2746
+ }
2747
+ async getTaskAttachments(id) {
2748
+ return this.tasks.getAttachments(id);
2749
+ }
2750
+ async getTaskProgress(id, options) {
2751
+ return this.tasks.getProgress(id, options);
2752
+ }
2753
+ async* subscribeToStream(options = {}) {
2754
+ yield* this.tasks.subscribe(options);
2755
+ }
2756
+ async getProjects() {
2757
+ return this.projects.list();
2758
+ }
2759
+ }
2760
+
2761
+ // src/cli/commands/remote-commands.ts
2762
+ function redact(value) {
2763
+ if (!value)
2764
+ return null;
2765
+ if (value.length <= 8)
2766
+ return "***";
2767
+ return `${value.slice(0, 4)}...${value.slice(-4)}`;
2768
+ }
2769
+ function output(data, jsonMode) {
2770
+ if (jsonMode)
2771
+ console.log(JSON.stringify(data, null, 2));
2772
+ }
2773
+ function formatTaskLine(task) {
2774
+ const id = task.short_id || task.id.slice(0, 8);
2775
+ const status = task.status || "unknown";
2776
+ const priority = task.priority || "medium";
2777
+ const assigned = task.assigned_to ? ` -> ${task.assigned_to}` : "";
2778
+ return `${chalk.dim(id)} ${status.padEnd(11)} ${priority.padEnd(8)} ${task.title}${assigned}`;
2779
+ }
2780
+ function remoteClient() {
2781
+ const remote = getRemoteApiConfig();
2782
+ if (remote.mode !== "remote" || !remote.apiUrl) {
2783
+ console.error(chalk.red("Remote mode requires TODOS_API_URL or todos config --set apiUrl=<url>."));
2784
+ process.exit(1);
2785
+ }
2786
+ return new TodosClient({ baseUrl: remote.apiUrl, apiKey: remote.apiKey || undefined });
2787
+ }
2788
+ function parseTags(tags) {
2789
+ return tags ? tags.split(",").map((tag) => tag.trim()).filter(Boolean) : undefined;
2790
+ }
2791
+ function parseSetArg(raw) {
2792
+ const index = raw.indexOf("=");
2793
+ if (index <= 0) {
2794
+ console.error(chalk.red("Expected --set key=value."));
2795
+ process.exit(1);
2796
+ }
2797
+ return [raw.slice(0, index), raw.slice(index + 1)];
2798
+ }
2799
+ function printableRemoteConfig() {
2800
+ const remote = getRemoteApiConfig();
2801
+ return {
2802
+ mode: remote.mode,
2803
+ apiUrl: remote.apiUrl,
2804
+ apiKey: redact(remote.apiKey),
2805
+ source: remote.source
2806
+ };
2807
+ }
2808
+ function registerRemoteCommands(program2) {
2809
+ program2.description("Remote todos client for hosted compatible APIs").addHelpText("after", `
2810
+ Remote mode uses TODOS_API_URL/config apiUrl and TODOS_API_KEY/config apiKey. Local SQLite is not initialized.`);
2811
+ program2.command("login").description("Configure remote API URL and API key").option("--api-url <url>", "Hosted todos API URL").option("--api-key <key>", "API key/token").action((opts) => {
2812
+ const apiUrl = normalizeApiUrl(opts.apiUrl);
2813
+ const patch = { mode: "remote" };
2814
+ if (apiUrl)
2815
+ patch.apiUrl = apiUrl;
2816
+ if (opts.apiKey)
2817
+ patch.apiKey = opts.apiKey;
2818
+ const next = updateConfig(patch);
2819
+ const globalOpts = program2.opts();
2820
+ if (globalOpts.json) {
2821
+ output({ mode: next.mode, apiUrl: next.apiUrl ?? null, apiKey: redact(next.apiKey) }, true);
2822
+ } else {
2823
+ console.log(chalk.green("Remote todos configured."));
2824
+ if (next.apiUrl)
2825
+ console.log(chalk.dim(` API: ${next.apiUrl}`));
2826
+ console.log(chalk.dim(` API key: ${redact(next.apiKey) || "(not configured)"}`));
2827
+ }
2828
+ });
2829
+ program2.command("logout").description("Remove stored remote API key and return to local mode").action(() => {
2830
+ const current = loadConfig();
2831
+ const next = updateConfig({ ...current, mode: "local", apiKey: undefined });
2832
+ const globalOpts = program2.opts();
2833
+ if (globalOpts.json)
2834
+ output({ mode: next.mode ?? "local", apiKey: null }, true);
2835
+ else
2836
+ console.log(chalk.green("Remote API key removed. Local mode restored."));
2837
+ });
2838
+ program2.command("config").description("View or update remote configuration").option("--get <key>", "Get a config value").option("--set <key=value>", "Set apiUrl, apiKey, or mode").action((opts) => {
2839
+ const globalOpts = program2.opts();
2840
+ if (opts.set) {
2841
+ const [key, value] = parseSetArg(opts.set);
2842
+ if (!["apiUrl", "apiKey", "mode"].includes(key)) {
2843
+ console.error(chalk.red(`Unsupported remote config key: ${key}`));
2844
+ process.exit(1);
2845
+ }
2846
+ if (key === "mode" && value !== "local" && value !== "remote") {
2847
+ console.error(chalk.red("mode must be local or remote."));
2848
+ process.exit(1);
2849
+ }
2850
+ const patch = key === "apiUrl" ? { apiUrl: normalizeApiUrl(value) || undefined, mode: "remote" } : key === "apiKey" ? { apiKey: value } : { mode: value };
2851
+ const next = updateConfig(patch);
2852
+ if (globalOpts.json)
2853
+ output({ ...next, apiKey: redact(next.apiKey) }, true);
2854
+ else
2855
+ console.log(chalk.green(`Set ${key}.`));
2856
+ return;
2857
+ }
2858
+ const printable = printableRemoteConfig();
2859
+ if (opts.get) {
2860
+ const value = printable[opts.get];
2861
+ if (globalOpts.json)
2862
+ output(value ?? null, true);
2863
+ else
2864
+ console.log(value ?? "");
2865
+ return;
2866
+ }
2867
+ if (globalOpts.json)
2868
+ output(printable, true);
2869
+ else
2870
+ console.log(JSON.stringify(printable, null, 2));
2871
+ });
2872
+ program2.command("add <title>").description("Create a new remote task").option("-d, --description <text>", "Task description").option("-p, --priority <level>", "Priority: low, medium, high, critical").option("-t, --tags <tags>", "Comma-separated tags").option("--tag <tags>", "Comma-separated tags (alias for --tags)").option("--plan <id>", "Assign to a plan").option("--assign <agent>", "Assign to agent").option("--project <id>", "Assign to project").action(async (title, opts) => {
2873
+ const globalOpts = program2.opts();
2874
+ const task = await remoteClient().tasks.create({
2875
+ title,
2876
+ description: opts.description,
2877
+ priority: opts.priority,
2878
+ project_id: opts.project || globalOpts.project,
2879
+ plan_id: opts.plan,
2880
+ assigned_to: opts.assign,
2881
+ tags: parseTags(opts.tags || opts.tag)
2882
+ });
2883
+ if (globalOpts.json)
2884
+ output(task, true);
2885
+ else {
2886
+ console.log(chalk.green("Task created:"));
2887
+ console.log(formatTaskLine(task));
2888
+ }
2889
+ });
2890
+ program2.command("list").description("List remote tasks").option("-s, --status <status>", "Filter by status").option("-p, --priority <priority>", "Filter by priority").option("--assigned <agent>", "Filter by assigned agent").option("--project <id>", "Filter by project").option("--limit <n>", "Max tasks to return").option("--format <fmt>", "Output format: table, compact, json").action(async (opts) => {
2891
+ const globalOpts = program2.opts();
2892
+ const tasks = await remoteClient().tasks.list({
2893
+ status: opts.status,
2894
+ project_id: opts.project || globalOpts.project,
2895
+ assigned_to: opts.assigned,
2896
+ limit: opts.limit ? Number.parseInt(opts.limit, 10) : undefined
2897
+ });
2898
+ const filtered = opts.priority ? tasks.filter((task) => task.priority === opts.priority) : tasks;
2899
+ const fmt = opts.format || (globalOpts.json ? "json" : "table");
2900
+ if (fmt === "json") {
2901
+ output(filtered, true);
2902
+ } else if (fmt === "compact") {
2903
+ for (const task of filtered)
2904
+ process.stdout.write(`${formatTaskLine(task)}
2905
+ `);
2906
+ } else if (filtered.length === 0) {
2907
+ console.log(chalk.dim("No tasks found."));
2908
+ } else {
2909
+ console.log(chalk.bold(`${filtered.length} task(s):
2910
+ `));
2911
+ for (const task of filtered)
2912
+ console.log(formatTaskLine(task));
2913
+ }
2914
+ });
2915
+ program2.command("show <id>").description("Show a remote task").action(async (id) => {
2916
+ const globalOpts = program2.opts();
2917
+ const task = await remoteClient().tasks.get(id);
2918
+ if (globalOpts.json)
2919
+ output(task, true);
2920
+ else
2921
+ console.log(JSON.stringify(task, null, 2));
2922
+ });
2923
+ program2.command("update <id>").description("Update a remote task").option("--title <text>", "New title").option("-d, --description <text>", "New description").option("-s, --status <status>", "New status").option("-p, --priority <priority>", "New priority").option("--assign <agent>", "Assign to agent").option("--tags <tags>", "New tags").action(async (id, opts) => {
2924
+ const globalOpts = program2.opts();
2925
+ const patch = {};
2926
+ if (opts.title !== undefined)
2927
+ patch["title"] = opts.title;
2928
+ if (opts.description !== undefined)
2929
+ patch["description"] = opts.description;
2930
+ if (opts.status !== undefined)
2931
+ patch["status"] = opts.status;
2932
+ if (opts.priority !== undefined)
2933
+ patch["priority"] = opts.priority;
2934
+ if (opts.assign !== undefined)
2935
+ patch["assigned_to"] = opts.assign;
2936
+ if (opts.tags !== undefined)
2937
+ patch["tags"] = parseTags(opts.tags);
2938
+ const task = await remoteClient().tasks.update(id, patch);
2939
+ if (globalOpts.json)
2940
+ output(task, true);
2941
+ else {
2942
+ console.log(chalk.green("Task updated:"));
2943
+ console.log(formatTaskLine(task));
2944
+ }
2945
+ });
2946
+ program2.command("start <id>").description("Start a remote task").action(async (id) => {
2947
+ const globalOpts = program2.opts();
2948
+ const task = await remoteClient().tasks.start(id, globalOpts.agent);
2949
+ if (globalOpts.json)
2950
+ output(task, true);
2951
+ else
2952
+ console.log(formatTaskLine(task));
2953
+ });
2954
+ program2.command("done <id>").description("Complete a remote task").action(async (id) => {
2955
+ const globalOpts = program2.opts();
2956
+ const task = await remoteClient().tasks.complete(id, globalOpts.agent);
2957
+ if (globalOpts.json)
2958
+ output(task, true);
2959
+ else
2960
+ console.log(formatTaskLine(task));
2961
+ });
2962
+ program2.command("delete <id>").description("Delete a remote task").action(async (id) => {
2963
+ const globalOpts = program2.opts();
2964
+ const result = await remoteClient().tasks.delete(id);
2965
+ if (globalOpts.json)
2966
+ output(result, true);
2967
+ else
2968
+ console.log(chalk.green("Task deleted."));
2969
+ });
2970
+ program2.command("count").description("Show remote task counts").action(async () => {
2971
+ const globalOpts = program2.opts();
2972
+ const status = await remoteClient().tasks.status({ project_id: globalOpts.project });
2973
+ if (globalOpts.json)
2974
+ output(status, true);
2975
+ else
2976
+ console.log(`total: ${status.total} pending: ${status.pending} in_progress: ${status.in_progress} completed: ${status.completed}`);
2977
+ });
2978
+ program2.command("status").description("Show remote project health").option("--agent <id>", "Include next task for this agent").option("--project <id>", "Filter to project").option("-j, --json", "Output as JSON").action(async (opts) => {
2979
+ const status = await remoteClient().tasks.status({
2980
+ project_id: opts.project || program2.opts().project,
2981
+ agent_id: opts.agent
2982
+ });
2983
+ if (opts.json || program2.opts().json)
2984
+ output(status, true);
2985
+ else
2986
+ console.log(`Tasks: ${status.pending} pending | ${status.in_progress} active | ${status.completed} done | ${status.total} total`);
2987
+ });
2988
+ program2.command("next").description("Show the best remote pending task").option("--agent <id>", "Prefer tasks assigned to this agent").option("--project <id>", "Filter to project").option("-j, --json", "Output as JSON").action(async (opts) => {
2989
+ const result = await remoteClient().tasks.next({
2990
+ agent_id: opts.agent,
2991
+ project_id: opts.project || program2.opts().project
2992
+ });
2993
+ if (opts.json || program2.opts().json)
2994
+ output(result.task, true);
2995
+ else if (!result.task)
2996
+ console.log(chalk.dim("No tasks available."));
2997
+ else
2998
+ console.log(formatTaskLine(result.task));
2999
+ });
3000
+ program2.command("claim <agent>").description("Claim the best remote pending task for an agent").option("--project <id>", "Filter to project").option("-j, --json", "Output as JSON").action(async (agent, opts) => {
3001
+ const result = await remoteClient().tasks.claim(agent, opts.project || program2.opts().project);
3002
+ if (opts.json || program2.opts().json)
3003
+ output(result, true);
3004
+ else if (!result.task)
3005
+ console.log(chalk.dim("No tasks available to claim."));
3006
+ else
3007
+ console.log(chalk.green(`Claimed: ${formatTaskLine(result.task)}`));
3008
+ });
3009
+ program2.command("projects").description("List or create remote projects").option("--add <path>", "Register a project path").option("--name <name>", "Project name").action(async (opts) => {
3010
+ const globalOpts = program2.opts();
3011
+ if (opts.add) {
3012
+ const project = await remoteClient().projects.create({
3013
+ name: opts.name || opts.add.split(/[\\/]/).filter(Boolean).at(-1) || opts.add,
3014
+ path: opts.add
3015
+ });
3016
+ if (globalOpts.json)
3017
+ output(project, true);
3018
+ else
3019
+ console.log(chalk.green(`Project registered: ${project.name}`));
3020
+ return;
3021
+ }
3022
+ const projects = await remoteClient().projects.list();
3023
+ if (globalOpts.json)
3024
+ output(projects, true);
3025
+ else
3026
+ for (const project of projects)
3027
+ console.log(`${project.id.slice(0, 8)} ${project.name} ${project.path}`);
3028
+ });
3029
+ program2.command("agents").description("List remote agents").action(async () => {
3030
+ const globalOpts = program2.opts();
3031
+ const agents = await remoteClient().agents.list();
3032
+ if (globalOpts.json)
3033
+ output(agents, true);
3034
+ else
3035
+ for (const agent of agents)
3036
+ console.log(`${agent.id.slice(0, 8)} ${agent.name} ${agent.status}`);
3037
+ });
3038
+ program2.command("search <query>").description("Search remote tasks client-side").option("-j, --json", "Output as JSON").action(async (query, opts) => {
3039
+ const lowered = query.toLowerCase();
3040
+ const tasks = await remoteClient().tasks.list({ limit: 1000 });
3041
+ const matches = tasks.filter((task) => task.title.toLowerCase().includes(lowered) || String(task.description || "").toLowerCase().includes(lowered));
3042
+ if (opts.json || program2.opts().json)
3043
+ output(matches, true);
3044
+ else
3045
+ for (const task of matches)
3046
+ console.log(formatTaskLine(task));
3047
+ });
3048
+ }
3049
+
3050
+ // src/cli/remote-index.ts
3051
+ var program2 = new Command;
3052
+ program2.name("todos-remote").description("Remote-only todos CLI for hosted compatible APIs").version(getPackageVersion()).option("--project <id>", "Remote project ID").option("-j, --json", "Output as JSON").option("--agent <name>", "Agent name").option("--session <id>", "Session ID");
3053
+ registerRemoteCommands(program2);
3054
+ program2.parse();