@hasna/testers 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +196 -0
- package/dashboard/dist/assets/index-CDcHt94n.css +1 -0
- package/dashboard/dist/assets/index-DCNDCh61.js +49 -0
- package/dashboard/dist/index.html +13 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +4112 -0
- package/dist/db/agents.d.ts +10 -0
- package/dist/db/agents.d.ts.map +1 -0
- package/dist/db/database.d.ts +10 -0
- package/dist/db/database.d.ts.map +1 -0
- package/dist/db/projects.d.ts +11 -0
- package/dist/db/projects.d.ts.map +1 -0
- package/dist/db/results.d.ts +20 -0
- package/dist/db/results.d.ts.map +1 -0
- package/dist/db/runs.d.ts +9 -0
- package/dist/db/runs.d.ts.map +1 -0
- package/dist/db/scenarios.d.ts +8 -0
- package/dist/db/scenarios.d.ts.map +1 -0
- package/dist/db/screenshots.d.ts +13 -0
- package/dist/db/screenshots.d.ts.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2515 -0
- package/dist/lib/ai-client.d.ts +66 -0
- package/dist/lib/ai-client.d.ts.map +1 -0
- package/dist/lib/browser.d.ts +64 -0
- package/dist/lib/browser.d.ts.map +1 -0
- package/dist/lib/config.d.ts +18 -0
- package/dist/lib/config.d.ts.map +1 -0
- package/dist/lib/reporter.d.ts +18 -0
- package/dist/lib/reporter.d.ts.map +1 -0
- package/dist/lib/runner.d.ts +36 -0
- package/dist/lib/runner.d.ts.map +1 -0
- package/dist/lib/screenshotter.d.ts +60 -0
- package/dist/lib/screenshotter.d.ts.map +1 -0
- package/dist/lib/todos-connector.d.ts +32 -0
- package/dist/lib/todos-connector.d.ts.map +1 -0
- package/dist/mcp/index.d.ts +3 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +5903 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +1654 -0
- package/dist/types/index.d.ts +276 -0
- package/dist/types/index.d.ts.map +1 -0
- package/package.json +78 -0
|
@@ -0,0 +1,4112 @@
|
|
|
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
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
9
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
10
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
11
|
+
for (let key of __getOwnPropNames(mod))
|
|
12
|
+
if (!__hasOwnProp.call(to, key))
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: () => mod[key],
|
|
15
|
+
enumerable: true
|
|
16
|
+
});
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
20
|
+
var __require = import.meta.require;
|
|
21
|
+
|
|
22
|
+
// node_modules/commander/lib/error.js
|
|
23
|
+
var require_error = __commonJS((exports) => {
|
|
24
|
+
class CommanderError extends Error {
|
|
25
|
+
constructor(exitCode, code, message) {
|
|
26
|
+
super(message);
|
|
27
|
+
Error.captureStackTrace(this, this.constructor);
|
|
28
|
+
this.name = this.constructor.name;
|
|
29
|
+
this.code = code;
|
|
30
|
+
this.exitCode = exitCode;
|
|
31
|
+
this.nestedError = undefined;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
class InvalidArgumentError extends CommanderError {
|
|
36
|
+
constructor(message) {
|
|
37
|
+
super(1, "commander.invalidArgument", message);
|
|
38
|
+
Error.captureStackTrace(this, this.constructor);
|
|
39
|
+
this.name = this.constructor.name;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.CommanderError = CommanderError;
|
|
43
|
+
exports.InvalidArgumentError = InvalidArgumentError;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// node_modules/commander/lib/argument.js
|
|
47
|
+
var require_argument = __commonJS((exports) => {
|
|
48
|
+
var { InvalidArgumentError } = require_error();
|
|
49
|
+
|
|
50
|
+
class Argument {
|
|
51
|
+
constructor(name, description) {
|
|
52
|
+
this.description = description || "";
|
|
53
|
+
this.variadic = false;
|
|
54
|
+
this.parseArg = undefined;
|
|
55
|
+
this.defaultValue = undefined;
|
|
56
|
+
this.defaultValueDescription = undefined;
|
|
57
|
+
this.argChoices = undefined;
|
|
58
|
+
switch (name[0]) {
|
|
59
|
+
case "<":
|
|
60
|
+
this.required = true;
|
|
61
|
+
this._name = name.slice(1, -1);
|
|
62
|
+
break;
|
|
63
|
+
case "[":
|
|
64
|
+
this.required = false;
|
|
65
|
+
this._name = name.slice(1, -1);
|
|
66
|
+
break;
|
|
67
|
+
default:
|
|
68
|
+
this.required = true;
|
|
69
|
+
this._name = name;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
if (this._name.length > 3 && this._name.slice(-3) === "...") {
|
|
73
|
+
this.variadic = true;
|
|
74
|
+
this._name = this._name.slice(0, -3);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
name() {
|
|
78
|
+
return this._name;
|
|
79
|
+
}
|
|
80
|
+
_concatValue(value, previous) {
|
|
81
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
82
|
+
return [value];
|
|
83
|
+
}
|
|
84
|
+
return previous.concat(value);
|
|
85
|
+
}
|
|
86
|
+
default(value, description) {
|
|
87
|
+
this.defaultValue = value;
|
|
88
|
+
this.defaultValueDescription = description;
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
argParser(fn) {
|
|
92
|
+
this.parseArg = fn;
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
choices(values) {
|
|
96
|
+
this.argChoices = values.slice();
|
|
97
|
+
this.parseArg = (arg, previous) => {
|
|
98
|
+
if (!this.argChoices.includes(arg)) {
|
|
99
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
100
|
+
}
|
|
101
|
+
if (this.variadic) {
|
|
102
|
+
return this._concatValue(arg, previous);
|
|
103
|
+
}
|
|
104
|
+
return arg;
|
|
105
|
+
};
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
argRequired() {
|
|
109
|
+
this.required = true;
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
argOptional() {
|
|
113
|
+
this.required = false;
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function humanReadableArgName(arg) {
|
|
118
|
+
const nameOutput = arg.name() + (arg.variadic === true ? "..." : "");
|
|
119
|
+
return arg.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
|
|
120
|
+
}
|
|
121
|
+
exports.Argument = Argument;
|
|
122
|
+
exports.humanReadableArgName = humanReadableArgName;
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// node_modules/commander/lib/help.js
|
|
126
|
+
var require_help = __commonJS((exports) => {
|
|
127
|
+
var { humanReadableArgName } = require_argument();
|
|
128
|
+
|
|
129
|
+
class Help {
|
|
130
|
+
constructor() {
|
|
131
|
+
this.helpWidth = undefined;
|
|
132
|
+
this.minWidthToWrap = 40;
|
|
133
|
+
this.sortSubcommands = false;
|
|
134
|
+
this.sortOptions = false;
|
|
135
|
+
this.showGlobalOptions = false;
|
|
136
|
+
}
|
|
137
|
+
prepareContext(contextOptions) {
|
|
138
|
+
this.helpWidth = this.helpWidth ?? contextOptions.helpWidth ?? 80;
|
|
139
|
+
}
|
|
140
|
+
visibleCommands(cmd) {
|
|
141
|
+
const visibleCommands = cmd.commands.filter((cmd2) => !cmd2._hidden);
|
|
142
|
+
const helpCommand = cmd._getHelpCommand();
|
|
143
|
+
if (helpCommand && !helpCommand._hidden) {
|
|
144
|
+
visibleCommands.push(helpCommand);
|
|
145
|
+
}
|
|
146
|
+
if (this.sortSubcommands) {
|
|
147
|
+
visibleCommands.sort((a, b) => {
|
|
148
|
+
return a.name().localeCompare(b.name());
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
return visibleCommands;
|
|
152
|
+
}
|
|
153
|
+
compareOptions(a, b) {
|
|
154
|
+
const getSortKey = (option) => {
|
|
155
|
+
return option.short ? option.short.replace(/^-/, "") : option.long.replace(/^--/, "");
|
|
156
|
+
};
|
|
157
|
+
return getSortKey(a).localeCompare(getSortKey(b));
|
|
158
|
+
}
|
|
159
|
+
visibleOptions(cmd) {
|
|
160
|
+
const visibleOptions = cmd.options.filter((option) => !option.hidden);
|
|
161
|
+
const helpOption = cmd._getHelpOption();
|
|
162
|
+
if (helpOption && !helpOption.hidden) {
|
|
163
|
+
const removeShort = helpOption.short && cmd._findOption(helpOption.short);
|
|
164
|
+
const removeLong = helpOption.long && cmd._findOption(helpOption.long);
|
|
165
|
+
if (!removeShort && !removeLong) {
|
|
166
|
+
visibleOptions.push(helpOption);
|
|
167
|
+
} else if (helpOption.long && !removeLong) {
|
|
168
|
+
visibleOptions.push(cmd.createOption(helpOption.long, helpOption.description));
|
|
169
|
+
} else if (helpOption.short && !removeShort) {
|
|
170
|
+
visibleOptions.push(cmd.createOption(helpOption.short, helpOption.description));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (this.sortOptions) {
|
|
174
|
+
visibleOptions.sort(this.compareOptions);
|
|
175
|
+
}
|
|
176
|
+
return visibleOptions;
|
|
177
|
+
}
|
|
178
|
+
visibleGlobalOptions(cmd) {
|
|
179
|
+
if (!this.showGlobalOptions)
|
|
180
|
+
return [];
|
|
181
|
+
const globalOptions = [];
|
|
182
|
+
for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
183
|
+
const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
|
|
184
|
+
globalOptions.push(...visibleOptions);
|
|
185
|
+
}
|
|
186
|
+
if (this.sortOptions) {
|
|
187
|
+
globalOptions.sort(this.compareOptions);
|
|
188
|
+
}
|
|
189
|
+
return globalOptions;
|
|
190
|
+
}
|
|
191
|
+
visibleArguments(cmd) {
|
|
192
|
+
if (cmd._argsDescription) {
|
|
193
|
+
cmd.registeredArguments.forEach((argument) => {
|
|
194
|
+
argument.description = argument.description || cmd._argsDescription[argument.name()] || "";
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
if (cmd.registeredArguments.find((argument) => argument.description)) {
|
|
198
|
+
return cmd.registeredArguments;
|
|
199
|
+
}
|
|
200
|
+
return [];
|
|
201
|
+
}
|
|
202
|
+
subcommandTerm(cmd) {
|
|
203
|
+
const args = cmd.registeredArguments.map((arg) => humanReadableArgName(arg)).join(" ");
|
|
204
|
+
return cmd._name + (cmd._aliases[0] ? "|" + cmd._aliases[0] : "") + (cmd.options.length ? " [options]" : "") + (args ? " " + args : "");
|
|
205
|
+
}
|
|
206
|
+
optionTerm(option) {
|
|
207
|
+
return option.flags;
|
|
208
|
+
}
|
|
209
|
+
argumentTerm(argument) {
|
|
210
|
+
return argument.name();
|
|
211
|
+
}
|
|
212
|
+
longestSubcommandTermLength(cmd, helper) {
|
|
213
|
+
return helper.visibleCommands(cmd).reduce((max, command) => {
|
|
214
|
+
return Math.max(max, this.displayWidth(helper.styleSubcommandTerm(helper.subcommandTerm(command))));
|
|
215
|
+
}, 0);
|
|
216
|
+
}
|
|
217
|
+
longestOptionTermLength(cmd, helper) {
|
|
218
|
+
return helper.visibleOptions(cmd).reduce((max, option) => {
|
|
219
|
+
return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
|
|
220
|
+
}, 0);
|
|
221
|
+
}
|
|
222
|
+
longestGlobalOptionTermLength(cmd, helper) {
|
|
223
|
+
return helper.visibleGlobalOptions(cmd).reduce((max, option) => {
|
|
224
|
+
return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
|
|
225
|
+
}, 0);
|
|
226
|
+
}
|
|
227
|
+
longestArgumentTermLength(cmd, helper) {
|
|
228
|
+
return helper.visibleArguments(cmd).reduce((max, argument) => {
|
|
229
|
+
return Math.max(max, this.displayWidth(helper.styleArgumentTerm(helper.argumentTerm(argument))));
|
|
230
|
+
}, 0);
|
|
231
|
+
}
|
|
232
|
+
commandUsage(cmd) {
|
|
233
|
+
let cmdName = cmd._name;
|
|
234
|
+
if (cmd._aliases[0]) {
|
|
235
|
+
cmdName = cmdName + "|" + cmd._aliases[0];
|
|
236
|
+
}
|
|
237
|
+
let ancestorCmdNames = "";
|
|
238
|
+
for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
239
|
+
ancestorCmdNames = ancestorCmd.name() + " " + ancestorCmdNames;
|
|
240
|
+
}
|
|
241
|
+
return ancestorCmdNames + cmdName + " " + cmd.usage();
|
|
242
|
+
}
|
|
243
|
+
commandDescription(cmd) {
|
|
244
|
+
return cmd.description();
|
|
245
|
+
}
|
|
246
|
+
subcommandDescription(cmd) {
|
|
247
|
+
return cmd.summary() || cmd.description();
|
|
248
|
+
}
|
|
249
|
+
optionDescription(option) {
|
|
250
|
+
const extraInfo = [];
|
|
251
|
+
if (option.argChoices) {
|
|
252
|
+
extraInfo.push(`choices: ${option.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
253
|
+
}
|
|
254
|
+
if (option.defaultValue !== undefined) {
|
|
255
|
+
const showDefault = option.required || option.optional || option.isBoolean() && typeof option.defaultValue === "boolean";
|
|
256
|
+
if (showDefault) {
|
|
257
|
+
extraInfo.push(`default: ${option.defaultValueDescription || JSON.stringify(option.defaultValue)}`);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (option.presetArg !== undefined && option.optional) {
|
|
261
|
+
extraInfo.push(`preset: ${JSON.stringify(option.presetArg)}`);
|
|
262
|
+
}
|
|
263
|
+
if (option.envVar !== undefined) {
|
|
264
|
+
extraInfo.push(`env: ${option.envVar}`);
|
|
265
|
+
}
|
|
266
|
+
if (extraInfo.length > 0) {
|
|
267
|
+
return `${option.description} (${extraInfo.join(", ")})`;
|
|
268
|
+
}
|
|
269
|
+
return option.description;
|
|
270
|
+
}
|
|
271
|
+
argumentDescription(argument) {
|
|
272
|
+
const extraInfo = [];
|
|
273
|
+
if (argument.argChoices) {
|
|
274
|
+
extraInfo.push(`choices: ${argument.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
275
|
+
}
|
|
276
|
+
if (argument.defaultValue !== undefined) {
|
|
277
|
+
extraInfo.push(`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`);
|
|
278
|
+
}
|
|
279
|
+
if (extraInfo.length > 0) {
|
|
280
|
+
const extraDescription = `(${extraInfo.join(", ")})`;
|
|
281
|
+
if (argument.description) {
|
|
282
|
+
return `${argument.description} ${extraDescription}`;
|
|
283
|
+
}
|
|
284
|
+
return extraDescription;
|
|
285
|
+
}
|
|
286
|
+
return argument.description;
|
|
287
|
+
}
|
|
288
|
+
formatHelp(cmd, helper) {
|
|
289
|
+
const termWidth = helper.padWidth(cmd, helper);
|
|
290
|
+
const helpWidth = helper.helpWidth ?? 80;
|
|
291
|
+
function callFormatItem(term, description) {
|
|
292
|
+
return helper.formatItem(term, termWidth, description, helper);
|
|
293
|
+
}
|
|
294
|
+
let output = [
|
|
295
|
+
`${helper.styleTitle("Usage:")} ${helper.styleUsage(helper.commandUsage(cmd))}`,
|
|
296
|
+
""
|
|
297
|
+
];
|
|
298
|
+
const commandDescription = helper.commandDescription(cmd);
|
|
299
|
+
if (commandDescription.length > 0) {
|
|
300
|
+
output = output.concat([
|
|
301
|
+
helper.boxWrap(helper.styleCommandDescription(commandDescription), helpWidth),
|
|
302
|
+
""
|
|
303
|
+
]);
|
|
304
|
+
}
|
|
305
|
+
const argumentList = helper.visibleArguments(cmd).map((argument) => {
|
|
306
|
+
return callFormatItem(helper.styleArgumentTerm(helper.argumentTerm(argument)), helper.styleArgumentDescription(helper.argumentDescription(argument)));
|
|
307
|
+
});
|
|
308
|
+
if (argumentList.length > 0) {
|
|
309
|
+
output = output.concat([
|
|
310
|
+
helper.styleTitle("Arguments:"),
|
|
311
|
+
...argumentList,
|
|
312
|
+
""
|
|
313
|
+
]);
|
|
314
|
+
}
|
|
315
|
+
const optionList = helper.visibleOptions(cmd).map((option) => {
|
|
316
|
+
return callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option)));
|
|
317
|
+
});
|
|
318
|
+
if (optionList.length > 0) {
|
|
319
|
+
output = output.concat([
|
|
320
|
+
helper.styleTitle("Options:"),
|
|
321
|
+
...optionList,
|
|
322
|
+
""
|
|
323
|
+
]);
|
|
324
|
+
}
|
|
325
|
+
if (helper.showGlobalOptions) {
|
|
326
|
+
const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
|
|
327
|
+
return callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option)));
|
|
328
|
+
});
|
|
329
|
+
if (globalOptionList.length > 0) {
|
|
330
|
+
output = output.concat([
|
|
331
|
+
helper.styleTitle("Global Options:"),
|
|
332
|
+
...globalOptionList,
|
|
333
|
+
""
|
|
334
|
+
]);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const commandList = helper.visibleCommands(cmd).map((cmd2) => {
|
|
338
|
+
return callFormatItem(helper.styleSubcommandTerm(helper.subcommandTerm(cmd2)), helper.styleSubcommandDescription(helper.subcommandDescription(cmd2)));
|
|
339
|
+
});
|
|
340
|
+
if (commandList.length > 0) {
|
|
341
|
+
output = output.concat([
|
|
342
|
+
helper.styleTitle("Commands:"),
|
|
343
|
+
...commandList,
|
|
344
|
+
""
|
|
345
|
+
]);
|
|
346
|
+
}
|
|
347
|
+
return output.join(`
|
|
348
|
+
`);
|
|
349
|
+
}
|
|
350
|
+
displayWidth(str) {
|
|
351
|
+
return stripColor(str).length;
|
|
352
|
+
}
|
|
353
|
+
styleTitle(str) {
|
|
354
|
+
return str;
|
|
355
|
+
}
|
|
356
|
+
styleUsage(str) {
|
|
357
|
+
return str.split(" ").map((word) => {
|
|
358
|
+
if (word === "[options]")
|
|
359
|
+
return this.styleOptionText(word);
|
|
360
|
+
if (word === "[command]")
|
|
361
|
+
return this.styleSubcommandText(word);
|
|
362
|
+
if (word[0] === "[" || word[0] === "<")
|
|
363
|
+
return this.styleArgumentText(word);
|
|
364
|
+
return this.styleCommandText(word);
|
|
365
|
+
}).join(" ");
|
|
366
|
+
}
|
|
367
|
+
styleCommandDescription(str) {
|
|
368
|
+
return this.styleDescriptionText(str);
|
|
369
|
+
}
|
|
370
|
+
styleOptionDescription(str) {
|
|
371
|
+
return this.styleDescriptionText(str);
|
|
372
|
+
}
|
|
373
|
+
styleSubcommandDescription(str) {
|
|
374
|
+
return this.styleDescriptionText(str);
|
|
375
|
+
}
|
|
376
|
+
styleArgumentDescription(str) {
|
|
377
|
+
return this.styleDescriptionText(str);
|
|
378
|
+
}
|
|
379
|
+
styleDescriptionText(str) {
|
|
380
|
+
return str;
|
|
381
|
+
}
|
|
382
|
+
styleOptionTerm(str) {
|
|
383
|
+
return this.styleOptionText(str);
|
|
384
|
+
}
|
|
385
|
+
styleSubcommandTerm(str) {
|
|
386
|
+
return str.split(" ").map((word) => {
|
|
387
|
+
if (word === "[options]")
|
|
388
|
+
return this.styleOptionText(word);
|
|
389
|
+
if (word[0] === "[" || word[0] === "<")
|
|
390
|
+
return this.styleArgumentText(word);
|
|
391
|
+
return this.styleSubcommandText(word);
|
|
392
|
+
}).join(" ");
|
|
393
|
+
}
|
|
394
|
+
styleArgumentTerm(str) {
|
|
395
|
+
return this.styleArgumentText(str);
|
|
396
|
+
}
|
|
397
|
+
styleOptionText(str) {
|
|
398
|
+
return str;
|
|
399
|
+
}
|
|
400
|
+
styleArgumentText(str) {
|
|
401
|
+
return str;
|
|
402
|
+
}
|
|
403
|
+
styleSubcommandText(str) {
|
|
404
|
+
return str;
|
|
405
|
+
}
|
|
406
|
+
styleCommandText(str) {
|
|
407
|
+
return str;
|
|
408
|
+
}
|
|
409
|
+
padWidth(cmd, helper) {
|
|
410
|
+
return Math.max(helper.longestOptionTermLength(cmd, helper), helper.longestGlobalOptionTermLength(cmd, helper), helper.longestSubcommandTermLength(cmd, helper), helper.longestArgumentTermLength(cmd, helper));
|
|
411
|
+
}
|
|
412
|
+
preformatted(str) {
|
|
413
|
+
return /\n[^\S\r\n]/.test(str);
|
|
414
|
+
}
|
|
415
|
+
formatItem(term, termWidth, description, helper) {
|
|
416
|
+
const itemIndent = 2;
|
|
417
|
+
const itemIndentStr = " ".repeat(itemIndent);
|
|
418
|
+
if (!description)
|
|
419
|
+
return itemIndentStr + term;
|
|
420
|
+
const paddedTerm = term.padEnd(termWidth + term.length - helper.displayWidth(term));
|
|
421
|
+
const spacerWidth = 2;
|
|
422
|
+
const helpWidth = this.helpWidth ?? 80;
|
|
423
|
+
const remainingWidth = helpWidth - termWidth - spacerWidth - itemIndent;
|
|
424
|
+
let formattedDescription;
|
|
425
|
+
if (remainingWidth < this.minWidthToWrap || helper.preformatted(description)) {
|
|
426
|
+
formattedDescription = description;
|
|
427
|
+
} else {
|
|
428
|
+
const wrappedDescription = helper.boxWrap(description, remainingWidth);
|
|
429
|
+
formattedDescription = wrappedDescription.replace(/\n/g, `
|
|
430
|
+
` + " ".repeat(termWidth + spacerWidth));
|
|
431
|
+
}
|
|
432
|
+
return itemIndentStr + paddedTerm + " ".repeat(spacerWidth) + formattedDescription.replace(/\n/g, `
|
|
433
|
+
${itemIndentStr}`);
|
|
434
|
+
}
|
|
435
|
+
boxWrap(str, width) {
|
|
436
|
+
if (width < this.minWidthToWrap)
|
|
437
|
+
return str;
|
|
438
|
+
const rawLines = str.split(/\r\n|\n/);
|
|
439
|
+
const chunkPattern = /[\s]*[^\s]+/g;
|
|
440
|
+
const wrappedLines = [];
|
|
441
|
+
rawLines.forEach((line) => {
|
|
442
|
+
const chunks = line.match(chunkPattern);
|
|
443
|
+
if (chunks === null) {
|
|
444
|
+
wrappedLines.push("");
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
let sumChunks = [chunks.shift()];
|
|
448
|
+
let sumWidth = this.displayWidth(sumChunks[0]);
|
|
449
|
+
chunks.forEach((chunk) => {
|
|
450
|
+
const visibleWidth = this.displayWidth(chunk);
|
|
451
|
+
if (sumWidth + visibleWidth <= width) {
|
|
452
|
+
sumChunks.push(chunk);
|
|
453
|
+
sumWidth += visibleWidth;
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
wrappedLines.push(sumChunks.join(""));
|
|
457
|
+
const nextChunk = chunk.trimStart();
|
|
458
|
+
sumChunks = [nextChunk];
|
|
459
|
+
sumWidth = this.displayWidth(nextChunk);
|
|
460
|
+
});
|
|
461
|
+
wrappedLines.push(sumChunks.join(""));
|
|
462
|
+
});
|
|
463
|
+
return wrappedLines.join(`
|
|
464
|
+
`);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
function stripColor(str) {
|
|
468
|
+
const sgrPattern = /\x1b\[\d*(;\d*)*m/g;
|
|
469
|
+
return str.replace(sgrPattern, "");
|
|
470
|
+
}
|
|
471
|
+
exports.Help = Help;
|
|
472
|
+
exports.stripColor = stripColor;
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
// node_modules/commander/lib/option.js
|
|
476
|
+
var require_option = __commonJS((exports) => {
|
|
477
|
+
var { InvalidArgumentError } = require_error();
|
|
478
|
+
|
|
479
|
+
class Option {
|
|
480
|
+
constructor(flags, description) {
|
|
481
|
+
this.flags = flags;
|
|
482
|
+
this.description = description || "";
|
|
483
|
+
this.required = flags.includes("<");
|
|
484
|
+
this.optional = flags.includes("[");
|
|
485
|
+
this.variadic = /\w\.\.\.[>\]]$/.test(flags);
|
|
486
|
+
this.mandatory = false;
|
|
487
|
+
const optionFlags = splitOptionFlags(flags);
|
|
488
|
+
this.short = optionFlags.shortFlag;
|
|
489
|
+
this.long = optionFlags.longFlag;
|
|
490
|
+
this.negate = false;
|
|
491
|
+
if (this.long) {
|
|
492
|
+
this.negate = this.long.startsWith("--no-");
|
|
493
|
+
}
|
|
494
|
+
this.defaultValue = undefined;
|
|
495
|
+
this.defaultValueDescription = undefined;
|
|
496
|
+
this.presetArg = undefined;
|
|
497
|
+
this.envVar = undefined;
|
|
498
|
+
this.parseArg = undefined;
|
|
499
|
+
this.hidden = false;
|
|
500
|
+
this.argChoices = undefined;
|
|
501
|
+
this.conflictsWith = [];
|
|
502
|
+
this.implied = undefined;
|
|
503
|
+
}
|
|
504
|
+
default(value, description) {
|
|
505
|
+
this.defaultValue = value;
|
|
506
|
+
this.defaultValueDescription = description;
|
|
507
|
+
return this;
|
|
508
|
+
}
|
|
509
|
+
preset(arg) {
|
|
510
|
+
this.presetArg = arg;
|
|
511
|
+
return this;
|
|
512
|
+
}
|
|
513
|
+
conflicts(names) {
|
|
514
|
+
this.conflictsWith = this.conflictsWith.concat(names);
|
|
515
|
+
return this;
|
|
516
|
+
}
|
|
517
|
+
implies(impliedOptionValues) {
|
|
518
|
+
let newImplied = impliedOptionValues;
|
|
519
|
+
if (typeof impliedOptionValues === "string") {
|
|
520
|
+
newImplied = { [impliedOptionValues]: true };
|
|
521
|
+
}
|
|
522
|
+
this.implied = Object.assign(this.implied || {}, newImplied);
|
|
523
|
+
return this;
|
|
524
|
+
}
|
|
525
|
+
env(name) {
|
|
526
|
+
this.envVar = name;
|
|
527
|
+
return this;
|
|
528
|
+
}
|
|
529
|
+
argParser(fn) {
|
|
530
|
+
this.parseArg = fn;
|
|
531
|
+
return this;
|
|
532
|
+
}
|
|
533
|
+
makeOptionMandatory(mandatory = true) {
|
|
534
|
+
this.mandatory = !!mandatory;
|
|
535
|
+
return this;
|
|
536
|
+
}
|
|
537
|
+
hideHelp(hide = true) {
|
|
538
|
+
this.hidden = !!hide;
|
|
539
|
+
return this;
|
|
540
|
+
}
|
|
541
|
+
_concatValue(value, previous) {
|
|
542
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
543
|
+
return [value];
|
|
544
|
+
}
|
|
545
|
+
return previous.concat(value);
|
|
546
|
+
}
|
|
547
|
+
choices(values) {
|
|
548
|
+
this.argChoices = values.slice();
|
|
549
|
+
this.parseArg = (arg, previous) => {
|
|
550
|
+
if (!this.argChoices.includes(arg)) {
|
|
551
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
552
|
+
}
|
|
553
|
+
if (this.variadic) {
|
|
554
|
+
return this._concatValue(arg, previous);
|
|
555
|
+
}
|
|
556
|
+
return arg;
|
|
557
|
+
};
|
|
558
|
+
return this;
|
|
559
|
+
}
|
|
560
|
+
name() {
|
|
561
|
+
if (this.long) {
|
|
562
|
+
return this.long.replace(/^--/, "");
|
|
563
|
+
}
|
|
564
|
+
return this.short.replace(/^-/, "");
|
|
565
|
+
}
|
|
566
|
+
attributeName() {
|
|
567
|
+
if (this.negate) {
|
|
568
|
+
return camelcase(this.name().replace(/^no-/, ""));
|
|
569
|
+
}
|
|
570
|
+
return camelcase(this.name());
|
|
571
|
+
}
|
|
572
|
+
is(arg) {
|
|
573
|
+
return this.short === arg || this.long === arg;
|
|
574
|
+
}
|
|
575
|
+
isBoolean() {
|
|
576
|
+
return !this.required && !this.optional && !this.negate;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
class DualOptions {
|
|
581
|
+
constructor(options) {
|
|
582
|
+
this.positiveOptions = new Map;
|
|
583
|
+
this.negativeOptions = new Map;
|
|
584
|
+
this.dualOptions = new Set;
|
|
585
|
+
options.forEach((option) => {
|
|
586
|
+
if (option.negate) {
|
|
587
|
+
this.negativeOptions.set(option.attributeName(), option);
|
|
588
|
+
} else {
|
|
589
|
+
this.positiveOptions.set(option.attributeName(), option);
|
|
590
|
+
}
|
|
591
|
+
});
|
|
592
|
+
this.negativeOptions.forEach((value, key) => {
|
|
593
|
+
if (this.positiveOptions.has(key)) {
|
|
594
|
+
this.dualOptions.add(key);
|
|
595
|
+
}
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
valueFromOption(value, option) {
|
|
599
|
+
const optionKey = option.attributeName();
|
|
600
|
+
if (!this.dualOptions.has(optionKey))
|
|
601
|
+
return true;
|
|
602
|
+
const preset = this.negativeOptions.get(optionKey).presetArg;
|
|
603
|
+
const negativeValue = preset !== undefined ? preset : false;
|
|
604
|
+
return option.negate === (negativeValue === value);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
function camelcase(str) {
|
|
608
|
+
return str.split("-").reduce((str2, word) => {
|
|
609
|
+
return str2 + word[0].toUpperCase() + word.slice(1);
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
function splitOptionFlags(flags) {
|
|
613
|
+
let shortFlag;
|
|
614
|
+
let longFlag;
|
|
615
|
+
const shortFlagExp = /^-[^-]$/;
|
|
616
|
+
const longFlagExp = /^--[^-]/;
|
|
617
|
+
const flagParts = flags.split(/[ |,]+/).concat("guard");
|
|
618
|
+
if (shortFlagExp.test(flagParts[0]))
|
|
619
|
+
shortFlag = flagParts.shift();
|
|
620
|
+
if (longFlagExp.test(flagParts[0]))
|
|
621
|
+
longFlag = flagParts.shift();
|
|
622
|
+
if (!shortFlag && shortFlagExp.test(flagParts[0]))
|
|
623
|
+
shortFlag = flagParts.shift();
|
|
624
|
+
if (!shortFlag && longFlagExp.test(flagParts[0])) {
|
|
625
|
+
shortFlag = longFlag;
|
|
626
|
+
longFlag = flagParts.shift();
|
|
627
|
+
}
|
|
628
|
+
if (flagParts[0].startsWith("-")) {
|
|
629
|
+
const unsupportedFlag = flagParts[0];
|
|
630
|
+
const baseError = `option creation failed due to '${unsupportedFlag}' in option flags '${flags}'`;
|
|
631
|
+
if (/^-[^-][^-]/.test(unsupportedFlag))
|
|
632
|
+
throw new Error(`${baseError}
|
|
633
|
+
- a short flag is a single dash and a single character
|
|
634
|
+
- either use a single dash and a single character (for a short flag)
|
|
635
|
+
- or use a double dash for a long option (and can have two, like '--ws, --workspace')`);
|
|
636
|
+
if (shortFlagExp.test(unsupportedFlag))
|
|
637
|
+
throw new Error(`${baseError}
|
|
638
|
+
- too many short flags`);
|
|
639
|
+
if (longFlagExp.test(unsupportedFlag))
|
|
640
|
+
throw new Error(`${baseError}
|
|
641
|
+
- too many long flags`);
|
|
642
|
+
throw new Error(`${baseError}
|
|
643
|
+
- unrecognised flag format`);
|
|
644
|
+
}
|
|
645
|
+
if (shortFlag === undefined && longFlag === undefined)
|
|
646
|
+
throw new Error(`option creation failed due to no flags found in '${flags}'.`);
|
|
647
|
+
return { shortFlag, longFlag };
|
|
648
|
+
}
|
|
649
|
+
exports.Option = Option;
|
|
650
|
+
exports.DualOptions = DualOptions;
|
|
651
|
+
});
|
|
652
|
+
|
|
653
|
+
// node_modules/commander/lib/suggestSimilar.js
|
|
654
|
+
var require_suggestSimilar = __commonJS((exports) => {
|
|
655
|
+
var maxDistance = 3;
|
|
656
|
+
function editDistance(a, b) {
|
|
657
|
+
if (Math.abs(a.length - b.length) > maxDistance)
|
|
658
|
+
return Math.max(a.length, b.length);
|
|
659
|
+
const d = [];
|
|
660
|
+
for (let i = 0;i <= a.length; i++) {
|
|
661
|
+
d[i] = [i];
|
|
662
|
+
}
|
|
663
|
+
for (let j = 0;j <= b.length; j++) {
|
|
664
|
+
d[0][j] = j;
|
|
665
|
+
}
|
|
666
|
+
for (let j = 1;j <= b.length; j++) {
|
|
667
|
+
for (let i = 1;i <= a.length; i++) {
|
|
668
|
+
let cost = 1;
|
|
669
|
+
if (a[i - 1] === b[j - 1]) {
|
|
670
|
+
cost = 0;
|
|
671
|
+
} else {
|
|
672
|
+
cost = 1;
|
|
673
|
+
}
|
|
674
|
+
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
|
|
675
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
676
|
+
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
return d[a.length][b.length];
|
|
681
|
+
}
|
|
682
|
+
function suggestSimilar(word, candidates) {
|
|
683
|
+
if (!candidates || candidates.length === 0)
|
|
684
|
+
return "";
|
|
685
|
+
candidates = Array.from(new Set(candidates));
|
|
686
|
+
const searchingOptions = word.startsWith("--");
|
|
687
|
+
if (searchingOptions) {
|
|
688
|
+
word = word.slice(2);
|
|
689
|
+
candidates = candidates.map((candidate) => candidate.slice(2));
|
|
690
|
+
}
|
|
691
|
+
let similar = [];
|
|
692
|
+
let bestDistance = maxDistance;
|
|
693
|
+
const minSimilarity = 0.4;
|
|
694
|
+
candidates.forEach((candidate) => {
|
|
695
|
+
if (candidate.length <= 1)
|
|
696
|
+
return;
|
|
697
|
+
const distance = editDistance(word, candidate);
|
|
698
|
+
const length = Math.max(word.length, candidate.length);
|
|
699
|
+
const similarity = (length - distance) / length;
|
|
700
|
+
if (similarity > minSimilarity) {
|
|
701
|
+
if (distance < bestDistance) {
|
|
702
|
+
bestDistance = distance;
|
|
703
|
+
similar = [candidate];
|
|
704
|
+
} else if (distance === bestDistance) {
|
|
705
|
+
similar.push(candidate);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
});
|
|
709
|
+
similar.sort((a, b) => a.localeCompare(b));
|
|
710
|
+
if (searchingOptions) {
|
|
711
|
+
similar = similar.map((candidate) => `--${candidate}`);
|
|
712
|
+
}
|
|
713
|
+
if (similar.length > 1) {
|
|
714
|
+
return `
|
|
715
|
+
(Did you mean one of ${similar.join(", ")}?)`;
|
|
716
|
+
}
|
|
717
|
+
if (similar.length === 1) {
|
|
718
|
+
return `
|
|
719
|
+
(Did you mean ${similar[0]}?)`;
|
|
720
|
+
}
|
|
721
|
+
return "";
|
|
722
|
+
}
|
|
723
|
+
exports.suggestSimilar = suggestSimilar;
|
|
724
|
+
});
|
|
725
|
+
|
|
726
|
+
// node_modules/commander/lib/command.js
|
|
727
|
+
var require_command = __commonJS((exports) => {
|
|
728
|
+
var EventEmitter = __require("events").EventEmitter;
|
|
729
|
+
var childProcess = __require("child_process");
|
|
730
|
+
var path = __require("path");
|
|
731
|
+
var fs = __require("fs");
|
|
732
|
+
var process2 = __require("process");
|
|
733
|
+
var { Argument, humanReadableArgName } = require_argument();
|
|
734
|
+
var { CommanderError } = require_error();
|
|
735
|
+
var { Help, stripColor } = require_help();
|
|
736
|
+
var { Option, DualOptions } = require_option();
|
|
737
|
+
var { suggestSimilar } = require_suggestSimilar();
|
|
738
|
+
|
|
739
|
+
class Command extends EventEmitter {
|
|
740
|
+
constructor(name) {
|
|
741
|
+
super();
|
|
742
|
+
this.commands = [];
|
|
743
|
+
this.options = [];
|
|
744
|
+
this.parent = null;
|
|
745
|
+
this._allowUnknownOption = false;
|
|
746
|
+
this._allowExcessArguments = false;
|
|
747
|
+
this.registeredArguments = [];
|
|
748
|
+
this._args = this.registeredArguments;
|
|
749
|
+
this.args = [];
|
|
750
|
+
this.rawArgs = [];
|
|
751
|
+
this.processedArgs = [];
|
|
752
|
+
this._scriptPath = null;
|
|
753
|
+
this._name = name || "";
|
|
754
|
+
this._optionValues = {};
|
|
755
|
+
this._optionValueSources = {};
|
|
756
|
+
this._storeOptionsAsProperties = false;
|
|
757
|
+
this._actionHandler = null;
|
|
758
|
+
this._executableHandler = false;
|
|
759
|
+
this._executableFile = null;
|
|
760
|
+
this._executableDir = null;
|
|
761
|
+
this._defaultCommandName = null;
|
|
762
|
+
this._exitCallback = null;
|
|
763
|
+
this._aliases = [];
|
|
764
|
+
this._combineFlagAndOptionalValue = true;
|
|
765
|
+
this._description = "";
|
|
766
|
+
this._summary = "";
|
|
767
|
+
this._argsDescription = undefined;
|
|
768
|
+
this._enablePositionalOptions = false;
|
|
769
|
+
this._passThroughOptions = false;
|
|
770
|
+
this._lifeCycleHooks = {};
|
|
771
|
+
this._showHelpAfterError = false;
|
|
772
|
+
this._showSuggestionAfterError = true;
|
|
773
|
+
this._savedState = null;
|
|
774
|
+
this._outputConfiguration = {
|
|
775
|
+
writeOut: (str) => process2.stdout.write(str),
|
|
776
|
+
writeErr: (str) => process2.stderr.write(str),
|
|
777
|
+
outputError: (str, write) => write(str),
|
|
778
|
+
getOutHelpWidth: () => process2.stdout.isTTY ? process2.stdout.columns : undefined,
|
|
779
|
+
getErrHelpWidth: () => process2.stderr.isTTY ? process2.stderr.columns : undefined,
|
|
780
|
+
getOutHasColors: () => useColor() ?? (process2.stdout.isTTY && process2.stdout.hasColors?.()),
|
|
781
|
+
getErrHasColors: () => useColor() ?? (process2.stderr.isTTY && process2.stderr.hasColors?.()),
|
|
782
|
+
stripColor: (str) => stripColor(str)
|
|
783
|
+
};
|
|
784
|
+
this._hidden = false;
|
|
785
|
+
this._helpOption = undefined;
|
|
786
|
+
this._addImplicitHelpCommand = undefined;
|
|
787
|
+
this._helpCommand = undefined;
|
|
788
|
+
this._helpConfiguration = {};
|
|
789
|
+
}
|
|
790
|
+
copyInheritedSettings(sourceCommand) {
|
|
791
|
+
this._outputConfiguration = sourceCommand._outputConfiguration;
|
|
792
|
+
this._helpOption = sourceCommand._helpOption;
|
|
793
|
+
this._helpCommand = sourceCommand._helpCommand;
|
|
794
|
+
this._helpConfiguration = sourceCommand._helpConfiguration;
|
|
795
|
+
this._exitCallback = sourceCommand._exitCallback;
|
|
796
|
+
this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
|
|
797
|
+
this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
|
|
798
|
+
this._allowExcessArguments = sourceCommand._allowExcessArguments;
|
|
799
|
+
this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
|
|
800
|
+
this._showHelpAfterError = sourceCommand._showHelpAfterError;
|
|
801
|
+
this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;
|
|
802
|
+
return this;
|
|
803
|
+
}
|
|
804
|
+
_getCommandAndAncestors() {
|
|
805
|
+
const result = [];
|
|
806
|
+
for (let command = this;command; command = command.parent) {
|
|
807
|
+
result.push(command);
|
|
808
|
+
}
|
|
809
|
+
return result;
|
|
810
|
+
}
|
|
811
|
+
command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
|
|
812
|
+
let desc = actionOptsOrExecDesc;
|
|
813
|
+
let opts = execOpts;
|
|
814
|
+
if (typeof desc === "object" && desc !== null) {
|
|
815
|
+
opts = desc;
|
|
816
|
+
desc = null;
|
|
817
|
+
}
|
|
818
|
+
opts = opts || {};
|
|
819
|
+
const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
|
|
820
|
+
const cmd = this.createCommand(name);
|
|
821
|
+
if (desc) {
|
|
822
|
+
cmd.description(desc);
|
|
823
|
+
cmd._executableHandler = true;
|
|
824
|
+
}
|
|
825
|
+
if (opts.isDefault)
|
|
826
|
+
this._defaultCommandName = cmd._name;
|
|
827
|
+
cmd._hidden = !!(opts.noHelp || opts.hidden);
|
|
828
|
+
cmd._executableFile = opts.executableFile || null;
|
|
829
|
+
if (args)
|
|
830
|
+
cmd.arguments(args);
|
|
831
|
+
this._registerCommand(cmd);
|
|
832
|
+
cmd.parent = this;
|
|
833
|
+
cmd.copyInheritedSettings(this);
|
|
834
|
+
if (desc)
|
|
835
|
+
return this;
|
|
836
|
+
return cmd;
|
|
837
|
+
}
|
|
838
|
+
createCommand(name) {
|
|
839
|
+
return new Command(name);
|
|
840
|
+
}
|
|
841
|
+
createHelp() {
|
|
842
|
+
return Object.assign(new Help, this.configureHelp());
|
|
843
|
+
}
|
|
844
|
+
configureHelp(configuration) {
|
|
845
|
+
if (configuration === undefined)
|
|
846
|
+
return this._helpConfiguration;
|
|
847
|
+
this._helpConfiguration = configuration;
|
|
848
|
+
return this;
|
|
849
|
+
}
|
|
850
|
+
configureOutput(configuration) {
|
|
851
|
+
if (configuration === undefined)
|
|
852
|
+
return this._outputConfiguration;
|
|
853
|
+
Object.assign(this._outputConfiguration, configuration);
|
|
854
|
+
return this;
|
|
855
|
+
}
|
|
856
|
+
showHelpAfterError(displayHelp = true) {
|
|
857
|
+
if (typeof displayHelp !== "string")
|
|
858
|
+
displayHelp = !!displayHelp;
|
|
859
|
+
this._showHelpAfterError = displayHelp;
|
|
860
|
+
return this;
|
|
861
|
+
}
|
|
862
|
+
showSuggestionAfterError(displaySuggestion = true) {
|
|
863
|
+
this._showSuggestionAfterError = !!displaySuggestion;
|
|
864
|
+
return this;
|
|
865
|
+
}
|
|
866
|
+
addCommand(cmd, opts) {
|
|
867
|
+
if (!cmd._name) {
|
|
868
|
+
throw new Error(`Command passed to .addCommand() must have a name
|
|
869
|
+
- specify the name in Command constructor or using .name()`);
|
|
870
|
+
}
|
|
871
|
+
opts = opts || {};
|
|
872
|
+
if (opts.isDefault)
|
|
873
|
+
this._defaultCommandName = cmd._name;
|
|
874
|
+
if (opts.noHelp || opts.hidden)
|
|
875
|
+
cmd._hidden = true;
|
|
876
|
+
this._registerCommand(cmd);
|
|
877
|
+
cmd.parent = this;
|
|
878
|
+
cmd._checkForBrokenPassThrough();
|
|
879
|
+
return this;
|
|
880
|
+
}
|
|
881
|
+
createArgument(name, description) {
|
|
882
|
+
return new Argument(name, description);
|
|
883
|
+
}
|
|
884
|
+
argument(name, description, fn, defaultValue) {
|
|
885
|
+
const argument = this.createArgument(name, description);
|
|
886
|
+
if (typeof fn === "function") {
|
|
887
|
+
argument.default(defaultValue).argParser(fn);
|
|
888
|
+
} else {
|
|
889
|
+
argument.default(fn);
|
|
890
|
+
}
|
|
891
|
+
this.addArgument(argument);
|
|
892
|
+
return this;
|
|
893
|
+
}
|
|
894
|
+
arguments(names) {
|
|
895
|
+
names.trim().split(/ +/).forEach((detail) => {
|
|
896
|
+
this.argument(detail);
|
|
897
|
+
});
|
|
898
|
+
return this;
|
|
899
|
+
}
|
|
900
|
+
addArgument(argument) {
|
|
901
|
+
const previousArgument = this.registeredArguments.slice(-1)[0];
|
|
902
|
+
if (previousArgument && previousArgument.variadic) {
|
|
903
|
+
throw new Error(`only the last argument can be variadic '${previousArgument.name()}'`);
|
|
904
|
+
}
|
|
905
|
+
if (argument.required && argument.defaultValue !== undefined && argument.parseArg === undefined) {
|
|
906
|
+
throw new Error(`a default value for a required argument is never used: '${argument.name()}'`);
|
|
907
|
+
}
|
|
908
|
+
this.registeredArguments.push(argument);
|
|
909
|
+
return this;
|
|
910
|
+
}
|
|
911
|
+
helpCommand(enableOrNameAndArgs, description) {
|
|
912
|
+
if (typeof enableOrNameAndArgs === "boolean") {
|
|
913
|
+
this._addImplicitHelpCommand = enableOrNameAndArgs;
|
|
914
|
+
return this;
|
|
915
|
+
}
|
|
916
|
+
enableOrNameAndArgs = enableOrNameAndArgs ?? "help [command]";
|
|
917
|
+
const [, helpName, helpArgs] = enableOrNameAndArgs.match(/([^ ]+) *(.*)/);
|
|
918
|
+
const helpDescription = description ?? "display help for command";
|
|
919
|
+
const helpCommand = this.createCommand(helpName);
|
|
920
|
+
helpCommand.helpOption(false);
|
|
921
|
+
if (helpArgs)
|
|
922
|
+
helpCommand.arguments(helpArgs);
|
|
923
|
+
if (helpDescription)
|
|
924
|
+
helpCommand.description(helpDescription);
|
|
925
|
+
this._addImplicitHelpCommand = true;
|
|
926
|
+
this._helpCommand = helpCommand;
|
|
927
|
+
return this;
|
|
928
|
+
}
|
|
929
|
+
addHelpCommand(helpCommand, deprecatedDescription) {
|
|
930
|
+
if (typeof helpCommand !== "object") {
|
|
931
|
+
this.helpCommand(helpCommand, deprecatedDescription);
|
|
932
|
+
return this;
|
|
933
|
+
}
|
|
934
|
+
this._addImplicitHelpCommand = true;
|
|
935
|
+
this._helpCommand = helpCommand;
|
|
936
|
+
return this;
|
|
937
|
+
}
|
|
938
|
+
_getHelpCommand() {
|
|
939
|
+
const hasImplicitHelpCommand = this._addImplicitHelpCommand ?? (this.commands.length && !this._actionHandler && !this._findCommand("help"));
|
|
940
|
+
if (hasImplicitHelpCommand) {
|
|
941
|
+
if (this._helpCommand === undefined) {
|
|
942
|
+
this.helpCommand(undefined, undefined);
|
|
943
|
+
}
|
|
944
|
+
return this._helpCommand;
|
|
945
|
+
}
|
|
946
|
+
return null;
|
|
947
|
+
}
|
|
948
|
+
hook(event, listener) {
|
|
949
|
+
const allowedValues = ["preSubcommand", "preAction", "postAction"];
|
|
950
|
+
if (!allowedValues.includes(event)) {
|
|
951
|
+
throw new Error(`Unexpected value for event passed to hook : '${event}'.
|
|
952
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
953
|
+
}
|
|
954
|
+
if (this._lifeCycleHooks[event]) {
|
|
955
|
+
this._lifeCycleHooks[event].push(listener);
|
|
956
|
+
} else {
|
|
957
|
+
this._lifeCycleHooks[event] = [listener];
|
|
958
|
+
}
|
|
959
|
+
return this;
|
|
960
|
+
}
|
|
961
|
+
exitOverride(fn) {
|
|
962
|
+
if (fn) {
|
|
963
|
+
this._exitCallback = fn;
|
|
964
|
+
} else {
|
|
965
|
+
this._exitCallback = (err) => {
|
|
966
|
+
if (err.code !== "commander.executeSubCommandAsync") {
|
|
967
|
+
throw err;
|
|
968
|
+
} else {}
|
|
969
|
+
};
|
|
970
|
+
}
|
|
971
|
+
return this;
|
|
972
|
+
}
|
|
973
|
+
_exit(exitCode, code, message) {
|
|
974
|
+
if (this._exitCallback) {
|
|
975
|
+
this._exitCallback(new CommanderError(exitCode, code, message));
|
|
976
|
+
}
|
|
977
|
+
process2.exit(exitCode);
|
|
978
|
+
}
|
|
979
|
+
action(fn) {
|
|
980
|
+
const listener = (args) => {
|
|
981
|
+
const expectedArgsCount = this.registeredArguments.length;
|
|
982
|
+
const actionArgs = args.slice(0, expectedArgsCount);
|
|
983
|
+
if (this._storeOptionsAsProperties) {
|
|
984
|
+
actionArgs[expectedArgsCount] = this;
|
|
985
|
+
} else {
|
|
986
|
+
actionArgs[expectedArgsCount] = this.opts();
|
|
987
|
+
}
|
|
988
|
+
actionArgs.push(this);
|
|
989
|
+
return fn.apply(this, actionArgs);
|
|
990
|
+
};
|
|
991
|
+
this._actionHandler = listener;
|
|
992
|
+
return this;
|
|
993
|
+
}
|
|
994
|
+
createOption(flags, description) {
|
|
995
|
+
return new Option(flags, description);
|
|
996
|
+
}
|
|
997
|
+
_callParseArg(target, value, previous, invalidArgumentMessage) {
|
|
998
|
+
try {
|
|
999
|
+
return target.parseArg(value, previous);
|
|
1000
|
+
} catch (err) {
|
|
1001
|
+
if (err.code === "commander.invalidArgument") {
|
|
1002
|
+
const message = `${invalidArgumentMessage} ${err.message}`;
|
|
1003
|
+
this.error(message, { exitCode: err.exitCode, code: err.code });
|
|
1004
|
+
}
|
|
1005
|
+
throw err;
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
_registerOption(option) {
|
|
1009
|
+
const matchingOption = option.short && this._findOption(option.short) || option.long && this._findOption(option.long);
|
|
1010
|
+
if (matchingOption) {
|
|
1011
|
+
const matchingFlag = option.long && this._findOption(option.long) ? option.long : option.short;
|
|
1012
|
+
throw new Error(`Cannot add option '${option.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'
|
|
1013
|
+
- already used by option '${matchingOption.flags}'`);
|
|
1014
|
+
}
|
|
1015
|
+
this.options.push(option);
|
|
1016
|
+
}
|
|
1017
|
+
_registerCommand(command) {
|
|
1018
|
+
const knownBy = (cmd) => {
|
|
1019
|
+
return [cmd.name()].concat(cmd.aliases());
|
|
1020
|
+
};
|
|
1021
|
+
const alreadyUsed = knownBy(command).find((name) => this._findCommand(name));
|
|
1022
|
+
if (alreadyUsed) {
|
|
1023
|
+
const existingCmd = knownBy(this._findCommand(alreadyUsed)).join("|");
|
|
1024
|
+
const newCmd = knownBy(command).join("|");
|
|
1025
|
+
throw new Error(`cannot add command '${newCmd}' as already have command '${existingCmd}'`);
|
|
1026
|
+
}
|
|
1027
|
+
this.commands.push(command);
|
|
1028
|
+
}
|
|
1029
|
+
addOption(option) {
|
|
1030
|
+
this._registerOption(option);
|
|
1031
|
+
const oname = option.name();
|
|
1032
|
+
const name = option.attributeName();
|
|
1033
|
+
if (option.negate) {
|
|
1034
|
+
const positiveLongFlag = option.long.replace(/^--no-/, "--");
|
|
1035
|
+
if (!this._findOption(positiveLongFlag)) {
|
|
1036
|
+
this.setOptionValueWithSource(name, option.defaultValue === undefined ? true : option.defaultValue, "default");
|
|
1037
|
+
}
|
|
1038
|
+
} else if (option.defaultValue !== undefined) {
|
|
1039
|
+
this.setOptionValueWithSource(name, option.defaultValue, "default");
|
|
1040
|
+
}
|
|
1041
|
+
const handleOptionValue = (val, invalidValueMessage, valueSource) => {
|
|
1042
|
+
if (val == null && option.presetArg !== undefined) {
|
|
1043
|
+
val = option.presetArg;
|
|
1044
|
+
}
|
|
1045
|
+
const oldValue = this.getOptionValue(name);
|
|
1046
|
+
if (val !== null && option.parseArg) {
|
|
1047
|
+
val = this._callParseArg(option, val, oldValue, invalidValueMessage);
|
|
1048
|
+
} else if (val !== null && option.variadic) {
|
|
1049
|
+
val = option._concatValue(val, oldValue);
|
|
1050
|
+
}
|
|
1051
|
+
if (val == null) {
|
|
1052
|
+
if (option.negate) {
|
|
1053
|
+
val = false;
|
|
1054
|
+
} else if (option.isBoolean() || option.optional) {
|
|
1055
|
+
val = true;
|
|
1056
|
+
} else {
|
|
1057
|
+
val = "";
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
this.setOptionValueWithSource(name, val, valueSource);
|
|
1061
|
+
};
|
|
1062
|
+
this.on("option:" + oname, (val) => {
|
|
1063
|
+
const invalidValueMessage = `error: option '${option.flags}' argument '${val}' is invalid.`;
|
|
1064
|
+
handleOptionValue(val, invalidValueMessage, "cli");
|
|
1065
|
+
});
|
|
1066
|
+
if (option.envVar) {
|
|
1067
|
+
this.on("optionEnv:" + oname, (val) => {
|
|
1068
|
+
const invalidValueMessage = `error: option '${option.flags}' value '${val}' from env '${option.envVar}' is invalid.`;
|
|
1069
|
+
handleOptionValue(val, invalidValueMessage, "env");
|
|
1070
|
+
});
|
|
1071
|
+
}
|
|
1072
|
+
return this;
|
|
1073
|
+
}
|
|
1074
|
+
_optionEx(config, flags, description, fn, defaultValue) {
|
|
1075
|
+
if (typeof flags === "object" && flags instanceof Option) {
|
|
1076
|
+
throw new Error("To add an Option object use addOption() instead of option() or requiredOption()");
|
|
1077
|
+
}
|
|
1078
|
+
const option = this.createOption(flags, description);
|
|
1079
|
+
option.makeOptionMandatory(!!config.mandatory);
|
|
1080
|
+
if (typeof fn === "function") {
|
|
1081
|
+
option.default(defaultValue).argParser(fn);
|
|
1082
|
+
} else if (fn instanceof RegExp) {
|
|
1083
|
+
const regex = fn;
|
|
1084
|
+
fn = (val, def) => {
|
|
1085
|
+
const m = regex.exec(val);
|
|
1086
|
+
return m ? m[0] : def;
|
|
1087
|
+
};
|
|
1088
|
+
option.default(defaultValue).argParser(fn);
|
|
1089
|
+
} else {
|
|
1090
|
+
option.default(fn);
|
|
1091
|
+
}
|
|
1092
|
+
return this.addOption(option);
|
|
1093
|
+
}
|
|
1094
|
+
option(flags, description, parseArg, defaultValue) {
|
|
1095
|
+
return this._optionEx({}, flags, description, parseArg, defaultValue);
|
|
1096
|
+
}
|
|
1097
|
+
requiredOption(flags, description, parseArg, defaultValue) {
|
|
1098
|
+
return this._optionEx({ mandatory: true }, flags, description, parseArg, defaultValue);
|
|
1099
|
+
}
|
|
1100
|
+
combineFlagAndOptionalValue(combine = true) {
|
|
1101
|
+
this._combineFlagAndOptionalValue = !!combine;
|
|
1102
|
+
return this;
|
|
1103
|
+
}
|
|
1104
|
+
allowUnknownOption(allowUnknown = true) {
|
|
1105
|
+
this._allowUnknownOption = !!allowUnknown;
|
|
1106
|
+
return this;
|
|
1107
|
+
}
|
|
1108
|
+
allowExcessArguments(allowExcess = true) {
|
|
1109
|
+
this._allowExcessArguments = !!allowExcess;
|
|
1110
|
+
return this;
|
|
1111
|
+
}
|
|
1112
|
+
enablePositionalOptions(positional = true) {
|
|
1113
|
+
this._enablePositionalOptions = !!positional;
|
|
1114
|
+
return this;
|
|
1115
|
+
}
|
|
1116
|
+
passThroughOptions(passThrough = true) {
|
|
1117
|
+
this._passThroughOptions = !!passThrough;
|
|
1118
|
+
this._checkForBrokenPassThrough();
|
|
1119
|
+
return this;
|
|
1120
|
+
}
|
|
1121
|
+
_checkForBrokenPassThrough() {
|
|
1122
|
+
if (this.parent && this._passThroughOptions && !this.parent._enablePositionalOptions) {
|
|
1123
|
+
throw new Error(`passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`);
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
storeOptionsAsProperties(storeAsProperties = true) {
|
|
1127
|
+
if (this.options.length) {
|
|
1128
|
+
throw new Error("call .storeOptionsAsProperties() before adding options");
|
|
1129
|
+
}
|
|
1130
|
+
if (Object.keys(this._optionValues).length) {
|
|
1131
|
+
throw new Error("call .storeOptionsAsProperties() before setting option values");
|
|
1132
|
+
}
|
|
1133
|
+
this._storeOptionsAsProperties = !!storeAsProperties;
|
|
1134
|
+
return this;
|
|
1135
|
+
}
|
|
1136
|
+
getOptionValue(key) {
|
|
1137
|
+
if (this._storeOptionsAsProperties) {
|
|
1138
|
+
return this[key];
|
|
1139
|
+
}
|
|
1140
|
+
return this._optionValues[key];
|
|
1141
|
+
}
|
|
1142
|
+
setOptionValue(key, value) {
|
|
1143
|
+
return this.setOptionValueWithSource(key, value, undefined);
|
|
1144
|
+
}
|
|
1145
|
+
setOptionValueWithSource(key, value, source) {
|
|
1146
|
+
if (this._storeOptionsAsProperties) {
|
|
1147
|
+
this[key] = value;
|
|
1148
|
+
} else {
|
|
1149
|
+
this._optionValues[key] = value;
|
|
1150
|
+
}
|
|
1151
|
+
this._optionValueSources[key] = source;
|
|
1152
|
+
return this;
|
|
1153
|
+
}
|
|
1154
|
+
getOptionValueSource(key) {
|
|
1155
|
+
return this._optionValueSources[key];
|
|
1156
|
+
}
|
|
1157
|
+
getOptionValueSourceWithGlobals(key) {
|
|
1158
|
+
let source;
|
|
1159
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1160
|
+
if (cmd.getOptionValueSource(key) !== undefined) {
|
|
1161
|
+
source = cmd.getOptionValueSource(key);
|
|
1162
|
+
}
|
|
1163
|
+
});
|
|
1164
|
+
return source;
|
|
1165
|
+
}
|
|
1166
|
+
_prepareUserArgs(argv, parseOptions) {
|
|
1167
|
+
if (argv !== undefined && !Array.isArray(argv)) {
|
|
1168
|
+
throw new Error("first parameter to parse must be array or undefined");
|
|
1169
|
+
}
|
|
1170
|
+
parseOptions = parseOptions || {};
|
|
1171
|
+
if (argv === undefined && parseOptions.from === undefined) {
|
|
1172
|
+
if (process2.versions?.electron) {
|
|
1173
|
+
parseOptions.from = "electron";
|
|
1174
|
+
}
|
|
1175
|
+
const execArgv = process2.execArgv ?? [];
|
|
1176
|
+
if (execArgv.includes("-e") || execArgv.includes("--eval") || execArgv.includes("-p") || execArgv.includes("--print")) {
|
|
1177
|
+
parseOptions.from = "eval";
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
if (argv === undefined) {
|
|
1181
|
+
argv = process2.argv;
|
|
1182
|
+
}
|
|
1183
|
+
this.rawArgs = argv.slice();
|
|
1184
|
+
let userArgs;
|
|
1185
|
+
switch (parseOptions.from) {
|
|
1186
|
+
case undefined:
|
|
1187
|
+
case "node":
|
|
1188
|
+
this._scriptPath = argv[1];
|
|
1189
|
+
userArgs = argv.slice(2);
|
|
1190
|
+
break;
|
|
1191
|
+
case "electron":
|
|
1192
|
+
if (process2.defaultApp) {
|
|
1193
|
+
this._scriptPath = argv[1];
|
|
1194
|
+
userArgs = argv.slice(2);
|
|
1195
|
+
} else {
|
|
1196
|
+
userArgs = argv.slice(1);
|
|
1197
|
+
}
|
|
1198
|
+
break;
|
|
1199
|
+
case "user":
|
|
1200
|
+
userArgs = argv.slice(0);
|
|
1201
|
+
break;
|
|
1202
|
+
case "eval":
|
|
1203
|
+
userArgs = argv.slice(1);
|
|
1204
|
+
break;
|
|
1205
|
+
default:
|
|
1206
|
+
throw new Error(`unexpected parse option { from: '${parseOptions.from}' }`);
|
|
1207
|
+
}
|
|
1208
|
+
if (!this._name && this._scriptPath)
|
|
1209
|
+
this.nameFromFilename(this._scriptPath);
|
|
1210
|
+
this._name = this._name || "program";
|
|
1211
|
+
return userArgs;
|
|
1212
|
+
}
|
|
1213
|
+
parse(argv, parseOptions) {
|
|
1214
|
+
this._prepareForParse();
|
|
1215
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1216
|
+
this._parseCommand([], userArgs);
|
|
1217
|
+
return this;
|
|
1218
|
+
}
|
|
1219
|
+
async parseAsync(argv, parseOptions) {
|
|
1220
|
+
this._prepareForParse();
|
|
1221
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1222
|
+
await this._parseCommand([], userArgs);
|
|
1223
|
+
return this;
|
|
1224
|
+
}
|
|
1225
|
+
_prepareForParse() {
|
|
1226
|
+
if (this._savedState === null) {
|
|
1227
|
+
this.saveStateBeforeParse();
|
|
1228
|
+
} else {
|
|
1229
|
+
this.restoreStateBeforeParse();
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
saveStateBeforeParse() {
|
|
1233
|
+
this._savedState = {
|
|
1234
|
+
_name: this._name,
|
|
1235
|
+
_optionValues: { ...this._optionValues },
|
|
1236
|
+
_optionValueSources: { ...this._optionValueSources }
|
|
1237
|
+
};
|
|
1238
|
+
}
|
|
1239
|
+
restoreStateBeforeParse() {
|
|
1240
|
+
if (this._storeOptionsAsProperties)
|
|
1241
|
+
throw new Error(`Can not call parse again when storeOptionsAsProperties is true.
|
|
1242
|
+
- either make a new Command for each call to parse, or stop storing options as properties`);
|
|
1243
|
+
this._name = this._savedState._name;
|
|
1244
|
+
this._scriptPath = null;
|
|
1245
|
+
this.rawArgs = [];
|
|
1246
|
+
this._optionValues = { ...this._savedState._optionValues };
|
|
1247
|
+
this._optionValueSources = { ...this._savedState._optionValueSources };
|
|
1248
|
+
this.args = [];
|
|
1249
|
+
this.processedArgs = [];
|
|
1250
|
+
}
|
|
1251
|
+
_checkForMissingExecutable(executableFile, executableDir, subcommandName) {
|
|
1252
|
+
if (fs.existsSync(executableFile))
|
|
1253
|
+
return;
|
|
1254
|
+
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";
|
|
1255
|
+
const executableMissing = `'${executableFile}' does not exist
|
|
1256
|
+
- if '${subcommandName}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
|
|
1257
|
+
- if the default executable name is not suitable, use the executableFile option to supply a custom name or path
|
|
1258
|
+
- ${executableDirMessage}`;
|
|
1259
|
+
throw new Error(executableMissing);
|
|
1260
|
+
}
|
|
1261
|
+
_executeSubCommand(subcommand, args) {
|
|
1262
|
+
args = args.slice();
|
|
1263
|
+
let launchWithNode = false;
|
|
1264
|
+
const sourceExt = [".js", ".ts", ".tsx", ".mjs", ".cjs"];
|
|
1265
|
+
function findFile(baseDir, baseName) {
|
|
1266
|
+
const localBin = path.resolve(baseDir, baseName);
|
|
1267
|
+
if (fs.existsSync(localBin))
|
|
1268
|
+
return localBin;
|
|
1269
|
+
if (sourceExt.includes(path.extname(baseName)))
|
|
1270
|
+
return;
|
|
1271
|
+
const foundExt = sourceExt.find((ext) => fs.existsSync(`${localBin}${ext}`));
|
|
1272
|
+
if (foundExt)
|
|
1273
|
+
return `${localBin}${foundExt}`;
|
|
1274
|
+
return;
|
|
1275
|
+
}
|
|
1276
|
+
this._checkForMissingMandatoryOptions();
|
|
1277
|
+
this._checkForConflictingOptions();
|
|
1278
|
+
let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;
|
|
1279
|
+
let executableDir = this._executableDir || "";
|
|
1280
|
+
if (this._scriptPath) {
|
|
1281
|
+
let resolvedScriptPath;
|
|
1282
|
+
try {
|
|
1283
|
+
resolvedScriptPath = fs.realpathSync(this._scriptPath);
|
|
1284
|
+
} catch {
|
|
1285
|
+
resolvedScriptPath = this._scriptPath;
|
|
1286
|
+
}
|
|
1287
|
+
executableDir = path.resolve(path.dirname(resolvedScriptPath), executableDir);
|
|
1288
|
+
}
|
|
1289
|
+
if (executableDir) {
|
|
1290
|
+
let localFile = findFile(executableDir, executableFile);
|
|
1291
|
+
if (!localFile && !subcommand._executableFile && this._scriptPath) {
|
|
1292
|
+
const legacyName = path.basename(this._scriptPath, path.extname(this._scriptPath));
|
|
1293
|
+
if (legacyName !== this._name) {
|
|
1294
|
+
localFile = findFile(executableDir, `${legacyName}-${subcommand._name}`);
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
executableFile = localFile || executableFile;
|
|
1298
|
+
}
|
|
1299
|
+
launchWithNode = sourceExt.includes(path.extname(executableFile));
|
|
1300
|
+
let proc;
|
|
1301
|
+
if (process2.platform !== "win32") {
|
|
1302
|
+
if (launchWithNode) {
|
|
1303
|
+
args.unshift(executableFile);
|
|
1304
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1305
|
+
proc = childProcess.spawn(process2.argv[0], args, { stdio: "inherit" });
|
|
1306
|
+
} else {
|
|
1307
|
+
proc = childProcess.spawn(executableFile, args, { stdio: "inherit" });
|
|
1308
|
+
}
|
|
1309
|
+
} else {
|
|
1310
|
+
this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
|
|
1311
|
+
args.unshift(executableFile);
|
|
1312
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1313
|
+
proc = childProcess.spawn(process2.execPath, args, { stdio: "inherit" });
|
|
1314
|
+
}
|
|
1315
|
+
if (!proc.killed) {
|
|
1316
|
+
const signals = ["SIGUSR1", "SIGUSR2", "SIGTERM", "SIGINT", "SIGHUP"];
|
|
1317
|
+
signals.forEach((signal) => {
|
|
1318
|
+
process2.on(signal, () => {
|
|
1319
|
+
if (proc.killed === false && proc.exitCode === null) {
|
|
1320
|
+
proc.kill(signal);
|
|
1321
|
+
}
|
|
1322
|
+
});
|
|
1323
|
+
});
|
|
1324
|
+
}
|
|
1325
|
+
const exitCallback = this._exitCallback;
|
|
1326
|
+
proc.on("close", (code) => {
|
|
1327
|
+
code = code ?? 1;
|
|
1328
|
+
if (!exitCallback) {
|
|
1329
|
+
process2.exit(code);
|
|
1330
|
+
} else {
|
|
1331
|
+
exitCallback(new CommanderError(code, "commander.executeSubCommandAsync", "(close)"));
|
|
1332
|
+
}
|
|
1333
|
+
});
|
|
1334
|
+
proc.on("error", (err) => {
|
|
1335
|
+
if (err.code === "ENOENT") {
|
|
1336
|
+
this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
|
|
1337
|
+
} else if (err.code === "EACCES") {
|
|
1338
|
+
throw new Error(`'${executableFile}' not executable`);
|
|
1339
|
+
}
|
|
1340
|
+
if (!exitCallback) {
|
|
1341
|
+
process2.exit(1);
|
|
1342
|
+
} else {
|
|
1343
|
+
const wrappedError = new CommanderError(1, "commander.executeSubCommandAsync", "(error)");
|
|
1344
|
+
wrappedError.nestedError = err;
|
|
1345
|
+
exitCallback(wrappedError);
|
|
1346
|
+
}
|
|
1347
|
+
});
|
|
1348
|
+
this.runningCommand = proc;
|
|
1349
|
+
}
|
|
1350
|
+
_dispatchSubcommand(commandName, operands, unknown) {
|
|
1351
|
+
const subCommand = this._findCommand(commandName);
|
|
1352
|
+
if (!subCommand)
|
|
1353
|
+
this.help({ error: true });
|
|
1354
|
+
subCommand._prepareForParse();
|
|
1355
|
+
let promiseChain;
|
|
1356
|
+
promiseChain = this._chainOrCallSubCommandHook(promiseChain, subCommand, "preSubcommand");
|
|
1357
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
1358
|
+
if (subCommand._executableHandler) {
|
|
1359
|
+
this._executeSubCommand(subCommand, operands.concat(unknown));
|
|
1360
|
+
} else {
|
|
1361
|
+
return subCommand._parseCommand(operands, unknown);
|
|
1362
|
+
}
|
|
1363
|
+
});
|
|
1364
|
+
return promiseChain;
|
|
1365
|
+
}
|
|
1366
|
+
_dispatchHelpCommand(subcommandName) {
|
|
1367
|
+
if (!subcommandName) {
|
|
1368
|
+
this.help();
|
|
1369
|
+
}
|
|
1370
|
+
const subCommand = this._findCommand(subcommandName);
|
|
1371
|
+
if (subCommand && !subCommand._executableHandler) {
|
|
1372
|
+
subCommand.help();
|
|
1373
|
+
}
|
|
1374
|
+
return this._dispatchSubcommand(subcommandName, [], [this._getHelpOption()?.long ?? this._getHelpOption()?.short ?? "--help"]);
|
|
1375
|
+
}
|
|
1376
|
+
_checkNumberOfArguments() {
|
|
1377
|
+
this.registeredArguments.forEach((arg, i) => {
|
|
1378
|
+
if (arg.required && this.args[i] == null) {
|
|
1379
|
+
this.missingArgument(arg.name());
|
|
1380
|
+
}
|
|
1381
|
+
});
|
|
1382
|
+
if (this.registeredArguments.length > 0 && this.registeredArguments[this.registeredArguments.length - 1].variadic) {
|
|
1383
|
+
return;
|
|
1384
|
+
}
|
|
1385
|
+
if (this.args.length > this.registeredArguments.length) {
|
|
1386
|
+
this._excessArguments(this.args);
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
_processArguments() {
|
|
1390
|
+
const myParseArg = (argument, value, previous) => {
|
|
1391
|
+
let parsedValue = value;
|
|
1392
|
+
if (value !== null && argument.parseArg) {
|
|
1393
|
+
const invalidValueMessage = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'.`;
|
|
1394
|
+
parsedValue = this._callParseArg(argument, value, previous, invalidValueMessage);
|
|
1395
|
+
}
|
|
1396
|
+
return parsedValue;
|
|
1397
|
+
};
|
|
1398
|
+
this._checkNumberOfArguments();
|
|
1399
|
+
const processedArgs = [];
|
|
1400
|
+
this.registeredArguments.forEach((declaredArg, index) => {
|
|
1401
|
+
let value = declaredArg.defaultValue;
|
|
1402
|
+
if (declaredArg.variadic) {
|
|
1403
|
+
if (index < this.args.length) {
|
|
1404
|
+
value = this.args.slice(index);
|
|
1405
|
+
if (declaredArg.parseArg) {
|
|
1406
|
+
value = value.reduce((processed, v) => {
|
|
1407
|
+
return myParseArg(declaredArg, v, processed);
|
|
1408
|
+
}, declaredArg.defaultValue);
|
|
1409
|
+
}
|
|
1410
|
+
} else if (value === undefined) {
|
|
1411
|
+
value = [];
|
|
1412
|
+
}
|
|
1413
|
+
} else if (index < this.args.length) {
|
|
1414
|
+
value = this.args[index];
|
|
1415
|
+
if (declaredArg.parseArg) {
|
|
1416
|
+
value = myParseArg(declaredArg, value, declaredArg.defaultValue);
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
processedArgs[index] = value;
|
|
1420
|
+
});
|
|
1421
|
+
this.processedArgs = processedArgs;
|
|
1422
|
+
}
|
|
1423
|
+
_chainOrCall(promise, fn) {
|
|
1424
|
+
if (promise && promise.then && typeof promise.then === "function") {
|
|
1425
|
+
return promise.then(() => fn());
|
|
1426
|
+
}
|
|
1427
|
+
return fn();
|
|
1428
|
+
}
|
|
1429
|
+
_chainOrCallHooks(promise, event) {
|
|
1430
|
+
let result = promise;
|
|
1431
|
+
const hooks = [];
|
|
1432
|
+
this._getCommandAndAncestors().reverse().filter((cmd) => cmd._lifeCycleHooks[event] !== undefined).forEach((hookedCommand) => {
|
|
1433
|
+
hookedCommand._lifeCycleHooks[event].forEach((callback) => {
|
|
1434
|
+
hooks.push({ hookedCommand, callback });
|
|
1435
|
+
});
|
|
1436
|
+
});
|
|
1437
|
+
if (event === "postAction") {
|
|
1438
|
+
hooks.reverse();
|
|
1439
|
+
}
|
|
1440
|
+
hooks.forEach((hookDetail) => {
|
|
1441
|
+
result = this._chainOrCall(result, () => {
|
|
1442
|
+
return hookDetail.callback(hookDetail.hookedCommand, this);
|
|
1443
|
+
});
|
|
1444
|
+
});
|
|
1445
|
+
return result;
|
|
1446
|
+
}
|
|
1447
|
+
_chainOrCallSubCommandHook(promise, subCommand, event) {
|
|
1448
|
+
let result = promise;
|
|
1449
|
+
if (this._lifeCycleHooks[event] !== undefined) {
|
|
1450
|
+
this._lifeCycleHooks[event].forEach((hook) => {
|
|
1451
|
+
result = this._chainOrCall(result, () => {
|
|
1452
|
+
return hook(this, subCommand);
|
|
1453
|
+
});
|
|
1454
|
+
});
|
|
1455
|
+
}
|
|
1456
|
+
return result;
|
|
1457
|
+
}
|
|
1458
|
+
_parseCommand(operands, unknown) {
|
|
1459
|
+
const parsed = this.parseOptions(unknown);
|
|
1460
|
+
this._parseOptionsEnv();
|
|
1461
|
+
this._parseOptionsImplied();
|
|
1462
|
+
operands = operands.concat(parsed.operands);
|
|
1463
|
+
unknown = parsed.unknown;
|
|
1464
|
+
this.args = operands.concat(unknown);
|
|
1465
|
+
if (operands && this._findCommand(operands[0])) {
|
|
1466
|
+
return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
|
|
1467
|
+
}
|
|
1468
|
+
if (this._getHelpCommand() && operands[0] === this._getHelpCommand().name()) {
|
|
1469
|
+
return this._dispatchHelpCommand(operands[1]);
|
|
1470
|
+
}
|
|
1471
|
+
if (this._defaultCommandName) {
|
|
1472
|
+
this._outputHelpIfRequested(unknown);
|
|
1473
|
+
return this._dispatchSubcommand(this._defaultCommandName, operands, unknown);
|
|
1474
|
+
}
|
|
1475
|
+
if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) {
|
|
1476
|
+
this.help({ error: true });
|
|
1477
|
+
}
|
|
1478
|
+
this._outputHelpIfRequested(parsed.unknown);
|
|
1479
|
+
this._checkForMissingMandatoryOptions();
|
|
1480
|
+
this._checkForConflictingOptions();
|
|
1481
|
+
const checkForUnknownOptions = () => {
|
|
1482
|
+
if (parsed.unknown.length > 0) {
|
|
1483
|
+
this.unknownOption(parsed.unknown[0]);
|
|
1484
|
+
}
|
|
1485
|
+
};
|
|
1486
|
+
const commandEvent = `command:${this.name()}`;
|
|
1487
|
+
if (this._actionHandler) {
|
|
1488
|
+
checkForUnknownOptions();
|
|
1489
|
+
this._processArguments();
|
|
1490
|
+
let promiseChain;
|
|
1491
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "preAction");
|
|
1492
|
+
promiseChain = this._chainOrCall(promiseChain, () => this._actionHandler(this.processedArgs));
|
|
1493
|
+
if (this.parent) {
|
|
1494
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
1495
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1496
|
+
});
|
|
1497
|
+
}
|
|
1498
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "postAction");
|
|
1499
|
+
return promiseChain;
|
|
1500
|
+
}
|
|
1501
|
+
if (this.parent && this.parent.listenerCount(commandEvent)) {
|
|
1502
|
+
checkForUnknownOptions();
|
|
1503
|
+
this._processArguments();
|
|
1504
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1505
|
+
} else if (operands.length) {
|
|
1506
|
+
if (this._findCommand("*")) {
|
|
1507
|
+
return this._dispatchSubcommand("*", operands, unknown);
|
|
1508
|
+
}
|
|
1509
|
+
if (this.listenerCount("command:*")) {
|
|
1510
|
+
this.emit("command:*", operands, unknown);
|
|
1511
|
+
} else if (this.commands.length) {
|
|
1512
|
+
this.unknownCommand();
|
|
1513
|
+
} else {
|
|
1514
|
+
checkForUnknownOptions();
|
|
1515
|
+
this._processArguments();
|
|
1516
|
+
}
|
|
1517
|
+
} else if (this.commands.length) {
|
|
1518
|
+
checkForUnknownOptions();
|
|
1519
|
+
this.help({ error: true });
|
|
1520
|
+
} else {
|
|
1521
|
+
checkForUnknownOptions();
|
|
1522
|
+
this._processArguments();
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
_findCommand(name) {
|
|
1526
|
+
if (!name)
|
|
1527
|
+
return;
|
|
1528
|
+
return this.commands.find((cmd) => cmd._name === name || cmd._aliases.includes(name));
|
|
1529
|
+
}
|
|
1530
|
+
_findOption(arg) {
|
|
1531
|
+
return this.options.find((option) => option.is(arg));
|
|
1532
|
+
}
|
|
1533
|
+
_checkForMissingMandatoryOptions() {
|
|
1534
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1535
|
+
cmd.options.forEach((anOption) => {
|
|
1536
|
+
if (anOption.mandatory && cmd.getOptionValue(anOption.attributeName()) === undefined) {
|
|
1537
|
+
cmd.missingMandatoryOptionValue(anOption);
|
|
1538
|
+
}
|
|
1539
|
+
});
|
|
1540
|
+
});
|
|
1541
|
+
}
|
|
1542
|
+
_checkForConflictingLocalOptions() {
|
|
1543
|
+
const definedNonDefaultOptions = this.options.filter((option) => {
|
|
1544
|
+
const optionKey = option.attributeName();
|
|
1545
|
+
if (this.getOptionValue(optionKey) === undefined) {
|
|
1546
|
+
return false;
|
|
1547
|
+
}
|
|
1548
|
+
return this.getOptionValueSource(optionKey) !== "default";
|
|
1549
|
+
});
|
|
1550
|
+
const optionsWithConflicting = definedNonDefaultOptions.filter((option) => option.conflictsWith.length > 0);
|
|
1551
|
+
optionsWithConflicting.forEach((option) => {
|
|
1552
|
+
const conflictingAndDefined = definedNonDefaultOptions.find((defined) => option.conflictsWith.includes(defined.attributeName()));
|
|
1553
|
+
if (conflictingAndDefined) {
|
|
1554
|
+
this._conflictingOption(option, conflictingAndDefined);
|
|
1555
|
+
}
|
|
1556
|
+
});
|
|
1557
|
+
}
|
|
1558
|
+
_checkForConflictingOptions() {
|
|
1559
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1560
|
+
cmd._checkForConflictingLocalOptions();
|
|
1561
|
+
});
|
|
1562
|
+
}
|
|
1563
|
+
parseOptions(argv) {
|
|
1564
|
+
const operands = [];
|
|
1565
|
+
const unknown = [];
|
|
1566
|
+
let dest = operands;
|
|
1567
|
+
const args = argv.slice();
|
|
1568
|
+
function maybeOption(arg) {
|
|
1569
|
+
return arg.length > 1 && arg[0] === "-";
|
|
1570
|
+
}
|
|
1571
|
+
let activeVariadicOption = null;
|
|
1572
|
+
while (args.length) {
|
|
1573
|
+
const arg = args.shift();
|
|
1574
|
+
if (arg === "--") {
|
|
1575
|
+
if (dest === unknown)
|
|
1576
|
+
dest.push(arg);
|
|
1577
|
+
dest.push(...args);
|
|
1578
|
+
break;
|
|
1579
|
+
}
|
|
1580
|
+
if (activeVariadicOption && !maybeOption(arg)) {
|
|
1581
|
+
this.emit(`option:${activeVariadicOption.name()}`, arg);
|
|
1582
|
+
continue;
|
|
1583
|
+
}
|
|
1584
|
+
activeVariadicOption = null;
|
|
1585
|
+
if (maybeOption(arg)) {
|
|
1586
|
+
const option = this._findOption(arg);
|
|
1587
|
+
if (option) {
|
|
1588
|
+
if (option.required) {
|
|
1589
|
+
const value = args.shift();
|
|
1590
|
+
if (value === undefined)
|
|
1591
|
+
this.optionMissingArgument(option);
|
|
1592
|
+
this.emit(`option:${option.name()}`, value);
|
|
1593
|
+
} else if (option.optional) {
|
|
1594
|
+
let value = null;
|
|
1595
|
+
if (args.length > 0 && !maybeOption(args[0])) {
|
|
1596
|
+
value = args.shift();
|
|
1597
|
+
}
|
|
1598
|
+
this.emit(`option:${option.name()}`, value);
|
|
1599
|
+
} else {
|
|
1600
|
+
this.emit(`option:${option.name()}`);
|
|
1601
|
+
}
|
|
1602
|
+
activeVariadicOption = option.variadic ? option : null;
|
|
1603
|
+
continue;
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
if (arg.length > 2 && arg[0] === "-" && arg[1] !== "-") {
|
|
1607
|
+
const option = this._findOption(`-${arg[1]}`);
|
|
1608
|
+
if (option) {
|
|
1609
|
+
if (option.required || option.optional && this._combineFlagAndOptionalValue) {
|
|
1610
|
+
this.emit(`option:${option.name()}`, arg.slice(2));
|
|
1611
|
+
} else {
|
|
1612
|
+
this.emit(`option:${option.name()}`);
|
|
1613
|
+
args.unshift(`-${arg.slice(2)}`);
|
|
1614
|
+
}
|
|
1615
|
+
continue;
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
if (/^--[^=]+=/.test(arg)) {
|
|
1619
|
+
const index = arg.indexOf("=");
|
|
1620
|
+
const option = this._findOption(arg.slice(0, index));
|
|
1621
|
+
if (option && (option.required || option.optional)) {
|
|
1622
|
+
this.emit(`option:${option.name()}`, arg.slice(index + 1));
|
|
1623
|
+
continue;
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
if (maybeOption(arg)) {
|
|
1627
|
+
dest = unknown;
|
|
1628
|
+
}
|
|
1629
|
+
if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
|
|
1630
|
+
if (this._findCommand(arg)) {
|
|
1631
|
+
operands.push(arg);
|
|
1632
|
+
if (args.length > 0)
|
|
1633
|
+
unknown.push(...args);
|
|
1634
|
+
break;
|
|
1635
|
+
} else if (this._getHelpCommand() && arg === this._getHelpCommand().name()) {
|
|
1636
|
+
operands.push(arg);
|
|
1637
|
+
if (args.length > 0)
|
|
1638
|
+
operands.push(...args);
|
|
1639
|
+
break;
|
|
1640
|
+
} else if (this._defaultCommandName) {
|
|
1641
|
+
unknown.push(arg);
|
|
1642
|
+
if (args.length > 0)
|
|
1643
|
+
unknown.push(...args);
|
|
1644
|
+
break;
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
if (this._passThroughOptions) {
|
|
1648
|
+
dest.push(arg);
|
|
1649
|
+
if (args.length > 0)
|
|
1650
|
+
dest.push(...args);
|
|
1651
|
+
break;
|
|
1652
|
+
}
|
|
1653
|
+
dest.push(arg);
|
|
1654
|
+
}
|
|
1655
|
+
return { operands, unknown };
|
|
1656
|
+
}
|
|
1657
|
+
opts() {
|
|
1658
|
+
if (this._storeOptionsAsProperties) {
|
|
1659
|
+
const result = {};
|
|
1660
|
+
const len = this.options.length;
|
|
1661
|
+
for (let i = 0;i < len; i++) {
|
|
1662
|
+
const key = this.options[i].attributeName();
|
|
1663
|
+
result[key] = key === this._versionOptionName ? this._version : this[key];
|
|
1664
|
+
}
|
|
1665
|
+
return result;
|
|
1666
|
+
}
|
|
1667
|
+
return this._optionValues;
|
|
1668
|
+
}
|
|
1669
|
+
optsWithGlobals() {
|
|
1670
|
+
return this._getCommandAndAncestors().reduce((combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()), {});
|
|
1671
|
+
}
|
|
1672
|
+
error(message, errorOptions) {
|
|
1673
|
+
this._outputConfiguration.outputError(`${message}
|
|
1674
|
+
`, this._outputConfiguration.writeErr);
|
|
1675
|
+
if (typeof this._showHelpAfterError === "string") {
|
|
1676
|
+
this._outputConfiguration.writeErr(`${this._showHelpAfterError}
|
|
1677
|
+
`);
|
|
1678
|
+
} else if (this._showHelpAfterError) {
|
|
1679
|
+
this._outputConfiguration.writeErr(`
|
|
1680
|
+
`);
|
|
1681
|
+
this.outputHelp({ error: true });
|
|
1682
|
+
}
|
|
1683
|
+
const config = errorOptions || {};
|
|
1684
|
+
const exitCode = config.exitCode || 1;
|
|
1685
|
+
const code = config.code || "commander.error";
|
|
1686
|
+
this._exit(exitCode, code, message);
|
|
1687
|
+
}
|
|
1688
|
+
_parseOptionsEnv() {
|
|
1689
|
+
this.options.forEach((option) => {
|
|
1690
|
+
if (option.envVar && option.envVar in process2.env) {
|
|
1691
|
+
const optionKey = option.attributeName();
|
|
1692
|
+
if (this.getOptionValue(optionKey) === undefined || ["default", "config", "env"].includes(this.getOptionValueSource(optionKey))) {
|
|
1693
|
+
if (option.required || option.optional) {
|
|
1694
|
+
this.emit(`optionEnv:${option.name()}`, process2.env[option.envVar]);
|
|
1695
|
+
} else {
|
|
1696
|
+
this.emit(`optionEnv:${option.name()}`);
|
|
1697
|
+
}
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1700
|
+
});
|
|
1701
|
+
}
|
|
1702
|
+
_parseOptionsImplied() {
|
|
1703
|
+
const dualHelper = new DualOptions(this.options);
|
|
1704
|
+
const hasCustomOptionValue = (optionKey) => {
|
|
1705
|
+
return this.getOptionValue(optionKey) !== undefined && !["default", "implied"].includes(this.getOptionValueSource(optionKey));
|
|
1706
|
+
};
|
|
1707
|
+
this.options.filter((option) => option.implied !== undefined && hasCustomOptionValue(option.attributeName()) && dualHelper.valueFromOption(this.getOptionValue(option.attributeName()), option)).forEach((option) => {
|
|
1708
|
+
Object.keys(option.implied).filter((impliedKey) => !hasCustomOptionValue(impliedKey)).forEach((impliedKey) => {
|
|
1709
|
+
this.setOptionValueWithSource(impliedKey, option.implied[impliedKey], "implied");
|
|
1710
|
+
});
|
|
1711
|
+
});
|
|
1712
|
+
}
|
|
1713
|
+
missingArgument(name) {
|
|
1714
|
+
const message = `error: missing required argument '${name}'`;
|
|
1715
|
+
this.error(message, { code: "commander.missingArgument" });
|
|
1716
|
+
}
|
|
1717
|
+
optionMissingArgument(option) {
|
|
1718
|
+
const message = `error: option '${option.flags}' argument missing`;
|
|
1719
|
+
this.error(message, { code: "commander.optionMissingArgument" });
|
|
1720
|
+
}
|
|
1721
|
+
missingMandatoryOptionValue(option) {
|
|
1722
|
+
const message = `error: required option '${option.flags}' not specified`;
|
|
1723
|
+
this.error(message, { code: "commander.missingMandatoryOptionValue" });
|
|
1724
|
+
}
|
|
1725
|
+
_conflictingOption(option, conflictingOption) {
|
|
1726
|
+
const findBestOptionFromValue = (option2) => {
|
|
1727
|
+
const optionKey = option2.attributeName();
|
|
1728
|
+
const optionValue = this.getOptionValue(optionKey);
|
|
1729
|
+
const negativeOption = this.options.find((target) => target.negate && optionKey === target.attributeName());
|
|
1730
|
+
const positiveOption = this.options.find((target) => !target.negate && optionKey === target.attributeName());
|
|
1731
|
+
if (negativeOption && (negativeOption.presetArg === undefined && optionValue === false || negativeOption.presetArg !== undefined && optionValue === negativeOption.presetArg)) {
|
|
1732
|
+
return negativeOption;
|
|
1733
|
+
}
|
|
1734
|
+
return positiveOption || option2;
|
|
1735
|
+
};
|
|
1736
|
+
const getErrorMessage = (option2) => {
|
|
1737
|
+
const bestOption = findBestOptionFromValue(option2);
|
|
1738
|
+
const optionKey = bestOption.attributeName();
|
|
1739
|
+
const source = this.getOptionValueSource(optionKey);
|
|
1740
|
+
if (source === "env") {
|
|
1741
|
+
return `environment variable '${bestOption.envVar}'`;
|
|
1742
|
+
}
|
|
1743
|
+
return `option '${bestOption.flags}'`;
|
|
1744
|
+
};
|
|
1745
|
+
const message = `error: ${getErrorMessage(option)} cannot be used with ${getErrorMessage(conflictingOption)}`;
|
|
1746
|
+
this.error(message, { code: "commander.conflictingOption" });
|
|
1747
|
+
}
|
|
1748
|
+
unknownOption(flag) {
|
|
1749
|
+
if (this._allowUnknownOption)
|
|
1750
|
+
return;
|
|
1751
|
+
let suggestion = "";
|
|
1752
|
+
if (flag.startsWith("--") && this._showSuggestionAfterError) {
|
|
1753
|
+
let candidateFlags = [];
|
|
1754
|
+
let command = this;
|
|
1755
|
+
do {
|
|
1756
|
+
const moreFlags = command.createHelp().visibleOptions(command).filter((option) => option.long).map((option) => option.long);
|
|
1757
|
+
candidateFlags = candidateFlags.concat(moreFlags);
|
|
1758
|
+
command = command.parent;
|
|
1759
|
+
} while (command && !command._enablePositionalOptions);
|
|
1760
|
+
suggestion = suggestSimilar(flag, candidateFlags);
|
|
1761
|
+
}
|
|
1762
|
+
const message = `error: unknown option '${flag}'${suggestion}`;
|
|
1763
|
+
this.error(message, { code: "commander.unknownOption" });
|
|
1764
|
+
}
|
|
1765
|
+
_excessArguments(receivedArgs) {
|
|
1766
|
+
if (this._allowExcessArguments)
|
|
1767
|
+
return;
|
|
1768
|
+
const expected = this.registeredArguments.length;
|
|
1769
|
+
const s = expected === 1 ? "" : "s";
|
|
1770
|
+
const forSubcommand = this.parent ? ` for '${this.name()}'` : "";
|
|
1771
|
+
const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;
|
|
1772
|
+
this.error(message, { code: "commander.excessArguments" });
|
|
1773
|
+
}
|
|
1774
|
+
unknownCommand() {
|
|
1775
|
+
const unknownName = this.args[0];
|
|
1776
|
+
let suggestion = "";
|
|
1777
|
+
if (this._showSuggestionAfterError) {
|
|
1778
|
+
const candidateNames = [];
|
|
1779
|
+
this.createHelp().visibleCommands(this).forEach((command) => {
|
|
1780
|
+
candidateNames.push(command.name());
|
|
1781
|
+
if (command.alias())
|
|
1782
|
+
candidateNames.push(command.alias());
|
|
1783
|
+
});
|
|
1784
|
+
suggestion = suggestSimilar(unknownName, candidateNames);
|
|
1785
|
+
}
|
|
1786
|
+
const message = `error: unknown command '${unknownName}'${suggestion}`;
|
|
1787
|
+
this.error(message, { code: "commander.unknownCommand" });
|
|
1788
|
+
}
|
|
1789
|
+
version(str, flags, description) {
|
|
1790
|
+
if (str === undefined)
|
|
1791
|
+
return this._version;
|
|
1792
|
+
this._version = str;
|
|
1793
|
+
flags = flags || "-V, --version";
|
|
1794
|
+
description = description || "output the version number";
|
|
1795
|
+
const versionOption = this.createOption(flags, description);
|
|
1796
|
+
this._versionOptionName = versionOption.attributeName();
|
|
1797
|
+
this._registerOption(versionOption);
|
|
1798
|
+
this.on("option:" + versionOption.name(), () => {
|
|
1799
|
+
this._outputConfiguration.writeOut(`${str}
|
|
1800
|
+
`);
|
|
1801
|
+
this._exit(0, "commander.version", str);
|
|
1802
|
+
});
|
|
1803
|
+
return this;
|
|
1804
|
+
}
|
|
1805
|
+
description(str, argsDescription) {
|
|
1806
|
+
if (str === undefined && argsDescription === undefined)
|
|
1807
|
+
return this._description;
|
|
1808
|
+
this._description = str;
|
|
1809
|
+
if (argsDescription) {
|
|
1810
|
+
this._argsDescription = argsDescription;
|
|
1811
|
+
}
|
|
1812
|
+
return this;
|
|
1813
|
+
}
|
|
1814
|
+
summary(str) {
|
|
1815
|
+
if (str === undefined)
|
|
1816
|
+
return this._summary;
|
|
1817
|
+
this._summary = str;
|
|
1818
|
+
return this;
|
|
1819
|
+
}
|
|
1820
|
+
alias(alias) {
|
|
1821
|
+
if (alias === undefined)
|
|
1822
|
+
return this._aliases[0];
|
|
1823
|
+
let command = this;
|
|
1824
|
+
if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
|
|
1825
|
+
command = this.commands[this.commands.length - 1];
|
|
1826
|
+
}
|
|
1827
|
+
if (alias === command._name)
|
|
1828
|
+
throw new Error("Command alias can't be the same as its name");
|
|
1829
|
+
const matchingCommand = this.parent?._findCommand(alias);
|
|
1830
|
+
if (matchingCommand) {
|
|
1831
|
+
const existingCmd = [matchingCommand.name()].concat(matchingCommand.aliases()).join("|");
|
|
1832
|
+
throw new Error(`cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`);
|
|
1833
|
+
}
|
|
1834
|
+
command._aliases.push(alias);
|
|
1835
|
+
return this;
|
|
1836
|
+
}
|
|
1837
|
+
aliases(aliases) {
|
|
1838
|
+
if (aliases === undefined)
|
|
1839
|
+
return this._aliases;
|
|
1840
|
+
aliases.forEach((alias) => this.alias(alias));
|
|
1841
|
+
return this;
|
|
1842
|
+
}
|
|
1843
|
+
usage(str) {
|
|
1844
|
+
if (str === undefined) {
|
|
1845
|
+
if (this._usage)
|
|
1846
|
+
return this._usage;
|
|
1847
|
+
const args = this.registeredArguments.map((arg) => {
|
|
1848
|
+
return humanReadableArgName(arg);
|
|
1849
|
+
});
|
|
1850
|
+
return [].concat(this.options.length || this._helpOption !== null ? "[options]" : [], this.commands.length ? "[command]" : [], this.registeredArguments.length ? args : []).join(" ");
|
|
1851
|
+
}
|
|
1852
|
+
this._usage = str;
|
|
1853
|
+
return this;
|
|
1854
|
+
}
|
|
1855
|
+
name(str) {
|
|
1856
|
+
if (str === undefined)
|
|
1857
|
+
return this._name;
|
|
1858
|
+
this._name = str;
|
|
1859
|
+
return this;
|
|
1860
|
+
}
|
|
1861
|
+
nameFromFilename(filename) {
|
|
1862
|
+
this._name = path.basename(filename, path.extname(filename));
|
|
1863
|
+
return this;
|
|
1864
|
+
}
|
|
1865
|
+
executableDir(path2) {
|
|
1866
|
+
if (path2 === undefined)
|
|
1867
|
+
return this._executableDir;
|
|
1868
|
+
this._executableDir = path2;
|
|
1869
|
+
return this;
|
|
1870
|
+
}
|
|
1871
|
+
helpInformation(contextOptions) {
|
|
1872
|
+
const helper = this.createHelp();
|
|
1873
|
+
const context = this._getOutputContext(contextOptions);
|
|
1874
|
+
helper.prepareContext({
|
|
1875
|
+
error: context.error,
|
|
1876
|
+
helpWidth: context.helpWidth,
|
|
1877
|
+
outputHasColors: context.hasColors
|
|
1878
|
+
});
|
|
1879
|
+
const text = helper.formatHelp(this, helper);
|
|
1880
|
+
if (context.hasColors)
|
|
1881
|
+
return text;
|
|
1882
|
+
return this._outputConfiguration.stripColor(text);
|
|
1883
|
+
}
|
|
1884
|
+
_getOutputContext(contextOptions) {
|
|
1885
|
+
contextOptions = contextOptions || {};
|
|
1886
|
+
const error = !!contextOptions.error;
|
|
1887
|
+
let baseWrite;
|
|
1888
|
+
let hasColors;
|
|
1889
|
+
let helpWidth;
|
|
1890
|
+
if (error) {
|
|
1891
|
+
baseWrite = (str) => this._outputConfiguration.writeErr(str);
|
|
1892
|
+
hasColors = this._outputConfiguration.getErrHasColors();
|
|
1893
|
+
helpWidth = this._outputConfiguration.getErrHelpWidth();
|
|
1894
|
+
} else {
|
|
1895
|
+
baseWrite = (str) => this._outputConfiguration.writeOut(str);
|
|
1896
|
+
hasColors = this._outputConfiguration.getOutHasColors();
|
|
1897
|
+
helpWidth = this._outputConfiguration.getOutHelpWidth();
|
|
1898
|
+
}
|
|
1899
|
+
const write = (str) => {
|
|
1900
|
+
if (!hasColors)
|
|
1901
|
+
str = this._outputConfiguration.stripColor(str);
|
|
1902
|
+
return baseWrite(str);
|
|
1903
|
+
};
|
|
1904
|
+
return { error, write, hasColors, helpWidth };
|
|
1905
|
+
}
|
|
1906
|
+
outputHelp(contextOptions) {
|
|
1907
|
+
let deprecatedCallback;
|
|
1908
|
+
if (typeof contextOptions === "function") {
|
|
1909
|
+
deprecatedCallback = contextOptions;
|
|
1910
|
+
contextOptions = undefined;
|
|
1911
|
+
}
|
|
1912
|
+
const outputContext = this._getOutputContext(contextOptions);
|
|
1913
|
+
const eventContext = {
|
|
1914
|
+
error: outputContext.error,
|
|
1915
|
+
write: outputContext.write,
|
|
1916
|
+
command: this
|
|
1917
|
+
};
|
|
1918
|
+
this._getCommandAndAncestors().reverse().forEach((command) => command.emit("beforeAllHelp", eventContext));
|
|
1919
|
+
this.emit("beforeHelp", eventContext);
|
|
1920
|
+
let helpInformation = this.helpInformation({ error: outputContext.error });
|
|
1921
|
+
if (deprecatedCallback) {
|
|
1922
|
+
helpInformation = deprecatedCallback(helpInformation);
|
|
1923
|
+
if (typeof helpInformation !== "string" && !Buffer.isBuffer(helpInformation)) {
|
|
1924
|
+
throw new Error("outputHelp callback must return a string or a Buffer");
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1927
|
+
outputContext.write(helpInformation);
|
|
1928
|
+
if (this._getHelpOption()?.long) {
|
|
1929
|
+
this.emit(this._getHelpOption().long);
|
|
1930
|
+
}
|
|
1931
|
+
this.emit("afterHelp", eventContext);
|
|
1932
|
+
this._getCommandAndAncestors().forEach((command) => command.emit("afterAllHelp", eventContext));
|
|
1933
|
+
}
|
|
1934
|
+
helpOption(flags, description) {
|
|
1935
|
+
if (typeof flags === "boolean") {
|
|
1936
|
+
if (flags) {
|
|
1937
|
+
this._helpOption = this._helpOption ?? undefined;
|
|
1938
|
+
} else {
|
|
1939
|
+
this._helpOption = null;
|
|
1940
|
+
}
|
|
1941
|
+
return this;
|
|
1942
|
+
}
|
|
1943
|
+
flags = flags ?? "-h, --help";
|
|
1944
|
+
description = description ?? "display help for command";
|
|
1945
|
+
this._helpOption = this.createOption(flags, description);
|
|
1946
|
+
return this;
|
|
1947
|
+
}
|
|
1948
|
+
_getHelpOption() {
|
|
1949
|
+
if (this._helpOption === undefined) {
|
|
1950
|
+
this.helpOption(undefined, undefined);
|
|
1951
|
+
}
|
|
1952
|
+
return this._helpOption;
|
|
1953
|
+
}
|
|
1954
|
+
addHelpOption(option) {
|
|
1955
|
+
this._helpOption = option;
|
|
1956
|
+
return this;
|
|
1957
|
+
}
|
|
1958
|
+
help(contextOptions) {
|
|
1959
|
+
this.outputHelp(contextOptions);
|
|
1960
|
+
let exitCode = Number(process2.exitCode ?? 0);
|
|
1961
|
+
if (exitCode === 0 && contextOptions && typeof contextOptions !== "function" && contextOptions.error) {
|
|
1962
|
+
exitCode = 1;
|
|
1963
|
+
}
|
|
1964
|
+
this._exit(exitCode, "commander.help", "(outputHelp)");
|
|
1965
|
+
}
|
|
1966
|
+
addHelpText(position, text) {
|
|
1967
|
+
const allowedValues = ["beforeAll", "before", "after", "afterAll"];
|
|
1968
|
+
if (!allowedValues.includes(position)) {
|
|
1969
|
+
throw new Error(`Unexpected value for position to addHelpText.
|
|
1970
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
1971
|
+
}
|
|
1972
|
+
const helpEvent = `${position}Help`;
|
|
1973
|
+
this.on(helpEvent, (context) => {
|
|
1974
|
+
let helpStr;
|
|
1975
|
+
if (typeof text === "function") {
|
|
1976
|
+
helpStr = text({ error: context.error, command: context.command });
|
|
1977
|
+
} else {
|
|
1978
|
+
helpStr = text;
|
|
1979
|
+
}
|
|
1980
|
+
if (helpStr) {
|
|
1981
|
+
context.write(`${helpStr}
|
|
1982
|
+
`);
|
|
1983
|
+
}
|
|
1984
|
+
});
|
|
1985
|
+
return this;
|
|
1986
|
+
}
|
|
1987
|
+
_outputHelpIfRequested(args) {
|
|
1988
|
+
const helpOption = this._getHelpOption();
|
|
1989
|
+
const helpRequested = helpOption && args.find((arg) => helpOption.is(arg));
|
|
1990
|
+
if (helpRequested) {
|
|
1991
|
+
this.outputHelp();
|
|
1992
|
+
this._exit(0, "commander.helpDisplayed", "(outputHelp)");
|
|
1993
|
+
}
|
|
1994
|
+
}
|
|
1995
|
+
}
|
|
1996
|
+
function incrementNodeInspectorPort(args) {
|
|
1997
|
+
return args.map((arg) => {
|
|
1998
|
+
if (!arg.startsWith("--inspect")) {
|
|
1999
|
+
return arg;
|
|
2000
|
+
}
|
|
2001
|
+
let debugOption;
|
|
2002
|
+
let debugHost = "127.0.0.1";
|
|
2003
|
+
let debugPort = "9229";
|
|
2004
|
+
let match;
|
|
2005
|
+
if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
|
|
2006
|
+
debugOption = match[1];
|
|
2007
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
|
|
2008
|
+
debugOption = match[1];
|
|
2009
|
+
if (/^\d+$/.test(match[3])) {
|
|
2010
|
+
debugPort = match[3];
|
|
2011
|
+
} else {
|
|
2012
|
+
debugHost = match[3];
|
|
2013
|
+
}
|
|
2014
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) {
|
|
2015
|
+
debugOption = match[1];
|
|
2016
|
+
debugHost = match[3];
|
|
2017
|
+
debugPort = match[4];
|
|
2018
|
+
}
|
|
2019
|
+
if (debugOption && debugPort !== "0") {
|
|
2020
|
+
return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
|
|
2021
|
+
}
|
|
2022
|
+
return arg;
|
|
2023
|
+
});
|
|
2024
|
+
}
|
|
2025
|
+
function useColor() {
|
|
2026
|
+
if (process2.env.NO_COLOR || process2.env.FORCE_COLOR === "0" || process2.env.FORCE_COLOR === "false")
|
|
2027
|
+
return false;
|
|
2028
|
+
if (process2.env.FORCE_COLOR || process2.env.CLICOLOR_FORCE !== undefined)
|
|
2029
|
+
return true;
|
|
2030
|
+
return;
|
|
2031
|
+
}
|
|
2032
|
+
exports.Command = Command;
|
|
2033
|
+
exports.useColor = useColor;
|
|
2034
|
+
});
|
|
2035
|
+
|
|
2036
|
+
// node_modules/commander/index.js
|
|
2037
|
+
var require_commander = __commonJS((exports) => {
|
|
2038
|
+
var { Argument } = require_argument();
|
|
2039
|
+
var { Command } = require_command();
|
|
2040
|
+
var { CommanderError, InvalidArgumentError } = require_error();
|
|
2041
|
+
var { Help } = require_help();
|
|
2042
|
+
var { Option } = require_option();
|
|
2043
|
+
exports.program = new Command;
|
|
2044
|
+
exports.createCommand = (name) => new Command(name);
|
|
2045
|
+
exports.createOption = (flags, description) => new Option(flags, description);
|
|
2046
|
+
exports.createArgument = (name, description) => new Argument(name, description);
|
|
2047
|
+
exports.Command = Command;
|
|
2048
|
+
exports.Option = Option;
|
|
2049
|
+
exports.Argument = Argument;
|
|
2050
|
+
exports.Help = Help;
|
|
2051
|
+
exports.CommanderError = CommanderError;
|
|
2052
|
+
exports.InvalidArgumentError = InvalidArgumentError;
|
|
2053
|
+
exports.InvalidOptionArgumentError = InvalidArgumentError;
|
|
2054
|
+
});
|
|
2055
|
+
|
|
2056
|
+
// node_modules/commander/esm.mjs
|
|
2057
|
+
var import__ = __toESM(require_commander(), 1);
|
|
2058
|
+
var {
|
|
2059
|
+
program,
|
|
2060
|
+
createCommand,
|
|
2061
|
+
createArgument,
|
|
2062
|
+
createOption,
|
|
2063
|
+
CommanderError,
|
|
2064
|
+
InvalidArgumentError,
|
|
2065
|
+
InvalidOptionArgumentError,
|
|
2066
|
+
Command,
|
|
2067
|
+
Argument,
|
|
2068
|
+
Option,
|
|
2069
|
+
Help
|
|
2070
|
+
} = import__.default;
|
|
2071
|
+
|
|
2072
|
+
// src/cli/index.tsx
|
|
2073
|
+
import chalk2 from "chalk";
|
|
2074
|
+
import { readFileSync as readFileSync2, readdirSync, writeFileSync } from "fs";
|
|
2075
|
+
import { join as join5, resolve } from "path";
|
|
2076
|
+
|
|
2077
|
+
// src/types/index.ts
|
|
2078
|
+
var MODEL_MAP = {
|
|
2079
|
+
quick: "claude-haiku-4-5-20251001",
|
|
2080
|
+
thorough: "claude-sonnet-4-6-20260311",
|
|
2081
|
+
deep: "claude-opus-4-6-20260311"
|
|
2082
|
+
};
|
|
2083
|
+
function scenarioFromRow(row) {
|
|
2084
|
+
return {
|
|
2085
|
+
id: row.id,
|
|
2086
|
+
shortId: row.short_id,
|
|
2087
|
+
projectId: row.project_id,
|
|
2088
|
+
name: row.name,
|
|
2089
|
+
description: row.description,
|
|
2090
|
+
steps: JSON.parse(row.steps),
|
|
2091
|
+
tags: JSON.parse(row.tags),
|
|
2092
|
+
priority: row.priority,
|
|
2093
|
+
model: row.model,
|
|
2094
|
+
timeoutMs: row.timeout_ms,
|
|
2095
|
+
targetPath: row.target_path,
|
|
2096
|
+
requiresAuth: row.requires_auth === 1,
|
|
2097
|
+
authConfig: row.auth_config ? JSON.parse(row.auth_config) : null,
|
|
2098
|
+
metadata: row.metadata ? JSON.parse(row.metadata) : null,
|
|
2099
|
+
version: row.version,
|
|
2100
|
+
createdAt: row.created_at,
|
|
2101
|
+
updatedAt: row.updated_at
|
|
2102
|
+
};
|
|
2103
|
+
}
|
|
2104
|
+
function runFromRow(row) {
|
|
2105
|
+
return {
|
|
2106
|
+
id: row.id,
|
|
2107
|
+
projectId: row.project_id,
|
|
2108
|
+
status: row.status,
|
|
2109
|
+
url: row.url,
|
|
2110
|
+
model: row.model,
|
|
2111
|
+
headed: row.headed === 1,
|
|
2112
|
+
parallel: row.parallel,
|
|
2113
|
+
total: row.total,
|
|
2114
|
+
passed: row.passed,
|
|
2115
|
+
failed: row.failed,
|
|
2116
|
+
startedAt: row.started_at,
|
|
2117
|
+
finishedAt: row.finished_at,
|
|
2118
|
+
metadata: row.metadata ? JSON.parse(row.metadata) : null
|
|
2119
|
+
};
|
|
2120
|
+
}
|
|
2121
|
+
function resultFromRow(row) {
|
|
2122
|
+
return {
|
|
2123
|
+
id: row.id,
|
|
2124
|
+
runId: row.run_id,
|
|
2125
|
+
scenarioId: row.scenario_id,
|
|
2126
|
+
status: row.status,
|
|
2127
|
+
reasoning: row.reasoning,
|
|
2128
|
+
error: row.error,
|
|
2129
|
+
stepsCompleted: row.steps_completed,
|
|
2130
|
+
stepsTotal: row.steps_total,
|
|
2131
|
+
durationMs: row.duration_ms,
|
|
2132
|
+
model: row.model,
|
|
2133
|
+
tokensUsed: row.tokens_used,
|
|
2134
|
+
costCents: row.cost_cents,
|
|
2135
|
+
metadata: row.metadata ? JSON.parse(row.metadata) : null,
|
|
2136
|
+
createdAt: row.created_at
|
|
2137
|
+
};
|
|
2138
|
+
}
|
|
2139
|
+
function screenshotFromRow(row) {
|
|
2140
|
+
return {
|
|
2141
|
+
id: row.id,
|
|
2142
|
+
resultId: row.result_id,
|
|
2143
|
+
stepNumber: row.step_number,
|
|
2144
|
+
action: row.action,
|
|
2145
|
+
filePath: row.file_path,
|
|
2146
|
+
width: row.width,
|
|
2147
|
+
height: row.height,
|
|
2148
|
+
timestamp: row.timestamp
|
|
2149
|
+
};
|
|
2150
|
+
}
|
|
2151
|
+
class VersionConflictError extends Error {
|
|
2152
|
+
constructor(entity, id) {
|
|
2153
|
+
super(`Version conflict on ${entity}: ${id}`);
|
|
2154
|
+
this.name = "VersionConflictError";
|
|
2155
|
+
}
|
|
2156
|
+
}
|
|
2157
|
+
|
|
2158
|
+
class BrowserError extends Error {
|
|
2159
|
+
constructor(message) {
|
|
2160
|
+
super(message);
|
|
2161
|
+
this.name = "BrowserError";
|
|
2162
|
+
}
|
|
2163
|
+
}
|
|
2164
|
+
|
|
2165
|
+
class AIClientError extends Error {
|
|
2166
|
+
constructor(message) {
|
|
2167
|
+
super(message);
|
|
2168
|
+
this.name = "AIClientError";
|
|
2169
|
+
}
|
|
2170
|
+
}
|
|
2171
|
+
|
|
2172
|
+
class TodosConnectionError extends Error {
|
|
2173
|
+
constructor(message) {
|
|
2174
|
+
super(message);
|
|
2175
|
+
this.name = "TodosConnectionError";
|
|
2176
|
+
}
|
|
2177
|
+
}
|
|
2178
|
+
|
|
2179
|
+
// src/db/database.ts
|
|
2180
|
+
import { Database } from "bun:sqlite";
|
|
2181
|
+
import { mkdirSync, existsSync } from "fs";
|
|
2182
|
+
import { dirname, join } from "path";
|
|
2183
|
+
import { homedir } from "os";
|
|
2184
|
+
var db = null;
|
|
2185
|
+
function now() {
|
|
2186
|
+
return new Date().toISOString();
|
|
2187
|
+
}
|
|
2188
|
+
function uuid() {
|
|
2189
|
+
return crypto.randomUUID();
|
|
2190
|
+
}
|
|
2191
|
+
function shortUuid() {
|
|
2192
|
+
return uuid().slice(0, 8);
|
|
2193
|
+
}
|
|
2194
|
+
function resolveDbPath() {
|
|
2195
|
+
const envPath = process.env["TESTERS_DB_PATH"];
|
|
2196
|
+
if (envPath)
|
|
2197
|
+
return envPath;
|
|
2198
|
+
const dir = join(homedir(), ".testers");
|
|
2199
|
+
if (!existsSync(dir))
|
|
2200
|
+
mkdirSync(dir, { recursive: true });
|
|
2201
|
+
return join(dir, "testers.db");
|
|
2202
|
+
}
|
|
2203
|
+
var MIGRATIONS = [
|
|
2204
|
+
`
|
|
2205
|
+
CREATE TABLE IF NOT EXISTS projects (
|
|
2206
|
+
id TEXT PRIMARY KEY,
|
|
2207
|
+
name TEXT NOT NULL UNIQUE,
|
|
2208
|
+
path TEXT UNIQUE,
|
|
2209
|
+
description TEXT,
|
|
2210
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
2211
|
+
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
2212
|
+
);
|
|
2213
|
+
|
|
2214
|
+
CREATE TABLE IF NOT EXISTS agents (
|
|
2215
|
+
id TEXT PRIMARY KEY,
|
|
2216
|
+
name TEXT NOT NULL UNIQUE,
|
|
2217
|
+
description TEXT,
|
|
2218
|
+
role TEXT,
|
|
2219
|
+
metadata TEXT DEFAULT '{}',
|
|
2220
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
2221
|
+
last_seen_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
2222
|
+
);
|
|
2223
|
+
|
|
2224
|
+
CREATE TABLE IF NOT EXISTS scenarios (
|
|
2225
|
+
id TEXT PRIMARY KEY,
|
|
2226
|
+
short_id TEXT NOT NULL UNIQUE,
|
|
2227
|
+
project_id TEXT REFERENCES projects(id) ON DELETE SET NULL,
|
|
2228
|
+
name TEXT NOT NULL,
|
|
2229
|
+
description TEXT NOT NULL DEFAULT '',
|
|
2230
|
+
steps TEXT NOT NULL DEFAULT '[]',
|
|
2231
|
+
tags TEXT NOT NULL DEFAULT '[]',
|
|
2232
|
+
priority TEXT NOT NULL DEFAULT 'medium' CHECK(priority IN ('low','medium','high','critical')),
|
|
2233
|
+
model TEXT,
|
|
2234
|
+
timeout_ms INTEGER,
|
|
2235
|
+
target_path TEXT,
|
|
2236
|
+
requires_auth INTEGER NOT NULL DEFAULT 0,
|
|
2237
|
+
auth_config TEXT,
|
|
2238
|
+
metadata TEXT DEFAULT '{}',
|
|
2239
|
+
version INTEGER NOT NULL DEFAULT 1,
|
|
2240
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
2241
|
+
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
2242
|
+
);
|
|
2243
|
+
|
|
2244
|
+
CREATE TABLE IF NOT EXISTS runs (
|
|
2245
|
+
id TEXT PRIMARY KEY,
|
|
2246
|
+
project_id TEXT REFERENCES projects(id) ON DELETE SET NULL,
|
|
2247
|
+
status TEXT NOT NULL DEFAULT 'pending' CHECK(status IN ('pending','running','passed','failed','cancelled')),
|
|
2248
|
+
url TEXT NOT NULL,
|
|
2249
|
+
model TEXT NOT NULL,
|
|
2250
|
+
headed INTEGER NOT NULL DEFAULT 0,
|
|
2251
|
+
parallel INTEGER NOT NULL DEFAULT 1,
|
|
2252
|
+
total INTEGER NOT NULL DEFAULT 0,
|
|
2253
|
+
passed INTEGER NOT NULL DEFAULT 0,
|
|
2254
|
+
failed INTEGER NOT NULL DEFAULT 0,
|
|
2255
|
+
started_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
2256
|
+
finished_at TEXT,
|
|
2257
|
+
metadata TEXT DEFAULT '{}'
|
|
2258
|
+
);
|
|
2259
|
+
|
|
2260
|
+
CREATE TABLE IF NOT EXISTS results (
|
|
2261
|
+
id TEXT PRIMARY KEY,
|
|
2262
|
+
run_id TEXT NOT NULL REFERENCES runs(id) ON DELETE CASCADE,
|
|
2263
|
+
scenario_id TEXT NOT NULL REFERENCES scenarios(id) ON DELETE CASCADE,
|
|
2264
|
+
status TEXT NOT NULL DEFAULT 'skipped' CHECK(status IN ('passed','failed','error','skipped')),
|
|
2265
|
+
reasoning TEXT,
|
|
2266
|
+
error TEXT,
|
|
2267
|
+
steps_completed INTEGER NOT NULL DEFAULT 0,
|
|
2268
|
+
steps_total INTEGER NOT NULL DEFAULT 0,
|
|
2269
|
+
duration_ms INTEGER NOT NULL DEFAULT 0,
|
|
2270
|
+
model TEXT NOT NULL,
|
|
2271
|
+
tokens_used INTEGER NOT NULL DEFAULT 0,
|
|
2272
|
+
cost_cents REAL NOT NULL DEFAULT 0,
|
|
2273
|
+
metadata TEXT DEFAULT '{}',
|
|
2274
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
2275
|
+
);
|
|
2276
|
+
|
|
2277
|
+
CREATE TABLE IF NOT EXISTS screenshots (
|
|
2278
|
+
id TEXT PRIMARY KEY,
|
|
2279
|
+
result_id TEXT NOT NULL REFERENCES results(id) ON DELETE CASCADE,
|
|
2280
|
+
step_number INTEGER NOT NULL,
|
|
2281
|
+
action TEXT NOT NULL,
|
|
2282
|
+
file_path TEXT NOT NULL,
|
|
2283
|
+
width INTEGER NOT NULL DEFAULT 0,
|
|
2284
|
+
height INTEGER NOT NULL DEFAULT 0,
|
|
2285
|
+
timestamp TEXT NOT NULL DEFAULT (datetime('now'))
|
|
2286
|
+
);
|
|
2287
|
+
|
|
2288
|
+
CREATE TABLE IF NOT EXISTS _migrations (
|
|
2289
|
+
id INTEGER PRIMARY KEY,
|
|
2290
|
+
applied_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
2291
|
+
);
|
|
2292
|
+
`,
|
|
2293
|
+
`
|
|
2294
|
+
CREATE INDEX IF NOT EXISTS idx_scenarios_project ON scenarios(project_id);
|
|
2295
|
+
CREATE INDEX IF NOT EXISTS idx_scenarios_priority ON scenarios(priority);
|
|
2296
|
+
CREATE INDEX IF NOT EXISTS idx_scenarios_short_id ON scenarios(short_id);
|
|
2297
|
+
CREATE INDEX IF NOT EXISTS idx_runs_project ON runs(project_id);
|
|
2298
|
+
CREATE INDEX IF NOT EXISTS idx_runs_status ON runs(status);
|
|
2299
|
+
CREATE INDEX IF NOT EXISTS idx_results_run ON results(run_id);
|
|
2300
|
+
CREATE INDEX IF NOT EXISTS idx_results_scenario ON results(scenario_id);
|
|
2301
|
+
CREATE INDEX IF NOT EXISTS idx_results_status ON results(status);
|
|
2302
|
+
CREATE INDEX IF NOT EXISTS idx_screenshots_result ON screenshots(result_id);
|
|
2303
|
+
`,
|
|
2304
|
+
`
|
|
2305
|
+
ALTER TABLE projects ADD COLUMN scenario_prefix TEXT DEFAULT 'TST';
|
|
2306
|
+
ALTER TABLE projects ADD COLUMN scenario_counter INTEGER DEFAULT 0;
|
|
2307
|
+
`
|
|
2308
|
+
];
|
|
2309
|
+
function applyMigrations(database) {
|
|
2310
|
+
const applied = database.query("SELECT id FROM _migrations ORDER BY id").all();
|
|
2311
|
+
const appliedIds = new Set(applied.map((r) => r.id));
|
|
2312
|
+
for (let i = 0;i < MIGRATIONS.length; i++) {
|
|
2313
|
+
const migrationId = i + 1;
|
|
2314
|
+
if (appliedIds.has(migrationId))
|
|
2315
|
+
continue;
|
|
2316
|
+
const migration = MIGRATIONS[i];
|
|
2317
|
+
database.exec(migration);
|
|
2318
|
+
database.query("INSERT INTO _migrations (id, applied_at) VALUES (?, ?)").run(migrationId, now());
|
|
2319
|
+
}
|
|
2320
|
+
}
|
|
2321
|
+
function getDatabase() {
|
|
2322
|
+
if (db)
|
|
2323
|
+
return db;
|
|
2324
|
+
const dbPath = resolveDbPath();
|
|
2325
|
+
const dir = dirname(dbPath);
|
|
2326
|
+
if (dbPath !== ":memory:" && !existsSync(dir)) {
|
|
2327
|
+
mkdirSync(dir, { recursive: true });
|
|
2328
|
+
}
|
|
2329
|
+
db = new Database(dbPath);
|
|
2330
|
+
db.exec("PRAGMA journal_mode = WAL");
|
|
2331
|
+
db.exec("PRAGMA foreign_keys = ON");
|
|
2332
|
+
db.exec("PRAGMA busy_timeout = 5000");
|
|
2333
|
+
db.exec(`
|
|
2334
|
+
CREATE TABLE IF NOT EXISTS _migrations (
|
|
2335
|
+
id INTEGER PRIMARY KEY,
|
|
2336
|
+
applied_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
2337
|
+
);
|
|
2338
|
+
`);
|
|
2339
|
+
applyMigrations(db);
|
|
2340
|
+
return db;
|
|
2341
|
+
}
|
|
2342
|
+
function resolvePartialId(table, partialId) {
|
|
2343
|
+
const database = getDatabase();
|
|
2344
|
+
const rows = database.query(`SELECT id FROM ${table} WHERE id LIKE ? || '%'`).all(partialId);
|
|
2345
|
+
if (rows.length === 1)
|
|
2346
|
+
return rows[0].id;
|
|
2347
|
+
return null;
|
|
2348
|
+
}
|
|
2349
|
+
|
|
2350
|
+
// src/db/scenarios.ts
|
|
2351
|
+
function nextShortId(projectId) {
|
|
2352
|
+
const db2 = getDatabase();
|
|
2353
|
+
if (projectId) {
|
|
2354
|
+
const project = db2.query("SELECT scenario_prefix, scenario_counter FROM projects WHERE id = ?").get(projectId);
|
|
2355
|
+
if (project) {
|
|
2356
|
+
const next = project.scenario_counter + 1;
|
|
2357
|
+
db2.query("UPDATE projects SET scenario_counter = ? WHERE id = ?").run(next, projectId);
|
|
2358
|
+
return `${project.scenario_prefix}-${next}`;
|
|
2359
|
+
}
|
|
2360
|
+
}
|
|
2361
|
+
return shortUuid();
|
|
2362
|
+
}
|
|
2363
|
+
function createScenario(input) {
|
|
2364
|
+
const db2 = getDatabase();
|
|
2365
|
+
const id = uuid();
|
|
2366
|
+
const short_id = nextShortId(input.projectId);
|
|
2367
|
+
const timestamp = now();
|
|
2368
|
+
db2.query(`
|
|
2369
|
+
INSERT INTO scenarios (id, short_id, project_id, name, description, steps, tags, priority, model, timeout_ms, target_path, requires_auth, auth_config, metadata, version, created_at, updated_at)
|
|
2370
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?)
|
|
2371
|
+
`).run(id, short_id, input.projectId ?? null, input.name, input.description, JSON.stringify(input.steps ?? []), JSON.stringify(input.tags ?? []), input.priority ?? "medium", input.model ?? null, input.timeoutMs ?? null, input.targetPath ?? null, input.requiresAuth ? 1 : 0, input.authConfig ? JSON.stringify(input.authConfig) : null, input.metadata ? JSON.stringify(input.metadata) : null, timestamp, timestamp);
|
|
2372
|
+
return getScenario(id);
|
|
2373
|
+
}
|
|
2374
|
+
function getScenario(id) {
|
|
2375
|
+
const db2 = getDatabase();
|
|
2376
|
+
let row = db2.query("SELECT * FROM scenarios WHERE id = ?").get(id);
|
|
2377
|
+
if (row)
|
|
2378
|
+
return scenarioFromRow(row);
|
|
2379
|
+
row = db2.query("SELECT * FROM scenarios WHERE short_id = ?").get(id);
|
|
2380
|
+
if (row)
|
|
2381
|
+
return scenarioFromRow(row);
|
|
2382
|
+
const fullId = resolvePartialId("scenarios", id);
|
|
2383
|
+
if (fullId) {
|
|
2384
|
+
row = db2.query("SELECT * FROM scenarios WHERE id = ?").get(fullId);
|
|
2385
|
+
if (row)
|
|
2386
|
+
return scenarioFromRow(row);
|
|
2387
|
+
}
|
|
2388
|
+
return null;
|
|
2389
|
+
}
|
|
2390
|
+
function getScenarioByShortId(shortId) {
|
|
2391
|
+
const db2 = getDatabase();
|
|
2392
|
+
const row = db2.query("SELECT * FROM scenarios WHERE short_id = ?").get(shortId);
|
|
2393
|
+
return row ? scenarioFromRow(row) : null;
|
|
2394
|
+
}
|
|
2395
|
+
function listScenarios(filter) {
|
|
2396
|
+
const db2 = getDatabase();
|
|
2397
|
+
const conditions = [];
|
|
2398
|
+
const params = [];
|
|
2399
|
+
if (filter?.projectId) {
|
|
2400
|
+
conditions.push("project_id = ?");
|
|
2401
|
+
params.push(filter.projectId);
|
|
2402
|
+
}
|
|
2403
|
+
if (filter?.tags && filter.tags.length > 0) {
|
|
2404
|
+
for (const tag of filter.tags) {
|
|
2405
|
+
conditions.push("tags LIKE ?");
|
|
2406
|
+
params.push(`%"${tag}"%`);
|
|
2407
|
+
}
|
|
2408
|
+
}
|
|
2409
|
+
if (filter?.priority) {
|
|
2410
|
+
conditions.push("priority = ?");
|
|
2411
|
+
params.push(filter.priority);
|
|
2412
|
+
}
|
|
2413
|
+
if (filter?.search) {
|
|
2414
|
+
conditions.push("(name LIKE ? OR description LIKE ?)");
|
|
2415
|
+
const term = `%${filter.search}%`;
|
|
2416
|
+
params.push(term, term);
|
|
2417
|
+
}
|
|
2418
|
+
let sql = "SELECT * FROM scenarios";
|
|
2419
|
+
if (conditions.length > 0) {
|
|
2420
|
+
sql += " WHERE " + conditions.join(" AND ");
|
|
2421
|
+
}
|
|
2422
|
+
sql += " ORDER BY created_at DESC";
|
|
2423
|
+
if (filter?.limit) {
|
|
2424
|
+
sql += " LIMIT ?";
|
|
2425
|
+
params.push(filter.limit);
|
|
2426
|
+
}
|
|
2427
|
+
if (filter?.offset) {
|
|
2428
|
+
sql += " OFFSET ?";
|
|
2429
|
+
params.push(filter.offset);
|
|
2430
|
+
}
|
|
2431
|
+
const rows = db2.query(sql).all(...params);
|
|
2432
|
+
return rows.map(scenarioFromRow);
|
|
2433
|
+
}
|
|
2434
|
+
function updateScenario(id, input, version) {
|
|
2435
|
+
const db2 = getDatabase();
|
|
2436
|
+
const existing = getScenario(id);
|
|
2437
|
+
if (!existing) {
|
|
2438
|
+
throw new Error(`Scenario not found: ${id}`);
|
|
2439
|
+
}
|
|
2440
|
+
if (existing.version !== version) {
|
|
2441
|
+
throw new VersionConflictError("scenario", existing.id);
|
|
2442
|
+
}
|
|
2443
|
+
const sets = [];
|
|
2444
|
+
const params = [];
|
|
2445
|
+
if (input.name !== undefined) {
|
|
2446
|
+
sets.push("name = ?");
|
|
2447
|
+
params.push(input.name);
|
|
2448
|
+
}
|
|
2449
|
+
if (input.description !== undefined) {
|
|
2450
|
+
sets.push("description = ?");
|
|
2451
|
+
params.push(input.description);
|
|
2452
|
+
}
|
|
2453
|
+
if (input.steps !== undefined) {
|
|
2454
|
+
sets.push("steps = ?");
|
|
2455
|
+
params.push(JSON.stringify(input.steps));
|
|
2456
|
+
}
|
|
2457
|
+
if (input.tags !== undefined) {
|
|
2458
|
+
sets.push("tags = ?");
|
|
2459
|
+
params.push(JSON.stringify(input.tags));
|
|
2460
|
+
}
|
|
2461
|
+
if (input.priority !== undefined) {
|
|
2462
|
+
sets.push("priority = ?");
|
|
2463
|
+
params.push(input.priority);
|
|
2464
|
+
}
|
|
2465
|
+
if (input.model !== undefined) {
|
|
2466
|
+
sets.push("model = ?");
|
|
2467
|
+
params.push(input.model);
|
|
2468
|
+
}
|
|
2469
|
+
if (input.timeoutMs !== undefined) {
|
|
2470
|
+
sets.push("timeout_ms = ?");
|
|
2471
|
+
params.push(input.timeoutMs);
|
|
2472
|
+
}
|
|
2473
|
+
if (input.targetPath !== undefined) {
|
|
2474
|
+
sets.push("target_path = ?");
|
|
2475
|
+
params.push(input.targetPath);
|
|
2476
|
+
}
|
|
2477
|
+
if (input.requiresAuth !== undefined) {
|
|
2478
|
+
sets.push("requires_auth = ?");
|
|
2479
|
+
params.push(input.requiresAuth ? 1 : 0);
|
|
2480
|
+
}
|
|
2481
|
+
if (input.authConfig !== undefined) {
|
|
2482
|
+
sets.push("auth_config = ?");
|
|
2483
|
+
params.push(JSON.stringify(input.authConfig));
|
|
2484
|
+
}
|
|
2485
|
+
if (input.metadata !== undefined) {
|
|
2486
|
+
sets.push("metadata = ?");
|
|
2487
|
+
params.push(JSON.stringify(input.metadata));
|
|
2488
|
+
}
|
|
2489
|
+
if (sets.length === 0) {
|
|
2490
|
+
return existing;
|
|
2491
|
+
}
|
|
2492
|
+
sets.push("version = ?");
|
|
2493
|
+
params.push(version + 1);
|
|
2494
|
+
sets.push("updated_at = ?");
|
|
2495
|
+
params.push(now());
|
|
2496
|
+
params.push(existing.id);
|
|
2497
|
+
params.push(version);
|
|
2498
|
+
const result = db2.query(`UPDATE scenarios SET ${sets.join(", ")} WHERE id = ? AND version = ?`).run(...params);
|
|
2499
|
+
if (result.changes === 0) {
|
|
2500
|
+
throw new VersionConflictError("scenario", existing.id);
|
|
2501
|
+
}
|
|
2502
|
+
return getScenario(existing.id);
|
|
2503
|
+
}
|
|
2504
|
+
function deleteScenario(id) {
|
|
2505
|
+
const db2 = getDatabase();
|
|
2506
|
+
const scenario = getScenario(id);
|
|
2507
|
+
if (!scenario)
|
|
2508
|
+
return false;
|
|
2509
|
+
const result = db2.query("DELETE FROM scenarios WHERE id = ?").run(scenario.id);
|
|
2510
|
+
return result.changes > 0;
|
|
2511
|
+
}
|
|
2512
|
+
|
|
2513
|
+
// src/db/runs.ts
|
|
2514
|
+
function createRun(input) {
|
|
2515
|
+
const db2 = getDatabase();
|
|
2516
|
+
const id = uuid();
|
|
2517
|
+
const timestamp = now();
|
|
2518
|
+
db2.query(`
|
|
2519
|
+
INSERT INTO runs (id, project_id, status, url, model, headed, parallel, total, passed, failed, started_at, finished_at, metadata)
|
|
2520
|
+
VALUES (?, ?, 'pending', ?, ?, ?, ?, 0, 0, 0, ?, NULL, ?)
|
|
2521
|
+
`).run(id, input.projectId ?? null, input.url, input.model, input.headed ? 1 : 0, input.parallel ?? 1, timestamp, input.model ? JSON.stringify({}) : null);
|
|
2522
|
+
return getRun(id);
|
|
2523
|
+
}
|
|
2524
|
+
function getRun(id) {
|
|
2525
|
+
const db2 = getDatabase();
|
|
2526
|
+
let row = db2.query("SELECT * FROM runs WHERE id = ?").get(id);
|
|
2527
|
+
if (row)
|
|
2528
|
+
return runFromRow(row);
|
|
2529
|
+
const fullId = resolvePartialId("runs", id);
|
|
2530
|
+
if (fullId) {
|
|
2531
|
+
row = db2.query("SELECT * FROM runs WHERE id = ?").get(fullId);
|
|
2532
|
+
if (row)
|
|
2533
|
+
return runFromRow(row);
|
|
2534
|
+
}
|
|
2535
|
+
return null;
|
|
2536
|
+
}
|
|
2537
|
+
function listRuns(filter) {
|
|
2538
|
+
const db2 = getDatabase();
|
|
2539
|
+
const conditions = [];
|
|
2540
|
+
const params = [];
|
|
2541
|
+
if (filter?.projectId) {
|
|
2542
|
+
conditions.push("project_id = ?");
|
|
2543
|
+
params.push(filter.projectId);
|
|
2544
|
+
}
|
|
2545
|
+
if (filter?.status) {
|
|
2546
|
+
conditions.push("status = ?");
|
|
2547
|
+
params.push(filter.status);
|
|
2548
|
+
}
|
|
2549
|
+
let sql = "SELECT * FROM runs";
|
|
2550
|
+
if (conditions.length > 0) {
|
|
2551
|
+
sql += " WHERE " + conditions.join(" AND ");
|
|
2552
|
+
}
|
|
2553
|
+
sql += " ORDER BY started_at DESC";
|
|
2554
|
+
if (filter?.limit) {
|
|
2555
|
+
sql += " LIMIT ?";
|
|
2556
|
+
params.push(filter.limit);
|
|
2557
|
+
}
|
|
2558
|
+
if (filter?.offset) {
|
|
2559
|
+
sql += " OFFSET ?";
|
|
2560
|
+
params.push(filter.offset);
|
|
2561
|
+
}
|
|
2562
|
+
const rows = db2.query(sql).all(...params);
|
|
2563
|
+
return rows.map(runFromRow);
|
|
2564
|
+
}
|
|
2565
|
+
function updateRun(id, updates) {
|
|
2566
|
+
const db2 = getDatabase();
|
|
2567
|
+
const existing = getRun(id);
|
|
2568
|
+
if (!existing) {
|
|
2569
|
+
throw new Error(`Run not found: ${id}`);
|
|
2570
|
+
}
|
|
2571
|
+
const sets = [];
|
|
2572
|
+
const params = [];
|
|
2573
|
+
if (updates.status !== undefined) {
|
|
2574
|
+
sets.push("status = ?");
|
|
2575
|
+
params.push(updates.status);
|
|
2576
|
+
}
|
|
2577
|
+
if (updates.url !== undefined) {
|
|
2578
|
+
sets.push("url = ?");
|
|
2579
|
+
params.push(updates.url);
|
|
2580
|
+
}
|
|
2581
|
+
if (updates.model !== undefined) {
|
|
2582
|
+
sets.push("model = ?");
|
|
2583
|
+
params.push(updates.model);
|
|
2584
|
+
}
|
|
2585
|
+
if (updates.headed !== undefined) {
|
|
2586
|
+
sets.push("headed = ?");
|
|
2587
|
+
params.push(updates.headed);
|
|
2588
|
+
}
|
|
2589
|
+
if (updates.parallel !== undefined) {
|
|
2590
|
+
sets.push("parallel = ?");
|
|
2591
|
+
params.push(updates.parallel);
|
|
2592
|
+
}
|
|
2593
|
+
if (updates.total !== undefined) {
|
|
2594
|
+
sets.push("total = ?");
|
|
2595
|
+
params.push(updates.total);
|
|
2596
|
+
}
|
|
2597
|
+
if (updates.passed !== undefined) {
|
|
2598
|
+
sets.push("passed = ?");
|
|
2599
|
+
params.push(updates.passed);
|
|
2600
|
+
}
|
|
2601
|
+
if (updates.failed !== undefined) {
|
|
2602
|
+
sets.push("failed = ?");
|
|
2603
|
+
params.push(updates.failed);
|
|
2604
|
+
}
|
|
2605
|
+
if (updates.started_at !== undefined) {
|
|
2606
|
+
sets.push("started_at = ?");
|
|
2607
|
+
params.push(updates.started_at);
|
|
2608
|
+
}
|
|
2609
|
+
if (updates.finished_at !== undefined) {
|
|
2610
|
+
sets.push("finished_at = ?");
|
|
2611
|
+
params.push(updates.finished_at);
|
|
2612
|
+
}
|
|
2613
|
+
if (updates.metadata !== undefined) {
|
|
2614
|
+
sets.push("metadata = ?");
|
|
2615
|
+
params.push(updates.metadata);
|
|
2616
|
+
}
|
|
2617
|
+
if (sets.length === 0) {
|
|
2618
|
+
return existing;
|
|
2619
|
+
}
|
|
2620
|
+
params.push(existing.id);
|
|
2621
|
+
db2.query(`UPDATE runs SET ${sets.join(", ")} WHERE id = ?`).run(...params);
|
|
2622
|
+
return getRun(existing.id);
|
|
2623
|
+
}
|
|
2624
|
+
|
|
2625
|
+
// src/db/results.ts
|
|
2626
|
+
function createResult(input) {
|
|
2627
|
+
const db2 = getDatabase();
|
|
2628
|
+
const id = uuid();
|
|
2629
|
+
const timestamp = now();
|
|
2630
|
+
db2.query(`
|
|
2631
|
+
INSERT INTO results (id, run_id, scenario_id, status, reasoning, error, steps_completed, steps_total, duration_ms, model, tokens_used, cost_cents, metadata, created_at)
|
|
2632
|
+
VALUES (?, ?, ?, 'skipped', NULL, NULL, 0, ?, 0, ?, 0, 0, '{}', ?)
|
|
2633
|
+
`).run(id, input.runId, input.scenarioId, input.stepsTotal, input.model, timestamp);
|
|
2634
|
+
return getResult(id);
|
|
2635
|
+
}
|
|
2636
|
+
function getResult(id) {
|
|
2637
|
+
const db2 = getDatabase();
|
|
2638
|
+
let row = db2.query("SELECT * FROM results WHERE id = ?").get(id);
|
|
2639
|
+
if (row)
|
|
2640
|
+
return resultFromRow(row);
|
|
2641
|
+
const fullId = resolvePartialId("results", id);
|
|
2642
|
+
if (fullId) {
|
|
2643
|
+
row = db2.query("SELECT * FROM results WHERE id = ?").get(fullId);
|
|
2644
|
+
if (row)
|
|
2645
|
+
return resultFromRow(row);
|
|
2646
|
+
}
|
|
2647
|
+
return null;
|
|
2648
|
+
}
|
|
2649
|
+
function listResults(runId) {
|
|
2650
|
+
const db2 = getDatabase();
|
|
2651
|
+
const rows = db2.query("SELECT * FROM results WHERE run_id = ? ORDER BY created_at ASC").all(runId);
|
|
2652
|
+
return rows.map(resultFromRow);
|
|
2653
|
+
}
|
|
2654
|
+
function updateResult(id, updates) {
|
|
2655
|
+
const db2 = getDatabase();
|
|
2656
|
+
const existing = getResult(id);
|
|
2657
|
+
if (!existing) {
|
|
2658
|
+
throw new Error(`Result not found: ${id}`);
|
|
2659
|
+
}
|
|
2660
|
+
const sets = [];
|
|
2661
|
+
const params = [];
|
|
2662
|
+
if (updates.status !== undefined) {
|
|
2663
|
+
sets.push("status = ?");
|
|
2664
|
+
params.push(updates.status);
|
|
2665
|
+
}
|
|
2666
|
+
if (updates.reasoning !== undefined) {
|
|
2667
|
+
sets.push("reasoning = ?");
|
|
2668
|
+
params.push(updates.reasoning);
|
|
2669
|
+
}
|
|
2670
|
+
if (updates.error !== undefined) {
|
|
2671
|
+
sets.push("error = ?");
|
|
2672
|
+
params.push(updates.error);
|
|
2673
|
+
}
|
|
2674
|
+
if (updates.stepsCompleted !== undefined) {
|
|
2675
|
+
sets.push("steps_completed = ?");
|
|
2676
|
+
params.push(updates.stepsCompleted);
|
|
2677
|
+
}
|
|
2678
|
+
if (updates.durationMs !== undefined) {
|
|
2679
|
+
sets.push("duration_ms = ?");
|
|
2680
|
+
params.push(updates.durationMs);
|
|
2681
|
+
}
|
|
2682
|
+
if (updates.tokensUsed !== undefined) {
|
|
2683
|
+
sets.push("tokens_used = ?");
|
|
2684
|
+
params.push(updates.tokensUsed);
|
|
2685
|
+
}
|
|
2686
|
+
if (updates.costCents !== undefined) {
|
|
2687
|
+
sets.push("cost_cents = ?");
|
|
2688
|
+
params.push(updates.costCents);
|
|
2689
|
+
}
|
|
2690
|
+
if (sets.length === 0) {
|
|
2691
|
+
return existing;
|
|
2692
|
+
}
|
|
2693
|
+
params.push(existing.id);
|
|
2694
|
+
db2.query(`UPDATE results SET ${sets.join(", ")} WHERE id = ?`).run(...params);
|
|
2695
|
+
return getResult(existing.id);
|
|
2696
|
+
}
|
|
2697
|
+
function getResultsByRun(runId) {
|
|
2698
|
+
return listResults(runId);
|
|
2699
|
+
}
|
|
2700
|
+
|
|
2701
|
+
// src/db/screenshots.ts
|
|
2702
|
+
function createScreenshot(input) {
|
|
2703
|
+
const db2 = getDatabase();
|
|
2704
|
+
const id = uuid();
|
|
2705
|
+
const timestamp = now();
|
|
2706
|
+
db2.query(`
|
|
2707
|
+
INSERT INTO screenshots (id, result_id, step_number, action, file_path, width, height, timestamp)
|
|
2708
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
2709
|
+
`).run(id, input.resultId, input.stepNumber, input.action, input.filePath, input.width, input.height, timestamp);
|
|
2710
|
+
return getScreenshot(id);
|
|
2711
|
+
}
|
|
2712
|
+
function getScreenshot(id) {
|
|
2713
|
+
const db2 = getDatabase();
|
|
2714
|
+
const row = db2.query("SELECT * FROM screenshots WHERE id = ?").get(id);
|
|
2715
|
+
return row ? screenshotFromRow(row) : null;
|
|
2716
|
+
}
|
|
2717
|
+
function listScreenshots(resultId) {
|
|
2718
|
+
const db2 = getDatabase();
|
|
2719
|
+
const rows = db2.query("SELECT * FROM screenshots WHERE result_id = ? ORDER BY step_number ASC").all(resultId);
|
|
2720
|
+
return rows.map(screenshotFromRow);
|
|
2721
|
+
}
|
|
2722
|
+
|
|
2723
|
+
// src/lib/browser.ts
|
|
2724
|
+
import { chromium } from "playwright";
|
|
2725
|
+
import { execSync } from "child_process";
|
|
2726
|
+
var DEFAULT_VIEWPORT = { width: 1280, height: 720 };
|
|
2727
|
+
async function launchBrowser(options) {
|
|
2728
|
+
const headless = options?.headless ?? true;
|
|
2729
|
+
const viewport = options?.viewport ?? DEFAULT_VIEWPORT;
|
|
2730
|
+
try {
|
|
2731
|
+
const browser = await chromium.launch({
|
|
2732
|
+
headless,
|
|
2733
|
+
args: [
|
|
2734
|
+
`--window-size=${viewport.width},${viewport.height}`
|
|
2735
|
+
]
|
|
2736
|
+
});
|
|
2737
|
+
return browser;
|
|
2738
|
+
} catch (error) {
|
|
2739
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2740
|
+
throw new BrowserError(`Failed to launch browser: ${message}`);
|
|
2741
|
+
}
|
|
2742
|
+
}
|
|
2743
|
+
async function getPage(browser, options) {
|
|
2744
|
+
const viewport = options?.viewport ?? DEFAULT_VIEWPORT;
|
|
2745
|
+
try {
|
|
2746
|
+
const context = await browser.newContext({
|
|
2747
|
+
viewport,
|
|
2748
|
+
userAgent: options?.userAgent,
|
|
2749
|
+
locale: options?.locale
|
|
2750
|
+
});
|
|
2751
|
+
const page = await context.newPage();
|
|
2752
|
+
return page;
|
|
2753
|
+
} catch (error) {
|
|
2754
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2755
|
+
throw new BrowserError(`Failed to create page: ${message}`);
|
|
2756
|
+
}
|
|
2757
|
+
}
|
|
2758
|
+
async function closeBrowser(browser) {
|
|
2759
|
+
try {
|
|
2760
|
+
await browser.close();
|
|
2761
|
+
} catch (error) {
|
|
2762
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2763
|
+
throw new BrowserError(`Failed to close browser: ${message}`);
|
|
2764
|
+
}
|
|
2765
|
+
}
|
|
2766
|
+
async function installBrowser() {
|
|
2767
|
+
try {
|
|
2768
|
+
execSync("bunx playwright install chromium", {
|
|
2769
|
+
stdio: "inherit"
|
|
2770
|
+
});
|
|
2771
|
+
} catch (error) {
|
|
2772
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2773
|
+
throw new BrowserError(`Failed to install browser: ${message}`);
|
|
2774
|
+
}
|
|
2775
|
+
}
|
|
2776
|
+
|
|
2777
|
+
// src/lib/screenshotter.ts
|
|
2778
|
+
import { mkdirSync as mkdirSync2, existsSync as existsSync2 } from "fs";
|
|
2779
|
+
import { join as join2 } from "path";
|
|
2780
|
+
import { homedir as homedir2 } from "os";
|
|
2781
|
+
function slugify(text) {
|
|
2782
|
+
return text.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
2783
|
+
}
|
|
2784
|
+
function generateFilename(stepNumber, action) {
|
|
2785
|
+
const padded = String(stepNumber).padStart(3, "0");
|
|
2786
|
+
const slug = slugify(action);
|
|
2787
|
+
return `${padded}-${slug}.png`;
|
|
2788
|
+
}
|
|
2789
|
+
function getScreenshotDir(baseDir, runId, scenarioSlug) {
|
|
2790
|
+
return join2(baseDir, runId, scenarioSlug);
|
|
2791
|
+
}
|
|
2792
|
+
function ensureDir(dirPath) {
|
|
2793
|
+
if (!existsSync2(dirPath)) {
|
|
2794
|
+
mkdirSync2(dirPath, { recursive: true });
|
|
2795
|
+
}
|
|
2796
|
+
}
|
|
2797
|
+
var DEFAULT_BASE_DIR = join2(homedir2(), ".testers", "screenshots");
|
|
2798
|
+
|
|
2799
|
+
class Screenshotter {
|
|
2800
|
+
baseDir;
|
|
2801
|
+
format;
|
|
2802
|
+
quality;
|
|
2803
|
+
fullPage;
|
|
2804
|
+
constructor(options = {}) {
|
|
2805
|
+
this.baseDir = options.baseDir ?? DEFAULT_BASE_DIR;
|
|
2806
|
+
this.format = options.format ?? "png";
|
|
2807
|
+
this.quality = options.quality ?? 90;
|
|
2808
|
+
this.fullPage = options.fullPage ?? false;
|
|
2809
|
+
}
|
|
2810
|
+
async capture(page, options) {
|
|
2811
|
+
const dir = getScreenshotDir(this.baseDir, options.runId, options.scenarioSlug);
|
|
2812
|
+
const filename = generateFilename(options.stepNumber, options.action);
|
|
2813
|
+
const filePath = join2(dir, filename);
|
|
2814
|
+
ensureDir(dir);
|
|
2815
|
+
await page.screenshot({
|
|
2816
|
+
path: filePath,
|
|
2817
|
+
fullPage: this.fullPage,
|
|
2818
|
+
type: this.format,
|
|
2819
|
+
quality: this.format === "jpeg" ? this.quality : undefined
|
|
2820
|
+
});
|
|
2821
|
+
const viewport = page.viewportSize() ?? { width: 0, height: 0 };
|
|
2822
|
+
return {
|
|
2823
|
+
filePath,
|
|
2824
|
+
width: viewport.width,
|
|
2825
|
+
height: viewport.height,
|
|
2826
|
+
timestamp: new Date().toISOString()
|
|
2827
|
+
};
|
|
2828
|
+
}
|
|
2829
|
+
async captureFullPage(page, options) {
|
|
2830
|
+
const dir = getScreenshotDir(this.baseDir, options.runId, options.scenarioSlug);
|
|
2831
|
+
const filename = generateFilename(options.stepNumber, options.action);
|
|
2832
|
+
const filePath = join2(dir, filename);
|
|
2833
|
+
ensureDir(dir);
|
|
2834
|
+
await page.screenshot({
|
|
2835
|
+
path: filePath,
|
|
2836
|
+
fullPage: true,
|
|
2837
|
+
type: this.format,
|
|
2838
|
+
quality: this.format === "jpeg" ? this.quality : undefined
|
|
2839
|
+
});
|
|
2840
|
+
const viewport = page.viewportSize() ?? { width: 0, height: 0 };
|
|
2841
|
+
return {
|
|
2842
|
+
filePath,
|
|
2843
|
+
width: viewport.width,
|
|
2844
|
+
height: viewport.height,
|
|
2845
|
+
timestamp: new Date().toISOString()
|
|
2846
|
+
};
|
|
2847
|
+
}
|
|
2848
|
+
async captureElement(page, selector, options) {
|
|
2849
|
+
const dir = getScreenshotDir(this.baseDir, options.runId, options.scenarioSlug);
|
|
2850
|
+
const filename = generateFilename(options.stepNumber, options.action);
|
|
2851
|
+
const filePath = join2(dir, filename);
|
|
2852
|
+
ensureDir(dir);
|
|
2853
|
+
await page.locator(selector).screenshot({
|
|
2854
|
+
path: filePath,
|
|
2855
|
+
type: this.format,
|
|
2856
|
+
quality: this.format === "jpeg" ? this.quality : undefined
|
|
2857
|
+
});
|
|
2858
|
+
const viewport = page.viewportSize() ?? { width: 0, height: 0 };
|
|
2859
|
+
return {
|
|
2860
|
+
filePath,
|
|
2861
|
+
width: viewport.width,
|
|
2862
|
+
height: viewport.height,
|
|
2863
|
+
timestamp: new Date().toISOString()
|
|
2864
|
+
};
|
|
2865
|
+
}
|
|
2866
|
+
}
|
|
2867
|
+
|
|
2868
|
+
// src/lib/ai-client.ts
|
|
2869
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
2870
|
+
function resolveModel(nameOrPreset) {
|
|
2871
|
+
if (nameOrPreset in MODEL_MAP) {
|
|
2872
|
+
return MODEL_MAP[nameOrPreset];
|
|
2873
|
+
}
|
|
2874
|
+
return nameOrPreset;
|
|
2875
|
+
}
|
|
2876
|
+
var BROWSER_TOOLS = [
|
|
2877
|
+
{
|
|
2878
|
+
name: "navigate",
|
|
2879
|
+
description: "Navigate the browser to a specific URL.",
|
|
2880
|
+
input_schema: {
|
|
2881
|
+
type: "object",
|
|
2882
|
+
properties: {
|
|
2883
|
+
url: { type: "string", description: "The URL to navigate to." }
|
|
2884
|
+
},
|
|
2885
|
+
required: ["url"]
|
|
2886
|
+
}
|
|
2887
|
+
},
|
|
2888
|
+
{
|
|
2889
|
+
name: "click",
|
|
2890
|
+
description: "Click on an element matching the given CSS selector.",
|
|
2891
|
+
input_schema: {
|
|
2892
|
+
type: "object",
|
|
2893
|
+
properties: {
|
|
2894
|
+
selector: {
|
|
2895
|
+
type: "string",
|
|
2896
|
+
description: "CSS selector of the element to click."
|
|
2897
|
+
}
|
|
2898
|
+
},
|
|
2899
|
+
required: ["selector"]
|
|
2900
|
+
}
|
|
2901
|
+
},
|
|
2902
|
+
{
|
|
2903
|
+
name: "fill",
|
|
2904
|
+
description: "Fill an input field with the given value.",
|
|
2905
|
+
input_schema: {
|
|
2906
|
+
type: "object",
|
|
2907
|
+
properties: {
|
|
2908
|
+
selector: {
|
|
2909
|
+
type: "string",
|
|
2910
|
+
description: "CSS selector of the input field."
|
|
2911
|
+
},
|
|
2912
|
+
value: {
|
|
2913
|
+
type: "string",
|
|
2914
|
+
description: "The value to fill into the input."
|
|
2915
|
+
}
|
|
2916
|
+
},
|
|
2917
|
+
required: ["selector", "value"]
|
|
2918
|
+
}
|
|
2919
|
+
},
|
|
2920
|
+
{
|
|
2921
|
+
name: "select_option",
|
|
2922
|
+
description: "Select an option from a dropdown/select element.",
|
|
2923
|
+
input_schema: {
|
|
2924
|
+
type: "object",
|
|
2925
|
+
properties: {
|
|
2926
|
+
selector: {
|
|
2927
|
+
type: "string",
|
|
2928
|
+
description: "CSS selector of the select element."
|
|
2929
|
+
},
|
|
2930
|
+
value: {
|
|
2931
|
+
type: "string",
|
|
2932
|
+
description: "The value of the option to select."
|
|
2933
|
+
}
|
|
2934
|
+
},
|
|
2935
|
+
required: ["selector", "value"]
|
|
2936
|
+
}
|
|
2937
|
+
},
|
|
2938
|
+
{
|
|
2939
|
+
name: "screenshot",
|
|
2940
|
+
description: "Take a screenshot of the current page state.",
|
|
2941
|
+
input_schema: {
|
|
2942
|
+
type: "object",
|
|
2943
|
+
properties: {},
|
|
2944
|
+
required: []
|
|
2945
|
+
}
|
|
2946
|
+
},
|
|
2947
|
+
{
|
|
2948
|
+
name: "get_text",
|
|
2949
|
+
description: "Get the text content of an element matching the selector.",
|
|
2950
|
+
input_schema: {
|
|
2951
|
+
type: "object",
|
|
2952
|
+
properties: {
|
|
2953
|
+
selector: {
|
|
2954
|
+
type: "string",
|
|
2955
|
+
description: "CSS selector of the element."
|
|
2956
|
+
}
|
|
2957
|
+
},
|
|
2958
|
+
required: ["selector"]
|
|
2959
|
+
}
|
|
2960
|
+
},
|
|
2961
|
+
{
|
|
2962
|
+
name: "get_url",
|
|
2963
|
+
description: "Get the current page URL.",
|
|
2964
|
+
input_schema: {
|
|
2965
|
+
type: "object",
|
|
2966
|
+
properties: {},
|
|
2967
|
+
required: []
|
|
2968
|
+
}
|
|
2969
|
+
},
|
|
2970
|
+
{
|
|
2971
|
+
name: "wait_for",
|
|
2972
|
+
description: "Wait for an element matching the selector to appear on the page.",
|
|
2973
|
+
input_schema: {
|
|
2974
|
+
type: "object",
|
|
2975
|
+
properties: {
|
|
2976
|
+
selector: {
|
|
2977
|
+
type: "string",
|
|
2978
|
+
description: "CSS selector to wait for."
|
|
2979
|
+
},
|
|
2980
|
+
timeout: {
|
|
2981
|
+
type: "number",
|
|
2982
|
+
description: "Maximum time to wait in milliseconds (default: 10000)."
|
|
2983
|
+
}
|
|
2984
|
+
},
|
|
2985
|
+
required: ["selector"]
|
|
2986
|
+
}
|
|
2987
|
+
},
|
|
2988
|
+
{
|
|
2989
|
+
name: "go_back",
|
|
2990
|
+
description: "Navigate back to the previous page.",
|
|
2991
|
+
input_schema: {
|
|
2992
|
+
type: "object",
|
|
2993
|
+
properties: {},
|
|
2994
|
+
required: []
|
|
2995
|
+
}
|
|
2996
|
+
},
|
|
2997
|
+
{
|
|
2998
|
+
name: "press_key",
|
|
2999
|
+
description: "Press a keyboard key (e.g., Enter, Tab, Escape, ArrowDown).",
|
|
3000
|
+
input_schema: {
|
|
3001
|
+
type: "object",
|
|
3002
|
+
properties: {
|
|
3003
|
+
key: {
|
|
3004
|
+
type: "string",
|
|
3005
|
+
description: "The key to press (e.g., 'Enter', 'Tab', 'Escape')."
|
|
3006
|
+
}
|
|
3007
|
+
},
|
|
3008
|
+
required: ["key"]
|
|
3009
|
+
}
|
|
3010
|
+
},
|
|
3011
|
+
{
|
|
3012
|
+
name: "assert_visible",
|
|
3013
|
+
description: "Assert that an element matching the selector is visible on the page. Returns 'true' or 'false'.",
|
|
3014
|
+
input_schema: {
|
|
3015
|
+
type: "object",
|
|
3016
|
+
properties: {
|
|
3017
|
+
selector: {
|
|
3018
|
+
type: "string",
|
|
3019
|
+
description: "CSS selector of the element to check."
|
|
3020
|
+
}
|
|
3021
|
+
},
|
|
3022
|
+
required: ["selector"]
|
|
3023
|
+
}
|
|
3024
|
+
},
|
|
3025
|
+
{
|
|
3026
|
+
name: "assert_text",
|
|
3027
|
+
description: "Assert that the given text is visible somewhere on the page. Returns 'true' or 'false'.",
|
|
3028
|
+
input_schema: {
|
|
3029
|
+
type: "object",
|
|
3030
|
+
properties: {
|
|
3031
|
+
text: {
|
|
3032
|
+
type: "string",
|
|
3033
|
+
description: "The text to search for on the page."
|
|
3034
|
+
}
|
|
3035
|
+
},
|
|
3036
|
+
required: ["text"]
|
|
3037
|
+
}
|
|
3038
|
+
},
|
|
3039
|
+
{
|
|
3040
|
+
name: "report_result",
|
|
3041
|
+
description: "Report the final test result. Call this when you have completed testing the scenario. This MUST be the last tool you call.",
|
|
3042
|
+
input_schema: {
|
|
3043
|
+
type: "object",
|
|
3044
|
+
properties: {
|
|
3045
|
+
status: {
|
|
3046
|
+
type: "string",
|
|
3047
|
+
enum: ["passed", "failed"],
|
|
3048
|
+
description: "Whether the test scenario passed or failed."
|
|
3049
|
+
},
|
|
3050
|
+
reasoning: {
|
|
3051
|
+
type: "string",
|
|
3052
|
+
description: "Detailed explanation of why the test passed or failed, including any issues found."
|
|
3053
|
+
}
|
|
3054
|
+
},
|
|
3055
|
+
required: ["status", "reasoning"]
|
|
3056
|
+
}
|
|
3057
|
+
}
|
|
3058
|
+
];
|
|
3059
|
+
async function executeTool(page, screenshotter, toolName, toolInput, context) {
|
|
3060
|
+
try {
|
|
3061
|
+
switch (toolName) {
|
|
3062
|
+
case "navigate": {
|
|
3063
|
+
const url = toolInput.url;
|
|
3064
|
+
await page.goto(url, { waitUntil: "domcontentloaded" });
|
|
3065
|
+
const screenshot = await screenshotter.capture(page, {
|
|
3066
|
+
runId: context.runId,
|
|
3067
|
+
scenarioSlug: context.scenarioSlug,
|
|
3068
|
+
stepNumber: context.stepNumber,
|
|
3069
|
+
action: "navigate"
|
|
3070
|
+
});
|
|
3071
|
+
return {
|
|
3072
|
+
result: `Navigated to ${url}`,
|
|
3073
|
+
screenshot
|
|
3074
|
+
};
|
|
3075
|
+
}
|
|
3076
|
+
case "click": {
|
|
3077
|
+
const selector = toolInput.selector;
|
|
3078
|
+
await page.click(selector);
|
|
3079
|
+
const screenshot = await screenshotter.capture(page, {
|
|
3080
|
+
runId: context.runId,
|
|
3081
|
+
scenarioSlug: context.scenarioSlug,
|
|
3082
|
+
stepNumber: context.stepNumber,
|
|
3083
|
+
action: "click"
|
|
3084
|
+
});
|
|
3085
|
+
return {
|
|
3086
|
+
result: `Clicked element: ${selector}`,
|
|
3087
|
+
screenshot
|
|
3088
|
+
};
|
|
3089
|
+
}
|
|
3090
|
+
case "fill": {
|
|
3091
|
+
const selector = toolInput.selector;
|
|
3092
|
+
const value = toolInput.value;
|
|
3093
|
+
await page.fill(selector, value);
|
|
3094
|
+
return {
|
|
3095
|
+
result: `Filled "${selector}" with value`
|
|
3096
|
+
};
|
|
3097
|
+
}
|
|
3098
|
+
case "select_option": {
|
|
3099
|
+
const selector = toolInput.selector;
|
|
3100
|
+
const value = toolInput.value;
|
|
3101
|
+
await page.selectOption(selector, value);
|
|
3102
|
+
return {
|
|
3103
|
+
result: `Selected option "${value}" in ${selector}`
|
|
3104
|
+
};
|
|
3105
|
+
}
|
|
3106
|
+
case "screenshot": {
|
|
3107
|
+
const screenshot = await screenshotter.capture(page, {
|
|
3108
|
+
runId: context.runId,
|
|
3109
|
+
scenarioSlug: context.scenarioSlug,
|
|
3110
|
+
stepNumber: context.stepNumber,
|
|
3111
|
+
action: "screenshot"
|
|
3112
|
+
});
|
|
3113
|
+
return {
|
|
3114
|
+
result: "Screenshot captured",
|
|
3115
|
+
screenshot
|
|
3116
|
+
};
|
|
3117
|
+
}
|
|
3118
|
+
case "get_text": {
|
|
3119
|
+
const selector = toolInput.selector;
|
|
3120
|
+
const text = await page.locator(selector).textContent();
|
|
3121
|
+
return {
|
|
3122
|
+
result: text ?? "(no text content)"
|
|
3123
|
+
};
|
|
3124
|
+
}
|
|
3125
|
+
case "get_url": {
|
|
3126
|
+
return {
|
|
3127
|
+
result: page.url()
|
|
3128
|
+
};
|
|
3129
|
+
}
|
|
3130
|
+
case "wait_for": {
|
|
3131
|
+
const selector = toolInput.selector;
|
|
3132
|
+
const timeout = typeof toolInput.timeout === "number" ? toolInput.timeout : 1e4;
|
|
3133
|
+
await page.waitForSelector(selector, { timeout });
|
|
3134
|
+
return {
|
|
3135
|
+
result: `Element "${selector}" appeared`
|
|
3136
|
+
};
|
|
3137
|
+
}
|
|
3138
|
+
case "go_back": {
|
|
3139
|
+
await page.goBack();
|
|
3140
|
+
return {
|
|
3141
|
+
result: "Navigated back"
|
|
3142
|
+
};
|
|
3143
|
+
}
|
|
3144
|
+
case "press_key": {
|
|
3145
|
+
const key = toolInput.key;
|
|
3146
|
+
await page.keyboard.press(key);
|
|
3147
|
+
return {
|
|
3148
|
+
result: `Pressed key: ${key}`
|
|
3149
|
+
};
|
|
3150
|
+
}
|
|
3151
|
+
case "assert_visible": {
|
|
3152
|
+
const selector = toolInput.selector;
|
|
3153
|
+
try {
|
|
3154
|
+
const visible = await page.locator(selector).isVisible();
|
|
3155
|
+
return { result: visible ? "true" : "false" };
|
|
3156
|
+
} catch {
|
|
3157
|
+
return { result: "false" };
|
|
3158
|
+
}
|
|
3159
|
+
}
|
|
3160
|
+
case "assert_text": {
|
|
3161
|
+
const text = toolInput.text;
|
|
3162
|
+
try {
|
|
3163
|
+
const bodyText = await page.locator("body").textContent();
|
|
3164
|
+
const found = bodyText ? bodyText.includes(text) : false;
|
|
3165
|
+
return { result: found ? "true" : "false" };
|
|
3166
|
+
} catch {
|
|
3167
|
+
return { result: "false" };
|
|
3168
|
+
}
|
|
3169
|
+
}
|
|
3170
|
+
case "report_result": {
|
|
3171
|
+
const status = toolInput.status;
|
|
3172
|
+
const reasoning = toolInput.reasoning;
|
|
3173
|
+
return {
|
|
3174
|
+
result: `Test ${status}: ${reasoning}`
|
|
3175
|
+
};
|
|
3176
|
+
}
|
|
3177
|
+
default:
|
|
3178
|
+
return { result: `Unknown tool: ${toolName}` };
|
|
3179
|
+
}
|
|
3180
|
+
} catch (error) {
|
|
3181
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
3182
|
+
return { result: `Error executing ${toolName}: ${message}` };
|
|
3183
|
+
}
|
|
3184
|
+
}
|
|
3185
|
+
async function runAgentLoop(options) {
|
|
3186
|
+
const {
|
|
3187
|
+
client,
|
|
3188
|
+
page,
|
|
3189
|
+
scenario,
|
|
3190
|
+
screenshotter,
|
|
3191
|
+
model,
|
|
3192
|
+
runId,
|
|
3193
|
+
maxTurns = 30
|
|
3194
|
+
} = options;
|
|
3195
|
+
const systemPrompt = [
|
|
3196
|
+
"You are a QA testing agent. Test the following scenario by interacting with the browser.",
|
|
3197
|
+
"Use the provided tools to navigate, click, fill forms, and verify results.",
|
|
3198
|
+
"When done, call report_result with your findings.",
|
|
3199
|
+
"Be methodical: navigate to the target page first, then follow the test steps.",
|
|
3200
|
+
"If a step fails, try reasonable alternatives before reporting failure.",
|
|
3201
|
+
"Always report a final result \u2014 never leave a test incomplete."
|
|
3202
|
+
].join(" ");
|
|
3203
|
+
const userParts = [
|
|
3204
|
+
`**Scenario:** ${scenario.name}`,
|
|
3205
|
+
`**Description:** ${scenario.description}`
|
|
3206
|
+
];
|
|
3207
|
+
if (scenario.targetPath) {
|
|
3208
|
+
userParts.push(`**Target Path:** ${scenario.targetPath}`);
|
|
3209
|
+
}
|
|
3210
|
+
if (scenario.steps.length > 0) {
|
|
3211
|
+
userParts.push("**Steps:**");
|
|
3212
|
+
for (let i = 0;i < scenario.steps.length; i++) {
|
|
3213
|
+
userParts.push(`${i + 1}. ${scenario.steps[i]}`);
|
|
3214
|
+
}
|
|
3215
|
+
}
|
|
3216
|
+
const userMessage = userParts.join(`
|
|
3217
|
+
`);
|
|
3218
|
+
const screenshots = [];
|
|
3219
|
+
let tokensUsed = 0;
|
|
3220
|
+
let stepNumber = 0;
|
|
3221
|
+
const scenarioSlug = scenario.name.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
|
|
3222
|
+
let messages = [
|
|
3223
|
+
{ role: "user", content: userMessage }
|
|
3224
|
+
];
|
|
3225
|
+
try {
|
|
3226
|
+
for (let turn = 0;turn < maxTurns; turn++) {
|
|
3227
|
+
const response = await client.messages.create({
|
|
3228
|
+
model,
|
|
3229
|
+
max_tokens: 4096,
|
|
3230
|
+
system: systemPrompt,
|
|
3231
|
+
tools: BROWSER_TOOLS,
|
|
3232
|
+
messages
|
|
3233
|
+
});
|
|
3234
|
+
if (response.usage) {
|
|
3235
|
+
tokensUsed += response.usage.input_tokens + response.usage.output_tokens;
|
|
3236
|
+
}
|
|
3237
|
+
const toolUseBlocks = response.content.filter((block) => block.type === "tool_use");
|
|
3238
|
+
if (toolUseBlocks.length === 0 && response.stop_reason === "end_turn") {
|
|
3239
|
+
const textBlocks = response.content.filter((block) => block.type === "text");
|
|
3240
|
+
const textReasoning = textBlocks.map((b) => b.text).join(`
|
|
3241
|
+
`);
|
|
3242
|
+
return {
|
|
3243
|
+
status: "error",
|
|
3244
|
+
reasoning: textReasoning || "Agent ended without calling report_result",
|
|
3245
|
+
stepsCompleted: stepNumber,
|
|
3246
|
+
tokensUsed,
|
|
3247
|
+
screenshots
|
|
3248
|
+
};
|
|
3249
|
+
}
|
|
3250
|
+
const toolResults = [];
|
|
3251
|
+
for (const toolBlock of toolUseBlocks) {
|
|
3252
|
+
stepNumber++;
|
|
3253
|
+
const toolInput = toolBlock.input;
|
|
3254
|
+
const execResult = await executeTool(page, screenshotter, toolBlock.name, toolInput, { runId, scenarioSlug, stepNumber });
|
|
3255
|
+
if (execResult.screenshot) {
|
|
3256
|
+
screenshots.push({
|
|
3257
|
+
...execResult.screenshot,
|
|
3258
|
+
action: toolBlock.name,
|
|
3259
|
+
stepNumber
|
|
3260
|
+
});
|
|
3261
|
+
}
|
|
3262
|
+
toolResults.push({
|
|
3263
|
+
type: "tool_result",
|
|
3264
|
+
tool_use_id: toolBlock.id,
|
|
3265
|
+
content: execResult.result
|
|
3266
|
+
});
|
|
3267
|
+
if (toolBlock.name === "report_result") {
|
|
3268
|
+
const status = toolInput.status;
|
|
3269
|
+
const reasoning = toolInput.reasoning;
|
|
3270
|
+
return {
|
|
3271
|
+
status,
|
|
3272
|
+
reasoning,
|
|
3273
|
+
stepsCompleted: stepNumber,
|
|
3274
|
+
tokensUsed,
|
|
3275
|
+
screenshots
|
|
3276
|
+
};
|
|
3277
|
+
}
|
|
3278
|
+
}
|
|
3279
|
+
messages = [
|
|
3280
|
+
...messages,
|
|
3281
|
+
{ role: "assistant", content: response.content },
|
|
3282
|
+
{ role: "user", content: toolResults }
|
|
3283
|
+
];
|
|
3284
|
+
}
|
|
3285
|
+
return {
|
|
3286
|
+
status: "error",
|
|
3287
|
+
reasoning: `Agent reached maximum turn limit (${maxTurns}) without reporting a result`,
|
|
3288
|
+
stepsCompleted: stepNumber,
|
|
3289
|
+
tokensUsed,
|
|
3290
|
+
screenshots
|
|
3291
|
+
};
|
|
3292
|
+
} catch (error) {
|
|
3293
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
3294
|
+
throw new AIClientError(`Agent loop failed: ${message}`);
|
|
3295
|
+
}
|
|
3296
|
+
}
|
|
3297
|
+
function createClient(apiKey) {
|
|
3298
|
+
const key = apiKey ?? process.env["ANTHROPIC_API_KEY"];
|
|
3299
|
+
if (!key) {
|
|
3300
|
+
throw new AIClientError("No Anthropic API key provided. Set ANTHROPIC_API_KEY or pass it explicitly.");
|
|
3301
|
+
}
|
|
3302
|
+
return new Anthropic({ apiKey: key });
|
|
3303
|
+
}
|
|
3304
|
+
|
|
3305
|
+
// src/lib/config.ts
|
|
3306
|
+
import { homedir as homedir3 } from "os";
|
|
3307
|
+
import { join as join3 } from "path";
|
|
3308
|
+
import { readFileSync, existsSync as existsSync3 } from "fs";
|
|
3309
|
+
var CONFIG_DIR = join3(homedir3(), ".testers");
|
|
3310
|
+
var CONFIG_PATH = join3(CONFIG_DIR, "config.json");
|
|
3311
|
+
function getDefaultConfig() {
|
|
3312
|
+
return {
|
|
3313
|
+
defaultModel: "claude-haiku-4-5-20251001",
|
|
3314
|
+
models: { ...MODEL_MAP },
|
|
3315
|
+
browser: {
|
|
3316
|
+
headless: true,
|
|
3317
|
+
viewport: { width: 1280, height: 720 },
|
|
3318
|
+
timeout: 60000
|
|
3319
|
+
},
|
|
3320
|
+
screenshots: {
|
|
3321
|
+
dir: join3(homedir3(), ".testers", "screenshots"),
|
|
3322
|
+
format: "png",
|
|
3323
|
+
quality: 90,
|
|
3324
|
+
fullPage: false
|
|
3325
|
+
}
|
|
3326
|
+
};
|
|
3327
|
+
}
|
|
3328
|
+
function loadConfig() {
|
|
3329
|
+
const defaults = getDefaultConfig();
|
|
3330
|
+
let fileConfig = {};
|
|
3331
|
+
if (existsSync3(CONFIG_PATH)) {
|
|
3332
|
+
try {
|
|
3333
|
+
const raw = readFileSync(CONFIG_PATH, "utf-8");
|
|
3334
|
+
fileConfig = JSON.parse(raw);
|
|
3335
|
+
} catch {}
|
|
3336
|
+
}
|
|
3337
|
+
const config = {
|
|
3338
|
+
defaultModel: fileConfig.defaultModel ?? defaults.defaultModel,
|
|
3339
|
+
models: fileConfig.models ? { ...defaults.models, ...fileConfig.models } : { ...defaults.models },
|
|
3340
|
+
browser: fileConfig.browser ? { ...defaults.browser, ...fileConfig.browser } : { ...defaults.browser },
|
|
3341
|
+
screenshots: fileConfig.screenshots ? { ...defaults.screenshots, ...fileConfig.screenshots } : { ...defaults.screenshots },
|
|
3342
|
+
anthropicApiKey: fileConfig.anthropicApiKey,
|
|
3343
|
+
todosDbPath: fileConfig.todosDbPath
|
|
3344
|
+
};
|
|
3345
|
+
const envModel = process.env["TESTERS_MODEL"];
|
|
3346
|
+
if (envModel) {
|
|
3347
|
+
config.defaultModel = envModel;
|
|
3348
|
+
}
|
|
3349
|
+
const envScreenshotsDir = process.env["TESTERS_SCREENSHOTS_DIR"];
|
|
3350
|
+
if (envScreenshotsDir) {
|
|
3351
|
+
config.screenshots.dir = envScreenshotsDir;
|
|
3352
|
+
}
|
|
3353
|
+
const envApiKey = process.env["ANTHROPIC_API_KEY"];
|
|
3354
|
+
if (envApiKey) {
|
|
3355
|
+
config.anthropicApiKey = envApiKey;
|
|
3356
|
+
}
|
|
3357
|
+
return config;
|
|
3358
|
+
}
|
|
3359
|
+
|
|
3360
|
+
// src/lib/runner.ts
|
|
3361
|
+
var eventHandler = null;
|
|
3362
|
+
function emit(event) {
|
|
3363
|
+
if (eventHandler)
|
|
3364
|
+
eventHandler(event);
|
|
3365
|
+
}
|
|
3366
|
+
async function runSingleScenario(scenario, runId, options) {
|
|
3367
|
+
const config = loadConfig();
|
|
3368
|
+
const model = resolveModel(options.model ?? scenario.model ?? config.defaultModel);
|
|
3369
|
+
const client = createClient(options.apiKey ?? config.anthropicApiKey);
|
|
3370
|
+
const screenshotter = new Screenshotter({
|
|
3371
|
+
baseDir: options.screenshotDir ?? config.screenshots.dir
|
|
3372
|
+
});
|
|
3373
|
+
const result = createResult({
|
|
3374
|
+
runId,
|
|
3375
|
+
scenarioId: scenario.id,
|
|
3376
|
+
model,
|
|
3377
|
+
stepsTotal: scenario.steps.length || 10
|
|
3378
|
+
});
|
|
3379
|
+
emit({ type: "scenario:start", scenarioId: scenario.id, scenarioName: scenario.name, resultId: result.id, runId });
|
|
3380
|
+
let browser = null;
|
|
3381
|
+
let page = null;
|
|
3382
|
+
try {
|
|
3383
|
+
browser = await launchBrowser({ headless: !(options.headed ?? false) });
|
|
3384
|
+
page = await getPage(browser, {
|
|
3385
|
+
viewport: config.browser.viewport
|
|
3386
|
+
});
|
|
3387
|
+
const targetUrl = scenario.targetPath ? `${options.url.replace(/\/$/, "")}${scenario.targetPath}` : options.url;
|
|
3388
|
+
await page.goto(targetUrl, { timeout: options.timeout ?? config.browser.timeout });
|
|
3389
|
+
const agentResult = await runAgentLoop({
|
|
3390
|
+
client,
|
|
3391
|
+
page,
|
|
3392
|
+
scenario,
|
|
3393
|
+
screenshotter,
|
|
3394
|
+
model,
|
|
3395
|
+
runId,
|
|
3396
|
+
maxTurns: 30
|
|
3397
|
+
});
|
|
3398
|
+
for (const ss of agentResult.screenshots) {
|
|
3399
|
+
createScreenshot({
|
|
3400
|
+
resultId: result.id,
|
|
3401
|
+
stepNumber: ss.stepNumber,
|
|
3402
|
+
action: ss.action,
|
|
3403
|
+
filePath: ss.filePath,
|
|
3404
|
+
width: ss.width,
|
|
3405
|
+
height: ss.height
|
|
3406
|
+
});
|
|
3407
|
+
emit({ type: "screenshot:captured", screenshotPath: ss.filePath, scenarioId: scenario.id, runId });
|
|
3408
|
+
}
|
|
3409
|
+
const updatedResult = updateResult(result.id, {
|
|
3410
|
+
status: agentResult.status,
|
|
3411
|
+
reasoning: agentResult.reasoning,
|
|
3412
|
+
stepsCompleted: agentResult.stepsCompleted,
|
|
3413
|
+
durationMs: Date.now() - new Date(result.createdAt).getTime(),
|
|
3414
|
+
tokensUsed: agentResult.tokensUsed,
|
|
3415
|
+
costCents: estimateCost(model, agentResult.tokensUsed)
|
|
3416
|
+
});
|
|
3417
|
+
const eventType = agentResult.status === "passed" ? "scenario:pass" : "scenario:fail";
|
|
3418
|
+
emit({ type: eventType, scenarioId: scenario.id, scenarioName: scenario.name, resultId: result.id, runId });
|
|
3419
|
+
return updatedResult;
|
|
3420
|
+
} catch (error) {
|
|
3421
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
3422
|
+
const updatedResult = updateResult(result.id, {
|
|
3423
|
+
status: "error",
|
|
3424
|
+
error: errorMsg,
|
|
3425
|
+
durationMs: Date.now() - new Date(result.createdAt).getTime()
|
|
3426
|
+
});
|
|
3427
|
+
emit({ type: "scenario:error", scenarioId: scenario.id, scenarioName: scenario.name, error: errorMsg, runId });
|
|
3428
|
+
return updatedResult;
|
|
3429
|
+
} finally {
|
|
3430
|
+
if (browser)
|
|
3431
|
+
await closeBrowser(browser);
|
|
3432
|
+
}
|
|
3433
|
+
}
|
|
3434
|
+
async function runBatch(scenarios, options) {
|
|
3435
|
+
const config = loadConfig();
|
|
3436
|
+
const model = resolveModel(options.model ?? config.defaultModel);
|
|
3437
|
+
const parallel = options.parallel ?? 1;
|
|
3438
|
+
const run = createRun({
|
|
3439
|
+
url: options.url,
|
|
3440
|
+
model,
|
|
3441
|
+
headed: options.headed,
|
|
3442
|
+
parallel,
|
|
3443
|
+
projectId: options.projectId
|
|
3444
|
+
});
|
|
3445
|
+
updateRun(run.id, { status: "running", total: scenarios.length });
|
|
3446
|
+
const results = [];
|
|
3447
|
+
if (parallel <= 1) {
|
|
3448
|
+
for (const scenario of scenarios) {
|
|
3449
|
+
const result = await runSingleScenario(scenario, run.id, options);
|
|
3450
|
+
results.push(result);
|
|
3451
|
+
}
|
|
3452
|
+
} else {
|
|
3453
|
+
const queue = [...scenarios];
|
|
3454
|
+
const running = [];
|
|
3455
|
+
const processNext = async () => {
|
|
3456
|
+
const scenario = queue.shift();
|
|
3457
|
+
if (!scenario)
|
|
3458
|
+
return;
|
|
3459
|
+
const result = await runSingleScenario(scenario, run.id, options);
|
|
3460
|
+
results.push(result);
|
|
3461
|
+
await processNext();
|
|
3462
|
+
};
|
|
3463
|
+
const workers = Math.min(parallel, scenarios.length);
|
|
3464
|
+
for (let i = 0;i < workers; i++) {
|
|
3465
|
+
running.push(processNext());
|
|
3466
|
+
}
|
|
3467
|
+
await Promise.all(running);
|
|
3468
|
+
}
|
|
3469
|
+
const passed = results.filter((r) => r.status === "passed").length;
|
|
3470
|
+
const failed = results.filter((r) => r.status === "failed" || r.status === "error").length;
|
|
3471
|
+
const finalStatus = failed > 0 ? "failed" : "passed";
|
|
3472
|
+
const finalRun = updateRun(run.id, {
|
|
3473
|
+
status: finalStatus,
|
|
3474
|
+
passed,
|
|
3475
|
+
failed,
|
|
3476
|
+
total: scenarios.length,
|
|
3477
|
+
finished_at: new Date().toISOString()
|
|
3478
|
+
});
|
|
3479
|
+
emit({ type: "run:complete", runId: run.id });
|
|
3480
|
+
return { run: finalRun, results };
|
|
3481
|
+
}
|
|
3482
|
+
async function runByFilter(options) {
|
|
3483
|
+
let scenarios;
|
|
3484
|
+
if (options.scenarioIds && options.scenarioIds.length > 0) {
|
|
3485
|
+
const all = listScenarios({ projectId: options.projectId });
|
|
3486
|
+
scenarios = all.filter((s) => options.scenarioIds.includes(s.id) || options.scenarioIds.includes(s.shortId));
|
|
3487
|
+
} else {
|
|
3488
|
+
scenarios = listScenarios({
|
|
3489
|
+
projectId: options.projectId,
|
|
3490
|
+
tags: options.tags,
|
|
3491
|
+
priority: options.priority
|
|
3492
|
+
});
|
|
3493
|
+
}
|
|
3494
|
+
if (scenarios.length === 0) {
|
|
3495
|
+
const config = loadConfig();
|
|
3496
|
+
const model = resolveModel(options.model ?? config.defaultModel);
|
|
3497
|
+
const run = createRun({ url: options.url, model, projectId: options.projectId });
|
|
3498
|
+
updateRun(run.id, { status: "passed", total: 0, finished_at: new Date().toISOString() });
|
|
3499
|
+
return { run: getRun(run.id), results: [] };
|
|
3500
|
+
}
|
|
3501
|
+
return runBatch(scenarios, options);
|
|
3502
|
+
}
|
|
3503
|
+
function estimateCost(model, tokens) {
|
|
3504
|
+
const costs = {
|
|
3505
|
+
"claude-haiku-4-5-20251001": 0.1,
|
|
3506
|
+
"claude-sonnet-4-6-20260311": 0.9,
|
|
3507
|
+
"claude-opus-4-6-20260311": 3
|
|
3508
|
+
};
|
|
3509
|
+
const costPer1M = costs[model] ?? 0.5;
|
|
3510
|
+
return tokens / 1e6 * costPer1M * 100;
|
|
3511
|
+
}
|
|
3512
|
+
|
|
3513
|
+
// src/lib/reporter.ts
|
|
3514
|
+
import chalk from "chalk";
|
|
3515
|
+
function formatTerminal(run, results) {
|
|
3516
|
+
const lines = [];
|
|
3517
|
+
lines.push("");
|
|
3518
|
+
lines.push(chalk.bold(` Run ${run.id.slice(0, 8)} \u2014 ${run.url}`));
|
|
3519
|
+
lines.push(chalk.dim(` Model: ${run.model} | Parallel: ${run.parallel} | Headed: ${run.headed ? "yes" : "no"}`));
|
|
3520
|
+
lines.push("");
|
|
3521
|
+
for (const result of results) {
|
|
3522
|
+
const scenario = getScenario(result.scenarioId);
|
|
3523
|
+
const name = scenario ? `${scenario.shortId}: ${scenario.name}` : result.scenarioId.slice(0, 8);
|
|
3524
|
+
const screenshots = listScreenshots(result.id);
|
|
3525
|
+
const duration = `${(result.durationMs / 1000).toFixed(1)}s`;
|
|
3526
|
+
const screenshotCount = screenshots.length;
|
|
3527
|
+
let statusIcon;
|
|
3528
|
+
let statusColor;
|
|
3529
|
+
switch (result.status) {
|
|
3530
|
+
case "passed":
|
|
3531
|
+
statusIcon = chalk.green("PASS");
|
|
3532
|
+
statusColor = chalk.green;
|
|
3533
|
+
break;
|
|
3534
|
+
case "failed":
|
|
3535
|
+
statusIcon = chalk.red("FAIL");
|
|
3536
|
+
statusColor = chalk.red;
|
|
3537
|
+
break;
|
|
3538
|
+
case "error":
|
|
3539
|
+
statusIcon = chalk.yellow("ERR ");
|
|
3540
|
+
statusColor = chalk.yellow;
|
|
3541
|
+
break;
|
|
3542
|
+
default:
|
|
3543
|
+
statusIcon = chalk.dim("SKIP");
|
|
3544
|
+
statusColor = chalk.dim;
|
|
3545
|
+
break;
|
|
3546
|
+
}
|
|
3547
|
+
lines.push(` ${statusIcon} ${statusColor(name)} ${chalk.dim(duration)} ${chalk.dim(`${screenshotCount} screenshots`)}`);
|
|
3548
|
+
if (result.reasoning && (result.status === "failed" || result.status === "error")) {
|
|
3549
|
+
lines.push(chalk.dim(` ${result.reasoning}`));
|
|
3550
|
+
}
|
|
3551
|
+
if (result.error) {
|
|
3552
|
+
lines.push(chalk.red(` ${result.error}`));
|
|
3553
|
+
}
|
|
3554
|
+
}
|
|
3555
|
+
lines.push("");
|
|
3556
|
+
lines.push(formatSummary(run));
|
|
3557
|
+
lines.push("");
|
|
3558
|
+
return lines.join(`
|
|
3559
|
+
`);
|
|
3560
|
+
}
|
|
3561
|
+
function formatSummary(run) {
|
|
3562
|
+
const duration = run.finishedAt ? `${((new Date(run.finishedAt).getTime() - new Date(run.startedAt).getTime()) / 1000).toFixed(1)}s` : "running";
|
|
3563
|
+
const passedStr = chalk.green(`${run.passed} passed`);
|
|
3564
|
+
const failedStr = run.failed > 0 ? chalk.red(` ${run.failed} failed`) : "";
|
|
3565
|
+
const totalStr = chalk.dim(` (${run.total} total)`);
|
|
3566
|
+
return ` ${passedStr}${failedStr}${totalStr} in ${duration}`;
|
|
3567
|
+
}
|
|
3568
|
+
function formatJSON(run, results) {
|
|
3569
|
+
const output = {
|
|
3570
|
+
run: {
|
|
3571
|
+
id: run.id,
|
|
3572
|
+
url: run.url,
|
|
3573
|
+
status: run.status,
|
|
3574
|
+
model: run.model,
|
|
3575
|
+
headed: run.headed,
|
|
3576
|
+
parallel: run.parallel,
|
|
3577
|
+
total: run.total,
|
|
3578
|
+
passed: run.passed,
|
|
3579
|
+
failed: run.failed,
|
|
3580
|
+
startedAt: run.startedAt,
|
|
3581
|
+
finishedAt: run.finishedAt
|
|
3582
|
+
},
|
|
3583
|
+
results: results.map((r) => {
|
|
3584
|
+
const scenario = getScenario(r.scenarioId);
|
|
3585
|
+
const screenshots = listScreenshots(r.id);
|
|
3586
|
+
return {
|
|
3587
|
+
id: r.id,
|
|
3588
|
+
scenarioId: r.scenarioId,
|
|
3589
|
+
scenarioName: scenario?.name ?? null,
|
|
3590
|
+
scenarioShortId: scenario?.shortId ?? null,
|
|
3591
|
+
status: r.status,
|
|
3592
|
+
reasoning: r.reasoning,
|
|
3593
|
+
error: r.error,
|
|
3594
|
+
stepsCompleted: r.stepsCompleted,
|
|
3595
|
+
stepsTotal: r.stepsTotal,
|
|
3596
|
+
durationMs: r.durationMs,
|
|
3597
|
+
model: r.model,
|
|
3598
|
+
tokensUsed: r.tokensUsed,
|
|
3599
|
+
costCents: r.costCents,
|
|
3600
|
+
screenshots: screenshots.map((s) => ({
|
|
3601
|
+
stepNumber: s.stepNumber,
|
|
3602
|
+
action: s.action,
|
|
3603
|
+
filePath: s.filePath
|
|
3604
|
+
}))
|
|
3605
|
+
};
|
|
3606
|
+
}),
|
|
3607
|
+
summary: {
|
|
3608
|
+
total: run.total,
|
|
3609
|
+
passed: run.passed,
|
|
3610
|
+
failed: run.failed,
|
|
3611
|
+
totalTokens: results.reduce((sum, r) => sum + r.tokensUsed, 0),
|
|
3612
|
+
totalCostCents: results.reduce((sum, r) => sum + r.costCents, 0),
|
|
3613
|
+
durationMs: run.finishedAt ? new Date(run.finishedAt).getTime() - new Date(run.startedAt).getTime() : null
|
|
3614
|
+
}
|
|
3615
|
+
};
|
|
3616
|
+
return JSON.stringify(output, null, 2);
|
|
3617
|
+
}
|
|
3618
|
+
function getExitCode(run) {
|
|
3619
|
+
if (run.status === "passed")
|
|
3620
|
+
return 0;
|
|
3621
|
+
if (run.status === "failed")
|
|
3622
|
+
return 1;
|
|
3623
|
+
return 2;
|
|
3624
|
+
}
|
|
3625
|
+
function formatRunList(runs) {
|
|
3626
|
+
const lines = [];
|
|
3627
|
+
lines.push("");
|
|
3628
|
+
lines.push(chalk.bold(" Recent Runs"));
|
|
3629
|
+
lines.push("");
|
|
3630
|
+
if (runs.length === 0) {
|
|
3631
|
+
lines.push(chalk.dim(" No runs found."));
|
|
3632
|
+
lines.push("");
|
|
3633
|
+
return lines.join(`
|
|
3634
|
+
`);
|
|
3635
|
+
}
|
|
3636
|
+
for (const run of runs) {
|
|
3637
|
+
const statusIcon = run.status === "passed" ? chalk.green("PASS") : run.status === "failed" ? chalk.red("FAIL") : run.status === "running" ? chalk.blue("RUN ") : chalk.dim(run.status.toUpperCase().padEnd(4));
|
|
3638
|
+
const date = new Date(run.startedAt).toLocaleString();
|
|
3639
|
+
const id = run.id.slice(0, 8);
|
|
3640
|
+
lines.push(` ${statusIcon} ${chalk.dim(id)} ${run.url} ${chalk.dim(`${run.passed}/${run.total}`)} ${chalk.dim(date)}`);
|
|
3641
|
+
}
|
|
3642
|
+
lines.push("");
|
|
3643
|
+
return lines.join(`
|
|
3644
|
+
`);
|
|
3645
|
+
}
|
|
3646
|
+
function formatScenarioList(scenarios) {
|
|
3647
|
+
const lines = [];
|
|
3648
|
+
lines.push("");
|
|
3649
|
+
lines.push(chalk.bold(" Scenarios"));
|
|
3650
|
+
lines.push("");
|
|
3651
|
+
if (scenarios.length === 0) {
|
|
3652
|
+
lines.push(chalk.dim(" No scenarios found. Use 'testers add' to create one."));
|
|
3653
|
+
lines.push("");
|
|
3654
|
+
return lines.join(`
|
|
3655
|
+
`);
|
|
3656
|
+
}
|
|
3657
|
+
for (const s of scenarios) {
|
|
3658
|
+
const priorityColor = s.priority === "critical" ? chalk.red : s.priority === "high" ? chalk.yellow : s.priority === "medium" ? chalk.blue : chalk.dim;
|
|
3659
|
+
const tags = s.tags.length > 0 ? chalk.dim(` [${s.tags.join(", ")}]`) : "";
|
|
3660
|
+
lines.push(` ${chalk.cyan(s.shortId)} ${s.name} ${priorityColor(s.priority)}${tags}`);
|
|
3661
|
+
}
|
|
3662
|
+
lines.push("");
|
|
3663
|
+
return lines.join(`
|
|
3664
|
+
`);
|
|
3665
|
+
}
|
|
3666
|
+
|
|
3667
|
+
// src/lib/todos-connector.ts
|
|
3668
|
+
import { Database as Database2 } from "bun:sqlite";
|
|
3669
|
+
import { existsSync as existsSync4 } from "fs";
|
|
3670
|
+
import { join as join4 } from "path";
|
|
3671
|
+
import { homedir as homedir4 } from "os";
|
|
3672
|
+
function resolveTodosDbPath() {
|
|
3673
|
+
const envPath = process.env["TODOS_DB_PATH"];
|
|
3674
|
+
if (envPath)
|
|
3675
|
+
return envPath;
|
|
3676
|
+
return join4(homedir4(), ".todos", "todos.db");
|
|
3677
|
+
}
|
|
3678
|
+
function connectToTodos() {
|
|
3679
|
+
const dbPath = resolveTodosDbPath();
|
|
3680
|
+
if (!existsSync4(dbPath)) {
|
|
3681
|
+
throw new TodosConnectionError(`Todos database not found at ${dbPath}. Install @hasna/todos or set TODOS_DB_PATH.`);
|
|
3682
|
+
}
|
|
3683
|
+
const db2 = new Database2(dbPath, { readonly: true });
|
|
3684
|
+
db2.exec("PRAGMA foreign_keys = ON");
|
|
3685
|
+
return db2;
|
|
3686
|
+
}
|
|
3687
|
+
function pullTasks(options = {}) {
|
|
3688
|
+
const db2 = connectToTodos();
|
|
3689
|
+
try {
|
|
3690
|
+
let query = "SELECT id, short_id, title, description, status, priority, tags, project_id FROM tasks WHERE 1=1";
|
|
3691
|
+
const params = [];
|
|
3692
|
+
if (options.status) {
|
|
3693
|
+
query += " AND status = ?";
|
|
3694
|
+
params.push(options.status);
|
|
3695
|
+
} else {
|
|
3696
|
+
query += " AND status IN ('pending', 'in_progress')";
|
|
3697
|
+
}
|
|
3698
|
+
if (options.priority) {
|
|
3699
|
+
query += " AND priority = ?";
|
|
3700
|
+
params.push(options.priority);
|
|
3701
|
+
}
|
|
3702
|
+
if (options.projectName) {
|
|
3703
|
+
const project = db2.query("SELECT id FROM projects WHERE name = ?").get(options.projectName);
|
|
3704
|
+
if (project) {
|
|
3705
|
+
query += " AND project_id = ?";
|
|
3706
|
+
params.push(project.id);
|
|
3707
|
+
}
|
|
3708
|
+
}
|
|
3709
|
+
query += " ORDER BY CASE priority WHEN 'critical' THEN 0 WHEN 'high' THEN 1 WHEN 'medium' THEN 2 WHEN 'low' THEN 3 END";
|
|
3710
|
+
const tasks = db2.query(query).all(...params);
|
|
3711
|
+
if (options.tags && options.tags.length > 0) {
|
|
3712
|
+
return tasks.filter((task) => {
|
|
3713
|
+
const taskTags = JSON.parse(task.tags || "[]");
|
|
3714
|
+
return options.tags.some((tag) => taskTags.includes(tag));
|
|
3715
|
+
});
|
|
3716
|
+
}
|
|
3717
|
+
return tasks;
|
|
3718
|
+
} finally {
|
|
3719
|
+
db2.close();
|
|
3720
|
+
}
|
|
3721
|
+
}
|
|
3722
|
+
function taskToScenarioInput(task, projectId) {
|
|
3723
|
+
const tags = JSON.parse(task.tags || "[]");
|
|
3724
|
+
const priority = ["low", "medium", "high", "critical"].includes(task.priority) ? task.priority : "medium";
|
|
3725
|
+
const steps = [];
|
|
3726
|
+
if (task.description) {
|
|
3727
|
+
const lines = task.description.split(`
|
|
3728
|
+
`);
|
|
3729
|
+
for (const line of lines) {
|
|
3730
|
+
const match = line.match(/^\s*\d+[\.\)]\s*(.+)/);
|
|
3731
|
+
if (match?.[1]) {
|
|
3732
|
+
steps.push(match[1].trim());
|
|
3733
|
+
}
|
|
3734
|
+
}
|
|
3735
|
+
}
|
|
3736
|
+
return {
|
|
3737
|
+
name: task.title.replace(/^(OPE\d+-\d+|[A-Z]+-\d+):\s*/, ""),
|
|
3738
|
+
description: task.description || task.title,
|
|
3739
|
+
steps,
|
|
3740
|
+
tags,
|
|
3741
|
+
priority,
|
|
3742
|
+
projectId,
|
|
3743
|
+
metadata: { todosTaskId: task.id, todosShortId: task.short_id }
|
|
3744
|
+
};
|
|
3745
|
+
}
|
|
3746
|
+
function importFromTodos(options = {}) {
|
|
3747
|
+
const tasks = pullTasks({
|
|
3748
|
+
projectName: options.projectName,
|
|
3749
|
+
tags: options.tags ?? ["qa", "test", "testing"],
|
|
3750
|
+
priority: options.priority
|
|
3751
|
+
});
|
|
3752
|
+
const existing = listScenarios({ projectId: options.projectId });
|
|
3753
|
+
const existingTodoIds = new Set(existing.filter((s) => s.metadata?.todosTaskId).map((s) => s.metadata.todosTaskId));
|
|
3754
|
+
let imported = 0;
|
|
3755
|
+
let skipped = 0;
|
|
3756
|
+
for (const task of tasks) {
|
|
3757
|
+
if (existingTodoIds.has(task.id)) {
|
|
3758
|
+
skipped++;
|
|
3759
|
+
continue;
|
|
3760
|
+
}
|
|
3761
|
+
const input = taskToScenarioInput(task, options.projectId);
|
|
3762
|
+
createScenario(input);
|
|
3763
|
+
imported++;
|
|
3764
|
+
}
|
|
3765
|
+
return { imported, skipped };
|
|
3766
|
+
}
|
|
3767
|
+
|
|
3768
|
+
// src/cli/index.tsx
|
|
3769
|
+
var program2 = new Command;
|
|
3770
|
+
program2.name("testers").version("0.0.1").description("AI-powered browser testing CLI");
|
|
3771
|
+
program2.command("add <name>").description("Create a new test scenario").option("-d, --description <text>", "Scenario description", "").option("-s, --steps <step>", "Test step (repeatable)", (val, acc) => {
|
|
3772
|
+
acc.push(val);
|
|
3773
|
+
return acc;
|
|
3774
|
+
}, []).option("-t, --tag <tag>", "Tag (repeatable)", (val, acc) => {
|
|
3775
|
+
acc.push(val);
|
|
3776
|
+
return acc;
|
|
3777
|
+
}, []).option("-p, --priority <level>", "Priority level", "medium").option("-m, --model <model>", "AI model to use").option("--path <path>", "Target path on the URL").option("--auth", "Requires authentication", false).option("--timeout <ms>", "Timeout in milliseconds").action((name, opts) => {
|
|
3778
|
+
try {
|
|
3779
|
+
const scenario = createScenario({
|
|
3780
|
+
name,
|
|
3781
|
+
description: opts.description || name,
|
|
3782
|
+
steps: opts.steps,
|
|
3783
|
+
tags: opts.tag,
|
|
3784
|
+
priority: opts.priority,
|
|
3785
|
+
model: opts.model,
|
|
3786
|
+
targetPath: opts.path,
|
|
3787
|
+
requiresAuth: opts.auth,
|
|
3788
|
+
timeoutMs: opts.timeout ? parseInt(opts.timeout, 10) : undefined
|
|
3789
|
+
});
|
|
3790
|
+
console.log(chalk2.green(`Created scenario ${chalk2.bold(scenario.shortId)}: ${scenario.name}`));
|
|
3791
|
+
} catch (error) {
|
|
3792
|
+
console.error(chalk2.red(`Error: ${error instanceof Error ? error.message : String(error)}`));
|
|
3793
|
+
process.exit(1);
|
|
3794
|
+
}
|
|
3795
|
+
});
|
|
3796
|
+
program2.command("list").description("List test scenarios").option("-t, --tag <tag>", "Filter by tag").option("-p, --priority <level>", "Filter by priority").option("--project <id>", "Filter by project ID").option("-l, --limit <n>", "Limit results", "50").action((opts) => {
|
|
3797
|
+
try {
|
|
3798
|
+
const scenarios = listScenarios({
|
|
3799
|
+
tags: opts.tag ? [opts.tag] : undefined,
|
|
3800
|
+
priority: opts.priority,
|
|
3801
|
+
projectId: opts.project,
|
|
3802
|
+
limit: parseInt(opts.limit, 10)
|
|
3803
|
+
});
|
|
3804
|
+
console.log(formatScenarioList(scenarios));
|
|
3805
|
+
} catch (error) {
|
|
3806
|
+
console.error(chalk2.red(`Error: ${error instanceof Error ? error.message : String(error)}`));
|
|
3807
|
+
process.exit(1);
|
|
3808
|
+
}
|
|
3809
|
+
});
|
|
3810
|
+
program2.command("show <id>").description("Show scenario details").action((id) => {
|
|
3811
|
+
try {
|
|
3812
|
+
const scenario = getScenario(id) ?? getScenarioByShortId(id);
|
|
3813
|
+
if (!scenario) {
|
|
3814
|
+
console.error(chalk2.red(`Scenario not found: ${id}`));
|
|
3815
|
+
process.exit(1);
|
|
3816
|
+
}
|
|
3817
|
+
console.log("");
|
|
3818
|
+
console.log(chalk2.bold(` Scenario ${scenario.shortId}`));
|
|
3819
|
+
console.log(` Name: ${scenario.name}`);
|
|
3820
|
+
console.log(` ID: ${chalk2.dim(scenario.id)}`);
|
|
3821
|
+
console.log(` Description: ${scenario.description}`);
|
|
3822
|
+
console.log(` Priority: ${scenario.priority}`);
|
|
3823
|
+
console.log(` Model: ${scenario.model ?? chalk2.dim("default")}`);
|
|
3824
|
+
console.log(` Tags: ${scenario.tags.length > 0 ? scenario.tags.join(", ") : chalk2.dim("none")}`);
|
|
3825
|
+
console.log(` Path: ${scenario.targetPath ?? chalk2.dim("none")}`);
|
|
3826
|
+
console.log(` Auth: ${scenario.requiresAuth ? "yes" : "no"}`);
|
|
3827
|
+
console.log(` Timeout: ${scenario.timeoutMs ? `${scenario.timeoutMs}ms` : chalk2.dim("default")}`);
|
|
3828
|
+
console.log(` Version: ${scenario.version}`);
|
|
3829
|
+
console.log(` Created: ${scenario.createdAt}`);
|
|
3830
|
+
console.log(` Updated: ${scenario.updatedAt}`);
|
|
3831
|
+
if (scenario.steps.length > 0) {
|
|
3832
|
+
console.log("");
|
|
3833
|
+
console.log(chalk2.bold(" Steps:"));
|
|
3834
|
+
for (let i = 0;i < scenario.steps.length; i++) {
|
|
3835
|
+
console.log(` ${i + 1}. ${scenario.steps[i]}`);
|
|
3836
|
+
}
|
|
3837
|
+
}
|
|
3838
|
+
console.log("");
|
|
3839
|
+
} catch (error) {
|
|
3840
|
+
console.error(chalk2.red(`Error: ${error instanceof Error ? error.message : String(error)}`));
|
|
3841
|
+
process.exit(1);
|
|
3842
|
+
}
|
|
3843
|
+
});
|
|
3844
|
+
program2.command("update <id>").description("Update a scenario").option("-n, --name <name>", "New name").option("-d, --description <text>", "New description").option("-s, --steps <step>", "Replace steps (repeatable)", (val, acc) => {
|
|
3845
|
+
acc.push(val);
|
|
3846
|
+
return acc;
|
|
3847
|
+
}, []).option("-t, --tag <tag>", "Replace tags (repeatable)", (val, acc) => {
|
|
3848
|
+
acc.push(val);
|
|
3849
|
+
return acc;
|
|
3850
|
+
}, []).option("-p, --priority <level>", "New priority").option("-m, --model <model>", "New model").action((id, opts) => {
|
|
3851
|
+
try {
|
|
3852
|
+
const scenario = getScenario(id) ?? getScenarioByShortId(id);
|
|
3853
|
+
if (!scenario) {
|
|
3854
|
+
console.error(chalk2.red(`Scenario not found: ${id}`));
|
|
3855
|
+
process.exit(1);
|
|
3856
|
+
}
|
|
3857
|
+
const updated = updateScenario(scenario.id, {
|
|
3858
|
+
name: opts.name,
|
|
3859
|
+
description: opts.description,
|
|
3860
|
+
steps: opts.steps.length > 0 ? opts.steps : undefined,
|
|
3861
|
+
tags: opts.tag.length > 0 ? opts.tag : undefined,
|
|
3862
|
+
priority: opts.priority,
|
|
3863
|
+
model: opts.model
|
|
3864
|
+
}, scenario.version);
|
|
3865
|
+
console.log(chalk2.green(`Updated scenario ${chalk2.bold(updated.shortId)}: ${updated.name}`));
|
|
3866
|
+
} catch (error) {
|
|
3867
|
+
console.error(chalk2.red(`Error: ${error instanceof Error ? error.message : String(error)}`));
|
|
3868
|
+
process.exit(1);
|
|
3869
|
+
}
|
|
3870
|
+
});
|
|
3871
|
+
program2.command("delete <id>").description("Delete a scenario").action((id) => {
|
|
3872
|
+
try {
|
|
3873
|
+
const scenario = getScenario(id) ?? getScenarioByShortId(id);
|
|
3874
|
+
if (!scenario) {
|
|
3875
|
+
console.error(chalk2.red(`Scenario not found: ${id}`));
|
|
3876
|
+
process.exit(1);
|
|
3877
|
+
}
|
|
3878
|
+
const deleted = deleteScenario(scenario.id);
|
|
3879
|
+
if (deleted) {
|
|
3880
|
+
console.log(chalk2.green(`Deleted scenario ${scenario.shortId}: ${scenario.name}`));
|
|
3881
|
+
} else {
|
|
3882
|
+
console.error(chalk2.red(`Failed to delete scenario: ${id}`));
|
|
3883
|
+
process.exit(1);
|
|
3884
|
+
}
|
|
3885
|
+
} catch (error) {
|
|
3886
|
+
console.error(chalk2.red(`Error: ${error instanceof Error ? error.message : String(error)}`));
|
|
3887
|
+
process.exit(1);
|
|
3888
|
+
}
|
|
3889
|
+
});
|
|
3890
|
+
program2.command("run <url> [description]").description("Run test scenarios against a URL").option("-t, --tag <tag>", "Filter by tag (repeatable)", (val, acc) => {
|
|
3891
|
+
acc.push(val);
|
|
3892
|
+
return acc;
|
|
3893
|
+
}, []).option("-s, --scenario <id>", "Run specific scenario ID").option("-p, --priority <level>", "Filter by priority").option("--headed", "Run browser in headed mode", false).option("-m, --model <model>", "AI model to use").option("--parallel <n>", "Number of parallel browsers", "1").option("--json", "Output results as JSON", false).option("-o, --output <filepath>", "Write JSON results to file").option("--timeout <ms>", "Timeout in milliseconds").option("--from-todos", "Import scenarios from todos before running", false).option("--project <id>", "Project ID").action(async (url, description, opts) => {
|
|
3894
|
+
try {
|
|
3895
|
+
if (description) {
|
|
3896
|
+
const scenario = createScenario({
|
|
3897
|
+
name: description,
|
|
3898
|
+
description,
|
|
3899
|
+
tags: ["ad-hoc"],
|
|
3900
|
+
projectId: opts.project
|
|
3901
|
+
});
|
|
3902
|
+
const { run: run2, results: results2 } = await runByFilter({
|
|
3903
|
+
url,
|
|
3904
|
+
scenarioIds: [scenario.id],
|
|
3905
|
+
model: opts.model,
|
|
3906
|
+
headed: opts.headed,
|
|
3907
|
+
parallel: parseInt(opts.parallel, 10),
|
|
3908
|
+
timeout: opts.timeout ? parseInt(opts.timeout, 10) : undefined,
|
|
3909
|
+
projectId: opts.project
|
|
3910
|
+
});
|
|
3911
|
+
if (opts.json || opts.output) {
|
|
3912
|
+
const jsonOutput = formatJSON(run2, results2);
|
|
3913
|
+
if (opts.output) {
|
|
3914
|
+
writeFileSync(opts.output, jsonOutput, "utf-8");
|
|
3915
|
+
console.log(chalk2.green(`Results written to ${opts.output}`));
|
|
3916
|
+
}
|
|
3917
|
+
if (opts.json) {
|
|
3918
|
+
console.log(jsonOutput);
|
|
3919
|
+
}
|
|
3920
|
+
} else {
|
|
3921
|
+
console.log(formatTerminal(run2, results2));
|
|
3922
|
+
}
|
|
3923
|
+
process.exit(getExitCode(run2));
|
|
3924
|
+
}
|
|
3925
|
+
if (opts.fromTodos) {
|
|
3926
|
+
const result = importFromTodos({ projectId: opts.project });
|
|
3927
|
+
console.log(chalk2.blue(`Imported ${result.imported} scenarios from todos (${result.skipped} skipped)`));
|
|
3928
|
+
}
|
|
3929
|
+
const { run, results } = await runByFilter({
|
|
3930
|
+
url,
|
|
3931
|
+
tags: opts.tag.length > 0 ? opts.tag : undefined,
|
|
3932
|
+
scenarioIds: opts.scenario ? [opts.scenario] : undefined,
|
|
3933
|
+
priority: opts.priority,
|
|
3934
|
+
model: opts.model,
|
|
3935
|
+
headed: opts.headed,
|
|
3936
|
+
parallel: parseInt(opts.parallel, 10),
|
|
3937
|
+
timeout: opts.timeout ? parseInt(opts.timeout, 10) : undefined,
|
|
3938
|
+
projectId: opts.project
|
|
3939
|
+
});
|
|
3940
|
+
if (opts.json || opts.output) {
|
|
3941
|
+
const jsonOutput = formatJSON(run, results);
|
|
3942
|
+
if (opts.output) {
|
|
3943
|
+
writeFileSync(opts.output, jsonOutput, "utf-8");
|
|
3944
|
+
console.log(chalk2.green(`Results written to ${opts.output}`));
|
|
3945
|
+
}
|
|
3946
|
+
if (opts.json) {
|
|
3947
|
+
console.log(jsonOutput);
|
|
3948
|
+
}
|
|
3949
|
+
} else {
|
|
3950
|
+
console.log(formatTerminal(run, results));
|
|
3951
|
+
}
|
|
3952
|
+
process.exit(getExitCode(run));
|
|
3953
|
+
} catch (error) {
|
|
3954
|
+
console.error(chalk2.red(`Error: ${error instanceof Error ? error.message : String(error)}`));
|
|
3955
|
+
process.exit(1);
|
|
3956
|
+
}
|
|
3957
|
+
});
|
|
3958
|
+
program2.command("runs").description("List past test runs").option("--status <status>", "Filter by status").option("-l, --limit <n>", "Limit results", "20").action((opts) => {
|
|
3959
|
+
try {
|
|
3960
|
+
const runs = listRuns({
|
|
3961
|
+
status: opts.status,
|
|
3962
|
+
limit: parseInt(opts.limit, 10)
|
|
3963
|
+
});
|
|
3964
|
+
console.log(formatRunList(runs));
|
|
3965
|
+
} catch (error) {
|
|
3966
|
+
console.error(chalk2.red(`Error: ${error instanceof Error ? error.message : String(error)}`));
|
|
3967
|
+
process.exit(1);
|
|
3968
|
+
}
|
|
3969
|
+
});
|
|
3970
|
+
program2.command("results <run-id>").description("Show results for a test run").action((runId) => {
|
|
3971
|
+
try {
|
|
3972
|
+
const run = getRun(runId);
|
|
3973
|
+
if (!run) {
|
|
3974
|
+
console.error(chalk2.red(`Run not found: ${runId}`));
|
|
3975
|
+
process.exit(1);
|
|
3976
|
+
}
|
|
3977
|
+
const results = getResultsByRun(run.id);
|
|
3978
|
+
console.log(formatTerminal(run, results));
|
|
3979
|
+
} catch (error) {
|
|
3980
|
+
console.error(chalk2.red(`Error: ${error instanceof Error ? error.message : String(error)}`));
|
|
3981
|
+
process.exit(1);
|
|
3982
|
+
}
|
|
3983
|
+
});
|
|
3984
|
+
program2.command("screenshots <id>").description("List screenshots for a run or result").action((id) => {
|
|
3985
|
+
try {
|
|
3986
|
+
const run = getRun(id);
|
|
3987
|
+
if (run) {
|
|
3988
|
+
const results = getResultsByRun(run.id);
|
|
3989
|
+
let total = 0;
|
|
3990
|
+
console.log("");
|
|
3991
|
+
console.log(chalk2.bold(` Screenshots for run ${run.id.slice(0, 8)}`));
|
|
3992
|
+
console.log("");
|
|
3993
|
+
for (const result of results) {
|
|
3994
|
+
const screenshots2 = listScreenshots(result.id);
|
|
3995
|
+
if (screenshots2.length > 0) {
|
|
3996
|
+
const scenario = getScenario(result.scenarioId);
|
|
3997
|
+
const label = scenario ? `${scenario.shortId}: ${scenario.name}` : result.scenarioId.slice(0, 8);
|
|
3998
|
+
console.log(chalk2.bold(` ${label}`));
|
|
3999
|
+
for (const ss of screenshots2) {
|
|
4000
|
+
console.log(` ${chalk2.dim(String(ss.stepNumber).padStart(3, "0"))} ${ss.action} \u2014 ${chalk2.dim(ss.filePath)}`);
|
|
4001
|
+
total++;
|
|
4002
|
+
}
|
|
4003
|
+
console.log("");
|
|
4004
|
+
}
|
|
4005
|
+
}
|
|
4006
|
+
if (total === 0) {
|
|
4007
|
+
console.log(chalk2.dim(" No screenshots found."));
|
|
4008
|
+
console.log("");
|
|
4009
|
+
}
|
|
4010
|
+
return;
|
|
4011
|
+
}
|
|
4012
|
+
const screenshots = listScreenshots(id);
|
|
4013
|
+
if (screenshots.length > 0) {
|
|
4014
|
+
console.log("");
|
|
4015
|
+
console.log(chalk2.bold(` Screenshots for result ${id.slice(0, 8)}`));
|
|
4016
|
+
console.log("");
|
|
4017
|
+
for (const ss of screenshots) {
|
|
4018
|
+
console.log(` ${chalk2.dim(String(ss.stepNumber).padStart(3, "0"))} ${ss.action} \u2014 ${chalk2.dim(ss.filePath)}`);
|
|
4019
|
+
}
|
|
4020
|
+
console.log("");
|
|
4021
|
+
return;
|
|
4022
|
+
}
|
|
4023
|
+
console.error(chalk2.red(`No screenshots found for: ${id}`));
|
|
4024
|
+
process.exit(1);
|
|
4025
|
+
} catch (error) {
|
|
4026
|
+
console.error(chalk2.red(`Error: ${error instanceof Error ? error.message : String(error)}`));
|
|
4027
|
+
process.exit(1);
|
|
4028
|
+
}
|
|
4029
|
+
});
|
|
4030
|
+
program2.command("import <dir>").description("Import markdown test files as scenarios").action((dir) => {
|
|
4031
|
+
try {
|
|
4032
|
+
const absDir = resolve(dir);
|
|
4033
|
+
const files = readdirSync(absDir).filter((f) => f.endsWith(".md"));
|
|
4034
|
+
if (files.length === 0) {
|
|
4035
|
+
console.log(chalk2.dim("No .md files found in directory."));
|
|
4036
|
+
return;
|
|
4037
|
+
}
|
|
4038
|
+
let imported = 0;
|
|
4039
|
+
for (const file of files) {
|
|
4040
|
+
const content = readFileSync2(join5(absDir, file), "utf-8");
|
|
4041
|
+
const lines = content.split(`
|
|
4042
|
+
`);
|
|
4043
|
+
let name = file.replace(/\.md$/, "");
|
|
4044
|
+
const headingLine = lines.find((l) => l.startsWith("# "));
|
|
4045
|
+
if (headingLine) {
|
|
4046
|
+
name = headingLine.replace(/^#\s+/, "").trim();
|
|
4047
|
+
}
|
|
4048
|
+
const descriptionLines = [];
|
|
4049
|
+
const steps = [];
|
|
4050
|
+
for (const line of lines) {
|
|
4051
|
+
if (line.startsWith("# "))
|
|
4052
|
+
continue;
|
|
4053
|
+
const stepMatch = line.match(/^\s*\d+[\.\)]\s*(.+)/);
|
|
4054
|
+
if (stepMatch?.[1]) {
|
|
4055
|
+
steps.push(stepMatch[1].trim());
|
|
4056
|
+
} else if (line.trim()) {
|
|
4057
|
+
descriptionLines.push(line.trim());
|
|
4058
|
+
}
|
|
4059
|
+
}
|
|
4060
|
+
const scenario = createScenario({
|
|
4061
|
+
name,
|
|
4062
|
+
description: descriptionLines.join(" ") || name,
|
|
4063
|
+
steps
|
|
4064
|
+
});
|
|
4065
|
+
console.log(chalk2.green(` Imported ${chalk2.bold(scenario.shortId)}: ${scenario.name}`));
|
|
4066
|
+
imported++;
|
|
4067
|
+
}
|
|
4068
|
+
console.log("");
|
|
4069
|
+
console.log(chalk2.green(`Imported ${imported} scenario(s) from ${absDir}`));
|
|
4070
|
+
} catch (error) {
|
|
4071
|
+
console.error(chalk2.red(`Error: ${error instanceof Error ? error.message : String(error)}`));
|
|
4072
|
+
process.exit(1);
|
|
4073
|
+
}
|
|
4074
|
+
});
|
|
4075
|
+
program2.command("config").description("Show current configuration").action(() => {
|
|
4076
|
+
try {
|
|
4077
|
+
const config = loadConfig();
|
|
4078
|
+
console.log(JSON.stringify(config, null, 2));
|
|
4079
|
+
} catch (error) {
|
|
4080
|
+
console.error(chalk2.red(`Error: ${error instanceof Error ? error.message : String(error)}`));
|
|
4081
|
+
process.exit(1);
|
|
4082
|
+
}
|
|
4083
|
+
});
|
|
4084
|
+
program2.command("status").description("Show database and auth status").action(() => {
|
|
4085
|
+
try {
|
|
4086
|
+
const config = loadConfig();
|
|
4087
|
+
const hasApiKey = !!config.anthropicApiKey || !!process.env["ANTHROPIC_API_KEY"];
|
|
4088
|
+
const dbPath = join5(process.env["HOME"] ?? "~", ".testers", "testers.db");
|
|
4089
|
+
console.log("");
|
|
4090
|
+
console.log(chalk2.bold(" Open Testers Status"));
|
|
4091
|
+
console.log("");
|
|
4092
|
+
console.log(` ANTHROPIC_API_KEY: ${hasApiKey ? chalk2.green("set") : chalk2.red("not set")}`);
|
|
4093
|
+
console.log(` Database: ${dbPath}`);
|
|
4094
|
+
console.log(` Default model: ${config.defaultModel}`);
|
|
4095
|
+
console.log(` Screenshots dir: ${config.screenshots.dir}`);
|
|
4096
|
+
console.log("");
|
|
4097
|
+
} catch (error) {
|
|
4098
|
+
console.error(chalk2.red(`Error: ${error instanceof Error ? error.message : String(error)}`));
|
|
4099
|
+
process.exit(1);
|
|
4100
|
+
}
|
|
4101
|
+
});
|
|
4102
|
+
program2.command("install-browser").description("Install Playwright Chromium browser").action(async () => {
|
|
4103
|
+
try {
|
|
4104
|
+
console.log(chalk2.blue("Installing Playwright Chromium..."));
|
|
4105
|
+
await installBrowser();
|
|
4106
|
+
console.log(chalk2.green("Browser installed successfully."));
|
|
4107
|
+
} catch (error) {
|
|
4108
|
+
console.error(chalk2.red(`Error: ${error instanceof Error ? error.message : String(error)}`));
|
|
4109
|
+
process.exit(1);
|
|
4110
|
+
}
|
|
4111
|
+
});
|
|
4112
|
+
program2.parse();
|