@axiomify/cli 4.0.0 → 5.0.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.
Files changed (4) hide show
  1. package/README.md +56 -52
  2. package/dist/index.js +3199 -59
  3. package/dist/index.mjs +3198 -59
  4. package/package.json +5 -2
package/dist/index.mjs CHANGED
@@ -1,32 +1,2919 @@
1
1
  #!/usr/bin/env node
2
- import { Command } from 'commander';
3
2
  import * as esbuild from 'esbuild';
4
3
  import path4 from 'path';
5
4
  import fs, { existsSync } from 'fs';
6
5
  import { spawn } from 'child_process';
6
+ import { prompt } from 'enquirer';
7
+ import { execa } from 'execa';
7
8
  import fs2 from 'fs/promises';
9
+ import pc from 'picocolors';
8
10
 
11
+ var __create = Object.create;
12
+ var __defProp = Object.defineProperty;
13
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
14
+ var __getOwnPropNames = Object.getOwnPropertyNames;
15
+ var __getProtoOf = Object.getPrototypeOf;
16
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
17
  var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
10
18
  get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
11
19
  }) : x)(function(x) {
12
20
  if (typeof require !== "undefined") return require.apply(this, arguments);
13
21
  throw Error('Dynamic require of "' + x + '" is not supported');
14
22
  });
