unity-agentic-tools 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of unity-agentic-tools might be problematic. Click here for more details.

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