@hasna/uptime 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3945 @@
1
+ #!/usr/bin/env bun
2
+ // @bun
3
+ var __create = Object.create;
4
+ var __getProtoOf = Object.getPrototypeOf;
5
+ var __defProp = Object.defineProperty;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ function __accessProp(key) {
9
+ return this[key];
10
+ }
11
+ var __toESMCache_node;
12
+ var __toESMCache_esm;
13
+ var __toESM = (mod, isNodeMode, target) => {
14
+ var canCache = mod != null && typeof mod === "object";
15
+ if (canCache) {
16
+ var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
17
+ var cached = cache.get(mod);
18
+ if (cached)
19
+ return cached;
20
+ }
21
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
22
+ const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
23
+ for (let key of __getOwnPropNames(mod))
24
+ if (!__hasOwnProp.call(to, key))
25
+ __defProp(to, key, {
26
+ get: __accessProp.bind(mod, key),
27
+ enumerable: true
28
+ });
29
+ if (canCache)
30
+ cache.set(mod, to);
31
+ return to;
32
+ };
33
+ var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
34
+ var __require = import.meta.require;
35
+
36
+ // node_modules/commander/lib/error.js
37
+ var require_error = __commonJS((exports) => {
38
+ class CommanderError extends Error {
39
+ constructor(exitCode, code, message) {
40
+ super(message);
41
+ Error.captureStackTrace(this, this.constructor);
42
+ this.name = this.constructor.name;
43
+ this.code = code;
44
+ this.exitCode = exitCode;
45
+ this.nestedError = undefined;
46
+ }
47
+ }
48
+
49
+ class InvalidArgumentError extends CommanderError {
50
+ constructor(message) {
51
+ super(1, "commander.invalidArgument", message);
52
+ Error.captureStackTrace(this, this.constructor);
53
+ this.name = this.constructor.name;
54
+ }
55
+ }
56
+ exports.CommanderError = CommanderError;
57
+ exports.InvalidArgumentError = InvalidArgumentError;
58
+ });
59
+
60
+ // node_modules/commander/lib/argument.js
61
+ var require_argument = __commonJS((exports) => {
62
+ var { InvalidArgumentError } = require_error();
63
+
64
+ class Argument {
65
+ constructor(name, description) {
66
+ this.description = description || "";
67
+ this.variadic = false;
68
+ this.parseArg = undefined;
69
+ this.defaultValue = undefined;
70
+ this.defaultValueDescription = undefined;
71
+ this.argChoices = undefined;
72
+ switch (name[0]) {
73
+ case "<":
74
+ this.required = true;
75
+ this._name = name.slice(1, -1);
76
+ break;
77
+ case "[":
78
+ this.required = false;
79
+ this._name = name.slice(1, -1);
80
+ break;
81
+ default:
82
+ this.required = true;
83
+ this._name = name;
84
+ break;
85
+ }
86
+ if (this._name.length > 3 && this._name.slice(-3) === "...") {
87
+ this.variadic = true;
88
+ this._name = this._name.slice(0, -3);
89
+ }
90
+ }
91
+ name() {
92
+ return this._name;
93
+ }
94
+ _concatValue(value, previous) {
95
+ if (previous === this.defaultValue || !Array.isArray(previous)) {
96
+ return [value];
97
+ }
98
+ return previous.concat(value);
99
+ }
100
+ default(value, description) {
101
+ this.defaultValue = value;
102
+ this.defaultValueDescription = description;
103
+ return this;
104
+ }
105
+ argParser(fn) {
106
+ this.parseArg = fn;
107
+ return this;
108
+ }
109
+ choices(values) {
110
+ this.argChoices = values.slice();
111
+ this.parseArg = (arg, previous) => {
112
+ if (!this.argChoices.includes(arg)) {
113
+ throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
114
+ }
115
+ if (this.variadic) {
116
+ return this._concatValue(arg, previous);
117
+ }
118
+ return arg;
119
+ };
120
+ return this;
121
+ }
122
+ argRequired() {
123
+ this.required = true;
124
+ return this;
125
+ }
126
+ argOptional() {
127
+ this.required = false;
128
+ return this;
129
+ }
130
+ }
131
+ function humanReadableArgName(arg) {
132
+ const nameOutput = arg.name() + (arg.variadic === true ? "..." : "");
133
+ return arg.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
134
+ }
135
+ exports.Argument = Argument;
136
+ exports.humanReadableArgName = humanReadableArgName;
137
+ });
138
+
139
+ // node_modules/commander/lib/help.js
140
+ var require_help = __commonJS((exports) => {
141
+ var { humanReadableArgName } = require_argument();
142
+
143
+ class Help {
144
+ constructor() {
145
+ this.helpWidth = undefined;
146
+ this.minWidthToWrap = 40;
147
+ this.sortSubcommands = false;
148
+ this.sortOptions = false;
149
+ this.showGlobalOptions = false;
150
+ }
151
+ prepareContext(contextOptions) {
152
+ this.helpWidth = this.helpWidth ?? contextOptions.helpWidth ?? 80;
153
+ }
154
+ visibleCommands(cmd) {
155
+ const visibleCommands = cmd.commands.filter((cmd2) => !cmd2._hidden);
156
+ const helpCommand = cmd._getHelpCommand();
157
+ if (helpCommand && !helpCommand._hidden) {
158
+ visibleCommands.push(helpCommand);
159
+ }
160
+ if (this.sortSubcommands) {
161
+ visibleCommands.sort((a, b) => {
162
+ return a.name().localeCompare(b.name());
163
+ });
164
+ }
165
+ return visibleCommands;
166
+ }
167
+ compareOptions(a, b) {
168
+ const getSortKey = (option) => {
169
+ return option.short ? option.short.replace(/^-/, "") : option.long.replace(/^--/, "");
170
+ };
171
+ return getSortKey(a).localeCompare(getSortKey(b));
172
+ }
173
+ visibleOptions(cmd) {
174
+ const visibleOptions = cmd.options.filter((option) => !option.hidden);
175
+ const helpOption = cmd._getHelpOption();
176
+ if (helpOption && !helpOption.hidden) {
177
+ const removeShort = helpOption.short && cmd._findOption(helpOption.short);
178
+ const removeLong = helpOption.long && cmd._findOption(helpOption.long);
179
+ if (!removeShort && !removeLong) {
180
+ visibleOptions.push(helpOption);
181
+ } else if (helpOption.long && !removeLong) {
182
+ visibleOptions.push(cmd.createOption(helpOption.long, helpOption.description));
183
+ } else if (helpOption.short && !removeShort) {
184
+ visibleOptions.push(cmd.createOption(helpOption.short, helpOption.description));
185
+ }
186
+ }
187
+ if (this.sortOptions) {
188
+ visibleOptions.sort(this.compareOptions);
189
+ }
190
+ return visibleOptions;
191
+ }
192
+ visibleGlobalOptions(cmd) {
193
+ if (!this.showGlobalOptions)
194
+ return [];
195
+ const globalOptions = [];
196
+ for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
197
+ const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
198
+ globalOptions.push(...visibleOptions);
199
+ }
200
+ if (this.sortOptions) {
201
+ globalOptions.sort(this.compareOptions);
202
+ }
203
+ return globalOptions;
204
+ }
205
+ visibleArguments(cmd) {
206
+ if (cmd._argsDescription) {
207
+ cmd.registeredArguments.forEach((argument) => {
208
+ argument.description = argument.description || cmd._argsDescription[argument.name()] || "";
209
+ });
210
+ }
211
+ if (cmd.registeredArguments.find((argument) => argument.description)) {
212
+ return cmd.registeredArguments;
213
+ }
214
+ return [];
215
+ }
216
+ subcommandTerm(cmd) {
217
+ const args = cmd.registeredArguments.map((arg) => humanReadableArgName(arg)).join(" ");
218
+ return cmd._name + (cmd._aliases[0] ? "|" + cmd._aliases[0] : "") + (cmd.options.length ? " [options]" : "") + (args ? " " + args : "");
219
+ }
220
+ optionTerm(option) {
221
+ return option.flags;
222
+ }
223
+ argumentTerm(argument) {
224
+ return argument.name();
225
+ }
226
+ longestSubcommandTermLength(cmd, helper) {
227
+ return helper.visibleCommands(cmd).reduce((max, command) => {
228
+ return Math.max(max, this.displayWidth(helper.styleSubcommandTerm(helper.subcommandTerm(command))));
229
+ }, 0);
230
+ }
231
+ longestOptionTermLength(cmd, helper) {
232
+ return helper.visibleOptions(cmd).reduce((max, option) => {
233
+ return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
234
+ }, 0);
235
+ }
236
+ longestGlobalOptionTermLength(cmd, helper) {
237
+ return helper.visibleGlobalOptions(cmd).reduce((max, option) => {
238
+ return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
239
+ }, 0);
240
+ }
241
+ longestArgumentTermLength(cmd, helper) {
242
+ return helper.visibleArguments(cmd).reduce((max, argument) => {
243
+ return Math.max(max, this.displayWidth(helper.styleArgumentTerm(helper.argumentTerm(argument))));
244
+ }, 0);
245
+ }
246
+ commandUsage(cmd) {
247
+ let cmdName = cmd._name;
248
+ if (cmd._aliases[0]) {
249
+ cmdName = cmdName + "|" + cmd._aliases[0];
250
+ }
251
+ let ancestorCmdNames = "";
252
+ for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
253
+ ancestorCmdNames = ancestorCmd.name() + " " + ancestorCmdNames;
254
+ }
255
+ return ancestorCmdNames + cmdName + " " + cmd.usage();
256
+ }
257
+ commandDescription(cmd) {
258
+ return cmd.description();
259
+ }
260
+ subcommandDescription(cmd) {
261
+ return cmd.summary() || cmd.description();
262
+ }
263
+ optionDescription(option) {
264
+ const extraInfo = [];
265
+ if (option.argChoices) {
266
+ extraInfo.push(`choices: ${option.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
267
+ }
268
+ if (option.defaultValue !== undefined) {
269
+ const showDefault = option.required || option.optional || option.isBoolean() && typeof option.defaultValue === "boolean";
270
+ if (showDefault) {
271
+ extraInfo.push(`default: ${option.defaultValueDescription || JSON.stringify(option.defaultValue)}`);
272
+ }
273
+ }
274
+ if (option.presetArg !== undefined && option.optional) {
275
+ extraInfo.push(`preset: ${JSON.stringify(option.presetArg)}`);
276
+ }
277
+ if (option.envVar !== undefined) {
278
+ extraInfo.push(`env: ${option.envVar}`);
279
+ }
280
+ if (extraInfo.length > 0) {
281
+ return `${option.description} (${extraInfo.join(", ")})`;
282
+ }
283
+ return option.description;
284
+ }
285
+ argumentDescription(argument) {
286
+ const extraInfo = [];
287
+ if (argument.argChoices) {
288
+ extraInfo.push(`choices: ${argument.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
289
+ }
290
+ if (argument.defaultValue !== undefined) {
291
+ extraInfo.push(`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`);
292
+ }
293
+ if (extraInfo.length > 0) {
294
+ const extraDescription = `(${extraInfo.join(", ")})`;
295
+ if (argument.description) {
296
+ return `${argument.description} ${extraDescription}`;
297
+ }
298
+ return extraDescription;
299
+ }
300
+ return argument.description;
301
+ }
302
+ formatHelp(cmd, helper) {
303
+ const termWidth = helper.padWidth(cmd, helper);
304
+ const helpWidth = helper.helpWidth ?? 80;
305
+ function callFormatItem(term, description) {
306
+ return helper.formatItem(term, termWidth, description, helper);
307
+ }
308
+ let output = [
309
+ `${helper.styleTitle("Usage:")} ${helper.styleUsage(helper.commandUsage(cmd))}`,
310
+ ""
311
+ ];
312
+ const commandDescription = helper.commandDescription(cmd);
313
+ if (commandDescription.length > 0) {
314
+ output = output.concat([
315
+ helper.boxWrap(helper.styleCommandDescription(commandDescription), helpWidth),
316
+ ""
317
+ ]);
318
+ }
319
+ const argumentList = helper.visibleArguments(cmd).map((argument) => {
320
+ return callFormatItem(helper.styleArgumentTerm(helper.argumentTerm(argument)), helper.styleArgumentDescription(helper.argumentDescription(argument)));
321
+ });
322
+ if (argumentList.length > 0) {
323
+ output = output.concat([
324
+ helper.styleTitle("Arguments:"),
325
+ ...argumentList,
326
+ ""
327
+ ]);
328
+ }
329
+ const optionList = helper.visibleOptions(cmd).map((option) => {
330
+ return callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option)));
331
+ });
332
+ if (optionList.length > 0) {
333
+ output = output.concat([
334
+ helper.styleTitle("Options:"),
335
+ ...optionList,
336
+ ""
337
+ ]);
338
+ }
339
+ if (helper.showGlobalOptions) {
340
+ const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
341
+ return callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option)));
342
+ });
343
+ if (globalOptionList.length > 0) {
344
+ output = output.concat([
345
+ helper.styleTitle("Global Options:"),
346
+ ...globalOptionList,
347
+ ""
348
+ ]);
349
+ }
350
+ }
351
+ const commandList = helper.visibleCommands(cmd).map((cmd2) => {
352
+ return callFormatItem(helper.styleSubcommandTerm(helper.subcommandTerm(cmd2)), helper.styleSubcommandDescription(helper.subcommandDescription(cmd2)));
353
+ });
354
+ if (commandList.length > 0) {
355
+ output = output.concat([
356
+ helper.styleTitle("Commands:"),
357
+ ...commandList,
358
+ ""
359
+ ]);
360
+ }
361
+ return output.join(`
362
+ `);
363
+ }
364
+ displayWidth(str) {
365
+ return stripColor(str).length;
366
+ }
367
+ styleTitle(str) {
368
+ return str;
369
+ }
370
+ styleUsage(str) {
371
+ return str.split(" ").map((word) => {
372
+ if (word === "[options]")
373
+ return this.styleOptionText(word);
374
+ if (word === "[command]")
375
+ return this.styleSubcommandText(word);
376
+ if (word[0] === "[" || word[0] === "<")
377
+ return this.styleArgumentText(word);
378
+ return this.styleCommandText(word);
379
+ }).join(" ");
380
+ }
381
+ styleCommandDescription(str) {
382
+ return this.styleDescriptionText(str);
383
+ }
384
+ styleOptionDescription(str) {
385
+ return this.styleDescriptionText(str);
386
+ }
387
+ styleSubcommandDescription(str) {
388
+ return this.styleDescriptionText(str);
389
+ }
390
+ styleArgumentDescription(str) {
391
+ return this.styleDescriptionText(str);
392
+ }
393
+ styleDescriptionText(str) {
394
+ return str;
395
+ }
396
+ styleOptionTerm(str) {
397
+ return this.styleOptionText(str);
398
+ }
399
+ styleSubcommandTerm(str) {
400
+ return str.split(" ").map((word) => {
401
+ if (word === "[options]")
402
+ return this.styleOptionText(word);
403
+ if (word[0] === "[" || word[0] === "<")
404
+ return this.styleArgumentText(word);
405
+ return this.styleSubcommandText(word);
406
+ }).join(" ");
407
+ }
408
+ styleArgumentTerm(str) {
409
+ return this.styleArgumentText(str);
410
+ }
411
+ styleOptionText(str) {
412
+ return str;
413
+ }
414
+ styleArgumentText(str) {
415
+ return str;
416
+ }
417
+ styleSubcommandText(str) {
418
+ return str;
419
+ }
420
+ styleCommandText(str) {
421
+ return str;
422
+ }
423
+ padWidth(cmd, helper) {
424
+ return Math.max(helper.longestOptionTermLength(cmd, helper), helper.longestGlobalOptionTermLength(cmd, helper), helper.longestSubcommandTermLength(cmd, helper), helper.longestArgumentTermLength(cmd, helper));
425
+ }
426
+ preformatted(str) {
427
+ return /\n[^\S\r\n]/.test(str);
428
+ }
429
+ formatItem(term, termWidth, description, helper) {
430
+ const itemIndent = 2;
431
+ const itemIndentStr = " ".repeat(itemIndent);
432
+ if (!description)
433
+ return itemIndentStr + term;
434
+ const paddedTerm = term.padEnd(termWidth + term.length - helper.displayWidth(term));
435
+ const spacerWidth = 2;
436
+ const helpWidth = this.helpWidth ?? 80;
437
+ const remainingWidth = helpWidth - termWidth - spacerWidth - itemIndent;
438
+ let formattedDescription;
439
+ if (remainingWidth < this.minWidthToWrap || helper.preformatted(description)) {
440
+ formattedDescription = description;
441
+ } else {
442
+ const wrappedDescription = helper.boxWrap(description, remainingWidth);
443
+ formattedDescription = wrappedDescription.replace(/\n/g, `
444
+ ` + " ".repeat(termWidth + spacerWidth));
445
+ }
446
+ return itemIndentStr + paddedTerm + " ".repeat(spacerWidth) + formattedDescription.replace(/\n/g, `
447
+ ${itemIndentStr}`);
448
+ }
449
+ boxWrap(str, width) {
450
+ if (width < this.minWidthToWrap)
451
+ return str;
452
+ const rawLines = str.split(/\r\n|\n/);
453
+ const chunkPattern = /[\s]*[^\s]+/g;
454
+ const wrappedLines = [];
455
+ rawLines.forEach((line) => {
456
+ const chunks = line.match(chunkPattern);
457
+ if (chunks === null) {
458
+ wrappedLines.push("");
459
+ return;
460
+ }
461
+ let sumChunks = [chunks.shift()];
462
+ let sumWidth = this.displayWidth(sumChunks[0]);
463
+ chunks.forEach((chunk) => {
464
+ const visibleWidth = this.displayWidth(chunk);
465
+ if (sumWidth + visibleWidth <= width) {
466
+ sumChunks.push(chunk);
467
+ sumWidth += visibleWidth;
468
+ return;
469
+ }
470
+ wrappedLines.push(sumChunks.join(""));
471
+ const nextChunk = chunk.trimStart();
472
+ sumChunks = [nextChunk];
473
+ sumWidth = this.displayWidth(nextChunk);
474
+ });
475
+ wrappedLines.push(sumChunks.join(""));
476
+ });
477
+ return wrappedLines.join(`
478
+ `);
479
+ }
480
+ }
481
+ function stripColor(str) {
482
+ const sgrPattern = /\x1b\[\d*(;\d*)*m/g;
483
+ return str.replace(sgrPattern, "");
484
+ }
485
+ exports.Help = Help;
486
+ exports.stripColor = stripColor;
487
+ });
488
+
489
+ // node_modules/commander/lib/option.js
490
+ var require_option = __commonJS((exports) => {
491
+ var { InvalidArgumentError } = require_error();
492
+
493
+ class Option {
494
+ constructor(flags, description) {
495
+ this.flags = flags;
496
+ this.description = description || "";
497
+ this.required = flags.includes("<");
498
+ this.optional = flags.includes("[");
499
+ this.variadic = /\w\.\.\.[>\]]$/.test(flags);
500
+ this.mandatory = false;
501
+ const optionFlags = splitOptionFlags(flags);
502
+ this.short = optionFlags.shortFlag;
503
+ this.long = optionFlags.longFlag;
504
+ this.negate = false;
505
+ if (this.long) {
506
+ this.negate = this.long.startsWith("--no-");
507
+ }
508
+ this.defaultValue = undefined;
509
+ this.defaultValueDescription = undefined;
510
+ this.presetArg = undefined;
511
+ this.envVar = undefined;
512
+ this.parseArg = undefined;
513
+ this.hidden = false;
514
+ this.argChoices = undefined;
515
+ this.conflictsWith = [];
516
+ this.implied = undefined;
517
+ }
518
+ default(value, description) {
519
+ this.defaultValue = value;
520
+ this.defaultValueDescription = description;
521
+ return this;
522
+ }
523
+ preset(arg) {
524
+ this.presetArg = arg;
525
+ return this;
526
+ }
527
+ conflicts(names) {
528
+ this.conflictsWith = this.conflictsWith.concat(names);
529
+ return this;
530
+ }
531
+ implies(impliedOptionValues) {
532
+ let newImplied = impliedOptionValues;
533
+ if (typeof impliedOptionValues === "string") {
534
+ newImplied = { [impliedOptionValues]: true };
535
+ }
536
+ this.implied = Object.assign(this.implied || {}, newImplied);
537
+ return this;
538
+ }
539
+ env(name) {
540
+ this.envVar = name;
541
+ return this;
542
+ }
543
+ argParser(fn) {
544
+ this.parseArg = fn;
545
+ return this;
546
+ }
547
+ makeOptionMandatory(mandatory = true) {
548
+ this.mandatory = !!mandatory;
549
+ return this;
550
+ }
551
+ hideHelp(hide = true) {
552
+ this.hidden = !!hide;
553
+ return this;
554
+ }
555
+ _concatValue(value, previous) {
556
+ if (previous === this.defaultValue || !Array.isArray(previous)) {
557
+ return [value];
558
+ }
559
+ return previous.concat(value);
560
+ }
561
+ choices(values) {
562
+ this.argChoices = values.slice();
563
+ this.parseArg = (arg, previous) => {
564
+ if (!this.argChoices.includes(arg)) {
565
+ throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
566
+ }
567
+ if (this.variadic) {
568
+ return this._concatValue(arg, previous);
569
+ }
570
+ return arg;
571
+ };
572
+ return this;
573
+ }
574
+ name() {
575
+ if (this.long) {
576
+ return this.long.replace(/^--/, "");
577
+ }
578
+ return this.short.replace(/^-/, "");
579
+ }
580
+ attributeName() {
581
+ if (this.negate) {
582
+ return camelcase(this.name().replace(/^no-/, ""));
583
+ }
584
+ return camelcase(this.name());
585
+ }
586
+ is(arg) {
587
+ return this.short === arg || this.long === arg;
588
+ }
589
+ isBoolean() {
590
+ return !this.required && !this.optional && !this.negate;
591
+ }
592
+ }
593
+
594
+ class DualOptions {
595
+ constructor(options) {
596
+ this.positiveOptions = new Map;
597
+ this.negativeOptions = new Map;
598
+ this.dualOptions = new Set;
599
+ options.forEach((option) => {
600
+ if (option.negate) {
601
+ this.negativeOptions.set(option.attributeName(), option);
602
+ } else {
603
+ this.positiveOptions.set(option.attributeName(), option);
604
+ }
605
+ });
606
+ this.negativeOptions.forEach((value, key) => {
607
+ if (this.positiveOptions.has(key)) {
608
+ this.dualOptions.add(key);
609
+ }
610
+ });
611
+ }
612
+ valueFromOption(value, option) {
613
+ const optionKey = option.attributeName();
614
+ if (!this.dualOptions.has(optionKey))
615
+ return true;
616
+ const preset = this.negativeOptions.get(optionKey).presetArg;
617
+ const negativeValue = preset !== undefined ? preset : false;
618
+ return option.negate === (negativeValue === value);
619
+ }
620
+ }
621
+ function camelcase(str) {
622
+ return str.split("-").reduce((str2, word) => {
623
+ return str2 + word[0].toUpperCase() + word.slice(1);
624
+ });
625
+ }
626
+ function splitOptionFlags(flags) {
627
+ let shortFlag;
628
+ let longFlag;
629
+ const shortFlagExp = /^-[^-]$/;
630
+ const longFlagExp = /^--[^-]/;
631
+ const flagParts = flags.split(/[ |,]+/).concat("guard");
632
+ if (shortFlagExp.test(flagParts[0]))
633
+ shortFlag = flagParts.shift();
634
+ if (longFlagExp.test(flagParts[0]))
635
+ longFlag = flagParts.shift();
636
+ if (!shortFlag && shortFlagExp.test(flagParts[0]))
637
+ shortFlag = flagParts.shift();
638
+ if (!shortFlag && longFlagExp.test(flagParts[0])) {
639
+ shortFlag = longFlag;
640
+ longFlag = flagParts.shift();
641
+ }
642
+ if (flagParts[0].startsWith("-")) {
643
+ const unsupportedFlag = flagParts[0];
644
+ const baseError = `option creation failed due to '${unsupportedFlag}' in option flags '${flags}'`;
645
+ if (/^-[^-][^-]/.test(unsupportedFlag))
646
+ throw new Error(`${baseError}
647
+ - a short flag is a single dash and a single character
648
+ - either use a single dash and a single character (for a short flag)
649
+ - or use a double dash for a long option (and can have two, like '--ws, --workspace')`);
650
+ if (shortFlagExp.test(unsupportedFlag))
651
+ throw new Error(`${baseError}
652
+ - too many short flags`);
653
+ if (longFlagExp.test(unsupportedFlag))
654
+ throw new Error(`${baseError}
655
+ - too many long flags`);
656
+ throw new Error(`${baseError}
657
+ - unrecognised flag format`);
658
+ }
659
+ if (shortFlag === undefined && longFlag === undefined)
660
+ throw new Error(`option creation failed due to no flags found in '${flags}'.`);
661
+ return { shortFlag, longFlag };
662
+ }
663
+ exports.Option = Option;
664
+ exports.DualOptions = DualOptions;
665
+ });
666
+
667
+ // node_modules/commander/lib/suggestSimilar.js
668
+ var require_suggestSimilar = __commonJS((exports) => {
669
+ var maxDistance = 3;
670
+ function editDistance(a, b) {
671
+ if (Math.abs(a.length - b.length) > maxDistance)
672
+ return Math.max(a.length, b.length);
673
+ const d = [];
674
+ for (let i = 0;i <= a.length; i++) {
675
+ d[i] = [i];
676
+ }
677
+ for (let j = 0;j <= b.length; j++) {
678
+ d[0][j] = j;
679
+ }
680
+ for (let j = 1;j <= b.length; j++) {
681
+ for (let i = 1;i <= a.length; i++) {
682
+ let cost = 1;
683
+ if (a[i - 1] === b[j - 1]) {
684
+ cost = 0;
685
+ } else {
686
+ cost = 1;
687
+ }
688
+ d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
689
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
690
+ d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
691
+ }
692
+ }
693
+ }
694
+ return d[a.length][b.length];
695
+ }
696
+ function suggestSimilar(word, candidates) {
697
+ if (!candidates || candidates.length === 0)
698
+ return "";
699
+ candidates = Array.from(new Set(candidates));
700
+ const searchingOptions = word.startsWith("--");
701
+ if (searchingOptions) {
702
+ word = word.slice(2);
703
+ candidates = candidates.map((candidate) => candidate.slice(2));
704
+ }
705
+ let similar = [];
706
+ let bestDistance = maxDistance;
707
+ const minSimilarity = 0.4;
708
+ candidates.forEach((candidate) => {
709
+ if (candidate.length <= 1)
710
+ return;
711
+ const distance = editDistance(word, candidate);
712
+ const length = Math.max(word.length, candidate.length);
713
+ const similarity = (length - distance) / length;
714
+ if (similarity > minSimilarity) {
715
+ if (distance < bestDistance) {
716
+ bestDistance = distance;
717
+ similar = [candidate];
718
+ } else if (distance === bestDistance) {
719
+ similar.push(candidate);
720
+ }
721
+ }
722
+ });
723
+ similar.sort((a, b) => a.localeCompare(b));
724
+ if (searchingOptions) {
725
+ similar = similar.map((candidate) => `--${candidate}`);
726
+ }
727
+ if (similar.length > 1) {
728
+ return `
729
+ (Did you mean one of ${similar.join(", ")}?)`;
730
+ }
731
+ if (similar.length === 1) {
732
+ return `
733
+ (Did you mean ${similar[0]}?)`;
734
+ }
735
+ return "";
736
+ }
737
+ exports.suggestSimilar = suggestSimilar;
738
+ });
739
+
740
+ // node_modules/commander/lib/command.js
741
+ var require_command = __commonJS((exports) => {
742
+ var EventEmitter = __require("events").EventEmitter;
743
+ var childProcess = __require("child_process");
744
+ var path = __require("path");
745
+ var fs = __require("fs");
746
+ var process2 = __require("process");
747
+ var { Argument, humanReadableArgName } = require_argument();
748
+ var { CommanderError } = require_error();
749
+ var { Help, stripColor } = require_help();
750
+ var { Option, DualOptions } = require_option();
751
+ var { suggestSimilar } = require_suggestSimilar();
752
+
753
+ class Command extends EventEmitter {
754
+ constructor(name) {
755
+ super();
756
+ this.commands = [];
757
+ this.options = [];
758
+ this.parent = null;
759
+ this._allowUnknownOption = false;
760
+ this._allowExcessArguments = false;
761
+ this.registeredArguments = [];
762
+ this._args = this.registeredArguments;
763
+ this.args = [];
764
+ this.rawArgs = [];
765
+ this.processedArgs = [];
766
+ this._scriptPath = null;
767
+ this._name = name || "";
768
+ this._optionValues = {};
769
+ this._optionValueSources = {};
770
+ this._storeOptionsAsProperties = false;
771
+ this._actionHandler = null;
772
+ this._executableHandler = false;
773
+ this._executableFile = null;
774
+ this._executableDir = null;
775
+ this._defaultCommandName = null;
776
+ this._exitCallback = null;
777
+ this._aliases = [];
778
+ this._combineFlagAndOptionalValue = true;
779
+ this._description = "";
780
+ this._summary = "";
781
+ this._argsDescription = undefined;
782
+ this._enablePositionalOptions = false;
783
+ this._passThroughOptions = false;
784
+ this._lifeCycleHooks = {};
785
+ this._showHelpAfterError = false;
786
+ this._showSuggestionAfterError = true;
787
+ this._savedState = null;
788
+ this._outputConfiguration = {
789
+ writeOut: (str) => process2.stdout.write(str),
790
+ writeErr: (str) => process2.stderr.write(str),
791
+ outputError: (str, write) => write(str),
792
+ getOutHelpWidth: () => process2.stdout.isTTY ? process2.stdout.columns : undefined,
793
+ getErrHelpWidth: () => process2.stderr.isTTY ? process2.stderr.columns : undefined,
794
+ getOutHasColors: () => useColor() ?? (process2.stdout.isTTY && process2.stdout.hasColors?.()),
795
+ getErrHasColors: () => useColor() ?? (process2.stderr.isTTY && process2.stderr.hasColors?.()),
796
+ stripColor: (str) => stripColor(str)
797
+ };
798
+ this._hidden = false;
799
+ this._helpOption = undefined;
800
+ this._addImplicitHelpCommand = undefined;
801
+ this._helpCommand = undefined;
802
+ this._helpConfiguration = {};
803
+ }
804
+ copyInheritedSettings(sourceCommand) {
805
+ this._outputConfiguration = sourceCommand._outputConfiguration;
806
+ this._helpOption = sourceCommand._helpOption;
807
+ this._helpCommand = sourceCommand._helpCommand;
808
+ this._helpConfiguration = sourceCommand._helpConfiguration;
809
+ this._exitCallback = sourceCommand._exitCallback;
810
+ this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
811
+ this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
812
+ this._allowExcessArguments = sourceCommand._allowExcessArguments;
813
+ this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
814
+ this._showHelpAfterError = sourceCommand._showHelpAfterError;
815
+ this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;
816
+ return this;
817
+ }
818
+ _getCommandAndAncestors() {
819
+ const result = [];
820
+ for (let command = this;command; command = command.parent) {
821
+ result.push(command);
822
+ }
823
+ return result;
824
+ }
825
+ command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
826
+ let desc = actionOptsOrExecDesc;
827
+ let opts = execOpts;
828
+ if (typeof desc === "object" && desc !== null) {
829
+ opts = desc;
830
+ desc = null;
831
+ }
832
+ opts = opts || {};
833
+ const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
834
+ const cmd = this.createCommand(name);
835
+ if (desc) {
836
+ cmd.description(desc);
837
+ cmd._executableHandler = true;
838
+ }
839
+ if (opts.isDefault)
840
+ this._defaultCommandName = cmd._name;
841
+ cmd._hidden = !!(opts.noHelp || opts.hidden);
842
+ cmd._executableFile = opts.executableFile || null;
843
+ if (args)
844
+ cmd.arguments(args);
845
+ this._registerCommand(cmd);
846
+ cmd.parent = this;
847
+ cmd.copyInheritedSettings(this);
848
+ if (desc)
849
+ return this;
850
+ return cmd;
851
+ }
852
+ createCommand(name) {
853
+ return new Command(name);
854
+ }
855
+ createHelp() {
856
+ return Object.assign(new Help, this.configureHelp());
857
+ }
858
+ configureHelp(configuration) {
859
+ if (configuration === undefined)
860
+ return this._helpConfiguration;
861
+ this._helpConfiguration = configuration;
862
+ return this;
863
+ }
864
+ configureOutput(configuration) {
865
+ if (configuration === undefined)
866
+ return this._outputConfiguration;
867
+ Object.assign(this._outputConfiguration, configuration);
868
+ return this;
869
+ }
870
+ showHelpAfterError(displayHelp = true) {
871
+ if (typeof displayHelp !== "string")
872
+ displayHelp = !!displayHelp;
873
+ this._showHelpAfterError = displayHelp;
874
+ return this;
875
+ }
876
+ showSuggestionAfterError(displaySuggestion = true) {
877
+ this._showSuggestionAfterError = !!displaySuggestion;
878
+ return this;
879
+ }
880
+ addCommand(cmd, opts) {
881
+ if (!cmd._name) {
882
+ throw new Error(`Command passed to .addCommand() must have a name
883
+ - specify the name in Command constructor or using .name()`);
884
+ }
885
+ opts = opts || {};
886
+ if (opts.isDefault)
887
+ this._defaultCommandName = cmd._name;
888
+ if (opts.noHelp || opts.hidden)
889
+ cmd._hidden = true;
890
+ this._registerCommand(cmd);
891
+ cmd.parent = this;
892
+ cmd._checkForBrokenPassThrough();
893
+ return this;
894
+ }
895
+ createArgument(name, description) {
896
+ return new Argument(name, description);
897
+ }
898
+ argument(name, description, fn, defaultValue) {
899
+ const argument = this.createArgument(name, description);
900
+ if (typeof fn === "function") {
901
+ argument.default(defaultValue).argParser(fn);
902
+ } else {
903
+ argument.default(fn);
904
+ }
905
+ this.addArgument(argument);
906
+ return this;
907
+ }
908
+ arguments(names) {
909
+ names.trim().split(/ +/).forEach((detail) => {
910
+ this.argument(detail);
911
+ });
912
+ return this;
913
+ }
914
+ addArgument(argument) {
915
+ const previousArgument = this.registeredArguments.slice(-1)[0];
916
+ if (previousArgument && previousArgument.variadic) {
917
+ throw new Error(`only the last argument can be variadic '${previousArgument.name()}'`);
918
+ }
919
+ if (argument.required && argument.defaultValue !== undefined && argument.parseArg === undefined) {
920
+ throw new Error(`a default value for a required argument is never used: '${argument.name()}'`);
921
+ }
922
+ this.registeredArguments.push(argument);
923
+ return this;
924
+ }
925
+ helpCommand(enableOrNameAndArgs, description) {
926
+ if (typeof enableOrNameAndArgs === "boolean") {
927
+ this._addImplicitHelpCommand = enableOrNameAndArgs;
928
+ return this;
929
+ }
930
+ enableOrNameAndArgs = enableOrNameAndArgs ?? "help [command]";
931
+ const [, helpName, helpArgs] = enableOrNameAndArgs.match(/([^ ]+) *(.*)/);
932
+ const helpDescription = description ?? "display help for command";
933
+ const helpCommand = this.createCommand(helpName);
934
+ helpCommand.helpOption(false);
935
+ if (helpArgs)
936
+ helpCommand.arguments(helpArgs);
937
+ if (helpDescription)
938
+ helpCommand.description(helpDescription);
939
+ this._addImplicitHelpCommand = true;
940
+ this._helpCommand = helpCommand;
941
+ return this;
942
+ }
943
+ addHelpCommand(helpCommand, deprecatedDescription) {
944
+ if (typeof helpCommand !== "object") {
945
+ this.helpCommand(helpCommand, deprecatedDescription);
946
+ return this;
947
+ }
948
+ this._addImplicitHelpCommand = true;
949
+ this._helpCommand = helpCommand;
950
+ return this;
951
+ }
952
+ _getHelpCommand() {
953
+ const hasImplicitHelpCommand = this._addImplicitHelpCommand ?? (this.commands.length && !this._actionHandler && !this._findCommand("help"));
954
+ if (hasImplicitHelpCommand) {
955
+ if (this._helpCommand === undefined) {
956
+ this.helpCommand(undefined, undefined);
957
+ }
958
+ return this._helpCommand;
959
+ }
960
+ return null;
961
+ }
962
+ hook(event, listener) {
963
+ const allowedValues = ["preSubcommand", "preAction", "postAction"];
964
+ if (!allowedValues.includes(event)) {
965
+ throw new Error(`Unexpected value for event passed to hook : '${event}'.
966
+ Expecting one of '${allowedValues.join("', '")}'`);
967
+ }
968
+ if (this._lifeCycleHooks[event]) {
969
+ this._lifeCycleHooks[event].push(listener);
970
+ } else {
971
+ this._lifeCycleHooks[event] = [listener];
972
+ }
973
+ return this;
974
+ }
975
+ exitOverride(fn) {
976
+ if (fn) {
977
+ this._exitCallback = fn;
978
+ } else {
979
+ this._exitCallback = (err) => {
980
+ if (err.code !== "commander.executeSubCommandAsync") {
981
+ throw err;
982
+ } else {}
983
+ };
984
+ }
985
+ return this;
986
+ }
987
+ _exit(exitCode, code, message) {
988
+ if (this._exitCallback) {
989
+ this._exitCallback(new CommanderError(exitCode, code, message));
990
+ }
991
+ process2.exit(exitCode);
992
+ }
993
+ action(fn) {
994
+ const listener = (args) => {
995
+ const expectedArgsCount = this.registeredArguments.length;
996
+ const actionArgs = args.slice(0, expectedArgsCount);
997
+ if (this._storeOptionsAsProperties) {
998
+ actionArgs[expectedArgsCount] = this;
999
+ } else {
1000
+ actionArgs[expectedArgsCount] = this.opts();
1001
+ }
1002
+ actionArgs.push(this);
1003
+ return fn.apply(this, actionArgs);
1004
+ };
1005
+ this._actionHandler = listener;
1006
+ return this;
1007
+ }
1008
+ createOption(flags, description) {
1009
+ return new Option(flags, description);
1010
+ }
1011
+ _callParseArg(target, value, previous, invalidArgumentMessage) {
1012
+ try {
1013
+ return target.parseArg(value, previous);
1014
+ } catch (err) {
1015
+ if (err.code === "commander.invalidArgument") {
1016
+ const message = `${invalidArgumentMessage} ${err.message}`;
1017
+ this.error(message, { exitCode: err.exitCode, code: err.code });
1018
+ }
1019
+ throw err;
1020
+ }
1021
+ }
1022
+ _registerOption(option) {
1023
+ const matchingOption = option.short && this._findOption(option.short) || option.long && this._findOption(option.long);
1024
+ if (matchingOption) {
1025
+ const matchingFlag = option.long && this._findOption(option.long) ? option.long : option.short;
1026
+ throw new Error(`Cannot add option '${option.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'
1027
+ - already used by option '${matchingOption.flags}'`);
1028
+ }
1029
+ this.options.push(option);
1030
+ }
1031
+ _registerCommand(command) {
1032
+ const knownBy = (cmd) => {
1033
+ return [cmd.name()].concat(cmd.aliases());
1034
+ };
1035
+ const alreadyUsed = knownBy(command).find((name) => this._findCommand(name));
1036
+ if (alreadyUsed) {
1037
+ const existingCmd = knownBy(this._findCommand(alreadyUsed)).join("|");
1038
+ const newCmd = knownBy(command).join("|");
1039
+ throw new Error(`cannot add command '${newCmd}' as already have command '${existingCmd}'`);
1040
+ }
1041
+ this.commands.push(command);
1042
+ }
1043
+ addOption(option) {
1044
+ this._registerOption(option);
1045
+ const oname = option.name();
1046
+ const name = option.attributeName();
1047
+ if (option.negate) {
1048
+ const positiveLongFlag = option.long.replace(/^--no-/, "--");
1049
+ if (!this._findOption(positiveLongFlag)) {
1050
+ this.setOptionValueWithSource(name, option.defaultValue === undefined ? true : option.defaultValue, "default");
1051
+ }
1052
+ } else if (option.defaultValue !== undefined) {
1053
+ this.setOptionValueWithSource(name, option.defaultValue, "default");
1054
+ }
1055
+ const handleOptionValue = (val, invalidValueMessage, valueSource) => {
1056
+ if (val == null && option.presetArg !== undefined) {
1057
+ val = option.presetArg;
1058
+ }
1059
+ const oldValue = this.getOptionValue(name);
1060
+ if (val !== null && option.parseArg) {
1061
+ val = this._callParseArg(option, val, oldValue, invalidValueMessage);
1062
+ } else if (val !== null && option.variadic) {
1063
+ val = option._concatValue(val, oldValue);
1064
+ }
1065
+ if (val == null) {
1066
+ if (option.negate) {
1067
+ val = false;
1068
+ } else if (option.isBoolean() || option.optional) {
1069
+ val = true;
1070
+ } else {
1071
+ val = "";
1072
+ }
1073
+ }
1074
+ this.setOptionValueWithSource(name, val, valueSource);
1075
+ };
1076
+ this.on("option:" + oname, (val) => {
1077
+ const invalidValueMessage = `error: option '${option.flags}' argument '${val}' is invalid.`;
1078
+ handleOptionValue(val, invalidValueMessage, "cli");
1079
+ });
1080
+ if (option.envVar) {
1081
+ this.on("optionEnv:" + oname, (val) => {
1082
+ const invalidValueMessage = `error: option '${option.flags}' value '${val}' from env '${option.envVar}' is invalid.`;
1083
+ handleOptionValue(val, invalidValueMessage, "env");
1084
+ });
1085
+ }
1086
+ return this;
1087
+ }
1088
+ _optionEx(config, flags, description, fn, defaultValue) {
1089
+ if (typeof flags === "object" && flags instanceof Option) {
1090
+ throw new Error("To add an Option object use addOption() instead of option() or requiredOption()");
1091
+ }
1092
+ const option = this.createOption(flags, description);
1093
+ option.makeOptionMandatory(!!config.mandatory);
1094
+ if (typeof fn === "function") {
1095
+ option.default(defaultValue).argParser(fn);
1096
+ } else if (fn instanceof RegExp) {
1097
+ const regex = fn;
1098
+ fn = (val, def) => {
1099
+ const m = regex.exec(val);
1100
+ return m ? m[0] : def;
1101
+ };
1102
+ option.default(defaultValue).argParser(fn);
1103
+ } else {
1104
+ option.default(fn);
1105
+ }
1106
+ return this.addOption(option);
1107
+ }
1108
+ option(flags, description, parseArg, defaultValue) {
1109
+ return this._optionEx({}, flags, description, parseArg, defaultValue);
1110
+ }
1111
+ requiredOption(flags, description, parseArg, defaultValue) {
1112
+ return this._optionEx({ mandatory: true }, flags, description, parseArg, defaultValue);
1113
+ }
1114
+ combineFlagAndOptionalValue(combine = true) {
1115
+ this._combineFlagAndOptionalValue = !!combine;
1116
+ return this;
1117
+ }
1118
+ allowUnknownOption(allowUnknown = true) {
1119
+ this._allowUnknownOption = !!allowUnknown;
1120
+ return this;
1121
+ }
1122
+ allowExcessArguments(allowExcess = true) {
1123
+ this._allowExcessArguments = !!allowExcess;
1124
+ return this;
1125
+ }
1126
+ enablePositionalOptions(positional = true) {
1127
+ this._enablePositionalOptions = !!positional;
1128
+ return this;
1129
+ }
1130
+ passThroughOptions(passThrough = true) {
1131
+ this._passThroughOptions = !!passThrough;
1132
+ this._checkForBrokenPassThrough();
1133
+ return this;
1134
+ }
1135
+ _checkForBrokenPassThrough() {
1136
+ if (this.parent && this._passThroughOptions && !this.parent._enablePositionalOptions) {
1137
+ throw new Error(`passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`);
1138
+ }
1139
+ }
1140
+ storeOptionsAsProperties(storeAsProperties = true) {
1141
+ if (this.options.length) {
1142
+ throw new Error("call .storeOptionsAsProperties() before adding options");
1143
+ }
1144
+ if (Object.keys(this._optionValues).length) {
1145
+ throw new Error("call .storeOptionsAsProperties() before setting option values");
1146
+ }
1147
+ this._storeOptionsAsProperties = !!storeAsProperties;
1148
+ return this;
1149
+ }
1150
+ getOptionValue(key) {
1151
+ if (this._storeOptionsAsProperties) {
1152
+ return this[key];
1153
+ }
1154
+ return this._optionValues[key];
1155
+ }
1156
+ setOptionValue(key, value) {
1157
+ return this.setOptionValueWithSource(key, value, undefined);
1158
+ }
1159
+ setOptionValueWithSource(key, value, source) {
1160
+ if (this._storeOptionsAsProperties) {
1161
+ this[key] = value;
1162
+ } else {
1163
+ this._optionValues[key] = value;
1164
+ }
1165
+ this._optionValueSources[key] = source;
1166
+ return this;
1167
+ }
1168
+ getOptionValueSource(key) {
1169
+ return this._optionValueSources[key];
1170
+ }
1171
+ getOptionValueSourceWithGlobals(key) {
1172
+ let source;
1173
+ this._getCommandAndAncestors().forEach((cmd) => {
1174
+ if (cmd.getOptionValueSource(key) !== undefined) {
1175
+ source = cmd.getOptionValueSource(key);
1176
+ }
1177
+ });
1178
+ return source;
1179
+ }
1180
+ _prepareUserArgs(argv, parseOptions) {
1181
+ if (argv !== undefined && !Array.isArray(argv)) {
1182
+ throw new Error("first parameter to parse must be array or undefined");
1183
+ }
1184
+ parseOptions = parseOptions || {};
1185
+ if (argv === undefined && parseOptions.from === undefined) {
1186
+ if (process2.versions?.electron) {
1187
+ parseOptions.from = "electron";
1188
+ }
1189
+ const execArgv = process2.execArgv ?? [];
1190
+ if (execArgv.includes("-e") || execArgv.includes("--eval") || execArgv.includes("-p") || execArgv.includes("--print")) {
1191
+ parseOptions.from = "eval";
1192
+ }
1193
+ }
1194
+ if (argv === undefined) {
1195
+ argv = process2.argv;
1196
+ }
1197
+ this.rawArgs = argv.slice();
1198
+ let userArgs;
1199
+ switch (parseOptions.from) {
1200
+ case undefined:
1201
+ case "node":
1202
+ this._scriptPath = argv[1];
1203
+ userArgs = argv.slice(2);
1204
+ break;
1205
+ case "electron":
1206
+ if (process2.defaultApp) {
1207
+ this._scriptPath = argv[1];
1208
+ userArgs = argv.slice(2);
1209
+ } else {
1210
+ userArgs = argv.slice(1);
1211
+ }
1212
+ break;
1213
+ case "user":
1214
+ userArgs = argv.slice(0);
1215
+ break;
1216
+ case "eval":
1217
+ userArgs = argv.slice(1);
1218
+ break;
1219
+ default:
1220
+ throw new Error(`unexpected parse option { from: '${parseOptions.from}' }`);
1221
+ }
1222
+ if (!this._name && this._scriptPath)
1223
+ this.nameFromFilename(this._scriptPath);
1224
+ this._name = this._name || "program";
1225
+ return userArgs;
1226
+ }
1227
+ parse(argv, parseOptions) {
1228
+ this._prepareForParse();
1229
+ const userArgs = this._prepareUserArgs(argv, parseOptions);
1230
+ this._parseCommand([], userArgs);
1231
+ return this;
1232
+ }
1233
+ async parseAsync(argv, parseOptions) {
1234
+ this._prepareForParse();
1235
+ const userArgs = this._prepareUserArgs(argv, parseOptions);
1236
+ await this._parseCommand([], userArgs);
1237
+ return this;
1238
+ }
1239
+ _prepareForParse() {
1240
+ if (this._savedState === null) {
1241
+ this.saveStateBeforeParse();
1242
+ } else {
1243
+ this.restoreStateBeforeParse();
1244
+ }
1245
+ }
1246
+ saveStateBeforeParse() {
1247
+ this._savedState = {
1248
+ _name: this._name,
1249
+ _optionValues: { ...this._optionValues },
1250
+ _optionValueSources: { ...this._optionValueSources }
1251
+ };
1252
+ }
1253
+ restoreStateBeforeParse() {
1254
+ if (this._storeOptionsAsProperties)
1255
+ throw new Error(`Can not call parse again when storeOptionsAsProperties is true.
1256
+ - either make a new Command for each call to parse, or stop storing options as properties`);
1257
+ this._name = this._savedState._name;
1258
+ this._scriptPath = null;
1259
+ this.rawArgs = [];
1260
+ this._optionValues = { ...this._savedState._optionValues };
1261
+ this._optionValueSources = { ...this._savedState._optionValueSources };
1262
+ this.args = [];
1263
+ this.processedArgs = [];
1264
+ }
1265
+ _checkForMissingExecutable(executableFile, executableDir, subcommandName) {
1266
+ if (fs.existsSync(executableFile))
1267
+ return;
1268
+ const executableDirMessage = executableDir ? `searched for local subcommand relative to directory '${executableDir}'` : "no directory for search for local subcommand, use .executableDir() to supply a custom directory";
1269
+ const executableMissing = `'${executableFile}' does not exist
1270
+ - if '${subcommandName}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
1271
+ - if the default executable name is not suitable, use the executableFile option to supply a custom name or path
1272
+ - ${executableDirMessage}`;
1273
+ throw new Error(executableMissing);
1274
+ }
1275
+ _executeSubCommand(subcommand, args) {
1276
+ args = args.slice();
1277
+ let launchWithNode = false;
1278
+ const sourceExt = [".js", ".ts", ".tsx", ".mjs", ".cjs"];
1279
+ function findFile(baseDir, baseName) {
1280
+ const localBin = path.resolve(baseDir, baseName);
1281
+ if (fs.existsSync(localBin))
1282
+ return localBin;
1283
+ if (sourceExt.includes(path.extname(baseName)))
1284
+ return;
1285
+ const foundExt = sourceExt.find((ext) => fs.existsSync(`${localBin}${ext}`));
1286
+ if (foundExt)
1287
+ return `${localBin}${foundExt}`;
1288
+ return;
1289
+ }
1290
+ this._checkForMissingMandatoryOptions();
1291
+ this._checkForConflictingOptions();
1292
+ let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;
1293
+ let executableDir = this._executableDir || "";
1294
+ if (this._scriptPath) {
1295
+ let resolvedScriptPath;
1296
+ try {
1297
+ resolvedScriptPath = fs.realpathSync(this._scriptPath);
1298
+ } catch {
1299
+ resolvedScriptPath = this._scriptPath;
1300
+ }
1301
+ executableDir = path.resolve(path.dirname(resolvedScriptPath), executableDir);
1302
+ }
1303
+ if (executableDir) {
1304
+ let localFile = findFile(executableDir, executableFile);
1305
+ if (!localFile && !subcommand._executableFile && this._scriptPath) {
1306
+ const legacyName = path.basename(this._scriptPath, path.extname(this._scriptPath));
1307
+ if (legacyName !== this._name) {
1308
+ localFile = findFile(executableDir, `${legacyName}-${subcommand._name}`);
1309
+ }
1310
+ }
1311
+ executableFile = localFile || executableFile;
1312
+ }
1313
+ launchWithNode = sourceExt.includes(path.extname(executableFile));
1314
+ let proc;
1315
+ if (process2.platform !== "win32") {
1316
+ if (launchWithNode) {
1317
+ args.unshift(executableFile);
1318
+ args = incrementNodeInspectorPort(process2.execArgv).concat(args);
1319
+ proc = childProcess.spawn(process2.argv[0], args, { stdio: "inherit" });
1320
+ } else {
1321
+ proc = childProcess.spawn(executableFile, args, { stdio: "inherit" });
1322
+ }
1323
+ } else {
1324
+ this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
1325
+ args.unshift(executableFile);
1326
+ args = incrementNodeInspectorPort(process2.execArgv).concat(args);
1327
+ proc = childProcess.spawn(process2.execPath, args, { stdio: "inherit" });
1328
+ }
1329
+ if (!proc.killed) {
1330
+ const signals = ["SIGUSR1", "SIGUSR2", "SIGTERM", "SIGINT", "SIGHUP"];
1331
+ signals.forEach((signal) => {
1332
+ process2.on(signal, () => {
1333
+ if (proc.killed === false && proc.exitCode === null) {
1334
+ proc.kill(signal);
1335
+ }
1336
+ });
1337
+ });
1338
+ }
1339
+ const exitCallback = this._exitCallback;
1340
+ proc.on("close", (code) => {
1341
+ code = code ?? 1;
1342
+ if (!exitCallback) {
1343
+ process2.exit(code);
1344
+ } else {
1345
+ exitCallback(new CommanderError(code, "commander.executeSubCommandAsync", "(close)"));
1346
+ }
1347
+ });
1348
+ proc.on("error", (err) => {
1349
+ if (err.code === "ENOENT") {
1350
+ this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
1351
+ } else if (err.code === "EACCES") {
1352
+ throw new Error(`'${executableFile}' not executable`);
1353
+ }
1354
+ if (!exitCallback) {
1355
+ process2.exit(1);
1356
+ } else {
1357
+ const wrappedError = new CommanderError(1, "commander.executeSubCommandAsync", "(error)");
1358
+ wrappedError.nestedError = err;
1359
+ exitCallback(wrappedError);
1360
+ }
1361
+ });
1362
+ this.runningCommand = proc;
1363
+ }
1364
+ _dispatchSubcommand(commandName, operands, unknown) {
1365
+ const subCommand = this._findCommand(commandName);
1366
+ if (!subCommand)
1367
+ this.help({ error: true });
1368
+ subCommand._prepareForParse();
1369
+ let promiseChain;
1370
+ promiseChain = this._chainOrCallSubCommandHook(promiseChain, subCommand, "preSubcommand");
1371
+ promiseChain = this._chainOrCall(promiseChain, () => {
1372
+ if (subCommand._executableHandler) {
1373
+ this._executeSubCommand(subCommand, operands.concat(unknown));
1374
+ } else {
1375
+ return subCommand._parseCommand(operands, unknown);
1376
+ }
1377
+ });
1378
+ return promiseChain;
1379
+ }
1380
+ _dispatchHelpCommand(subcommandName) {
1381
+ if (!subcommandName) {
1382
+ this.help();
1383
+ }
1384
+ const subCommand = this._findCommand(subcommandName);
1385
+ if (subCommand && !subCommand._executableHandler) {
1386
+ subCommand.help();
1387
+ }
1388
+ return this._dispatchSubcommand(subcommandName, [], [this._getHelpOption()?.long ?? this._getHelpOption()?.short ?? "--help"]);
1389
+ }
1390
+ _checkNumberOfArguments() {
1391
+ this.registeredArguments.forEach((arg, i) => {
1392
+ if (arg.required && this.args[i] == null) {
1393
+ this.missingArgument(arg.name());
1394
+ }
1395
+ });
1396
+ if (this.registeredArguments.length > 0 && this.registeredArguments[this.registeredArguments.length - 1].variadic) {
1397
+ return;
1398
+ }
1399
+ if (this.args.length > this.registeredArguments.length) {
1400
+ this._excessArguments(this.args);
1401
+ }
1402
+ }
1403
+ _processArguments() {
1404
+ const myParseArg = (argument, value, previous) => {
1405
+ let parsedValue = value;
1406
+ if (value !== null && argument.parseArg) {
1407
+ const invalidValueMessage = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'.`;
1408
+ parsedValue = this._callParseArg(argument, value, previous, invalidValueMessage);
1409
+ }
1410
+ return parsedValue;
1411
+ };
1412
+ this._checkNumberOfArguments();
1413
+ const processedArgs = [];
1414
+ this.registeredArguments.forEach((declaredArg, index) => {
1415
+ let value = declaredArg.defaultValue;
1416
+ if (declaredArg.variadic) {
1417
+ if (index < this.args.length) {
1418
+ value = this.args.slice(index);
1419
+ if (declaredArg.parseArg) {
1420
+ value = value.reduce((processed, v) => {
1421
+ return myParseArg(declaredArg, v, processed);
1422
+ }, declaredArg.defaultValue);
1423
+ }
1424
+ } else if (value === undefined) {
1425
+ value = [];
1426
+ }
1427
+ } else if (index < this.args.length) {
1428
+ value = this.args[index];
1429
+ if (declaredArg.parseArg) {
1430
+ value = myParseArg(declaredArg, value, declaredArg.defaultValue);
1431
+ }
1432
+ }
1433
+ processedArgs[index] = value;
1434
+ });
1435
+ this.processedArgs = processedArgs;
1436
+ }
1437
+ _chainOrCall(promise, fn) {
1438
+ if (promise && promise.then && typeof promise.then === "function") {
1439
+ return promise.then(() => fn());
1440
+ }
1441
+ return fn();
1442
+ }
1443
+ _chainOrCallHooks(promise, event) {
1444
+ let result = promise;
1445
+ const hooks = [];
1446
+ this._getCommandAndAncestors().reverse().filter((cmd) => cmd._lifeCycleHooks[event] !== undefined).forEach((hookedCommand) => {
1447
+ hookedCommand._lifeCycleHooks[event].forEach((callback) => {
1448
+ hooks.push({ hookedCommand, callback });
1449
+ });
1450
+ });
1451
+ if (event === "postAction") {
1452
+ hooks.reverse();
1453
+ }
1454
+ hooks.forEach((hookDetail) => {
1455
+ result = this._chainOrCall(result, () => {
1456
+ return hookDetail.callback(hookDetail.hookedCommand, this);
1457
+ });
1458
+ });
1459
+ return result;
1460
+ }
1461
+ _chainOrCallSubCommandHook(promise, subCommand, event) {
1462
+ let result = promise;
1463
+ if (this._lifeCycleHooks[event] !== undefined) {
1464
+ this._lifeCycleHooks[event].forEach((hook) => {
1465
+ result = this._chainOrCall(result, () => {
1466
+ return hook(this, subCommand);
1467
+ });
1468
+ });
1469
+ }
1470
+ return result;
1471
+ }
1472
+ _parseCommand(operands, unknown) {
1473
+ const parsed = this.parseOptions(unknown);
1474
+ this._parseOptionsEnv();
1475
+ this._parseOptionsImplied();
1476
+ operands = operands.concat(parsed.operands);
1477
+ unknown = parsed.unknown;
1478
+ this.args = operands.concat(unknown);
1479
+ if (operands && this._findCommand(operands[0])) {
1480
+ return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
1481
+ }
1482
+ if (this._getHelpCommand() && operands[0] === this._getHelpCommand().name()) {
1483
+ return this._dispatchHelpCommand(operands[1]);
1484
+ }
1485
+ if (this._defaultCommandName) {
1486
+ this._outputHelpIfRequested(unknown);
1487
+ return this._dispatchSubcommand(this._defaultCommandName, operands, unknown);
1488
+ }
1489
+ if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) {
1490
+ this.help({ error: true });
1491
+ }
1492
+ this._outputHelpIfRequested(parsed.unknown);
1493
+ this._checkForMissingMandatoryOptions();
1494
+ this._checkForConflictingOptions();
1495
+ const checkForUnknownOptions = () => {
1496
+ if (parsed.unknown.length > 0) {
1497
+ this.unknownOption(parsed.unknown[0]);
1498
+ }
1499
+ };
1500
+ const commandEvent = `command:${this.name()}`;
1501
+ if (this._actionHandler) {
1502
+ checkForUnknownOptions();
1503
+ this._processArguments();
1504
+ let promiseChain;
1505
+ promiseChain = this._chainOrCallHooks(promiseChain, "preAction");
1506
+ promiseChain = this._chainOrCall(promiseChain, () => this._actionHandler(this.processedArgs));
1507
+ if (this.parent) {
1508
+ promiseChain = this._chainOrCall(promiseChain, () => {
1509
+ this.parent.emit(commandEvent, operands, unknown);
1510
+ });
1511
+ }
1512
+ promiseChain = this._chainOrCallHooks(promiseChain, "postAction");
1513
+ return promiseChain;
1514
+ }
1515
+ if (this.parent && this.parent.listenerCount(commandEvent)) {
1516
+ checkForUnknownOptions();
1517
+ this._processArguments();
1518
+ this.parent.emit(commandEvent, operands, unknown);
1519
+ } else if (operands.length) {
1520
+ if (this._findCommand("*")) {
1521
+ return this._dispatchSubcommand("*", operands, unknown);
1522
+ }
1523
+ if (this.listenerCount("command:*")) {
1524
+ this.emit("command:*", operands, unknown);
1525
+ } else if (this.commands.length) {
1526
+ this.unknownCommand();
1527
+ } else {
1528
+ checkForUnknownOptions();
1529
+ this._processArguments();
1530
+ }
1531
+ } else if (this.commands.length) {
1532
+ checkForUnknownOptions();
1533
+ this.help({ error: true });
1534
+ } else {
1535
+ checkForUnknownOptions();
1536
+ this._processArguments();
1537
+ }
1538
+ }
1539
+ _findCommand(name) {
1540
+ if (!name)
1541
+ return;
1542
+ return this.commands.find((cmd) => cmd._name === name || cmd._aliases.includes(name));
1543
+ }
1544
+ _findOption(arg) {
1545
+ return this.options.find((option) => option.is(arg));
1546
+ }
1547
+ _checkForMissingMandatoryOptions() {
1548
+ this._getCommandAndAncestors().forEach((cmd) => {
1549
+ cmd.options.forEach((anOption) => {
1550
+ if (anOption.mandatory && cmd.getOptionValue(anOption.attributeName()) === undefined) {
1551
+ cmd.missingMandatoryOptionValue(anOption);
1552
+ }
1553
+ });
1554
+ });
1555
+ }
1556
+ _checkForConflictingLocalOptions() {
1557
+ const definedNonDefaultOptions = this.options.filter((option) => {
1558
+ const optionKey = option.attributeName();
1559
+ if (this.getOptionValue(optionKey) === undefined) {
1560
+ return false;
1561
+ }
1562
+ return this.getOptionValueSource(optionKey) !== "default";
1563
+ });
1564
+ const optionsWithConflicting = definedNonDefaultOptions.filter((option) => option.conflictsWith.length > 0);
1565
+ optionsWithConflicting.forEach((option) => {
1566
+ const conflictingAndDefined = definedNonDefaultOptions.find((defined) => option.conflictsWith.includes(defined.attributeName()));
1567
+ if (conflictingAndDefined) {
1568
+ this._conflictingOption(option, conflictingAndDefined);
1569
+ }
1570
+ });
1571
+ }
1572
+ _checkForConflictingOptions() {
1573
+ this._getCommandAndAncestors().forEach((cmd) => {
1574
+ cmd._checkForConflictingLocalOptions();
1575
+ });
1576
+ }
1577
+ parseOptions(argv) {
1578
+ const operands = [];
1579
+ const unknown = [];
1580
+ let dest = operands;
1581
+ const args = argv.slice();
1582
+ function maybeOption(arg) {
1583
+ return arg.length > 1 && arg[0] === "-";
1584
+ }
1585
+ let activeVariadicOption = null;
1586
+ while (args.length) {
1587
+ const arg = args.shift();
1588
+ if (arg === "--") {
1589
+ if (dest === unknown)
1590
+ dest.push(arg);
1591
+ dest.push(...args);
1592
+ break;
1593
+ }
1594
+ if (activeVariadicOption && !maybeOption(arg)) {
1595
+ this.emit(`option:${activeVariadicOption.name()}`, arg);
1596
+ continue;
1597
+ }
1598
+ activeVariadicOption = null;
1599
+ if (maybeOption(arg)) {
1600
+ const option = this._findOption(arg);
1601
+ if (option) {
1602
+ if (option.required) {
1603
+ const value = args.shift();
1604
+ if (value === undefined)
1605
+ this.optionMissingArgument(option);
1606
+ this.emit(`option:${option.name()}`, value);
1607
+ } else if (option.optional) {
1608
+ let value = null;
1609
+ if (args.length > 0 && !maybeOption(args[0])) {
1610
+ value = args.shift();
1611
+ }
1612
+ this.emit(`option:${option.name()}`, value);
1613
+ } else {
1614
+ this.emit(`option:${option.name()}`);
1615
+ }
1616
+ activeVariadicOption = option.variadic ? option : null;
1617
+ continue;
1618
+ }
1619
+ }
1620
+ if (arg.length > 2 && arg[0] === "-" && arg[1] !== "-") {
1621
+ const option = this._findOption(`-${arg[1]}`);
1622
+ if (option) {
1623
+ if (option.required || option.optional && this._combineFlagAndOptionalValue) {
1624
+ this.emit(`option:${option.name()}`, arg.slice(2));
1625
+ } else {
1626
+ this.emit(`option:${option.name()}`);
1627
+ args.unshift(`-${arg.slice(2)}`);
1628
+ }
1629
+ continue;
1630
+ }
1631
+ }
1632
+ if (/^--[^=]+=/.test(arg)) {
1633
+ const index = arg.indexOf("=");
1634
+ const option = this._findOption(arg.slice(0, index));
1635
+ if (option && (option.required || option.optional)) {
1636
+ this.emit(`option:${option.name()}`, arg.slice(index + 1));
1637
+ continue;
1638
+ }
1639
+ }
1640
+ if (maybeOption(arg)) {
1641
+ dest = unknown;
1642
+ }
1643
+ if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
1644
+ if (this._findCommand(arg)) {
1645
+ operands.push(arg);
1646
+ if (args.length > 0)
1647
+ unknown.push(...args);
1648
+ break;
1649
+ } else if (this._getHelpCommand() && arg === this._getHelpCommand().name()) {
1650
+ operands.push(arg);
1651
+ if (args.length > 0)
1652
+ operands.push(...args);
1653
+ break;
1654
+ } else if (this._defaultCommandName) {
1655
+ unknown.push(arg);
1656
+ if (args.length > 0)
1657
+ unknown.push(...args);
1658
+ break;
1659
+ }
1660
+ }
1661
+ if (this._passThroughOptions) {
1662
+ dest.push(arg);
1663
+ if (args.length > 0)
1664
+ dest.push(...args);
1665
+ break;
1666
+ }
1667
+ dest.push(arg);
1668
+ }
1669
+ return { operands, unknown };
1670
+ }
1671
+ opts() {
1672
+ if (this._storeOptionsAsProperties) {
1673
+ const result = {};
1674
+ const len = this.options.length;
1675
+ for (let i = 0;i < len; i++) {
1676
+ const key = this.options[i].attributeName();
1677
+ result[key] = key === this._versionOptionName ? this._version : this[key];
1678
+ }
1679
+ return result;
1680
+ }
1681
+ return this._optionValues;
1682
+ }
1683
+ optsWithGlobals() {
1684
+ return this._getCommandAndAncestors().reduce((combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()), {});
1685
+ }
1686
+ error(message, errorOptions) {
1687
+ this._outputConfiguration.outputError(`${message}
1688
+ `, this._outputConfiguration.writeErr);
1689
+ if (typeof this._showHelpAfterError === "string") {
1690
+ this._outputConfiguration.writeErr(`${this._showHelpAfterError}
1691
+ `);
1692
+ } else if (this._showHelpAfterError) {
1693
+ this._outputConfiguration.writeErr(`
1694
+ `);
1695
+ this.outputHelp({ error: true });
1696
+ }
1697
+ const config = errorOptions || {};
1698
+ const exitCode = config.exitCode || 1;
1699
+ const code = config.code || "commander.error";
1700
+ this._exit(exitCode, code, message);
1701
+ }
1702
+ _parseOptionsEnv() {
1703
+ this.options.forEach((option) => {
1704
+ if (option.envVar && option.envVar in process2.env) {
1705
+ const optionKey = option.attributeName();
1706
+ if (this.getOptionValue(optionKey) === undefined || ["default", "config", "env"].includes(this.getOptionValueSource(optionKey))) {
1707
+ if (option.required || option.optional) {
1708
+ this.emit(`optionEnv:${option.name()}`, process2.env[option.envVar]);
1709
+ } else {
1710
+ this.emit(`optionEnv:${option.name()}`);
1711
+ }
1712
+ }
1713
+ }
1714
+ });
1715
+ }
1716
+ _parseOptionsImplied() {
1717
+ const dualHelper = new DualOptions(this.options);
1718
+ const hasCustomOptionValue = (optionKey) => {
1719
+ return this.getOptionValue(optionKey) !== undefined && !["default", "implied"].includes(this.getOptionValueSource(optionKey));
1720
+ };
1721
+ this.options.filter((option) => option.implied !== undefined && hasCustomOptionValue(option.attributeName()) && dualHelper.valueFromOption(this.getOptionValue(option.attributeName()), option)).forEach((option) => {
1722
+ Object.keys(option.implied).filter((impliedKey) => !hasCustomOptionValue(impliedKey)).forEach((impliedKey) => {
1723
+ this.setOptionValueWithSource(impliedKey, option.implied[impliedKey], "implied");
1724
+ });
1725
+ });
1726
+ }
1727
+ missingArgument(name) {
1728
+ const message = `error: missing required argument '${name}'`;
1729
+ this.error(message, { code: "commander.missingArgument" });
1730
+ }
1731
+ optionMissingArgument(option) {
1732
+ const message = `error: option '${option.flags}' argument missing`;
1733
+ this.error(message, { code: "commander.optionMissingArgument" });
1734
+ }
1735
+ missingMandatoryOptionValue(option) {
1736
+ const message = `error: required option '${option.flags}' not specified`;
1737
+ this.error(message, { code: "commander.missingMandatoryOptionValue" });
1738
+ }
1739
+ _conflictingOption(option, conflictingOption) {
1740
+ const findBestOptionFromValue = (option2) => {
1741
+ const optionKey = option2.attributeName();
1742
+ const optionValue = this.getOptionValue(optionKey);
1743
+ const negativeOption = this.options.find((target) => target.negate && optionKey === target.attributeName());
1744
+ const positiveOption = this.options.find((target) => !target.negate && optionKey === target.attributeName());
1745
+ if (negativeOption && (negativeOption.presetArg === undefined && optionValue === false || negativeOption.presetArg !== undefined && optionValue === negativeOption.presetArg)) {
1746
+ return negativeOption;
1747
+ }
1748
+ return positiveOption || option2;
1749
+ };
1750
+ const getErrorMessage = (option2) => {
1751
+ const bestOption = findBestOptionFromValue(option2);
1752
+ const optionKey = bestOption.attributeName();
1753
+ const source = this.getOptionValueSource(optionKey);
1754
+ if (source === "env") {
1755
+ return `environment variable '${bestOption.envVar}'`;
1756
+ }
1757
+ return `option '${bestOption.flags}'`;
1758
+ };
1759
+ const message = `error: ${getErrorMessage(option)} cannot be used with ${getErrorMessage(conflictingOption)}`;
1760
+ this.error(message, { code: "commander.conflictingOption" });
1761
+ }
1762
+ unknownOption(flag) {
1763
+ if (this._allowUnknownOption)
1764
+ return;
1765
+ let suggestion = "";
1766
+ if (flag.startsWith("--") && this._showSuggestionAfterError) {
1767
+ let candidateFlags = [];
1768
+ let command = this;
1769
+ do {
1770
+ const moreFlags = command.createHelp().visibleOptions(command).filter((option) => option.long).map((option) => option.long);
1771
+ candidateFlags = candidateFlags.concat(moreFlags);
1772
+ command = command.parent;
1773
+ } while (command && !command._enablePositionalOptions);
1774
+ suggestion = suggestSimilar(flag, candidateFlags);
1775
+ }
1776
+ const message = `error: unknown option '${flag}'${suggestion}`;
1777
+ this.error(message, { code: "commander.unknownOption" });
1778
+ }
1779
+ _excessArguments(receivedArgs) {
1780
+ if (this._allowExcessArguments)
1781
+ return;
1782
+ const expected = this.registeredArguments.length;
1783
+ const s = expected === 1 ? "" : "s";
1784
+ const forSubcommand = this.parent ? ` for '${this.name()}'` : "";
1785
+ const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;
1786
+ this.error(message, { code: "commander.excessArguments" });
1787
+ }
1788
+ unknownCommand() {
1789
+ const unknownName = this.args[0];
1790
+ let suggestion = "";
1791
+ if (this._showSuggestionAfterError) {
1792
+ const candidateNames = [];
1793
+ this.createHelp().visibleCommands(this).forEach((command) => {
1794
+ candidateNames.push(command.name());
1795
+ if (command.alias())
1796
+ candidateNames.push(command.alias());
1797
+ });
1798
+ suggestion = suggestSimilar(unknownName, candidateNames);
1799
+ }
1800
+ const message = `error: unknown command '${unknownName}'${suggestion}`;
1801
+ this.error(message, { code: "commander.unknownCommand" });
1802
+ }
1803
+ version(str, flags, description) {
1804
+ if (str === undefined)
1805
+ return this._version;
1806
+ this._version = str;
1807
+ flags = flags || "-V, --version";
1808
+ description = description || "output the version number";
1809
+ const versionOption = this.createOption(flags, description);
1810
+ this._versionOptionName = versionOption.attributeName();
1811
+ this._registerOption(versionOption);
1812
+ this.on("option:" + versionOption.name(), () => {
1813
+ this._outputConfiguration.writeOut(`${str}
1814
+ `);
1815
+ this._exit(0, "commander.version", str);
1816
+ });
1817
+ return this;
1818
+ }
1819
+ description(str, argsDescription) {
1820
+ if (str === undefined && argsDescription === undefined)
1821
+ return this._description;
1822
+ this._description = str;
1823
+ if (argsDescription) {
1824
+ this._argsDescription = argsDescription;
1825
+ }
1826
+ return this;
1827
+ }
1828
+ summary(str) {
1829
+ if (str === undefined)
1830
+ return this._summary;
1831
+ this._summary = str;
1832
+ return this;
1833
+ }
1834
+ alias(alias) {
1835
+ if (alias === undefined)
1836
+ return this._aliases[0];
1837
+ let command = this;
1838
+ if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
1839
+ command = this.commands[this.commands.length - 1];
1840
+ }
1841
+ if (alias === command._name)
1842
+ throw new Error("Command alias can't be the same as its name");
1843
+ const matchingCommand = this.parent?._findCommand(alias);
1844
+ if (matchingCommand) {
1845
+ const existingCmd = [matchingCommand.name()].concat(matchingCommand.aliases()).join("|");
1846
+ throw new Error(`cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`);
1847
+ }
1848
+ command._aliases.push(alias);
1849
+ return this;
1850
+ }
1851
+ aliases(aliases) {
1852
+ if (aliases === undefined)
1853
+ return this._aliases;
1854
+ aliases.forEach((alias) => this.alias(alias));
1855
+ return this;
1856
+ }
1857
+ usage(str) {
1858
+ if (str === undefined) {
1859
+ if (this._usage)
1860
+ return this._usage;
1861
+ const args = this.registeredArguments.map((arg) => {
1862
+ return humanReadableArgName(arg);
1863
+ });
1864
+ return [].concat(this.options.length || this._helpOption !== null ? "[options]" : [], this.commands.length ? "[command]" : [], this.registeredArguments.length ? args : []).join(" ");
1865
+ }
1866
+ this._usage = str;
1867
+ return this;
1868
+ }
1869
+ name(str) {
1870
+ if (str === undefined)
1871
+ return this._name;
1872
+ this._name = str;
1873
+ return this;
1874
+ }
1875
+ nameFromFilename(filename) {
1876
+ this._name = path.basename(filename, path.extname(filename));
1877
+ return this;
1878
+ }
1879
+ executableDir(path2) {
1880
+ if (path2 === undefined)
1881
+ return this._executableDir;
1882
+ this._executableDir = path2;
1883
+ return this;
1884
+ }
1885
+ helpInformation(contextOptions) {
1886
+ const helper = this.createHelp();
1887
+ const context = this._getOutputContext(contextOptions);
1888
+ helper.prepareContext({
1889
+ error: context.error,
1890
+ helpWidth: context.helpWidth,
1891
+ outputHasColors: context.hasColors
1892
+ });
1893
+ const text = helper.formatHelp(this, helper);
1894
+ if (context.hasColors)
1895
+ return text;
1896
+ return this._outputConfiguration.stripColor(text);
1897
+ }
1898
+ _getOutputContext(contextOptions) {
1899
+ contextOptions = contextOptions || {};
1900
+ const error = !!contextOptions.error;
1901
+ let baseWrite;
1902
+ let hasColors;
1903
+ let helpWidth;
1904
+ if (error) {
1905
+ baseWrite = (str) => this._outputConfiguration.writeErr(str);
1906
+ hasColors = this._outputConfiguration.getErrHasColors();
1907
+ helpWidth = this._outputConfiguration.getErrHelpWidth();
1908
+ } else {
1909
+ baseWrite = (str) => this._outputConfiguration.writeOut(str);
1910
+ hasColors = this._outputConfiguration.getOutHasColors();
1911
+ helpWidth = this._outputConfiguration.getOutHelpWidth();
1912
+ }
1913
+ const write = (str) => {
1914
+ if (!hasColors)
1915
+ str = this._outputConfiguration.stripColor(str);
1916
+ return baseWrite(str);
1917
+ };
1918
+ return { error, write, hasColors, helpWidth };
1919
+ }
1920
+ outputHelp(contextOptions) {
1921
+ let deprecatedCallback;
1922
+ if (typeof contextOptions === "function") {
1923
+ deprecatedCallback = contextOptions;
1924
+ contextOptions = undefined;
1925
+ }
1926
+ const outputContext = this._getOutputContext(contextOptions);
1927
+ const eventContext = {
1928
+ error: outputContext.error,
1929
+ write: outputContext.write,
1930
+ command: this
1931
+ };
1932
+ this._getCommandAndAncestors().reverse().forEach((command) => command.emit("beforeAllHelp", eventContext));
1933
+ this.emit("beforeHelp", eventContext);
1934
+ let helpInformation = this.helpInformation({ error: outputContext.error });
1935
+ if (deprecatedCallback) {
1936
+ helpInformation = deprecatedCallback(helpInformation);
1937
+ if (typeof helpInformation !== "string" && !Buffer.isBuffer(helpInformation)) {
1938
+ throw new Error("outputHelp callback must return a string or a Buffer");
1939
+ }
1940
+ }
1941
+ outputContext.write(helpInformation);
1942
+ if (this._getHelpOption()?.long) {
1943
+ this.emit(this._getHelpOption().long);
1944
+ }
1945
+ this.emit("afterHelp", eventContext);
1946
+ this._getCommandAndAncestors().forEach((command) => command.emit("afterAllHelp", eventContext));
1947
+ }
1948
+ helpOption(flags, description) {
1949
+ if (typeof flags === "boolean") {
1950
+ if (flags) {
1951
+ this._helpOption = this._helpOption ?? undefined;
1952
+ } else {
1953
+ this._helpOption = null;
1954
+ }
1955
+ return this;
1956
+ }
1957
+ flags = flags ?? "-h, --help";
1958
+ description = description ?? "display help for command";
1959
+ this._helpOption = this.createOption(flags, description);
1960
+ return this;
1961
+ }
1962
+ _getHelpOption() {
1963
+ if (this._helpOption === undefined) {
1964
+ this.helpOption(undefined, undefined);
1965
+ }
1966
+ return this._helpOption;
1967
+ }
1968
+ addHelpOption(option) {
1969
+ this._helpOption = option;
1970
+ return this;
1971
+ }
1972
+ help(contextOptions) {
1973
+ this.outputHelp(contextOptions);
1974
+ let exitCode = Number(process2.exitCode ?? 0);
1975
+ if (exitCode === 0 && contextOptions && typeof contextOptions !== "function" && contextOptions.error) {
1976
+ exitCode = 1;
1977
+ }
1978
+ this._exit(exitCode, "commander.help", "(outputHelp)");
1979
+ }
1980
+ addHelpText(position, text) {
1981
+ const allowedValues = ["beforeAll", "before", "after", "afterAll"];
1982
+ if (!allowedValues.includes(position)) {
1983
+ throw new Error(`Unexpected value for position to addHelpText.
1984
+ Expecting one of '${allowedValues.join("', '")}'`);
1985
+ }
1986
+ const helpEvent = `${position}Help`;
1987
+ this.on(helpEvent, (context) => {
1988
+ let helpStr;
1989
+ if (typeof text === "function") {
1990
+ helpStr = text({ error: context.error, command: context.command });
1991
+ } else {
1992
+ helpStr = text;
1993
+ }
1994
+ if (helpStr) {
1995
+ context.write(`${helpStr}
1996
+ `);
1997
+ }
1998
+ });
1999
+ return this;
2000
+ }
2001
+ _outputHelpIfRequested(args) {
2002
+ const helpOption = this._getHelpOption();
2003
+ const helpRequested = helpOption && args.find((arg) => helpOption.is(arg));
2004
+ if (helpRequested) {
2005
+ this.outputHelp();
2006
+ this._exit(0, "commander.helpDisplayed", "(outputHelp)");
2007
+ }
2008
+ }
2009
+ }
2010
+ function incrementNodeInspectorPort(args) {
2011
+ return args.map((arg) => {
2012
+ if (!arg.startsWith("--inspect")) {
2013
+ return arg;
2014
+ }
2015
+ let debugOption;
2016
+ let debugHost = "127.0.0.1";
2017
+ let debugPort = "9229";
2018
+ let match;
2019
+ if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
2020
+ debugOption = match[1];
2021
+ } else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
2022
+ debugOption = match[1];
2023
+ if (/^\d+$/.test(match[3])) {
2024
+ debugPort = match[3];
2025
+ } else {
2026
+ debugHost = match[3];
2027
+ }
2028
+ } else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) {
2029
+ debugOption = match[1];
2030
+ debugHost = match[3];
2031
+ debugPort = match[4];
2032
+ }
2033
+ if (debugOption && debugPort !== "0") {
2034
+ return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
2035
+ }
2036
+ return arg;
2037
+ });
2038
+ }
2039
+ function useColor() {
2040
+ if (process2.env.NO_COLOR || process2.env.FORCE_COLOR === "0" || process2.env.FORCE_COLOR === "false")
2041
+ return false;
2042
+ if (process2.env.FORCE_COLOR || process2.env.CLICOLOR_FORCE !== undefined)
2043
+ return true;
2044
+ return;
2045
+ }
2046
+ exports.Command = Command;
2047
+ exports.useColor = useColor;
2048
+ });
2049
+
2050
+ // node_modules/commander/index.js
2051
+ var require_commander = __commonJS((exports) => {
2052
+ var { Argument } = require_argument();
2053
+ var { Command } = require_command();
2054
+ var { CommanderError, InvalidArgumentError } = require_error();
2055
+ var { Help } = require_help();
2056
+ var { Option } = require_option();
2057
+ exports.program = new Command;
2058
+ exports.createCommand = (name) => new Command(name);
2059
+ exports.createOption = (flags, description) => new Option(flags, description);
2060
+ exports.createArgument = (name, description) => new Argument(name, description);
2061
+ exports.Command = Command;
2062
+ exports.Option = Option;
2063
+ exports.Argument = Argument;
2064
+ exports.Help = Help;
2065
+ exports.CommanderError = CommanderError;
2066
+ exports.InvalidArgumentError = InvalidArgumentError;
2067
+ exports.InvalidOptionArgumentError = InvalidArgumentError;
2068
+ });
2069
+
2070
+ // node_modules/commander/esm.mjs
2071
+ var import__ = __toESM(require_commander(), 1);
2072
+ var {
2073
+ program,
2074
+ createCommand,
2075
+ createArgument,
2076
+ createOption,
2077
+ CommanderError,
2078
+ InvalidArgumentError,
2079
+ InvalidOptionArgumentError,
2080
+ Command,
2081
+ Argument,
2082
+ Option,
2083
+ Help
2084
+ } = import__.default;
2085
+
2086
+ // node_modules/chalk/source/vendor/ansi-styles/index.js
2087
+ var ANSI_BACKGROUND_OFFSET = 10;
2088
+ var wrapAnsi16 = (offset = 0) => (code) => `\x1B[${code + offset}m`;
2089
+ var wrapAnsi256 = (offset = 0) => (code) => `\x1B[${38 + offset};5;${code}m`;
2090
+ var wrapAnsi16m = (offset = 0) => (red, green, blue) => `\x1B[${38 + offset};2;${red};${green};${blue}m`;
2091
+ var styles = {
2092
+ modifier: {
2093
+ reset: [0, 0],
2094
+ bold: [1, 22],
2095
+ dim: [2, 22],
2096
+ italic: [3, 23],
2097
+ underline: [4, 24],
2098
+ overline: [53, 55],
2099
+ inverse: [7, 27],
2100
+ hidden: [8, 28],
2101
+ strikethrough: [9, 29]
2102
+ },
2103
+ color: {
2104
+ black: [30, 39],
2105
+ red: [31, 39],
2106
+ green: [32, 39],
2107
+ yellow: [33, 39],
2108
+ blue: [34, 39],
2109
+ magenta: [35, 39],
2110
+ cyan: [36, 39],
2111
+ white: [37, 39],
2112
+ blackBright: [90, 39],
2113
+ gray: [90, 39],
2114
+ grey: [90, 39],
2115
+ redBright: [91, 39],
2116
+ greenBright: [92, 39],
2117
+ yellowBright: [93, 39],
2118
+ blueBright: [94, 39],
2119
+ magentaBright: [95, 39],
2120
+ cyanBright: [96, 39],
2121
+ whiteBright: [97, 39]
2122
+ },
2123
+ bgColor: {
2124
+ bgBlack: [40, 49],
2125
+ bgRed: [41, 49],
2126
+ bgGreen: [42, 49],
2127
+ bgYellow: [43, 49],
2128
+ bgBlue: [44, 49],
2129
+ bgMagenta: [45, 49],
2130
+ bgCyan: [46, 49],
2131
+ bgWhite: [47, 49],
2132
+ bgBlackBright: [100, 49],
2133
+ bgGray: [100, 49],
2134
+ bgGrey: [100, 49],
2135
+ bgRedBright: [101, 49],
2136
+ bgGreenBright: [102, 49],
2137
+ bgYellowBright: [103, 49],
2138
+ bgBlueBright: [104, 49],
2139
+ bgMagentaBright: [105, 49],
2140
+ bgCyanBright: [106, 49],
2141
+ bgWhiteBright: [107, 49]
2142
+ }
2143
+ };
2144
+ var modifierNames = Object.keys(styles.modifier);
2145
+ var foregroundColorNames = Object.keys(styles.color);
2146
+ var backgroundColorNames = Object.keys(styles.bgColor);
2147
+ var colorNames = [...foregroundColorNames, ...backgroundColorNames];
2148
+ function assembleStyles() {
2149
+ const codes = new Map;
2150
+ for (const [groupName, group] of Object.entries(styles)) {
2151
+ for (const [styleName, style] of Object.entries(group)) {
2152
+ styles[styleName] = {
2153
+ open: `\x1B[${style[0]}m`,
2154
+ close: `\x1B[${style[1]}m`
2155
+ };
2156
+ group[styleName] = styles[styleName];
2157
+ codes.set(style[0], style[1]);
2158
+ }
2159
+ Object.defineProperty(styles, groupName, {
2160
+ value: group,
2161
+ enumerable: false
2162
+ });
2163
+ }
2164
+ Object.defineProperty(styles, "codes", {
2165
+ value: codes,
2166
+ enumerable: false
2167
+ });
2168
+ styles.color.close = "\x1B[39m";
2169
+ styles.bgColor.close = "\x1B[49m";
2170
+ styles.color.ansi = wrapAnsi16();
2171
+ styles.color.ansi256 = wrapAnsi256();
2172
+ styles.color.ansi16m = wrapAnsi16m();
2173
+ styles.bgColor.ansi = wrapAnsi16(ANSI_BACKGROUND_OFFSET);
2174
+ styles.bgColor.ansi256 = wrapAnsi256(ANSI_BACKGROUND_OFFSET);
2175
+ styles.bgColor.ansi16m = wrapAnsi16m(ANSI_BACKGROUND_OFFSET);
2176
+ Object.defineProperties(styles, {
2177
+ rgbToAnsi256: {
2178
+ value(red, green, blue) {
2179
+ if (red === green && green === blue) {
2180
+ if (red < 8) {
2181
+ return 16;
2182
+ }
2183
+ if (red > 248) {
2184
+ return 231;
2185
+ }
2186
+ return Math.round((red - 8) / 247 * 24) + 232;
2187
+ }
2188
+ return 16 + 36 * Math.round(red / 255 * 5) + 6 * Math.round(green / 255 * 5) + Math.round(blue / 255 * 5);
2189
+ },
2190
+ enumerable: false
2191
+ },
2192
+ hexToRgb: {
2193
+ value(hex) {
2194
+ const matches = /[a-f\d]{6}|[a-f\d]{3}/i.exec(hex.toString(16));
2195
+ if (!matches) {
2196
+ return [0, 0, 0];
2197
+ }
2198
+ let [colorString] = matches;
2199
+ if (colorString.length === 3) {
2200
+ colorString = [...colorString].map((character) => character + character).join("");
2201
+ }
2202
+ const integer = Number.parseInt(colorString, 16);
2203
+ return [
2204
+ integer >> 16 & 255,
2205
+ integer >> 8 & 255,
2206
+ integer & 255
2207
+ ];
2208
+ },
2209
+ enumerable: false
2210
+ },
2211
+ hexToAnsi256: {
2212
+ value: (hex) => styles.rgbToAnsi256(...styles.hexToRgb(hex)),
2213
+ enumerable: false
2214
+ },
2215
+ ansi256ToAnsi: {
2216
+ value(code) {
2217
+ if (code < 8) {
2218
+ return 30 + code;
2219
+ }
2220
+ if (code < 16) {
2221
+ return 90 + (code - 8);
2222
+ }
2223
+ let red;
2224
+ let green;
2225
+ let blue;
2226
+ if (code >= 232) {
2227
+ red = ((code - 232) * 10 + 8) / 255;
2228
+ green = red;
2229
+ blue = red;
2230
+ } else {
2231
+ code -= 16;
2232
+ const remainder = code % 36;
2233
+ red = Math.floor(code / 36) / 5;
2234
+ green = Math.floor(remainder / 6) / 5;
2235
+ blue = remainder % 6 / 5;
2236
+ }
2237
+ const value = Math.max(red, green, blue) * 2;
2238
+ if (value === 0) {
2239
+ return 30;
2240
+ }
2241
+ let result = 30 + (Math.round(blue) << 2 | Math.round(green) << 1 | Math.round(red));
2242
+ if (value === 2) {
2243
+ result += 60;
2244
+ }
2245
+ return result;
2246
+ },
2247
+ enumerable: false
2248
+ },
2249
+ rgbToAnsi: {
2250
+ value: (red, green, blue) => styles.ansi256ToAnsi(styles.rgbToAnsi256(red, green, blue)),
2251
+ enumerable: false
2252
+ },
2253
+ hexToAnsi: {
2254
+ value: (hex) => styles.ansi256ToAnsi(styles.hexToAnsi256(hex)),
2255
+ enumerable: false
2256
+ }
2257
+ });
2258
+ return styles;
2259
+ }
2260
+ var ansiStyles = assembleStyles();
2261
+ var ansi_styles_default = ansiStyles;
2262
+
2263
+ // node_modules/chalk/source/vendor/supports-color/index.js
2264
+ import process2 from "process";
2265
+ import os from "os";
2266
+ import tty from "tty";
2267
+ function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : process2.argv) {
2268
+ const prefix = flag.startsWith("-") ? "" : flag.length === 1 ? "-" : "--";
2269
+ const position = argv.indexOf(prefix + flag);
2270
+ const terminatorPosition = argv.indexOf("--");
2271
+ return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
2272
+ }
2273
+ var { env } = process2;
2274
+ var flagForceColor;
2275
+ if (hasFlag("no-color") || hasFlag("no-colors") || hasFlag("color=false") || hasFlag("color=never")) {
2276
+ flagForceColor = 0;
2277
+ } else if (hasFlag("color") || hasFlag("colors") || hasFlag("color=true") || hasFlag("color=always")) {
2278
+ flagForceColor = 1;
2279
+ }
2280
+ function envForceColor() {
2281
+ if ("FORCE_COLOR" in env) {
2282
+ if (env.FORCE_COLOR === "true") {
2283
+ return 1;
2284
+ }
2285
+ if (env.FORCE_COLOR === "false") {
2286
+ return 0;
2287
+ }
2288
+ return env.FORCE_COLOR.length === 0 ? 1 : Math.min(Number.parseInt(env.FORCE_COLOR, 10), 3);
2289
+ }
2290
+ }
2291
+ function translateLevel(level) {
2292
+ if (level === 0) {
2293
+ return false;
2294
+ }
2295
+ return {
2296
+ level,
2297
+ hasBasic: true,
2298
+ has256: level >= 2,
2299
+ has16m: level >= 3
2300
+ };
2301
+ }
2302
+ function _supportsColor(haveStream, { streamIsTTY, sniffFlags = true } = {}) {
2303
+ const noFlagForceColor = envForceColor();
2304
+ if (noFlagForceColor !== undefined) {
2305
+ flagForceColor = noFlagForceColor;
2306
+ }
2307
+ const forceColor = sniffFlags ? flagForceColor : noFlagForceColor;
2308
+ if (forceColor === 0) {
2309
+ return 0;
2310
+ }
2311
+ if (sniffFlags) {
2312
+ if (hasFlag("color=16m") || hasFlag("color=full") || hasFlag("color=truecolor")) {
2313
+ return 3;
2314
+ }
2315
+ if (hasFlag("color=256")) {
2316
+ return 2;
2317
+ }
2318
+ }
2319
+ if ("TF_BUILD" in env && "AGENT_NAME" in env) {
2320
+ return 1;
2321
+ }
2322
+ if (haveStream && !streamIsTTY && forceColor === undefined) {
2323
+ return 0;
2324
+ }
2325
+ const min = forceColor || 0;
2326
+ if (env.TERM === "dumb") {
2327
+ return min;
2328
+ }
2329
+ if (process2.platform === "win32") {
2330
+ const osRelease = os.release().split(".");
2331
+ if (Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) {
2332
+ return Number(osRelease[2]) >= 14931 ? 3 : 2;
2333
+ }
2334
+ return 1;
2335
+ }
2336
+ if ("CI" in env) {
2337
+ if (["GITHUB_ACTIONS", "GITEA_ACTIONS", "CIRCLECI"].some((key) => (key in env))) {
2338
+ return 3;
2339
+ }
2340
+ if (["TRAVIS", "APPVEYOR", "GITLAB_CI", "BUILDKITE", "DRONE"].some((sign) => (sign in env)) || env.CI_NAME === "codeship") {
2341
+ return 1;
2342
+ }
2343
+ return min;
2344
+ }
2345
+ if ("TEAMCITY_VERSION" in env) {
2346
+ return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
2347
+ }
2348
+ if (env.COLORTERM === "truecolor") {
2349
+ return 3;
2350
+ }
2351
+ if (env.TERM === "xterm-kitty") {
2352
+ return 3;
2353
+ }
2354
+ if (env.TERM === "xterm-ghostty") {
2355
+ return 3;
2356
+ }
2357
+ if (env.TERM === "wezterm") {
2358
+ return 3;
2359
+ }
2360
+ if ("TERM_PROGRAM" in env) {
2361
+ const version = Number.parseInt((env.TERM_PROGRAM_VERSION || "").split(".")[0], 10);
2362
+ switch (env.TERM_PROGRAM) {
2363
+ case "iTerm.app": {
2364
+ return version >= 3 ? 3 : 2;
2365
+ }
2366
+ case "Apple_Terminal": {
2367
+ return 2;
2368
+ }
2369
+ }
2370
+ }
2371
+ if (/-256(color)?$/i.test(env.TERM)) {
2372
+ return 2;
2373
+ }
2374
+ if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
2375
+ return 1;
2376
+ }
2377
+ if ("COLORTERM" in env) {
2378
+ return 1;
2379
+ }
2380
+ return min;
2381
+ }
2382
+ function createSupportsColor(stream, options = {}) {
2383
+ const level = _supportsColor(stream, {
2384
+ streamIsTTY: stream && stream.isTTY,
2385
+ ...options
2386
+ });
2387
+ return translateLevel(level);
2388
+ }
2389
+ var supportsColor = {
2390
+ stdout: createSupportsColor({ isTTY: tty.isatty(1) }),
2391
+ stderr: createSupportsColor({ isTTY: tty.isatty(2) })
2392
+ };
2393
+ var supports_color_default = supportsColor;
2394
+
2395
+ // node_modules/chalk/source/utilities.js
2396
+ function stringReplaceAll(string, substring, replacer) {
2397
+ let index = string.indexOf(substring);
2398
+ if (index === -1) {
2399
+ return string;
2400
+ }
2401
+ const substringLength = substring.length;
2402
+ let endIndex = 0;
2403
+ let returnValue = "";
2404
+ do {
2405
+ returnValue += string.slice(endIndex, index) + substring + replacer;
2406
+ endIndex = index + substringLength;
2407
+ index = string.indexOf(substring, endIndex);
2408
+ } while (index !== -1);
2409
+ returnValue += string.slice(endIndex);
2410
+ return returnValue;
2411
+ }
2412
+ function stringEncaseCRLFWithFirstIndex(string, prefix, postfix, index) {
2413
+ let endIndex = 0;
2414
+ let returnValue = "";
2415
+ do {
2416
+ const gotCR = string[index - 1] === "\r";
2417
+ returnValue += string.slice(endIndex, gotCR ? index - 1 : index) + prefix + (gotCR ? `\r
2418
+ ` : `
2419
+ `) + postfix;
2420
+ endIndex = index + 1;
2421
+ index = string.indexOf(`
2422
+ `, endIndex);
2423
+ } while (index !== -1);
2424
+ returnValue += string.slice(endIndex);
2425
+ return returnValue;
2426
+ }
2427
+
2428
+ // node_modules/chalk/source/index.js
2429
+ var { stdout: stdoutColor, stderr: stderrColor } = supports_color_default;
2430
+ var GENERATOR = Symbol("GENERATOR");
2431
+ var STYLER = Symbol("STYLER");
2432
+ var IS_EMPTY = Symbol("IS_EMPTY");
2433
+ var levelMapping = [
2434
+ "ansi",
2435
+ "ansi",
2436
+ "ansi256",
2437
+ "ansi16m"
2438
+ ];
2439
+ var styles2 = Object.create(null);
2440
+ var applyOptions = (object, options = {}) => {
2441
+ if (options.level && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) {
2442
+ throw new Error("The `level` option should be an integer from 0 to 3");
2443
+ }
2444
+ const colorLevel = stdoutColor ? stdoutColor.level : 0;
2445
+ object.level = options.level === undefined ? colorLevel : options.level;
2446
+ };
2447
+ var chalkFactory = (options) => {
2448
+ const chalk = (...strings) => strings.join(" ");
2449
+ applyOptions(chalk, options);
2450
+ Object.setPrototypeOf(chalk, createChalk.prototype);
2451
+ return chalk;
2452
+ };
2453
+ function createChalk(options) {
2454
+ return chalkFactory(options);
2455
+ }
2456
+ Object.setPrototypeOf(createChalk.prototype, Function.prototype);
2457
+ for (const [styleName, style] of Object.entries(ansi_styles_default)) {
2458
+ styles2[styleName] = {
2459
+ get() {
2460
+ const builder = createBuilder(this, createStyler(style.open, style.close, this[STYLER]), this[IS_EMPTY]);
2461
+ Object.defineProperty(this, styleName, { value: builder });
2462
+ return builder;
2463
+ }
2464
+ };
2465
+ }
2466
+ styles2.visible = {
2467
+ get() {
2468
+ const builder = createBuilder(this, this[STYLER], true);
2469
+ Object.defineProperty(this, "visible", { value: builder });
2470
+ return builder;
2471
+ }
2472
+ };
2473
+ var getModelAnsi = (model, level, type, ...arguments_) => {
2474
+ if (model === "rgb") {
2475
+ if (level === "ansi16m") {
2476
+ return ansi_styles_default[type].ansi16m(...arguments_);
2477
+ }
2478
+ if (level === "ansi256") {
2479
+ return ansi_styles_default[type].ansi256(ansi_styles_default.rgbToAnsi256(...arguments_));
2480
+ }
2481
+ return ansi_styles_default[type].ansi(ansi_styles_default.rgbToAnsi(...arguments_));
2482
+ }
2483
+ if (model === "hex") {
2484
+ return getModelAnsi("rgb", level, type, ...ansi_styles_default.hexToRgb(...arguments_));
2485
+ }
2486
+ return ansi_styles_default[type][model](...arguments_);
2487
+ };
2488
+ var usedModels = ["rgb", "hex", "ansi256"];
2489
+ for (const model of usedModels) {
2490
+ styles2[model] = {
2491
+ get() {
2492
+ const { level } = this;
2493
+ return function(...arguments_) {
2494
+ const styler = createStyler(getModelAnsi(model, levelMapping[level], "color", ...arguments_), ansi_styles_default.color.close, this[STYLER]);
2495
+ return createBuilder(this, styler, this[IS_EMPTY]);
2496
+ };
2497
+ }
2498
+ };
2499
+ const bgModel = "bg" + model[0].toUpperCase() + model.slice(1);
2500
+ styles2[bgModel] = {
2501
+ get() {
2502
+ const { level } = this;
2503
+ return function(...arguments_) {
2504
+ const styler = createStyler(getModelAnsi(model, levelMapping[level], "bgColor", ...arguments_), ansi_styles_default.bgColor.close, this[STYLER]);
2505
+ return createBuilder(this, styler, this[IS_EMPTY]);
2506
+ };
2507
+ }
2508
+ };
2509
+ }
2510
+ var proto = Object.defineProperties(() => {}, {
2511
+ ...styles2,
2512
+ level: {
2513
+ enumerable: true,
2514
+ get() {
2515
+ return this[GENERATOR].level;
2516
+ },
2517
+ set(level) {
2518
+ this[GENERATOR].level = level;
2519
+ }
2520
+ }
2521
+ });
2522
+ var createStyler = (open, close, parent) => {
2523
+ let openAll;
2524
+ let closeAll;
2525
+ if (parent === undefined) {
2526
+ openAll = open;
2527
+ closeAll = close;
2528
+ } else {
2529
+ openAll = parent.openAll + open;
2530
+ closeAll = close + parent.closeAll;
2531
+ }
2532
+ return {
2533
+ open,
2534
+ close,
2535
+ openAll,
2536
+ closeAll,
2537
+ parent
2538
+ };
2539
+ };
2540
+ var createBuilder = (self, _styler, _isEmpty) => {
2541
+ const builder = (...arguments_) => applyStyle(builder, arguments_.length === 1 ? "" + arguments_[0] : arguments_.join(" "));
2542
+ Object.setPrototypeOf(builder, proto);
2543
+ builder[GENERATOR] = self;
2544
+ builder[STYLER] = _styler;
2545
+ builder[IS_EMPTY] = _isEmpty;
2546
+ return builder;
2547
+ };
2548
+ var applyStyle = (self, string) => {
2549
+ if (self.level <= 0 || !string) {
2550
+ return self[IS_EMPTY] ? "" : string;
2551
+ }
2552
+ let styler = self[STYLER];
2553
+ if (styler === undefined) {
2554
+ return string;
2555
+ }
2556
+ const { openAll, closeAll } = styler;
2557
+ if (string.includes("\x1B")) {
2558
+ while (styler !== undefined) {
2559
+ string = stringReplaceAll(string, styler.close, styler.open);
2560
+ styler = styler.parent;
2561
+ }
2562
+ }
2563
+ const lfIndex = string.indexOf(`
2564
+ `);
2565
+ if (lfIndex !== -1) {
2566
+ string = stringEncaseCRLFWithFirstIndex(string, closeAll, openAll, lfIndex);
2567
+ }
2568
+ return openAll + string + closeAll;
2569
+ };
2570
+ Object.defineProperties(createChalk.prototype, styles2);
2571
+ var chalk = createChalk();
2572
+ var chalkStderr = createChalk({ level: stderrColor ? stderrColor.level : 0 });
2573
+ var source_default = chalk;
2574
+
2575
+ // src/cli/index.ts
2576
+ import { existsSync } from "fs";
2577
+
2578
+ // src/checks.ts
2579
+ import net from "net";
2580
+ async function runMonitorCheck(monitor, options = {}) {
2581
+ if (!monitor.enabled) {
2582
+ return { status: "down", latencyMs: null, error: "monitor is disabled" };
2583
+ }
2584
+ if (monitor.kind === "http")
2585
+ return runHttpCheck(monitor, options.fetch ?? fetch);
2586
+ return runTcpCheck(monitor);
2587
+ }
2588
+ async function runHttpCheck(monitor, fetchImpl = fetch) {
2589
+ if (!monitor.url)
2590
+ return { status: "down", latencyMs: null, error: "missing url" };
2591
+ const controller = new AbortController;
2592
+ const timeout = setTimeout(() => controller.abort(), monitor.timeoutMs);
2593
+ const started = performance.now();
2594
+ try {
2595
+ const response = await fetchImpl(monitor.url, {
2596
+ method: monitor.method || "GET",
2597
+ redirect: "manual",
2598
+ signal: controller.signal
2599
+ });
2600
+ const latencyMs = Math.round((performance.now() - started) * 100) / 100;
2601
+ const ok = monitor.expectedStatus == null ? response.status >= 200 && response.status < 400 : response.status === monitor.expectedStatus;
2602
+ return {
2603
+ status: ok ? "up" : "down",
2604
+ latencyMs,
2605
+ statusCode: response.status,
2606
+ error: ok ? null : `unexpected status ${response.status}`
2607
+ };
2608
+ } catch (error) {
2609
+ return {
2610
+ status: "down",
2611
+ latencyMs: Math.round((performance.now() - started) * 100) / 100,
2612
+ statusCode: null,
2613
+ error: error instanceof Error ? error.message : String(error)
2614
+ };
2615
+ } finally {
2616
+ clearTimeout(timeout);
2617
+ }
2618
+ }
2619
+ async function runTcpCheck(monitor) {
2620
+ if (!monitor.host || !monitor.port)
2621
+ return { status: "down", latencyMs: null, error: "missing host or port" };
2622
+ const started = performance.now();
2623
+ return new Promise((resolve) => {
2624
+ const socket = net.createConnection({ host: monitor.host, port: monitor.port, timeout: monitor.timeoutMs });
2625
+ let settled = false;
2626
+ const finish = (result) => {
2627
+ if (settled)
2628
+ return;
2629
+ settled = true;
2630
+ socket.destroy();
2631
+ resolve(result);
2632
+ };
2633
+ socket.once("connect", () => {
2634
+ finish({ status: "up", latencyMs: Math.round((performance.now() - started) * 100) / 100, statusCode: null, error: null });
2635
+ });
2636
+ socket.once("timeout", () => {
2637
+ finish({ status: "down", latencyMs: Math.round((performance.now() - started) * 100) / 100, statusCode: null, error: "tcp timeout" });
2638
+ });
2639
+ socket.once("error", (error) => {
2640
+ finish({ status: "down", latencyMs: Math.round((performance.now() - started) * 100) / 100, statusCode: null, error: error.message });
2641
+ });
2642
+ });
2643
+ }
2644
+
2645
+ // src/store.ts
2646
+ import { mkdirSync as mkdirSync2 } from "fs";
2647
+ import { dirname } from "path";
2648
+ import { randomUUID } from "crypto";
2649
+ import { Database } from "bun:sqlite";
2650
+
2651
+ // src/paths.ts
2652
+ import { mkdirSync } from "fs";
2653
+ import { homedir } from "os";
2654
+ import { join } from "path";
2655
+ function uptimeHome() {
2656
+ return process.env.HASNA_UPTIME_HOME || join(homedir(), ".hasna", "uptime");
2657
+ }
2658
+ function uptimeDbPath() {
2659
+ return process.env.HASNA_UPTIME_DB || join(uptimeHome(), "uptime.db");
2660
+ }
2661
+ function ensureUptimeHome() {
2662
+ const home = uptimeHome();
2663
+ mkdirSync(home, { recursive: true });
2664
+ return home;
2665
+ }
2666
+
2667
+ // src/limits.ts
2668
+ var MIN_INTERVAL_SECONDS = 1;
2669
+ var MAX_INTERVAL_SECONDS = 86400;
2670
+ var MIN_TIMEOUT_MS = 1;
2671
+ var MAX_TIMEOUT_MS = 60000;
2672
+ var MIN_RETRY_COUNT = 0;
2673
+ var MAX_RETRY_COUNT = 10;
2674
+ var MAX_RESULT_LIMIT = 1000;
2675
+
2676
+ // src/store.ts
2677
+ class UptimeStore {
2678
+ dbPath;
2679
+ db;
2680
+ constructor(options = {}) {
2681
+ this.dbPath = options.dbPath ?? uptimeDbPath();
2682
+ if (this.dbPath !== ":memory:") {
2683
+ mkdirSync2(dirname(this.dbPath), { recursive: true });
2684
+ }
2685
+ this.db = new Database(this.dbPath, { create: true });
2686
+ this.db.run("PRAGMA journal_mode = WAL");
2687
+ this.db.run("PRAGMA foreign_keys = ON");
2688
+ this.migrate();
2689
+ }
2690
+ close() {
2691
+ this.db.close();
2692
+ }
2693
+ migrate() {
2694
+ this.db.run(`
2695
+ CREATE TABLE IF NOT EXISTS monitors (
2696
+ id TEXT PRIMARY KEY,
2697
+ name TEXT NOT NULL UNIQUE,
2698
+ kind TEXT NOT NULL CHECK (kind IN ('http', 'tcp')),
2699
+ url TEXT,
2700
+ host TEXT,
2701
+ port INTEGER,
2702
+ method TEXT NOT NULL DEFAULT 'GET',
2703
+ expected_status INTEGER,
2704
+ interval_seconds INTEGER NOT NULL DEFAULT 60,
2705
+ timeout_ms INTEGER NOT NULL DEFAULT 5000,
2706
+ retry_count INTEGER NOT NULL DEFAULT 0,
2707
+ enabled INTEGER NOT NULL DEFAULT 1,
2708
+ status TEXT NOT NULL DEFAULT 'unknown',
2709
+ last_checked_at TEXT,
2710
+ created_at TEXT NOT NULL,
2711
+ updated_at TEXT NOT NULL
2712
+ )
2713
+ `);
2714
+ this.db.run(`
2715
+ CREATE TABLE IF NOT EXISTS check_results (
2716
+ id TEXT PRIMARY KEY,
2717
+ monitor_id TEXT NOT NULL REFERENCES monitors(id) ON DELETE CASCADE,
2718
+ checked_at TEXT NOT NULL,
2719
+ status TEXT NOT NULL CHECK (status IN ('up', 'down')),
2720
+ latency_ms REAL,
2721
+ status_code INTEGER,
2722
+ error TEXT,
2723
+ attempt_count INTEGER NOT NULL DEFAULT 1
2724
+ )
2725
+ `);
2726
+ this.db.run(`
2727
+ CREATE TABLE IF NOT EXISTS incidents (
2728
+ id TEXT PRIMARY KEY,
2729
+ monitor_id TEXT NOT NULL REFERENCES monitors(id) ON DELETE CASCADE,
2730
+ status TEXT NOT NULL CHECK (status IN ('open', 'closed')),
2731
+ opened_at TEXT NOT NULL,
2732
+ closed_at TEXT,
2733
+ last_failure_at TEXT NOT NULL,
2734
+ failure_count INTEGER NOT NULL DEFAULT 1,
2735
+ recovery_check_id TEXT,
2736
+ reason TEXT
2737
+ )
2738
+ `);
2739
+ this.db.run("CREATE INDEX IF NOT EXISTS idx_results_monitor_time ON check_results(monitor_id, checked_at DESC)");
2740
+ this.db.run("CREATE INDEX IF NOT EXISTS idx_incidents_monitor_status ON incidents(monitor_id, status)");
2741
+ }
2742
+ createMonitor(input) {
2743
+ const normalized = normalizeCreateMonitor(input);
2744
+ const now = new Date().toISOString();
2745
+ const monitor = {
2746
+ id: newId("mon"),
2747
+ name: normalized.name,
2748
+ kind: normalized.kind,
2749
+ url: normalized.url ?? null,
2750
+ host: normalized.host ?? null,
2751
+ port: normalized.port ?? null,
2752
+ method: normalized.method ?? "GET",
2753
+ expectedStatus: normalized.expectedStatus ?? null,
2754
+ intervalSeconds: normalized.intervalSeconds ?? 60,
2755
+ timeoutMs: normalized.timeoutMs ?? 5000,
2756
+ retryCount: normalized.retryCount ?? 0,
2757
+ enabled: normalized.enabled ?? true,
2758
+ status: normalized.enabled === false ? "paused" : "unknown",
2759
+ lastCheckedAt: null,
2760
+ createdAt: now,
2761
+ updatedAt: now
2762
+ };
2763
+ this.db.query(`INSERT INTO monitors (
2764
+ id, name, kind, url, host, port, method, expected_status,
2765
+ interval_seconds, timeout_ms, retry_count, enabled, status,
2766
+ last_checked_at, created_at, updated_at
2767
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(monitor.id, monitor.name, monitor.kind, monitor.url, monitor.host, monitor.port, monitor.method, monitor.expectedStatus, monitor.intervalSeconds, monitor.timeoutMs, monitor.retryCount, monitor.enabled ? 1 : 0, monitor.status, monitor.lastCheckedAt, monitor.createdAt, monitor.updatedAt);
2768
+ return monitor;
2769
+ }
2770
+ listMonitors(options = {}) {
2771
+ const rows = options.includeDisabled ? this.db.query("SELECT * FROM monitors ORDER BY name ASC").all() : this.db.query("SELECT * FROM monitors WHERE enabled = 1 ORDER BY name ASC").all();
2772
+ return rows.map(monitorFromRow);
2773
+ }
2774
+ getMonitor(idOrName) {
2775
+ const row = this.db.query("SELECT * FROM monitors WHERE id = ? OR name = ?").get(idOrName, idOrName);
2776
+ return row ? monitorFromRow(row) : null;
2777
+ }
2778
+ updateMonitor(idOrName, input) {
2779
+ const current = this.getMonitor(idOrName);
2780
+ if (!current)
2781
+ throw new Error(`Monitor not found: ${idOrName}`);
2782
+ const updatedAt = new Date().toISOString();
2783
+ const next = normalizeUpdateMonitor(current, input, updatedAt);
2784
+ this.db.query(`UPDATE monitors SET
2785
+ name = ?, kind = ?, url = ?, host = ?, port = ?, method = ?,
2786
+ expected_status = ?, interval_seconds = ?, timeout_ms = ?,
2787
+ retry_count = ?, enabled = ?, status = ?, last_checked_at = ?, updated_at = ?
2788
+ WHERE id = ?`).run(next.name, next.kind, next.url, next.host, next.port, next.method, next.expectedStatus, next.intervalSeconds, next.timeoutMs, next.retryCount, next.enabled ? 1 : 0, next.status, next.lastCheckedAt, updatedAt, current.id);
2789
+ return this.getMonitor(current.id);
2790
+ }
2791
+ deleteMonitor(idOrName) {
2792
+ const current = this.getMonitor(idOrName);
2793
+ if (!current)
2794
+ return false;
2795
+ this.db.query("DELETE FROM monitors WHERE id = ?").run(current.id);
2796
+ return true;
2797
+ }
2798
+ recordCheckResult(input) {
2799
+ const monitor = this.getMonitor(input.monitorId);
2800
+ if (!monitor)
2801
+ throw new Error(`Monitor not found: ${input.monitorId}`);
2802
+ const checkedAt = input.checkedAt ?? new Date().toISOString();
2803
+ const result = {
2804
+ id: newId("chk"),
2805
+ monitorId: monitor.id,
2806
+ checkedAt,
2807
+ status: input.status,
2808
+ latencyMs: input.latencyMs,
2809
+ statusCode: input.statusCode,
2810
+ error: input.error,
2811
+ attemptCount: Math.max(1, input.attemptCount)
2812
+ };
2813
+ const tx = this.db.transaction(() => {
2814
+ this.db.query(`INSERT INTO check_results (
2815
+ id, monitor_id, checked_at, status, latency_ms, status_code, error, attempt_count
2816
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`).run(result.id, result.monitorId, result.checkedAt, result.status, result.latencyMs, result.statusCode, result.error, result.attemptCount);
2817
+ this.db.query("UPDATE monitors SET status = ?, last_checked_at = ?, updated_at = ? WHERE id = ?").run(result.status, result.checkedAt, result.checkedAt, result.monitorId);
2818
+ this.reconcileIncidentInTransaction(result);
2819
+ });
2820
+ tx();
2821
+ return result;
2822
+ }
2823
+ listResults(options = {}) {
2824
+ const limit = clampLimit(options.limit ?? 50);
2825
+ const rows = options.monitorId ? this.db.query("SELECT * FROM check_results WHERE monitor_id = ? ORDER BY checked_at DESC LIMIT ?").all(options.monitorId, limit) : this.db.query("SELECT * FROM check_results ORDER BY checked_at DESC LIMIT ?").all(limit);
2826
+ return rows.map(checkResultFromRow);
2827
+ }
2828
+ listIncidents(options = {}) {
2829
+ const clauses = [];
2830
+ const args = [];
2831
+ if (options.status) {
2832
+ clauses.push("status = ?");
2833
+ args.push(options.status);
2834
+ }
2835
+ if (options.monitorId) {
2836
+ clauses.push("monitor_id = ?");
2837
+ args.push(options.monitorId);
2838
+ }
2839
+ const where = clauses.length ? `WHERE ${clauses.join(" AND ")}` : "";
2840
+ args.push(clampLimit(options.limit ?? 50));
2841
+ const rows = this.db.query(`SELECT * FROM incidents ${where} ORDER BY opened_at DESC LIMIT ?`).all(...args);
2842
+ return rows.map(incidentFromRow);
2843
+ }
2844
+ getOpenIncident(monitorId) {
2845
+ const row = this.db.query("SELECT * FROM incidents WHERE monitor_id = ? AND status = 'open' ORDER BY opened_at DESC LIMIT 1").get(monitorId);
2846
+ return row ? incidentFromRow(row) : null;
2847
+ }
2848
+ summary() {
2849
+ const monitors = this.listMonitors({ includeDisabled: true });
2850
+ const summaries = monitors.map((monitor) => this.monitorSummary(monitor));
2851
+ return {
2852
+ generatedAt: new Date().toISOString(),
2853
+ monitors: summaries,
2854
+ totals: {
2855
+ monitors: monitors.length,
2856
+ enabled: monitors.filter((m) => m.enabled).length,
2857
+ up: monitors.filter((m) => m.status === "up").length,
2858
+ down: monitors.filter((m) => m.status === "down").length,
2859
+ paused: monitors.filter((m) => !m.enabled || m.status === "paused").length,
2860
+ unknown: monitors.filter((m) => m.status === "unknown").length,
2861
+ openIncidents: this.listIncidents({ status: "open", limit: 1000 }).length
2862
+ }
2863
+ };
2864
+ }
2865
+ monitorSummary(monitor) {
2866
+ const row = this.db.query(`SELECT
2867
+ COUNT(*) as total,
2868
+ SUM(CASE WHEN status = 'up' THEN 1 ELSE 0 END) as up_count,
2869
+ SUM(CASE WHEN status = 'down' THEN 1 ELSE 0 END) as down_count,
2870
+ AVG(CASE WHEN status = 'up' THEN latency_ms ELSE NULL END) as avg_latency
2871
+ FROM check_results WHERE monitor_id = ?`).get(monitor.id);
2872
+ const total = Number(row.total ?? 0);
2873
+ const up = Number(row.up_count ?? 0);
2874
+ const down = Number(row.down_count ?? 0);
2875
+ return {
2876
+ monitor,
2877
+ totalChecks: total,
2878
+ upChecks: up,
2879
+ downChecks: down,
2880
+ uptimePercent: total > 0 ? round(up / total * 100, 4) : null,
2881
+ averageLatencyMs: row.avg_latency == null ? null : round(row.avg_latency, 2),
2882
+ openIncident: this.getOpenIncident(monitor.id)
2883
+ };
2884
+ }
2885
+ reconcileIncidentInTransaction(result) {
2886
+ const open = this.db.query("SELECT * FROM incidents WHERE monitor_id = ? AND status = 'open' ORDER BY opened_at DESC LIMIT 1").get(result.monitorId);
2887
+ if (result.status === "down") {
2888
+ if (open) {
2889
+ this.db.query("UPDATE incidents SET last_failure_at = ?, failure_count = failure_count + 1, reason = COALESCE(?, reason) WHERE id = ?").run(result.checkedAt, result.error, open.id);
2890
+ } else {
2891
+ this.db.query(`INSERT INTO incidents (
2892
+ id, monitor_id, status, opened_at, closed_at, last_failure_at,
2893
+ failure_count, recovery_check_id, reason
2894
+ ) VALUES (?, ?, 'open', ?, NULL, ?, 1, NULL, ?)`).run(newId("inc"), result.monitorId, result.checkedAt, result.checkedAt, result.error);
2895
+ }
2896
+ return;
2897
+ }
2898
+ if (open) {
2899
+ this.db.query("UPDATE incidents SET status = 'closed', closed_at = ?, recovery_check_id = ? WHERE id = ?").run(result.checkedAt, result.id, open.id);
2900
+ }
2901
+ }
2902
+ }
2903
+ function normalizeCreateMonitor(input) {
2904
+ const name = input.name?.trim();
2905
+ if (!name)
2906
+ throw new Error("Monitor name is required");
2907
+ const method = normalizeMethod(input.method ?? "GET");
2908
+ const expectedStatus = normalizeExpectedStatus(input.expectedStatus);
2909
+ if (input.kind === "http") {
2910
+ const url = normalizeHttpUrl(input.url);
2911
+ return {
2912
+ name,
2913
+ kind: input.kind,
2914
+ url,
2915
+ method,
2916
+ expectedStatus,
2917
+ intervalSeconds: boundedInteger(input.intervalSeconds ?? 60, "intervalSeconds", MIN_INTERVAL_SECONDS, MAX_INTERVAL_SECONDS),
2918
+ timeoutMs: boundedInteger(input.timeoutMs ?? 5000, "timeoutMs", MIN_TIMEOUT_MS, MAX_TIMEOUT_MS),
2919
+ retryCount: boundedInteger(input.retryCount ?? 0, "retryCount", MIN_RETRY_COUNT, MAX_RETRY_COUNT),
2920
+ enabled: input.enabled ?? true
2921
+ };
2922
+ } else if (input.kind === "tcp") {
2923
+ const host = input.host?.trim();
2924
+ if (!host)
2925
+ throw new Error("TCP monitors require host");
2926
+ if (!Number.isInteger(input.port) || input.port <= 0 || input.port > 65535) {
2927
+ throw new Error("TCP monitors require a port from 1 to 65535");
2928
+ }
2929
+ return {
2930
+ name,
2931
+ kind: input.kind,
2932
+ host,
2933
+ port: input.port,
2934
+ method,
2935
+ expectedStatus: null,
2936
+ intervalSeconds: boundedInteger(input.intervalSeconds ?? 60, "intervalSeconds", MIN_INTERVAL_SECONDS, MAX_INTERVAL_SECONDS),
2937
+ timeoutMs: boundedInteger(input.timeoutMs ?? 5000, "timeoutMs", MIN_TIMEOUT_MS, MAX_TIMEOUT_MS),
2938
+ retryCount: boundedInteger(input.retryCount ?? 0, "retryCount", MIN_RETRY_COUNT, MAX_RETRY_COUNT),
2939
+ enabled: input.enabled ?? true
2940
+ };
2941
+ } else {
2942
+ throw new Error("Monitor kind must be http or tcp");
2943
+ }
2944
+ }
2945
+ function normalizeUpdateMonitor(current, input, updatedAt) {
2946
+ const merged = {
2947
+ ...current,
2948
+ ...input,
2949
+ expectedStatus: input.expectedStatus === undefined ? current.expectedStatus : input.expectedStatus,
2950
+ updatedAt
2951
+ };
2952
+ const normalized = normalizeCreateMonitor({
2953
+ name: merged.name,
2954
+ kind: merged.kind,
2955
+ url: merged.url ?? undefined,
2956
+ host: merged.host ?? undefined,
2957
+ port: merged.port ?? undefined,
2958
+ method: merged.method,
2959
+ expectedStatus: merged.expectedStatus,
2960
+ intervalSeconds: merged.intervalSeconds,
2961
+ timeoutMs: merged.timeoutMs,
2962
+ retryCount: merged.retryCount,
2963
+ enabled: merged.enabled
2964
+ });
2965
+ const checkDefinitionChanged = normalized.kind !== current.kind || (normalized.url ?? null) !== current.url || (normalized.host ?? null) !== current.host || (normalized.port ?? null) !== current.port || normalized.method !== current.method || normalized.expectedStatus !== current.expectedStatus;
2966
+ const status = normalized.enabled ? checkDefinitionChanged || !current.enabled ? "unknown" : current.status : "paused";
2967
+ return {
2968
+ ...current,
2969
+ name: normalized.name,
2970
+ kind: normalized.kind,
2971
+ url: normalized.url ?? null,
2972
+ host: normalized.host ?? null,
2973
+ port: normalized.port ?? null,
2974
+ method: normalized.method,
2975
+ expectedStatus: normalized.expectedStatus,
2976
+ intervalSeconds: normalized.intervalSeconds,
2977
+ timeoutMs: normalized.timeoutMs,
2978
+ retryCount: normalized.retryCount,
2979
+ enabled: normalized.enabled,
2980
+ status,
2981
+ lastCheckedAt: checkDefinitionChanged ? null : current.lastCheckedAt,
2982
+ updatedAt
2983
+ };
2984
+ }
2985
+ function normalizeHttpUrl(value) {
2986
+ const raw = value?.trim();
2987
+ if (!raw)
2988
+ throw new Error("HTTP monitors require url");
2989
+ const parsed = new URL(raw);
2990
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
2991
+ throw new Error("HTTP monitor url must use http or https");
2992
+ }
2993
+ return parsed.toString();
2994
+ }
2995
+ function normalizeMethod(value) {
2996
+ const method = value.trim().toUpperCase();
2997
+ if (!/^[A-Z]+$/.test(method))
2998
+ throw new Error("HTTP method must contain only letters");
2999
+ return method;
3000
+ }
3001
+ function normalizeExpectedStatus(value) {
3002
+ if (value == null)
3003
+ return null;
3004
+ if (!Number.isInteger(value) || value < 100 || value > 599) {
3005
+ throw new Error("expectedStatus must be an HTTP status from 100 to 599");
3006
+ }
3007
+ return value;
3008
+ }
3009
+ function monitorFromRow(row) {
3010
+ return {
3011
+ id: row.id,
3012
+ name: row.name,
3013
+ kind: row.kind,
3014
+ url: row.url,
3015
+ host: row.host,
3016
+ port: row.port,
3017
+ method: row.method,
3018
+ expectedStatus: row.expected_status,
3019
+ intervalSeconds: row.interval_seconds,
3020
+ timeoutMs: row.timeout_ms,
3021
+ retryCount: row.retry_count,
3022
+ enabled: Boolean(row.enabled),
3023
+ status: row.status,
3024
+ lastCheckedAt: row.last_checked_at,
3025
+ createdAt: row.created_at,
3026
+ updatedAt: row.updated_at
3027
+ };
3028
+ }
3029
+ function checkResultFromRow(row) {
3030
+ return {
3031
+ id: row.id,
3032
+ monitorId: row.monitor_id,
3033
+ checkedAt: row.checked_at,
3034
+ status: row.status,
3035
+ latencyMs: row.latency_ms,
3036
+ statusCode: row.status_code,
3037
+ error: row.error,
3038
+ attemptCount: row.attempt_count
3039
+ };
3040
+ }
3041
+ function incidentFromRow(row) {
3042
+ return {
3043
+ id: row.id,
3044
+ monitorId: row.monitor_id,
3045
+ status: row.status,
3046
+ openedAt: row.opened_at,
3047
+ closedAt: row.closed_at,
3048
+ lastFailureAt: row.last_failure_at,
3049
+ failureCount: row.failure_count,
3050
+ recoveryCheckId: row.recovery_check_id,
3051
+ reason: row.reason
3052
+ };
3053
+ }
3054
+ function newId(prefix) {
3055
+ return `${prefix}_${randomUUID().replace(/-/g, "").slice(0, 18)}`;
3056
+ }
3057
+ function boundedInteger(value, label, min, max) {
3058
+ if (!Number.isInteger(value) || value < min || value > max) {
3059
+ throw new Error(`${label} must be an integer from ${min} to ${max}`);
3060
+ }
3061
+ return value;
3062
+ }
3063
+ function clampLimit(value) {
3064
+ if (!Number.isFinite(value))
3065
+ return 50;
3066
+ return Math.max(1, Math.min(Math.floor(value), MAX_RESULT_LIMIT));
3067
+ }
3068
+ function round(value, places) {
3069
+ const factor = 10 ** places;
3070
+ return Math.round(value * factor) / factor;
3071
+ }
3072
+
3073
+ // src/service.ts
3074
+ class UptimeService {
3075
+ store;
3076
+ checkRunner;
3077
+ inFlightChecks = new Set;
3078
+ constructor(options = {}) {
3079
+ this.store = options.store ?? new UptimeStore(options);
3080
+ this.checkRunner = options.checkRunner ?? runMonitorCheck;
3081
+ }
3082
+ close() {
3083
+ this.store.close();
3084
+ }
3085
+ createMonitor(input) {
3086
+ return this.store.createMonitor(input);
3087
+ }
3088
+ updateMonitor(idOrName, input) {
3089
+ return this.store.updateMonitor(idOrName, input);
3090
+ }
3091
+ deleteMonitor(idOrName) {
3092
+ return this.store.deleteMonitor(idOrName);
3093
+ }
3094
+ listMonitors(options = {}) {
3095
+ return this.store.listMonitors(options);
3096
+ }
3097
+ getMonitor(idOrName) {
3098
+ return this.store.getMonitor(idOrName);
3099
+ }
3100
+ listResults(options = {}) {
3101
+ return this.store.listResults(options);
3102
+ }
3103
+ listIncidents(options = {}) {
3104
+ return this.store.listIncidents(options);
3105
+ }
3106
+ summary() {
3107
+ return this.store.summary();
3108
+ }
3109
+ async checkMonitor(idOrName) {
3110
+ const monitor = this.store.getMonitor(idOrName);
3111
+ if (!monitor)
3112
+ throw new Error(`Monitor not found: ${idOrName}`);
3113
+ if (!monitor.enabled)
3114
+ throw new Error(`Monitor is disabled: ${monitor.name}`);
3115
+ if (this.inFlightChecks.has(monitor.id))
3116
+ throw new Error(`Monitor check already in progress: ${monitor.name}`);
3117
+ this.inFlightChecks.add(monitor.id);
3118
+ try {
3119
+ let attemptCount = 0;
3120
+ let last = null;
3121
+ const maxAttempts = Math.max(1, monitor.retryCount + 1);
3122
+ while (attemptCount < maxAttempts) {
3123
+ attemptCount += 1;
3124
+ last = await this.checkRunner(monitor);
3125
+ if (last.status === "up")
3126
+ break;
3127
+ }
3128
+ return this.store.recordCheckResult({
3129
+ monitorId: monitor.id,
3130
+ status: last.status,
3131
+ latencyMs: last.latencyMs,
3132
+ statusCode: last.statusCode ?? null,
3133
+ error: last.error ?? null,
3134
+ attemptCount
3135
+ });
3136
+ } finally {
3137
+ this.inFlightChecks.delete(monitor.id);
3138
+ }
3139
+ }
3140
+ async checkAll() {
3141
+ const monitors = this.store.listMonitors();
3142
+ const results = [];
3143
+ for (const monitor of monitors) {
3144
+ results.push(await this.checkMonitor(monitor.id));
3145
+ }
3146
+ return results;
3147
+ }
3148
+ startScheduler(options = {}) {
3149
+ const tickMs = options.tickMs ?? 1000;
3150
+ const timer = setInterval(() => {
3151
+ this.runDueChecks().catch((error) => {
3152
+ console.error(error instanceof Error ? error.message : String(error));
3153
+ });
3154
+ }, tickMs);
3155
+ return {
3156
+ stop: () => clearInterval(timer)
3157
+ };
3158
+ }
3159
+ async runDueChecks(now = new Date) {
3160
+ const due = this.store.listMonitors().filter((monitor) => this.isDue(monitor, now));
3161
+ const results = [];
3162
+ for (const monitor of due) {
3163
+ const current = this.store.getMonitor(monitor.id);
3164
+ if (!current || !this.isDue(current, now))
3165
+ continue;
3166
+ results.push(await this.checkMonitor(current.id));
3167
+ }
3168
+ return results;
3169
+ }
3170
+ isDue(monitor, now) {
3171
+ if (!monitor.enabled)
3172
+ return false;
3173
+ if (this.inFlightChecks.has(monitor.id))
3174
+ return false;
3175
+ if (!monitor.lastCheckedAt)
3176
+ return true;
3177
+ const last = new Date(monitor.lastCheckedAt).getTime();
3178
+ return now.getTime() - last >= monitor.intervalSeconds * 1000;
3179
+ }
3180
+ }
3181
+
3182
+ // src/version.ts
3183
+ import { readFileSync } from "fs";
3184
+ import { dirname as dirname2, join as join2 } from "path";
3185
+ import { fileURLToPath } from "url";
3186
+ function packageVersion() {
3187
+ const here = dirname2(fileURLToPath(import.meta.url));
3188
+ const candidates = [
3189
+ join2(here, "..", "package.json"),
3190
+ join2(here, "..", "..", "package.json")
3191
+ ];
3192
+ for (const candidate of candidates) {
3193
+ try {
3194
+ return JSON.parse(readFileSync(candidate, "utf8")).version ?? "0.0.0";
3195
+ } catch {}
3196
+ }
3197
+ return "0.0.0";
3198
+ }
3199
+
3200
+ // src/dashboard.ts
3201
+ function dashboardHtml() {
3202
+ return `<!doctype html>
3203
+ <html lang="en">
3204
+ <head>
3205
+ <meta charset="utf-8" />
3206
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
3207
+ <title>Open Uptime</title>
3208
+ <style>
3209
+ :root {
3210
+ color-scheme: light;
3211
+ --bg: #f7f8fb;
3212
+ --panel: #ffffff;
3213
+ --text: #17202a;
3214
+ --muted: #5f6b7a;
3215
+ --line: #d8dee8;
3216
+ --up: #157347;
3217
+ --down: #b42318;
3218
+ --warn: #9a6700;
3219
+ --accent: #2457c5;
3220
+ }
3221
+ * { box-sizing: border-box; }
3222
+ body {
3223
+ margin: 0;
3224
+ font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
3225
+ background: var(--bg);
3226
+ color: var(--text);
3227
+ }
3228
+ header {
3229
+ border-bottom: 1px solid var(--line);
3230
+ background: var(--panel);
3231
+ padding: 18px 24px;
3232
+ display: flex;
3233
+ justify-content: space-between;
3234
+ gap: 16px;
3235
+ align-items: center;
3236
+ }
3237
+ h1 { margin: 0; font-size: 20px; letter-spacing: 0; }
3238
+ main { padding: 24px; max-width: 1180px; margin: 0 auto; }
3239
+ .grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
3240
+ .panel {
3241
+ background: var(--panel);
3242
+ border: 1px solid var(--line);
3243
+ border-radius: 8px;
3244
+ padding: 16px;
3245
+ }
3246
+ .metric { font-size: 28px; font-weight: 700; margin-top: 6px; }
3247
+ .muted { color: var(--muted); font-size: 13px; }
3248
+ .toolbar { display: flex; gap: 8px; align-items: center; }
3249
+ .stack { display: grid; gap: 16px; margin-top: 16px; }
3250
+ .form-grid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); align-items: end; }
3251
+ label { display: grid; gap: 5px; color: var(--muted); font-size: 12px; font-weight: 700; text-transform: uppercase; }
3252
+ input, select {
3253
+ width: 100%;
3254
+ border: 1px solid var(--line);
3255
+ border-radius: 8px;
3256
+ padding: 9px 10px;
3257
+ background: white;
3258
+ color: var(--text);
3259
+ font: inherit;
3260
+ }
3261
+ button {
3262
+ border: 1px solid var(--line);
3263
+ border-radius: 8px;
3264
+ background: var(--panel);
3265
+ color: var(--text);
3266
+ padding: 8px 12px;
3267
+ cursor: pointer;
3268
+ font-weight: 600;
3269
+ }
3270
+ button.primary { background: var(--accent); color: white; border-color: var(--accent); }
3271
+ button.danger { color: var(--down); }
3272
+ table { width: 100%; border-collapse: collapse; margin-top: 16px; }
3273
+ th, td { text-align: left; border-bottom: 1px solid var(--line); padding: 11px 8px; vertical-align: top; }
3274
+ th { color: var(--muted); font-size: 12px; text-transform: uppercase; }
3275
+ .badge { display: inline-flex; min-width: 72px; justify-content: center; border-radius: 999px; padding: 4px 8px; font-size: 12px; font-weight: 700; }
3276
+ .up { background: #dcfce7; color: var(--up); }
3277
+ .down { background: #fee2e2; color: var(--down); }
3278
+ .paused { background: #fef3c7; color: var(--warn); }
3279
+ .unknown { background: #e5e7eb; color: #374151; }
3280
+ .row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
3281
+ @media (max-width: 760px) {
3282
+ header { align-items: flex-start; flex-direction: column; }
3283
+ main { padding: 16px; }
3284
+ table { display: block; overflow-x: auto; white-space: nowrap; }
3285
+ }
3286
+ </style>
3287
+ </head>
3288
+ <body>
3289
+ <header>
3290
+ <div>
3291
+ <h1>Open Uptime</h1>
3292
+ <div class="muted">Local uptime and downtime monitoring</div>
3293
+ </div>
3294
+ <div class="toolbar">
3295
+ <button id="check-all" class="primary">Run Checks</button>
3296
+ <button id="refresh">Refresh</button>
3297
+ </div>
3298
+ </header>
3299
+ <main>
3300
+ <section class="grid" id="metrics"></section>
3301
+ <section class="panel" style="margin-top:16px">
3302
+ <strong>Add Monitor</strong>
3303
+ <form id="monitor-form" class="form-grid">
3304
+ <label>Name<input id="form-name" required /></label>
3305
+ <label>Kind<select id="form-kind"><option value="http">HTTP</option><option value="tcp">TCP</option></select></label>
3306
+ <label>URL<input id="form-url" placeholder="https://example.com/health" /></label>
3307
+ <label>Host<input id="form-host" placeholder="127.0.0.1" /></label>
3308
+ <label>Port<input id="form-port" type="number" min="1" max="65535" /></label>
3309
+ <label>Interval<input id="form-interval" type="number" min="1" value="60" /></label>
3310
+ <label>Timeout<input id="form-timeout" type="number" min="1" value="5000" /></label>
3311
+ <button id="form-submit" class="primary" type="submit">Add</button>
3312
+ <button id="form-cancel" type="button">Cancel</button>
3313
+ </form>
3314
+ <div class="muted" id="form-status"></div>
3315
+ </section>
3316
+ <section class="panel" style="margin-top:16px">
3317
+ <div style="display:flex;justify-content:space-between;gap:12px;align-items:center">
3318
+ <div>
3319
+ <strong>Monitors</strong>
3320
+ <div class="muted" id="generated"></div>
3321
+ </div>
3322
+ </div>
3323
+ <table>
3324
+ <thead>
3325
+ <tr><th>Status</th><th>Name</th><th>Target</th><th>Uptime</th><th>Latency</th><th>Last Check</th><th>Incident</th><th></th></tr>
3326
+ </thead>
3327
+ <tbody id="monitors"></tbody>
3328
+ </table>
3329
+ </section>
3330
+ <section class="stack">
3331
+ <section class="panel">
3332
+ <strong>Recent Results</strong>
3333
+ <table>
3334
+ <thead><tr><th>Status</th><th>Monitor</th><th>Checked</th><th>Latency</th><th>Error</th></tr></thead>
3335
+ <tbody id="results"></tbody>
3336
+ </table>
3337
+ </section>
3338
+ <section class="panel">
3339
+ <strong>Incidents</strong>
3340
+ <table>
3341
+ <thead><tr><th>Status</th><th>Monitor</th><th>Opened</th><th>Closed</th><th>Failures</th><th>Reason</th></tr></thead>
3342
+ <tbody id="incidents"></tbody>
3343
+ </table>
3344
+ </section>
3345
+ </section>
3346
+ </main>
3347
+ <script>
3348
+ let monitorCache = [];
3349
+ let editingId = null;
3350
+ const fmt = (value) => value == null || value === '' ? '-' : String(value);
3351
+ const pct = (value) => value == null ? '-' : Number(value).toFixed(2) + '%';
3352
+ const byId = (id) => document.getElementById(id);
3353
+ const text = (value) => document.createTextNode(fmt(value));
3354
+ function clear(node) { while (node.firstChild) node.removeChild(node.firstChild); }
3355
+ function cell(value) {
3356
+ const td = document.createElement('td');
3357
+ td.appendChild(text(value));
3358
+ return td;
3359
+ }
3360
+ function statusCell(status) {
3361
+ const td = document.createElement('td');
3362
+ const span = document.createElement('span');
3363
+ const safe = ['up', 'down', 'paused', 'unknown'].includes(status) ? status : 'unknown';
3364
+ span.className = 'badge ' + safe;
3365
+ span.textContent = status || 'unknown';
3366
+ td.appendChild(span);
3367
+ return td;
3368
+ }
3369
+ function button(label, handler, className) {
3370
+ const btn = document.createElement('button');
3371
+ btn.type = 'button';
3372
+ btn.textContent = label;
3373
+ if (className) btn.className = className;
3374
+ btn.addEventListener('click', handler);
3375
+ return btn;
3376
+ }
3377
+ async function load() {
3378
+ const [summary, results, incidents] = await Promise.all([
3379
+ fetch('/api/summary').then((r) => r.json()),
3380
+ fetch('/api/results?limit=20').then((r) => r.json()),
3381
+ fetch('/api/incidents?limit=20').then((r) => r.json()),
3382
+ ]);
3383
+ monitorCache = summary.monitors.map((item) => item.monitor);
3384
+ byId('generated').textContent = 'Generated ' + new Date(summary.generatedAt).toLocaleString();
3385
+ renderMetrics(summary);
3386
+ renderMonitors(summary);
3387
+ renderResults(results);
3388
+ renderIncidents(incidents);
3389
+ }
3390
+ function renderMetrics(summary) {
3391
+ const root = byId('metrics');
3392
+ clear(root);
3393
+ for (const [label, value] of [
3394
+ ['Monitors', summary.totals.monitors],
3395
+ ['Up', summary.totals.up],
3396
+ ['Down', summary.totals.down],
3397
+ ['Open incidents', summary.totals.openIncidents],
3398
+ ]) {
3399
+ const panel = document.createElement('div');
3400
+ panel.className = 'panel';
3401
+ const small = document.createElement('div');
3402
+ small.className = 'muted';
3403
+ small.textContent = label;
3404
+ const metric = document.createElement('div');
3405
+ metric.className = 'metric';
3406
+ metric.textContent = value;
3407
+ panel.append(small, metric);
3408
+ root.appendChild(panel);
3409
+ }
3410
+ }
3411
+ function renderMonitors(summary) {
3412
+ const root = byId('monitors');
3413
+ clear(root);
3414
+ for (const item of summary.monitors) {
3415
+ const m = item.monitor;
3416
+ const target = m.kind === 'http' ? m.url : m.host + ':' + m.port;
3417
+ const incident = item.openIncident ? 'open since ' + new Date(item.openIncident.openedAt).toLocaleString() : '-';
3418
+ const tr = document.createElement('tr');
3419
+ const name = document.createElement('td');
3420
+ const strong = document.createElement('strong');
3421
+ strong.textContent = m.name;
3422
+ const kind = document.createElement('div');
3423
+ kind.className = 'muted';
3424
+ kind.textContent = m.kind;
3425
+ name.append(strong, kind);
3426
+ const actions = document.createElement('td');
3427
+ actions.className = 'row-actions';
3428
+ actions.append(
3429
+ button('Check', () => checkOne(m.id)),
3430
+ button(m.enabled ? 'Pause' : 'Resume', () => setEnabled(m.id, !m.enabled)),
3431
+ button('Edit', () => fillForm(m.id)),
3432
+ button('Delete', () => deleteMonitor(m.id), 'danger'),
3433
+ );
3434
+ tr.append(
3435
+ statusCell(m.status),
3436
+ name,
3437
+ cell(target),
3438
+ cell(pct(item.uptimePercent)),
3439
+ cell(item.averageLatencyMs == null ? '-' : item.averageLatencyMs + ' ms'),
3440
+ cell(m.lastCheckedAt ? new Date(m.lastCheckedAt).toLocaleString() : '-'),
3441
+ cell(incident),
3442
+ actions,
3443
+ );
3444
+ root.appendChild(tr);
3445
+ }
3446
+ }
3447
+ function renderResults(results) {
3448
+ const root = byId('results');
3449
+ clear(root);
3450
+ for (const result of results) {
3451
+ const tr = document.createElement('tr');
3452
+ tr.append(
3453
+ statusCell(result.status),
3454
+ cell(result.monitorId),
3455
+ cell(new Date(result.checkedAt).toLocaleString()),
3456
+ cell(result.latencyMs == null ? '-' : result.latencyMs + ' ms'),
3457
+ cell(result.error),
3458
+ );
3459
+ root.appendChild(tr);
3460
+ }
3461
+ }
3462
+ function renderIncidents(incidents) {
3463
+ const root = byId('incidents');
3464
+ clear(root);
3465
+ for (const incident of incidents) {
3466
+ const tr = document.createElement('tr');
3467
+ tr.append(
3468
+ statusCell(incident.status),
3469
+ cell(incident.monitorId),
3470
+ cell(new Date(incident.openedAt).toLocaleString()),
3471
+ cell(incident.closedAt ? new Date(incident.closedAt).toLocaleString() : '-'),
3472
+ cell(incident.failureCount),
3473
+ cell(incident.reason),
3474
+ );
3475
+ root.appendChild(tr);
3476
+ }
3477
+ }
3478
+ async function checkOne(id) {
3479
+ await fetch('/api/monitors/' + encodeURIComponent(id) + '/check', { method: 'POST' });
3480
+ await load();
3481
+ }
3482
+ async function setEnabled(id, enabled) {
3483
+ await fetch('/api/monitors/' + encodeURIComponent(id), {
3484
+ method: 'PATCH',
3485
+ headers: { 'content-type': 'application/json' },
3486
+ body: JSON.stringify({ enabled }),
3487
+ });
3488
+ await load();
3489
+ }
3490
+ async function deleteMonitor(id) {
3491
+ await fetch('/api/monitors/' + encodeURIComponent(id), { method: 'DELETE' });
3492
+ await load();
3493
+ }
3494
+ function fillForm(id) {
3495
+ const m = monitorCache.find((item) => item.id === id);
3496
+ if (!m) return;
3497
+ editingId = m.id;
3498
+ byId('form-name').value = m.name;
3499
+ byId('form-kind').value = m.kind;
3500
+ byId('form-url').value = m.url || '';
3501
+ byId('form-host').value = m.host || '';
3502
+ byId('form-port').value = m.port || '';
3503
+ byId('form-interval').value = m.intervalSeconds;
3504
+ byId('form-timeout').value = m.timeoutMs;
3505
+ byId('form-submit').textContent = 'Save';
3506
+ byId('form-status').textContent = ['Editing', m.name].join(' ');
3507
+ }
3508
+ function resetForm() {
3509
+ editingId = null;
3510
+ byId('monitor-form').reset();
3511
+ byId('form-submit').textContent = 'Add';
3512
+ byId('form-status').textContent = '';
3513
+ }
3514
+ byId('monitor-form').addEventListener('submit', async (event) => {
3515
+ event.preventDefault();
3516
+ const kind = byId('form-kind').value;
3517
+ const body = {
3518
+ name: byId('form-name').value,
3519
+ kind,
3520
+ intervalSeconds: Number(byId('form-interval').value || 60),
3521
+ timeoutMs: Number(byId('form-timeout').value || 5000),
3522
+ };
3523
+ if (kind === 'http') body.url = byId('form-url').value;
3524
+ else {
3525
+ body.host = byId('form-host').value;
3526
+ body.port = Number(byId('form-port').value);
3527
+ }
3528
+ const response = await fetch(editingId ? '/api/monitors/' + encodeURIComponent(editingId) : '/api/monitors', {
3529
+ method: editingId ? 'PATCH' : 'POST',
3530
+ headers: { 'content-type': 'application/json' },
3531
+ body: JSON.stringify(body),
3532
+ });
3533
+ const payload = await response.json();
3534
+ byId('form-status').textContent = response.ok ? [editingId ? 'Saved' : 'Added', payload.name].join(' ') : payload.error;
3535
+ if (response.ok) resetForm();
3536
+ await load();
3537
+ });
3538
+ byId('form-cancel').addEventListener('click', resetForm);
3539
+ byId('refresh').addEventListener('click', load);
3540
+ byId('check-all').addEventListener('click', async () => {
3541
+ await fetch('/api/check-all', { method: 'POST' });
3542
+ await load();
3543
+ });
3544
+ load();
3545
+ </script>
3546
+ </body>
3547
+ </html>`;
3548
+ }
3549
+
3550
+ // src/api.ts
3551
+ function createApiHandler(service) {
3552
+ return async (request) => {
3553
+ const url = new URL(request.url);
3554
+ try {
3555
+ validateLocalMutationRequest(request, url);
3556
+ if (request.method === "GET" && url.pathname === "/") {
3557
+ return html(dashboardHtml());
3558
+ }
3559
+ if (request.method === "GET" && url.pathname === "/health") {
3560
+ return json({ ok: true, service: "uptime" });
3561
+ }
3562
+ if (request.method === "GET" && url.pathname === "/api/summary") {
3563
+ return json(service.summary());
3564
+ }
3565
+ if (request.method === "GET" && url.pathname === "/api/monitors") {
3566
+ return json(service.listMonitors({ includeDisabled: url.searchParams.get("includeDisabled") === "true" }));
3567
+ }
3568
+ if (request.method === "POST" && url.pathname === "/api/monitors") {
3569
+ return json(service.createMonitor(await jsonBody(request)), 201);
3570
+ }
3571
+ if (request.method === "GET" && url.pathname === "/api/incidents") {
3572
+ const status = url.searchParams.get("status");
3573
+ return json(service.listIncidents({
3574
+ status: status === "open" || status === "closed" ? status : undefined,
3575
+ monitorId: url.searchParams.get("monitorId") ?? undefined,
3576
+ limit: numericParam(url, "limit", 50)
3577
+ }));
3578
+ }
3579
+ if (request.method === "GET" && url.pathname === "/api/results") {
3580
+ return json(service.listResults({
3581
+ monitorId: url.searchParams.get("monitorId") ?? undefined,
3582
+ limit: numericParam(url, "limit", 50)
3583
+ }));
3584
+ }
3585
+ if (request.method === "POST" && url.pathname === "/api/check-all") {
3586
+ return json(await service.checkAll());
3587
+ }
3588
+ const monitorMatch = url.pathname.match(/^\/api\/monitors\/([^/]+)(?:\/(check))?$/);
3589
+ if (monitorMatch) {
3590
+ const id = decodeURIComponent(monitorMatch[1]);
3591
+ if (request.method === "GET" && !monitorMatch[2]) {
3592
+ const monitor = service.getMonitor(id);
3593
+ return monitor ? json(monitor) : json({ error: "not found" }, 404);
3594
+ }
3595
+ if (request.method === "PATCH" && !monitorMatch[2]) {
3596
+ return json(service.updateMonitor(id, await jsonBody(request)));
3597
+ }
3598
+ if (request.method === "DELETE" && !monitorMatch[2]) {
3599
+ return json({ deleted: service.deleteMonitor(id) });
3600
+ }
3601
+ if (request.method === "POST" && monitorMatch[2] === "check") {
3602
+ return json(await service.checkMonitor(id));
3603
+ }
3604
+ }
3605
+ return json({ error: "not found" }, 404);
3606
+ } catch (error) {
3607
+ return json({ error: error instanceof Error ? error.message : String(error) }, error instanceof ApiError ? error.status : 400);
3608
+ }
3609
+ };
3610
+ }
3611
+ function serveUptime(options = {}) {
3612
+ const service = options.service ?? new UptimeService(options);
3613
+ const scheduler = options.check ? service.startScheduler() : undefined;
3614
+ const server = Bun.serve({
3615
+ hostname: options.host ?? "127.0.0.1",
3616
+ port: options.port ?? 3899,
3617
+ fetch: createApiHandler(service)
3618
+ });
3619
+ return { server, service, scheduler };
3620
+ }
3621
+ function json(value, status = 200) {
3622
+ return new Response(JSON.stringify(value, null, 2), {
3623
+ status,
3624
+ headers: {
3625
+ "content-type": "application/json; charset=utf-8",
3626
+ "cache-control": "no-store"
3627
+ }
3628
+ });
3629
+ }
3630
+ function html(value) {
3631
+ return new Response(value, {
3632
+ headers: {
3633
+ "content-type": "text/html; charset=utf-8",
3634
+ "cache-control": "no-store"
3635
+ }
3636
+ });
3637
+ }
3638
+ function numericParam(url, name, fallback) {
3639
+ const raw = url.searchParams.get(name);
3640
+ if (!raw)
3641
+ return fallback;
3642
+ const parsed = Number(raw);
3643
+ return Number.isFinite(parsed) ? parsed : fallback;
3644
+ }
3645
+ function validateLocalMutationRequest(request, url) {
3646
+ if (!["POST", "PATCH", "DELETE"].includes(request.method))
3647
+ return;
3648
+ const origin = request.headers.get("origin");
3649
+ if (origin && origin !== `${url.protocol}//${url.host}`) {
3650
+ throw new ApiError("cross-origin mutation rejected", 403);
3651
+ }
3652
+ }
3653
+ async function jsonBody(request) {
3654
+ const contentType = request.headers.get("content-type") ?? "";
3655
+ const mediaType = contentType.split(";")[0]?.trim().toLowerCase();
3656
+ if (mediaType !== "application/json" && !mediaType.endsWith("+json")) {
3657
+ throw new ApiError("content-type must be application/json", 415);
3658
+ }
3659
+ return request.json();
3660
+ }
3661
+
3662
+ class ApiError extends Error {
3663
+ status;
3664
+ constructor(message, status) {
3665
+ super(message);
3666
+ this.status = status;
3667
+ }
3668
+ }
3669
+
3670
+ // src/cli/index.ts
3671
+ var program2 = new Command;
3672
+ program2.name("uptime").description("Local-first uptime and downtime monitoring").version(packageVersion()).option("-j, --json", "print JSON");
3673
+ function service() {
3674
+ return new UptimeService;
3675
+ }
3676
+ function wantsJson(opts) {
3677
+ return Boolean(opts?.json || program2.opts().json);
3678
+ }
3679
+ function print(value, text, opts) {
3680
+ if (wantsJson(opts))
3681
+ console.log(JSON.stringify(value, null, 2));
3682
+ else
3683
+ console.log(text);
3684
+ }
3685
+ function fail(error) {
3686
+ const message = error instanceof Error ? error.message : String(error);
3687
+ if (program2.opts().json)
3688
+ console.log(JSON.stringify({ ok: false, error: message }, null, 2));
3689
+ else
3690
+ console.error(source_default.red(message));
3691
+ process.exit(1);
3692
+ }
3693
+ program2.command("init").description("Initialize the local uptime store").option("-j, --json", "print JSON").action((opts) => {
3694
+ try {
3695
+ ensureUptimeHome();
3696
+ const svc = service();
3697
+ svc.close();
3698
+ const data = { ok: true, home: uptimeHome(), dbPath: uptimeDbPath(), exists: existsSync(uptimeDbPath()) };
3699
+ print(data, `Initialized ${data.dbPath}`, opts);
3700
+ } catch (error) {
3701
+ fail(error);
3702
+ }
3703
+ });
3704
+ program2.command("add <name>").description("Add an HTTP or TCP monitor").option("--url <url>", "HTTP/HTTPS URL to check").option("--tcp <host>", "TCP host to connect to").option("--port <port>", "TCP port", parseInteger).option("--method <method>", "HTTP method", "GET").option("--expected-status <status>", "exact expected HTTP status", parseInteger).option("--interval <seconds>", "check interval in seconds", parseInteger, 60).option("--timeout <ms>", "check timeout in milliseconds", parseInteger, 5000).option("--retries <count>", "retry count before recording a down result", parseInteger, 0).option("--disabled", "create the monitor disabled").option("-j, --json", "print JSON").action((name, opts) => {
3705
+ try {
3706
+ if (opts.url && opts.tcp)
3707
+ throw new Error("Choose either --url or --tcp, not both");
3708
+ const input = opts.tcp ? {
3709
+ name,
3710
+ kind: "tcp",
3711
+ host: opts.tcp,
3712
+ port: opts.port,
3713
+ intervalSeconds: opts.interval,
3714
+ timeoutMs: opts.timeout,
3715
+ retryCount: opts.retries,
3716
+ enabled: opts.disabled ? false : true
3717
+ } : {
3718
+ name,
3719
+ kind: "http",
3720
+ url: opts.url,
3721
+ method: opts.method,
3722
+ expectedStatus: opts.expectedStatus,
3723
+ intervalSeconds: opts.interval,
3724
+ timeoutMs: opts.timeout,
3725
+ retryCount: opts.retries,
3726
+ enabled: opts.disabled ? false : true
3727
+ };
3728
+ const svc = service();
3729
+ const monitor = svc.createMonitor(input);
3730
+ svc.close();
3731
+ print(monitor, `Added ${monitor.name} (${monitor.kind})`, opts);
3732
+ } catch (error) {
3733
+ fail(error);
3734
+ }
3735
+ });
3736
+ program2.command("list").description("List monitors").option("--all", "include disabled monitors").option("-j, --json", "print JSON").action((opts) => {
3737
+ try {
3738
+ const svc = service();
3739
+ const monitors = svc.listMonitors({ includeDisabled: opts.all });
3740
+ svc.close();
3741
+ print(monitors, renderMonitors(monitors), opts);
3742
+ } catch (error) {
3743
+ fail(error);
3744
+ }
3745
+ });
3746
+ program2.command("get <id-or-name>").description("Show one monitor").option("-j, --json", "print JSON").action((idOrName, opts) => {
3747
+ try {
3748
+ const svc = service();
3749
+ const monitor = svc.getMonitor(idOrName);
3750
+ svc.close();
3751
+ if (!monitor)
3752
+ throw new Error(`Monitor not found: ${idOrName}`);
3753
+ print(monitor, renderMonitorDetail(monitor), opts);
3754
+ } catch (error) {
3755
+ fail(error);
3756
+ }
3757
+ });
3758
+ program2.command("update <id-or-name>").description("Update monitor configuration").option("--name <name>", "new monitor name").option("--url <url>", "switch/update to an HTTP/HTTPS URL").option("--tcp <host>", "switch/update to a TCP host").option("--port <port>", "TCP port", parseInteger).option("--method <method>", "HTTP method").option("--expected-status <status>", "exact expected HTTP status", parseInteger).option("--interval <seconds>", "check interval in seconds", parseInteger).option("--timeout <ms>", "check timeout in milliseconds", parseInteger).option("--retries <count>", "retry count before recording a down result", parseInteger).option("--enable", "enable the monitor").option("--disable", "disable the monitor").option("-j, --json", "print JSON").action((idOrName, opts) => {
3759
+ try {
3760
+ if (opts.url && opts.tcp)
3761
+ throw new Error("Choose either --url or --tcp, not both");
3762
+ if (opts.enable && opts.disable)
3763
+ throw new Error("Choose either --enable or --disable, not both");
3764
+ const input = {};
3765
+ if (opts.name !== undefined)
3766
+ input.name = opts.name;
3767
+ if (opts.url !== undefined) {
3768
+ input.kind = "http";
3769
+ input.url = opts.url;
3770
+ }
3771
+ if (opts.tcp !== undefined) {
3772
+ input.kind = "tcp";
3773
+ input.host = opts.tcp;
3774
+ }
3775
+ if (opts.port !== undefined)
3776
+ input.port = opts.port;
3777
+ if (opts.method !== undefined)
3778
+ input.method = opts.method;
3779
+ if (opts.expectedStatus !== undefined)
3780
+ input.expectedStatus = opts.expectedStatus;
3781
+ if (opts.interval !== undefined)
3782
+ input.intervalSeconds = opts.interval;
3783
+ if (opts.timeout !== undefined)
3784
+ input.timeoutMs = opts.timeout;
3785
+ if (opts.retries !== undefined)
3786
+ input.retryCount = opts.retries;
3787
+ if (opts.enable) {
3788
+ input.enabled = true;
3789
+ }
3790
+ if (opts.disable) {
3791
+ input.enabled = false;
3792
+ }
3793
+ const svc = service();
3794
+ const monitor = svc.updateMonitor(idOrName, input);
3795
+ svc.close();
3796
+ print(monitor, `Updated ${monitor.name}`, opts);
3797
+ } catch (error) {
3798
+ fail(error);
3799
+ }
3800
+ });
3801
+ program2.command("pause <id-or-name>").description("Disable a monitor").option("-j, --json", "print JSON").action((idOrName, opts) => {
3802
+ try {
3803
+ const svc = service();
3804
+ const monitor = svc.updateMonitor(idOrName, { enabled: false });
3805
+ svc.close();
3806
+ print(monitor, `Paused ${monitor.name}`, opts);
3807
+ } catch (error) {
3808
+ fail(error);
3809
+ }
3810
+ });
3811
+ program2.command("resume <id-or-name>").description("Enable a monitor").option("-j, --json", "print JSON").action((idOrName, opts) => {
3812
+ try {
3813
+ const svc = service();
3814
+ const monitor = svc.updateMonitor(idOrName, { enabled: true });
3815
+ svc.close();
3816
+ print(monitor, `Resumed ${monitor.name}`, opts);
3817
+ } catch (error) {
3818
+ fail(error);
3819
+ }
3820
+ });
3821
+ program2.command("remove <id-or-name>").alias("rm").description("Remove a monitor and its local history").option("-j, --json", "print JSON").action((idOrName, opts) => {
3822
+ try {
3823
+ const svc = service();
3824
+ const deleted = svc.deleteMonitor(idOrName);
3825
+ svc.close();
3826
+ print({ deleted }, deleted ? `Removed ${idOrName}` : `Not found: ${idOrName}`, opts);
3827
+ } catch (error) {
3828
+ fail(error);
3829
+ }
3830
+ });
3831
+ program2.command("check [id-or-name]").description("Run a check for one monitor, or all monitors with --all").option("--all", "check all enabled monitors").option("-j, --json", "print JSON").action(async (idOrName, opts) => {
3832
+ try {
3833
+ const svc = service();
3834
+ const result = opts.all ? await svc.checkAll() : await svc.checkMonitor(idOrName ?? "");
3835
+ svc.close();
3836
+ print(result, Array.isArray(result) ? renderCheckResults(result) : renderCheckResults([result]), opts);
3837
+ } catch (error) {
3838
+ fail(error);
3839
+ }
3840
+ });
3841
+ program2.command("summary").description("Show uptime summary").option("-j, --json", "print JSON").action((opts) => {
3842
+ try {
3843
+ const svc = service();
3844
+ const summary = svc.summary();
3845
+ svc.close();
3846
+ print(summary, renderSummary(summary), opts);
3847
+ } catch (error) {
3848
+ fail(error);
3849
+ }
3850
+ });
3851
+ program2.command("results").description("List recent check results").option("--monitor <id>", "filter by monitor id").option("--limit <n>", "max rows", parseInteger, 20).option("-j, --json", "print JSON").action((opts) => {
3852
+ try {
3853
+ const svc = service();
3854
+ const results = svc.listResults({ monitorId: opts.monitor, limit: opts.limit });
3855
+ svc.close();
3856
+ print(results, renderCheckResults(results), opts);
3857
+ } catch (error) {
3858
+ fail(error);
3859
+ }
3860
+ });
3861
+ program2.command("incidents").description("List incidents").addOption(new Option("--status <status>", "incident status").choices(["open", "closed"])).option("--monitor <id>", "filter by monitor id").option("--limit <n>", "max rows", parseInteger, 20).option("-j, --json", "print JSON").action((opts) => {
3862
+ try {
3863
+ const svc = service();
3864
+ const incidents = svc.listIncidents({ status: opts.status, monitorId: opts.monitor, limit: opts.limit });
3865
+ svc.close();
3866
+ print(incidents, incidents.length ? incidents.map((i) => `${i.status.padEnd(6)} ${i.monitorId} ${i.openedAt} ${i.reason ?? ""}`).join(`
3867
+ `) : "No incidents", opts);
3868
+ } catch (error) {
3869
+ fail(error);
3870
+ }
3871
+ });
3872
+ program2.command("serve").description("Serve the local API and dashboard").option("--host <host>", "host to bind", "127.0.0.1").option("--port <port>", "port", parseInteger, 3899).option("--check", "run the scheduler while serving").option("-j, --json", "print JSON").action((opts) => {
3873
+ try {
3874
+ const { server } = serveUptime({ host: opts.host, port: opts.port, check: opts.check });
3875
+ const data = { ok: true, url: `http://${server.hostname}:${server.port}`, scheduler: Boolean(opts.check) };
3876
+ if (wantsJson(opts))
3877
+ console.log(JSON.stringify(data, null, 2));
3878
+ else
3879
+ console.log(`Open Uptime listening on ${source_default.cyan(data.url)}`);
3880
+ } catch (error) {
3881
+ fail(error);
3882
+ }
3883
+ });
3884
+ function parseInteger(value) {
3885
+ const parsed = Number(value);
3886
+ if (!Number.isInteger(parsed))
3887
+ throw new Error(`Expected integer, got ${value}`);
3888
+ return parsed;
3889
+ }
3890
+ function renderMonitors(monitors) {
3891
+ if (monitors.length === 0)
3892
+ return "No monitors";
3893
+ return monitors.map((monitor) => {
3894
+ const target = monitor.kind === "http" ? monitor.url : `${monitor.host}:${monitor.port}`;
3895
+ const status = renderStatus(monitor.status).padEnd(14);
3896
+ return `${status} ${monitor.name.padEnd(24)} ${monitor.kind.padEnd(4)} ${target}`;
3897
+ }).join(`
3898
+ `);
3899
+ }
3900
+ function renderMonitorDetail(monitor) {
3901
+ const target = monitor.kind === "http" ? monitor.url : `${monitor.host}:${monitor.port}`;
3902
+ return [
3903
+ `${source_default.bold(monitor.name)} ${renderStatus(monitor.status)}`,
3904
+ `id: ${monitor.id}`,
3905
+ `kind: ${monitor.kind}`,
3906
+ `target: ${target}`,
3907
+ `interval: ${monitor.intervalSeconds}s`,
3908
+ `timeout: ${monitor.timeoutMs}ms`,
3909
+ `retries: ${monitor.retryCount}`,
3910
+ `enabled: ${monitor.enabled}`,
3911
+ `last checked: ${monitor.lastCheckedAt ?? "-"}`
3912
+ ].join(`
3913
+ `);
3914
+ }
3915
+ function renderCheckResults(results) {
3916
+ if (results.length === 0)
3917
+ return "No results";
3918
+ return results.map((result) => {
3919
+ const latency = result.latencyMs == null ? "-" : `${result.latencyMs}ms`;
3920
+ return `${renderStatus(result.status).padEnd(12)} ${result.monitorId} ${result.checkedAt} ${latency} ${result.error ?? ""}`;
3921
+ }).join(`
3922
+ `);
3923
+ }
3924
+ function renderSummary(summary) {
3925
+ const lines = [
3926
+ `monitors: ${summary.totals.monitors} up: ${summary.totals.up} down: ${summary.totals.down} open incidents: ${summary.totals.openIncidents}`
3927
+ ];
3928
+ for (const item of summary.monitors) {
3929
+ const uptime = item.uptimePercent == null ? "-" : `${item.uptimePercent.toFixed(2)}%`;
3930
+ const latency = item.averageLatencyMs == null ? "-" : `${item.averageLatencyMs}ms`;
3931
+ lines.push(`${renderStatus(item.monitor.status).padEnd(12)} ${item.monitor.name.padEnd(24)} uptime ${uptime.padStart(8)} latency ${latency}`);
3932
+ }
3933
+ return lines.join(`
3934
+ `);
3935
+ }
3936
+ function renderStatus(status) {
3937
+ if (status === "up")
3938
+ return source_default.green("up");
3939
+ if (status === "down")
3940
+ return source_default.red("down");
3941
+ if (status === "paused")
3942
+ return source_default.yellow("paused");
3943
+ return source_default.gray(status);
3944
+ }
3945
+ program2.parseAsync(process.argv);