23
+ var __commonJS = (cb, mod) => function __require2() {
24
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
25
+ };
26
+ var __copyProps = (to, from, except, desc) => {
27
+ if (from && typeof from === "object" || typeof from === "function") {
28
+ for (let key of __getOwnPropNames(from))
29
+ if (!__hasOwnProp.call(to, key) && key !== except)
30
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
31
+ }
32
+ return to;
33
+ };
34
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
35
+ // If the importer is in node compatibility mode or this is not an ESM
36
+ // file that has been converted to a CommonJS file using a Babel-
37
+ // compatible transform (i.e. "__esModule" has not been set), then set
38
+ // "default" to the CommonJS "module.exports" for node compatibility.
39
+ __defProp(target, "default", { value: mod, enumerable: true }) ,
40
+ mod
41
+ ));
42
+
43
+ // node_modules/commander/lib/error.js
44
+ var require_error = __commonJS({
45
+ "node_modules/commander/lib/error.js"(exports) {
46
+ var CommanderError2 = class extends Error {
47
+ /**
48
+ * Constructs the CommanderError class
49
+ * @param {number} exitCode suggested exit code which could be used with process.exit
50
+ * @param {string} code an id string representing the error
51
+ * @param {string} message human-readable description of the error
52
+ * @constructor
53
+ */
54
+ constructor(exitCode, code, message) {
55
+ super(message);
56
+ Error.captureStackTrace(this, this.constructor);
57
+ this.name = this.constructor.name;
58
+ this.code = code;
59
+ this.exitCode = exitCode;
60
+ this.nestedError = void 0;
61
+ }
62
+ };
63
+ var InvalidArgumentError2 = class extends CommanderError2 {
64
+ /**
65
+ * Constructs the InvalidArgumentError class
66
+ * @param {string} [message] explanation of why argument is invalid
67
+ * @constructor
68
+ */
69
+ constructor(message) {
70
+ super(1, "commander.invalidArgument", message);
71
+ Error.captureStackTrace(this, this.constructor);
72
+ this.name = this.constructor.name;
73
+ }
74
+ };
75
+ exports.CommanderError = CommanderError2;
76
+ exports.InvalidArgumentError = InvalidArgumentError2;
77
+ }
78
+ });
79
+
80
+ // node_modules/commander/lib/argument.js
81
+ var require_argument = __commonJS({
82
+ "node_modules/commander/lib/argument.js"(exports) {
83
+ var { InvalidArgumentError: InvalidArgumentError2 } = require_error();
84
+ var Argument2 = class {
85
+ /**
86
+ * Initialize a new command argument with the given name and description.
87
+ * The default is that the argument is required, and you can explicitly
88
+ * indicate this with <> around the name. Put [] around the name for an optional argument.
89
+ *
90
+ * @param {string} name
91
+ * @param {string} [description]
92
+ */
93
+ constructor(name, description) {
94
+ this.description = description || "";
95
+ this.variadic = false;
96
+ this.parseArg = void 0;
97
+ this.defaultValue = void 0;
98
+ this.defaultValueDescription = void 0;
99
+ this.argChoices = void 0;
100
+ switch (name[0]) {
101
+ case "<":
102
+ this.required = true;
103
+ this._name = name.slice(1, -1);
104
+ break;
105
+ case "[":
106
+ this.required = false;
107
+ this._name = name.slice(1, -1);
108
+ break;
109
+ default:
110
+ this.required = true;
111
+ this._name = name;
112
+ break;
113
+ }
114
+ if (this._name.length > 3 && this._name.slice(-3) === "...") {
115
+ this.variadic = true;
116
+ this._name = this._name.slice(0, -3);
117
+ }
118
+ }
119
+ /**
120
+ * Return argument name.
121
+ *
122
+ * @return {string}
123
+ */
124
+ name() {
125
+ return this._name;
126
+ }
127
+ /**
128
+ * @package internal use only
129
+ */
130
+ _concatValue(value, previous) {
131
+ if (previous === this.defaultValue || !Array.isArray(previous)) {
132
+ return [value];
133
+ }
134
+ return previous.concat(value);
135
+ }
136
+ /**
137
+ * Set the default value, and optionally supply the description to be displayed in the help.
138
+ *
139
+ * @param {*} value
140
+ * @param {string} [description]
141
+ * @return {Argument}
142
+ */
143
+ default(value, description) {
144
+ this.defaultValue = value;
145
+ this.defaultValueDescription = description;
146
+ return this;
147
+ }
148
+ /**
149
+ * Set the custom handler for processing CLI command arguments into argument values.
150
+ *
151
+ * @param {Function} [fn]
152
+ * @return {Argument}
153
+ */
154
+ argParser(fn) {
155
+ this.parseArg = fn;
156
+ return this;
157
+ }
158
+ /**
159
+ * Only allow argument value to be one of choices.
160
+ *
161
+ * @param {string[]} values
162
+ * @return {Argument}
163
+ */
164
+ choices(values) {
165
+ this.argChoices = values.slice();
166
+ this.parseArg = (arg, previous) => {
167
+ if (!this.argChoices.includes(arg)) {
168
+ throw new InvalidArgumentError2(`Allowed choices are ${this.argChoices.join(", ")}.`);
169
+ }
170
+ if (this.variadic) {
171
+ return this._concatValue(arg, previous);
172
+ }
173
+ return arg;
174
+ };
175
+ return this;
176
+ }
177
+ /**
178
+ * Make argument required.
179
+ */
180
+ argRequired() {
181
+ this.required = true;
182
+ return this;
183
+ }
184
+ /**
185
+ * Make argument optional.
186
+ */
187
+ argOptional() {
188
+ this.required = false;
189
+ return this;
190
+ }
191
+ };
192
+ function humanReadableArgName(arg) {
193
+ const nameOutput = arg.name() + (arg.variadic === true ? "..." : "");
194
+ return arg.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
195
+ }
196
+ exports.Argument = Argument2;
197
+ exports.humanReadableArgName = humanReadableArgName;
198
+ }
199
+ });
200
+
201
+ // node_modules/commander/lib/help.js
202
+ var require_help = __commonJS({
203
+ "node_modules/commander/lib/help.js"(exports) {
204
+ var { humanReadableArgName } = require_argument();
205
+ var Help2 = class {
206
+ constructor() {
207
+ this.helpWidth = void 0;
208
+ this.sortSubcommands = false;
209
+ this.sortOptions = false;
210
+ this.showGlobalOptions = false;
211
+ }
212
+ /**
213
+ * Get an array of the visible subcommands. Includes a placeholder for the implicit help command, if there is one.
214
+ *
215
+ * @param {Command} cmd
216
+ * @returns {Command[]}
217
+ */
218
+ visibleCommands(cmd) {
219
+ const visibleCommands = cmd.commands.filter((cmd2) => !cmd2._hidden);
220
+ const helpCommand = cmd._getHelpCommand();
221
+ if (helpCommand && !helpCommand._hidden) {
222
+ visibleCommands.push(helpCommand);
223
+ }
224
+ if (this.sortSubcommands) {
225
+ visibleCommands.sort((a, b) => {
226
+ return a.name().localeCompare(b.name());
227
+ });
228
+ }
229
+ return visibleCommands;
230
+ }
231
+ /**
232
+ * Compare options for sort.
233
+ *
234
+ * @param {Option} a
235
+ * @param {Option} b
236
+ * @returns number
237
+ */
238
+ compareOptions(a, b) {
239
+ const getSortKey = (option) => {
240
+ return option.short ? option.short.replace(/^-/, "") : option.long.replace(/^--/, "");
241
+ };
242
+ return getSortKey(a).localeCompare(getSortKey(b));
243
+ }
244
+ /**
245
+ * Get an array of the visible options. Includes a placeholder for the implicit help option, if there is one.
246
+ *
247
+ * @param {Command} cmd
248
+ * @returns {Option[]}
249
+ */
250
+ visibleOptions(cmd) {
251
+ const visibleOptions = cmd.options.filter((option) => !option.hidden);
252
+ const helpOption = cmd._getHelpOption();
253
+ if (helpOption && !helpOption.hidden) {
254
+ const removeShort = helpOption.short && cmd._findOption(helpOption.short);
255
+ const removeLong = helpOption.long && cmd._findOption(helpOption.long);
256
+ if (!removeShort && !removeLong) {
257
+ visibleOptions.push(helpOption);
258
+ } else if (helpOption.long && !removeLong) {
259
+ visibleOptions.push(cmd.createOption(helpOption.long, helpOption.description));
260
+ } else if (helpOption.short && !removeShort) {
261
+ visibleOptions.push(cmd.createOption(helpOption.short, helpOption.description));
262
+ }
263
+ }
264
+ if (this.sortOptions) {
265
+ visibleOptions.sort(this.compareOptions);
266
+ }
267
+ return visibleOptions;
268
+ }
269
+ /**
270
+ * Get an array of the visible global options. (Not including help.)
271
+ *
272
+ * @param {Command} cmd
273
+ * @returns {Option[]}
274
+ */
275
+ visibleGlobalOptions(cmd) {
276
+ if (!this.showGlobalOptions) return [];
277
+ const globalOptions = [];
278
+ for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) {
279
+ const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
280
+ globalOptions.push(...visibleOptions);
281
+ }
282
+ if (this.sortOptions) {
283
+ globalOptions.sort(this.compareOptions);
284
+ }
285
+ return globalOptions;
286
+ }
287
+ /**
288
+ * Get an array of the arguments if any have a description.
289
+ *
290
+ * @param {Command} cmd
291
+ * @returns {Argument[]}
292
+ */
293
+ visibleArguments(cmd) {
294
+ if (cmd._argsDescription) {
295
+ cmd.registeredArguments.forEach((argument) => {
296
+ argument.description = argument.description || cmd._argsDescription[argument.name()] || "";
297
+ });
298
+ }
299
+ if (cmd.registeredArguments.find((argument) => argument.description)) {
300
+ return cmd.registeredArguments;
301
+ }
302
+ return [];
303
+ }
304
+ /**
305
+ * Get the command term to show in the list of subcommands.
306
+ *
307
+ * @param {Command} cmd
308
+ * @returns {string}
309
+ */
310
+ subcommandTerm(cmd) {
311
+ const args = cmd.registeredArguments.map((arg) => humanReadableArgName(arg)).join(" ");
312
+ return cmd._name + (cmd._aliases[0] ? "|" + cmd._aliases[0] : "") + (cmd.options.length ? " [options]" : "") + // simplistic check for non-help option
313
+ (args ? " " + args : "");
314
+ }
315
+ /**
316
+ * Get the option term to show in the list of options.
317
+ *
318
+ * @param {Option} option
319
+ * @returns {string}
320
+ */
321
+ optionTerm(option) {
322
+ return option.flags;
323
+ }
324
+ /**
325
+ * Get the argument term to show in the list of arguments.
326
+ *
327
+ * @param {Argument} argument
328
+ * @returns {string}
329
+ */
330
+ argumentTerm(argument) {
331
+ return argument.name();
332
+ }
333
+ /**
334
+ * Get the longest command term length.
335
+ *
336
+ * @param {Command} cmd
337
+ * @param {Help} helper
338
+ * @returns {number}
339
+ */
340
+ longestSubcommandTermLength(cmd, helper) {
341
+ return helper.visibleCommands(cmd).reduce((max, command) => {
342
+ return Math.max(max, helper.subcommandTerm(command).length);
343
+ }, 0);
344
+ }
345
+ /**
346
+ * Get the longest option term length.
347
+ *
348
+ * @param {Command} cmd
349
+ * @param {Help} helper
350
+ * @returns {number}
351
+ */
352
+ longestOptionTermLength(cmd, helper) {
353
+ return helper.visibleOptions(cmd).reduce((max, option) => {
354
+ return Math.max(max, helper.optionTerm(option).length);
355
+ }, 0);
356
+ }
357
+ /**
358
+ * Get the longest global option term length.
359
+ *
360
+ * @param {Command} cmd
361
+ * @param {Help} helper
362
+ * @returns {number}
363
+ */
364
+ longestGlobalOptionTermLength(cmd, helper) {
365
+ return helper.visibleGlobalOptions(cmd).reduce((max, option) => {
366
+ return Math.max(max, helper.optionTerm(option).length);
367
+ }, 0);
368
+ }
369
+ /**
370
+ * Get the longest argument term length.
371
+ *
372
+ * @param {Command} cmd
373
+ * @param {Help} helper
374
+ * @returns {number}
375
+ */
376
+ longestArgumentTermLength(cmd, helper) {
377
+ return helper.visibleArguments(cmd).reduce((max, argument) => {
378
+ return Math.max(max, helper.argumentTerm(argument).length);
379
+ }, 0);
380
+ }
381
+ /**
382
+ * Get the command usage to be displayed at the top of the built-in help.
383
+ *
384
+ * @param {Command} cmd
385
+ * @returns {string}
386
+ */
387
+ commandUsage(cmd) {
388
+ let cmdName = cmd._name;
389
+ if (cmd._aliases[0]) {
390
+ cmdName = cmdName + "|" + cmd._aliases[0];
391
+ }
392
+ let ancestorCmdNames = "";
393
+ for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) {
394
+ ancestorCmdNames = ancestorCmd.name() + " " + ancestorCmdNames;
395
+ }
396
+ return ancestorCmdNames + cmdName + " " + cmd.usage();
397
+ }
398
+ /**
399
+ * Get the description for the command.
400
+ *
401
+ * @param {Command} cmd
402
+ * @returns {string}
403
+ */
404
+ commandDescription(cmd) {
405
+ return cmd.description();
406
+ }
407
+ /**
408
+ * Get the subcommand summary to show in the list of subcommands.
409
+ * (Fallback to description for backwards compatibility.)
410
+ *
411
+ * @param {Command} cmd
412
+ * @returns {string}
413
+ */
414
+ subcommandDescription(cmd) {
415
+ return cmd.summary() || cmd.description();
416
+ }
417
+ /**
418
+ * Get the option description to show in the list of options.
419
+ *
420
+ * @param {Option} option
421
+ * @return {string}
422
+ */
423
+ optionDescription(option) {
424
+ const extraInfo = [];
425
+ if (option.argChoices) {
426
+ extraInfo.push(
427
+ // use stringify to match the display of the default value
428
+ `choices: ${option.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`
429
+ );
430
+ }
431
+ if (option.defaultValue !== void 0) {
432
+ const showDefault = option.required || option.optional || option.isBoolean() && typeof option.defaultValue === "boolean";
433
+ if (showDefault) {
434
+ extraInfo.push(`default: ${option.defaultValueDescription || JSON.stringify(option.defaultValue)}`);
435
+ }
436
+ }
437
+ if (option.presetArg !== void 0 && option.optional) {
438
+ extraInfo.push(`preset: ${JSON.stringify(option.presetArg)}`);
439
+ }
440
+ if (option.envVar !== void 0) {
441
+ extraInfo.push(`env: ${option.envVar}`);
442
+ }
443
+ if (extraInfo.length > 0) {
444
+ return `${option.description} (${extraInfo.join(", ")})`;
445
+ }
446
+ return option.description;
447
+ }
448
+ /**
449
+ * Get the argument description to show in the list of arguments.
450
+ *
451
+ * @param {Argument} argument
452
+ * @return {string}
453
+ */
454
+ argumentDescription(argument) {
455
+ const extraInfo = [];
456
+ if (argument.argChoices) {
457
+ extraInfo.push(
458
+ // use stringify to match the display of the default value
459
+ `choices: ${argument.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`
460
+ );
461
+ }
462
+ if (argument.defaultValue !== void 0) {
463
+ extraInfo.push(`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`);
464
+ }
465
+ if (extraInfo.length > 0) {
466
+ const extraDescripton = `(${extraInfo.join(", ")})`;
467
+ if (argument.description) {
468
+ return `${argument.description} ${extraDescripton}`;
469
+ }
470
+ return extraDescripton;
471
+ }
472
+ return argument.description;
473
+ }
474
+ /**
475
+ * Generate the built-in help text.
476
+ *
477
+ * @param {Command} cmd
478
+ * @param {Help} helper
479
+ * @returns {string}
480
+ */
481
+ formatHelp(cmd, helper) {
482
+ const termWidth = helper.padWidth(cmd, helper);
483
+ const helpWidth = helper.helpWidth || 80;
484
+ const itemIndentWidth = 2;
485
+ const itemSeparatorWidth = 2;
486
+ function formatItem(term, description) {
487
+ if (description) {
488
+ const fullText = `${term.padEnd(termWidth + itemSeparatorWidth)}${description}`;
489
+ return helper.wrap(fullText, helpWidth - itemIndentWidth, termWidth + itemSeparatorWidth);
490
+ }
491
+ return term;
492
+ }
493
+ function formatList(textArray) {
494
+ return textArray.join("\n").replace(/^/gm, " ".repeat(itemIndentWidth));
495
+ }
496
+ let output = [`Usage: ${helper.commandUsage(cmd)}`, ""];
497
+ const commandDescription = helper.commandDescription(cmd);
498
+ if (commandDescription.length > 0) {
499
+ output = output.concat([helper.wrap(commandDescription, helpWidth, 0), ""]);
500
+ }
501
+ const argumentList = helper.visibleArguments(cmd).map((argument) => {
502
+ return formatItem(helper.argumentTerm(argument), helper.argumentDescription(argument));
503
+ });
504
+ if (argumentList.length > 0) {
505
+ output = output.concat(["Arguments:", formatList(argumentList), ""]);
506
+ }
507
+ const optionList = helper.visibleOptions(cmd).map((option) => {
508
+ return formatItem(helper.optionTerm(option), helper.optionDescription(option));
509
+ });
510
+ if (optionList.length > 0) {
511
+ output = output.concat(["Options:", formatList(optionList), ""]);
512
+ }
513
+ if (this.showGlobalOptions) {
514
+ const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
515
+ return formatItem(helper.optionTerm(option), helper.optionDescription(option));
516
+ });
517
+ if (globalOptionList.length > 0) {
518
+ output = output.concat(["Global Options:", formatList(globalOptionList), ""]);
519
+ }
520
+ }
521
+ const commandList = helper.visibleCommands(cmd).map((cmd2) => {
522
+ return formatItem(helper.subcommandTerm(cmd2), helper.subcommandDescription(cmd2));
523
+ });
524
+ if (commandList.length > 0) {
525
+ output = output.concat(["Commands:", formatList(commandList), ""]);
526
+ }
527
+ return output.join("\n");
528
+ }
529
+ /**
530
+ * Calculate the pad width from the maximum term length.
531
+ *
532
+ * @param {Command} cmd
533
+ * @param {Help} helper
534
+ * @returns {number}
535
+ */
536
+ padWidth(cmd, helper) {
537
+ return Math.max(
538
+ helper.longestOptionTermLength(cmd, helper),
539
+ helper.longestGlobalOptionTermLength(cmd, helper),
540
+ helper.longestSubcommandTermLength(cmd, helper),
541
+ helper.longestArgumentTermLength(cmd, helper)
542
+ );
543
+ }
544
+ /**
545
+ * Wrap the given string to width characters per line, with lines after the first indented.
546
+ * Do not wrap if insufficient room for wrapping (minColumnWidth), or string is manually formatted.
547
+ *
548
+ * @param {string} str
549
+ * @param {number} width
550
+ * @param {number} indent
551
+ * @param {number} [minColumnWidth=40]
552
+ * @return {string}
553
+ *
554
+ */
555
+ wrap(str, width, indent, minColumnWidth = 40) {
556
+ const indents = " \\f\\t\\v\xA0\u1680\u2000-\u200A\u202F\u205F\u3000\uFEFF";
557
+ const manualIndent = new RegExp(`[\\n][${indents}]+`);
558
+ if (str.match(manualIndent)) return str;
559
+ const columnWidth = width - indent;
560
+ if (columnWidth < minColumnWidth) return str;
561
+ const leadingStr = str.slice(0, indent);
562
+ const columnText = str.slice(indent).replace("\r\n", "\n");
563
+ const indentString = " ".repeat(indent);
564
+ const zeroWidthSpace = "\u200B";
565
+ const breaks = `\\s${zeroWidthSpace}`;
566
+ const regex = new RegExp(`
567
+ |.{1,${columnWidth - 1}}([${breaks}]|$)|[^${breaks}]+?([${breaks}]|$)`, "g");
568
+ const lines = columnText.match(regex) || [];
569
+ return leadingStr + lines.map((line, i) => {
570
+ if (line === "\n") return "";
571
+ return (i > 0 ? indentString : "") + line.trimEnd();
572
+ }).join("\n");
573
+ }
574
+ };
575
+ exports.Help = Help2;
576
+ }
577
+ });
578
+
579
+ // node_modules/commander/lib/option.js
580
+ var require_option = __commonJS({
581
+ "node_modules/commander/lib/option.js"(exports) {
582
+ var { InvalidArgumentError: InvalidArgumentError2 } = require_error();
583
+ var Option2 = class {
584
+ /**
585
+ * Initialize a new `Option` with the given `flags` and `description`.
586
+ *
587
+ * @param {string} flags
588
+ * @param {string} [description]
589
+ */
590
+ constructor(flags, description) {
591
+ this.flags = flags;
592
+ this.description = description || "";
593
+ this.required = flags.includes("<");
594
+ this.optional = flags.includes("[");
595
+ this.variadic = /\w\.\.\.[>\]]$/.test(flags);
596
+ this.mandatory = false;
597
+ const optionFlags = splitOptionFlags(flags);
598
+ this.short = optionFlags.shortFlag;
599
+ this.long = optionFlags.longFlag;
600
+ this.negate = false;
601
+ if (this.long) {
602
+ this.negate = this.long.startsWith("--no-");
603
+ }
604
+ this.defaultValue = void 0;
605
+ this.defaultValueDescription = void 0;
606
+ this.presetArg = void 0;
607
+ this.envVar = void 0;
608
+ this.parseArg = void 0;
609
+ this.hidden = false;
610
+ this.argChoices = void 0;
611
+ this.conflictsWith = [];
612
+ this.implied = void 0;
613
+ }
614
+ /**
615
+ * Set the default value, and optionally supply the description to be displayed in the help.
616
+ *
617
+ * @param {*} value
618
+ * @param {string} [description]
619
+ * @return {Option}
620
+ */
621
+ default(value, description) {
622
+ this.defaultValue = value;
623
+ this.defaultValueDescription = description;
624
+ return this;
625
+ }
626
+ /**
627
+ * Preset to use when option used without option-argument, especially optional but also boolean and negated.
628
+ * The custom processing (parseArg) is called.
629
+ *
630
+ * @example
631
+ * new Option('--color').default('GREYSCALE').preset('RGB');
632
+ * new Option('--donate [amount]').preset('20').argParser(parseFloat);
633
+ *
634
+ * @param {*} arg
635
+ * @return {Option}
636
+ */
637
+ preset(arg) {
638
+ this.presetArg = arg;
639
+ return this;
640
+ }
641
+ /**
642
+ * Add option name(s) that conflict with this option.
643
+ * An error will be displayed if conflicting options are found during parsing.
644
+ *
645
+ * @example
646
+ * new Option('--rgb').conflicts('cmyk');
647
+ * new Option('--js').conflicts(['ts', 'jsx']);
648
+ *
649
+ * @param {(string | string[])} names
650
+ * @return {Option}
651
+ */
652
+ conflicts(names) {
653
+ this.conflictsWith = this.conflictsWith.concat(names);
654
+ return this;
655
+ }
656
+ /**
657
+ * Specify implied option values for when this option is set and the implied options are not.
658
+ *
659
+ * The custom processing (parseArg) is not called on the implied values.
660
+ *
661
+ * @example
662
+ * program
663
+ * .addOption(new Option('--log', 'write logging information to file'))
664
+ * .addOption(new Option('--trace', 'log extra details').implies({ log: 'trace.txt' }));
665
+ *
666
+ * @param {Object} impliedOptionValues
667
+ * @return {Option}
668
+ */
669
+ implies(impliedOptionValues) {
670
+ let newImplied = impliedOptionValues;
671
+ if (typeof impliedOptionValues === "string") {
672
+ newImplied = { [impliedOptionValues]: true };
673
+ }
674
+ this.implied = Object.assign(this.implied || {}, newImplied);
675
+ return this;
676
+ }
677
+ /**
678
+ * Set environment variable to check for option value.
679
+ *
680
+ * An environment variable is only used if when processed the current option value is
681
+ * undefined, or the source of the current value is 'default' or 'config' or 'env'.
682
+ *
683
+ * @param {string} name
684
+ * @return {Option}
685
+ */
686
+ env(name) {
687
+ this.envVar = name;
688
+ return this;
689
+ }
690
+ /**
691
+ * Set the custom handler for processing CLI option arguments into option values.
692
+ *
693
+ * @param {Function} [fn]
694
+ * @return {Option}
695
+ */
696
+ argParser(fn) {
697
+ this.parseArg = fn;
698
+ return this;
699
+ }
700
+ /**
701
+ * Whether the option is mandatory and must have a value after parsing.
702
+ *
703
+ * @param {boolean} [mandatory=true]
704
+ * @return {Option}
705
+ */
706
+ makeOptionMandatory(mandatory = true) {
707
+ this.mandatory = !!mandatory;
708
+ return this;
709
+ }
710
+ /**
711
+ * Hide option in help.
712
+ *
713
+ * @param {boolean} [hide=true]
714
+ * @return {Option}
715
+ */
716
+ hideHelp(hide = true) {
717
+ this.hidden = !!hide;
718
+ return this;
719
+ }
720
+ /**
721
+ * @package internal use only
722
+ */
723
+ _concatValue(value, previous) {
724
+ if (previous === this.defaultValue || !Array.isArray(previous)) {
725
+ return [value];
726
+ }
727
+ return previous.concat(value);
728
+ }
729
+ /**
730
+ * Only allow option value to be one of choices.
731
+ *
732
+ * @param {string[]} values
733
+ * @return {Option}
734
+ */
735
+ choices(values) {
736
+ this.argChoices = values.slice();
737
+ this.parseArg = (arg, previous) => {
738
+ if (!this.argChoices.includes(arg)) {
739
+ throw new InvalidArgumentError2(`Allowed choices are ${this.argChoices.join(", ")}.`);
740
+ }
741
+ if (this.variadic) {
742
+ return this._concatValue(arg, previous);
743
+ }
744
+ return arg;
745
+ };
746
+ return this;
747
+ }
748
+ /**
749
+ * Return option name.
750
+ *
751
+ * @return {string}
752
+ */
753
+ name() {
754
+ if (this.long) {
755
+ return this.long.replace(/^--/, "");
756
+ }
757
+ return this.short.replace(/^-/, "");
758
+ }
759
+ /**
760
+ * Return option name, in a camelcase format that can be used
761
+ * as a object attribute key.
762
+ *
763
+ * @return {string}
764
+ */
765
+ attributeName() {
766
+ return camelcase(this.name().replace(/^no-/, ""));
767
+ }
768
+ /**
769
+ * Check if `arg` matches the short or long flag.
770
+ *
771
+ * @param {string} arg
772
+ * @return {boolean}
773
+ * @package internal use only
774
+ */
775
+ is(arg) {
776
+ return this.short === arg || this.long === arg;
777
+ }
778
+ /**
779
+ * Return whether a boolean option.
780
+ *
781
+ * Options are one of boolean, negated, required argument, or optional argument.
782
+ *
783
+ * @return {boolean}
784
+ * @package internal use only
785
+ */
786
+ isBoolean() {
787
+ return !this.required && !this.optional && !this.negate;
788
+ }
789
+ };
790
+ var DualOptions = class {
791
+ /**
792
+ * @param {Option[]} options
793
+ */
794
+ constructor(options) {
795
+ this.positiveOptions = /* @__PURE__ */ new Map();
796
+ this.negativeOptions = /* @__PURE__ */ new Map();
797
+ this.dualOptions = /* @__PURE__ */ new Set();
798
+ options.forEach((option) => {
799
+ if (option.negate) {
800
+ this.negativeOptions.set(option.attributeName(), option);
801
+ } else {
802
+ this.positiveOptions.set(option.attributeName(), option);
803
+ }
804
+ });
805
+ this.negativeOptions.forEach((value, key) => {
806
+ if (this.positiveOptions.has(key)) {
807
+ this.dualOptions.add(key);
808
+ }
809
+ });
810
+ }
811
+ /**
812
+ * Did the value come from the option, and not from possible matching dual option?
813
+ *
814
+ * @param {*} value
815
+ * @param {Option} option
816
+ * @returns {boolean}
817
+ */
818
+ valueFromOption(value, option) {
819
+ const optionKey = option.attributeName();
820
+ if (!this.dualOptions.has(optionKey)) return true;
821
+ const preset = this.negativeOptions.get(optionKey).presetArg;
822
+ const negativeValue = preset !== void 0 ? preset : false;
823
+ return option.negate === (negativeValue === value);
824
+ }
825
+ };
826
+ function camelcase(str) {
827
+ return str.split("-").reduce((str2, word) => {
828
+ return str2 + word[0].toUpperCase() + word.slice(1);
829
+ });
830
+ }
831
+ function splitOptionFlags(flags) {
832
+ let shortFlag;
833
+ let longFlag;
834
+ const flagParts = flags.split(/[ |,]+/);
835
+ if (flagParts.length > 1 && !/^[[<]/.test(flagParts[1])) shortFlag = flagParts.shift();
836
+ longFlag = flagParts.shift();
837
+ if (!shortFlag && /^-[^-]$/.test(longFlag)) {
838
+ shortFlag = longFlag;
839
+ longFlag = void 0;
840
+ }
841
+ return { shortFlag, longFlag };
842
+ }
843
+ exports.Option = Option2;
844
+ exports.DualOptions = DualOptions;
845
+ }
846
+ });
847
+
848
+ // node_modules/commander/lib/suggestSimilar.js
849
+ var require_suggestSimilar = __commonJS({
850
+ "node_modules/commander/lib/suggestSimilar.js"(exports) {
851
+ var maxDistance = 3;
852
+ function editDistance(a, b) {
853
+ if (Math.abs(a.length - b.length) > maxDistance) return Math.max(a.length, b.length);
854
+ const d = [];
855
+ for (let i = 0; i <= a.length; i++) {
856
+ d[i] = [i];
857
+ }
858
+ for (let j = 0; j <= b.length; j++) {
859
+ d[0][j] = j;
860
+ }
861
+ for (let j = 1; j <= b.length; j++) {
862
+ for (let i = 1; i <= a.length; i++) {
863
+ let cost = 1;
864
+ if (a[i - 1] === b[j - 1]) {
865
+ cost = 0;
866
+ } else {
867
+ cost = 1;
868
+ }
869
+ d[i][j] = Math.min(
870
+ d[i - 1][j] + 1,
871
+ // deletion
872
+ d[i][j - 1] + 1,
873
+ // insertion
874
+ d[i - 1][j - 1] + cost
875
+ // substitution
876
+ );
877
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
878
+ d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
879
+ }
880
+ }
881
+ }
882
+ return d[a.length][b.length];
883
+ }
884
+ function suggestSimilar(word, candidates) {
885
+ if (!candidates || candidates.length === 0) return "";
886
+ candidates = Array.from(new Set(candidates));
887
+ const searchingOptions = word.startsWith("--");
888
+ if (searchingOptions) {
889
+ word = word.slice(2);
890
+ candidates = candidates.map((candidate) => candidate.slice(2));
891
+ }
892
+ let similar = [];
893
+ let bestDistance = maxDistance;
894
+ const minSimilarity = 0.4;
895
+ candidates.forEach((candidate) => {
896
+ if (candidate.length <= 1) return;
897
+ const distance = editDistance(word, candidate);
898
+ const length = Math.max(word.length, candidate.length);
899
+ const similarity = (length - distance) / length;
900
+ if (similarity > minSimilarity) {
901
+ if (distance < bestDistance) {
902
+ bestDistance = distance;
903
+ similar = [candidate];
904
+ } else if (distance === bestDistance) {
905
+ similar.push(candidate);
906
+ }
907
+ }
908
+ });
909
+ similar.sort((a, b) => a.localeCompare(b));
910
+ if (searchingOptions) {
911
+ similar = similar.map((candidate) => `--${candidate}`);
912
+ }
913
+ if (similar.length > 1) {
914
+ return `
915
+ (Did you mean one of ${similar.join(", ")}?)`;
916
+ }
917
+ if (similar.length === 1) {
918
+ return `
919
+ (Did you mean ${similar[0]}?)`;
920
+ }
921
+ return "";
922
+ }
923
+ exports.suggestSimilar = suggestSimilar;
924
+ }
925
+ });
926
+
927
+ // node_modules/commander/lib/command.js
928
+ var require_command = __commonJS({
929
+ "node_modules/commander/lib/command.js"(exports) {
930
+ var EventEmitter = __require("events").EventEmitter;
931
+ var childProcess = __require("child_process");
932
+ var path6 = __require("path");
933
+ var fs4 = __require("fs");
934
+ var process2 = __require("process");
935
+ var { Argument: Argument2, humanReadableArgName } = require_argument();
936
+ var { CommanderError: CommanderError2 } = require_error();
937
+ var { Help: Help2 } = require_help();
938
+ var { Option: Option2, DualOptions } = require_option();
939
+ var { suggestSimilar } = require_suggestSimilar();
940
+ var Command2 = class _Command extends EventEmitter {
941
+ /**
942
+ * Initialize a new `Command`.
943
+ *
944
+ * @param {string} [name]
945
+ */
946
+ constructor(name) {
947
+ super();
948
+ this.commands = [];
949
+ this.options = [];
950
+ this.parent = null;
951
+ this._allowUnknownOption = false;
952
+ this._allowExcessArguments = true;
953
+ this.registeredArguments = [];
954
+ this._args = this.registeredArguments;
955
+ this.args = [];
956
+ this.rawArgs = [];
957
+ this.processedArgs = [];
958
+ this._scriptPath = null;
959
+ this._name = name || "";
960
+ this._optionValues = {};
961
+ this._optionValueSources = {};
962
+ this._storeOptionsAsProperties = false;
963
+ this._actionHandler = null;
964
+ this._executableHandler = false;
965
+ this._executableFile = null;
966
+ this._executableDir = null;
967
+ this._defaultCommandName = null;
968
+ this._exitCallback = null;
969
+ this._aliases = [];
970
+ this._combineFlagAndOptionalValue = true;
971
+ this._description = "";
972
+ this._summary = "";
973
+ this._argsDescription = void 0;
974
+ this._enablePositionalOptions = false;
975
+ this._passThroughOptions = false;
976
+ this._lifeCycleHooks = {};
977
+ this._showHelpAfterError = false;
978
+ this._showSuggestionAfterError = true;
979
+ this._outputConfiguration = {
980
+ writeOut: (str) => process2.stdout.write(str),
981
+ writeErr: (str) => process2.stderr.write(str),
982
+ getOutHelpWidth: () => process2.stdout.isTTY ? process2.stdout.columns : void 0,
983
+ getErrHelpWidth: () => process2.stderr.isTTY ? process2.stderr.columns : void 0,
984
+ outputError: (str, write) => write(str)
985
+ };
986
+ this._hidden = false;
987
+ this._helpOption = void 0;
988
+ this._addImplicitHelpCommand = void 0;
989
+ this._helpCommand = void 0;
990
+ this._helpConfiguration = {};
991
+ }
992
+ /**
993
+ * Copy settings that are useful to have in common across root command and subcommands.
994
+ *
995
+ * (Used internally when adding a command using `.command()` so subcommands inherit parent settings.)
996
+ *
997
+ * @param {Command} sourceCommand
998
+ * @return {Command} `this` command for chaining
999
+ */
1000
+ copyInheritedSettings(sourceCommand) {
1001
+ this._outputConfiguration = sourceCommand._outputConfiguration;
1002
+ this._helpOption = sourceCommand._helpOption;
1003
+ this._helpCommand = sourceCommand._helpCommand;
1004
+ this._helpConfiguration = sourceCommand._helpConfiguration;
1005
+ this._exitCallback = sourceCommand._exitCallback;
1006
+ this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
1007
+ this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
1008
+ this._allowExcessArguments = sourceCommand._allowExcessArguments;
1009
+ this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
1010
+ this._showHelpAfterError = sourceCommand._showHelpAfterError;
1011
+ this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;
1012
+ return this;
1013
+ }
1014
+ /**
1015
+ * @returns {Command[]}
1016
+ * @private
1017
+ */
1018
+ _getCommandAndAncestors() {
1019
+ const result = [];
1020
+ for (let command = this; command; command = command.parent) {
1021
+ result.push(command);
1022
+ }
1023
+ return result;
1024
+ }
1025
+ /**
1026
+ * Define a command.
1027
+ *
1028
+ * There are two styles of command: pay attention to where to put the description.
1029
+ *
1030
+ * @example
1031
+ * // Command implemented using action handler (description is supplied separately to `.command`)
1032
+ * program
1033
+ * .command('clone <source> [destination]')
1034
+ * .description('clone a repository into a newly created directory')
1035
+ * .action((source, destination) => {
1036
+ * console.log('clone command called');
1037
+ * });
1038
+ *
1039
+ * // Command implemented using separate executable file (description is second parameter to `.command`)
1040
+ * program
1041
+ * .command('start <service>', 'start named service')
1042
+ * .command('stop [service]', 'stop named service, or all if no name supplied');
1043
+ *
1044
+ * @param {string} nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`
1045
+ * @param {(Object|string)} [actionOptsOrExecDesc] - configuration options (for action), or description (for executable)
1046
+ * @param {Object} [execOpts] - configuration options (for executable)
1047
+ * @return {Command} returns new command for action handler, or `this` for executable command
1048
+ */
1049
+ command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
1050
+ let desc = actionOptsOrExecDesc;
1051
+ let opts = execOpts;
1052
+ if (typeof desc === "object" && desc !== null) {
1053
+ opts = desc;
1054
+ desc = null;
1055
+ }
1056
+ opts = opts || {};
1057
+ const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
1058
+ const cmd = this.createCommand(name);
1059
+ if (desc) {
1060
+ cmd.description(desc);
1061
+ cmd._executableHandler = true;
1062
+ }
1063
+ if (opts.isDefault) this._defaultCommandName = cmd._name;
1064
+ cmd._hidden = !!(opts.noHelp || opts.hidden);
1065
+ cmd._executableFile = opts.executableFile || null;
1066
+ if (args) cmd.arguments(args);
1067
+ this._registerCommand(cmd);
1068
+ cmd.parent = this;
1069
+ cmd.copyInheritedSettings(this);
1070
+ if (desc) return this;
1071
+ return cmd;
1072
+ }
1073
+ /**
1074
+ * Factory routine to create a new unattached command.
1075
+ *
1076
+ * See .command() for creating an attached subcommand, which uses this routine to
1077
+ * create the command. You can override createCommand to customise subcommands.
1078
+ *
1079
+ * @param {string} [name]
1080
+ * @return {Command} new command
1081
+ */
1082
+ createCommand(name) {
1083
+ return new _Command(name);
1084
+ }
1085
+ /**
1086
+ * You can customise the help with a subclass of Help by overriding createHelp,
1087
+ * or by overriding Help properties using configureHelp().
1088
+ *
1089
+ * @return {Help}
1090
+ */
1091
+ createHelp() {
1092
+ return Object.assign(new Help2(), this.configureHelp());
1093
+ }
1094
+ /**
1095
+ * You can customise the help by overriding Help properties using configureHelp(),
1096
+ * or with a subclass of Help by overriding createHelp().
1097
+ *
1098
+ * @param {Object} [configuration] - configuration options
1099
+ * @return {(Command|Object)} `this` command for chaining, or stored configuration
1100
+ */
1101
+ configureHelp(configuration) {
1102
+ if (configuration === void 0) return this._helpConfiguration;
1103
+ this._helpConfiguration = configuration;
1104
+ return this;
1105
+ }
1106
+ /**
1107
+ * The default output goes to stdout and stderr. You can customise this for special
1108
+ * applications. You can also customise the display of errors by overriding outputError.
1109
+ *
1110
+ * The configuration properties are all functions:
1111
+ *
1112
+ * // functions to change where being written, stdout and stderr
1113
+ * writeOut(str)
1114
+ * writeErr(str)
1115
+ * // matching functions to specify width for wrapping help
1116
+ * getOutHelpWidth()
1117
+ * getErrHelpWidth()
1118
+ * // functions based on what is being written out
1119
+ * outputError(str, write) // used for displaying errors, and not used for displaying help
1120
+ *
1121
+ * @param {Object} [configuration] - configuration options
1122
+ * @return {(Command|Object)} `this` command for chaining, or stored configuration
1123
+ */
1124
+ configureOutput(configuration) {
1125
+ if (configuration === void 0) return this._outputConfiguration;
1126
+ Object.assign(this._outputConfiguration, configuration);
1127
+ return this;
1128
+ }
1129
+ /**
1130
+ * Display the help or a custom message after an error occurs.
1131
+ *
1132
+ * @param {(boolean|string)} [displayHelp]
1133
+ * @return {Command} `this` command for chaining
1134
+ */
1135
+ showHelpAfterError(displayHelp = true) {
1136
+ if (typeof displayHelp !== "string") displayHelp = !!displayHelp;
1137
+ this._showHelpAfterError = displayHelp;
1138
+ return this;
1139
+ }
1140
+ /**
1141
+ * Display suggestion of similar commands for unknown commands, or options for unknown options.
1142
+ *
1143
+ * @param {boolean} [displaySuggestion]
1144
+ * @return {Command} `this` command for chaining
1145
+ */
1146
+ showSuggestionAfterError(displaySuggestion = true) {
1147
+ this._showSuggestionAfterError = !!displaySuggestion;
1148
+ return this;
1149
+ }
1150
+ /**
1151
+ * Add a prepared subcommand.
1152
+ *
1153
+ * See .command() for creating an attached subcommand which inherits settings from its parent.
1154
+ *
1155
+ * @param {Command} cmd - new subcommand
1156
+ * @param {Object} [opts] - configuration options
1157
+ * @return {Command} `this` command for chaining
1158
+ */
1159
+ addCommand(cmd, opts) {
1160
+ if (!cmd._name) {
1161
+ throw new Error(`Command passed to .addCommand() must have a name
1162
+ - specify the name in Command constructor or using .name()`);
1163
+ }
1164
+ opts = opts || {};
1165
+ if (opts.isDefault) this._defaultCommandName = cmd._name;
1166
+ if (opts.noHelp || opts.hidden) cmd._hidden = true;
1167
+ this._registerCommand(cmd);
1168
+ cmd.parent = this;
1169
+ cmd._checkForBrokenPassThrough();
1170
+ return this;
1171
+ }
1172
+ /**
1173
+ * Factory routine to create a new unattached argument.
1174
+ *
1175
+ * See .argument() for creating an attached argument, which uses this routine to
1176
+ * create the argument. You can override createArgument to return a custom argument.
1177
+ *
1178
+ * @param {string} name
1179
+ * @param {string} [description]
1180
+ * @return {Argument} new argument
1181
+ */
1182
+ createArgument(name, description) {
1183
+ return new Argument2(name, description);
1184
+ }
1185
+ /**
1186
+ * Define argument syntax for command.
1187
+ *
1188
+ * The default is that the argument is required, and you can explicitly
1189
+ * indicate this with <> around the name. Put [] around the name for an optional argument.
1190
+ *
1191
+ * @example
1192
+ * program.argument('<input-file>');
1193
+ * program.argument('[output-file]');
1194
+ *
1195
+ * @param {string} name
1196
+ * @param {string} [description]
1197
+ * @param {(Function|*)} [fn] - custom argument processing function
1198
+ * @param {*} [defaultValue]
1199
+ * @return {Command} `this` command for chaining
1200
+ */
1201
+ argument(name, description, fn, defaultValue) {
1202
+ const argument = this.createArgument(name, description);
1203
+ if (typeof fn === "function") {
1204
+ argument.default(defaultValue).argParser(fn);
1205
+ } else {
1206
+ argument.default(fn);
1207
+ }
1208
+ this.addArgument(argument);
1209
+ return this;
1210
+ }
1211
+ /**
1212
+ * Define argument syntax for command, adding multiple at once (without descriptions).
1213
+ *
1214
+ * See also .argument().
1215
+ *
1216
+ * @example
1217
+ * program.arguments('<cmd> [env]');
1218
+ *
1219
+ * @param {string} names
1220
+ * @return {Command} `this` command for chaining
1221
+ */
1222
+ arguments(names) {
1223
+ names.trim().split(/ +/).forEach((detail) => {
1224
+ this.argument(detail);
1225
+ });
1226
+ return this;
1227
+ }
1228
+ /**
1229
+ * Define argument syntax for command, adding a prepared argument.
1230
+ *
1231
+ * @param {Argument} argument
1232
+ * @return {Command} `this` command for chaining
1233
+ */
1234
+ addArgument(argument) {
1235
+ const previousArgument = this.registeredArguments.slice(-1)[0];
1236
+ if (previousArgument && previousArgument.variadic) {
1237
+ throw new Error(`only the last argument can be variadic '${previousArgument.name()}'`);
1238
+ }
1239
+ if (argument.required && argument.defaultValue !== void 0 && argument.parseArg === void 0) {
1240
+ throw new Error(`a default value for a required argument is never used: '${argument.name()}'`);
1241
+ }
1242
+ this.registeredArguments.push(argument);
1243
+ return this;
1244
+ }
1245
+ /**
1246
+ * Customise or override default help command. By default a help command is automatically added if your command has subcommands.
1247
+ *
1248
+ * program.helpCommand('help [cmd]');
1249
+ * program.helpCommand('help [cmd]', 'show help');
1250
+ * program.helpCommand(false); // suppress default help command
1251
+ * program.helpCommand(true); // add help command even if no subcommands
1252
+ *
1253
+ * @param {string|boolean} enableOrNameAndArgs - enable with custom name and/or arguments, or boolean to override whether added
1254
+ * @param {string} [description] - custom description
1255
+ * @return {Command} `this` command for chaining
1256
+ */
1257
+ helpCommand(enableOrNameAndArgs, description) {
1258
+ if (typeof enableOrNameAndArgs === "boolean") {
1259
+ this._addImplicitHelpCommand = enableOrNameAndArgs;
1260
+ return this;
1261
+ }
1262
+ enableOrNameAndArgs = enableOrNameAndArgs ?? "help [command]";
1263
+ const [, helpName, helpArgs] = enableOrNameAndArgs.match(/([^ ]+) *(.*)/);
1264
+ const helpDescription = description ?? "display help for command";
1265
+ const helpCommand = this.createCommand(helpName);
1266
+ helpCommand.helpOption(false);
1267
+ if (helpArgs) helpCommand.arguments(helpArgs);
1268
+ if (helpDescription) helpCommand.description(helpDescription);
1269
+ this._addImplicitHelpCommand = true;
1270
+ this._helpCommand = helpCommand;
1271
+ return this;
1272
+ }
1273
+ /**
1274
+ * Add prepared custom help command.
1275
+ *
1276
+ * @param {(Command|string|boolean)} helpCommand - custom help command, or deprecated enableOrNameAndArgs as for `.helpCommand()`
1277
+ * @param {string} [deprecatedDescription] - deprecated custom description used with custom name only
1278
+ * @return {Command} `this` command for chaining
1279
+ */
1280
+ addHelpCommand(helpCommand, deprecatedDescription) {
1281
+ if (typeof helpCommand !== "object") {
1282
+ this.helpCommand(helpCommand, deprecatedDescription);
1283
+ return this;
1284
+ }
1285
+ this._addImplicitHelpCommand = true;
1286
+ this._helpCommand = helpCommand;
1287
+ return this;
1288
+ }
1289
+ /**
1290
+ * Lazy create help command.
1291
+ *
1292
+ * @return {(Command|null)}
1293
+ * @package
1294
+ */
1295
+ _getHelpCommand() {
1296
+ const hasImplicitHelpCommand = this._addImplicitHelpCommand ?? (this.commands.length && !this._actionHandler && !this._findCommand("help"));
1297
+ if (hasImplicitHelpCommand) {
1298
+ if (this._helpCommand === void 0) {
1299
+ this.helpCommand(void 0, void 0);
1300
+ }
1301
+ return this._helpCommand;
1302
+ }
1303
+ return null;
1304
+ }
1305
+ /**
1306
+ * Add hook for life cycle event.
1307
+ *
1308
+ * @param {string} event
1309
+ * @param {Function} listener
1310
+ * @return {Command} `this` command for chaining
1311
+ */
1312
+ hook(event, listener) {
1313
+ const allowedValues = ["preSubcommand", "preAction", "postAction"];
1314
+ if (!allowedValues.includes(event)) {
1315
+ throw new Error(`Unexpected value for event passed to hook : '${event}'.
1316
+ Expecting one of '${allowedValues.join("', '")}'`);
1317
+ }
1318
+ if (this._lifeCycleHooks[event]) {
1319
+ this._lifeCycleHooks[event].push(listener);
1320
+ } else {
1321
+ this._lifeCycleHooks[event] = [listener];
1322
+ }
1323
+ return this;
1324
+ }
1325
+ /**
1326
+ * Register callback to use as replacement for calling process.exit.
1327
+ *
1328
+ * @param {Function} [fn] optional callback which will be passed a CommanderError, defaults to throwing
1329
+ * @return {Command} `this` command for chaining
1330
+ */
1331
+ exitOverride(fn) {
1332
+ if (fn) {
1333
+ this._exitCallback = fn;
1334
+ } else {
1335
+ this._exitCallback = (err) => {
1336
+ if (err.code !== "commander.executeSubCommandAsync") {
1337
+ throw err;
1338
+ }
1339
+ };
1340
+ }
1341
+ return this;
1342
+ }
1343
+ /**
1344
+ * Call process.exit, and _exitCallback if defined.
1345
+ *
1346
+ * @param {number} exitCode exit code for using with process.exit
1347
+ * @param {string} code an id string representing the error
1348
+ * @param {string} message human-readable description of the error
1349
+ * @return never
1350
+ * @private
1351
+ */
1352
+ _exit(exitCode, code, message) {
1353
+ if (this._exitCallback) {
1354
+ this._exitCallback(new CommanderError2(exitCode, code, message));
1355
+ }
1356
+ process2.exit(exitCode);
1357
+ }
1358
+ /**
1359
+ * Register callback `fn` for the command.
1360
+ *
1361
+ * @example
1362
+ * program
1363
+ * .command('serve')
1364
+ * .description('start service')
1365
+ * .action(function() {
1366
+ * // do work here
1367
+ * });
1368
+ *
1369
+ * @param {Function} fn
1370
+ * @return {Command} `this` command for chaining
1371
+ */
1372
+ action(fn) {
1373
+ const listener = (args) => {
1374
+ const expectedArgsCount = this.registeredArguments.length;
1375
+ const actionArgs = args.slice(0, expectedArgsCount);
1376
+ if (this._storeOptionsAsProperties) {
1377
+ actionArgs[expectedArgsCount] = this;
1378
+ } else {
1379
+ actionArgs[expectedArgsCount] = this.opts();
1380
+ }
1381
+ actionArgs.push(this);
1382
+ return fn.apply(this, actionArgs);
1383
+ };
1384
+ this._actionHandler = listener;
1385
+ return this;
1386
+ }
1387
+ /**
1388
+ * Factory routine to create a new unattached option.
1389
+ *
1390
+ * See .option() for creating an attached option, which uses this routine to
1391
+ * create the option. You can override createOption to return a custom option.
1392
+ *
1393
+ * @param {string} flags
1394
+ * @param {string} [description]
1395
+ * @return {Option} new option
1396
+ */
1397
+ createOption(flags, description) {
1398
+ return new Option2(flags, description);
1399
+ }
1400
+ /**
1401
+ * Wrap parseArgs to catch 'commander.invalidArgument'.
1402
+ *
1403
+ * @param {(Option | Argument)} target
1404
+ * @param {string} value
1405
+ * @param {*} previous
1406
+ * @param {string} invalidArgumentMessage
1407
+ * @private
1408
+ */
1409
+ _callParseArg(target, value, previous, invalidArgumentMessage) {
1410
+ try {
1411
+ return target.parseArg(value, previous);
1412
+ } catch (err) {
1413
+ if (err.code === "commander.invalidArgument") {
1414
+ const message = `${invalidArgumentMessage} ${err.message}`;
1415
+ this.error(message, { exitCode: err.exitCode, code: err.code });
1416
+ }
1417
+ throw err;
1418
+ }
1419
+ }
1420
+ /**
1421
+ * Check for option flag conflicts.
1422
+ * Register option if no conflicts found, or throw on conflict.
1423
+ *
1424
+ * @param {Option} option
1425
+ * @api private
1426
+ */
1427
+ _registerOption(option) {
1428
+ const matchingOption = option.short && this._findOption(option.short) || option.long && this._findOption(option.long);
1429
+ if (matchingOption) {
1430
+ const matchingFlag = option.long && this._findOption(option.long) ? option.long : option.short;
1431
+ throw new Error(`Cannot add option '${option.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'
1432
+ - already used by option '${matchingOption.flags}'`);
1433
+ }
1434
+ this.options.push(option);
1435
+ }
1436
+ /**
1437
+ * Check for command name and alias conflicts with existing commands.
1438
+ * Register command if no conflicts found, or throw on conflict.
1439
+ *
1440
+ * @param {Command} command
1441
+ * @api private
1442
+ */
1443
+ _registerCommand(command) {
1444
+ const knownBy = (cmd) => {
1445
+ return [cmd.name()].concat(cmd.aliases());
1446
+ };
1447
+ const alreadyUsed = knownBy(command).find((name) => this._findCommand(name));
1448
+ if (alreadyUsed) {
1449
+ const existingCmd = knownBy(this._findCommand(alreadyUsed)).join("|");
1450
+ const newCmd = knownBy(command).join("|");
1451
+ throw new Error(`cannot add command '${newCmd}' as already have command '${existingCmd}'`);
1452
+ }
1453
+ this.commands.push(command);
1454
+ }
1455
+ /**
1456
+ * Add an option.
1457
+ *
1458
+ * @param {Option} option
1459
+ * @return {Command} `this` command for chaining
1460
+ */
1461
+ addOption(option) {
1462
+ this._registerOption(option);
1463
+ const oname = option.name();
1464
+ const name = option.attributeName();
1465
+ if (option.negate) {
1466
+ const positiveLongFlag = option.long.replace(/^--no-/, "--");
1467
+ if (!this._findOption(positiveLongFlag)) {
1468
+ this.setOptionValueWithSource(name, option.defaultValue === void 0 ? true : option.defaultValue, "default");
1469
+ }
1470
+ } else if (option.defaultValue !== void 0) {
1471
+ this.setOptionValueWithSource(name, option.defaultValue, "default");
1472
+ }
1473
+ const handleOptionValue = (val, invalidValueMessage, valueSource) => {
1474
+ if (val == null && option.presetArg !== void 0) {
1475
+ val = option.presetArg;
1476
+ }
1477
+ const oldValue = this.getOptionValue(name);
1478
+ if (val !== null && option.parseArg) {
1479
+ val = this._callParseArg(option, val, oldValue, invalidValueMessage);
1480
+ } else if (val !== null && option.variadic) {
1481
+ val = option._concatValue(val, oldValue);
1482
+ }
1483
+ if (val == null) {
1484
+ if (option.negate) {
1485
+ val = false;
1486
+ } else if (option.isBoolean() || option.optional) {
1487
+ val = true;
1488
+ } else {
1489
+ val = "";
1490
+ }
1491
+ }
1492
+ this.setOptionValueWithSource(name, val, valueSource);
1493
+ };
1494
+ this.on("option:" + oname, (val) => {
1495
+ const invalidValueMessage = `error: option '${option.flags}' argument '${val}' is invalid.`;
1496
+ handleOptionValue(val, invalidValueMessage, "cli");
1497
+ });
1498
+ if (option.envVar) {
1499
+ this.on("optionEnv:" + oname, (val) => {
1500
+ const invalidValueMessage = `error: option '${option.flags}' value '${val}' from env '${option.envVar}' is invalid.`;
1501
+ handleOptionValue(val, invalidValueMessage, "env");
1502
+ });
1503
+ }
1504
+ return this;
1505
+ }
1506
+ /**
1507
+ * Internal implementation shared by .option() and .requiredOption()
1508
+ *
1509
+ * @private
1510
+ */
1511
+ _optionEx(config, flags, description, fn, defaultValue) {
1512
+ if (typeof flags === "object" && flags instanceof Option2) {
1513
+ throw new Error("To add an Option object use addOption() instead of option() or requiredOption()");
1514
+ }
1515
+ const option = this.createOption(flags, description);
1516
+ option.makeOptionMandatory(!!config.mandatory);
1517
+ if (typeof fn === "function") {
1518
+ option.default(defaultValue).argParser(fn);
1519
+ } else if (fn instanceof RegExp) {
1520
+ const regex = fn;
1521
+ fn = (val, def) => {
1522
+ const m = regex.exec(val);
1523
+ return m ? m[0] : def;
1524
+ };
1525
+ option.default(defaultValue).argParser(fn);
1526
+ } else {
1527
+ option.default(fn);
1528
+ }
1529
+ return this.addOption(option);
1530
+ }
1531
+ /**
1532
+ * Define option with `flags`, `description`, and optional argument parsing function or `defaultValue` or both.
1533
+ *
1534
+ * The `flags` string contains the short and/or long flags, separated by comma, a pipe or space. A required
1535
+ * option-argument is indicated by `<>` and an optional option-argument by `[]`.
1536
+ *
1537
+ * See the README for more details, and see also addOption() and requiredOption().
1538
+ *
1539
+ * @example
1540
+ * program
1541
+ * .option('-p, --pepper', 'add pepper')
1542
+ * .option('-p, --pizza-type <TYPE>', 'type of pizza') // required option-argument
1543
+ * .option('-c, --cheese [CHEESE]', 'add extra cheese', 'mozzarella') // optional option-argument with default
1544
+ * .option('-t, --tip <VALUE>', 'add tip to purchase cost', parseFloat) // custom parse function
1545
+ *
1546
+ * @param {string} flags
1547
+ * @param {string} [description]
1548
+ * @param {(Function|*)} [parseArg] - custom option processing function or default value
1549
+ * @param {*} [defaultValue]
1550
+ * @return {Command} `this` command for chaining
1551
+ */
1552
+ option(flags, description, parseArg, defaultValue) {
1553
+ return this._optionEx({}, flags, description, parseArg, defaultValue);
1554
+ }
1555
+ /**
1556
+ * Add a required option which must have a value after parsing. This usually means
1557
+ * the option must be specified on the command line. (Otherwise the same as .option().)
1558
+ *
1559
+ * The `flags` string contains the short and/or long flags, separated by comma, a pipe or space.
1560
+ *
1561
+ * @param {string} flags
1562
+ * @param {string} [description]
1563
+ * @param {(Function|*)} [parseArg] - custom option processing function or default value
1564
+ * @param {*} [defaultValue]
1565
+ * @return {Command} `this` command for chaining
1566
+ */
1567
+ requiredOption(flags, description, parseArg, defaultValue) {
1568
+ return this._optionEx({ mandatory: true }, flags, description, parseArg, defaultValue);
1569
+ }
1570
+ /**
1571
+ * Alter parsing of short flags with optional values.
1572
+ *
1573
+ * @example
1574
+ * // for `.option('-f,--flag [value]'):
1575
+ * program.combineFlagAndOptionalValue(true); // `-f80` is treated like `--flag=80`, this is the default behaviour
1576
+ * program.combineFlagAndOptionalValue(false) // `-fb` is treated like `-f -b`
1577
+ *
1578
+ * @param {boolean} [combine=true] - if `true` or omitted, an optional value can be specified directly after the flag.
1579
+ */
1580
+ combineFlagAndOptionalValue(combine = true) {
1581
+ this._combineFlagAndOptionalValue = !!combine;
1582
+ return this;
1583
+ }
1584
+ /**
1585
+ * Allow unknown options on the command line.
1586
+ *
1587
+ * @param {boolean} [allowUnknown=true] - if `true` or omitted, no error will be thrown
1588
+ * for unknown options.
1589
+ */
1590
+ allowUnknownOption(allowUnknown = true) {
1591
+ this._allowUnknownOption = !!allowUnknown;
1592
+ return this;
1593
+ }
1594
+ /**
1595
+ * Allow excess command-arguments on the command line. Pass false to make excess arguments an error.
1596
+ *
1597
+ * @param {boolean} [allowExcess=true] - if `true` or omitted, no error will be thrown
1598
+ * for excess arguments.
1599
+ */
1600
+ allowExcessArguments(allowExcess = true) {
1601
+ this._allowExcessArguments = !!allowExcess;
1602
+ return this;
1603
+ }
1604
+ /**
1605
+ * Enable positional options. Positional means global options are specified before subcommands which lets
1606
+ * subcommands reuse the same option names, and also enables subcommands to turn on passThroughOptions.
1607
+ * The default behaviour is non-positional and global options may appear anywhere on the command line.
1608
+ *
1609
+ * @param {boolean} [positional=true]
1610
+ */
1611
+ enablePositionalOptions(positional = true) {
1612
+ this._enablePositionalOptions = !!positional;
1613
+ return this;
1614
+ }
1615
+ /**
1616
+ * Pass through options that come after command-arguments rather than treat them as command-options,
1617
+ * so actual command-options come before command-arguments. Turning this on for a subcommand requires
1618
+ * positional options to have been enabled on the program (parent commands).
1619
+ * The default behaviour is non-positional and options may appear before or after command-arguments.
1620
+ *
1621
+ * @param {boolean} [passThrough=true]
1622
+ * for unknown options.
1623
+ */
1624
+ passThroughOptions(passThrough = true) {
1625
+ this._passThroughOptions = !!passThrough;
1626
+ this._checkForBrokenPassThrough();
1627
+ return this;
1628
+ }
1629
+ /**
1630
+ * @private
1631
+ */
1632
+ _checkForBrokenPassThrough() {
1633
+ if (this.parent && this._passThroughOptions && !this.parent._enablePositionalOptions) {
1634
+ throw new Error(`passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`);
1635
+ }
1636
+ }
1637
+ /**
1638
+ * Whether to store option values as properties on command object,
1639
+ * or store separately (specify false). In both cases the option values can be accessed using .opts().
1640
+ *
1641
+ * @param {boolean} [storeAsProperties=true]
1642
+ * @return {Command} `this` command for chaining
1643
+ */
1644
+ storeOptionsAsProperties(storeAsProperties = true) {
1645
+ if (this.options.length) {
1646
+ throw new Error("call .storeOptionsAsProperties() before adding options");
1647
+ }
1648
+ if (Object.keys(this._optionValues).length) {
1649
+ throw new Error("call .storeOptionsAsProperties() before setting option values");
1650
+ }
1651
+ this._storeOptionsAsProperties = !!storeAsProperties;
1652
+ return this;
1653
+ }
1654
+ /**
1655
+ * Retrieve option value.
1656
+ *
1657
+ * @param {string} key
1658
+ * @return {Object} value
1659
+ */
1660
+ getOptionValue(key) {
1661
+ if (this._storeOptionsAsProperties) {
1662
+ return this[key];
1663
+ }
1664
+ return this._optionValues[key];
1665
+ }
1666
+ /**
1667
+ * Store option value.
1668
+ *
1669
+ * @param {string} key
1670
+ * @param {Object} value
1671
+ * @return {Command} `this` command for chaining
1672
+ */
1673
+ setOptionValue(key, value) {
1674
+ return this.setOptionValueWithSource(key, value, void 0);
1675
+ }
1676
+ /**
1677
+ * Store option value and where the value came from.
1678
+ *
1679
+ * @param {string} key
1680
+ * @param {Object} value
1681
+ * @param {string} source - expected values are default/config/env/cli/implied
1682
+ * @return {Command} `this` command for chaining
1683
+ */
1684
+ setOptionValueWithSource(key, value, source) {
1685
+ if (this._storeOptionsAsProperties) {
1686
+ this[key] = value;
1687
+ } else {
1688
+ this._optionValues[key] = value;
1689
+ }
1690
+ this._optionValueSources[key] = source;
1691
+ return this;
1692
+ }
1693
+ /**
1694
+ * Get source of option value.
1695
+ * Expected values are default | config | env | cli | implied
1696
+ *
1697
+ * @param {string} key
1698
+ * @return {string}
1699
+ */
1700
+ getOptionValueSource(key) {
1701
+ return this._optionValueSources[key];
1702
+ }
1703
+ /**
1704
+ * Get source of option value. See also .optsWithGlobals().
1705
+ * Expected values are default | config | env | cli | implied
1706
+ *
1707
+ * @param {string} key
1708
+ * @return {string}
1709
+ */
1710
+ getOptionValueSourceWithGlobals(key) {
1711
+ let source;
1712
+ this._getCommandAndAncestors().forEach((cmd) => {
1713
+ if (cmd.getOptionValueSource(key) !== void 0) {
1714
+ source = cmd.getOptionValueSource(key);
1715
+ }
1716
+ });
1717
+ return source;
1718
+ }
1719
+ /**
1720
+ * Get user arguments from implied or explicit arguments.
1721
+ * Side-effects: set _scriptPath if args included script. Used for default program name, and subcommand searches.
1722
+ *
1723
+ * @private
1724
+ */
1725
+ _prepareUserArgs(argv, parseOptions) {
1726
+ if (argv !== void 0 && !Array.isArray(argv)) {
1727
+ throw new Error("first parameter to parse must be array or undefined");
1728
+ }
1729
+ parseOptions = parseOptions || {};
1730
+ if (argv === void 0) {
1731
+ argv = process2.argv;
1732
+ if (process2.versions && process2.versions.electron) {
1733
+ parseOptions.from = "electron";
1734
+ }
1735
+ }
1736
+ this.rawArgs = argv.slice();
1737
+ let userArgs;
1738
+ switch (parseOptions.from) {
1739
+ case void 0:
1740
+ case "node":
1741
+ this._scriptPath = argv[1];
1742
+ userArgs = argv.slice(2);
1743
+ break;
1744
+ case "electron":
1745
+ if (process2.defaultApp) {
1746
+ this._scriptPath = argv[1];
1747
+ userArgs = argv.slice(2);
1748
+ } else {
1749
+ userArgs = argv.slice(1);
1750
+ }
1751
+ break;
1752
+ case "user":
1753
+ userArgs = argv.slice(0);
1754
+ break;
1755
+ default:
1756
+ throw new Error(`unexpected parse option { from: '${parseOptions.from}' }`);
1757
+ }
1758
+ if (!this._name && this._scriptPath) this.nameFromFilename(this._scriptPath);
1759
+ this._name = this._name || "program";
1760
+ return userArgs;
1761
+ }
1762
+ /**
1763
+ * Parse `argv`, setting options and invoking commands when defined.
1764
+ *
1765
+ * The default expectation is that the arguments are from node and have the application as argv[0]
1766
+ * and the script being run in argv[1], with user parameters after that.
1767
+ *
1768
+ * @example
1769
+ * program.parse(process.argv);
1770
+ * program.parse(); // implicitly use process.argv and auto-detect node vs electron conventions
1771
+ * program.parse(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
1772
+ *
1773
+ * @param {string[]} [argv] - optional, defaults to process.argv
1774
+ * @param {Object} [parseOptions] - optionally specify style of options with from: node/user/electron
1775
+ * @param {string} [parseOptions.from] - where the args are from: 'node', 'user', 'electron'
1776
+ * @return {Command} `this` command for chaining
1777
+ */
1778
+ parse(argv, parseOptions) {
1779
+ const userArgs = this._prepareUserArgs(argv, parseOptions);
1780
+ this._parseCommand([], userArgs);
1781
+ return this;
1782
+ }
1783
+ /**
1784
+ * Parse `argv`, setting options and invoking commands when defined.
1785
+ *
1786
+ * Use parseAsync instead of parse if any of your action handlers are async. Returns a Promise.
1787
+ *
1788
+ * The default expectation is that the arguments are from node and have the application as argv[0]
1789
+ * and the script being run in argv[1], with user parameters after that.
1790
+ *
1791
+ * @example
1792
+ * await program.parseAsync(process.argv);
1793
+ * await program.parseAsync(); // implicitly use process.argv and auto-detect node vs electron conventions
1794
+ * await program.parseAsync(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
1795
+ *
1796
+ * @param {string[]} [argv]
1797
+ * @param {Object} [parseOptions]
1798
+ * @param {string} parseOptions.from - where the args are from: 'node', 'user', 'electron'
1799
+ * @return {Promise}
1800
+ */
1801
+ async parseAsync(argv, parseOptions) {
1802
+ const userArgs = this._prepareUserArgs(argv, parseOptions);
1803
+ await this._parseCommand([], userArgs);
1804
+ return this;
1805
+ }
1806
+ /**
1807
+ * Execute a sub-command executable.
1808
+ *
1809
+ * @private
1810
+ */
1811
+ _executeSubCommand(subcommand, args) {
1812
+ args = args.slice();
1813
+ let launchWithNode = false;
1814
+ const sourceExt = [".js", ".ts", ".tsx", ".mjs", ".cjs"];
1815
+ function findFile(baseDir, baseName) {
1816
+ const localBin = path6.resolve(baseDir, baseName);
1817
+ if (fs4.existsSync(localBin)) return localBin;
1818
+ if (sourceExt.includes(path6.extname(baseName))) return void 0;
1819
+ const foundExt = sourceExt.find((ext) => fs4.existsSync(`${localBin}${ext}`));
1820
+ if (foundExt) return `${localBin}${foundExt}`;
1821
+ return void 0;
1822
+ }
1823
+ this._checkForMissingMandatoryOptions();
1824
+ this._checkForConflictingOptions();
1825
+ let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;
1826
+ let executableDir = this._executableDir || "";
1827
+ if (this._scriptPath) {
1828
+ let resolvedScriptPath;
1829
+ try {
1830
+ resolvedScriptPath = fs4.realpathSync(this._scriptPath);
1831
+ } catch (err) {
1832
+ resolvedScriptPath = this._scriptPath;
1833
+ }
1834
+ executableDir = path6.resolve(path6.dirname(resolvedScriptPath), executableDir);
1835
+ }
1836
+ if (executableDir) {
1837
+ let localFile = findFile(executableDir, executableFile);
1838
+ if (!localFile && !subcommand._executableFile && this._scriptPath) {
1839
+ const legacyName = path6.basename(this._scriptPath, path6.extname(this._scriptPath));
1840
+ if (legacyName !== this._name) {
1841
+ localFile = findFile(executableDir, `${legacyName}-${subcommand._name}`);
1842
+ }
1843
+ }
1844
+ executableFile = localFile || executableFile;
1845
+ }
1846
+ launchWithNode = sourceExt.includes(path6.extname(executableFile));
1847
+ let proc;
1848
+ if (process2.platform !== "win32") {
1849
+ if (launchWithNode) {
1850
+ args.unshift(executableFile);
1851
+ args = incrementNodeInspectorPort(process2.execArgv).concat(args);
1852
+ proc = childProcess.spawn(process2.argv[0], args, { stdio: "inherit" });
1853
+ } else {
1854
+ proc = childProcess.spawn(executableFile, args, { stdio: "inherit" });
1855
+ }
1856
+ } else {
1857
+ args.unshift(executableFile);
1858
+ args = incrementNodeInspectorPort(process2.execArgv).concat(args);
1859
+ proc = childProcess.spawn(process2.execPath, args, { stdio: "inherit" });
1860
+ }
1861
+ if (!proc.killed) {
1862
+ const signals = ["SIGUSR1", "SIGUSR2", "SIGTERM", "SIGINT", "SIGHUP"];
1863
+ signals.forEach((signal) => {
1864
+ process2.on(signal, () => {
1865
+ if (proc.killed === false && proc.exitCode === null) {
1866
+ proc.kill(signal);
1867
+ }
1868
+ });
1869
+ });
1870
+ }
1871
+ const exitCallback = this._exitCallback;
1872
+ proc.on("close", (code, _signal) => {
1873
+ code = code ?? 1;
1874
+ if (!exitCallback) {
1875
+ process2.exit(code);
1876
+ } else {
1877
+ exitCallback(new CommanderError2(code, "commander.executeSubCommandAsync", "(close)"));
1878
+ }
1879
+ });
1880
+ proc.on("error", (err) => {
1881
+ if (err.code === "ENOENT") {
1882
+ 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";
1883
+ const executableMissing = `'${executableFile}' does not exist
1884
+ - if '${subcommand._name}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
1885
+ - if the default executable name is not suitable, use the executableFile option to supply a custom name or path
1886
+ - ${executableDirMessage}`;
1887
+ throw new Error(executableMissing);
1888
+ } else if (err.code === "EACCES") {
1889
+ throw new Error(`'${executableFile}' not executable`);
1890
+ }
1891
+ if (!exitCallback) {
1892
+ process2.exit(1);
1893
+ } else {
1894
+ const wrappedError = new CommanderError2(1, "commander.executeSubCommandAsync", "(error)");
1895
+ wrappedError.nestedError = err;
1896
+ exitCallback(wrappedError);
1897
+ }
1898
+ });
1899
+ this.runningCommand = proc;
1900
+ }
1901
+ /**
1902
+ * @private
1903
+ */
1904
+ _dispatchSubcommand(commandName, operands, unknown) {
1905
+ const subCommand = this._findCommand(commandName);
1906
+ if (!subCommand) this.help({ error: true });
1907
+ let promiseChain;
1908
+ promiseChain = this._chainOrCallSubCommandHook(promiseChain, subCommand, "preSubcommand");
1909
+ promiseChain = this._chainOrCall(promiseChain, () => {
1910
+ if (subCommand._executableHandler) {
1911
+ this._executeSubCommand(subCommand, operands.concat(unknown));
1912
+ } else {
1913
+ return subCommand._parseCommand(operands, unknown);
1914
+ }
1915
+ });
1916
+ return promiseChain;
1917
+ }
1918
+ /**
1919
+ * Invoke help directly if possible, or dispatch if necessary.
1920
+ * e.g. help foo
1921
+ *
1922
+ * @private
1923
+ */
1924
+ _dispatchHelpCommand(subcommandName) {
1925
+ if (!subcommandName) {
1926
+ this.help();
1927
+ }
1928
+ const subCommand = this._findCommand(subcommandName);
1929
+ if (subCommand && !subCommand._executableHandler) {
1930
+ subCommand.help();
1931
+ }
1932
+ return this._dispatchSubcommand(subcommandName, [], [
1933
+ this._getHelpOption()?.long ?? this._getHelpOption()?.short ?? "--help"
1934
+ ]);
1935
+ }
1936
+ /**
1937
+ * Check this.args against expected this.registeredArguments.
1938
+ *
1939
+ * @private
1940
+ */
1941
+ _checkNumberOfArguments() {
1942
+ this.registeredArguments.forEach((arg, i) => {
1943
+ if (arg.required && this.args[i] == null) {
1944
+ this.missingArgument(arg.name());
1945
+ }
1946
+ });
1947
+ if (this.registeredArguments.length > 0 && this.registeredArguments[this.registeredArguments.length - 1].variadic) {
1948
+ return;
1949
+ }
1950
+ if (this.args.length > this.registeredArguments.length) {
1951
+ this._excessArguments(this.args);
1952
+ }
1953
+ }
1954
+ /**
1955
+ * Process this.args using this.registeredArguments and save as this.processedArgs!
1956
+ *
1957
+ * @private
1958
+ */
1959
+ _processArguments() {
1960
+ const myParseArg = (argument, value, previous) => {
1961
+ let parsedValue = value;
1962
+ if (value !== null && argument.parseArg) {
1963
+ const invalidValueMessage = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'.`;
1964
+ parsedValue = this._callParseArg(argument, value, previous, invalidValueMessage);
1965
+ }
1966
+ return parsedValue;
1967
+ };
1968
+ this._checkNumberOfArguments();
1969
+ const processedArgs = [];
1970
+ this.registeredArguments.forEach((declaredArg, index) => {
1971
+ let value = declaredArg.defaultValue;
1972
+ if (declaredArg.variadic) {
1973
+ if (index < this.args.length) {
1974
+ value = this.args.slice(index);
1975
+ if (declaredArg.parseArg) {
1976
+ value = value.reduce((processed, v) => {
1977
+ return myParseArg(declaredArg, v, processed);
1978
+ }, declaredArg.defaultValue);
1979
+ }
1980
+ } else if (value === void 0) {
1981
+ value = [];
1982
+ }
1983
+ } else if (index < this.args.length) {
1984
+ value = this.args[index];
1985
+ if (declaredArg.parseArg) {
1986
+ value = myParseArg(declaredArg, value, declaredArg.defaultValue);
1987
+ }
1988
+ }
1989
+ processedArgs[index] = value;
1990
+ });
1991
+ this.processedArgs = processedArgs;
1992
+ }
1993
+ /**
1994
+ * Once we have a promise we chain, but call synchronously until then.
1995
+ *
1996
+ * @param {(Promise|undefined)} promise
1997
+ * @param {Function} fn
1998
+ * @return {(Promise|undefined)}
1999
+ * @private
2000
+ */
2001
+ _chainOrCall(promise, fn) {
2002
+ if (promise && promise.then && typeof promise.then === "function") {
2003
+ return promise.then(() => fn());
2004
+ }
2005
+ return fn();
2006
+ }
2007
+ /**
2008
+ *
2009
+ * @param {(Promise|undefined)} promise
2010
+ * @param {string} event
2011
+ * @return {(Promise|undefined)}
2012
+ * @private
2013
+ */
2014
+ _chainOrCallHooks(promise, event) {
2015
+ let result = promise;
2016
+ const hooks = [];
2017
+ this._getCommandAndAncestors().reverse().filter((cmd) => cmd._lifeCycleHooks[event] !== void 0).forEach((hookedCommand) => {
2018
+ hookedCommand._lifeCycleHooks[event].forEach((callback) => {
2019
+ hooks.push({ hookedCommand, callback });
2020
+ });
2021
+ });
2022
+ if (event === "postAction") {
2023
+ hooks.reverse();
2024
+ }
2025
+ hooks.forEach((hookDetail) => {
2026
+ result = this._chainOrCall(result, () => {
2027
+ return hookDetail.callback(hookDetail.hookedCommand, this);
2028
+ });
2029
+ });
2030
+ return result;
2031
+ }
2032
+ /**
2033
+ *
2034
+ * @param {(Promise|undefined)} promise
2035
+ * @param {Command} subCommand
2036
+ * @param {string} event
2037
+ * @return {(Promise|undefined)}
2038
+ * @private
2039
+ */
2040
+ _chainOrCallSubCommandHook(promise, subCommand, event) {
2041
+ let result = promise;
2042
+ if (this._lifeCycleHooks[event] !== void 0) {
2043
+ this._lifeCycleHooks[event].forEach((hook) => {
2044
+ result = this._chainOrCall(result, () => {
2045
+ return hook(this, subCommand);
2046
+ });
2047
+ });
2048
+ }
2049
+ return result;
2050
+ }
2051
+ /**
2052
+ * Process arguments in context of this command.
2053
+ * Returns action result, in case it is a promise.
2054
+ *
2055
+ * @private
2056
+ */
2057
+ _parseCommand(operands, unknown) {
2058
+ const parsed = this.parseOptions(unknown);
2059
+ this._parseOptionsEnv();
2060
+ this._parseOptionsImplied();
2061
+ operands = operands.concat(parsed.operands);
2062
+ unknown = parsed.unknown;
2063
+ this.args = operands.concat(unknown);
2064
+ if (operands && this._findCommand(operands[0])) {
2065
+ return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
2066
+ }
2067
+ if (this._getHelpCommand() && operands[0] === this._getHelpCommand().name()) {
2068
+ return this._dispatchHelpCommand(operands[1]);
2069
+ }
2070
+ if (this._defaultCommandName) {
2071
+ this._outputHelpIfRequested(unknown);
2072
+ return this._dispatchSubcommand(this._defaultCommandName, operands, unknown);
2073
+ }
2074
+ if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) {
2075
+ this.help({ error: true });
2076
+ }
2077
+ this._outputHelpIfRequested(parsed.unknown);
2078
+ this._checkForMissingMandatoryOptions();
2079
+ this._checkForConflictingOptions();
2080
+ const checkForUnknownOptions = () => {
2081
+ if (parsed.unknown.length > 0) {
2082
+ this.unknownOption(parsed.unknown[0]);
2083
+ }
2084
+ };
2085
+ const commandEvent = `command:${this.name()}`;
2086
+ if (this._actionHandler) {
2087
+ checkForUnknownOptions();
2088
+ this._processArguments();
2089
+ let promiseChain;
2090
+ promiseChain = this._chainOrCallHooks(promiseChain, "preAction");
2091
+ promiseChain = this._chainOrCall(promiseChain, () => this._actionHandler(this.processedArgs));
2092
+ if (this.parent) {
2093
+ promiseChain = this._chainOrCall(promiseChain, () => {
2094
+ this.parent.emit(commandEvent, operands, unknown);
2095
+ });
2096
+ }
2097
+ promiseChain = this._chainOrCallHooks(promiseChain, "postAction");
2098
+ return promiseChain;
2099
+ }
2100
+ if (this.parent && this.parent.listenerCount(commandEvent)) {
2101
+ checkForUnknownOptions();
2102
+ this._processArguments();
2103
+ this.parent.emit(commandEvent, operands, unknown);
2104
+ } else if (operands.length) {
2105
+ if (this._findCommand("*")) {
2106
+ return this._dispatchSubcommand("*", operands, unknown);
2107
+ }
2108
+ if (this.listenerCount("command:*")) {
2109
+ this.emit("command:*", operands, unknown);
2110
+ } else if (this.commands.length) {
2111
+ this.unknownCommand();
2112
+ } else {
2113
+ checkForUnknownOptions();
2114
+ this._processArguments();
2115
+ }
2116
+ } else if (this.commands.length) {
2117
+ checkForUnknownOptions();
2118
+ this.help({ error: true });
2119
+ } else {
2120
+ checkForUnknownOptions();
2121
+ this._processArguments();
2122
+ }
2123
+ }
2124
+ /**
2125
+ * Find matching command.
2126
+ *
2127
+ * @private
2128
+ */
2129
+ _findCommand(name) {
2130
+ if (!name) return void 0;
2131
+ return this.commands.find((cmd) => cmd._name === name || cmd._aliases.includes(name));
2132
+ }
2133
+ /**
2134
+ * Return an option matching `arg` if any.
2135
+ *
2136
+ * @param {string} arg
2137
+ * @return {Option}
2138
+ * @package internal use only
2139
+ */
2140
+ _findOption(arg) {
2141
+ return this.options.find((option) => option.is(arg));
2142
+ }
2143
+ /**
2144
+ * Display an error message if a mandatory option does not have a value.
2145
+ * Called after checking for help flags in leaf subcommand.
2146
+ *
2147
+ * @private
2148
+ */
2149
+ _checkForMissingMandatoryOptions() {
2150
+ this._getCommandAndAncestors().forEach((cmd) => {
2151
+ cmd.options.forEach((anOption) => {
2152
+ if (anOption.mandatory && cmd.getOptionValue(anOption.attributeName()) === void 0) {
2153
+ cmd.missingMandatoryOptionValue(anOption);
2154
+ }
2155
+ });
2156
+ });
2157
+ }
2158
+ /**
2159
+ * Display an error message if conflicting options are used together in this.
2160
+ *
2161
+ * @private
2162
+ */
2163
+ _checkForConflictingLocalOptions() {
2164
+ const definedNonDefaultOptions = this.options.filter(
2165
+ (option) => {
2166
+ const optionKey = option.attributeName();
2167
+ if (this.getOptionValue(optionKey) === void 0) {
2168
+ return false;
2169
+ }
2170
+ return this.getOptionValueSource(optionKey) !== "default";
2171
+ }
2172
+ );
2173
+ const optionsWithConflicting = definedNonDefaultOptions.filter(
2174
+ (option) => option.conflictsWith.length > 0
2175
+ );
2176
+ optionsWithConflicting.forEach((option) => {
2177
+ const conflictingAndDefined = definedNonDefaultOptions.find(
2178
+ (defined) => option.conflictsWith.includes(defined.attributeName())
2179
+ );
2180
+ if (conflictingAndDefined) {
2181
+ this._conflictingOption(option, conflictingAndDefined);
2182
+ }
2183
+ });
2184
+ }
2185
+ /**
2186
+ * Display an error message if conflicting options are used together.
2187
+ * Called after checking for help flags in leaf subcommand.
2188
+ *
2189
+ * @private
2190
+ */
2191
+ _checkForConflictingOptions() {
2192
+ this._getCommandAndAncestors().forEach((cmd) => {
2193
+ cmd._checkForConflictingLocalOptions();
2194
+ });
2195
+ }
2196
+ /**
2197
+ * Parse options from `argv` removing known options,
2198
+ * and return argv split into operands and unknown arguments.
2199
+ *
2200
+ * Examples:
2201
+ *
2202
+ * argv => operands, unknown
2203
+ * --known kkk op => [op], []
2204
+ * op --known kkk => [op], []
2205
+ * sub --unknown uuu op => [sub], [--unknown uuu op]
2206
+ * sub -- --unknown uuu op => [sub --unknown uuu op], []
2207
+ *
2208
+ * @param {string[]} argv
2209
+ * @return {{operands: string[], unknown: string[]}}
2210
+ */
2211
+ parseOptions(argv) {
2212
+ const operands = [];
2213
+ const unknown = [];
2214
+ let dest = operands;
2215
+ const args = argv.slice();
2216
+ function maybeOption(arg) {
2217
+ return arg.length > 1 && arg[0] === "-";
2218
+ }
2219
+ let activeVariadicOption = null;
2220
+ while (args.length) {
2221
+ const arg = args.shift();
2222
+ if (arg === "--") {
2223
+ if (dest === unknown) dest.push(arg);
2224
+ dest.push(...args);
2225
+ break;
2226
+ }
2227
+ if (activeVariadicOption && !maybeOption(arg)) {
2228
+ this.emit(`option:${activeVariadicOption.name()}`, arg);
2229
+ continue;
2230
+ }
2231
+ activeVariadicOption = null;
2232
+ if (maybeOption(arg)) {
2233
+ const option = this._findOption(arg);
2234
+ if (option) {
2235
+ if (option.required) {
2236
+ const value = args.shift();
2237
+ if (value === void 0) this.optionMissingArgument(option);
2238
+ this.emit(`option:${option.name()}`, value);
2239
+ } else if (option.optional) {
2240
+ let value = null;
2241
+ if (args.length > 0 && !maybeOption(args[0])) {
2242
+ value = args.shift();
2243
+ }
2244
+ this.emit(`option:${option.name()}`, value);
2245
+ } else {
2246
+ this.emit(`option:${option.name()}`);
2247
+ }
2248
+ activeVariadicOption = option.variadic ? option : null;
2249
+ continue;
2250
+ }
2251
+ }
2252
+ if (arg.length > 2 && arg[0] === "-" && arg[1] !== "-") {
2253
+ const option = this._findOption(`-${arg[1]}`);
2254
+ if (option) {
2255
+ if (option.required || option.optional && this._combineFlagAndOptionalValue) {
2256
+ this.emit(`option:${option.name()}`, arg.slice(2));
2257
+ } else {
2258
+ this.emit(`option:${option.name()}`);
2259
+ args.unshift(`-${arg.slice(2)}`);
2260
+ }
2261
+ continue;
2262
+ }
2263
+ }
2264
+ if (/^--[^=]+=/.test(arg)) {
2265
+ const index = arg.indexOf("=");
2266
+ const option = this._findOption(arg.slice(0, index));
2267
+ if (option && (option.required || option.optional)) {
2268
+ this.emit(`option:${option.name()}`, arg.slice(index + 1));
2269
+ continue;
2270
+ }
2271
+ }
2272
+ if (maybeOption(arg)) {
2273
+ dest = unknown;
2274
+ }
2275
+ if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
2276
+ if (this._findCommand(arg)) {
2277
+ operands.push(arg);
2278
+ if (args.length > 0) unknown.push(...args);
2279
+ break;
2280
+ } else if (this._getHelpCommand() && arg === this._getHelpCommand().name()) {
2281
+ operands.push(arg);
2282
+ if (args.length > 0) operands.push(...args);
2283
+ break;
2284
+ } else if (this._defaultCommandName) {
2285
+ unknown.push(arg);
2286
+ if (args.length > 0) unknown.push(...args);
2287
+ break;
2288
+ }
2289
+ }
2290
+ if (this._passThroughOptions) {
2291
+ dest.push(arg);
2292
+ if (args.length > 0) dest.push(...args);
2293
+ break;
2294
+ }
2295
+ dest.push(arg);
2296
+ }
2297
+ return { operands, unknown };
2298
+ }
2299
+ /**
2300
+ * Return an object containing local option values as key-value pairs.
2301
+ *
2302
+ * @return {Object}
2303
+ */
2304
+ opts() {
2305
+ if (this._storeOptionsAsProperties) {
2306
+ const result = {};
2307
+ const len = this.options.length;
2308
+ for (let i = 0; i < len; i++) {
2309
+ const key = this.options[i].attributeName();
2310
+ result[key] = key === this._versionOptionName ? this._version : this[key];
2311
+ }
2312
+ return result;
2313
+ }
2314
+ return this._optionValues;
2315
+ }
2316
+ /**
2317
+ * Return an object containing merged local and global option values as key-value pairs.
2318
+ *
2319
+ * @return {Object}
2320
+ */
2321
+ optsWithGlobals() {
2322
+ return this._getCommandAndAncestors().reduce(
2323
+ (combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()),
2324
+ {}
2325
+ );
2326
+ }
2327
+ /**
2328
+ * Display error message and exit (or call exitOverride).
2329
+ *
2330
+ * @param {string} message
2331
+ * @param {Object} [errorOptions]
2332
+ * @param {string} [errorOptions.code] - an id string representing the error
2333
+ * @param {number} [errorOptions.exitCode] - used with process.exit
2334
+ */
2335
+ error(message, errorOptions) {
2336
+ this._outputConfiguration.outputError(`${message}
2337
+ `, this._outputConfiguration.writeErr);
2338
+ if (typeof this._showHelpAfterError === "string") {
2339
+ this._outputConfiguration.writeErr(`${this._showHelpAfterError}
2340
+ `);
2341
+ } else if (this._showHelpAfterError) {
2342
+ this._outputConfiguration.writeErr("\n");
2343
+ this.outputHelp({ error: true });
2344
+ }
2345
+ const config = errorOptions || {};
2346
+ const exitCode = config.exitCode || 1;
2347
+ const code = config.code || "commander.error";
2348
+ this._exit(exitCode, code, message);
2349
+ }
2350
+ /**
2351
+ * Apply any option related environment variables, if option does
2352
+ * not have a value from cli or client code.
2353
+ *
2354
+ * @private
2355
+ */
2356
+ _parseOptionsEnv() {
2357
+ this.options.forEach((option) => {
2358
+ if (option.envVar && option.envVar in process2.env) {
2359
+ const optionKey = option.attributeName();
2360
+ if (this.getOptionValue(optionKey) === void 0 || ["default", "config", "env"].includes(this.getOptionValueSource(optionKey))) {
2361
+ if (option.required || option.optional) {
2362
+ this.emit(`optionEnv:${option.name()}`, process2.env[option.envVar]);
2363
+ } else {
2364
+ this.emit(`optionEnv:${option.name()}`);
2365
+ }
2366
+ }
2367
+ }
2368
+ });
2369
+ }
2370
+ /**
2371
+ * Apply any implied option values, if option is undefined or default value.
2372
+ *
2373
+ * @private
2374
+ */
2375
+ _parseOptionsImplied() {
2376
+ const dualHelper = new DualOptions(this.options);
2377
+ const hasCustomOptionValue = (optionKey) => {
2378
+ return this.getOptionValue(optionKey) !== void 0 && !["default", "implied"].includes(this.getOptionValueSource(optionKey));
2379
+ };
2380
+ this.options.filter((option) => option.implied !== void 0 && hasCustomOptionValue(option.attributeName()) && dualHelper.valueFromOption(this.getOptionValue(option.attributeName()), option)).forEach((option) => {
2381
+ Object.keys(option.implied).filter((impliedKey) => !hasCustomOptionValue(impliedKey)).forEach((impliedKey) => {
2382
+ this.setOptionValueWithSource(impliedKey, option.implied[impliedKey], "implied");
2383
+ });
2384
+ });
2385
+ }
2386
+ /**
2387
+ * Argument `name` is missing.
2388
+ *
2389
+ * @param {string} name
2390
+ * @private
2391
+ */
2392
+ missingArgument(name) {
2393
+ const message = `error: missing required argument '${name}'`;
2394
+ this.error(message, { code: "commander.missingArgument" });
2395
+ }
2396
+ /**
2397
+ * `Option` is missing an argument.
2398
+ *
2399
+ * @param {Option} option
2400
+ * @private
2401
+ */
2402
+ optionMissingArgument(option) {
2403
+ const message = `error: option '${option.flags}' argument missing`;
2404
+ this.error(message, { code: "commander.optionMissingArgument" });
2405
+ }
2406
+ /**
2407
+ * `Option` does not have a value, and is a mandatory option.
2408
+ *
2409
+ * @param {Option} option
2410
+ * @private
2411
+ */
2412
+ missingMandatoryOptionValue(option) {
2413
+ const message = `error: required option '${option.flags}' not specified`;
2414
+ this.error(message, { code: "commander.missingMandatoryOptionValue" });
2415
+ }
2416
+ /**
2417
+ * `Option` conflicts with another option.
2418
+ *
2419
+ * @param {Option} option
2420
+ * @param {Option} conflictingOption
2421
+ * @private
2422
+ */
2423
+ _conflictingOption(option, conflictingOption) {
2424
+ const findBestOptionFromValue = (option2) => {
2425
+ const optionKey = option2.attributeName();
2426
+ const optionValue = this.getOptionValue(optionKey);
2427
+ const negativeOption = this.options.find((target) => target.negate && optionKey === target.attributeName());
2428
+ const positiveOption = this.options.find((target) => !target.negate && optionKey === target.attributeName());
2429
+ if (negativeOption && (negativeOption.presetArg === void 0 && optionValue === false || negativeOption.presetArg !== void 0 && optionValue === negativeOption.presetArg)) {
2430
+ return negativeOption;
2431
+ }
2432
+ return positiveOption || option2;
2433
+ };
2434
+ const getErrorMessage = (option2) => {
2435
+ const bestOption = findBestOptionFromValue(option2);
2436
+ const optionKey = bestOption.attributeName();
2437
+ const source = this.getOptionValueSource(optionKey);
2438
+ if (source === "env") {
2439
+ return `environment variable '${bestOption.envVar}'`;
2440
+ }
2441
+ return `option '${bestOption.flags}'`;
2442
+ };
2443
+ const message = `error: ${getErrorMessage(option)} cannot be used with ${getErrorMessage(conflictingOption)}`;
2444
+ this.error(message, { code: "commander.conflictingOption" });
2445
+ }
2446
+ /**
2447
+ * Unknown option `flag`.
2448
+ *
2449
+ * @param {string} flag
2450
+ * @private
2451
+ */
2452
+ unknownOption(flag) {
2453
+ if (this._allowUnknownOption) return;
2454
+ let suggestion = "";
2455
+ if (flag.startsWith("--") && this._showSuggestionAfterError) {
2456
+ let candidateFlags = [];
2457
+ let command = this;
2458
+ do {
2459
+ const moreFlags = command.createHelp().visibleOptions(command).filter((option) => option.long).map((option) => option.long);
2460
+ candidateFlags = candidateFlags.concat(moreFlags);
2461
+ command = command.parent;
2462
+ } while (command && !command._enablePositionalOptions);
2463
+ suggestion = suggestSimilar(flag, candidateFlags);
2464
+ }
2465
+ const message = `error: unknown option '${flag}'${suggestion}`;
2466
+ this.error(message, { code: "commander.unknownOption" });
2467
+ }
2468
+ /**
2469
+ * Excess arguments, more than expected.
2470
+ *
2471
+ * @param {string[]} receivedArgs
2472
+ * @private
2473
+ */
2474
+ _excessArguments(receivedArgs) {
2475
+ if (this._allowExcessArguments) return;
2476
+ const expected = this.registeredArguments.length;
2477
+ const s = expected === 1 ? "" : "s";
2478
+ const forSubcommand = this.parent ? ` for '${this.name()}'` : "";
2479
+ const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;
2480
+ this.error(message, { code: "commander.excessArguments" });
2481
+ }
2482
+ /**
2483
+ * Unknown command.
2484
+ *
2485
+ * @private
2486
+ */
2487
+ unknownCommand() {
2488
+ const unknownName = this.args[0];
2489
+ let suggestion = "";
2490
+ if (this._showSuggestionAfterError) {
2491
+ const candidateNames = [];
2492
+ this.createHelp().visibleCommands(this).forEach((command) => {
2493
+ candidateNames.push(command.name());
2494
+ if (command.alias()) candidateNames.push(command.alias());
2495
+ });
2496
+ suggestion = suggestSimilar(unknownName, candidateNames);
2497
+ }
2498
+ const message = `error: unknown command '${unknownName}'${suggestion}`;
2499
+ this.error(message, { code: "commander.unknownCommand" });
2500
+ }
2501
+ /**
2502
+ * Get or set the program version.
2503
+ *
2504
+ * This method auto-registers the "-V, --version" option which will print the version number.
2505
+ *
2506
+ * You can optionally supply the flags and description to override the defaults.
2507
+ *
2508
+ * @param {string} [str]
2509
+ * @param {string} [flags]
2510
+ * @param {string} [description]
2511
+ * @return {(this | string | undefined)} `this` command for chaining, or version string if no arguments
2512
+ */
2513
+ version(str, flags, description) {
2514
+ if (str === void 0) return this._version;
2515
+ this._version = str;
2516
+ flags = flags || "-V, --version";
2517
+ description = description || "output the version number";
2518
+ const versionOption = this.createOption(flags, description);
2519
+ this._versionOptionName = versionOption.attributeName();
2520
+ this._registerOption(versionOption);
2521
+ this.on("option:" + versionOption.name(), () => {
2522
+ this._outputConfiguration.writeOut(`${str}
2523
+ `);
2524
+ this._exit(0, "commander.version", str);
2525
+ });
2526
+ return this;
2527
+ }
2528
+ /**
2529
+ * Set the description.
2530
+ *
2531
+ * @param {string} [str]
2532
+ * @param {Object} [argsDescription]
2533
+ * @return {(string|Command)}
2534
+ */
2535
+ description(str, argsDescription) {
2536
+ if (str === void 0 && argsDescription === void 0) return this._description;
2537
+ this._description = str;
2538
+ if (argsDescription) {
2539
+ this._argsDescription = argsDescription;
2540
+ }
2541
+ return this;
2542
+ }
2543
+ /**
2544
+ * Set the summary. Used when listed as subcommand of parent.
2545
+ *
2546
+ * @param {string} [str]
2547
+ * @return {(string|Command)}
2548
+ */
2549
+ summary(str) {
2550
+ if (str === void 0) return this._summary;
2551
+ this._summary = str;
2552
+ return this;
2553
+ }
2554
+ /**
2555
+ * Set an alias for the command.
2556
+ *
2557
+ * You may call more than once to add multiple aliases. Only the first alias is shown in the auto-generated help.
2558
+ *
2559
+ * @param {string} [alias]
2560
+ * @return {(string|Command)}
2561
+ */
2562
+ alias(alias) {
2563
+ if (alias === void 0) return this._aliases[0];
2564
+ let command = this;
2565
+ if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
2566
+ command = this.commands[this.commands.length - 1];
2567
+ }
2568
+ if (alias === command._name) throw new Error("Command alias can't be the same as its name");
2569
+ const matchingCommand = this.parent?._findCommand(alias);
2570
+ if (matchingCommand) {
2571
+ const existingCmd = [matchingCommand.name()].concat(matchingCommand.aliases()).join("|");
2572
+ throw new Error(`cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`);
2573
+ }
2574
+ command._aliases.push(alias);
2575
+ return this;
2576
+ }
2577
+ /**
2578
+ * Set aliases for the command.
2579
+ *
2580
+ * Only the first alias is shown in the auto-generated help.
2581
+ *
2582
+ * @param {string[]} [aliases]
2583
+ * @return {(string[]|Command)}
2584
+ */
2585
+ aliases(aliases) {
2586
+ if (aliases === void 0) return this._aliases;
2587
+ aliases.forEach((alias) => this.alias(alias));
2588
+ return this;
2589
+ }
2590
+ /**
2591
+ * Set / get the command usage `str`.
2592
+ *
2593
+ * @param {string} [str]
2594
+ * @return {(string|Command)}
2595
+ */
2596
+ usage(str) {
2597
+ if (str === void 0) {
2598
+ if (this._usage) return this._usage;
2599
+ const args = this.registeredArguments.map((arg) => {
2600
+ return humanReadableArgName(arg);
2601
+ });
2602
+ return [].concat(
2603
+ this.options.length || this._helpOption !== null ? "[options]" : [],
2604
+ this.commands.length ? "[command]" : [],
2605
+ this.registeredArguments.length ? args : []
2606
+ ).join(" ");
2607
+ }
2608
+ this._usage = str;
2609
+ return this;
2610
+ }
2611
+ /**
2612
+ * Get or set the name of the command.
2613
+ *
2614
+ * @param {string} [str]
2615
+ * @return {(string|Command)}
2616
+ */
2617
+ name(str) {
2618
+ if (str === void 0) return this._name;
2619
+ this._name = str;
2620
+ return this;
2621
+ }
2622
+ /**
2623
+ * Set the name of the command from script filename, such as process.argv[1],
2624
+ * or require.main.filename, or __filename.
2625
+ *
2626
+ * (Used internally and public although not documented in README.)
2627
+ *
2628
+ * @example
2629
+ * program.nameFromFilename(require.main.filename);
2630
+ *
2631
+ * @param {string} filename
2632
+ * @return {Command}
2633
+ */
2634
+ nameFromFilename(filename) {
2635
+ this._name = path6.basename(filename, path6.extname(filename));
2636
+ return this;
2637
+ }
2638
+ /**
2639
+ * Get or set the directory for searching for executable subcommands of this command.
2640
+ *
2641
+ * @example
2642
+ * program.executableDir(__dirname);
2643
+ * // or
2644
+ * program.executableDir('subcommands');
2645
+ *
2646
+ * @param {string} [path]
2647
+ * @return {(string|null|Command)}
2648
+ */
2649
+ executableDir(path7) {
2650
+ if (path7 === void 0) return this._executableDir;
2651
+ this._executableDir = path7;
2652
+ return this;
2653
+ }
2654
+ /**
2655
+ * Return program help documentation.
2656
+ *
2657
+ * @param {{ error: boolean }} [contextOptions] - pass {error:true} to wrap for stderr instead of stdout
2658
+ * @return {string}
2659
+ */
2660
+ helpInformation(contextOptions) {
2661
+ const helper = this.createHelp();
2662
+ if (helper.helpWidth === void 0) {
2663
+ helper.helpWidth = contextOptions && contextOptions.error ? this._outputConfiguration.getErrHelpWidth() : this._outputConfiguration.getOutHelpWidth();
2664
+ }
2665
+ return helper.formatHelp(this, helper);
2666
+ }
2667
+ /**
2668
+ * @private
2669
+ */
2670
+ _getHelpContext(contextOptions) {
2671
+ contextOptions = contextOptions || {};
2672
+ const context2 = { error: !!contextOptions.error };
2673
+ let write;
2674
+ if (context2.error) {
2675
+ write = (arg) => this._outputConfiguration.writeErr(arg);
2676
+ } else {
2677
+ write = (arg) => this._outputConfiguration.writeOut(arg);
2678
+ }
2679
+ context2.write = contextOptions.write || write;
2680
+ context2.command = this;
2681
+ return context2;
2682
+ }
2683
+ /**
2684
+ * Output help information for this command.
2685
+ *
2686
+ * Outputs built-in help, and custom text added using `.addHelpText()`.
2687
+ *
2688
+ * @param {{ error: boolean } | Function} [contextOptions] - pass {error:true} to write to stderr instead of stdout
2689
+ */
2690
+ outputHelp(contextOptions) {
2691
+ let deprecatedCallback;
2692
+ if (typeof contextOptions === "function") {
2693
+ deprecatedCallback = contextOptions;
2694
+ contextOptions = void 0;
2695
+ }
2696
+ const context2 = this._getHelpContext(contextOptions);
2697
+ this._getCommandAndAncestors().reverse().forEach((command) => command.emit("beforeAllHelp", context2));
2698
+ this.emit("beforeHelp", context2);
2699
+ let helpInformation = this.helpInformation(context2);
2700
+ if (deprecatedCallback) {
2701
+ helpInformation = deprecatedCallback(helpInformation);
2702
+ if (typeof helpInformation !== "string" && !Buffer.isBuffer(helpInformation)) {
2703
+ throw new Error("outputHelp callback must return a string or a Buffer");
2704
+ }
2705
+ }
2706
+ context2.write(helpInformation);
2707
+ if (this._getHelpOption()?.long) {
2708
+ this.emit(this._getHelpOption().long);
2709
+ }
2710
+ this.emit("afterHelp", context2);
2711
+ this._getCommandAndAncestors().forEach((command) => command.emit("afterAllHelp", context2));
2712
+ }
2713
+ /**
2714
+ * You can pass in flags and a description to customise the built-in help option.
2715
+ * Pass in false to disable the built-in help option.
2716
+ *
2717
+ * @example
2718
+ * program.helpOption('-?, --help' 'show help'); // customise
2719
+ * program.helpOption(false); // disable
2720
+ *
2721
+ * @param {(string | boolean)} flags
2722
+ * @param {string} [description]
2723
+ * @return {Command} `this` command for chaining
2724
+ */
2725
+ helpOption(flags, description) {
2726
+ if (typeof flags === "boolean") {
2727
+ if (flags) {
2728
+ this._helpOption = this._helpOption ?? void 0;
2729
+ } else {
2730
+ this._helpOption = null;
2731
+ }
2732
+ return this;
2733
+ }
2734
+ flags = flags ?? "-h, --help";
2735
+ description = description ?? "display help for command";
2736
+ this._helpOption = this.createOption(flags, description);
2737
+ return this;
2738
+ }
2739
+ /**
2740
+ * Lazy create help option.
2741
+ * Returns null if has been disabled with .helpOption(false).
2742
+ *
2743
+ * @returns {(Option | null)} the help option
2744
+ * @package internal use only
2745
+ */
2746
+ _getHelpOption() {
2747
+ if (this._helpOption === void 0) {
2748
+ this.helpOption(void 0, void 0);
2749
+ }
2750
+ return this._helpOption;
2751
+ }
2752
+ /**
2753
+ * Supply your own option to use for the built-in help option.
2754
+ * This is an alternative to using helpOption() to customise the flags and description etc.
2755
+ *
2756
+ * @param {Option} option
2757
+ * @return {Command} `this` command for chaining
2758
+ */
2759
+ addHelpOption(option) {
2760
+ this._helpOption = option;
2761
+ return this;
2762
+ }
2763
+ /**
2764
+ * Output help information and exit.
2765
+ *
2766
+ * Outputs built-in help, and custom text added using `.addHelpText()`.
2767
+ *
2768
+ * @param {{ error: boolean }} [contextOptions] - pass {error:true} to write to stderr instead of stdout
2769
+ */
2770
+ help(contextOptions) {
2771
+ this.outputHelp(contextOptions);
2772
+ let exitCode = process2.exitCode || 0;
2773
+ if (exitCode === 0 && contextOptions && typeof contextOptions !== "function" && contextOptions.error) {
2774
+ exitCode = 1;
2775
+ }
2776
+ this._exit(exitCode, "commander.help", "(outputHelp)");
2777
+ }
2778
+ /**
2779
+ * Add additional text to be displayed with the built-in help.
2780
+ *
2781
+ * Position is 'before' or 'after' to affect just this command,
2782
+ * and 'beforeAll' or 'afterAll' to affect this command and all its subcommands.
2783
+ *
2784
+ * @param {string} position - before or after built-in help
2785
+ * @param {(string | Function)} text - string to add, or a function returning a string
2786
+ * @return {Command} `this` command for chaining
2787
+ */
2788
+ addHelpText(position, text) {
2789
+ const allowedValues = ["beforeAll", "before", "after", "afterAll"];
2790
+ if (!allowedValues.includes(position)) {
2791
+ throw new Error(`Unexpected value for position to addHelpText.
2792
+ Expecting one of '${allowedValues.join("', '")}'`);
2793
+ }
2794
+ const helpEvent = `${position}Help`;
2795
+ this.on(helpEvent, (context2) => {
2796
+ let helpStr;
2797
+ if (typeof text === "function") {
2798
+ helpStr = text({ error: context2.error, command: context2.command });
2799
+ } else {
2800
+ helpStr = text;
2801
+ }
2802
+ if (helpStr) {
2803
+ context2.write(`${helpStr}
2804
+ `);
2805
+ }
2806
+ });
2807
+ return this;
2808
+ }
2809
+ /**
2810
+ * Output help information if help flags specified
2811
+ *
2812
+ * @param {Array} args - array of options to search for help flags
2813
+ * @private
2814
+ */
2815
+ _outputHelpIfRequested(args) {
2816
+ const helpOption = this._getHelpOption();
2817
+ const helpRequested = helpOption && args.find((arg) => helpOption.is(arg));
2818
+ if (helpRequested) {
2819
+ this.outputHelp();
2820
+ this._exit(0, "commander.helpDisplayed", "(outputHelp)");
2821
+ }
2822
+ }
2823
+ };
2824
+ function incrementNodeInspectorPort(args) {
2825
+ return args.map((arg) => {
2826
+ if (!arg.startsWith("--inspect")) {
2827
+ return arg;
2828
+ }
2829
+ let debugOption;
2830
+ let debugHost = "127.0.0.1";
2831
+ let debugPort = "9229";
2832
+ let match;
2833
+ if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
2834
+ debugOption = match[1];
2835
+ } else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
2836
+ debugOption = match[1];
2837
+ if (/^\d+$/.test(match[3])) {
2838
+ debugPort = match[3];
2839
+ } else {
2840
+ debugHost = match[3];
2841
+ }
2842
+ } else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) {
2843
+ debugOption = match[1];
2844
+ debugHost = match[3];
2845
+ debugPort = match[4];
2846
+ }
2847
+ if (debugOption && debugPort !== "0") {
2848
+ return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
2849
+ }
2850
+ return arg;
2851
+ });
2852
+ }
2853
+ exports.Command = Command2;
2854
+ }
2855
+ });
2856
+
2857
+ // node_modules/commander/index.js
2858
+ var require_commander = __commonJS({
2859
+ "node_modules/commander/index.js"(exports) {
2860
+ var { Argument: Argument2 } = require_argument();
2861
+ var { Command: Command2 } = require_command();
2862
+ var { CommanderError: CommanderError2, InvalidArgumentError: InvalidArgumentError2 } = require_error();
2863
+ var { Help: Help2 } = require_help();
2864
+ var { Option: Option2 } = require_option();
2865
+ exports.program = new Command2();
2866
+ exports.createCommand = (name) => new Command2(name);
2867
+ exports.createOption = (flags, description) => new Option2(flags, description);
2868
+ exports.createArgument = (name, description) => new Argument2(name, description);
2869
+ exports.Command = Command2;
2870
+ exports.Option = Option2;
2871
+ exports.Argument = Argument2;
2872
+ exports.Help = Help2;
2873
+ exports.CommanderError = CommanderError2;
2874
+ exports.InvalidArgumentError = InvalidArgumentError2;
2875
+ exports.InvalidOptionArgumentError = InvalidArgumentError2;
2876
+ }
2877
+ });
2878
+
2879
+ // node_modules/commander/esm.mjs
2880
+ var import_index = __toESM(require_commander());
2881
+ var {
2882
+ program,
2883
+ createCommand,
2884
+ createArgument,
2885
+ createOption,
2886
+ CommanderError,
2887
+ InvalidArgumentError,
2888
+ InvalidOptionArgumentError,
2889
+ // deprecated old name
2890
+ Command,
2891
+ Argument,
2892
+ Option,
2893
+ Help
2894
+ } = import_index.default;
2895
+
2896
+ // package.json
2897
+ var package_default = {
2898
+ version: "5.0.0"};
2899
+ var ALWAYS_EXTERNAL = ["uWebSockets.js"];
15
2900
  function getUserExternals(cwd) {
2901
+ let pkgExternals = [];
16
2902
  try {
17
2903
  const pkgPath = path4.join(cwd, "package.json");
18
2904
  if (fs.existsSync(pkgPath)) {
19
2905
  const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
20
2906
  const deps = Object.keys(pkg.dependencies || {});
21
2907
  const devDeps = Object.keys(pkg.devDependencies || {});
22
- return [...deps, ...devDeps];
2908
+ pkgExternals = [...deps, ...devDeps];
23
2909
  }
24
2910
  } catch (err) {
2911
+ const message = err instanceof Error ? err.message : String(err);
25
2912
  console.warn(
26
- "[Axiomify CLI] Failed to read package.json, defaulting to empty externals."
2913
+ `[Axiomify CLI] Failed to read package.json (${message}); defaulting to empty externals.`
27
2914
  );
28
2915
  }
29
- return [];
2916
+ return Array.from(/* @__PURE__ */ new Set([...ALWAYS_EXTERNAL, ...pkgExternals]));
30
2917
  }
31
2918
 
32
2919
  // src/commands/build.ts
@@ -56,25 +2943,46 @@ async function devServer(entry) {
56
2943
  const entryPath = path4.resolve(process.cwd(), entry);
57
2944
  const outPath = path4.resolve(process.cwd(), ".axiomify/dev.js");
58
2945
  let child = null;
2946
+ let firstBuild = true;
2947
+ const startChild = () => {
2948
+ child = spawn("node", [outPath], { stdio: "inherit" });
2949
+ child.on("error", (err) => {
2950
+ console.error("\u274C Failed to start process:", err);
2951
+ });
2952
+ };
2953
+ const GRACEFUL_KILL_MS = 3e3;
59
2954
  const restartServer = () => {
60
- if (child) {
2955
+ if (child && child.exitCode === null && child.signalCode === null) {
61
2956
  child.removeAllListeners("exit");
62
- child.once("exit", () => {
63
- child = spawn("node", [outPath], { stdio: "inherit" });
2957
+ const oldChild = child;
2958
+ oldChild.once("exit", () => {
2959
+ startChild();
64
2960
  });
65
- child.kill("SIGKILL");
2961
+ oldChild.kill("SIGTERM");
2962
+ const forceKill = setTimeout(() => {
2963
+ if (oldChild.exitCode === null && oldChild.signalCode === null) {
2964
+ oldChild.kill("SIGKILL");
2965
+ }
2966
+ }, GRACEFUL_KILL_MS);
2967
+ forceKill.unref();
66
2968
  } else {
67
- child = spawn("node", [outPath], { stdio: "inherit" });
2969
+ startChild();
68
2970
  }
69
2971
  };
70
2972
  const watchPlugin = {
71
2973
  name: "watch-plugin",
72
2974
  setup(build3) {
73
2975
  build3.onEnd((result) => {
74
- if (result.errors.length > 0) {
75
- console.error("\u274C Build failed. Waiting for changes...");
2976
+ if (result.errors.length === 0) {
2977
+ if (firstBuild) {
2978
+ firstBuild = false;
2979
+ restartServer();
2980
+ } else {
2981
+ console.log("\u{1F504} Changes detected, restarting...");
2982
+ restartServer();
2983
+ }
76
2984
  } else {
77
- restartServer();
2985
+ console.error("\u274C Build failed. Fix errors to trigger a restart.");
78
2986
  }
79
2987
  });
80
2988
  }
@@ -84,6 +2992,7 @@ async function devServer(entry) {
84
2992
  entryPoints: [entryPath],
85
2993
  bundle: true,
86
2994
  platform: "node",
2995
+ format: "cjs",
87
2996
  outfile: outPath,
88
2997
  external: [.../* @__PURE__ */ new Set([...userExternals, "node:*"])],
89
2998
  plugins: [watchPlugin]
@@ -98,7 +3007,7 @@ async function devServer(entry) {
98
3007
  child.removeAllListeners("exit");
99
3008
  child.kill("SIGTERM");
100
3009
  setTimeout(() => {
101
- if (child && !child.killed) child.kill("SIGKILL");
3010
+ if (child && child.exitCode === null) child.kill("SIGKILL");
102
3011
  }, 2e3).unref();
103
3012
  }
104
3013
  try {
@@ -112,43 +3021,207 @@ async function devServer(entry) {
112
3021
  console.log(`\u{1F440} Axiomify Dev Engine watching for changes...`);
113
3022
  await ctx.watch();
114
3023
  }
3024
+ var DEV_COMMAND_BY_PM = {
3025
+ npm: "npm run dev",
3026
+ pnpm: "pnpm dev",
3027
+ yarn: "yarn dev"
3028
+ };
115
3029
  async function initProject(targetDir, options = {}) {
116
- const dir = path4.resolve(process.cwd(), targetDir);
117
- await fs2.mkdir(path4.join(dir, "src"), { recursive: true });
118
- const targets = [
119
- path4.join(dir, "package.json"),
120
- path4.join(dir, "tsconfig.json"),
121
- path4.join(dir, "src", "index.ts")
122
- ];
123
- const collisions = targets.filter((p) => existsSync(p));
124
- if (collisions.length > 0 && !options.force) {
3030
+ console.log(pc.cyan(pc.bold("\n\u{1F680} Axiomify Project Initializer\n")));
3031
+ const questions = [];
3032
+ if (!targetDir) {
3033
+ questions.push({
3034
+ type: "input",
3035
+ name: "projectName",
3036
+ message: "What is your project name?",
3037
+ initial: "my-axiomify-app"
3038
+ });
3039
+ }
3040
+ questions.push(
3041
+ {
3042
+ type: "select",
3043
+ name: "adapter",
3044
+ message: "Which HTTP adapter do you want to use?",
3045
+ choices: [
3046
+ "Native (uWS \u2014 Fastest, 50k+ req/s)",
3047
+ "Fastify (High-throughput, recommended)",
3048
+ "Express (Max ecosystem compatibility)",
3049
+ "Hapi (Enterprise, plugin-first)",
3050
+ "Node HTTP (Zero dependency)"
3051
+ ],
3052
+ initial: 0
3053
+ // Native is the recommended default
3054
+ },
3055
+ {
3056
+ type: "input",
3057
+ name: "description",
3058
+ message: "Project description",
3059
+ initial: "A production-ready Axiomify service"
3060
+ },
3061
+ {
3062
+ type: "confirm",
3063
+ name: "useEslint",
3064
+ message: "Add ESLint + Prettier + EditorConfig?",
3065
+ initial: true
3066
+ },
3067
+ {
3068
+ type: "select",
3069
+ name: "packageManager",
3070
+ message: "Preferred package manager?",
3071
+ choices: ["npm", "pnpm", "yarn"],
3072
+ initial: 0
3073
+ },
3074
+ {
3075
+ type: "confirm",
3076
+ name: "useGit",
3077
+ message: "Initialize a git repository?",
3078
+ initial: true
3079
+ },
3080
+ {
3081
+ type: "confirm",
3082
+ name: "installDeps",
3083
+ message: "Install dependencies automatically?",
3084
+ initial: true
3085
+ }
3086
+ );
3087
+ const answers = await prompt(questions);
3088
+ const projectName = targetDir || answers.projectName;
3089
+ if (!projectName || projectName.trim().length === 0) {
3090
+ console.error(
3091
+ pc.red(
3092
+ "\u274C A project name is required. Aborting to avoid writing into the current directory."
3093
+ )
3094
+ );
3095
+ process.exit(1);
3096
+ }
3097
+ if (
3098
+ // eslint-disable-next-line no-control-regex
3099
+ /[<>:"|?*\u0000-\u001f]/.test(projectName) || projectName.includes("..")
3100
+ ) {
125
3101
  console.error(
126
- "\u274C Refusing to overwrite existing files:\n" + collisions.map((p) => ` - ${p}`).join("\n") + "\n\nRe-run with '--force' if you really want to replace them."
3102
+ pc.red(
3103
+ `\u274C Invalid project name: "${projectName}". Names cannot contain path traversal or control characters.`
3104
+ )
127
3105
  );
128
3106
  process.exit(1);
129
3107
  }
3108
+ const dir = path4.resolve(process.cwd(), projectName);
3109
+ if (existsSync(dir) && !options.force && targetDir) {
3110
+ const targets = [
3111
+ path4.join(dir, "package.json"),
3112
+ path4.join(dir, "tsconfig.json"),
3113
+ path4.join(dir, "src", "index.ts")
3114
+ ];
3115
+ const collisions = targets.filter((p) => existsSync(p));
3116
+ if (collisions.length > 0) {
3117
+ console.error(
3118
+ pc.red("\u274C Refusing to overwrite existing files:\n") + collisions.map((p) => ` - ${p}`).join("\n") + pc.yellow(
3119
+ "\n\nRe-run with '--force' if you really want to replace them."
3120
+ )
3121
+ );
3122
+ process.exit(1);
3123
+ }
3124
+ }
3125
+ await fs2.mkdir(path4.join(dir, "src"), { recursive: true });
3126
+ const AXIOMIFY_VERSION = `^${package_default.version}`;
3127
+ let adapterPackage = "@axiomify/native";
3128
+ let adapterImport = "import { NativeAdapter } from '@axiomify/native';";
3129
+ let adapterInit = "const server = new NativeAdapter(app, { port: 3000 });\n server.listen(() => console.log(' Axiomify Native on :3000'));";
3130
+ if (answers.adapter.includes("Fastify")) {
3131
+ adapterPackage = "@axiomify/fastify";
3132
+ adapterImport = "import { FastifyAdapter } from '@axiomify/fastify';";
3133
+ adapterInit = "const server = new FastifyAdapter(app);\n await server.listen(3000);\n console.log(' Axiomify Fastify on :3000');";
3134
+ } else if (answers.adapter.includes("Express")) {
3135
+ adapterPackage = "@axiomify/express";
3136
+ adapterImport = "import { ExpressAdapter } from '@axiomify/express';";
3137
+ adapterInit = "const server = new ExpressAdapter(app);\n server.listen(3000, () => console.log(' Axiomify Express on :3000'));";
3138
+ } else if (answers.adapter.includes("Hapi")) {
3139
+ adapterPackage = "@axiomify/hapi";
3140
+ adapterImport = "import { HapiAdapter } from '@axiomify/hapi';";
3141
+ adapterInit = "const server = new HapiAdapter(app);\n await server.listen(3000);\n console.log(' Axiomify Hapi on :3000');";
3142
+ } else if (answers.adapter.includes("HTTP")) {
3143
+ adapterPackage = "@axiomify/http";
3144
+ adapterImport = "import { HttpAdapter } from '@axiomify/http';";
3145
+ adapterInit = "const server = new HttpAdapter(app);\n server.listen(3000, () => console.log(' Axiomify HTTP on :3000'));";
3146
+ }
130
3147
  const pkgJson = {
131
- name: "axiomify-app",
3148
+ name: projectName,
132
3149
  version: "1.0.0",
133
3150
  private: true,
3151
+ description: answers.description,
134
3152
  scripts: {
135
3153
  dev: "axiomify dev src/index.ts",
136
3154
  build: "axiomify build src/index.ts",
137
3155
  start: "node dist/index.js",
138
- routes: "axiomify routes src/index.ts"
3156
+ routes: "axiomify routes src/index.ts",
3157
+ typecheck: "tsc --noEmit"
139
3158
  },
140
3159
  dependencies: {
141
- "@axiomify/core": "latest",
142
- "@axiomify/express": "latest"
3160
+ "@axiomify/core": AXIOMIFY_VERSION,
3161
+ [adapterPackage]: AXIOMIFY_VERSION,
3162
+ // Inject selected adapter
3163
+ "@axiomify/helmet": AXIOMIFY_VERSION,
3164
+ "@axiomify/cors": AXIOMIFY_VERSION,
3165
+ "@axiomify/logger": AXIOMIFY_VERSION,
3166
+ "@axiomify/security": AXIOMIFY_VERSION,
3167
+ "@axiomify/rate-limit": AXIOMIFY_VERSION,
3168
+ "@axiomify/fingerprint": AXIOMIFY_VERSION
143
3169
  },
144
3170
  devDependencies: {
145
- // Keep scaffolded projects on the same TS major as the workspace
146
- // (^6) so types like `satisfies`, `const` type parameters, etc., don't
147
- // drift between the framework and user code.
148
- typescript: "^6.0.0",
149
- "@types/node": "^22.0.0"
3171
+ typescript: "^5.4.0",
3172
+ "@types/node": "^22.0.0",
3173
+ "@axiomify/cli": AXIOMIFY_VERSION
150
3174
  }
151
3175
  };
3176
+ if (answers.useEslint) {
3177
+ pkgJson.devDependencies = {
3178
+ ...pkgJson.devDependencies,
3179
+ eslint: "^8.57.0",
3180
+ prettier: "^3.0.0",
3181
+ "eslint-config-prettier": "^9.0.0",
3182
+ "eslint-plugin-prettier": "^5.0.0",
3183
+ "@typescript-eslint/eslint-plugin": "^7.0.0",
3184
+ "@typescript-eslint/parser": "^7.0.0"
3185
+ };
3186
+ pkgJson.scripts.lint = "eslint . --ext .ts";
3187
+ pkgJson.scripts["lint:fix"] = "eslint . --ext .ts --fix";
3188
+ pkgJson.scripts.format = "prettier --write .";
3189
+ const eslintConfig = `module.exports = {
3190
+ root: true,
3191
+ parser: '@typescript-eslint/parser',
3192
+ plugins: ['@typescript-eslint', 'prettier'],
3193
+ extends: [
3194
+ 'eslint:recommended',
3195
+ 'plugin:@typescript-eslint/recommended',
3196
+ 'plugin:prettier/recommended',
3197
+ ],
3198
+ parserOptions: {
3199
+ ecmaVersion: 2022,
3200
+ sourceType: 'module',
3201
+ },
3202
+ env: {
3203
+ node: true,
3204
+ es2022: true,
3205
+ },
3206
+ };`;
3207
+ const prettierConfig = `{
3208
+ "semi": true,
3209
+ "trailingComma": "all",
3210
+ "singleQuote": true,
3211
+ "printWidth": 100,
3212
+ "tabWidth": 2
3213
+ }`;
3214
+ await fs2.writeFile(path4.join(dir, ".eslintrc.cjs"), eslintConfig);
3215
+ await fs2.writeFile(path4.join(dir, ".prettierrc"), prettierConfig);
3216
+ await fs2.writeFile(
3217
+ path4.join(dir, ".prettierignore"),
3218
+ "dist\nnode_modules\ncoverage\n"
3219
+ );
3220
+ await fs2.writeFile(
3221
+ path4.join(dir, ".editorconfig"),
3222
+ "root = true\n\n[*]\ncharset = utf-8\nend_of_line = lf\nindent_style = space\nindent_size = 2\ninsert_final_newline = true\ntrim_trailing_whitespace = true\n"
3223
+ );
3224
+ }
152
3225
  const tsConfig = {
153
3226
  compilerOptions: {
154
3227
  target: "ES2022",
@@ -158,30 +3231,54 @@ async function initProject(targetDir, options = {}) {
158
3231
  esModuleInterop: true,
159
3232
  skipLibCheck: true,
160
3233
  forceConsistentCasingInFileNames: true,
161
- outDir: "./dist"
3234
+ outDir: "./dist",
3235
+ rootDir: "./src",
3236
+ types: ["node"]
162
3237
  },
163
3238
  include: ["src/**/*"]
164
3239
  };
165
- const indexTs = `import { Axiomify, z } from '@axiomify/core';
166
- import { ExpressAdapter } from '@axiomify/express';
3240
+ const indexTs = `import { Axiomify } from '@axiomify/core';
3241
+ ${adapterImport}
3242
+ import { useHelmet } from '@axiomify/helmet';
3243
+ import { useCors } from '@axiomify/cors';
3244
+ import { useLogger } from '@axiomify/logger';
3245
+ import { useSecurity } from '@axiomify/security';
3246
+ import { useRateLimit, MemoryStore } from '@axiomify/rate-limit';
3247
+ import { useFingerprint } from '@axiomify/fingerprint';
167
3248
 
168
- // Exporting the app instance is required for the 'axiomify routes' CLI command
169
- export const app = new Axiomify();
3249
+ export const app = new Axiomify();
170
3250
 
171
- app.route({
172
- method: 'GET',
173
- path: '/health',
174
- handler: async (req, res) => {
175
- res.status(200).send({ status: 'healthy', timestamp: Date.now() }, 'System Operational');
176
- }
177
- });
3251
+ useHelmet(app);
3252
+ useCors(app, { credentials: false });
3253
+ useSecurity(app);
178
3254
 
179
- // Prevent listening during CLI inspection
180
- if (require.main === module) {
181
- const adapter = new ExpressAdapter(app);
182
- adapter.listen(3000, () => console.log('\u{1F680} Axiomify engine online on port 3000'));
183
- }
184
- `;
3255
+ // PRODUCTION NOTE
3256
+ // MemoryStore is per-process. For production, swap this for a RedisStore.
3257
+ useRateLimit(app, { max: 100, windowMs: 60_000, store: new MemoryStore() });
3258
+ useFingerprint(app);
3259
+ useLogger(app);
3260
+
3261
+ app.route({
3262
+ method: 'GET',
3263
+ path: '/health',
3264
+ handler: async (_req, res) => {
3265
+ res.status(200).send({ status: 'healthy' }, 'System Operational');
3266
+ },
3267
+ });
3268
+
3269
+ if (require.main === module) {
3270
+ ${adapterInit}
3271
+ }
3272
+ `;
3273
+ const gitignore = [
3274
+ "node_modules",
3275
+ "dist",
3276
+ ".axiomify",
3277
+ ".env",
3278
+ ".env.local",
3279
+ "coverage",
3280
+ "*.log"
3281
+ ].join("\n") + "\n";
185
3282
  await fs2.writeFile(
186
3283
  path4.join(dir, "package.json"),
187
3284
  JSON.stringify(pkgJson, null, 2)
@@ -191,8 +3288,42 @@ if (require.main === module) {
191
3288
  JSON.stringify(tsConfig, null, 2)
192
3289
  );
193
3290
  await fs2.writeFile(path4.join(dir, "src", "index.ts"), indexTs);
194
- console.log(`\u2705 Axiomify project initialized in ${dir}`);
195
- console.log(`\u{1F4E6} Run 'npm install' to install dependencies.`);
3291
+ await fs2.writeFile(path4.join(dir, ".gitignore"), gitignore);
3292
+ console.log(pc.green(`
3293
+ \u2705 Axiomify project initialized in ${pc.bold(dir)}`));
3294
+ if (answers.useGit && !existsSync(path4.join(dir, ".git"))) {
3295
+ try {
3296
+ await execa("git", ["init"], { cwd: dir });
3297
+ console.log(pc.green("\u2705 Git repository initialized"));
3298
+ } catch {
3299
+ console.log(pc.yellow("\u26A0\uFE0F Could not initialize git repository."));
3300
+ }
3301
+ }
3302
+ if (answers.installDeps) {
3303
+ const pkgManager = answers.packageManager || "npm";
3304
+ const installArgs = ["install"];
3305
+ console.log(pc.cyan(`\u{1F4E6} Installing dependencies using ${pkgManager}...`));
3306
+ try {
3307
+ await execa(pkgManager, installArgs, { cwd: dir, stdio: "inherit" });
3308
+ console.log(pc.green("\u2705 Dependencies installed successfully!"));
3309
+ } catch {
3310
+ console.error(
3311
+ pc.red("\u274C Failed to install dependencies. Please install manually.")
3312
+ );
3313
+ }
3314
+ } else {
3315
+ console.log(
3316
+ pc.yellow(
3317
+ `
3318
+ \u{1F4E6} Run "cd ${projectName} && ${answers.packageManager || "npm"} install" to get started.`
3319
+ )
3320
+ );
3321
+ }
3322
+ const devCommand = DEV_COMMAND_BY_PM[answers.packageManager || "npm"];
3323
+ console.log(
3324
+ pc.cyan(`
3325
+ \u{1F525} Run "${devCommand}" to start your development server!`)
3326
+ );
196
3327
  }
197
3328
  async function inspectRoutes(entry) {
198
3329
  const entryPath = path4.resolve(process.cwd(), entry);
@@ -211,6 +3342,12 @@ async function inspectRoutes(entry) {
211
3342
  delete __require.cache[__require.resolve(tempPath)];
212
3343
  } catch (e) {
213
3344
  }
3345
+ const inspectionTimeout = setTimeout(() => {
3346
+ console.warn(
3347
+ "\n\u26A0\uFE0F Route inspection is taking longer than expected.\n Your entry file may be starting a server unconditionally.\n Wrap the listen() call in `if (require.main === module) { ... }`\n so it only runs when the file is executed directly.\n"
3348
+ );
3349
+ }, 5e3);
3350
+ inspectionTimeout.unref();
214
3351
  const mod = __require(tempPath);
215
3352
  const app = mod.app || mod.default;
216
3353
  if (!app || typeof app.registeredRoutes === "undefined") {
@@ -248,10 +3385,12 @@ async function inspectRoutes(entry) {
248
3385
  }
249
3386
 
250
3387
  // src/index.ts
251
- var program = new Command();
252
- program.name("axiomify").description("The official CLI for the Axiomify framework").version("1.0.0");
253
- program.command("init").description("Bootstrap a new Axiomify project").argument("[directory]", "Target directory", ".").option("-f, --force", "Overwrite existing project files", false).action((directory, options) => initProject(directory, options));
254
- program.command("dev").description("Start the development server with hot-reload").argument("[entry]", "Entry file", "src/index.ts").action(devServer);
255
- program.command("build").description("Compile the application for production").argument("[entry]", "Entry file", "src/index.ts").action(buildProject);
256
- program.command("routes").description("Inspect and list all registered routes in the application").argument("[entry]", "Entry file", "src/index.ts").action(inspectRoutes);
257
- program.parse(process.argv);
3388
+ var program2 = new Command();
3389
+ program2.name("axiomify").description("The official CLI for the Axiomify framework").version(package_default.version);
3390
+ program2.command("init").description("Bootstrap a new Axiomify project").argument("[directory]", "Target directory").option("-f, --force", "Overwrite existing project files", false).action(
3391
+ (directory, options) => initProject(directory, options)
3392
+ );
3393
+ program2.command("dev").description("Start the development server with hot-reload").argument("[entry]", "Entry file", "src/index.ts").action(devServer);
3394
+ program2.command("build").description("Compile the application for production").argument("[entry]", "Entry file", "src/index.ts").action(buildProject);
3395
+ program2.command("routes").description("Inspect and list all registered routes in the application").argument("[entry]", "Entry file", "src/index.ts").action(inspectRoutes);
3396
+ program2.parse(process.argv);