@captainsafia/stitch 0.0.1 → 1.0.0-preview.c77f34b
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/dist/api.d.ts +170 -0
- package/dist/api.js +1837 -0
- package/dist/cli.js +4668 -0
- package/dist/mcp.js +30503 -0
- package/package.json +28 -11
- package/LICENSE +0 -21
- package/README.md +0 -263
package/dist/cli.js
ADDED
|
@@ -0,0 +1,4668 @@
|
|
|
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
|
+
var __using = (stack, value, async) => {
|
|
22
|
+
if (value != null) {
|
|
23
|
+
if (typeof value !== "object" && typeof value !== "function")
|
|
24
|
+
throw TypeError('Object expected to be assigned to "using" declaration');
|
|
25
|
+
let dispose;
|
|
26
|
+
if (async)
|
|
27
|
+
dispose = value[Symbol.asyncDispose];
|
|
28
|
+
if (dispose === undefined)
|
|
29
|
+
dispose = value[Symbol.dispose];
|
|
30
|
+
if (typeof dispose !== "function")
|
|
31
|
+
throw TypeError("Object not disposable");
|
|
32
|
+
stack.push([async, dispose, value]);
|
|
33
|
+
} else if (async) {
|
|
34
|
+
stack.push([async]);
|
|
35
|
+
}
|
|
36
|
+
return value;
|
|
37
|
+
};
|
|
38
|
+
var __callDispose = (stack, error, hasError) => {
|
|
39
|
+
let fail = (e) => error = hasError ? new SuppressedError(e, error, "An error was suppressed during disposal") : (hasError = true, e), next = (it) => {
|
|
40
|
+
while (it = stack.pop()) {
|
|
41
|
+
try {
|
|
42
|
+
var result = it[1] && it[1].call(it[2]);
|
|
43
|
+
if (it[0])
|
|
44
|
+
return Promise.resolve(result).then(next, (e) => (fail(e), next()));
|
|
45
|
+
} catch (e) {
|
|
46
|
+
fail(e);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (hasError)
|
|
50
|
+
throw error;
|
|
51
|
+
};
|
|
52
|
+
return next();
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// node_modules/commander/lib/error.js
|
|
56
|
+
var require_error = __commonJS((exports) => {
|
|
57
|
+
class CommanderError extends Error {
|
|
58
|
+
constructor(exitCode, code, message) {
|
|
59
|
+
super(message);
|
|
60
|
+
Error.captureStackTrace(this, this.constructor);
|
|
61
|
+
this.name = this.constructor.name;
|
|
62
|
+
this.code = code;
|
|
63
|
+
this.exitCode = exitCode;
|
|
64
|
+
this.nestedError = undefined;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
class InvalidArgumentError extends CommanderError {
|
|
69
|
+
constructor(message) {
|
|
70
|
+
super(1, "commander.invalidArgument", message);
|
|
71
|
+
Error.captureStackTrace(this, this.constructor);
|
|
72
|
+
this.name = this.constructor.name;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.CommanderError = CommanderError;
|
|
76
|
+
exports.InvalidArgumentError = InvalidArgumentError;
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// node_modules/commander/lib/argument.js
|
|
80
|
+
var require_argument = __commonJS((exports) => {
|
|
81
|
+
var { InvalidArgumentError } = require_error();
|
|
82
|
+
|
|
83
|
+
class Argument {
|
|
84
|
+
constructor(name, description) {
|
|
85
|
+
this.description = description || "";
|
|
86
|
+
this.variadic = false;
|
|
87
|
+
this.parseArg = undefined;
|
|
88
|
+
this.defaultValue = undefined;
|
|
89
|
+
this.defaultValueDescription = undefined;
|
|
90
|
+
this.argChoices = undefined;
|
|
91
|
+
switch (name[0]) {
|
|
92
|
+
case "<":
|
|
93
|
+
this.required = true;
|
|
94
|
+
this._name = name.slice(1, -1);
|
|
95
|
+
break;
|
|
96
|
+
case "[":
|
|
97
|
+
this.required = false;
|
|
98
|
+
this._name = name.slice(1, -1);
|
|
99
|
+
break;
|
|
100
|
+
default:
|
|
101
|
+
this.required = true;
|
|
102
|
+
this._name = name;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
if (this._name.length > 3 && this._name.slice(-3) === "...") {
|
|
106
|
+
this.variadic = true;
|
|
107
|
+
this._name = this._name.slice(0, -3);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
name() {
|
|
111
|
+
return this._name;
|
|
112
|
+
}
|
|
113
|
+
_concatValue(value, previous) {
|
|
114
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
115
|
+
return [value];
|
|
116
|
+
}
|
|
117
|
+
return previous.concat(value);
|
|
118
|
+
}
|
|
119
|
+
default(value, description) {
|
|
120
|
+
this.defaultValue = value;
|
|
121
|
+
this.defaultValueDescription = description;
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
argParser(fn) {
|
|
125
|
+
this.parseArg = fn;
|
|
126
|
+
return this;
|
|
127
|
+
}
|
|
128
|
+
choices(values) {
|
|
129
|
+
this.argChoices = values.slice();
|
|
130
|
+
this.parseArg = (arg, previous) => {
|
|
131
|
+
if (!this.argChoices.includes(arg)) {
|
|
132
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
133
|
+
}
|
|
134
|
+
if (this.variadic) {
|
|
135
|
+
return this._concatValue(arg, previous);
|
|
136
|
+
}
|
|
137
|
+
return arg;
|
|
138
|
+
};
|
|
139
|
+
return this;
|
|
140
|
+
}
|
|
141
|
+
argRequired() {
|
|
142
|
+
this.required = true;
|
|
143
|
+
return this;
|
|
144
|
+
}
|
|
145
|
+
argOptional() {
|
|
146
|
+
this.required = false;
|
|
147
|
+
return this;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function humanReadableArgName(arg) {
|
|
151
|
+
const nameOutput = arg.name() + (arg.variadic === true ? "..." : "");
|
|
152
|
+
return arg.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
|
|
153
|
+
}
|
|
154
|
+
exports.Argument = Argument;
|
|
155
|
+
exports.humanReadableArgName = humanReadableArgName;
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// node_modules/commander/lib/help.js
|
|
159
|
+
var require_help = __commonJS((exports) => {
|
|
160
|
+
var { humanReadableArgName } = require_argument();
|
|
161
|
+
|
|
162
|
+
class Help {
|
|
163
|
+
constructor() {
|
|
164
|
+
this.helpWidth = undefined;
|
|
165
|
+
this.sortSubcommands = false;
|
|
166
|
+
this.sortOptions = false;
|
|
167
|
+
this.showGlobalOptions = false;
|
|
168
|
+
}
|
|
169
|
+
visibleCommands(cmd) {
|
|
170
|
+
const visibleCommands = cmd.commands.filter((cmd2) => !cmd2._hidden);
|
|
171
|
+
const helpCommand = cmd._getHelpCommand();
|
|
172
|
+
if (helpCommand && !helpCommand._hidden) {
|
|
173
|
+
visibleCommands.push(helpCommand);
|
|
174
|
+
}
|
|
175
|
+
if (this.sortSubcommands) {
|
|
176
|
+
visibleCommands.sort((a, b) => {
|
|
177
|
+
return a.name().localeCompare(b.name());
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
return visibleCommands;
|
|
181
|
+
}
|
|
182
|
+
compareOptions(a, b) {
|
|
183
|
+
const getSortKey = (option) => {
|
|
184
|
+
return option.short ? option.short.replace(/^-/, "") : option.long.replace(/^--/, "");
|
|
185
|
+
};
|
|
186
|
+
return getSortKey(a).localeCompare(getSortKey(b));
|
|
187
|
+
}
|
|
188
|
+
visibleOptions(cmd) {
|
|
189
|
+
const visibleOptions = cmd.options.filter((option) => !option.hidden);
|
|
190
|
+
const helpOption = cmd._getHelpOption();
|
|
191
|
+
if (helpOption && !helpOption.hidden) {
|
|
192
|
+
const removeShort = helpOption.short && cmd._findOption(helpOption.short);
|
|
193
|
+
const removeLong = helpOption.long && cmd._findOption(helpOption.long);
|
|
194
|
+
if (!removeShort && !removeLong) {
|
|
195
|
+
visibleOptions.push(helpOption);
|
|
196
|
+
} else if (helpOption.long && !removeLong) {
|
|
197
|
+
visibleOptions.push(cmd.createOption(helpOption.long, helpOption.description));
|
|
198
|
+
} else if (helpOption.short && !removeShort) {
|
|
199
|
+
visibleOptions.push(cmd.createOption(helpOption.short, helpOption.description));
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (this.sortOptions) {
|
|
203
|
+
visibleOptions.sort(this.compareOptions);
|
|
204
|
+
}
|
|
205
|
+
return visibleOptions;
|
|
206
|
+
}
|
|
207
|
+
visibleGlobalOptions(cmd) {
|
|
208
|
+
if (!this.showGlobalOptions)
|
|
209
|
+
return [];
|
|
210
|
+
const globalOptions = [];
|
|
211
|
+
for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
212
|
+
const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
|
|
213
|
+
globalOptions.push(...visibleOptions);
|
|
214
|
+
}
|
|
215
|
+
if (this.sortOptions) {
|
|
216
|
+
globalOptions.sort(this.compareOptions);
|
|
217
|
+
}
|
|
218
|
+
return globalOptions;
|
|
219
|
+
}
|
|
220
|
+
visibleArguments(cmd) {
|
|
221
|
+
if (cmd._argsDescription) {
|
|
222
|
+
cmd.registeredArguments.forEach((argument) => {
|
|
223
|
+
argument.description = argument.description || cmd._argsDescription[argument.name()] || "";
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
if (cmd.registeredArguments.find((argument) => argument.description)) {
|
|
227
|
+
return cmd.registeredArguments;
|
|
228
|
+
}
|
|
229
|
+
return [];
|
|
230
|
+
}
|
|
231
|
+
subcommandTerm(cmd) {
|
|
232
|
+
const args = cmd.registeredArguments.map((arg) => humanReadableArgName(arg)).join(" ");
|
|
233
|
+
return cmd._name + (cmd._aliases[0] ? "|" + cmd._aliases[0] : "") + (cmd.options.length ? " [options]" : "") + (args ? " " + args : "");
|
|
234
|
+
}
|
|
235
|
+
optionTerm(option) {
|
|
236
|
+
return option.flags;
|
|
237
|
+
}
|
|
238
|
+
argumentTerm(argument) {
|
|
239
|
+
return argument.name();
|
|
240
|
+
}
|
|
241
|
+
longestSubcommandTermLength(cmd, helper) {
|
|
242
|
+
return helper.visibleCommands(cmd).reduce((max, command) => {
|
|
243
|
+
return Math.max(max, helper.subcommandTerm(command).length);
|
|
244
|
+
}, 0);
|
|
245
|
+
}
|
|
246
|
+
longestOptionTermLength(cmd, helper) {
|
|
247
|
+
return helper.visibleOptions(cmd).reduce((max, option) => {
|
|
248
|
+
return Math.max(max, helper.optionTerm(option).length);
|
|
249
|
+
}, 0);
|
|
250
|
+
}
|
|
251
|
+
longestGlobalOptionTermLength(cmd, helper) {
|
|
252
|
+
return helper.visibleGlobalOptions(cmd).reduce((max, option) => {
|
|
253
|
+
return Math.max(max, helper.optionTerm(option).length);
|
|
254
|
+
}, 0);
|
|
255
|
+
}
|
|
256
|
+
longestArgumentTermLength(cmd, helper) {
|
|
257
|
+
return helper.visibleArguments(cmd).reduce((max, argument) => {
|
|
258
|
+
return Math.max(max, helper.argumentTerm(argument).length);
|
|
259
|
+
}, 0);
|
|
260
|
+
}
|
|
261
|
+
commandUsage(cmd) {
|
|
262
|
+
let cmdName = cmd._name;
|
|
263
|
+
if (cmd._aliases[0]) {
|
|
264
|
+
cmdName = cmdName + "|" + cmd._aliases[0];
|
|
265
|
+
}
|
|
266
|
+
let ancestorCmdNames = "";
|
|
267
|
+
for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
268
|
+
ancestorCmdNames = ancestorCmd.name() + " " + ancestorCmdNames;
|
|
269
|
+
}
|
|
270
|
+
return ancestorCmdNames + cmdName + " " + cmd.usage();
|
|
271
|
+
}
|
|
272
|
+
commandDescription(cmd) {
|
|
273
|
+
return cmd.description();
|
|
274
|
+
}
|
|
275
|
+
subcommandDescription(cmd) {
|
|
276
|
+
return cmd.summary() || cmd.description();
|
|
277
|
+
}
|
|
278
|
+
optionDescription(option) {
|
|
279
|
+
const extraInfo = [];
|
|
280
|
+
if (option.argChoices) {
|
|
281
|
+
extraInfo.push(`choices: ${option.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
282
|
+
}
|
|
283
|
+
if (option.defaultValue !== undefined) {
|
|
284
|
+
const showDefault = option.required || option.optional || option.isBoolean() && typeof option.defaultValue === "boolean";
|
|
285
|
+
if (showDefault) {
|
|
286
|
+
extraInfo.push(`default: ${option.defaultValueDescription || JSON.stringify(option.defaultValue)}`);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
if (option.presetArg !== undefined && option.optional) {
|
|
290
|
+
extraInfo.push(`preset: ${JSON.stringify(option.presetArg)}`);
|
|
291
|
+
}
|
|
292
|
+
if (option.envVar !== undefined) {
|
|
293
|
+
extraInfo.push(`env: ${option.envVar}`);
|
|
294
|
+
}
|
|
295
|
+
if (extraInfo.length > 0) {
|
|
296
|
+
return `${option.description} (${extraInfo.join(", ")})`;
|
|
297
|
+
}
|
|
298
|
+
return option.description;
|
|
299
|
+
}
|
|
300
|
+
argumentDescription(argument) {
|
|
301
|
+
const extraInfo = [];
|
|
302
|
+
if (argument.argChoices) {
|
|
303
|
+
extraInfo.push(`choices: ${argument.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
304
|
+
}
|
|
305
|
+
if (argument.defaultValue !== undefined) {
|
|
306
|
+
extraInfo.push(`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`);
|
|
307
|
+
}
|
|
308
|
+
if (extraInfo.length > 0) {
|
|
309
|
+
const extraDescripton = `(${extraInfo.join(", ")})`;
|
|
310
|
+
if (argument.description) {
|
|
311
|
+
return `${argument.description} ${extraDescripton}`;
|
|
312
|
+
}
|
|
313
|
+
return extraDescripton;
|
|
314
|
+
}
|
|
315
|
+
return argument.description;
|
|
316
|
+
}
|
|
317
|
+
formatHelp(cmd, helper) {
|
|
318
|
+
const termWidth = helper.padWidth(cmd, helper);
|
|
319
|
+
const helpWidth = helper.helpWidth || 80;
|
|
320
|
+
const itemIndentWidth = 2;
|
|
321
|
+
const itemSeparatorWidth = 2;
|
|
322
|
+
function formatItem(term, description) {
|
|
323
|
+
if (description) {
|
|
324
|
+
const fullText = `${term.padEnd(termWidth + itemSeparatorWidth)}${description}`;
|
|
325
|
+
return helper.wrap(fullText, helpWidth - itemIndentWidth, termWidth + itemSeparatorWidth);
|
|
326
|
+
}
|
|
327
|
+
return term;
|
|
328
|
+
}
|
|
329
|
+
function formatList(textArray) {
|
|
330
|
+
return textArray.join(`
|
|
331
|
+
`).replace(/^/gm, " ".repeat(itemIndentWidth));
|
|
332
|
+
}
|
|
333
|
+
let output = [`Usage: ${helper.commandUsage(cmd)}`, ""];
|
|
334
|
+
const commandDescription = helper.commandDescription(cmd);
|
|
335
|
+
if (commandDescription.length > 0) {
|
|
336
|
+
output = output.concat([
|
|
337
|
+
helper.wrap(commandDescription, helpWidth, 0),
|
|
338
|
+
""
|
|
339
|
+
]);
|
|
340
|
+
}
|
|
341
|
+
const argumentList = helper.visibleArguments(cmd).map((argument) => {
|
|
342
|
+
return formatItem(helper.argumentTerm(argument), helper.argumentDescription(argument));
|
|
343
|
+
});
|
|
344
|
+
if (argumentList.length > 0) {
|
|
345
|
+
output = output.concat(["Arguments:", formatList(argumentList), ""]);
|
|
346
|
+
}
|
|
347
|
+
const optionList = helper.visibleOptions(cmd).map((option) => {
|
|
348
|
+
return formatItem(helper.optionTerm(option), helper.optionDescription(option));
|
|
349
|
+
});
|
|
350
|
+
if (optionList.length > 0) {
|
|
351
|
+
output = output.concat(["Options:", formatList(optionList), ""]);
|
|
352
|
+
}
|
|
353
|
+
if (this.showGlobalOptions) {
|
|
354
|
+
const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
|
|
355
|
+
return formatItem(helper.optionTerm(option), helper.optionDescription(option));
|
|
356
|
+
});
|
|
357
|
+
if (globalOptionList.length > 0) {
|
|
358
|
+
output = output.concat([
|
|
359
|
+
"Global Options:",
|
|
360
|
+
formatList(globalOptionList),
|
|
361
|
+
""
|
|
362
|
+
]);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
const commandList = helper.visibleCommands(cmd).map((cmd2) => {
|
|
366
|
+
return formatItem(helper.subcommandTerm(cmd2), helper.subcommandDescription(cmd2));
|
|
367
|
+
});
|
|
368
|
+
if (commandList.length > 0) {
|
|
369
|
+
output = output.concat(["Commands:", formatList(commandList), ""]);
|
|
370
|
+
}
|
|
371
|
+
return output.join(`
|
|
372
|
+
`);
|
|
373
|
+
}
|
|
374
|
+
padWidth(cmd, helper) {
|
|
375
|
+
return Math.max(helper.longestOptionTermLength(cmd, helper), helper.longestGlobalOptionTermLength(cmd, helper), helper.longestSubcommandTermLength(cmd, helper), helper.longestArgumentTermLength(cmd, helper));
|
|
376
|
+
}
|
|
377
|
+
wrap(str, width, indent, minColumnWidth = 40) {
|
|
378
|
+
const indents = " \\f\\t\\v\xA0\u1680\u2000-\u200A\u202F\u205F\u3000\uFEFF";
|
|
379
|
+
const manualIndent = new RegExp(`[\\n][${indents}]+`);
|
|
380
|
+
if (str.match(manualIndent))
|
|
381
|
+
return str;
|
|
382
|
+
const columnWidth = width - indent;
|
|
383
|
+
if (columnWidth < minColumnWidth)
|
|
384
|
+
return str;
|
|
385
|
+
const leadingStr = str.slice(0, indent);
|
|
386
|
+
const columnText = str.slice(indent).replace(`\r
|
|
387
|
+
`, `
|
|
388
|
+
`);
|
|
389
|
+
const indentString = " ".repeat(indent);
|
|
390
|
+
const zeroWidthSpace = "\u200B";
|
|
391
|
+
const breaks = `\\s${zeroWidthSpace}`;
|
|
392
|
+
const regex = new RegExp(`
|
|
393
|
+
|.{1,${columnWidth - 1}}([${breaks}]|$)|[^${breaks}]+?([${breaks}]|$)`, "g");
|
|
394
|
+
const lines = columnText.match(regex) || [];
|
|
395
|
+
return leadingStr + lines.map((line, i) => {
|
|
396
|
+
if (line === `
|
|
397
|
+
`)
|
|
398
|
+
return "";
|
|
399
|
+
return (i > 0 ? indentString : "") + line.trimEnd();
|
|
400
|
+
}).join(`
|
|
401
|
+
`);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
exports.Help = Help;
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
// node_modules/commander/lib/option.js
|
|
408
|
+
var require_option = __commonJS((exports) => {
|
|
409
|
+
var { InvalidArgumentError } = require_error();
|
|
410
|
+
|
|
411
|
+
class Option {
|
|
412
|
+
constructor(flags, description) {
|
|
413
|
+
this.flags = flags;
|
|
414
|
+
this.description = description || "";
|
|
415
|
+
this.required = flags.includes("<");
|
|
416
|
+
this.optional = flags.includes("[");
|
|
417
|
+
this.variadic = /\w\.\.\.[>\]]$/.test(flags);
|
|
418
|
+
this.mandatory = false;
|
|
419
|
+
const optionFlags = splitOptionFlags(flags);
|
|
420
|
+
this.short = optionFlags.shortFlag;
|
|
421
|
+
this.long = optionFlags.longFlag;
|
|
422
|
+
this.negate = false;
|
|
423
|
+
if (this.long) {
|
|
424
|
+
this.negate = this.long.startsWith("--no-");
|
|
425
|
+
}
|
|
426
|
+
this.defaultValue = undefined;
|
|
427
|
+
this.defaultValueDescription = undefined;
|
|
428
|
+
this.presetArg = undefined;
|
|
429
|
+
this.envVar = undefined;
|
|
430
|
+
this.parseArg = undefined;
|
|
431
|
+
this.hidden = false;
|
|
432
|
+
this.argChoices = undefined;
|
|
433
|
+
this.conflictsWith = [];
|
|
434
|
+
this.implied = undefined;
|
|
435
|
+
}
|
|
436
|
+
default(value, description) {
|
|
437
|
+
this.defaultValue = value;
|
|
438
|
+
this.defaultValueDescription = description;
|
|
439
|
+
return this;
|
|
440
|
+
}
|
|
441
|
+
preset(arg) {
|
|
442
|
+
this.presetArg = arg;
|
|
443
|
+
return this;
|
|
444
|
+
}
|
|
445
|
+
conflicts(names) {
|
|
446
|
+
this.conflictsWith = this.conflictsWith.concat(names);
|
|
447
|
+
return this;
|
|
448
|
+
}
|
|
449
|
+
implies(impliedOptionValues) {
|
|
450
|
+
let newImplied = impliedOptionValues;
|
|
451
|
+
if (typeof impliedOptionValues === "string") {
|
|
452
|
+
newImplied = { [impliedOptionValues]: true };
|
|
453
|
+
}
|
|
454
|
+
this.implied = Object.assign(this.implied || {}, newImplied);
|
|
455
|
+
return this;
|
|
456
|
+
}
|
|
457
|
+
env(name) {
|
|
458
|
+
this.envVar = name;
|
|
459
|
+
return this;
|
|
460
|
+
}
|
|
461
|
+
argParser(fn) {
|
|
462
|
+
this.parseArg = fn;
|
|
463
|
+
return this;
|
|
464
|
+
}
|
|
465
|
+
makeOptionMandatory(mandatory = true) {
|
|
466
|
+
this.mandatory = !!mandatory;
|
|
467
|
+
return this;
|
|
468
|
+
}
|
|
469
|
+
hideHelp(hide = true) {
|
|
470
|
+
this.hidden = !!hide;
|
|
471
|
+
return this;
|
|
472
|
+
}
|
|
473
|
+
_concatValue(value, previous) {
|
|
474
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
475
|
+
return [value];
|
|
476
|
+
}
|
|
477
|
+
return previous.concat(value);
|
|
478
|
+
}
|
|
479
|
+
choices(values) {
|
|
480
|
+
this.argChoices = values.slice();
|
|
481
|
+
this.parseArg = (arg, previous) => {
|
|
482
|
+
if (!this.argChoices.includes(arg)) {
|
|
483
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
484
|
+
}
|
|
485
|
+
if (this.variadic) {
|
|
486
|
+
return this._concatValue(arg, previous);
|
|
487
|
+
}
|
|
488
|
+
return arg;
|
|
489
|
+
};
|
|
490
|
+
return this;
|
|
491
|
+
}
|
|
492
|
+
name() {
|
|
493
|
+
if (this.long) {
|
|
494
|
+
return this.long.replace(/^--/, "");
|
|
495
|
+
}
|
|
496
|
+
return this.short.replace(/^-/, "");
|
|
497
|
+
}
|
|
498
|
+
attributeName() {
|
|
499
|
+
return camelcase(this.name().replace(/^no-/, ""));
|
|
500
|
+
}
|
|
501
|
+
is(arg) {
|
|
502
|
+
return this.short === arg || this.long === arg;
|
|
503
|
+
}
|
|
504
|
+
isBoolean() {
|
|
505
|
+
return !this.required && !this.optional && !this.negate;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
class DualOptions {
|
|
510
|
+
constructor(options) {
|
|
511
|
+
this.positiveOptions = new Map;
|
|
512
|
+
this.negativeOptions = new Map;
|
|
513
|
+
this.dualOptions = new Set;
|
|
514
|
+
options.forEach((option) => {
|
|
515
|
+
if (option.negate) {
|
|
516
|
+
this.negativeOptions.set(option.attributeName(), option);
|
|
517
|
+
} else {
|
|
518
|
+
this.positiveOptions.set(option.attributeName(), option);
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
this.negativeOptions.forEach((value, key) => {
|
|
522
|
+
if (this.positiveOptions.has(key)) {
|
|
523
|
+
this.dualOptions.add(key);
|
|
524
|
+
}
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
valueFromOption(value, option) {
|
|
528
|
+
const optionKey = option.attributeName();
|
|
529
|
+
if (!this.dualOptions.has(optionKey))
|
|
530
|
+
return true;
|
|
531
|
+
const preset = this.negativeOptions.get(optionKey).presetArg;
|
|
532
|
+
const negativeValue = preset !== undefined ? preset : false;
|
|
533
|
+
return option.negate === (negativeValue === value);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
function camelcase(str) {
|
|
537
|
+
return str.split("-").reduce((str2, word) => {
|
|
538
|
+
return str2 + word[0].toUpperCase() + word.slice(1);
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
function splitOptionFlags(flags) {
|
|
542
|
+
let shortFlag;
|
|
543
|
+
let longFlag;
|
|
544
|
+
const flagParts = flags.split(/[ |,]+/);
|
|
545
|
+
if (flagParts.length > 1 && !/^[[<]/.test(flagParts[1]))
|
|
546
|
+
shortFlag = flagParts.shift();
|
|
547
|
+
longFlag = flagParts.shift();
|
|
548
|
+
if (!shortFlag && /^-[^-]$/.test(longFlag)) {
|
|
549
|
+
shortFlag = longFlag;
|
|
550
|
+
longFlag = undefined;
|
|
551
|
+
}
|
|
552
|
+
return { shortFlag, longFlag };
|
|
553
|
+
}
|
|
554
|
+
exports.Option = Option;
|
|
555
|
+
exports.DualOptions = DualOptions;
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
// node_modules/commander/lib/suggestSimilar.js
|
|
559
|
+
var require_suggestSimilar = __commonJS((exports) => {
|
|
560
|
+
var maxDistance = 3;
|
|
561
|
+
function editDistance(a, b) {
|
|
562
|
+
if (Math.abs(a.length - b.length) > maxDistance)
|
|
563
|
+
return Math.max(a.length, b.length);
|
|
564
|
+
const d = [];
|
|
565
|
+
for (let i = 0;i <= a.length; i++) {
|
|
566
|
+
d[i] = [i];
|
|
567
|
+
}
|
|
568
|
+
for (let j = 0;j <= b.length; j++) {
|
|
569
|
+
d[0][j] = j;
|
|
570
|
+
}
|
|
571
|
+
for (let j = 1;j <= b.length; j++) {
|
|
572
|
+
for (let i = 1;i <= a.length; i++) {
|
|
573
|
+
let cost = 1;
|
|
574
|
+
if (a[i - 1] === b[j - 1]) {
|
|
575
|
+
cost = 0;
|
|
576
|
+
} else {
|
|
577
|
+
cost = 1;
|
|
578
|
+
}
|
|
579
|
+
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
|
|
580
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
581
|
+
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
return d[a.length][b.length];
|
|
586
|
+
}
|
|
587
|
+
function suggestSimilar(word, candidates) {
|
|
588
|
+
if (!candidates || candidates.length === 0)
|
|
589
|
+
return "";
|
|
590
|
+
candidates = Array.from(new Set(candidates));
|
|
591
|
+
const searchingOptions = word.startsWith("--");
|
|
592
|
+
if (searchingOptions) {
|
|
593
|
+
word = word.slice(2);
|
|
594
|
+
candidates = candidates.map((candidate) => candidate.slice(2));
|
|
595
|
+
}
|
|
596
|
+
let similar = [];
|
|
597
|
+
let bestDistance = maxDistance;
|
|
598
|
+
const minSimilarity = 0.4;
|
|
599
|
+
candidates.forEach((candidate) => {
|
|
600
|
+
if (candidate.length <= 1)
|
|
601
|
+
return;
|
|
602
|
+
const distance = editDistance(word, candidate);
|
|
603
|
+
const length = Math.max(word.length, candidate.length);
|
|
604
|
+
const similarity = (length - distance) / length;
|
|
605
|
+
if (similarity > minSimilarity) {
|
|
606
|
+
if (distance < bestDistance) {
|
|
607
|
+
bestDistance = distance;
|
|
608
|
+
similar = [candidate];
|
|
609
|
+
} else if (distance === bestDistance) {
|
|
610
|
+
similar.push(candidate);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
});
|
|
614
|
+
similar.sort((a, b) => a.localeCompare(b));
|
|
615
|
+
if (searchingOptions) {
|
|
616
|
+
similar = similar.map((candidate) => `--${candidate}`);
|
|
617
|
+
}
|
|
618
|
+
if (similar.length > 1) {
|
|
619
|
+
return `
|
|
620
|
+
(Did you mean one of ${similar.join(", ")}?)`;
|
|
621
|
+
}
|
|
622
|
+
if (similar.length === 1) {
|
|
623
|
+
return `
|
|
624
|
+
(Did you mean ${similar[0]}?)`;
|
|
625
|
+
}
|
|
626
|
+
return "";
|
|
627
|
+
}
|
|
628
|
+
exports.suggestSimilar = suggestSimilar;
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
// node_modules/commander/lib/command.js
|
|
632
|
+
var require_command = __commonJS((exports) => {
|
|
633
|
+
var EventEmitter = __require("events").EventEmitter;
|
|
634
|
+
var childProcess = __require("child_process");
|
|
635
|
+
var path = __require("path");
|
|
636
|
+
var fs = __require("fs");
|
|
637
|
+
var process2 = __require("process");
|
|
638
|
+
var { Argument, humanReadableArgName } = require_argument();
|
|
639
|
+
var { CommanderError } = require_error();
|
|
640
|
+
var { Help } = require_help();
|
|
641
|
+
var { Option, DualOptions } = require_option();
|
|
642
|
+
var { suggestSimilar } = require_suggestSimilar();
|
|
643
|
+
|
|
644
|
+
class Command extends EventEmitter {
|
|
645
|
+
constructor(name) {
|
|
646
|
+
super();
|
|
647
|
+
this.commands = [];
|
|
648
|
+
this.options = [];
|
|
649
|
+
this.parent = null;
|
|
650
|
+
this._allowUnknownOption = false;
|
|
651
|
+
this._allowExcessArguments = true;
|
|
652
|
+
this.registeredArguments = [];
|
|
653
|
+
this._args = this.registeredArguments;
|
|
654
|
+
this.args = [];
|
|
655
|
+
this.rawArgs = [];
|
|
656
|
+
this.processedArgs = [];
|
|
657
|
+
this._scriptPath = null;
|
|
658
|
+
this._name = name || "";
|
|
659
|
+
this._optionValues = {};
|
|
660
|
+
this._optionValueSources = {};
|
|
661
|
+
this._storeOptionsAsProperties = false;
|
|
662
|
+
this._actionHandler = null;
|
|
663
|
+
this._executableHandler = false;
|
|
664
|
+
this._executableFile = null;
|
|
665
|
+
this._executableDir = null;
|
|
666
|
+
this._defaultCommandName = null;
|
|
667
|
+
this._exitCallback = null;
|
|
668
|
+
this._aliases = [];
|
|
669
|
+
this._combineFlagAndOptionalValue = true;
|
|
670
|
+
this._description = "";
|
|
671
|
+
this._summary = "";
|
|
672
|
+
this._argsDescription = undefined;
|
|
673
|
+
this._enablePositionalOptions = false;
|
|
674
|
+
this._passThroughOptions = false;
|
|
675
|
+
this._lifeCycleHooks = {};
|
|
676
|
+
this._showHelpAfterError = false;
|
|
677
|
+
this._showSuggestionAfterError = true;
|
|
678
|
+
this._outputConfiguration = {
|
|
679
|
+
writeOut: (str) => process2.stdout.write(str),
|
|
680
|
+
writeErr: (str) => process2.stderr.write(str),
|
|
681
|
+
getOutHelpWidth: () => process2.stdout.isTTY ? process2.stdout.columns : undefined,
|
|
682
|
+
getErrHelpWidth: () => process2.stderr.isTTY ? process2.stderr.columns : undefined,
|
|
683
|
+
outputError: (str, write) => write(str)
|
|
684
|
+
};
|
|
685
|
+
this._hidden = false;
|
|
686
|
+
this._helpOption = undefined;
|
|
687
|
+
this._addImplicitHelpCommand = undefined;
|
|
688
|
+
this._helpCommand = undefined;
|
|
689
|
+
this._helpConfiguration = {};
|
|
690
|
+
}
|
|
691
|
+
copyInheritedSettings(sourceCommand) {
|
|
692
|
+
this._outputConfiguration = sourceCommand._outputConfiguration;
|
|
693
|
+
this._helpOption = sourceCommand._helpOption;
|
|
694
|
+
this._helpCommand = sourceCommand._helpCommand;
|
|
695
|
+
this._helpConfiguration = sourceCommand._helpConfiguration;
|
|
696
|
+
this._exitCallback = sourceCommand._exitCallback;
|
|
697
|
+
this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
|
|
698
|
+
this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
|
|
699
|
+
this._allowExcessArguments = sourceCommand._allowExcessArguments;
|
|
700
|
+
this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
|
|
701
|
+
this._showHelpAfterError = sourceCommand._showHelpAfterError;
|
|
702
|
+
this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;
|
|
703
|
+
return this;
|
|
704
|
+
}
|
|
705
|
+
_getCommandAndAncestors() {
|
|
706
|
+
const result = [];
|
|
707
|
+
for (let command = this;command; command = command.parent) {
|
|
708
|
+
result.push(command);
|
|
709
|
+
}
|
|
710
|
+
return result;
|
|
711
|
+
}
|
|
712
|
+
command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
|
|
713
|
+
let desc = actionOptsOrExecDesc;
|
|
714
|
+
let opts = execOpts;
|
|
715
|
+
if (typeof desc === "object" && desc !== null) {
|
|
716
|
+
opts = desc;
|
|
717
|
+
desc = null;
|
|
718
|
+
}
|
|
719
|
+
opts = opts || {};
|
|
720
|
+
const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
|
|
721
|
+
const cmd = this.createCommand(name);
|
|
722
|
+
if (desc) {
|
|
723
|
+
cmd.description(desc);
|
|
724
|
+
cmd._executableHandler = true;
|
|
725
|
+
}
|
|
726
|
+
if (opts.isDefault)
|
|
727
|
+
this._defaultCommandName = cmd._name;
|
|
728
|
+
cmd._hidden = !!(opts.noHelp || opts.hidden);
|
|
729
|
+
cmd._executableFile = opts.executableFile || null;
|
|
730
|
+
if (args)
|
|
731
|
+
cmd.arguments(args);
|
|
732
|
+
this._registerCommand(cmd);
|
|
733
|
+
cmd.parent = this;
|
|
734
|
+
cmd.copyInheritedSettings(this);
|
|
735
|
+
if (desc)
|
|
736
|
+
return this;
|
|
737
|
+
return cmd;
|
|
738
|
+
}
|
|
739
|
+
createCommand(name) {
|
|
740
|
+
return new Command(name);
|
|
741
|
+
}
|
|
742
|
+
createHelp() {
|
|
743
|
+
return Object.assign(new Help, this.configureHelp());
|
|
744
|
+
}
|
|
745
|
+
configureHelp(configuration) {
|
|
746
|
+
if (configuration === undefined)
|
|
747
|
+
return this._helpConfiguration;
|
|
748
|
+
this._helpConfiguration = configuration;
|
|
749
|
+
return this;
|
|
750
|
+
}
|
|
751
|
+
configureOutput(configuration) {
|
|
752
|
+
if (configuration === undefined)
|
|
753
|
+
return this._outputConfiguration;
|
|
754
|
+
Object.assign(this._outputConfiguration, configuration);
|
|
755
|
+
return this;
|
|
756
|
+
}
|
|
757
|
+
showHelpAfterError(displayHelp = true) {
|
|
758
|
+
if (typeof displayHelp !== "string")
|
|
759
|
+
displayHelp = !!displayHelp;
|
|
760
|
+
this._showHelpAfterError = displayHelp;
|
|
761
|
+
return this;
|
|
762
|
+
}
|
|
763
|
+
showSuggestionAfterError(displaySuggestion = true) {
|
|
764
|
+
this._showSuggestionAfterError = !!displaySuggestion;
|
|
765
|
+
return this;
|
|
766
|
+
}
|
|
767
|
+
addCommand(cmd, opts) {
|
|
768
|
+
if (!cmd._name) {
|
|
769
|
+
throw new Error(`Command passed to .addCommand() must have a name
|
|
770
|
+
- specify the name in Command constructor or using .name()`);
|
|
771
|
+
}
|
|
772
|
+
opts = opts || {};
|
|
773
|
+
if (opts.isDefault)
|
|
774
|
+
this._defaultCommandName = cmd._name;
|
|
775
|
+
if (opts.noHelp || opts.hidden)
|
|
776
|
+
cmd._hidden = true;
|
|
777
|
+
this._registerCommand(cmd);
|
|
778
|
+
cmd.parent = this;
|
|
779
|
+
cmd._checkForBrokenPassThrough();
|
|
780
|
+
return this;
|
|
781
|
+
}
|
|
782
|
+
createArgument(name, description) {
|
|
783
|
+
return new Argument(name, description);
|
|
784
|
+
}
|
|
785
|
+
argument(name, description, fn, defaultValue) {
|
|
786
|
+
const argument = this.createArgument(name, description);
|
|
787
|
+
if (typeof fn === "function") {
|
|
788
|
+
argument.default(defaultValue).argParser(fn);
|
|
789
|
+
} else {
|
|
790
|
+
argument.default(fn);
|
|
791
|
+
}
|
|
792
|
+
this.addArgument(argument);
|
|
793
|
+
return this;
|
|
794
|
+
}
|
|
795
|
+
arguments(names) {
|
|
796
|
+
names.trim().split(/ +/).forEach((detail) => {
|
|
797
|
+
this.argument(detail);
|
|
798
|
+
});
|
|
799
|
+
return this;
|
|
800
|
+
}
|
|
801
|
+
addArgument(argument) {
|
|
802
|
+
const previousArgument = this.registeredArguments.slice(-1)[0];
|
|
803
|
+
if (previousArgument && previousArgument.variadic) {
|
|
804
|
+
throw new Error(`only the last argument can be variadic '${previousArgument.name()}'`);
|
|
805
|
+
}
|
|
806
|
+
if (argument.required && argument.defaultValue !== undefined && argument.parseArg === undefined) {
|
|
807
|
+
throw new Error(`a default value for a required argument is never used: '${argument.name()}'`);
|
|
808
|
+
}
|
|
809
|
+
this.registeredArguments.push(argument);
|
|
810
|
+
return this;
|
|
811
|
+
}
|
|
812
|
+
helpCommand(enableOrNameAndArgs, description) {
|
|
813
|
+
if (typeof enableOrNameAndArgs === "boolean") {
|
|
814
|
+
this._addImplicitHelpCommand = enableOrNameAndArgs;
|
|
815
|
+
return this;
|
|
816
|
+
}
|
|
817
|
+
enableOrNameAndArgs = enableOrNameAndArgs ?? "help [command]";
|
|
818
|
+
const [, helpName, helpArgs] = enableOrNameAndArgs.match(/([^ ]+) *(.*)/);
|
|
819
|
+
const helpDescription = description ?? "display help for command";
|
|
820
|
+
const helpCommand = this.createCommand(helpName);
|
|
821
|
+
helpCommand.helpOption(false);
|
|
822
|
+
if (helpArgs)
|
|
823
|
+
helpCommand.arguments(helpArgs);
|
|
824
|
+
if (helpDescription)
|
|
825
|
+
helpCommand.description(helpDescription);
|
|
826
|
+
this._addImplicitHelpCommand = true;
|
|
827
|
+
this._helpCommand = helpCommand;
|
|
828
|
+
return this;
|
|
829
|
+
}
|
|
830
|
+
addHelpCommand(helpCommand, deprecatedDescription) {
|
|
831
|
+
if (typeof helpCommand !== "object") {
|
|
832
|
+
this.helpCommand(helpCommand, deprecatedDescription);
|
|
833
|
+
return this;
|
|
834
|
+
}
|
|
835
|
+
this._addImplicitHelpCommand = true;
|
|
836
|
+
this._helpCommand = helpCommand;
|
|
837
|
+
return this;
|
|
838
|
+
}
|
|
839
|
+
_getHelpCommand() {
|
|
840
|
+
const hasImplicitHelpCommand = this._addImplicitHelpCommand ?? (this.commands.length && !this._actionHandler && !this._findCommand("help"));
|
|
841
|
+
if (hasImplicitHelpCommand) {
|
|
842
|
+
if (this._helpCommand === undefined) {
|
|
843
|
+
this.helpCommand(undefined, undefined);
|
|
844
|
+
}
|
|
845
|
+
return this._helpCommand;
|
|
846
|
+
}
|
|
847
|
+
return null;
|
|
848
|
+
}
|
|
849
|
+
hook(event, listener) {
|
|
850
|
+
const allowedValues = ["preSubcommand", "preAction", "postAction"];
|
|
851
|
+
if (!allowedValues.includes(event)) {
|
|
852
|
+
throw new Error(`Unexpected value for event passed to hook : '${event}'.
|
|
853
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
854
|
+
}
|
|
855
|
+
if (this._lifeCycleHooks[event]) {
|
|
856
|
+
this._lifeCycleHooks[event].push(listener);
|
|
857
|
+
} else {
|
|
858
|
+
this._lifeCycleHooks[event] = [listener];
|
|
859
|
+
}
|
|
860
|
+
return this;
|
|
861
|
+
}
|
|
862
|
+
exitOverride(fn) {
|
|
863
|
+
if (fn) {
|
|
864
|
+
this._exitCallback = fn;
|
|
865
|
+
} else {
|
|
866
|
+
this._exitCallback = (err) => {
|
|
867
|
+
if (err.code !== "commander.executeSubCommandAsync") {
|
|
868
|
+
throw err;
|
|
869
|
+
} else {}
|
|
870
|
+
};
|
|
871
|
+
}
|
|
872
|
+
return this;
|
|
873
|
+
}
|
|
874
|
+
_exit(exitCode, code, message) {
|
|
875
|
+
if (this._exitCallback) {
|
|
876
|
+
this._exitCallback(new CommanderError(exitCode, code, message));
|
|
877
|
+
}
|
|
878
|
+
process2.exit(exitCode);
|
|
879
|
+
}
|
|
880
|
+
action(fn) {
|
|
881
|
+
const listener = (args) => {
|
|
882
|
+
const expectedArgsCount = this.registeredArguments.length;
|
|
883
|
+
const actionArgs = args.slice(0, expectedArgsCount);
|
|
884
|
+
if (this._storeOptionsAsProperties) {
|
|
885
|
+
actionArgs[expectedArgsCount] = this;
|
|
886
|
+
} else {
|
|
887
|
+
actionArgs[expectedArgsCount] = this.opts();
|
|
888
|
+
}
|
|
889
|
+
actionArgs.push(this);
|
|
890
|
+
return fn.apply(this, actionArgs);
|
|
891
|
+
};
|
|
892
|
+
this._actionHandler = listener;
|
|
893
|
+
return this;
|
|
894
|
+
}
|
|
895
|
+
createOption(flags, description) {
|
|
896
|
+
return new Option(flags, description);
|
|
897
|
+
}
|
|
898
|
+
_callParseArg(target, value, previous, invalidArgumentMessage) {
|
|
899
|
+
try {
|
|
900
|
+
return target.parseArg(value, previous);
|
|
901
|
+
} catch (err) {
|
|
902
|
+
if (err.code === "commander.invalidArgument") {
|
|
903
|
+
const message = `${invalidArgumentMessage} ${err.message}`;
|
|
904
|
+
this.error(message, { exitCode: err.exitCode, code: err.code });
|
|
905
|
+
}
|
|
906
|
+
throw err;
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
_registerOption(option) {
|
|
910
|
+
const matchingOption = option.short && this._findOption(option.short) || option.long && this._findOption(option.long);
|
|
911
|
+
if (matchingOption) {
|
|
912
|
+
const matchingFlag = option.long && this._findOption(option.long) ? option.long : option.short;
|
|
913
|
+
throw new Error(`Cannot add option '${option.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'
|
|
914
|
+
- already used by option '${matchingOption.flags}'`);
|
|
915
|
+
}
|
|
916
|
+
this.options.push(option);
|
|
917
|
+
}
|
|
918
|
+
_registerCommand(command) {
|
|
919
|
+
const knownBy = (cmd) => {
|
|
920
|
+
return [cmd.name()].concat(cmd.aliases());
|
|
921
|
+
};
|
|
922
|
+
const alreadyUsed = knownBy(command).find((name) => this._findCommand(name));
|
|
923
|
+
if (alreadyUsed) {
|
|
924
|
+
const existingCmd = knownBy(this._findCommand(alreadyUsed)).join("|");
|
|
925
|
+
const newCmd = knownBy(command).join("|");
|
|
926
|
+
throw new Error(`cannot add command '${newCmd}' as already have command '${existingCmd}'`);
|
|
927
|
+
}
|
|
928
|
+
this.commands.push(command);
|
|
929
|
+
}
|
|
930
|
+
addOption(option) {
|
|
931
|
+
this._registerOption(option);
|
|
932
|
+
const oname = option.name();
|
|
933
|
+
const name = option.attributeName();
|
|
934
|
+
if (option.negate) {
|
|
935
|
+
const positiveLongFlag = option.long.replace(/^--no-/, "--");
|
|
936
|
+
if (!this._findOption(positiveLongFlag)) {
|
|
937
|
+
this.setOptionValueWithSource(name, option.defaultValue === undefined ? true : option.defaultValue, "default");
|
|
938
|
+
}
|
|
939
|
+
} else if (option.defaultValue !== undefined) {
|
|
940
|
+
this.setOptionValueWithSource(name, option.defaultValue, "default");
|
|
941
|
+
}
|
|
942
|
+
const handleOptionValue = (val, invalidValueMessage, valueSource) => {
|
|
943
|
+
if (val == null && option.presetArg !== undefined) {
|
|
944
|
+
val = option.presetArg;
|
|
945
|
+
}
|
|
946
|
+
const oldValue = this.getOptionValue(name);
|
|
947
|
+
if (val !== null && option.parseArg) {
|
|
948
|
+
val = this._callParseArg(option, val, oldValue, invalidValueMessage);
|
|
949
|
+
} else if (val !== null && option.variadic) {
|
|
950
|
+
val = option._concatValue(val, oldValue);
|
|
951
|
+
}
|
|
952
|
+
if (val == null) {
|
|
953
|
+
if (option.negate) {
|
|
954
|
+
val = false;
|
|
955
|
+
} else if (option.isBoolean() || option.optional) {
|
|
956
|
+
val = true;
|
|
957
|
+
} else {
|
|
958
|
+
val = "";
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
this.setOptionValueWithSource(name, val, valueSource);
|
|
962
|
+
};
|
|
963
|
+
this.on("option:" + oname, (val) => {
|
|
964
|
+
const invalidValueMessage = `error: option '${option.flags}' argument '${val}' is invalid.`;
|
|
965
|
+
handleOptionValue(val, invalidValueMessage, "cli");
|
|
966
|
+
});
|
|
967
|
+
if (option.envVar) {
|
|
968
|
+
this.on("optionEnv:" + oname, (val) => {
|
|
969
|
+
const invalidValueMessage = `error: option '${option.flags}' value '${val}' from env '${option.envVar}' is invalid.`;
|
|
970
|
+
handleOptionValue(val, invalidValueMessage, "env");
|
|
971
|
+
});
|
|
972
|
+
}
|
|
973
|
+
return this;
|
|
974
|
+
}
|
|
975
|
+
_optionEx(config, flags, description, fn, defaultValue) {
|
|
976
|
+
if (typeof flags === "object" && flags instanceof Option) {
|
|
977
|
+
throw new Error("To add an Option object use addOption() instead of option() or requiredOption()");
|
|
978
|
+
}
|
|
979
|
+
const option = this.createOption(flags, description);
|
|
980
|
+
option.makeOptionMandatory(!!config.mandatory);
|
|
981
|
+
if (typeof fn === "function") {
|
|
982
|
+
option.default(defaultValue).argParser(fn);
|
|
983
|
+
} else if (fn instanceof RegExp) {
|
|
984
|
+
const regex = fn;
|
|
985
|
+
fn = (val, def) => {
|
|
986
|
+
const m = regex.exec(val);
|
|
987
|
+
return m ? m[0] : def;
|
|
988
|
+
};
|
|
989
|
+
option.default(defaultValue).argParser(fn);
|
|
990
|
+
} else {
|
|
991
|
+
option.default(fn);
|
|
992
|
+
}
|
|
993
|
+
return this.addOption(option);
|
|
994
|
+
}
|
|
995
|
+
option(flags, description, parseArg, defaultValue) {
|
|
996
|
+
return this._optionEx({}, flags, description, parseArg, defaultValue);
|
|
997
|
+
}
|
|
998
|
+
requiredOption(flags, description, parseArg, defaultValue) {
|
|
999
|
+
return this._optionEx({ mandatory: true }, flags, description, parseArg, defaultValue);
|
|
1000
|
+
}
|
|
1001
|
+
combineFlagAndOptionalValue(combine = true) {
|
|
1002
|
+
this._combineFlagAndOptionalValue = !!combine;
|
|
1003
|
+
return this;
|
|
1004
|
+
}
|
|
1005
|
+
allowUnknownOption(allowUnknown = true) {
|
|
1006
|
+
this._allowUnknownOption = !!allowUnknown;
|
|
1007
|
+
return this;
|
|
1008
|
+
}
|
|
1009
|
+
allowExcessArguments(allowExcess = true) {
|
|
1010
|
+
this._allowExcessArguments = !!allowExcess;
|
|
1011
|
+
return this;
|
|
1012
|
+
}
|
|
1013
|
+
enablePositionalOptions(positional = true) {
|
|
1014
|
+
this._enablePositionalOptions = !!positional;
|
|
1015
|
+
return this;
|
|
1016
|
+
}
|
|
1017
|
+
passThroughOptions(passThrough = true) {
|
|
1018
|
+
this._passThroughOptions = !!passThrough;
|
|
1019
|
+
this._checkForBrokenPassThrough();
|
|
1020
|
+
return this;
|
|
1021
|
+
}
|
|
1022
|
+
_checkForBrokenPassThrough() {
|
|
1023
|
+
if (this.parent && this._passThroughOptions && !this.parent._enablePositionalOptions) {
|
|
1024
|
+
throw new Error(`passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`);
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
storeOptionsAsProperties(storeAsProperties = true) {
|
|
1028
|
+
if (this.options.length) {
|
|
1029
|
+
throw new Error("call .storeOptionsAsProperties() before adding options");
|
|
1030
|
+
}
|
|
1031
|
+
if (Object.keys(this._optionValues).length) {
|
|
1032
|
+
throw new Error("call .storeOptionsAsProperties() before setting option values");
|
|
1033
|
+
}
|
|
1034
|
+
this._storeOptionsAsProperties = !!storeAsProperties;
|
|
1035
|
+
return this;
|
|
1036
|
+
}
|
|
1037
|
+
getOptionValue(key) {
|
|
1038
|
+
if (this._storeOptionsAsProperties) {
|
|
1039
|
+
return this[key];
|
|
1040
|
+
}
|
|
1041
|
+
return this._optionValues[key];
|
|
1042
|
+
}
|
|
1043
|
+
setOptionValue(key, value) {
|
|
1044
|
+
return this.setOptionValueWithSource(key, value, undefined);
|
|
1045
|
+
}
|
|
1046
|
+
setOptionValueWithSource(key, value, source) {
|
|
1047
|
+
if (this._storeOptionsAsProperties) {
|
|
1048
|
+
this[key] = value;
|
|
1049
|
+
} else {
|
|
1050
|
+
this._optionValues[key] = value;
|
|
1051
|
+
}
|
|
1052
|
+
this._optionValueSources[key] = source;
|
|
1053
|
+
return this;
|
|
1054
|
+
}
|
|
1055
|
+
getOptionValueSource(key) {
|
|
1056
|
+
return this._optionValueSources[key];
|
|
1057
|
+
}
|
|
1058
|
+
getOptionValueSourceWithGlobals(key) {
|
|
1059
|
+
let source;
|
|
1060
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1061
|
+
if (cmd.getOptionValueSource(key) !== undefined) {
|
|
1062
|
+
source = cmd.getOptionValueSource(key);
|
|
1063
|
+
}
|
|
1064
|
+
});
|
|
1065
|
+
return source;
|
|
1066
|
+
}
|
|
1067
|
+
_prepareUserArgs(argv, parseOptions) {
|
|
1068
|
+
if (argv !== undefined && !Array.isArray(argv)) {
|
|
1069
|
+
throw new Error("first parameter to parse must be array or undefined");
|
|
1070
|
+
}
|
|
1071
|
+
parseOptions = parseOptions || {};
|
|
1072
|
+
if (argv === undefined && parseOptions.from === undefined) {
|
|
1073
|
+
if (process2.versions?.electron) {
|
|
1074
|
+
parseOptions.from = "electron";
|
|
1075
|
+
}
|
|
1076
|
+
const execArgv = process2.execArgv ?? [];
|
|
1077
|
+
if (execArgv.includes("-e") || execArgv.includes("--eval") || execArgv.includes("-p") || execArgv.includes("--print")) {
|
|
1078
|
+
parseOptions.from = "eval";
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
if (argv === undefined) {
|
|
1082
|
+
argv = process2.argv;
|
|
1083
|
+
}
|
|
1084
|
+
this.rawArgs = argv.slice();
|
|
1085
|
+
let userArgs;
|
|
1086
|
+
switch (parseOptions.from) {
|
|
1087
|
+
case undefined:
|
|
1088
|
+
case "node":
|
|
1089
|
+
this._scriptPath = argv[1];
|
|
1090
|
+
userArgs = argv.slice(2);
|
|
1091
|
+
break;
|
|
1092
|
+
case "electron":
|
|
1093
|
+
if (process2.defaultApp) {
|
|
1094
|
+
this._scriptPath = argv[1];
|
|
1095
|
+
userArgs = argv.slice(2);
|
|
1096
|
+
} else {
|
|
1097
|
+
userArgs = argv.slice(1);
|
|
1098
|
+
}
|
|
1099
|
+
break;
|
|
1100
|
+
case "user":
|
|
1101
|
+
userArgs = argv.slice(0);
|
|
1102
|
+
break;
|
|
1103
|
+
case "eval":
|
|
1104
|
+
userArgs = argv.slice(1);
|
|
1105
|
+
break;
|
|
1106
|
+
default:
|
|
1107
|
+
throw new Error(`unexpected parse option { from: '${parseOptions.from}' }`);
|
|
1108
|
+
}
|
|
1109
|
+
if (!this._name && this._scriptPath)
|
|
1110
|
+
this.nameFromFilename(this._scriptPath);
|
|
1111
|
+
this._name = this._name || "program";
|
|
1112
|
+
return userArgs;
|
|
1113
|
+
}
|
|
1114
|
+
parse(argv, parseOptions) {
|
|
1115
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1116
|
+
this._parseCommand([], userArgs);
|
|
1117
|
+
return this;
|
|
1118
|
+
}
|
|
1119
|
+
async parseAsync(argv, parseOptions) {
|
|
1120
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1121
|
+
await this._parseCommand([], userArgs);
|
|
1122
|
+
return this;
|
|
1123
|
+
}
|
|
1124
|
+
_executeSubCommand(subcommand, args) {
|
|
1125
|
+
args = args.slice();
|
|
1126
|
+
let launchWithNode = false;
|
|
1127
|
+
const sourceExt = [".js", ".ts", ".tsx", ".mjs", ".cjs"];
|
|
1128
|
+
function findFile(baseDir, baseName) {
|
|
1129
|
+
const localBin = path.resolve(baseDir, baseName);
|
|
1130
|
+
if (fs.existsSync(localBin))
|
|
1131
|
+
return localBin;
|
|
1132
|
+
if (sourceExt.includes(path.extname(baseName)))
|
|
1133
|
+
return;
|
|
1134
|
+
const foundExt = sourceExt.find((ext) => fs.existsSync(`${localBin}${ext}`));
|
|
1135
|
+
if (foundExt)
|
|
1136
|
+
return `${localBin}${foundExt}`;
|
|
1137
|
+
return;
|
|
1138
|
+
}
|
|
1139
|
+
this._checkForMissingMandatoryOptions();
|
|
1140
|
+
this._checkForConflictingOptions();
|
|
1141
|
+
let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;
|
|
1142
|
+
let executableDir = this._executableDir || "";
|
|
1143
|
+
if (this._scriptPath) {
|
|
1144
|
+
let resolvedScriptPath;
|
|
1145
|
+
try {
|
|
1146
|
+
resolvedScriptPath = fs.realpathSync(this._scriptPath);
|
|
1147
|
+
} catch (err) {
|
|
1148
|
+
resolvedScriptPath = this._scriptPath;
|
|
1149
|
+
}
|
|
1150
|
+
executableDir = path.resolve(path.dirname(resolvedScriptPath), executableDir);
|
|
1151
|
+
}
|
|
1152
|
+
if (executableDir) {
|
|
1153
|
+
let localFile = findFile(executableDir, executableFile);
|
|
1154
|
+
if (!localFile && !subcommand._executableFile && this._scriptPath) {
|
|
1155
|
+
const legacyName = path.basename(this._scriptPath, path.extname(this._scriptPath));
|
|
1156
|
+
if (legacyName !== this._name) {
|
|
1157
|
+
localFile = findFile(executableDir, `${legacyName}-${subcommand._name}`);
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
executableFile = localFile || executableFile;
|
|
1161
|
+
}
|
|
1162
|
+
launchWithNode = sourceExt.includes(path.extname(executableFile));
|
|
1163
|
+
let proc;
|
|
1164
|
+
if (process2.platform !== "win32") {
|
|
1165
|
+
if (launchWithNode) {
|
|
1166
|
+
args.unshift(executableFile);
|
|
1167
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1168
|
+
proc = childProcess.spawn(process2.argv[0], args, { stdio: "inherit" });
|
|
1169
|
+
} else {
|
|
1170
|
+
proc = childProcess.spawn(executableFile, args, { stdio: "inherit" });
|
|
1171
|
+
}
|
|
1172
|
+
} else {
|
|
1173
|
+
args.unshift(executableFile);
|
|
1174
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1175
|
+
proc = childProcess.spawn(process2.execPath, args, { stdio: "inherit" });
|
|
1176
|
+
}
|
|
1177
|
+
if (!proc.killed) {
|
|
1178
|
+
const signals = ["SIGUSR1", "SIGUSR2", "SIGTERM", "SIGINT", "SIGHUP"];
|
|
1179
|
+
signals.forEach((signal) => {
|
|
1180
|
+
process2.on(signal, () => {
|
|
1181
|
+
if (proc.killed === false && proc.exitCode === null) {
|
|
1182
|
+
proc.kill(signal);
|
|
1183
|
+
}
|
|
1184
|
+
});
|
|
1185
|
+
});
|
|
1186
|
+
}
|
|
1187
|
+
const exitCallback = this._exitCallback;
|
|
1188
|
+
proc.on("close", (code) => {
|
|
1189
|
+
code = code ?? 1;
|
|
1190
|
+
if (!exitCallback) {
|
|
1191
|
+
process2.exit(code);
|
|
1192
|
+
} else {
|
|
1193
|
+
exitCallback(new CommanderError(code, "commander.executeSubCommandAsync", "(close)"));
|
|
1194
|
+
}
|
|
1195
|
+
});
|
|
1196
|
+
proc.on("error", (err) => {
|
|
1197
|
+
if (err.code === "ENOENT") {
|
|
1198
|
+
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";
|
|
1199
|
+
const executableMissing = `'${executableFile}' does not exist
|
|
1200
|
+
- if '${subcommand._name}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
|
|
1201
|
+
- if the default executable name is not suitable, use the executableFile option to supply a custom name or path
|
|
1202
|
+
- ${executableDirMessage}`;
|
|
1203
|
+
throw new Error(executableMissing);
|
|
1204
|
+
} else if (err.code === "EACCES") {
|
|
1205
|
+
throw new Error(`'${executableFile}' not executable`);
|
|
1206
|
+
}
|
|
1207
|
+
if (!exitCallback) {
|
|
1208
|
+
process2.exit(1);
|
|
1209
|
+
} else {
|
|
1210
|
+
const wrappedError = new CommanderError(1, "commander.executeSubCommandAsync", "(error)");
|
|
1211
|
+
wrappedError.nestedError = err;
|
|
1212
|
+
exitCallback(wrappedError);
|
|
1213
|
+
}
|
|
1214
|
+
});
|
|
1215
|
+
this.runningCommand = proc;
|
|
1216
|
+
}
|
|
1217
|
+
_dispatchSubcommand(commandName, operands, unknown) {
|
|
1218
|
+
const subCommand = this._findCommand(commandName);
|
|
1219
|
+
if (!subCommand)
|
|
1220
|
+
this.help({ error: true });
|
|
1221
|
+
let promiseChain;
|
|
1222
|
+
promiseChain = this._chainOrCallSubCommandHook(promiseChain, subCommand, "preSubcommand");
|
|
1223
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
1224
|
+
if (subCommand._executableHandler) {
|
|
1225
|
+
this._executeSubCommand(subCommand, operands.concat(unknown));
|
|
1226
|
+
} else {
|
|
1227
|
+
return subCommand._parseCommand(operands, unknown);
|
|
1228
|
+
}
|
|
1229
|
+
});
|
|
1230
|
+
return promiseChain;
|
|
1231
|
+
}
|
|
1232
|
+
_dispatchHelpCommand(subcommandName) {
|
|
1233
|
+
if (!subcommandName) {
|
|
1234
|
+
this.help();
|
|
1235
|
+
}
|
|
1236
|
+
const subCommand = this._findCommand(subcommandName);
|
|
1237
|
+
if (subCommand && !subCommand._executableHandler) {
|
|
1238
|
+
subCommand.help();
|
|
1239
|
+
}
|
|
1240
|
+
return this._dispatchSubcommand(subcommandName, [], [this._getHelpOption()?.long ?? this._getHelpOption()?.short ?? "--help"]);
|
|
1241
|
+
}
|
|
1242
|
+
_checkNumberOfArguments() {
|
|
1243
|
+
this.registeredArguments.forEach((arg, i) => {
|
|
1244
|
+
if (arg.required && this.args[i] == null) {
|
|
1245
|
+
this.missingArgument(arg.name());
|
|
1246
|
+
}
|
|
1247
|
+
});
|
|
1248
|
+
if (this.registeredArguments.length > 0 && this.registeredArguments[this.registeredArguments.length - 1].variadic) {
|
|
1249
|
+
return;
|
|
1250
|
+
}
|
|
1251
|
+
if (this.args.length > this.registeredArguments.length) {
|
|
1252
|
+
this._excessArguments(this.args);
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
_processArguments() {
|
|
1256
|
+
const myParseArg = (argument, value, previous) => {
|
|
1257
|
+
let parsedValue = value;
|
|
1258
|
+
if (value !== null && argument.parseArg) {
|
|
1259
|
+
const invalidValueMessage = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'.`;
|
|
1260
|
+
parsedValue = this._callParseArg(argument, value, previous, invalidValueMessage);
|
|
1261
|
+
}
|
|
1262
|
+
return parsedValue;
|
|
1263
|
+
};
|
|
1264
|
+
this._checkNumberOfArguments();
|
|
1265
|
+
const processedArgs = [];
|
|
1266
|
+
this.registeredArguments.forEach((declaredArg, index) => {
|
|
1267
|
+
let value = declaredArg.defaultValue;
|
|
1268
|
+
if (declaredArg.variadic) {
|
|
1269
|
+
if (index < this.args.length) {
|
|
1270
|
+
value = this.args.slice(index);
|
|
1271
|
+
if (declaredArg.parseArg) {
|
|
1272
|
+
value = value.reduce((processed, v) => {
|
|
1273
|
+
return myParseArg(declaredArg, v, processed);
|
|
1274
|
+
}, declaredArg.defaultValue);
|
|
1275
|
+
}
|
|
1276
|
+
} else if (value === undefined) {
|
|
1277
|
+
value = [];
|
|
1278
|
+
}
|
|
1279
|
+
} else if (index < this.args.length) {
|
|
1280
|
+
value = this.args[index];
|
|
1281
|
+
if (declaredArg.parseArg) {
|
|
1282
|
+
value = myParseArg(declaredArg, value, declaredArg.defaultValue);
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
processedArgs[index] = value;
|
|
1286
|
+
});
|
|
1287
|
+
this.processedArgs = processedArgs;
|
|
1288
|
+
}
|
|
1289
|
+
_chainOrCall(promise, fn) {
|
|
1290
|
+
if (promise && promise.then && typeof promise.then === "function") {
|
|
1291
|
+
return promise.then(() => fn());
|
|
1292
|
+
}
|
|
1293
|
+
return fn();
|
|
1294
|
+
}
|
|
1295
|
+
_chainOrCallHooks(promise, event) {
|
|
1296
|
+
let result = promise;
|
|
1297
|
+
const hooks = [];
|
|
1298
|
+
this._getCommandAndAncestors().reverse().filter((cmd) => cmd._lifeCycleHooks[event] !== undefined).forEach((hookedCommand) => {
|
|
1299
|
+
hookedCommand._lifeCycleHooks[event].forEach((callback) => {
|
|
1300
|
+
hooks.push({ hookedCommand, callback });
|
|
1301
|
+
});
|
|
1302
|
+
});
|
|
1303
|
+
if (event === "postAction") {
|
|
1304
|
+
hooks.reverse();
|
|
1305
|
+
}
|
|
1306
|
+
hooks.forEach((hookDetail) => {
|
|
1307
|
+
result = this._chainOrCall(result, () => {
|
|
1308
|
+
return hookDetail.callback(hookDetail.hookedCommand, this);
|
|
1309
|
+
});
|
|
1310
|
+
});
|
|
1311
|
+
return result;
|
|
1312
|
+
}
|
|
1313
|
+
_chainOrCallSubCommandHook(promise, subCommand, event) {
|
|
1314
|
+
let result = promise;
|
|
1315
|
+
if (this._lifeCycleHooks[event] !== undefined) {
|
|
1316
|
+
this._lifeCycleHooks[event].forEach((hook) => {
|
|
1317
|
+
result = this._chainOrCall(result, () => {
|
|
1318
|
+
return hook(this, subCommand);
|
|
1319
|
+
});
|
|
1320
|
+
});
|
|
1321
|
+
}
|
|
1322
|
+
return result;
|
|
1323
|
+
}
|
|
1324
|
+
_parseCommand(operands, unknown) {
|
|
1325
|
+
const parsed = this.parseOptions(unknown);
|
|
1326
|
+
this._parseOptionsEnv();
|
|
1327
|
+
this._parseOptionsImplied();
|
|
1328
|
+
operands = operands.concat(parsed.operands);
|
|
1329
|
+
unknown = parsed.unknown;
|
|
1330
|
+
this.args = operands.concat(unknown);
|
|
1331
|
+
if (operands && this._findCommand(operands[0])) {
|
|
1332
|
+
return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
|
|
1333
|
+
}
|
|
1334
|
+
if (this._getHelpCommand() && operands[0] === this._getHelpCommand().name()) {
|
|
1335
|
+
return this._dispatchHelpCommand(operands[1]);
|
|
1336
|
+
}
|
|
1337
|
+
if (this._defaultCommandName) {
|
|
1338
|
+
this._outputHelpIfRequested(unknown);
|
|
1339
|
+
return this._dispatchSubcommand(this._defaultCommandName, operands, unknown);
|
|
1340
|
+
}
|
|
1341
|
+
if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) {
|
|
1342
|
+
this.help({ error: true });
|
|
1343
|
+
}
|
|
1344
|
+
this._outputHelpIfRequested(parsed.unknown);
|
|
1345
|
+
this._checkForMissingMandatoryOptions();
|
|
1346
|
+
this._checkForConflictingOptions();
|
|
1347
|
+
const checkForUnknownOptions = () => {
|
|
1348
|
+
if (parsed.unknown.length > 0) {
|
|
1349
|
+
this.unknownOption(parsed.unknown[0]);
|
|
1350
|
+
}
|
|
1351
|
+
};
|
|
1352
|
+
const commandEvent = `command:${this.name()}`;
|
|
1353
|
+
if (this._actionHandler) {
|
|
1354
|
+
checkForUnknownOptions();
|
|
1355
|
+
this._processArguments();
|
|
1356
|
+
let promiseChain;
|
|
1357
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "preAction");
|
|
1358
|
+
promiseChain = this._chainOrCall(promiseChain, () => this._actionHandler(this.processedArgs));
|
|
1359
|
+
if (this.parent) {
|
|
1360
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
1361
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1362
|
+
});
|
|
1363
|
+
}
|
|
1364
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "postAction");
|
|
1365
|
+
return promiseChain;
|
|
1366
|
+
}
|
|
1367
|
+
if (this.parent && this.parent.listenerCount(commandEvent)) {
|
|
1368
|
+
checkForUnknownOptions();
|
|
1369
|
+
this._processArguments();
|
|
1370
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1371
|
+
} else if (operands.length) {
|
|
1372
|
+
if (this._findCommand("*")) {
|
|
1373
|
+
return this._dispatchSubcommand("*", operands, unknown);
|
|
1374
|
+
}
|
|
1375
|
+
if (this.listenerCount("command:*")) {
|
|
1376
|
+
this.emit("command:*", operands, unknown);
|
|
1377
|
+
} else if (this.commands.length) {
|
|
1378
|
+
this.unknownCommand();
|
|
1379
|
+
} else {
|
|
1380
|
+
checkForUnknownOptions();
|
|
1381
|
+
this._processArguments();
|
|
1382
|
+
}
|
|
1383
|
+
} else if (this.commands.length) {
|
|
1384
|
+
checkForUnknownOptions();
|
|
1385
|
+
this.help({ error: true });
|
|
1386
|
+
} else {
|
|
1387
|
+
checkForUnknownOptions();
|
|
1388
|
+
this._processArguments();
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
_findCommand(name) {
|
|
1392
|
+
if (!name)
|
|
1393
|
+
return;
|
|
1394
|
+
return this.commands.find((cmd) => cmd._name === name || cmd._aliases.includes(name));
|
|
1395
|
+
}
|
|
1396
|
+
_findOption(arg) {
|
|
1397
|
+
return this.options.find((option) => option.is(arg));
|
|
1398
|
+
}
|
|
1399
|
+
_checkForMissingMandatoryOptions() {
|
|
1400
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1401
|
+
cmd.options.forEach((anOption) => {
|
|
1402
|
+
if (anOption.mandatory && cmd.getOptionValue(anOption.attributeName()) === undefined) {
|
|
1403
|
+
cmd.missingMandatoryOptionValue(anOption);
|
|
1404
|
+
}
|
|
1405
|
+
});
|
|
1406
|
+
});
|
|
1407
|
+
}
|
|
1408
|
+
_checkForConflictingLocalOptions() {
|
|
1409
|
+
const definedNonDefaultOptions = this.options.filter((option) => {
|
|
1410
|
+
const optionKey = option.attributeName();
|
|
1411
|
+
if (this.getOptionValue(optionKey) === undefined) {
|
|
1412
|
+
return false;
|
|
1413
|
+
}
|
|
1414
|
+
return this.getOptionValueSource(optionKey) !== "default";
|
|
1415
|
+
});
|
|
1416
|
+
const optionsWithConflicting = definedNonDefaultOptions.filter((option) => option.conflictsWith.length > 0);
|
|
1417
|
+
optionsWithConflicting.forEach((option) => {
|
|
1418
|
+
const conflictingAndDefined = definedNonDefaultOptions.find((defined) => option.conflictsWith.includes(defined.attributeName()));
|
|
1419
|
+
if (conflictingAndDefined) {
|
|
1420
|
+
this._conflictingOption(option, conflictingAndDefined);
|
|
1421
|
+
}
|
|
1422
|
+
});
|
|
1423
|
+
}
|
|
1424
|
+
_checkForConflictingOptions() {
|
|
1425
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1426
|
+
cmd._checkForConflictingLocalOptions();
|
|
1427
|
+
});
|
|
1428
|
+
}
|
|
1429
|
+
parseOptions(argv) {
|
|
1430
|
+
const operands = [];
|
|
1431
|
+
const unknown = [];
|
|
1432
|
+
let dest = operands;
|
|
1433
|
+
const args = argv.slice();
|
|
1434
|
+
function maybeOption(arg) {
|
|
1435
|
+
return arg.length > 1 && arg[0] === "-";
|
|
1436
|
+
}
|
|
1437
|
+
let activeVariadicOption = null;
|
|
1438
|
+
while (args.length) {
|
|
1439
|
+
const arg = args.shift();
|
|
1440
|
+
if (arg === "--") {
|
|
1441
|
+
if (dest === unknown)
|
|
1442
|
+
dest.push(arg);
|
|
1443
|
+
dest.push(...args);
|
|
1444
|
+
break;
|
|
1445
|
+
}
|
|
1446
|
+
if (activeVariadicOption && !maybeOption(arg)) {
|
|
1447
|
+
this.emit(`option:${activeVariadicOption.name()}`, arg);
|
|
1448
|
+
continue;
|
|
1449
|
+
}
|
|
1450
|
+
activeVariadicOption = null;
|
|
1451
|
+
if (maybeOption(arg)) {
|
|
1452
|
+
const option = this._findOption(arg);
|
|
1453
|
+
if (option) {
|
|
1454
|
+
if (option.required) {
|
|
1455
|
+
const value = args.shift();
|
|
1456
|
+
if (value === undefined)
|
|
1457
|
+
this.optionMissingArgument(option);
|
|
1458
|
+
this.emit(`option:${option.name()}`, value);
|
|
1459
|
+
} else if (option.optional) {
|
|
1460
|
+
let value = null;
|
|
1461
|
+
if (args.length > 0 && !maybeOption(args[0])) {
|
|
1462
|
+
value = args.shift();
|
|
1463
|
+
}
|
|
1464
|
+
this.emit(`option:${option.name()}`, value);
|
|
1465
|
+
} else {
|
|
1466
|
+
this.emit(`option:${option.name()}`);
|
|
1467
|
+
}
|
|
1468
|
+
activeVariadicOption = option.variadic ? option : null;
|
|
1469
|
+
continue;
|
|
1470
|
+
}
|
|
1471
|
+
}
|
|
1472
|
+
if (arg.length > 2 && arg[0] === "-" && arg[1] !== "-") {
|
|
1473
|
+
const option = this._findOption(`-${arg[1]}`);
|
|
1474
|
+
if (option) {
|
|
1475
|
+
if (option.required || option.optional && this._combineFlagAndOptionalValue) {
|
|
1476
|
+
this.emit(`option:${option.name()}`, arg.slice(2));
|
|
1477
|
+
} else {
|
|
1478
|
+
this.emit(`option:${option.name()}`);
|
|
1479
|
+
args.unshift(`-${arg.slice(2)}`);
|
|
1480
|
+
}
|
|
1481
|
+
continue;
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
if (/^--[^=]+=/.test(arg)) {
|
|
1485
|
+
const index = arg.indexOf("=");
|
|
1486
|
+
const option = this._findOption(arg.slice(0, index));
|
|
1487
|
+
if (option && (option.required || option.optional)) {
|
|
1488
|
+
this.emit(`option:${option.name()}`, arg.slice(index + 1));
|
|
1489
|
+
continue;
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
if (maybeOption(arg)) {
|
|
1493
|
+
dest = unknown;
|
|
1494
|
+
}
|
|
1495
|
+
if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
|
|
1496
|
+
if (this._findCommand(arg)) {
|
|
1497
|
+
operands.push(arg);
|
|
1498
|
+
if (args.length > 0)
|
|
1499
|
+
unknown.push(...args);
|
|
1500
|
+
break;
|
|
1501
|
+
} else if (this._getHelpCommand() && arg === this._getHelpCommand().name()) {
|
|
1502
|
+
operands.push(arg);
|
|
1503
|
+
if (args.length > 0)
|
|
1504
|
+
operands.push(...args);
|
|
1505
|
+
break;
|
|
1506
|
+
} else if (this._defaultCommandName) {
|
|
1507
|
+
unknown.push(arg);
|
|
1508
|
+
if (args.length > 0)
|
|
1509
|
+
unknown.push(...args);
|
|
1510
|
+
break;
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
if (this._passThroughOptions) {
|
|
1514
|
+
dest.push(arg);
|
|
1515
|
+
if (args.length > 0)
|
|
1516
|
+
dest.push(...args);
|
|
1517
|
+
break;
|
|
1518
|
+
}
|
|
1519
|
+
dest.push(arg);
|
|
1520
|
+
}
|
|
1521
|
+
return { operands, unknown };
|
|
1522
|
+
}
|
|
1523
|
+
opts() {
|
|
1524
|
+
if (this._storeOptionsAsProperties) {
|
|
1525
|
+
const result = {};
|
|
1526
|
+
const len = this.options.length;
|
|
1527
|
+
for (let i = 0;i < len; i++) {
|
|
1528
|
+
const key = this.options[i].attributeName();
|
|
1529
|
+
result[key] = key === this._versionOptionName ? this._version : this[key];
|
|
1530
|
+
}
|
|
1531
|
+
return result;
|
|
1532
|
+
}
|
|
1533
|
+
return this._optionValues;
|
|
1534
|
+
}
|
|
1535
|
+
optsWithGlobals() {
|
|
1536
|
+
return this._getCommandAndAncestors().reduce((combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()), {});
|
|
1537
|
+
}
|
|
1538
|
+
error(message, errorOptions) {
|
|
1539
|
+
this._outputConfiguration.outputError(`${message}
|
|
1540
|
+
`, this._outputConfiguration.writeErr);
|
|
1541
|
+
if (typeof this._showHelpAfterError === "string") {
|
|
1542
|
+
this._outputConfiguration.writeErr(`${this._showHelpAfterError}
|
|
1543
|
+
`);
|
|
1544
|
+
} else if (this._showHelpAfterError) {
|
|
1545
|
+
this._outputConfiguration.writeErr(`
|
|
1546
|
+
`);
|
|
1547
|
+
this.outputHelp({ error: true });
|
|
1548
|
+
}
|
|
1549
|
+
const config = errorOptions || {};
|
|
1550
|
+
const exitCode = config.exitCode || 1;
|
|
1551
|
+
const code = config.code || "commander.error";
|
|
1552
|
+
this._exit(exitCode, code, message);
|
|
1553
|
+
}
|
|
1554
|
+
_parseOptionsEnv() {
|
|
1555
|
+
this.options.forEach((option) => {
|
|
1556
|
+
if (option.envVar && option.envVar in process2.env) {
|
|
1557
|
+
const optionKey = option.attributeName();
|
|
1558
|
+
if (this.getOptionValue(optionKey) === undefined || ["default", "config", "env"].includes(this.getOptionValueSource(optionKey))) {
|
|
1559
|
+
if (option.required || option.optional) {
|
|
1560
|
+
this.emit(`optionEnv:${option.name()}`, process2.env[option.envVar]);
|
|
1561
|
+
} else {
|
|
1562
|
+
this.emit(`optionEnv:${option.name()}`);
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
});
|
|
1567
|
+
}
|
|
1568
|
+
_parseOptionsImplied() {
|
|
1569
|
+
const dualHelper = new DualOptions(this.options);
|
|
1570
|
+
const hasCustomOptionValue = (optionKey) => {
|
|
1571
|
+
return this.getOptionValue(optionKey) !== undefined && !["default", "implied"].includes(this.getOptionValueSource(optionKey));
|
|
1572
|
+
};
|
|
1573
|
+
this.options.filter((option) => option.implied !== undefined && hasCustomOptionValue(option.attributeName()) && dualHelper.valueFromOption(this.getOptionValue(option.attributeName()), option)).forEach((option) => {
|
|
1574
|
+
Object.keys(option.implied).filter((impliedKey) => !hasCustomOptionValue(impliedKey)).forEach((impliedKey) => {
|
|
1575
|
+
this.setOptionValueWithSource(impliedKey, option.implied[impliedKey], "implied");
|
|
1576
|
+
});
|
|
1577
|
+
});
|
|
1578
|
+
}
|
|
1579
|
+
missingArgument(name) {
|
|
1580
|
+
const message = `error: missing required argument '${name}'`;
|
|
1581
|
+
this.error(message, { code: "commander.missingArgument" });
|
|
1582
|
+
}
|
|
1583
|
+
optionMissingArgument(option) {
|
|
1584
|
+
const message = `error: option '${option.flags}' argument missing`;
|
|
1585
|
+
this.error(message, { code: "commander.optionMissingArgument" });
|
|
1586
|
+
}
|
|
1587
|
+
missingMandatoryOptionValue(option) {
|
|
1588
|
+
const message = `error: required option '${option.flags}' not specified`;
|
|
1589
|
+
this.error(message, { code: "commander.missingMandatoryOptionValue" });
|
|
1590
|
+
}
|
|
1591
|
+
_conflictingOption(option, conflictingOption) {
|
|
1592
|
+
const findBestOptionFromValue = (option2) => {
|
|
1593
|
+
const optionKey = option2.attributeName();
|
|
1594
|
+
const optionValue = this.getOptionValue(optionKey);
|
|
1595
|
+
const negativeOption = this.options.find((target) => target.negate && optionKey === target.attributeName());
|
|
1596
|
+
const positiveOption = this.options.find((target) => !target.negate && optionKey === target.attributeName());
|
|
1597
|
+
if (negativeOption && (negativeOption.presetArg === undefined && optionValue === false || negativeOption.presetArg !== undefined && optionValue === negativeOption.presetArg)) {
|
|
1598
|
+
return negativeOption;
|
|
1599
|
+
}
|
|
1600
|
+
return positiveOption || option2;
|
|
1601
|
+
};
|
|
1602
|
+
const getErrorMessage = (option2) => {
|
|
1603
|
+
const bestOption = findBestOptionFromValue(option2);
|
|
1604
|
+
const optionKey = bestOption.attributeName();
|
|
1605
|
+
const source = this.getOptionValueSource(optionKey);
|
|
1606
|
+
if (source === "env") {
|
|
1607
|
+
return `environment variable '${bestOption.envVar}'`;
|
|
1608
|
+
}
|
|
1609
|
+
return `option '${bestOption.flags}'`;
|
|
1610
|
+
};
|
|
1611
|
+
const message = `error: ${getErrorMessage(option)} cannot be used with ${getErrorMessage(conflictingOption)}`;
|
|
1612
|
+
this.error(message, { code: "commander.conflictingOption" });
|
|
1613
|
+
}
|
|
1614
|
+
unknownOption(flag) {
|
|
1615
|
+
if (this._allowUnknownOption)
|
|
1616
|
+
return;
|
|
1617
|
+
let suggestion = "";
|
|
1618
|
+
if (flag.startsWith("--") && this._showSuggestionAfterError) {
|
|
1619
|
+
let candidateFlags = [];
|
|
1620
|
+
let command = this;
|
|
1621
|
+
do {
|
|
1622
|
+
const moreFlags = command.createHelp().visibleOptions(command).filter((option) => option.long).map((option) => option.long);
|
|
1623
|
+
candidateFlags = candidateFlags.concat(moreFlags);
|
|
1624
|
+
command = command.parent;
|
|
1625
|
+
} while (command && !command._enablePositionalOptions);
|
|
1626
|
+
suggestion = suggestSimilar(flag, candidateFlags);
|
|
1627
|
+
}
|
|
1628
|
+
const message = `error: unknown option '${flag}'${suggestion}`;
|
|
1629
|
+
this.error(message, { code: "commander.unknownOption" });
|
|
1630
|
+
}
|
|
1631
|
+
_excessArguments(receivedArgs) {
|
|
1632
|
+
if (this._allowExcessArguments)
|
|
1633
|
+
return;
|
|
1634
|
+
const expected = this.registeredArguments.length;
|
|
1635
|
+
const s = expected === 1 ? "" : "s";
|
|
1636
|
+
const forSubcommand = this.parent ? ` for '${this.name()}'` : "";
|
|
1637
|
+
const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;
|
|
1638
|
+
this.error(message, { code: "commander.excessArguments" });
|
|
1639
|
+
}
|
|
1640
|
+
unknownCommand() {
|
|
1641
|
+
const unknownName = this.args[0];
|
|
1642
|
+
let suggestion = "";
|
|
1643
|
+
if (this._showSuggestionAfterError) {
|
|
1644
|
+
const candidateNames = [];
|
|
1645
|
+
this.createHelp().visibleCommands(this).forEach((command) => {
|
|
1646
|
+
candidateNames.push(command.name());
|
|
1647
|
+
if (command.alias())
|
|
1648
|
+
candidateNames.push(command.alias());
|
|
1649
|
+
});
|
|
1650
|
+
suggestion = suggestSimilar(unknownName, candidateNames);
|
|
1651
|
+
}
|
|
1652
|
+
const message = `error: unknown command '${unknownName}'${suggestion}`;
|
|
1653
|
+
this.error(message, { code: "commander.unknownCommand" });
|
|
1654
|
+
}
|
|
1655
|
+
version(str, flags, description) {
|
|
1656
|
+
if (str === undefined)
|
|
1657
|
+
return this._version;
|
|
1658
|
+
this._version = str;
|
|
1659
|
+
flags = flags || "-V, --version";
|
|
1660
|
+
description = description || "output the version number";
|
|
1661
|
+
const versionOption = this.createOption(flags, description);
|
|
1662
|
+
this._versionOptionName = versionOption.attributeName();
|
|
1663
|
+
this._registerOption(versionOption);
|
|
1664
|
+
this.on("option:" + versionOption.name(), () => {
|
|
1665
|
+
this._outputConfiguration.writeOut(`${str}
|
|
1666
|
+
`);
|
|
1667
|
+
this._exit(0, "commander.version", str);
|
|
1668
|
+
});
|
|
1669
|
+
return this;
|
|
1670
|
+
}
|
|
1671
|
+
description(str, argsDescription) {
|
|
1672
|
+
if (str === undefined && argsDescription === undefined)
|
|
1673
|
+
return this._description;
|
|
1674
|
+
this._description = str;
|
|
1675
|
+
if (argsDescription) {
|
|
1676
|
+
this._argsDescription = argsDescription;
|
|
1677
|
+
}
|
|
1678
|
+
return this;
|
|
1679
|
+
}
|
|
1680
|
+
summary(str) {
|
|
1681
|
+
if (str === undefined)
|
|
1682
|
+
return this._summary;
|
|
1683
|
+
this._summary = str;
|
|
1684
|
+
return this;
|
|
1685
|
+
}
|
|
1686
|
+
alias(alias) {
|
|
1687
|
+
if (alias === undefined)
|
|
1688
|
+
return this._aliases[0];
|
|
1689
|
+
let command = this;
|
|
1690
|
+
if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
|
|
1691
|
+
command = this.commands[this.commands.length - 1];
|
|
1692
|
+
}
|
|
1693
|
+
if (alias === command._name)
|
|
1694
|
+
throw new Error("Command alias can't be the same as its name");
|
|
1695
|
+
const matchingCommand = this.parent?._findCommand(alias);
|
|
1696
|
+
if (matchingCommand) {
|
|
1697
|
+
const existingCmd = [matchingCommand.name()].concat(matchingCommand.aliases()).join("|");
|
|
1698
|
+
throw new Error(`cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`);
|
|
1699
|
+
}
|
|
1700
|
+
command._aliases.push(alias);
|
|
1701
|
+
return this;
|
|
1702
|
+
}
|
|
1703
|
+
aliases(aliases) {
|
|
1704
|
+
if (aliases === undefined)
|
|
1705
|
+
return this._aliases;
|
|
1706
|
+
aliases.forEach((alias) => this.alias(alias));
|
|
1707
|
+
return this;
|
|
1708
|
+
}
|
|
1709
|
+
usage(str) {
|
|
1710
|
+
if (str === undefined) {
|
|
1711
|
+
if (this._usage)
|
|
1712
|
+
return this._usage;
|
|
1713
|
+
const args = this.registeredArguments.map((arg) => {
|
|
1714
|
+
return humanReadableArgName(arg);
|
|
1715
|
+
});
|
|
1716
|
+
return [].concat(this.options.length || this._helpOption !== null ? "[options]" : [], this.commands.length ? "[command]" : [], this.registeredArguments.length ? args : []).join(" ");
|
|
1717
|
+
}
|
|
1718
|
+
this._usage = str;
|
|
1719
|
+
return this;
|
|
1720
|
+
}
|
|
1721
|
+
name(str) {
|
|
1722
|
+
if (str === undefined)
|
|
1723
|
+
return this._name;
|
|
1724
|
+
this._name = str;
|
|
1725
|
+
return this;
|
|
1726
|
+
}
|
|
1727
|
+
nameFromFilename(filename) {
|
|
1728
|
+
this._name = path.basename(filename, path.extname(filename));
|
|
1729
|
+
return this;
|
|
1730
|
+
}
|
|
1731
|
+
executableDir(path2) {
|
|
1732
|
+
if (path2 === undefined)
|
|
1733
|
+
return this._executableDir;
|
|
1734
|
+
this._executableDir = path2;
|
|
1735
|
+
return this;
|
|
1736
|
+
}
|
|
1737
|
+
helpInformation(contextOptions) {
|
|
1738
|
+
const helper = this.createHelp();
|
|
1739
|
+
if (helper.helpWidth === undefined) {
|
|
1740
|
+
helper.helpWidth = contextOptions && contextOptions.error ? this._outputConfiguration.getErrHelpWidth() : this._outputConfiguration.getOutHelpWidth();
|
|
1741
|
+
}
|
|
1742
|
+
return helper.formatHelp(this, helper);
|
|
1743
|
+
}
|
|
1744
|
+
_getHelpContext(contextOptions) {
|
|
1745
|
+
contextOptions = contextOptions || {};
|
|
1746
|
+
const context = { error: !!contextOptions.error };
|
|
1747
|
+
let write;
|
|
1748
|
+
if (context.error) {
|
|
1749
|
+
write = (arg) => this._outputConfiguration.writeErr(arg);
|
|
1750
|
+
} else {
|
|
1751
|
+
write = (arg) => this._outputConfiguration.writeOut(arg);
|
|
1752
|
+
}
|
|
1753
|
+
context.write = contextOptions.write || write;
|
|
1754
|
+
context.command = this;
|
|
1755
|
+
return context;
|
|
1756
|
+
}
|
|
1757
|
+
outputHelp(contextOptions) {
|
|
1758
|
+
let deprecatedCallback;
|
|
1759
|
+
if (typeof contextOptions === "function") {
|
|
1760
|
+
deprecatedCallback = contextOptions;
|
|
1761
|
+
contextOptions = undefined;
|
|
1762
|
+
}
|
|
1763
|
+
const context = this._getHelpContext(contextOptions);
|
|
1764
|
+
this._getCommandAndAncestors().reverse().forEach((command) => command.emit("beforeAllHelp", context));
|
|
1765
|
+
this.emit("beforeHelp", context);
|
|
1766
|
+
let helpInformation = this.helpInformation(context);
|
|
1767
|
+
if (deprecatedCallback) {
|
|
1768
|
+
helpInformation = deprecatedCallback(helpInformation);
|
|
1769
|
+
if (typeof helpInformation !== "string" && !Buffer.isBuffer(helpInformation)) {
|
|
1770
|
+
throw new Error("outputHelp callback must return a string or a Buffer");
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
context.write(helpInformation);
|
|
1774
|
+
if (this._getHelpOption()?.long) {
|
|
1775
|
+
this.emit(this._getHelpOption().long);
|
|
1776
|
+
}
|
|
1777
|
+
this.emit("afterHelp", context);
|
|
1778
|
+
this._getCommandAndAncestors().forEach((command) => command.emit("afterAllHelp", context));
|
|
1779
|
+
}
|
|
1780
|
+
helpOption(flags, description) {
|
|
1781
|
+
if (typeof flags === "boolean") {
|
|
1782
|
+
if (flags) {
|
|
1783
|
+
this._helpOption = this._helpOption ?? undefined;
|
|
1784
|
+
} else {
|
|
1785
|
+
this._helpOption = null;
|
|
1786
|
+
}
|
|
1787
|
+
return this;
|
|
1788
|
+
}
|
|
1789
|
+
flags = flags ?? "-h, --help";
|
|
1790
|
+
description = description ?? "display help for command";
|
|
1791
|
+
this._helpOption = this.createOption(flags, description);
|
|
1792
|
+
return this;
|
|
1793
|
+
}
|
|
1794
|
+
_getHelpOption() {
|
|
1795
|
+
if (this._helpOption === undefined) {
|
|
1796
|
+
this.helpOption(undefined, undefined);
|
|
1797
|
+
}
|
|
1798
|
+
return this._helpOption;
|
|
1799
|
+
}
|
|
1800
|
+
addHelpOption(option) {
|
|
1801
|
+
this._helpOption = option;
|
|
1802
|
+
return this;
|
|
1803
|
+
}
|
|
1804
|
+
help(contextOptions) {
|
|
1805
|
+
this.outputHelp(contextOptions);
|
|
1806
|
+
let exitCode = process2.exitCode || 0;
|
|
1807
|
+
if (exitCode === 0 && contextOptions && typeof contextOptions !== "function" && contextOptions.error) {
|
|
1808
|
+
exitCode = 1;
|
|
1809
|
+
}
|
|
1810
|
+
this._exit(exitCode, "commander.help", "(outputHelp)");
|
|
1811
|
+
}
|
|
1812
|
+
addHelpText(position, text) {
|
|
1813
|
+
const allowedValues = ["beforeAll", "before", "after", "afterAll"];
|
|
1814
|
+
if (!allowedValues.includes(position)) {
|
|
1815
|
+
throw new Error(`Unexpected value for position to addHelpText.
|
|
1816
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
1817
|
+
}
|
|
1818
|
+
const helpEvent = `${position}Help`;
|
|
1819
|
+
this.on(helpEvent, (context) => {
|
|
1820
|
+
let helpStr;
|
|
1821
|
+
if (typeof text === "function") {
|
|
1822
|
+
helpStr = text({ error: context.error, command: context.command });
|
|
1823
|
+
} else {
|
|
1824
|
+
helpStr = text;
|
|
1825
|
+
}
|
|
1826
|
+
if (helpStr) {
|
|
1827
|
+
context.write(`${helpStr}
|
|
1828
|
+
`);
|
|
1829
|
+
}
|
|
1830
|
+
});
|
|
1831
|
+
return this;
|
|
1832
|
+
}
|
|
1833
|
+
_outputHelpIfRequested(args) {
|
|
1834
|
+
const helpOption = this._getHelpOption();
|
|
1835
|
+
const helpRequested = helpOption && args.find((arg) => helpOption.is(arg));
|
|
1836
|
+
if (helpRequested) {
|
|
1837
|
+
this.outputHelp();
|
|
1838
|
+
this._exit(0, "commander.helpDisplayed", "(outputHelp)");
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
}
|
|
1842
|
+
function incrementNodeInspectorPort(args) {
|
|
1843
|
+
return args.map((arg) => {
|
|
1844
|
+
if (!arg.startsWith("--inspect")) {
|
|
1845
|
+
return arg;
|
|
1846
|
+
}
|
|
1847
|
+
let debugOption;
|
|
1848
|
+
let debugHost = "127.0.0.1";
|
|
1849
|
+
let debugPort = "9229";
|
|
1850
|
+
let match;
|
|
1851
|
+
if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
|
|
1852
|
+
debugOption = match[1];
|
|
1853
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
|
|
1854
|
+
debugOption = match[1];
|
|
1855
|
+
if (/^\d+$/.test(match[3])) {
|
|
1856
|
+
debugPort = match[3];
|
|
1857
|
+
} else {
|
|
1858
|
+
debugHost = match[3];
|
|
1859
|
+
}
|
|
1860
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) {
|
|
1861
|
+
debugOption = match[1];
|
|
1862
|
+
debugHost = match[3];
|
|
1863
|
+
debugPort = match[4];
|
|
1864
|
+
}
|
|
1865
|
+
if (debugOption && debugPort !== "0") {
|
|
1866
|
+
return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
|
|
1867
|
+
}
|
|
1868
|
+
return arg;
|
|
1869
|
+
});
|
|
1870
|
+
}
|
|
1871
|
+
exports.Command = Command;
|
|
1872
|
+
});
|
|
1873
|
+
|
|
1874
|
+
// node_modules/commander/index.js
|
|
1875
|
+
var require_commander = __commonJS((exports) => {
|
|
1876
|
+
var { Argument } = require_argument();
|
|
1877
|
+
var { Command } = require_command();
|
|
1878
|
+
var { CommanderError, InvalidArgumentError } = require_error();
|
|
1879
|
+
var { Help } = require_help();
|
|
1880
|
+
var { Option } = require_option();
|
|
1881
|
+
exports.program = new Command;
|
|
1882
|
+
exports.createCommand = (name) => new Command(name);
|
|
1883
|
+
exports.createOption = (flags, description) => new Option(flags, description);
|
|
1884
|
+
exports.createArgument = (name, description) => new Argument(name, description);
|
|
1885
|
+
exports.Command = Command;
|
|
1886
|
+
exports.Option = Option;
|
|
1887
|
+
exports.Argument = Argument;
|
|
1888
|
+
exports.Help = Help;
|
|
1889
|
+
exports.CommanderError = CommanderError;
|
|
1890
|
+
exports.InvalidArgumentError = InvalidArgumentError;
|
|
1891
|
+
exports.InvalidOptionArgumentError = InvalidArgumentError;
|
|
1892
|
+
});
|
|
1893
|
+
|
|
1894
|
+
// package.json
|
|
1895
|
+
var require_package = __commonJS((exports, module) => {
|
|
1896
|
+
module.exports = {
|
|
1897
|
+
name: "@captainsafia/stitch",
|
|
1898
|
+
version: "1.0.0-preview.c77f34b",
|
|
1899
|
+
type: "module",
|
|
1900
|
+
description: "A local-first CLI for recording intent and binding it to git commits/diffs",
|
|
1901
|
+
main: "dist/api.js",
|
|
1902
|
+
types: "dist/api.d.ts",
|
|
1903
|
+
bin: {
|
|
1904
|
+
stitch: "dist/cli.js",
|
|
1905
|
+
"stitch-mcp": "dist/mcp.js"
|
|
1906
|
+
},
|
|
1907
|
+
files: [
|
|
1908
|
+
"dist"
|
|
1909
|
+
],
|
|
1910
|
+
exports: {
|
|
1911
|
+
".": {
|
|
1912
|
+
types: "./dist/api.d.ts",
|
|
1913
|
+
import: "./dist/api.js"
|
|
1914
|
+
}
|
|
1915
|
+
},
|
|
1916
|
+
scripts: {
|
|
1917
|
+
build: "bun run build:api && bun run build:cli && bun run build:mcp",
|
|
1918
|
+
"build:api": "bun build src/api.ts --outdir dist --target bun && dts-bundle-generator -o dist/api.d.ts src/api.ts",
|
|
1919
|
+
"build:cli": "bun build src/cli.ts --outdir dist --target bun",
|
|
1920
|
+
"build:mcp": "bun build src/mcp.ts --outdir dist --target bun",
|
|
1921
|
+
compile: "bun run compile:cli && bun run compile:mcp",
|
|
1922
|
+
"compile:cli": "bun build src/cli.ts --compile --outfile stitch",
|
|
1923
|
+
"compile:mcp": "bun build src/mcp.ts --compile --outfile stitch-mcp",
|
|
1924
|
+
"compile:cli:linux-x64": "bun build src/cli.ts --compile --target=bun-linux-x64 --outfile stitch-linux-x64",
|
|
1925
|
+
"compile:cli:linux-arm64": "bun build src/cli.ts --compile --target=bun-linux-arm64 --outfile stitch-linux-arm64",
|
|
1926
|
+
"compile:cli:darwin-x64": "bun build src/cli.ts --compile --target=bun-darwin-x64 --outfile stitch-darwin-x64",
|
|
1927
|
+
"compile:cli:darwin-arm64": "bun build src/cli.ts --compile --target=bun-darwin-arm64 --outfile stitch-darwin-arm64",
|
|
1928
|
+
"compile:cli:windows-x64": "bun build src/cli.ts --compile --target=bun-windows-x64 --outfile stitch-windows-x64.exe",
|
|
1929
|
+
"compile:mcp:linux-x64": "bun build src/mcp.ts --compile --target=bun-linux-x64 --outfile stitch-mcp-linux-x64",
|
|
1930
|
+
"compile:mcp:linux-arm64": "bun build src/mcp.ts --compile --target=bun-linux-arm64 --outfile stitch-mcp-linux-arm64",
|
|
1931
|
+
"compile:mcp:darwin-x64": "bun build src/mcp.ts --compile --target=bun-darwin-x64 --outfile stitch-mcp-darwin-x64",
|
|
1932
|
+
"compile:mcp:darwin-arm64": "bun build src/mcp.ts --compile --target=bun-darwin-arm64 --outfile stitch-mcp-darwin-arm64",
|
|
1933
|
+
"compile:mcp:windows-x64": "bun build src/mcp.ts --compile --target=bun-windows-x64 --outfile stitch-mcp-windows-x64.exe",
|
|
1934
|
+
test: "bun test",
|
|
1935
|
+
typecheck: "tsc --noEmit"
|
|
1936
|
+
},
|
|
1937
|
+
devDependencies: {
|
|
1938
|
+
"@types/bun": "latest",
|
|
1939
|
+
"dts-bundle-generator": "^9.5.1",
|
|
1940
|
+
typescript: "^5"
|
|
1941
|
+
},
|
|
1942
|
+
dependencies: {
|
|
1943
|
+
"@modelcontextprotocol/sdk": "^1.25.1",
|
|
1944
|
+
"async-mutex": "^0.5.0",
|
|
1945
|
+
commander: "^12.0.0",
|
|
1946
|
+
"smol-toml": "^1.3.0",
|
|
1947
|
+
zod: "^4.2.1"
|
|
1948
|
+
},
|
|
1949
|
+
license: "MIT",
|
|
1950
|
+
repository: {
|
|
1951
|
+
type: "git",
|
|
1952
|
+
url: "https://github.com/captainsafia/stitch.git"
|
|
1953
|
+
},
|
|
1954
|
+
keywords: [
|
|
1955
|
+
"git",
|
|
1956
|
+
"intent",
|
|
1957
|
+
"dag",
|
|
1958
|
+
"cli",
|
|
1959
|
+
"developer-tools"
|
|
1960
|
+
],
|
|
1961
|
+
engines: {
|
|
1962
|
+
bun: ">=1.0.0"
|
|
1963
|
+
}
|
|
1964
|
+
};
|
|
1965
|
+
});
|
|
1966
|
+
|
|
1967
|
+
// node_modules/commander/esm.mjs
|
|
1968
|
+
var import__ = __toESM(require_commander(), 1);
|
|
1969
|
+
var {
|
|
1970
|
+
program,
|
|
1971
|
+
createCommand,
|
|
1972
|
+
createArgument,
|
|
1973
|
+
createOption,
|
|
1974
|
+
CommanderError,
|
|
1975
|
+
InvalidArgumentError,
|
|
1976
|
+
InvalidOptionArgumentError,
|
|
1977
|
+
Command,
|
|
1978
|
+
Argument,
|
|
1979
|
+
Option,
|
|
1980
|
+
Help
|
|
1981
|
+
} = import__.default;
|
|
1982
|
+
|
|
1983
|
+
// src/api.ts
|
|
1984
|
+
import { spawn } from "child_process";
|
|
1985
|
+
|
|
1986
|
+
// src/core/store.ts
|
|
1987
|
+
import { readdir, readFile, writeFile, mkdir } from "fs/promises";
|
|
1988
|
+
import { join } from "path";
|
|
1989
|
+
import { existsSync } from "fs";
|
|
1990
|
+
|
|
1991
|
+
// src/core/errors.ts
|
|
1992
|
+
class StitchError extends Error {
|
|
1993
|
+
constructor(message) {
|
|
1994
|
+
super(message);
|
|
1995
|
+
this.name = "StitchError";
|
|
1996
|
+
}
|
|
1997
|
+
}
|
|
1998
|
+
|
|
1999
|
+
class RepoNotFoundError extends StitchError {
|
|
2000
|
+
constructor(path) {
|
|
2001
|
+
super(path ? `Not a git repository: ${path}` : "Not a git repository (or any parent up to mount point)");
|
|
2002
|
+
this.name = "RepoNotFoundError";
|
|
2003
|
+
}
|
|
2004
|
+
}
|
|
2005
|
+
|
|
2006
|
+
class NotInitializedError extends StitchError {
|
|
2007
|
+
constructor() {
|
|
2008
|
+
super("Stitch is not initialized in this repository. Run 'stitch init' first.");
|
|
2009
|
+
this.name = "NotInitializedError";
|
|
2010
|
+
}
|
|
2011
|
+
}
|
|
2012
|
+
|
|
2013
|
+
class NoCurrentStitchError extends StitchError {
|
|
2014
|
+
constructor() {
|
|
2015
|
+
super("No current stitch. Start a new stitch with 'stitch start <title>' or switch to an existing one with 'stitch switch <id>'.");
|
|
2016
|
+
this.name = "NoCurrentStitchError";
|
|
2017
|
+
}
|
|
2018
|
+
}
|
|
2019
|
+
|
|
2020
|
+
class StitchNotFoundError extends StitchError {
|
|
2021
|
+
constructor(id) {
|
|
2022
|
+
super(`Stitch not found: ${id}`);
|
|
2023
|
+
this.name = "StitchNotFoundError";
|
|
2024
|
+
}
|
|
2025
|
+
}
|
|
2026
|
+
|
|
2027
|
+
class GitError extends StitchError {
|
|
2028
|
+
command;
|
|
2029
|
+
exitCode;
|
|
2030
|
+
constructor(message, command, exitCode) {
|
|
2031
|
+
super(`Git error: ${message}`);
|
|
2032
|
+
this.command = command;
|
|
2033
|
+
this.exitCode = exitCode;
|
|
2034
|
+
this.name = "GitError";
|
|
2035
|
+
}
|
|
2036
|
+
}
|
|
2037
|
+
|
|
2038
|
+
class ValidationError extends StitchError {
|
|
2039
|
+
constructor(message) {
|
|
2040
|
+
super(`Validation error: ${message}`);
|
|
2041
|
+
this.name = "ValidationError";
|
|
2042
|
+
}
|
|
2043
|
+
}
|
|
2044
|
+
|
|
2045
|
+
// node_modules/smol-toml/dist/error.js
|
|
2046
|
+
/*!
|
|
2047
|
+
* Copyright (c) Squirrel Chat et al., All rights reserved.
|
|
2048
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
2049
|
+
*
|
|
2050
|
+
* Redistribution and use in source and binary forms, with or without
|
|
2051
|
+
* modification, are permitted provided that the following conditions are met:
|
|
2052
|
+
*
|
|
2053
|
+
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
2054
|
+
* list of conditions and the following disclaimer.
|
|
2055
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
2056
|
+
* this list of conditions and the following disclaimer in the
|
|
2057
|
+
* documentation and/or other materials provided with the distribution.
|
|
2058
|
+
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
2059
|
+
* may be used to endorse or promote products derived from this software without
|
|
2060
|
+
* specific prior written permission.
|
|
2061
|
+
*
|
|
2062
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
2063
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
2064
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
2065
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
2066
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
2067
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
2068
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
2069
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
2070
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
2071
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
2072
|
+
*/
|
|
2073
|
+
function getLineColFromPtr(string, ptr) {
|
|
2074
|
+
let lines = string.slice(0, ptr).split(/\r\n|\n|\r/g);
|
|
2075
|
+
return [lines.length, lines.pop().length + 1];
|
|
2076
|
+
}
|
|
2077
|
+
function makeCodeBlock(string, line, column) {
|
|
2078
|
+
let lines = string.split(/\r\n|\n|\r/g);
|
|
2079
|
+
let codeblock = "";
|
|
2080
|
+
let numberLen = (Math.log10(line + 1) | 0) + 1;
|
|
2081
|
+
for (let i = line - 1;i <= line + 1; i++) {
|
|
2082
|
+
let l = lines[i - 1];
|
|
2083
|
+
if (!l)
|
|
2084
|
+
continue;
|
|
2085
|
+
codeblock += i.toString().padEnd(numberLen, " ");
|
|
2086
|
+
codeblock += ": ";
|
|
2087
|
+
codeblock += l;
|
|
2088
|
+
codeblock += `
|
|
2089
|
+
`;
|
|
2090
|
+
if (i === line) {
|
|
2091
|
+
codeblock += " ".repeat(numberLen + column + 2);
|
|
2092
|
+
codeblock += `^
|
|
2093
|
+
`;
|
|
2094
|
+
}
|
|
2095
|
+
}
|
|
2096
|
+
return codeblock;
|
|
2097
|
+
}
|
|
2098
|
+
|
|
2099
|
+
class TomlError extends Error {
|
|
2100
|
+
line;
|
|
2101
|
+
column;
|
|
2102
|
+
codeblock;
|
|
2103
|
+
constructor(message, options) {
|
|
2104
|
+
const [line, column] = getLineColFromPtr(options.toml, options.ptr);
|
|
2105
|
+
const codeblock = makeCodeBlock(options.toml, line, column);
|
|
2106
|
+
super(`Invalid TOML document: ${message}
|
|
2107
|
+
|
|
2108
|
+
${codeblock}`, options);
|
|
2109
|
+
this.line = line;
|
|
2110
|
+
this.column = column;
|
|
2111
|
+
this.codeblock = codeblock;
|
|
2112
|
+
}
|
|
2113
|
+
}
|
|
2114
|
+
|
|
2115
|
+
// node_modules/smol-toml/dist/util.js
|
|
2116
|
+
/*!
|
|
2117
|
+
* Copyright (c) Squirrel Chat et al., All rights reserved.
|
|
2118
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
2119
|
+
*
|
|
2120
|
+
* Redistribution and use in source and binary forms, with or without
|
|
2121
|
+
* modification, are permitted provided that the following conditions are met:
|
|
2122
|
+
*
|
|
2123
|
+
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
2124
|
+
* list of conditions and the following disclaimer.
|
|
2125
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
2126
|
+
* this list of conditions and the following disclaimer in the
|
|
2127
|
+
* documentation and/or other materials provided with the distribution.
|
|
2128
|
+
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
2129
|
+
* may be used to endorse or promote products derived from this software without
|
|
2130
|
+
* specific prior written permission.
|
|
2131
|
+
*
|
|
2132
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
2133
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
2134
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
2135
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
2136
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
2137
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
2138
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
2139
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
2140
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
2141
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
2142
|
+
*/
|
|
2143
|
+
function isEscaped(str, ptr) {
|
|
2144
|
+
let i = 0;
|
|
2145
|
+
while (str[ptr - ++i] === "\\")
|
|
2146
|
+
;
|
|
2147
|
+
return --i && i % 2;
|
|
2148
|
+
}
|
|
2149
|
+
function indexOfNewline(str, start = 0, end = str.length) {
|
|
2150
|
+
let idx = str.indexOf(`
|
|
2151
|
+
`, start);
|
|
2152
|
+
if (str[idx - 1] === "\r")
|
|
2153
|
+
idx--;
|
|
2154
|
+
return idx <= end ? idx : -1;
|
|
2155
|
+
}
|
|
2156
|
+
function skipComment(str, ptr) {
|
|
2157
|
+
for (let i = ptr;i < str.length; i++) {
|
|
2158
|
+
let c = str[i];
|
|
2159
|
+
if (c === `
|
|
2160
|
+
`)
|
|
2161
|
+
return i;
|
|
2162
|
+
if (c === "\r" && str[i + 1] === `
|
|
2163
|
+
`)
|
|
2164
|
+
return i + 1;
|
|
2165
|
+
if (c < " " && c !== "\t" || c === "\x7F") {
|
|
2166
|
+
throw new TomlError("control characters are not allowed in comments", {
|
|
2167
|
+
toml: str,
|
|
2168
|
+
ptr
|
|
2169
|
+
});
|
|
2170
|
+
}
|
|
2171
|
+
}
|
|
2172
|
+
return str.length;
|
|
2173
|
+
}
|
|
2174
|
+
function skipVoid(str, ptr, banNewLines, banComments) {
|
|
2175
|
+
let c;
|
|
2176
|
+
while ((c = str[ptr]) === " " || c === "\t" || !banNewLines && (c === `
|
|
2177
|
+
` || c === "\r" && str[ptr + 1] === `
|
|
2178
|
+
`))
|
|
2179
|
+
ptr++;
|
|
2180
|
+
return banComments || c !== "#" ? ptr : skipVoid(str, skipComment(str, ptr), banNewLines);
|
|
2181
|
+
}
|
|
2182
|
+
function skipUntil(str, ptr, sep, end, banNewLines = false) {
|
|
2183
|
+
if (!end) {
|
|
2184
|
+
ptr = indexOfNewline(str, ptr);
|
|
2185
|
+
return ptr < 0 ? str.length : ptr;
|
|
2186
|
+
}
|
|
2187
|
+
for (let i = ptr;i < str.length; i++) {
|
|
2188
|
+
let c = str[i];
|
|
2189
|
+
if (c === "#") {
|
|
2190
|
+
i = indexOfNewline(str, i);
|
|
2191
|
+
} else if (c === sep) {
|
|
2192
|
+
return i + 1;
|
|
2193
|
+
} else if (c === end || banNewLines && (c === `
|
|
2194
|
+
` || c === "\r" && str[i + 1] === `
|
|
2195
|
+
`)) {
|
|
2196
|
+
return i;
|
|
2197
|
+
}
|
|
2198
|
+
}
|
|
2199
|
+
throw new TomlError("cannot find end of structure", {
|
|
2200
|
+
toml: str,
|
|
2201
|
+
ptr
|
|
2202
|
+
});
|
|
2203
|
+
}
|
|
2204
|
+
function getStringEnd(str, seek) {
|
|
2205
|
+
let first = str[seek];
|
|
2206
|
+
let target = first === str[seek + 1] && str[seek + 1] === str[seek + 2] ? str.slice(seek, seek + 3) : first;
|
|
2207
|
+
seek += target.length - 1;
|
|
2208
|
+
do
|
|
2209
|
+
seek = str.indexOf(target, ++seek);
|
|
2210
|
+
while (seek > -1 && first !== "'" && isEscaped(str, seek));
|
|
2211
|
+
if (seek > -1) {
|
|
2212
|
+
seek += target.length;
|
|
2213
|
+
if (target.length > 1) {
|
|
2214
|
+
if (str[seek] === first)
|
|
2215
|
+
seek++;
|
|
2216
|
+
if (str[seek] === first)
|
|
2217
|
+
seek++;
|
|
2218
|
+
}
|
|
2219
|
+
}
|
|
2220
|
+
return seek;
|
|
2221
|
+
}
|
|
2222
|
+
|
|
2223
|
+
// node_modules/smol-toml/dist/date.js
|
|
2224
|
+
/*!
|
|
2225
|
+
* Copyright (c) Squirrel Chat et al., All rights reserved.
|
|
2226
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
2227
|
+
*
|
|
2228
|
+
* Redistribution and use in source and binary forms, with or without
|
|
2229
|
+
* modification, are permitted provided that the following conditions are met:
|
|
2230
|
+
*
|
|
2231
|
+
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
2232
|
+
* list of conditions and the following disclaimer.
|
|
2233
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
2234
|
+
* this list of conditions and the following disclaimer in the
|
|
2235
|
+
* documentation and/or other materials provided with the distribution.
|
|
2236
|
+
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
2237
|
+
* may be used to endorse or promote products derived from this software without
|
|
2238
|
+
* specific prior written permission.
|
|
2239
|
+
*
|
|
2240
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
2241
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
2242
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
2243
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
2244
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
2245
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
2246
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
2247
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
2248
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
2249
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
2250
|
+
*/
|
|
2251
|
+
var DATE_TIME_RE = /^(\d{4}-\d{2}-\d{2})?[T ]?(?:(\d{2}):\d{2}(?::\d{2}(?:\.\d+)?)?)?(Z|[-+]\d{2}:\d{2})?$/i;
|
|
2252
|
+
|
|
2253
|
+
class TomlDate extends Date {
|
|
2254
|
+
#hasDate = false;
|
|
2255
|
+
#hasTime = false;
|
|
2256
|
+
#offset = null;
|
|
2257
|
+
constructor(date) {
|
|
2258
|
+
let hasDate = true;
|
|
2259
|
+
let hasTime = true;
|
|
2260
|
+
let offset = "Z";
|
|
2261
|
+
if (typeof date === "string") {
|
|
2262
|
+
let match = date.match(DATE_TIME_RE);
|
|
2263
|
+
if (match) {
|
|
2264
|
+
if (!match[1]) {
|
|
2265
|
+
hasDate = false;
|
|
2266
|
+
date = `0000-01-01T${date}`;
|
|
2267
|
+
}
|
|
2268
|
+
hasTime = !!match[2];
|
|
2269
|
+
hasTime && date[10] === " " && (date = date.replace(" ", "T"));
|
|
2270
|
+
if (match[2] && +match[2] > 23) {
|
|
2271
|
+
date = "";
|
|
2272
|
+
} else {
|
|
2273
|
+
offset = match[3] || null;
|
|
2274
|
+
date = date.toUpperCase();
|
|
2275
|
+
if (!offset && hasTime)
|
|
2276
|
+
date += "Z";
|
|
2277
|
+
}
|
|
2278
|
+
} else {
|
|
2279
|
+
date = "";
|
|
2280
|
+
}
|
|
2281
|
+
}
|
|
2282
|
+
super(date);
|
|
2283
|
+
if (!isNaN(this.getTime())) {
|
|
2284
|
+
this.#hasDate = hasDate;
|
|
2285
|
+
this.#hasTime = hasTime;
|
|
2286
|
+
this.#offset = offset;
|
|
2287
|
+
}
|
|
2288
|
+
}
|
|
2289
|
+
isDateTime() {
|
|
2290
|
+
return this.#hasDate && this.#hasTime;
|
|
2291
|
+
}
|
|
2292
|
+
isLocal() {
|
|
2293
|
+
return !this.#hasDate || !this.#hasTime || !this.#offset;
|
|
2294
|
+
}
|
|
2295
|
+
isDate() {
|
|
2296
|
+
return this.#hasDate && !this.#hasTime;
|
|
2297
|
+
}
|
|
2298
|
+
isTime() {
|
|
2299
|
+
return this.#hasTime && !this.#hasDate;
|
|
2300
|
+
}
|
|
2301
|
+
isValid() {
|
|
2302
|
+
return this.#hasDate || this.#hasTime;
|
|
2303
|
+
}
|
|
2304
|
+
toISOString() {
|
|
2305
|
+
let iso = super.toISOString();
|
|
2306
|
+
if (this.isDate())
|
|
2307
|
+
return iso.slice(0, 10);
|
|
2308
|
+
if (this.isTime())
|
|
2309
|
+
return iso.slice(11, 23);
|
|
2310
|
+
if (this.#offset === null)
|
|
2311
|
+
return iso.slice(0, -1);
|
|
2312
|
+
if (this.#offset === "Z")
|
|
2313
|
+
return iso;
|
|
2314
|
+
let offset = +this.#offset.slice(1, 3) * 60 + +this.#offset.slice(4, 6);
|
|
2315
|
+
offset = this.#offset[0] === "-" ? offset : -offset;
|
|
2316
|
+
let offsetDate = new Date(this.getTime() - offset * 60000);
|
|
2317
|
+
return offsetDate.toISOString().slice(0, -1) + this.#offset;
|
|
2318
|
+
}
|
|
2319
|
+
static wrapAsOffsetDateTime(jsDate, offset = "Z") {
|
|
2320
|
+
let date = new TomlDate(jsDate);
|
|
2321
|
+
date.#offset = offset;
|
|
2322
|
+
return date;
|
|
2323
|
+
}
|
|
2324
|
+
static wrapAsLocalDateTime(jsDate) {
|
|
2325
|
+
let date = new TomlDate(jsDate);
|
|
2326
|
+
date.#offset = null;
|
|
2327
|
+
return date;
|
|
2328
|
+
}
|
|
2329
|
+
static wrapAsLocalDate(jsDate) {
|
|
2330
|
+
let date = new TomlDate(jsDate);
|
|
2331
|
+
date.#hasTime = false;
|
|
2332
|
+
date.#offset = null;
|
|
2333
|
+
return date;
|
|
2334
|
+
}
|
|
2335
|
+
static wrapAsLocalTime(jsDate) {
|
|
2336
|
+
let date = new TomlDate(jsDate);
|
|
2337
|
+
date.#hasDate = false;
|
|
2338
|
+
date.#offset = null;
|
|
2339
|
+
return date;
|
|
2340
|
+
}
|
|
2341
|
+
}
|
|
2342
|
+
|
|
2343
|
+
// node_modules/smol-toml/dist/primitive.js
|
|
2344
|
+
/*!
|
|
2345
|
+
* Copyright (c) Squirrel Chat et al., All rights reserved.
|
|
2346
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
2347
|
+
*
|
|
2348
|
+
* Redistribution and use in source and binary forms, with or without
|
|
2349
|
+
* modification, are permitted provided that the following conditions are met:
|
|
2350
|
+
*
|
|
2351
|
+
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
2352
|
+
* list of conditions and the following disclaimer.
|
|
2353
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
2354
|
+
* this list of conditions and the following disclaimer in the
|
|
2355
|
+
* documentation and/or other materials provided with the distribution.
|
|
2356
|
+
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
2357
|
+
* may be used to endorse or promote products derived from this software without
|
|
2358
|
+
* specific prior written permission.
|
|
2359
|
+
*
|
|
2360
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
2361
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
2362
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
2363
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
2364
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
2365
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
2366
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
2367
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
2368
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
2369
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
2370
|
+
*/
|
|
2371
|
+
var INT_REGEX = /^((0x[0-9a-fA-F](_?[0-9a-fA-F])*)|(([+-]|0[ob])?\d(_?\d)*))$/;
|
|
2372
|
+
var FLOAT_REGEX = /^[+-]?\d(_?\d)*(\.\d(_?\d)*)?([eE][+-]?\d(_?\d)*)?$/;
|
|
2373
|
+
var LEADING_ZERO = /^[+-]?0[0-9_]/;
|
|
2374
|
+
var ESCAPE_REGEX = /^[0-9a-f]{2,8}$/i;
|
|
2375
|
+
var ESC_MAP = {
|
|
2376
|
+
b: "\b",
|
|
2377
|
+
t: "\t",
|
|
2378
|
+
n: `
|
|
2379
|
+
`,
|
|
2380
|
+
f: "\f",
|
|
2381
|
+
r: "\r",
|
|
2382
|
+
e: "\x1B",
|
|
2383
|
+
'"': '"',
|
|
2384
|
+
"\\": "\\"
|
|
2385
|
+
};
|
|
2386
|
+
function parseString(str, ptr = 0, endPtr = str.length) {
|
|
2387
|
+
let isLiteral = str[ptr] === "'";
|
|
2388
|
+
let isMultiline = str[ptr++] === str[ptr] && str[ptr] === str[ptr + 1];
|
|
2389
|
+
if (isMultiline) {
|
|
2390
|
+
endPtr -= 2;
|
|
2391
|
+
if (str[ptr += 2] === "\r")
|
|
2392
|
+
ptr++;
|
|
2393
|
+
if (str[ptr] === `
|
|
2394
|
+
`)
|
|
2395
|
+
ptr++;
|
|
2396
|
+
}
|
|
2397
|
+
let tmp = 0;
|
|
2398
|
+
let isEscape;
|
|
2399
|
+
let parsed = "";
|
|
2400
|
+
let sliceStart = ptr;
|
|
2401
|
+
while (ptr < endPtr - 1) {
|
|
2402
|
+
let c = str[ptr++];
|
|
2403
|
+
if (c === `
|
|
2404
|
+
` || c === "\r" && str[ptr] === `
|
|
2405
|
+
`) {
|
|
2406
|
+
if (!isMultiline) {
|
|
2407
|
+
throw new TomlError("newlines are not allowed in strings", {
|
|
2408
|
+
toml: str,
|
|
2409
|
+
ptr: ptr - 1
|
|
2410
|
+
});
|
|
2411
|
+
}
|
|
2412
|
+
} else if (c < " " && c !== "\t" || c === "\x7F") {
|
|
2413
|
+
throw new TomlError("control characters are not allowed in strings", {
|
|
2414
|
+
toml: str,
|
|
2415
|
+
ptr: ptr - 1
|
|
2416
|
+
});
|
|
2417
|
+
}
|
|
2418
|
+
if (isEscape) {
|
|
2419
|
+
isEscape = false;
|
|
2420
|
+
if (c === "x" || c === "u" || c === "U") {
|
|
2421
|
+
let code = str.slice(ptr, ptr += c === "x" ? 2 : c === "u" ? 4 : 8);
|
|
2422
|
+
if (!ESCAPE_REGEX.test(code)) {
|
|
2423
|
+
throw new TomlError("invalid unicode escape", {
|
|
2424
|
+
toml: str,
|
|
2425
|
+
ptr: tmp
|
|
2426
|
+
});
|
|
2427
|
+
}
|
|
2428
|
+
try {
|
|
2429
|
+
parsed += String.fromCodePoint(parseInt(code, 16));
|
|
2430
|
+
} catch {
|
|
2431
|
+
throw new TomlError("invalid unicode escape", {
|
|
2432
|
+
toml: str,
|
|
2433
|
+
ptr: tmp
|
|
2434
|
+
});
|
|
2435
|
+
}
|
|
2436
|
+
} else if (isMultiline && (c === `
|
|
2437
|
+
` || c === " " || c === "\t" || c === "\r")) {
|
|
2438
|
+
ptr = skipVoid(str, ptr - 1, true);
|
|
2439
|
+
if (str[ptr] !== `
|
|
2440
|
+
` && str[ptr] !== "\r") {
|
|
2441
|
+
throw new TomlError("invalid escape: only line-ending whitespace may be escaped", {
|
|
2442
|
+
toml: str,
|
|
2443
|
+
ptr: tmp
|
|
2444
|
+
});
|
|
2445
|
+
}
|
|
2446
|
+
ptr = skipVoid(str, ptr);
|
|
2447
|
+
} else if (c in ESC_MAP) {
|
|
2448
|
+
parsed += ESC_MAP[c];
|
|
2449
|
+
} else {
|
|
2450
|
+
throw new TomlError("unrecognized escape sequence", {
|
|
2451
|
+
toml: str,
|
|
2452
|
+
ptr: tmp
|
|
2453
|
+
});
|
|
2454
|
+
}
|
|
2455
|
+
sliceStart = ptr;
|
|
2456
|
+
} else if (!isLiteral && c === "\\") {
|
|
2457
|
+
tmp = ptr - 1;
|
|
2458
|
+
isEscape = true;
|
|
2459
|
+
parsed += str.slice(sliceStart, tmp);
|
|
2460
|
+
}
|
|
2461
|
+
}
|
|
2462
|
+
return parsed + str.slice(sliceStart, endPtr - 1);
|
|
2463
|
+
}
|
|
2464
|
+
function parseValue(value, toml, ptr, integersAsBigInt) {
|
|
2465
|
+
if (value === "true")
|
|
2466
|
+
return true;
|
|
2467
|
+
if (value === "false")
|
|
2468
|
+
return false;
|
|
2469
|
+
if (value === "-inf")
|
|
2470
|
+
return -Infinity;
|
|
2471
|
+
if (value === "inf" || value === "+inf")
|
|
2472
|
+
return Infinity;
|
|
2473
|
+
if (value === "nan" || value === "+nan" || value === "-nan")
|
|
2474
|
+
return NaN;
|
|
2475
|
+
if (value === "-0")
|
|
2476
|
+
return integersAsBigInt ? 0n : 0;
|
|
2477
|
+
let isInt = INT_REGEX.test(value);
|
|
2478
|
+
if (isInt || FLOAT_REGEX.test(value)) {
|
|
2479
|
+
if (LEADING_ZERO.test(value)) {
|
|
2480
|
+
throw new TomlError("leading zeroes are not allowed", {
|
|
2481
|
+
toml,
|
|
2482
|
+
ptr
|
|
2483
|
+
});
|
|
2484
|
+
}
|
|
2485
|
+
value = value.replace(/_/g, "");
|
|
2486
|
+
let numeric = +value;
|
|
2487
|
+
if (isNaN(numeric)) {
|
|
2488
|
+
throw new TomlError("invalid number", {
|
|
2489
|
+
toml,
|
|
2490
|
+
ptr
|
|
2491
|
+
});
|
|
2492
|
+
}
|
|
2493
|
+
if (isInt) {
|
|
2494
|
+
if ((isInt = !Number.isSafeInteger(numeric)) && !integersAsBigInt) {
|
|
2495
|
+
throw new TomlError("integer value cannot be represented losslessly", {
|
|
2496
|
+
toml,
|
|
2497
|
+
ptr
|
|
2498
|
+
});
|
|
2499
|
+
}
|
|
2500
|
+
if (isInt || integersAsBigInt === true)
|
|
2501
|
+
numeric = BigInt(value);
|
|
2502
|
+
}
|
|
2503
|
+
return numeric;
|
|
2504
|
+
}
|
|
2505
|
+
const date = new TomlDate(value);
|
|
2506
|
+
if (!date.isValid()) {
|
|
2507
|
+
throw new TomlError("invalid value", {
|
|
2508
|
+
toml,
|
|
2509
|
+
ptr
|
|
2510
|
+
});
|
|
2511
|
+
}
|
|
2512
|
+
return date;
|
|
2513
|
+
}
|
|
2514
|
+
|
|
2515
|
+
// node_modules/smol-toml/dist/extract.js
|
|
2516
|
+
/*!
|
|
2517
|
+
* Copyright (c) Squirrel Chat et al., All rights reserved.
|
|
2518
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
2519
|
+
*
|
|
2520
|
+
* Redistribution and use in source and binary forms, with or without
|
|
2521
|
+
* modification, are permitted provided that the following conditions are met:
|
|
2522
|
+
*
|
|
2523
|
+
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
2524
|
+
* list of conditions and the following disclaimer.
|
|
2525
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
2526
|
+
* this list of conditions and the following disclaimer in the
|
|
2527
|
+
* documentation and/or other materials provided with the distribution.
|
|
2528
|
+
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
2529
|
+
* may be used to endorse or promote products derived from this software without
|
|
2530
|
+
* specific prior written permission.
|
|
2531
|
+
*
|
|
2532
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
2533
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
2534
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
2535
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
2536
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
2537
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
2538
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
2539
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
2540
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
2541
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
2542
|
+
*/
|
|
2543
|
+
function sliceAndTrimEndOf(str, startPtr, endPtr) {
|
|
2544
|
+
let value = str.slice(startPtr, endPtr);
|
|
2545
|
+
let commentIdx = value.indexOf("#");
|
|
2546
|
+
if (commentIdx > -1) {
|
|
2547
|
+
skipComment(str, commentIdx);
|
|
2548
|
+
value = value.slice(0, commentIdx);
|
|
2549
|
+
}
|
|
2550
|
+
return [value.trimEnd(), commentIdx];
|
|
2551
|
+
}
|
|
2552
|
+
function extractValue(str, ptr, end, depth, integersAsBigInt) {
|
|
2553
|
+
if (depth === 0) {
|
|
2554
|
+
throw new TomlError("document contains excessively nested structures. aborting.", {
|
|
2555
|
+
toml: str,
|
|
2556
|
+
ptr
|
|
2557
|
+
});
|
|
2558
|
+
}
|
|
2559
|
+
let c = str[ptr];
|
|
2560
|
+
if (c === "[" || c === "{") {
|
|
2561
|
+
let [value, endPtr2] = c === "[" ? parseArray(str, ptr, depth, integersAsBigInt) : parseInlineTable(str, ptr, depth, integersAsBigInt);
|
|
2562
|
+
if (end) {
|
|
2563
|
+
endPtr2 = skipVoid(str, endPtr2);
|
|
2564
|
+
if (str[endPtr2] === ",")
|
|
2565
|
+
endPtr2++;
|
|
2566
|
+
else if (str[endPtr2] !== end) {
|
|
2567
|
+
throw new TomlError("expected comma or end of structure", {
|
|
2568
|
+
toml: str,
|
|
2569
|
+
ptr: endPtr2
|
|
2570
|
+
});
|
|
2571
|
+
}
|
|
2572
|
+
}
|
|
2573
|
+
return [value, endPtr2];
|
|
2574
|
+
}
|
|
2575
|
+
let endPtr;
|
|
2576
|
+
if (c === '"' || c === "'") {
|
|
2577
|
+
endPtr = getStringEnd(str, ptr);
|
|
2578
|
+
let parsed = parseString(str, ptr, endPtr);
|
|
2579
|
+
if (end) {
|
|
2580
|
+
endPtr = skipVoid(str, endPtr);
|
|
2581
|
+
if (str[endPtr] && str[endPtr] !== "," && str[endPtr] !== end && str[endPtr] !== `
|
|
2582
|
+
` && str[endPtr] !== "\r") {
|
|
2583
|
+
throw new TomlError("unexpected character encountered", {
|
|
2584
|
+
toml: str,
|
|
2585
|
+
ptr: endPtr
|
|
2586
|
+
});
|
|
2587
|
+
}
|
|
2588
|
+
endPtr += +(str[endPtr] === ",");
|
|
2589
|
+
}
|
|
2590
|
+
return [parsed, endPtr];
|
|
2591
|
+
}
|
|
2592
|
+
endPtr = skipUntil(str, ptr, ",", end);
|
|
2593
|
+
let slice = sliceAndTrimEndOf(str, ptr, endPtr - +(str[endPtr - 1] === ","));
|
|
2594
|
+
if (!slice[0]) {
|
|
2595
|
+
throw new TomlError("incomplete key-value declaration: no value specified", {
|
|
2596
|
+
toml: str,
|
|
2597
|
+
ptr
|
|
2598
|
+
});
|
|
2599
|
+
}
|
|
2600
|
+
if (end && slice[1] > -1) {
|
|
2601
|
+
endPtr = skipVoid(str, ptr + slice[1]);
|
|
2602
|
+
endPtr += +(str[endPtr] === ",");
|
|
2603
|
+
}
|
|
2604
|
+
return [
|
|
2605
|
+
parseValue(slice[0], str, ptr, integersAsBigInt),
|
|
2606
|
+
endPtr
|
|
2607
|
+
];
|
|
2608
|
+
}
|
|
2609
|
+
|
|
2610
|
+
// node_modules/smol-toml/dist/struct.js
|
|
2611
|
+
/*!
|
|
2612
|
+
* Copyright (c) Squirrel Chat et al., All rights reserved.
|
|
2613
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
2614
|
+
*
|
|
2615
|
+
* Redistribution and use in source and binary forms, with or without
|
|
2616
|
+
* modification, are permitted provided that the following conditions are met:
|
|
2617
|
+
*
|
|
2618
|
+
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
2619
|
+
* list of conditions and the following disclaimer.
|
|
2620
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
2621
|
+
* this list of conditions and the following disclaimer in the
|
|
2622
|
+
* documentation and/or other materials provided with the distribution.
|
|
2623
|
+
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
2624
|
+
* may be used to endorse or promote products derived from this software without
|
|
2625
|
+
* specific prior written permission.
|
|
2626
|
+
*
|
|
2627
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
2628
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
2629
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
2630
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
2631
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
2632
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
2633
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
2634
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
2635
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
2636
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
2637
|
+
*/
|
|
2638
|
+
var KEY_PART_RE = /^[a-zA-Z0-9-_]+[ \t]*$/;
|
|
2639
|
+
function parseKey(str, ptr, end = "=") {
|
|
2640
|
+
let dot = ptr - 1;
|
|
2641
|
+
let parsed = [];
|
|
2642
|
+
let endPtr = str.indexOf(end, ptr);
|
|
2643
|
+
if (endPtr < 0) {
|
|
2644
|
+
throw new TomlError("incomplete key-value: cannot find end of key", {
|
|
2645
|
+
toml: str,
|
|
2646
|
+
ptr
|
|
2647
|
+
});
|
|
2648
|
+
}
|
|
2649
|
+
do {
|
|
2650
|
+
let c = str[ptr = ++dot];
|
|
2651
|
+
if (c !== " " && c !== "\t") {
|
|
2652
|
+
if (c === '"' || c === "'") {
|
|
2653
|
+
if (c === str[ptr + 1] && c === str[ptr + 2]) {
|
|
2654
|
+
throw new TomlError("multiline strings are not allowed in keys", {
|
|
2655
|
+
toml: str,
|
|
2656
|
+
ptr
|
|
2657
|
+
});
|
|
2658
|
+
}
|
|
2659
|
+
let eos = getStringEnd(str, ptr);
|
|
2660
|
+
if (eos < 0) {
|
|
2661
|
+
throw new TomlError("unfinished string encountered", {
|
|
2662
|
+
toml: str,
|
|
2663
|
+
ptr
|
|
2664
|
+
});
|
|
2665
|
+
}
|
|
2666
|
+
dot = str.indexOf(".", eos);
|
|
2667
|
+
let strEnd = str.slice(eos, dot < 0 || dot > endPtr ? endPtr : dot);
|
|
2668
|
+
let newLine = indexOfNewline(strEnd);
|
|
2669
|
+
if (newLine > -1) {
|
|
2670
|
+
throw new TomlError("newlines are not allowed in keys", {
|
|
2671
|
+
toml: str,
|
|
2672
|
+
ptr: ptr + dot + newLine
|
|
2673
|
+
});
|
|
2674
|
+
}
|
|
2675
|
+
if (strEnd.trimStart()) {
|
|
2676
|
+
throw new TomlError("found extra tokens after the string part", {
|
|
2677
|
+
toml: str,
|
|
2678
|
+
ptr: eos
|
|
2679
|
+
});
|
|
2680
|
+
}
|
|
2681
|
+
if (endPtr < eos) {
|
|
2682
|
+
endPtr = str.indexOf(end, eos);
|
|
2683
|
+
if (endPtr < 0) {
|
|
2684
|
+
throw new TomlError("incomplete key-value: cannot find end of key", {
|
|
2685
|
+
toml: str,
|
|
2686
|
+
ptr
|
|
2687
|
+
});
|
|
2688
|
+
}
|
|
2689
|
+
}
|
|
2690
|
+
parsed.push(parseString(str, ptr, eos));
|
|
2691
|
+
} else {
|
|
2692
|
+
dot = str.indexOf(".", ptr);
|
|
2693
|
+
let part = str.slice(ptr, dot < 0 || dot > endPtr ? endPtr : dot);
|
|
2694
|
+
if (!KEY_PART_RE.test(part)) {
|
|
2695
|
+
throw new TomlError("only letter, numbers, dashes and underscores are allowed in keys", {
|
|
2696
|
+
toml: str,
|
|
2697
|
+
ptr
|
|
2698
|
+
});
|
|
2699
|
+
}
|
|
2700
|
+
parsed.push(part.trimEnd());
|
|
2701
|
+
}
|
|
2702
|
+
}
|
|
2703
|
+
} while (dot + 1 && dot < endPtr);
|
|
2704
|
+
return [parsed, skipVoid(str, endPtr + 1, true, true)];
|
|
2705
|
+
}
|
|
2706
|
+
function parseInlineTable(str, ptr, depth, integersAsBigInt) {
|
|
2707
|
+
let res = {};
|
|
2708
|
+
let seen = new Set;
|
|
2709
|
+
let c;
|
|
2710
|
+
ptr++;
|
|
2711
|
+
while ((c = str[ptr++]) !== "}" && c) {
|
|
2712
|
+
if (c === ",") {
|
|
2713
|
+
throw new TomlError("expected value, found comma", {
|
|
2714
|
+
toml: str,
|
|
2715
|
+
ptr: ptr - 1
|
|
2716
|
+
});
|
|
2717
|
+
} else if (c === "#")
|
|
2718
|
+
ptr = skipComment(str, ptr);
|
|
2719
|
+
else if (c !== " " && c !== "\t" && c !== `
|
|
2720
|
+
` && c !== "\r") {
|
|
2721
|
+
let k;
|
|
2722
|
+
let t = res;
|
|
2723
|
+
let hasOwn = false;
|
|
2724
|
+
let [key, keyEndPtr] = parseKey(str, ptr - 1);
|
|
2725
|
+
for (let i = 0;i < key.length; i++) {
|
|
2726
|
+
if (i)
|
|
2727
|
+
t = hasOwn ? t[k] : t[k] = {};
|
|
2728
|
+
k = key[i];
|
|
2729
|
+
if ((hasOwn = Object.hasOwn(t, k)) && (typeof t[k] !== "object" || seen.has(t[k]))) {
|
|
2730
|
+
throw new TomlError("trying to redefine an already defined value", {
|
|
2731
|
+
toml: str,
|
|
2732
|
+
ptr
|
|
2733
|
+
});
|
|
2734
|
+
}
|
|
2735
|
+
if (!hasOwn && k === "__proto__") {
|
|
2736
|
+
Object.defineProperty(t, k, { enumerable: true, configurable: true, writable: true });
|
|
2737
|
+
}
|
|
2738
|
+
}
|
|
2739
|
+
if (hasOwn) {
|
|
2740
|
+
throw new TomlError("trying to redefine an already defined value", {
|
|
2741
|
+
toml: str,
|
|
2742
|
+
ptr
|
|
2743
|
+
});
|
|
2744
|
+
}
|
|
2745
|
+
let [value, valueEndPtr] = extractValue(str, keyEndPtr, "}", depth - 1, integersAsBigInt);
|
|
2746
|
+
seen.add(value);
|
|
2747
|
+
t[k] = value;
|
|
2748
|
+
ptr = valueEndPtr;
|
|
2749
|
+
}
|
|
2750
|
+
}
|
|
2751
|
+
if (!c) {
|
|
2752
|
+
throw new TomlError("unfinished table encountered", {
|
|
2753
|
+
toml: str,
|
|
2754
|
+
ptr
|
|
2755
|
+
});
|
|
2756
|
+
}
|
|
2757
|
+
return [res, ptr];
|
|
2758
|
+
}
|
|
2759
|
+
function parseArray(str, ptr, depth, integersAsBigInt) {
|
|
2760
|
+
let res = [];
|
|
2761
|
+
let c;
|
|
2762
|
+
ptr++;
|
|
2763
|
+
while ((c = str[ptr++]) !== "]" && c) {
|
|
2764
|
+
if (c === ",") {
|
|
2765
|
+
throw new TomlError("expected value, found comma", {
|
|
2766
|
+
toml: str,
|
|
2767
|
+
ptr: ptr - 1
|
|
2768
|
+
});
|
|
2769
|
+
} else if (c === "#")
|
|
2770
|
+
ptr = skipComment(str, ptr);
|
|
2771
|
+
else if (c !== " " && c !== "\t" && c !== `
|
|
2772
|
+
` && c !== "\r") {
|
|
2773
|
+
let e = extractValue(str, ptr - 1, "]", depth - 1, integersAsBigInt);
|
|
2774
|
+
res.push(e[0]);
|
|
2775
|
+
ptr = e[1];
|
|
2776
|
+
}
|
|
2777
|
+
}
|
|
2778
|
+
if (!c) {
|
|
2779
|
+
throw new TomlError("unfinished array encountered", {
|
|
2780
|
+
toml: str,
|
|
2781
|
+
ptr
|
|
2782
|
+
});
|
|
2783
|
+
}
|
|
2784
|
+
return [res, ptr];
|
|
2785
|
+
}
|
|
2786
|
+
|
|
2787
|
+
// node_modules/smol-toml/dist/parse.js
|
|
2788
|
+
/*!
|
|
2789
|
+
* Copyright (c) Squirrel Chat et al., All rights reserved.
|
|
2790
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
2791
|
+
*
|
|
2792
|
+
* Redistribution and use in source and binary forms, with or without
|
|
2793
|
+
* modification, are permitted provided that the following conditions are met:
|
|
2794
|
+
*
|
|
2795
|
+
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
2796
|
+
* list of conditions and the following disclaimer.
|
|
2797
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
2798
|
+
* this list of conditions and the following disclaimer in the
|
|
2799
|
+
* documentation and/or other materials provided with the distribution.
|
|
2800
|
+
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
2801
|
+
* may be used to endorse or promote products derived from this software without
|
|
2802
|
+
* specific prior written permission.
|
|
2803
|
+
*
|
|
2804
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
2805
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
2806
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
2807
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
2808
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
2809
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
2810
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
2811
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
2812
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
2813
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
2814
|
+
*/
|
|
2815
|
+
function peekTable(key, table, meta, type) {
|
|
2816
|
+
let t = table;
|
|
2817
|
+
let m = meta;
|
|
2818
|
+
let k;
|
|
2819
|
+
let hasOwn = false;
|
|
2820
|
+
let state;
|
|
2821
|
+
for (let i = 0;i < key.length; i++) {
|
|
2822
|
+
if (i) {
|
|
2823
|
+
t = hasOwn ? t[k] : t[k] = {};
|
|
2824
|
+
m = (state = m[k]).c;
|
|
2825
|
+
if (type === 0 && (state.t === 1 || state.t === 2)) {
|
|
2826
|
+
return null;
|
|
2827
|
+
}
|
|
2828
|
+
if (state.t === 2) {
|
|
2829
|
+
let l = t.length - 1;
|
|
2830
|
+
t = t[l];
|
|
2831
|
+
m = m[l].c;
|
|
2832
|
+
}
|
|
2833
|
+
}
|
|
2834
|
+
k = key[i];
|
|
2835
|
+
if ((hasOwn = Object.hasOwn(t, k)) && m[k]?.t === 0 && m[k]?.d) {
|
|
2836
|
+
return null;
|
|
2837
|
+
}
|
|
2838
|
+
if (!hasOwn) {
|
|
2839
|
+
if (k === "__proto__") {
|
|
2840
|
+
Object.defineProperty(t, k, { enumerable: true, configurable: true, writable: true });
|
|
2841
|
+
Object.defineProperty(m, k, { enumerable: true, configurable: true, writable: true });
|
|
2842
|
+
}
|
|
2843
|
+
m[k] = {
|
|
2844
|
+
t: i < key.length - 1 && type === 2 ? 3 : type,
|
|
2845
|
+
d: false,
|
|
2846
|
+
i: 0,
|
|
2847
|
+
c: {}
|
|
2848
|
+
};
|
|
2849
|
+
}
|
|
2850
|
+
}
|
|
2851
|
+
state = m[k];
|
|
2852
|
+
if (state.t !== type && !(type === 1 && state.t === 3)) {
|
|
2853
|
+
return null;
|
|
2854
|
+
}
|
|
2855
|
+
if (type === 2) {
|
|
2856
|
+
if (!state.d) {
|
|
2857
|
+
state.d = true;
|
|
2858
|
+
t[k] = [];
|
|
2859
|
+
}
|
|
2860
|
+
t[k].push(t = {});
|
|
2861
|
+
state.c[state.i++] = state = { t: 1, d: false, i: 0, c: {} };
|
|
2862
|
+
}
|
|
2863
|
+
if (state.d) {
|
|
2864
|
+
return null;
|
|
2865
|
+
}
|
|
2866
|
+
state.d = true;
|
|
2867
|
+
if (type === 1) {
|
|
2868
|
+
t = hasOwn ? t[k] : t[k] = {};
|
|
2869
|
+
} else if (type === 0 && hasOwn) {
|
|
2870
|
+
return null;
|
|
2871
|
+
}
|
|
2872
|
+
return [k, t, state.c];
|
|
2873
|
+
}
|
|
2874
|
+
function parse(toml, { maxDepth = 1000, integersAsBigInt } = {}) {
|
|
2875
|
+
let res = {};
|
|
2876
|
+
let meta = {};
|
|
2877
|
+
let tbl = res;
|
|
2878
|
+
let m = meta;
|
|
2879
|
+
for (let ptr = skipVoid(toml, 0);ptr < toml.length; ) {
|
|
2880
|
+
if (toml[ptr] === "[") {
|
|
2881
|
+
let isTableArray = toml[++ptr] === "[";
|
|
2882
|
+
let k = parseKey(toml, ptr += +isTableArray, "]");
|
|
2883
|
+
if (isTableArray) {
|
|
2884
|
+
if (toml[k[1] - 1] !== "]") {
|
|
2885
|
+
throw new TomlError("expected end of table declaration", {
|
|
2886
|
+
toml,
|
|
2887
|
+
ptr: k[1] - 1
|
|
2888
|
+
});
|
|
2889
|
+
}
|
|
2890
|
+
k[1]++;
|
|
2891
|
+
}
|
|
2892
|
+
let p = peekTable(k[0], res, meta, isTableArray ? 2 : 1);
|
|
2893
|
+
if (!p) {
|
|
2894
|
+
throw new TomlError("trying to redefine an already defined table or value", {
|
|
2895
|
+
toml,
|
|
2896
|
+
ptr
|
|
2897
|
+
});
|
|
2898
|
+
}
|
|
2899
|
+
m = p[2];
|
|
2900
|
+
tbl = p[1];
|
|
2901
|
+
ptr = k[1];
|
|
2902
|
+
} else {
|
|
2903
|
+
let k = parseKey(toml, ptr);
|
|
2904
|
+
let p = peekTable(k[0], tbl, m, 0);
|
|
2905
|
+
if (!p) {
|
|
2906
|
+
throw new TomlError("trying to redefine an already defined table or value", {
|
|
2907
|
+
toml,
|
|
2908
|
+
ptr
|
|
2909
|
+
});
|
|
2910
|
+
}
|
|
2911
|
+
let v = extractValue(toml, k[1], undefined, maxDepth, integersAsBigInt);
|
|
2912
|
+
p[1][p[0]] = v[0];
|
|
2913
|
+
ptr = v[1];
|
|
2914
|
+
}
|
|
2915
|
+
ptr = skipVoid(toml, ptr, true);
|
|
2916
|
+
if (toml[ptr] && toml[ptr] !== `
|
|
2917
|
+
` && toml[ptr] !== "\r") {
|
|
2918
|
+
throw new TomlError("each key-value declaration must be followed by an end-of-line", {
|
|
2919
|
+
toml,
|
|
2920
|
+
ptr
|
|
2921
|
+
});
|
|
2922
|
+
}
|
|
2923
|
+
ptr = skipVoid(toml, ptr);
|
|
2924
|
+
}
|
|
2925
|
+
return res;
|
|
2926
|
+
}
|
|
2927
|
+
|
|
2928
|
+
// node_modules/smol-toml/dist/stringify.js
|
|
2929
|
+
/*!
|
|
2930
|
+
* Copyright (c) Squirrel Chat et al., All rights reserved.
|
|
2931
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
2932
|
+
*
|
|
2933
|
+
* Redistribution and use in source and binary forms, with or without
|
|
2934
|
+
* modification, are permitted provided that the following conditions are met:
|
|
2935
|
+
*
|
|
2936
|
+
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
2937
|
+
* list of conditions and the following disclaimer.
|
|
2938
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
2939
|
+
* this list of conditions and the following disclaimer in the
|
|
2940
|
+
* documentation and/or other materials provided with the distribution.
|
|
2941
|
+
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
2942
|
+
* may be used to endorse or promote products derived from this software without
|
|
2943
|
+
* specific prior written permission.
|
|
2944
|
+
*
|
|
2945
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
2946
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
2947
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
2948
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
2949
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
2950
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
2951
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
2952
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
2953
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
2954
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
2955
|
+
*/
|
|
2956
|
+
var BARE_KEY = /^[a-z0-9-_]+$/i;
|
|
2957
|
+
function extendedTypeOf(obj) {
|
|
2958
|
+
let type = typeof obj;
|
|
2959
|
+
if (type === "object") {
|
|
2960
|
+
if (Array.isArray(obj))
|
|
2961
|
+
return "array";
|
|
2962
|
+
if (obj instanceof Date)
|
|
2963
|
+
return "date";
|
|
2964
|
+
}
|
|
2965
|
+
return type;
|
|
2966
|
+
}
|
|
2967
|
+
function isArrayOfTables(obj) {
|
|
2968
|
+
for (let i = 0;i < obj.length; i++) {
|
|
2969
|
+
if (extendedTypeOf(obj[i]) !== "object")
|
|
2970
|
+
return false;
|
|
2971
|
+
}
|
|
2972
|
+
return obj.length != 0;
|
|
2973
|
+
}
|
|
2974
|
+
function formatString(s) {
|
|
2975
|
+
return JSON.stringify(s).replace(/\x7f/g, "\\u007f");
|
|
2976
|
+
}
|
|
2977
|
+
function stringifyValue(val, type, depth, numberAsFloat) {
|
|
2978
|
+
if (depth === 0) {
|
|
2979
|
+
throw new Error("Could not stringify the object: maximum object depth exceeded");
|
|
2980
|
+
}
|
|
2981
|
+
if (type === "number") {
|
|
2982
|
+
if (isNaN(val))
|
|
2983
|
+
return "nan";
|
|
2984
|
+
if (val === Infinity)
|
|
2985
|
+
return "inf";
|
|
2986
|
+
if (val === -Infinity)
|
|
2987
|
+
return "-inf";
|
|
2988
|
+
if (numberAsFloat && Number.isInteger(val))
|
|
2989
|
+
return val.toFixed(1);
|
|
2990
|
+
return val.toString();
|
|
2991
|
+
}
|
|
2992
|
+
if (type === "bigint" || type === "boolean") {
|
|
2993
|
+
return val.toString();
|
|
2994
|
+
}
|
|
2995
|
+
if (type === "string") {
|
|
2996
|
+
return formatString(val);
|
|
2997
|
+
}
|
|
2998
|
+
if (type === "date") {
|
|
2999
|
+
if (isNaN(val.getTime())) {
|
|
3000
|
+
throw new TypeError("cannot serialize invalid date");
|
|
3001
|
+
}
|
|
3002
|
+
return val.toISOString();
|
|
3003
|
+
}
|
|
3004
|
+
if (type === "object") {
|
|
3005
|
+
return stringifyInlineTable(val, depth, numberAsFloat);
|
|
3006
|
+
}
|
|
3007
|
+
if (type === "array") {
|
|
3008
|
+
return stringifyArray(val, depth, numberAsFloat);
|
|
3009
|
+
}
|
|
3010
|
+
}
|
|
3011
|
+
function stringifyInlineTable(obj, depth, numberAsFloat) {
|
|
3012
|
+
let keys = Object.keys(obj);
|
|
3013
|
+
if (keys.length === 0)
|
|
3014
|
+
return "{}";
|
|
3015
|
+
let res = "{ ";
|
|
3016
|
+
for (let i = 0;i < keys.length; i++) {
|
|
3017
|
+
let k = keys[i];
|
|
3018
|
+
if (i)
|
|
3019
|
+
res += ", ";
|
|
3020
|
+
res += BARE_KEY.test(k) ? k : formatString(k);
|
|
3021
|
+
res += " = ";
|
|
3022
|
+
res += stringifyValue(obj[k], extendedTypeOf(obj[k]), depth - 1, numberAsFloat);
|
|
3023
|
+
}
|
|
3024
|
+
return res + " }";
|
|
3025
|
+
}
|
|
3026
|
+
function stringifyArray(array, depth, numberAsFloat) {
|
|
3027
|
+
if (array.length === 0)
|
|
3028
|
+
return "[]";
|
|
3029
|
+
let res = "[ ";
|
|
3030
|
+
for (let i = 0;i < array.length; i++) {
|
|
3031
|
+
if (i)
|
|
3032
|
+
res += ", ";
|
|
3033
|
+
if (array[i] === null || array[i] === undefined) {
|
|
3034
|
+
throw new TypeError("arrays cannot contain null or undefined values");
|
|
3035
|
+
}
|
|
3036
|
+
res += stringifyValue(array[i], extendedTypeOf(array[i]), depth - 1, numberAsFloat);
|
|
3037
|
+
}
|
|
3038
|
+
return res + " ]";
|
|
3039
|
+
}
|
|
3040
|
+
function stringifyArrayTable(array, key, depth, numberAsFloat) {
|
|
3041
|
+
if (depth === 0) {
|
|
3042
|
+
throw new Error("Could not stringify the object: maximum object depth exceeded");
|
|
3043
|
+
}
|
|
3044
|
+
let res = "";
|
|
3045
|
+
for (let i = 0;i < array.length; i++) {
|
|
3046
|
+
res += `${res && `
|
|
3047
|
+
`}[[${key}]]
|
|
3048
|
+
`;
|
|
3049
|
+
res += stringifyTable(0, array[i], key, depth, numberAsFloat);
|
|
3050
|
+
}
|
|
3051
|
+
return res;
|
|
3052
|
+
}
|
|
3053
|
+
function stringifyTable(tableKey, obj, prefix, depth, numberAsFloat) {
|
|
3054
|
+
if (depth === 0) {
|
|
3055
|
+
throw new Error("Could not stringify the object: maximum object depth exceeded");
|
|
3056
|
+
}
|
|
3057
|
+
let preamble = "";
|
|
3058
|
+
let tables = "";
|
|
3059
|
+
let keys = Object.keys(obj);
|
|
3060
|
+
for (let i = 0;i < keys.length; i++) {
|
|
3061
|
+
let k = keys[i];
|
|
3062
|
+
if (obj[k] !== null && obj[k] !== undefined) {
|
|
3063
|
+
let type = extendedTypeOf(obj[k]);
|
|
3064
|
+
if (type === "symbol" || type === "function") {
|
|
3065
|
+
throw new TypeError(`cannot serialize values of type '${type}'`);
|
|
3066
|
+
}
|
|
3067
|
+
let key = BARE_KEY.test(k) ? k : formatString(k);
|
|
3068
|
+
if (type === "array" && isArrayOfTables(obj[k])) {
|
|
3069
|
+
tables += (tables && `
|
|
3070
|
+
`) + stringifyArrayTable(obj[k], prefix ? `${prefix}.${key}` : key, depth - 1, numberAsFloat);
|
|
3071
|
+
} else if (type === "object") {
|
|
3072
|
+
let tblKey = prefix ? `${prefix}.${key}` : key;
|
|
3073
|
+
tables += (tables && `
|
|
3074
|
+
`) + stringifyTable(tblKey, obj[k], tblKey, depth - 1, numberAsFloat);
|
|
3075
|
+
} else {
|
|
3076
|
+
preamble += key;
|
|
3077
|
+
preamble += " = ";
|
|
3078
|
+
preamble += stringifyValue(obj[k], type, depth, numberAsFloat);
|
|
3079
|
+
preamble += `
|
|
3080
|
+
`;
|
|
3081
|
+
}
|
|
3082
|
+
}
|
|
3083
|
+
}
|
|
3084
|
+
if (tableKey && (preamble || !tables))
|
|
3085
|
+
preamble = preamble ? `[${tableKey}]
|
|
3086
|
+
${preamble}` : `[${tableKey}]`;
|
|
3087
|
+
return preamble && tables ? `${preamble}
|
|
3088
|
+
${tables}` : preamble || tables;
|
|
3089
|
+
}
|
|
3090
|
+
function stringify(obj, { maxDepth = 1000, numbersAsFloat = false } = {}) {
|
|
3091
|
+
if (extendedTypeOf(obj) !== "object") {
|
|
3092
|
+
throw new TypeError("stringify can only be called with an object");
|
|
3093
|
+
}
|
|
3094
|
+
let str = stringifyTable(0, obj, "", maxDepth, numbersAsFloat);
|
|
3095
|
+
if (str[str.length - 1] !== `
|
|
3096
|
+
`)
|
|
3097
|
+
return str + `
|
|
3098
|
+
`;
|
|
3099
|
+
return str;
|
|
3100
|
+
}
|
|
3101
|
+
|
|
3102
|
+
// node_modules/smol-toml/dist/index.js
|
|
3103
|
+
/*!
|
|
3104
|
+
* Copyright (c) Squirrel Chat et al., All rights reserved.
|
|
3105
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
3106
|
+
*
|
|
3107
|
+
* Redistribution and use in source and binary forms, with or without
|
|
3108
|
+
* modification, are permitted provided that the following conditions are met:
|
|
3109
|
+
*
|
|
3110
|
+
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
3111
|
+
* list of conditions and the following disclaimer.
|
|
3112
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
3113
|
+
* this list of conditions and the following disclaimer in the
|
|
3114
|
+
* documentation and/or other materials provided with the distribution.
|
|
3115
|
+
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
3116
|
+
* may be used to endorse or promote products derived from this software without
|
|
3117
|
+
* specific prior written permission.
|
|
3118
|
+
*
|
|
3119
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
3120
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
3121
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
3122
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
3123
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
3124
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
3125
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
3126
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
3127
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
3128
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
3129
|
+
*/
|
|
3130
|
+
|
|
3131
|
+
// src/core/frontmatter.ts
|
|
3132
|
+
var FRONTMATTER_DELIMITER = "+++";
|
|
3133
|
+
function parseStitchFile(content) {
|
|
3134
|
+
const lines = content.split(`
|
|
3135
|
+
`);
|
|
3136
|
+
if (lines[0] !== FRONTMATTER_DELIMITER) {
|
|
3137
|
+
throw new ValidationError("Invalid stitch file: missing frontmatter start");
|
|
3138
|
+
}
|
|
3139
|
+
let endIndex = -1;
|
|
3140
|
+
for (let i = 1;i < lines.length; i++) {
|
|
3141
|
+
if (lines[i] === FRONTMATTER_DELIMITER) {
|
|
3142
|
+
endIndex = i;
|
|
3143
|
+
break;
|
|
3144
|
+
}
|
|
3145
|
+
}
|
|
3146
|
+
if (endIndex === -1) {
|
|
3147
|
+
throw new ValidationError("Invalid stitch file: missing frontmatter end");
|
|
3148
|
+
}
|
|
3149
|
+
const frontmatterContent = lines.slice(1, endIndex).join(`
|
|
3150
|
+
`);
|
|
3151
|
+
const body = lines.slice(endIndex + 1).join(`
|
|
3152
|
+
`).trim();
|
|
3153
|
+
const parsed = parse(frontmatterContent);
|
|
3154
|
+
const frontmatter = toStitchFrontmatter(parsed);
|
|
3155
|
+
return { frontmatter, body };
|
|
3156
|
+
}
|
|
3157
|
+
function toStitchFrontmatter(obj) {
|
|
3158
|
+
if (typeof obj["id"] !== "string") {
|
|
3159
|
+
throw new ValidationError("Missing or invalid 'id' field");
|
|
3160
|
+
}
|
|
3161
|
+
if (typeof obj["title"] !== "string") {
|
|
3162
|
+
throw new ValidationError("Missing or invalid 'title' field");
|
|
3163
|
+
}
|
|
3164
|
+
if (typeof obj["status"] !== "string") {
|
|
3165
|
+
throw new ValidationError("Missing or invalid 'status' field");
|
|
3166
|
+
}
|
|
3167
|
+
if (typeof obj["created_at"] !== "string") {
|
|
3168
|
+
throw new ValidationError("Missing or invalid 'created_at' field");
|
|
3169
|
+
}
|
|
3170
|
+
if (typeof obj["updated_at"] !== "string") {
|
|
3171
|
+
throw new ValidationError("Missing or invalid 'updated_at' field");
|
|
3172
|
+
}
|
|
3173
|
+
const frontmatter = {
|
|
3174
|
+
id: obj["id"],
|
|
3175
|
+
title: obj["title"],
|
|
3176
|
+
status: obj["status"],
|
|
3177
|
+
created_at: obj["created_at"],
|
|
3178
|
+
updated_at: obj["updated_at"]
|
|
3179
|
+
};
|
|
3180
|
+
if (typeof obj["provenance"] === "string") {
|
|
3181
|
+
frontmatter.provenance = obj["provenance"];
|
|
3182
|
+
}
|
|
3183
|
+
if (typeof obj["confidence"] === "string") {
|
|
3184
|
+
frontmatter.confidence = obj["confidence"];
|
|
3185
|
+
}
|
|
3186
|
+
if (Array.isArray(obj["tags"])) {
|
|
3187
|
+
frontmatter.tags = obj["tags"];
|
|
3188
|
+
}
|
|
3189
|
+
const scope = obj["scope"];
|
|
3190
|
+
if (scope && typeof scope === "object" && !Array.isArray(scope)) {
|
|
3191
|
+
const scopeObj = scope;
|
|
3192
|
+
frontmatter.scope = {};
|
|
3193
|
+
if (Array.isArray(scopeObj["paths"])) {
|
|
3194
|
+
frontmatter.scope.paths = scopeObj["paths"];
|
|
3195
|
+
}
|
|
3196
|
+
}
|
|
3197
|
+
const relations = obj["relations"];
|
|
3198
|
+
if (relations && typeof relations === "object" && !Array.isArray(relations)) {
|
|
3199
|
+
const relObj = relations;
|
|
3200
|
+
frontmatter.relations = {};
|
|
3201
|
+
if (typeof relObj["parent"] === "string") {
|
|
3202
|
+
frontmatter.relations.parent = relObj["parent"];
|
|
3203
|
+
}
|
|
3204
|
+
if (Array.isArray(relObj["depends_on"])) {
|
|
3205
|
+
frontmatter.relations.depends_on = relObj["depends_on"];
|
|
3206
|
+
}
|
|
3207
|
+
}
|
|
3208
|
+
const git = obj["git"];
|
|
3209
|
+
if (git && typeof git === "object" && !Array.isArray(git)) {
|
|
3210
|
+
const gitObj = git;
|
|
3211
|
+
frontmatter.git = {};
|
|
3212
|
+
if (Array.isArray(gitObj["links"])) {
|
|
3213
|
+
frontmatter.git.links = gitObj["links"].map(parseGitLink);
|
|
3214
|
+
}
|
|
3215
|
+
if (Array.isArray(gitObj["fingerprints"])) {
|
|
3216
|
+
frontmatter.git.fingerprints = gitObj["fingerprints"].map(parseFingerprint);
|
|
3217
|
+
}
|
|
3218
|
+
}
|
|
3219
|
+
return frontmatter;
|
|
3220
|
+
}
|
|
3221
|
+
function parseGitLink(obj) {
|
|
3222
|
+
if (obj["kind"] === "commit" && typeof obj["sha"] === "string") {
|
|
3223
|
+
return { kind: "commit", sha: obj["sha"] };
|
|
3224
|
+
}
|
|
3225
|
+
if (obj["kind"] === "range" && typeof obj["range"] === "string") {
|
|
3226
|
+
return { kind: "range", range: obj["range"] };
|
|
3227
|
+
}
|
|
3228
|
+
throw new ValidationError(`Invalid git link: ${JSON.stringify(obj)}`);
|
|
3229
|
+
}
|
|
3230
|
+
function parseFingerprint(obj) {
|
|
3231
|
+
if (obj["algo"] === "sha256" && (obj["kind"] === "staged-diff" || obj["kind"] === "unified-diff") && typeof obj["value"] === "string") {
|
|
3232
|
+
return {
|
|
3233
|
+
algo: obj["algo"],
|
|
3234
|
+
kind: obj["kind"],
|
|
3235
|
+
value: obj["value"]
|
|
3236
|
+
};
|
|
3237
|
+
}
|
|
3238
|
+
throw new ValidationError(`Invalid fingerprint: ${JSON.stringify(obj)}`);
|
|
3239
|
+
}
|
|
3240
|
+
function serializeStitchFile(frontmatter, body) {
|
|
3241
|
+
const tomlObj = frontmatterToToml(frontmatter);
|
|
3242
|
+
const tomlStr = stringify(tomlObj);
|
|
3243
|
+
return `${FRONTMATTER_DELIMITER}
|
|
3244
|
+
${tomlStr}${FRONTMATTER_DELIMITER}
|
|
3245
|
+
|
|
3246
|
+
${body}
|
|
3247
|
+
`;
|
|
3248
|
+
}
|
|
3249
|
+
function frontmatterToToml(fm) {
|
|
3250
|
+
const obj = {
|
|
3251
|
+
id: fm.id,
|
|
3252
|
+
title: fm.title,
|
|
3253
|
+
status: fm.status,
|
|
3254
|
+
created_at: fm.created_at,
|
|
3255
|
+
updated_at: fm.updated_at
|
|
3256
|
+
};
|
|
3257
|
+
if (fm.provenance)
|
|
3258
|
+
obj["provenance"] = fm.provenance;
|
|
3259
|
+
if (fm.confidence)
|
|
3260
|
+
obj["confidence"] = fm.confidence;
|
|
3261
|
+
if (fm.tags && fm.tags.length > 0)
|
|
3262
|
+
obj["tags"] = fm.tags;
|
|
3263
|
+
if (fm.scope && Object.keys(fm.scope).length > 0) {
|
|
3264
|
+
obj["scope"] = fm.scope;
|
|
3265
|
+
}
|
|
3266
|
+
if (fm.relations) {
|
|
3267
|
+
const relations = {};
|
|
3268
|
+
if (fm.relations.parent)
|
|
3269
|
+
relations["parent"] = fm.relations.parent;
|
|
3270
|
+
if (fm.relations.depends_on && fm.relations.depends_on.length > 0) {
|
|
3271
|
+
relations["depends_on"] = fm.relations.depends_on;
|
|
3272
|
+
}
|
|
3273
|
+
if (Object.keys(relations).length > 0) {
|
|
3274
|
+
obj["relations"] = relations;
|
|
3275
|
+
}
|
|
3276
|
+
}
|
|
3277
|
+
if (fm.git) {
|
|
3278
|
+
const git = {};
|
|
3279
|
+
if (fm.git.links && fm.git.links.length > 0) {
|
|
3280
|
+
git["links"] = fm.git.links;
|
|
3281
|
+
}
|
|
3282
|
+
if (fm.git.fingerprints && fm.git.fingerprints.length > 0) {
|
|
3283
|
+
git["fingerprints"] = fm.git.fingerprints;
|
|
3284
|
+
}
|
|
3285
|
+
if (Object.keys(git).length > 0) {
|
|
3286
|
+
obj["git"] = git;
|
|
3287
|
+
}
|
|
3288
|
+
}
|
|
3289
|
+
return obj;
|
|
3290
|
+
}
|
|
3291
|
+
function updateTimestamp(frontmatter) {
|
|
3292
|
+
return {
|
|
3293
|
+
...frontmatter,
|
|
3294
|
+
updated_at: new Date().toISOString()
|
|
3295
|
+
};
|
|
3296
|
+
}
|
|
3297
|
+
|
|
3298
|
+
// src/core/ids.ts
|
|
3299
|
+
function generateStitchId() {
|
|
3300
|
+
const now = new Date;
|
|
3301
|
+
const dateStr = formatDate(now);
|
|
3302
|
+
const suffix = generateHexSuffix(4);
|
|
3303
|
+
return `S-${dateStr}-${suffix}`;
|
|
3304
|
+
}
|
|
3305
|
+
function formatDate(date) {
|
|
3306
|
+
const year = date.getFullYear();
|
|
3307
|
+
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
3308
|
+
const day = String(date.getDate()).padStart(2, "0");
|
|
3309
|
+
return `${year}${month}${day}`;
|
|
3310
|
+
}
|
|
3311
|
+
function generateHexSuffix(length) {
|
|
3312
|
+
const bytes = new Uint8Array(Math.ceil(length / 2));
|
|
3313
|
+
crypto.getRandomValues(bytes);
|
|
3314
|
+
return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("").slice(0, length);
|
|
3315
|
+
}
|
|
3316
|
+
|
|
3317
|
+
// src/core/model.ts
|
|
3318
|
+
var DEFAULT_STITCH_BODY = `## Intent
|
|
3319
|
+
|
|
3320
|
+
[Describe the goal or purpose of this change]
|
|
3321
|
+
|
|
3322
|
+
## Constraints
|
|
3323
|
+
|
|
3324
|
+
- [List any constraints or requirements]
|
|
3325
|
+
|
|
3326
|
+
## Alternatives
|
|
3327
|
+
|
|
3328
|
+
- [Document alternative approaches considered]
|
|
3329
|
+
|
|
3330
|
+
## Notes
|
|
3331
|
+
|
|
3332
|
+
[Additional context or information]
|
|
3333
|
+
`;
|
|
3334
|
+
|
|
3335
|
+
// src/core/store.ts
|
|
3336
|
+
var STITCH_DIR = ".stitch";
|
|
3337
|
+
var STITCHES_SUBDIR = "stitches";
|
|
3338
|
+
var CURRENT_FILE = "current";
|
|
3339
|
+
function getStitchDir(repoRoot) {
|
|
3340
|
+
return join(repoRoot, STITCH_DIR);
|
|
3341
|
+
}
|
|
3342
|
+
function getStitchesDir(repoRoot) {
|
|
3343
|
+
return join(repoRoot, STITCH_DIR, STITCHES_SUBDIR);
|
|
3344
|
+
}
|
|
3345
|
+
function getCurrentFilePath(repoRoot) {
|
|
3346
|
+
return join(repoRoot, STITCH_DIR, CURRENT_FILE);
|
|
3347
|
+
}
|
|
3348
|
+
function isInitialized(repoRoot) {
|
|
3349
|
+
return existsSync(getStitchDir(repoRoot));
|
|
3350
|
+
}
|
|
3351
|
+
async function initializeStitch(repoRoot) {
|
|
3352
|
+
const stitchesDir = getStitchesDir(repoRoot);
|
|
3353
|
+
const currentPath = getCurrentFilePath(repoRoot);
|
|
3354
|
+
await mkdir(stitchesDir, { recursive: true });
|
|
3355
|
+
if (!existsSync(currentPath)) {
|
|
3356
|
+
await writeFile(currentPath, "", "utf-8");
|
|
3357
|
+
}
|
|
3358
|
+
}
|
|
3359
|
+
async function getCurrentStitchId(repoRoot) {
|
|
3360
|
+
if (!isInitialized(repoRoot)) {
|
|
3361
|
+
throw new NotInitializedError;
|
|
3362
|
+
}
|
|
3363
|
+
const currentPath = getCurrentFilePath(repoRoot);
|
|
3364
|
+
const content = await readFile(currentPath, "utf-8");
|
|
3365
|
+
const trimmed = content.trim();
|
|
3366
|
+
return trimmed || null;
|
|
3367
|
+
}
|
|
3368
|
+
async function setCurrentStitchId(repoRoot, id) {
|
|
3369
|
+
if (!isInitialized(repoRoot)) {
|
|
3370
|
+
throw new NotInitializedError;
|
|
3371
|
+
}
|
|
3372
|
+
const currentPath = getCurrentFilePath(repoRoot);
|
|
3373
|
+
await writeFile(currentPath, id ?? "", "utf-8");
|
|
3374
|
+
}
|
|
3375
|
+
function getStitchFilePath(repoRoot, id) {
|
|
3376
|
+
return join(getStitchesDir(repoRoot), `${id}.md`);
|
|
3377
|
+
}
|
|
3378
|
+
async function createStitch(repoRoot, title, parentId) {
|
|
3379
|
+
if (!isInitialized(repoRoot)) {
|
|
3380
|
+
throw new NotInitializedError;
|
|
3381
|
+
}
|
|
3382
|
+
const id = generateStitchId();
|
|
3383
|
+
const now = new Date().toISOString();
|
|
3384
|
+
const frontmatter = {
|
|
3385
|
+
id,
|
|
3386
|
+
title,
|
|
3387
|
+
status: "open",
|
|
3388
|
+
created_at: now,
|
|
3389
|
+
updated_at: now,
|
|
3390
|
+
provenance: "human",
|
|
3391
|
+
confidence: "medium"
|
|
3392
|
+
};
|
|
3393
|
+
if (parentId) {
|
|
3394
|
+
frontmatter.relations = { parent: parentId };
|
|
3395
|
+
}
|
|
3396
|
+
const filePath = getStitchFilePath(repoRoot, id);
|
|
3397
|
+
const content = serializeStitchFile(frontmatter, DEFAULT_STITCH_BODY);
|
|
3398
|
+
await writeFile(filePath, content, "utf-8");
|
|
3399
|
+
return {
|
|
3400
|
+
frontmatter,
|
|
3401
|
+
body: DEFAULT_STITCH_BODY,
|
|
3402
|
+
filePath
|
|
3403
|
+
};
|
|
3404
|
+
}
|
|
3405
|
+
async function loadStitch(repoRoot, id) {
|
|
3406
|
+
if (!isInitialized(repoRoot)) {
|
|
3407
|
+
throw new NotInitializedError;
|
|
3408
|
+
}
|
|
3409
|
+
const filePath = getStitchFilePath(repoRoot, id);
|
|
3410
|
+
if (!existsSync(filePath)) {
|
|
3411
|
+
throw new StitchNotFoundError(id);
|
|
3412
|
+
}
|
|
3413
|
+
const content = await readFile(filePath, "utf-8");
|
|
3414
|
+
const { frontmatter, body } = parseStitchFile(content);
|
|
3415
|
+
return { frontmatter, body, filePath };
|
|
3416
|
+
}
|
|
3417
|
+
async function saveStitch(repoRoot, doc) {
|
|
3418
|
+
if (!isInitialized(repoRoot)) {
|
|
3419
|
+
throw new NotInitializedError;
|
|
3420
|
+
}
|
|
3421
|
+
const updatedFrontmatter = updateTimestamp(doc.frontmatter);
|
|
3422
|
+
const content = serializeStitchFile(updatedFrontmatter, doc.body);
|
|
3423
|
+
await writeFile(doc.filePath, content, "utf-8");
|
|
3424
|
+
return {
|
|
3425
|
+
...doc,
|
|
3426
|
+
frontmatter: updatedFrontmatter
|
|
3427
|
+
};
|
|
3428
|
+
}
|
|
3429
|
+
async function listStitches(repoRoot, filter) {
|
|
3430
|
+
if (!isInitialized(repoRoot)) {
|
|
3431
|
+
throw new NotInitializedError;
|
|
3432
|
+
}
|
|
3433
|
+
const stitchesDir = getStitchesDir(repoRoot);
|
|
3434
|
+
if (!existsSync(stitchesDir)) {
|
|
3435
|
+
return [];
|
|
3436
|
+
}
|
|
3437
|
+
const files = await readdir(stitchesDir);
|
|
3438
|
+
const mdFiles = files.filter((f) => f.endsWith(".md"));
|
|
3439
|
+
const docs = [];
|
|
3440
|
+
for (const file of mdFiles) {
|
|
3441
|
+
const filePath = join(stitchesDir, file);
|
|
3442
|
+
try {
|
|
3443
|
+
const content = await readFile(filePath, "utf-8");
|
|
3444
|
+
const { frontmatter, body } = parseStitchFile(content);
|
|
3445
|
+
if (filter?.status && frontmatter.status !== filter.status) {
|
|
3446
|
+
continue;
|
|
3447
|
+
}
|
|
3448
|
+
docs.push({ frontmatter, body, filePath });
|
|
3449
|
+
} catch {
|
|
3450
|
+
continue;
|
|
3451
|
+
}
|
|
3452
|
+
}
|
|
3453
|
+
docs.sort((a, b) => new Date(b.frontmatter.updated_at).getTime() - new Date(a.frontmatter.updated_at).getTime());
|
|
3454
|
+
return docs;
|
|
3455
|
+
}
|
|
3456
|
+
async function getLineage(repoRoot, id) {
|
|
3457
|
+
const lineage = [];
|
|
3458
|
+
let currentId = id;
|
|
3459
|
+
const visited = new Set;
|
|
3460
|
+
while (currentId) {
|
|
3461
|
+
if (visited.has(currentId)) {
|
|
3462
|
+
break;
|
|
3463
|
+
}
|
|
3464
|
+
visited.add(currentId);
|
|
3465
|
+
try {
|
|
3466
|
+
const doc = await loadStitch(repoRoot, currentId);
|
|
3467
|
+
lineage.push(currentId);
|
|
3468
|
+
currentId = doc.frontmatter.relations?.parent;
|
|
3469
|
+
} catch {
|
|
3470
|
+
break;
|
|
3471
|
+
}
|
|
3472
|
+
}
|
|
3473
|
+
return lineage;
|
|
3474
|
+
}
|
|
3475
|
+
async function requireCurrentStitchId(repoRoot) {
|
|
3476
|
+
const current = await getCurrentStitchId(repoRoot);
|
|
3477
|
+
if (!current) {
|
|
3478
|
+
throw new NoCurrentStitchError;
|
|
3479
|
+
}
|
|
3480
|
+
return current;
|
|
3481
|
+
}
|
|
3482
|
+
|
|
3483
|
+
// src/core/git.ts
|
|
3484
|
+
var {$ } = globalThis.Bun;
|
|
3485
|
+
async function getRepoRoot(cwd) {
|
|
3486
|
+
try {
|
|
3487
|
+
const result = await $`git rev-parse --show-toplevel`.cwd(cwd ?? process.cwd()).quiet().text();
|
|
3488
|
+
return result.trim();
|
|
3489
|
+
} catch {
|
|
3490
|
+
throw new RepoNotFoundError(cwd);
|
|
3491
|
+
}
|
|
3492
|
+
}
|
|
3493
|
+
async function commitExists(sha, repoRoot) {
|
|
3494
|
+
try {
|
|
3495
|
+
await $`git cat-file -e ${sha}^{commit}`.cwd(repoRoot).quiet();
|
|
3496
|
+
return true;
|
|
3497
|
+
} catch {
|
|
3498
|
+
return false;
|
|
3499
|
+
}
|
|
3500
|
+
}
|
|
3501
|
+
async function resolveRef(ref, repoRoot) {
|
|
3502
|
+
try {
|
|
3503
|
+
const result = await $`git rev-parse ${ref}`.cwd(repoRoot).quiet().text();
|
|
3504
|
+
return result.trim();
|
|
3505
|
+
} catch (error) {
|
|
3506
|
+
throw new GitError(`Failed to resolve ref: ${ref}`, `git rev-parse ${ref}`, 1);
|
|
3507
|
+
}
|
|
3508
|
+
}
|
|
3509
|
+
async function getStagedDiff(repoRoot) {
|
|
3510
|
+
try {
|
|
3511
|
+
const result = await $`git diff --staged`.cwd(repoRoot).quiet().text();
|
|
3512
|
+
return result;
|
|
3513
|
+
} catch (error) {
|
|
3514
|
+
throw new GitError("Failed to get staged diff", "git diff --staged", 1);
|
|
3515
|
+
}
|
|
3516
|
+
}
|
|
3517
|
+
async function hashDiff(diff) {
|
|
3518
|
+
const encoder = new TextEncoder;
|
|
3519
|
+
const data = encoder.encode(diff);
|
|
3520
|
+
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
|
|
3521
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
3522
|
+
return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
3523
|
+
}
|
|
3524
|
+
function parseBlameOutput(output) {
|
|
3525
|
+
const entries = [];
|
|
3526
|
+
const lines = output.split(`
|
|
3527
|
+
`);
|
|
3528
|
+
let currentSha = null;
|
|
3529
|
+
let currentLineNumber = null;
|
|
3530
|
+
for (let i = 0;i < lines.length; i++) {
|
|
3531
|
+
const line = lines[i];
|
|
3532
|
+
if (line === undefined)
|
|
3533
|
+
continue;
|
|
3534
|
+
const shaMatch = line.match(/^([a-f0-9]{40})\s+\d+\s+(\d+)/);
|
|
3535
|
+
if (shaMatch) {
|
|
3536
|
+
currentSha = shaMatch[1];
|
|
3537
|
+
currentLineNumber = parseInt(shaMatch[2], 10);
|
|
3538
|
+
continue;
|
|
3539
|
+
}
|
|
3540
|
+
if (line.startsWith("\t") && currentSha && currentLineNumber) {
|
|
3541
|
+
entries.push({
|
|
3542
|
+
sha: currentSha,
|
|
3543
|
+
lineNumber: currentLineNumber,
|
|
3544
|
+
lineText: line.slice(1)
|
|
3545
|
+
});
|
|
3546
|
+
currentSha = null;
|
|
3547
|
+
currentLineNumber = null;
|
|
3548
|
+
}
|
|
3549
|
+
}
|
|
3550
|
+
return entries;
|
|
3551
|
+
}
|
|
3552
|
+
async function blameFile(filePath, repoRoot) {
|
|
3553
|
+
try {
|
|
3554
|
+
const result = await $`git blame --line-porcelain ${filePath}`.cwd(repoRoot).quiet().text();
|
|
3555
|
+
return parseBlameOutput(result);
|
|
3556
|
+
} catch (error) {
|
|
3557
|
+
throw new GitError(`Failed to blame file: ${filePath}`, `git blame --line-porcelain ${filePath}`, 1);
|
|
3558
|
+
}
|
|
3559
|
+
}
|
|
3560
|
+
async function getCommitsInRange(range, repoRoot) {
|
|
3561
|
+
try {
|
|
3562
|
+
const result = await $`git rev-list ${range}`.cwd(repoRoot).quiet().text();
|
|
3563
|
+
return result.trim().split(`
|
|
3564
|
+
`).filter((s) => s.length > 0);
|
|
3565
|
+
} catch {
|
|
3566
|
+
return [];
|
|
3567
|
+
}
|
|
3568
|
+
}
|
|
3569
|
+
|
|
3570
|
+
// src/core/link.ts
|
|
3571
|
+
async function addCommitLink(repoRoot, doc, sha) {
|
|
3572
|
+
const exists = await commitExists(sha, repoRoot);
|
|
3573
|
+
if (!exists) {
|
|
3574
|
+
throw new ValidationError(`Commit not found: ${sha}`);
|
|
3575
|
+
}
|
|
3576
|
+
const fullSha = await resolveRef(sha, repoRoot);
|
|
3577
|
+
const link = { kind: "commit", sha: fullSha };
|
|
3578
|
+
return addLink(repoRoot, doc, link);
|
|
3579
|
+
}
|
|
3580
|
+
async function addRangeLink(repoRoot, doc, range) {
|
|
3581
|
+
const link = { kind: "range", range };
|
|
3582
|
+
return addLink(repoRoot, doc, link);
|
|
3583
|
+
}
|
|
3584
|
+
async function addStagedDiffFingerprint(repoRoot, doc) {
|
|
3585
|
+
const diff = await getStagedDiff(repoRoot);
|
|
3586
|
+
if (!diff.trim()) {
|
|
3587
|
+
throw new ValidationError("No staged changes to fingerprint");
|
|
3588
|
+
}
|
|
3589
|
+
const hash = await hashDiff(diff);
|
|
3590
|
+
const fingerprint = {
|
|
3591
|
+
algo: "sha256",
|
|
3592
|
+
kind: "staged-diff",
|
|
3593
|
+
value: hash
|
|
3594
|
+
};
|
|
3595
|
+
const updatedDoc = addFingerprint(doc, fingerprint);
|
|
3596
|
+
const savedDoc = await saveStitch(repoRoot, updatedDoc);
|
|
3597
|
+
return { doc: savedDoc, fingerprint };
|
|
3598
|
+
}
|
|
3599
|
+
function addLink(repoRoot, doc, link) {
|
|
3600
|
+
const git = doc.frontmatter.git ?? {};
|
|
3601
|
+
const links = git.links ?? [];
|
|
3602
|
+
const isDuplicate = links.some((existing) => {
|
|
3603
|
+
if (existing.kind !== link.kind)
|
|
3604
|
+
return false;
|
|
3605
|
+
if (existing.kind === "commit" && link.kind === "commit") {
|
|
3606
|
+
return existing.sha === link.sha;
|
|
3607
|
+
}
|
|
3608
|
+
if (existing.kind === "range" && link.kind === "range") {
|
|
3609
|
+
return existing.range === link.range;
|
|
3610
|
+
}
|
|
3611
|
+
return false;
|
|
3612
|
+
});
|
|
3613
|
+
if (isDuplicate) {
|
|
3614
|
+
return Promise.resolve(doc);
|
|
3615
|
+
}
|
|
3616
|
+
const updatedDoc = {
|
|
3617
|
+
...doc,
|
|
3618
|
+
frontmatter: {
|
|
3619
|
+
...doc.frontmatter,
|
|
3620
|
+
git: {
|
|
3621
|
+
...git,
|
|
3622
|
+
links: [...links, link]
|
|
3623
|
+
}
|
|
3624
|
+
}
|
|
3625
|
+
};
|
|
3626
|
+
return saveStitch(repoRoot, updatedDoc);
|
|
3627
|
+
}
|
|
3628
|
+
function addFingerprint(doc, fingerprint) {
|
|
3629
|
+
const git = doc.frontmatter.git ?? {};
|
|
3630
|
+
const fingerprints = git.fingerprints ?? [];
|
|
3631
|
+
const isDuplicate = fingerprints.some((existing) => existing.algo === fingerprint.algo && existing.kind === fingerprint.kind && existing.value === fingerprint.value);
|
|
3632
|
+
if (isDuplicate) {
|
|
3633
|
+
return doc;
|
|
3634
|
+
}
|
|
3635
|
+
return {
|
|
3636
|
+
...doc,
|
|
3637
|
+
frontmatter: {
|
|
3638
|
+
...doc.frontmatter,
|
|
3639
|
+
git: {
|
|
3640
|
+
...git,
|
|
3641
|
+
fingerprints: [...fingerprints, fingerprint]
|
|
3642
|
+
}
|
|
3643
|
+
}
|
|
3644
|
+
};
|
|
3645
|
+
}
|
|
3646
|
+
|
|
3647
|
+
// src/core/blame.ts
|
|
3648
|
+
async function buildCommitToStitchMap(repoRoot, stitches) {
|
|
3649
|
+
const map = new Map;
|
|
3650
|
+
for (const doc of stitches) {
|
|
3651
|
+
const links = doc.frontmatter.git?.links ?? [];
|
|
3652
|
+
for (const link of links) {
|
|
3653
|
+
if (link.kind === "commit") {
|
|
3654
|
+
const existing = map.get(link.sha) ?? [];
|
|
3655
|
+
if (!existing.includes(doc.frontmatter.id)) {
|
|
3656
|
+
map.set(link.sha, [...existing, doc.frontmatter.id]);
|
|
3657
|
+
}
|
|
3658
|
+
} else if (link.kind === "range") {
|
|
3659
|
+
const commits = await getCommitsInRange(link.range, repoRoot);
|
|
3660
|
+
for (const sha of commits) {
|
|
3661
|
+
const existing = map.get(sha) ?? [];
|
|
3662
|
+
if (!existing.includes(doc.frontmatter.id)) {
|
|
3663
|
+
map.set(sha, [...existing, doc.frontmatter.id]);
|
|
3664
|
+
}
|
|
3665
|
+
}
|
|
3666
|
+
}
|
|
3667
|
+
}
|
|
3668
|
+
}
|
|
3669
|
+
return map;
|
|
3670
|
+
}
|
|
3671
|
+
async function stitchBlame(repoRoot, filePath) {
|
|
3672
|
+
const blameEntries = await blameFile(filePath, repoRoot);
|
|
3673
|
+
const stitches = await listStitches(repoRoot);
|
|
3674
|
+
const commitMap = await buildCommitToStitchMap(repoRoot, stitches);
|
|
3675
|
+
const result = blameEntries.map((entry) => ({
|
|
3676
|
+
line: entry.lineNumber,
|
|
3677
|
+
sha: entry.sha,
|
|
3678
|
+
stitchIds: commitMap.get(entry.sha) ?? [],
|
|
3679
|
+
text: entry.lineText
|
|
3680
|
+
}));
|
|
3681
|
+
return result;
|
|
3682
|
+
}
|
|
3683
|
+
|
|
3684
|
+
// src/platform/paths.ts
|
|
3685
|
+
import { homedir } from "os";
|
|
3686
|
+
import { join as join2 } from "path";
|
|
3687
|
+
function getConfigDir() {
|
|
3688
|
+
const platform = process.platform;
|
|
3689
|
+
if (platform === "darwin") {
|
|
3690
|
+
return join2(homedir(), "Library", "Application Support", "stitch");
|
|
3691
|
+
}
|
|
3692
|
+
if (platform === "win32") {
|
|
3693
|
+
const appData = process.env["APPDATA"];
|
|
3694
|
+
if (appData) {
|
|
3695
|
+
return join2(appData, "stitch");
|
|
3696
|
+
}
|
|
3697
|
+
return join2(homedir(), "AppData", "Roaming", "stitch");
|
|
3698
|
+
}
|
|
3699
|
+
const xdgConfig = process.env["XDG_CONFIG_HOME"];
|
|
3700
|
+
if (xdgConfig) {
|
|
3701
|
+
return join2(xdgConfig, "stitch");
|
|
3702
|
+
}
|
|
3703
|
+
return join2(homedir(), ".config", "stitch");
|
|
3704
|
+
}
|
|
3705
|
+
function getEditor() {
|
|
3706
|
+
return process.env["VISUAL"] ?? process.env["EDITOR"] ?? (process.platform === "win32" ? "notepad" : "vi");
|
|
3707
|
+
}
|
|
3708
|
+
|
|
3709
|
+
// src/api.ts
|
|
3710
|
+
class StitchClient {
|
|
3711
|
+
repoRoot = null;
|
|
3712
|
+
repoRootOverride;
|
|
3713
|
+
constructor(options) {
|
|
3714
|
+
this.repoRootOverride = options?.repoRoot;
|
|
3715
|
+
}
|
|
3716
|
+
async getRepoRoot() {
|
|
3717
|
+
if (this.repoRoot) {
|
|
3718
|
+
return this.repoRoot;
|
|
3719
|
+
}
|
|
3720
|
+
if (this.repoRootOverride) {
|
|
3721
|
+
this.repoRoot = this.repoRootOverride;
|
|
3722
|
+
} else {
|
|
3723
|
+
this.repoRoot = await getRepoRoot();
|
|
3724
|
+
}
|
|
3725
|
+
return this.repoRoot;
|
|
3726
|
+
}
|
|
3727
|
+
async init() {
|
|
3728
|
+
const root = await this.getRepoRoot();
|
|
3729
|
+
await initializeStitch(root);
|
|
3730
|
+
}
|
|
3731
|
+
async isInitialized() {
|
|
3732
|
+
const root = await this.getRepoRoot();
|
|
3733
|
+
return isInitialized(root);
|
|
3734
|
+
}
|
|
3735
|
+
async start(title) {
|
|
3736
|
+
const root = await this.getRepoRoot();
|
|
3737
|
+
const doc = await createStitch(root, title);
|
|
3738
|
+
await setCurrentStitchId(root, doc.frontmatter.id);
|
|
3739
|
+
return doc;
|
|
3740
|
+
}
|
|
3741
|
+
async child(title) {
|
|
3742
|
+
const root = await this.getRepoRoot();
|
|
3743
|
+
const parentId = await requireCurrentStitchId(root);
|
|
3744
|
+
const doc = await createStitch(root, title, parentId);
|
|
3745
|
+
await setCurrentStitchId(root, doc.frontmatter.id);
|
|
3746
|
+
return doc;
|
|
3747
|
+
}
|
|
3748
|
+
async switch(id) {
|
|
3749
|
+
const root = await this.getRepoRoot();
|
|
3750
|
+
await loadStitch(root, id);
|
|
3751
|
+
await setCurrentStitchId(root, id);
|
|
3752
|
+
}
|
|
3753
|
+
async status() {
|
|
3754
|
+
const root = await this.getRepoRoot();
|
|
3755
|
+
if (!isInitialized(root)) {
|
|
3756
|
+
throw new NotInitializedError;
|
|
3757
|
+
}
|
|
3758
|
+
const current = await getCurrentStitchId(root);
|
|
3759
|
+
if (!current) {
|
|
3760
|
+
return { lineage: [] };
|
|
3761
|
+
}
|
|
3762
|
+
const lineage = await getLineage(root, current);
|
|
3763
|
+
return { current, lineage };
|
|
3764
|
+
}
|
|
3765
|
+
async list(filter) {
|
|
3766
|
+
const root = await this.getRepoRoot();
|
|
3767
|
+
return listStitches(root, filter);
|
|
3768
|
+
}
|
|
3769
|
+
async get(id) {
|
|
3770
|
+
const root = await this.getRepoRoot();
|
|
3771
|
+
return loadStitch(root, id);
|
|
3772
|
+
}
|
|
3773
|
+
async requireCurrentId() {
|
|
3774
|
+
const root = await this.getRepoRoot();
|
|
3775
|
+
return requireCurrentStitchId(root);
|
|
3776
|
+
}
|
|
3777
|
+
async openInEditor(id) {
|
|
3778
|
+
const root = await this.getRepoRoot();
|
|
3779
|
+
const stitchId = id ?? await requireCurrentStitchId(root);
|
|
3780
|
+
const filePath = getStitchFilePath(root, stitchId);
|
|
3781
|
+
const editor = getEditor();
|
|
3782
|
+
return new Promise((resolve, reject) => {
|
|
3783
|
+
const child = spawn(editor, [filePath], {
|
|
3784
|
+
stdio: "inherit",
|
|
3785
|
+
shell: true
|
|
3786
|
+
});
|
|
3787
|
+
child.on("close", (code) => {
|
|
3788
|
+
if (code === 0) {
|
|
3789
|
+
resolve();
|
|
3790
|
+
} else {
|
|
3791
|
+
reject(new Error(`Editor exited with code ${code}`));
|
|
3792
|
+
}
|
|
3793
|
+
});
|
|
3794
|
+
child.on("error", (err) => {
|
|
3795
|
+
reject(err);
|
|
3796
|
+
});
|
|
3797
|
+
});
|
|
3798
|
+
}
|
|
3799
|
+
async linkCommit(sha, id) {
|
|
3800
|
+
const root = await this.getRepoRoot();
|
|
3801
|
+
const stitchId = id ?? await requireCurrentStitchId(root);
|
|
3802
|
+
const doc = await loadStitch(root, stitchId);
|
|
3803
|
+
await addCommitLink(root, doc, sha);
|
|
3804
|
+
}
|
|
3805
|
+
async linkRange(range, id) {
|
|
3806
|
+
const root = await this.getRepoRoot();
|
|
3807
|
+
const stitchId = id ?? await requireCurrentStitchId(root);
|
|
3808
|
+
const doc = await loadStitch(root, stitchId);
|
|
3809
|
+
await addRangeLink(root, doc, range);
|
|
3810
|
+
}
|
|
3811
|
+
async linkStagedDiff(id) {
|
|
3812
|
+
const root = await this.getRepoRoot();
|
|
3813
|
+
const stitchId = id ?? await requireCurrentStitchId(root);
|
|
3814
|
+
const doc = await loadStitch(root, stitchId);
|
|
3815
|
+
const { fingerprint } = await addStagedDiffFingerprint(root, doc);
|
|
3816
|
+
return fingerprint;
|
|
3817
|
+
}
|
|
3818
|
+
async blame(path) {
|
|
3819
|
+
const root = await this.getRepoRoot();
|
|
3820
|
+
return stitchBlame(root, path);
|
|
3821
|
+
}
|
|
3822
|
+
[Symbol.dispose]() {
|
|
3823
|
+
this.close();
|
|
3824
|
+
}
|
|
3825
|
+
close() {
|
|
3826
|
+
this.repoRoot = null;
|
|
3827
|
+
}
|
|
3828
|
+
}
|
|
3829
|
+
|
|
3830
|
+
// src/core/render.ts
|
|
3831
|
+
function renderStitchList(stitches) {
|
|
3832
|
+
if (stitches.length === 0) {
|
|
3833
|
+
return "No stitches found.";
|
|
3834
|
+
}
|
|
3835
|
+
const lines = [];
|
|
3836
|
+
lines.push("ID STATUS TITLE");
|
|
3837
|
+
lines.push("\u2500".repeat(60));
|
|
3838
|
+
for (const doc of stitches) {
|
|
3839
|
+
const fm = doc.frontmatter;
|
|
3840
|
+
const id = fm.id.padEnd(18);
|
|
3841
|
+
const status = fm.status.padEnd(10);
|
|
3842
|
+
const title = truncate(fm.title, 30);
|
|
3843
|
+
lines.push(`${id} ${status} ${title}`);
|
|
3844
|
+
}
|
|
3845
|
+
return lines.join(`
|
|
3846
|
+
`);
|
|
3847
|
+
}
|
|
3848
|
+
function renderStatus(status) {
|
|
3849
|
+
const lines = [];
|
|
3850
|
+
if (status.current) {
|
|
3851
|
+
lines.push(`Current stitch: ${status.current}`);
|
|
3852
|
+
if (status.lineage.length > 1) {
|
|
3853
|
+
lines.push("");
|
|
3854
|
+
lines.push("Lineage:");
|
|
3855
|
+
for (let i = 0;i < status.lineage.length; i++) {
|
|
3856
|
+
const prefix = i === 0 ? "\u2192 " : " ";
|
|
3857
|
+
const indent = " ".repeat(i);
|
|
3858
|
+
lines.push(`${indent}${prefix}${status.lineage[i]}`);
|
|
3859
|
+
}
|
|
3860
|
+
}
|
|
3861
|
+
} else {
|
|
3862
|
+
lines.push("No current stitch.");
|
|
3863
|
+
lines.push("");
|
|
3864
|
+
lines.push("Start a new stitch with: stitch start <title>");
|
|
3865
|
+
lines.push("Or switch to an existing one: stitch switch <id>");
|
|
3866
|
+
}
|
|
3867
|
+
return lines.join(`
|
|
3868
|
+
`);
|
|
3869
|
+
}
|
|
3870
|
+
function renderStitchDoc(doc) {
|
|
3871
|
+
const fm = doc.frontmatter;
|
|
3872
|
+
const lines = [];
|
|
3873
|
+
lines.push(`# ${fm.title}`);
|
|
3874
|
+
lines.push("");
|
|
3875
|
+
lines.push(`ID: ${fm.id}`);
|
|
3876
|
+
lines.push(`Status: ${fm.status}`);
|
|
3877
|
+
lines.push(`Created: ${formatDate2(fm.created_at)}`);
|
|
3878
|
+
lines.push(`Updated: ${formatDate2(fm.updated_at)}`);
|
|
3879
|
+
if (fm.provenance) {
|
|
3880
|
+
lines.push(`Provenance: ${fm.provenance}`);
|
|
3881
|
+
}
|
|
3882
|
+
if (fm.confidence) {
|
|
3883
|
+
lines.push(`Confidence: ${fm.confidence}`);
|
|
3884
|
+
}
|
|
3885
|
+
if (fm.tags && fm.tags.length > 0) {
|
|
3886
|
+
lines.push(`Tags: ${fm.tags.join(", ")}`);
|
|
3887
|
+
}
|
|
3888
|
+
if (fm.relations?.parent) {
|
|
3889
|
+
lines.push(`Parent: ${fm.relations.parent}`);
|
|
3890
|
+
}
|
|
3891
|
+
if (fm.relations?.depends_on && fm.relations.depends_on.length > 0) {
|
|
3892
|
+
lines.push(`Depends on: ${fm.relations.depends_on.join(", ")}`);
|
|
3893
|
+
}
|
|
3894
|
+
if (fm.git?.links && fm.git.links.length > 0) {
|
|
3895
|
+
lines.push("");
|
|
3896
|
+
lines.push("Git links:");
|
|
3897
|
+
for (const link of fm.git.links) {
|
|
3898
|
+
if (link.kind === "commit") {
|
|
3899
|
+
lines.push(` - commit: ${link.sha.slice(0, 8)}`);
|
|
3900
|
+
} else {
|
|
3901
|
+
lines.push(` - range: ${link.range}`);
|
|
3902
|
+
}
|
|
3903
|
+
}
|
|
3904
|
+
}
|
|
3905
|
+
if (doc.body.trim()) {
|
|
3906
|
+
lines.push("");
|
|
3907
|
+
lines.push("\u2500".repeat(40));
|
|
3908
|
+
lines.push("");
|
|
3909
|
+
lines.push(doc.body.trim());
|
|
3910
|
+
}
|
|
3911
|
+
return lines.join(`
|
|
3912
|
+
`);
|
|
3913
|
+
}
|
|
3914
|
+
function renderBlamePlain(blameLines) {
|
|
3915
|
+
if (blameLines.length === 0) {
|
|
3916
|
+
return "No blame information available.";
|
|
3917
|
+
}
|
|
3918
|
+
const lines = [];
|
|
3919
|
+
const maxLineNum = Math.max(...blameLines.map((b) => b.line));
|
|
3920
|
+
const lineNumWidth = String(maxLineNum).length;
|
|
3921
|
+
for (const bl of blameLines) {
|
|
3922
|
+
const lineNum = String(bl.line).padStart(lineNumWidth);
|
|
3923
|
+
const sha = bl.sha.slice(0, 8);
|
|
3924
|
+
const stitch = bl.stitchIds.length > 0 ? bl.stitchIds[0] : "unstitched";
|
|
3925
|
+
const stitchPadded = (stitch ?? "unstitched").padEnd(18);
|
|
3926
|
+
lines.push(`${lineNum} \u2502 ${sha} \u2502 ${stitchPadded} \u2502 ${bl.text}`);
|
|
3927
|
+
}
|
|
3928
|
+
return lines.join(`
|
|
3929
|
+
`);
|
|
3930
|
+
}
|
|
3931
|
+
function renderBlameJson(blameLines) {
|
|
3932
|
+
return JSON.stringify(blameLines, null, 2);
|
|
3933
|
+
}
|
|
3934
|
+
function renderStitchListJson(stitches) {
|
|
3935
|
+
const items = stitches.map((doc) => ({
|
|
3936
|
+
id: doc.frontmatter.id,
|
|
3937
|
+
title: doc.frontmatter.title,
|
|
3938
|
+
status: doc.frontmatter.status,
|
|
3939
|
+
created_at: doc.frontmatter.created_at,
|
|
3940
|
+
updated_at: doc.frontmatter.updated_at,
|
|
3941
|
+
provenance: doc.frontmatter.provenance,
|
|
3942
|
+
confidence: doc.frontmatter.confidence,
|
|
3943
|
+
tags: doc.frontmatter.tags,
|
|
3944
|
+
parent: doc.frontmatter.relations?.parent,
|
|
3945
|
+
filePath: doc.filePath
|
|
3946
|
+
}));
|
|
3947
|
+
return JSON.stringify(items, null, 2);
|
|
3948
|
+
}
|
|
3949
|
+
function renderStatusJson(status) {
|
|
3950
|
+
return JSON.stringify(status, null, 2);
|
|
3951
|
+
}
|
|
3952
|
+
function formatDate2(isoString) {
|
|
3953
|
+
try {
|
|
3954
|
+
const date = new Date(isoString);
|
|
3955
|
+
return date.toLocaleString();
|
|
3956
|
+
} catch {
|
|
3957
|
+
return isoString;
|
|
3958
|
+
}
|
|
3959
|
+
}
|
|
3960
|
+
function truncate(str, maxLength) {
|
|
3961
|
+
if (str.length <= maxLength) {
|
|
3962
|
+
return str;
|
|
3963
|
+
}
|
|
3964
|
+
return str.slice(0, maxLength - 3) + "...";
|
|
3965
|
+
}
|
|
3966
|
+
function renderSuccess(message) {
|
|
3967
|
+
return `\u2713 ${message}`;
|
|
3968
|
+
}
|
|
3969
|
+
// src/platform/update/version.ts
|
|
3970
|
+
function parseVersion(version) {
|
|
3971
|
+
const previewMatch = version.match(/^(\d+)\.(\d+)\.(\d+)(-preview\.([a-f0-9]+))?$/);
|
|
3972
|
+
if (!previewMatch) {
|
|
3973
|
+
throw new Error(`Invalid version format: ${version}`);
|
|
3974
|
+
}
|
|
3975
|
+
return {
|
|
3976
|
+
major: parseInt(previewMatch[1], 10),
|
|
3977
|
+
minor: parseInt(previewMatch[2], 10),
|
|
3978
|
+
patch: parseInt(previewMatch[3], 10),
|
|
3979
|
+
prerelease: previewMatch[5],
|
|
3980
|
+
isPreview: !!previewMatch[4]
|
|
3981
|
+
};
|
|
3982
|
+
}
|
|
3983
|
+
function compareVersions(a, b) {
|
|
3984
|
+
const parsedA = parseVersion(a);
|
|
3985
|
+
const parsedB = parseVersion(b);
|
|
3986
|
+
if (parsedA.major !== parsedB.major) {
|
|
3987
|
+
return parsedA.major < parsedB.major ? -1 : 1;
|
|
3988
|
+
}
|
|
3989
|
+
if (parsedA.minor !== parsedB.minor) {
|
|
3990
|
+
return parsedA.minor < parsedB.minor ? -1 : 1;
|
|
3991
|
+
}
|
|
3992
|
+
if (parsedA.patch !== parsedB.patch) {
|
|
3993
|
+
return parsedA.patch < parsedB.patch ? -1 : 1;
|
|
3994
|
+
}
|
|
3995
|
+
if (!parsedA.isPreview && parsedB.isPreview)
|
|
3996
|
+
return 1;
|
|
3997
|
+
if (parsedA.isPreview && !parsedB.isPreview)
|
|
3998
|
+
return -1;
|
|
3999
|
+
if (parsedA.prerelease && parsedB.prerelease) {
|
|
4000
|
+
return parsedA.prerelease.localeCompare(parsedB.prerelease);
|
|
4001
|
+
}
|
|
4002
|
+
return 0;
|
|
4003
|
+
}
|
|
4004
|
+
function isUpdateAvailable(current, latest) {
|
|
4005
|
+
return compareVersions(current, latest) < 0;
|
|
4006
|
+
}
|
|
4007
|
+
// src/platform/update/check.ts
|
|
4008
|
+
import { readFile as readFile2, writeFile as writeFile2, mkdir as mkdir2 } from "fs/promises";
|
|
4009
|
+
import { existsSync as existsSync2 } from "fs";
|
|
4010
|
+
import { join as join3 } from "path";
|
|
4011
|
+
var CACHE_FILE = "update-check.json";
|
|
4012
|
+
var CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000;
|
|
4013
|
+
var GITHUB_API_URL = "https://api.github.com/repos/captainsafia/stitch/releases";
|
|
4014
|
+
function getCacheFilePath() {
|
|
4015
|
+
return join3(getConfigDir(), CACHE_FILE);
|
|
4016
|
+
}
|
|
4017
|
+
async function readCache() {
|
|
4018
|
+
const cachePath = getCacheFilePath();
|
|
4019
|
+
if (!existsSync2(cachePath)) {
|
|
4020
|
+
return null;
|
|
4021
|
+
}
|
|
4022
|
+
try {
|
|
4023
|
+
const content = await readFile2(cachePath, "utf-8");
|
|
4024
|
+
return JSON.parse(content);
|
|
4025
|
+
} catch {
|
|
4026
|
+
return null;
|
|
4027
|
+
}
|
|
4028
|
+
}
|
|
4029
|
+
async function writeCache(cache) {
|
|
4030
|
+
const configDir = getConfigDir();
|
|
4031
|
+
if (!existsSync2(configDir)) {
|
|
4032
|
+
await mkdir2(configDir, { recursive: true });
|
|
4033
|
+
}
|
|
4034
|
+
const cachePath = getCacheFilePath();
|
|
4035
|
+
await writeFile2(cachePath, JSON.stringify(cache, null, 2), "utf-8");
|
|
4036
|
+
}
|
|
4037
|
+
function isCacheValid(cache) {
|
|
4038
|
+
const now = Date.now();
|
|
4039
|
+
return now - cache.lastChecked < CHECK_INTERVAL_MS;
|
|
4040
|
+
}
|
|
4041
|
+
async function fetchLatestRelease() {
|
|
4042
|
+
try {
|
|
4043
|
+
const response = await fetch(`${GITHUB_API_URL}/latest`, {
|
|
4044
|
+
headers: {
|
|
4045
|
+
Accept: "application/vnd.github.v3+json",
|
|
4046
|
+
"User-Agent": "stitch-cli"
|
|
4047
|
+
}
|
|
4048
|
+
});
|
|
4049
|
+
if (!response.ok) {
|
|
4050
|
+
return null;
|
|
4051
|
+
}
|
|
4052
|
+
const data = await response.json();
|
|
4053
|
+
return data.tag_name.replace(/^v/, "");
|
|
4054
|
+
} catch {
|
|
4055
|
+
return null;
|
|
4056
|
+
}
|
|
4057
|
+
}
|
|
4058
|
+
async function fetchLatestPreviewRelease() {
|
|
4059
|
+
try {
|
|
4060
|
+
const response = await fetch(`${GITHUB_API_URL}?per_page=20`, {
|
|
4061
|
+
headers: {
|
|
4062
|
+
Accept: "application/vnd.github.v3+json",
|
|
4063
|
+
"User-Agent": "stitch-cli"
|
|
4064
|
+
}
|
|
4065
|
+
});
|
|
4066
|
+
if (!response.ok) {
|
|
4067
|
+
return null;
|
|
4068
|
+
}
|
|
4069
|
+
const releases = await response.json();
|
|
4070
|
+
for (const release of releases) {
|
|
4071
|
+
const version = release.tag_name.replace(/^v/, "");
|
|
4072
|
+
if (version.includes("-preview.")) {
|
|
4073
|
+
return version;
|
|
4074
|
+
}
|
|
4075
|
+
}
|
|
4076
|
+
return null;
|
|
4077
|
+
} catch {
|
|
4078
|
+
return null;
|
|
4079
|
+
}
|
|
4080
|
+
}
|
|
4081
|
+
async function getLatestVersion(forceRefresh = false) {
|
|
4082
|
+
if (!forceRefresh) {
|
|
4083
|
+
const cache = await readCache();
|
|
4084
|
+
if (cache && isCacheValid(cache)) {
|
|
4085
|
+
return {
|
|
4086
|
+
stable: cache.latestVersion,
|
|
4087
|
+
preview: cache.latestPreviewVersion ?? null
|
|
4088
|
+
};
|
|
4089
|
+
}
|
|
4090
|
+
}
|
|
4091
|
+
const [stableVersion, previewVersion] = await Promise.all([
|
|
4092
|
+
fetchLatestRelease(),
|
|
4093
|
+
fetchLatestPreviewRelease()
|
|
4094
|
+
]);
|
|
4095
|
+
if (stableVersion) {
|
|
4096
|
+
const cache = {
|
|
4097
|
+
lastChecked: Date.now(),
|
|
4098
|
+
latestVersion: stableVersion,
|
|
4099
|
+
latestPreviewVersion: previewVersion ?? undefined
|
|
4100
|
+
};
|
|
4101
|
+
writeCache(cache).catch(() => {});
|
|
4102
|
+
}
|
|
4103
|
+
return {
|
|
4104
|
+
stable: stableVersion,
|
|
4105
|
+
preview: previewVersion
|
|
4106
|
+
};
|
|
4107
|
+
}
|
|
4108
|
+
// src/platform/update/download.ts
|
|
4109
|
+
var DOWNLOAD_BASE_URL = "https://github.com/captainsafia/stitch/releases/download";
|
|
4110
|
+
function detectPlatform() {
|
|
4111
|
+
const platform = process.platform;
|
|
4112
|
+
const arch = process.arch;
|
|
4113
|
+
if (platform === "linux" && arch === "x64")
|
|
4114
|
+
return "linux-x64";
|
|
4115
|
+
if (platform === "linux" && arch === "arm64")
|
|
4116
|
+
return "linux-arm64";
|
|
4117
|
+
if (platform === "darwin" && arch === "x64")
|
|
4118
|
+
return "darwin-x64";
|
|
4119
|
+
if (platform === "darwin" && arch === "arm64")
|
|
4120
|
+
return "darwin-arm64";
|
|
4121
|
+
if (platform === "win32" && arch === "x64")
|
|
4122
|
+
return "windows-x64";
|
|
4123
|
+
throw new Error(`Unsupported platform: ${platform}-${arch}`);
|
|
4124
|
+
}
|
|
4125
|
+
function detectInstallMethod() {
|
|
4126
|
+
const execPath = process.execPath;
|
|
4127
|
+
const scriptPath = process.argv[1] ?? "";
|
|
4128
|
+
if (execPath.endsWith("/stitch") || execPath.endsWith("\\stitch.exe") || execPath.includes("stitch-")) {
|
|
4129
|
+
return "binary";
|
|
4130
|
+
}
|
|
4131
|
+
if (execPath.includes("bun")) {
|
|
4132
|
+
if (scriptPath.includes("node_modules")) {
|
|
4133
|
+
return "bun";
|
|
4134
|
+
}
|
|
4135
|
+
return "dev";
|
|
4136
|
+
}
|
|
4137
|
+
if (execPath.includes("node")) {
|
|
4138
|
+
return "npm";
|
|
4139
|
+
}
|
|
4140
|
+
return "binary";
|
|
4141
|
+
}
|
|
4142
|
+
function getDownloadUrl(version, platform) {
|
|
4143
|
+
const suffix = platform === "windows-x64" ? ".exe" : "";
|
|
4144
|
+
return `${DOWNLOAD_BASE_URL}/v${version}/stitch-${platform}${suffix}`;
|
|
4145
|
+
}
|
|
4146
|
+
async function downloadBinary(url, destPath, onProgress) {
|
|
4147
|
+
const response = await fetch(url, {
|
|
4148
|
+
headers: {
|
|
4149
|
+
"User-Agent": "stitch-cli"
|
|
4150
|
+
},
|
|
4151
|
+
redirect: "follow"
|
|
4152
|
+
});
|
|
4153
|
+
if (!response.ok) {
|
|
4154
|
+
if (response.status === 404) {
|
|
4155
|
+
throw new Error(`Binary not found at ${url}. This version may not be available for your platform.`);
|
|
4156
|
+
}
|
|
4157
|
+
if (response.status === 403) {
|
|
4158
|
+
throw new Error("Rate limited by GitHub. Please try again in a few minutes.");
|
|
4159
|
+
}
|
|
4160
|
+
throw new Error(`Download failed with status ${response.status}: ${response.statusText}`);
|
|
4161
|
+
}
|
|
4162
|
+
const contentLength = response.headers.get("content-length");
|
|
4163
|
+
const total = contentLength ? parseInt(contentLength, 10) : null;
|
|
4164
|
+
const reader = response.body?.getReader();
|
|
4165
|
+
if (!reader) {
|
|
4166
|
+
throw new Error("Failed to read response body");
|
|
4167
|
+
}
|
|
4168
|
+
const chunks = [];
|
|
4169
|
+
let downloaded = 0;
|
|
4170
|
+
while (true) {
|
|
4171
|
+
const { done, value } = await reader.read();
|
|
4172
|
+
if (done)
|
|
4173
|
+
break;
|
|
4174
|
+
chunks.push(value);
|
|
4175
|
+
downloaded += value.length;
|
|
4176
|
+
if (onProgress) {
|
|
4177
|
+
onProgress(downloaded, total);
|
|
4178
|
+
}
|
|
4179
|
+
}
|
|
4180
|
+
const binary = new Uint8Array(downloaded);
|
|
4181
|
+
let offset = 0;
|
|
4182
|
+
for (const chunk of chunks) {
|
|
4183
|
+
binary.set(chunk, offset);
|
|
4184
|
+
offset += chunk.length;
|
|
4185
|
+
}
|
|
4186
|
+
await Bun.write(destPath, binary);
|
|
4187
|
+
}
|
|
4188
|
+
// src/platform/update/install.ts
|
|
4189
|
+
import { rename, unlink, chmod } from "fs/promises";
|
|
4190
|
+
import { existsSync as existsSync3 } from "fs";
|
|
4191
|
+
import { dirname, join as join4 } from "path";
|
|
4192
|
+
var {$: $2 } = globalThis.Bun;
|
|
4193
|
+
function getCurrentExecutablePath() {
|
|
4194
|
+
return process.execPath;
|
|
4195
|
+
}
|
|
4196
|
+
async function cleanupOldBinaries(execPath) {
|
|
4197
|
+
const dir = dirname(execPath);
|
|
4198
|
+
const baseName = execPath.endsWith(".exe") ? "stitch" : "stitch";
|
|
4199
|
+
const oldPath = join4(dir, `${baseName}.old`);
|
|
4200
|
+
const oldPathExe = join4(dir, `${baseName}.old.exe`);
|
|
4201
|
+
for (const path of [oldPath, oldPathExe]) {
|
|
4202
|
+
if (existsSync3(path)) {
|
|
4203
|
+
try {
|
|
4204
|
+
await unlink(path);
|
|
4205
|
+
} catch {}
|
|
4206
|
+
}
|
|
4207
|
+
}
|
|
4208
|
+
}
|
|
4209
|
+
async function installBinaryUpdate(targetVersion, currentVersion, onProgress) {
|
|
4210
|
+
const platform = detectPlatform();
|
|
4211
|
+
const execPath = getCurrentExecutablePath();
|
|
4212
|
+
const dir = dirname(execPath);
|
|
4213
|
+
const isWindows = platform === "windows-x64";
|
|
4214
|
+
const newBinaryPath = join4(dir, isWindows ? "stitch.new.exe" : "stitch.new");
|
|
4215
|
+
const oldBinaryPath = join4(dir, isWindows ? "stitch.old.exe" : "stitch.old");
|
|
4216
|
+
const downloadUrl = getDownloadUrl(targetVersion, platform);
|
|
4217
|
+
try {
|
|
4218
|
+
await cleanupOldBinaries(execPath);
|
|
4219
|
+
onProgress?.(`Downloading stitch v${targetVersion}...`);
|
|
4220
|
+
let lastPercent = 0;
|
|
4221
|
+
await downloadBinary(downloadUrl, newBinaryPath, (downloaded, total) => {
|
|
4222
|
+
if (total) {
|
|
4223
|
+
const percent = Math.round(downloaded / total * 100);
|
|
4224
|
+
if (percent !== lastPercent && percent % 10 === 0) {
|
|
4225
|
+
onProgress?.(`Downloading: ${percent}%`);
|
|
4226
|
+
lastPercent = percent;
|
|
4227
|
+
}
|
|
4228
|
+
}
|
|
4229
|
+
});
|
|
4230
|
+
if (!isWindows) {
|
|
4231
|
+
await chmod(newBinaryPath, 493);
|
|
4232
|
+
}
|
|
4233
|
+
onProgress?.("Installing...");
|
|
4234
|
+
await rename(execPath, oldBinaryPath);
|
|
4235
|
+
await rename(newBinaryPath, execPath);
|
|
4236
|
+
try {
|
|
4237
|
+
await unlink(oldBinaryPath);
|
|
4238
|
+
} catch {}
|
|
4239
|
+
return {
|
|
4240
|
+
success: true,
|
|
4241
|
+
previousVersion: currentVersion,
|
|
4242
|
+
newVersion: targetVersion
|
|
4243
|
+
};
|
|
4244
|
+
} catch (error) {
|
|
4245
|
+
if (existsSync3(oldBinaryPath) && !existsSync3(execPath)) {
|
|
4246
|
+
try {
|
|
4247
|
+
await rename(oldBinaryPath, execPath);
|
|
4248
|
+
} catch {}
|
|
4249
|
+
}
|
|
4250
|
+
if (existsSync3(newBinaryPath)) {
|
|
4251
|
+
try {
|
|
4252
|
+
await unlink(newBinaryPath);
|
|
4253
|
+
} catch {}
|
|
4254
|
+
}
|
|
4255
|
+
return {
|
|
4256
|
+
success: false,
|
|
4257
|
+
previousVersion: currentVersion,
|
|
4258
|
+
newVersion: targetVersion,
|
|
4259
|
+
error: error instanceof Error ? error.message : String(error)
|
|
4260
|
+
};
|
|
4261
|
+
}
|
|
4262
|
+
}
|
|
4263
|
+
async function installNpmUpdate(targetVersion, currentVersion, onProgress) {
|
|
4264
|
+
try {
|
|
4265
|
+
onProgress?.(`Updating via npm to v${targetVersion}...`);
|
|
4266
|
+
const versionSpec = targetVersion === "latest" ? "@captainsafia/stitch@latest" : `@captainsafia/stitch@${targetVersion}`;
|
|
4267
|
+
const result = await $2`npm install -g ${versionSpec}`.quiet();
|
|
4268
|
+
if (result.exitCode !== 0) {
|
|
4269
|
+
return {
|
|
4270
|
+
success: false,
|
|
4271
|
+
previousVersion: currentVersion,
|
|
4272
|
+
newVersion: targetVersion,
|
|
4273
|
+
error: `npm install failed: ${result.stderr.toString()}`
|
|
4274
|
+
};
|
|
4275
|
+
}
|
|
4276
|
+
return {
|
|
4277
|
+
success: true,
|
|
4278
|
+
previousVersion: currentVersion,
|
|
4279
|
+
newVersion: targetVersion
|
|
4280
|
+
};
|
|
4281
|
+
} catch (error) {
|
|
4282
|
+
return {
|
|
4283
|
+
success: false,
|
|
4284
|
+
previousVersion: currentVersion,
|
|
4285
|
+
newVersion: targetVersion,
|
|
4286
|
+
error: error instanceof Error ? error.message : String(error)
|
|
4287
|
+
};
|
|
4288
|
+
}
|
|
4289
|
+
}
|
|
4290
|
+
async function installBunUpdate(targetVersion, currentVersion, onProgress) {
|
|
4291
|
+
try {
|
|
4292
|
+
onProgress?.(`Updating via bun to v${targetVersion}...`);
|
|
4293
|
+
const versionSpec = targetVersion === "latest" ? "@captainsafia/stitch@latest" : `@captainsafia/stitch@${targetVersion}`;
|
|
4294
|
+
const result = await $2`bun install -g ${versionSpec}`.quiet();
|
|
4295
|
+
if (result.exitCode !== 0) {
|
|
4296
|
+
return {
|
|
4297
|
+
success: false,
|
|
4298
|
+
previousVersion: currentVersion,
|
|
4299
|
+
newVersion: targetVersion,
|
|
4300
|
+
error: `bun install failed: ${result.stderr.toString()}`
|
|
4301
|
+
};
|
|
4302
|
+
}
|
|
4303
|
+
return {
|
|
4304
|
+
success: true,
|
|
4305
|
+
previousVersion: currentVersion,
|
|
4306
|
+
newVersion: targetVersion
|
|
4307
|
+
};
|
|
4308
|
+
} catch (error) {
|
|
4309
|
+
return {
|
|
4310
|
+
success: false,
|
|
4311
|
+
previousVersion: currentVersion,
|
|
4312
|
+
newVersion: targetVersion,
|
|
4313
|
+
error: error instanceof Error ? error.message : String(error)
|
|
4314
|
+
};
|
|
4315
|
+
}
|
|
4316
|
+
}
|
|
4317
|
+
async function installUpdate(targetVersion, currentVersion, onProgress) {
|
|
4318
|
+
const installMethod = detectInstallMethod();
|
|
4319
|
+
switch (installMethod) {
|
|
4320
|
+
case "binary":
|
|
4321
|
+
return installBinaryUpdate(targetVersion, currentVersion, onProgress);
|
|
4322
|
+
case "npm":
|
|
4323
|
+
return installNpmUpdate(targetVersion, currentVersion, onProgress);
|
|
4324
|
+
case "bun":
|
|
4325
|
+
return installBunUpdate(targetVersion, currentVersion, onProgress);
|
|
4326
|
+
case "dev":
|
|
4327
|
+
return {
|
|
4328
|
+
success: false,
|
|
4329
|
+
previousVersion: currentVersion,
|
|
4330
|
+
newVersion: targetVersion,
|
|
4331
|
+
error: "Cannot update in development mode. Use git pull or the appropriate package manager."
|
|
4332
|
+
};
|
|
4333
|
+
}
|
|
4334
|
+
}
|
|
4335
|
+
function getInstallMethodDescription(method) {
|
|
4336
|
+
switch (method) {
|
|
4337
|
+
case "binary":
|
|
4338
|
+
return "standalone binary";
|
|
4339
|
+
case "npm":
|
|
4340
|
+
return "npm global package";
|
|
4341
|
+
case "bun":
|
|
4342
|
+
return "bun global package";
|
|
4343
|
+
case "dev":
|
|
4344
|
+
return "development mode";
|
|
4345
|
+
}
|
|
4346
|
+
}
|
|
4347
|
+
// src/cli.ts
|
|
4348
|
+
var packageJson = await Promise.resolve().then(() => __toESM(require_package(), 1));
|
|
4349
|
+
var program2 = new Command;
|
|
4350
|
+
program2.name("stitch").description(packageJson.description).version(packageJson.version);
|
|
4351
|
+
async function checkForUpdates() {
|
|
4352
|
+
try {
|
|
4353
|
+
const versions = await getLatestVersion();
|
|
4354
|
+
const current = packageJson.version;
|
|
4355
|
+
if (versions.stable && isUpdateAvailable(current, versions.stable)) {
|
|
4356
|
+
console.error("");
|
|
4357
|
+
console.error(`A new version of stitch is available: ${versions.stable} (current: ${current})`);
|
|
4358
|
+
console.error("Run 'stitch update' to update.");
|
|
4359
|
+
console.error("");
|
|
4360
|
+
}
|
|
4361
|
+
} catch {}
|
|
4362
|
+
}
|
|
4363
|
+
var args = process.argv.slice(2);
|
|
4364
|
+
var skipCheckCommands = ["update", "--help", "-h", "--version", "-V"];
|
|
4365
|
+
if (!args.some((arg) => skipCheckCommands.includes(arg))) {
|
|
4366
|
+
checkForUpdates();
|
|
4367
|
+
}
|
|
4368
|
+
program2.command("init").description("Initialize stitch in the current repository").action(async () => {
|
|
4369
|
+
let __stack = [];
|
|
4370
|
+
try {
|
|
4371
|
+
const client = __using(__stack, new StitchClient, 0);
|
|
4372
|
+
try {
|
|
4373
|
+
const initialized = await client.isInitialized();
|
|
4374
|
+
if (initialized) {
|
|
4375
|
+
console.log("Stitch is already initialized in this repository.");
|
|
4376
|
+
return;
|
|
4377
|
+
}
|
|
4378
|
+
await client.init();
|
|
4379
|
+
console.log(renderSuccess("Stitch initialized."));
|
|
4380
|
+
console.log("");
|
|
4381
|
+
console.log("Next steps:");
|
|
4382
|
+
console.log(" stitch start <title> - Start a new stitch");
|
|
4383
|
+
} catch (error) {
|
|
4384
|
+
handleError(error);
|
|
4385
|
+
}
|
|
4386
|
+
} catch (_catch) {
|
|
4387
|
+
var _err = _catch, _hasErr = 1;
|
|
4388
|
+
} finally {
|
|
4389
|
+
__callDispose(__stack, _err, _hasErr);
|
|
4390
|
+
}
|
|
4391
|
+
});
|
|
4392
|
+
program2.command("start").description("Start a new stitch session").argument("<title...>", "Title for the new stitch").action(async (titleParts) => {
|
|
4393
|
+
let __stack = [];
|
|
4394
|
+
try {
|
|
4395
|
+
const client = __using(__stack, new StitchClient, 0);
|
|
4396
|
+
try {
|
|
4397
|
+
const title = titleParts.join(" ");
|
|
4398
|
+
const doc = await client.start(title);
|
|
4399
|
+
console.log(renderSuccess(`Created stitch: ${doc.frontmatter.id}`));
|
|
4400
|
+
console.log(`Title: ${doc.frontmatter.title}`);
|
|
4401
|
+
console.log(`File: ${doc.filePath}`);
|
|
4402
|
+
} catch (error) {
|
|
4403
|
+
handleError(error);
|
|
4404
|
+
}
|
|
4405
|
+
} catch (_catch) {
|
|
4406
|
+
var _err = _catch, _hasErr = 1;
|
|
4407
|
+
} finally {
|
|
4408
|
+
__callDispose(__stack, _err, _hasErr);
|
|
4409
|
+
}
|
|
4410
|
+
});
|
|
4411
|
+
program2.command("child").description("Create a child stitch under the current stitch").argument("<title...>", "Title for the child stitch").action(async (titleParts) => {
|
|
4412
|
+
let __stack = [];
|
|
4413
|
+
try {
|
|
4414
|
+
const client = __using(__stack, new StitchClient, 0);
|
|
4415
|
+
try {
|
|
4416
|
+
const title = titleParts.join(" ");
|
|
4417
|
+
const doc = await client.child(title);
|
|
4418
|
+
console.log(renderSuccess(`Created child stitch: ${doc.frontmatter.id}`));
|
|
4419
|
+
console.log(`Title: ${doc.frontmatter.title}`);
|
|
4420
|
+
console.log(`Parent: ${doc.frontmatter.relations?.parent}`);
|
|
4421
|
+
console.log(`File: ${doc.filePath}`);
|
|
4422
|
+
} catch (error) {
|
|
4423
|
+
handleError(error);
|
|
4424
|
+
}
|
|
4425
|
+
} catch (_catch) {
|
|
4426
|
+
var _err = _catch, _hasErr = 1;
|
|
4427
|
+
} finally {
|
|
4428
|
+
__callDispose(__stack, _err, _hasErr);
|
|
4429
|
+
}
|
|
4430
|
+
});
|
|
4431
|
+
program2.command("switch").description("Switch to a different stitch").argument("<id>", "Stitch ID to switch to").action(async (id) => {
|
|
4432
|
+
let __stack = [];
|
|
4433
|
+
try {
|
|
4434
|
+
const client = __using(__stack, new StitchClient, 0);
|
|
4435
|
+
try {
|
|
4436
|
+
await client.switch(id);
|
|
4437
|
+
console.log(renderSuccess(`Switched to stitch: ${id}`));
|
|
4438
|
+
} catch (error) {
|
|
4439
|
+
handleError(error);
|
|
4440
|
+
}
|
|
4441
|
+
} catch (_catch) {
|
|
4442
|
+
var _err = _catch, _hasErr = 1;
|
|
4443
|
+
} finally {
|
|
4444
|
+
__callDispose(__stack, _err, _hasErr);
|
|
4445
|
+
}
|
|
4446
|
+
});
|
|
4447
|
+
program2.command("status").description("Show current stitch status and lineage").option("-j, --json", "Output as JSON").action(async (options) => {
|
|
4448
|
+
let __stack = [];
|
|
4449
|
+
try {
|
|
4450
|
+
const client = __using(__stack, new StitchClient, 0);
|
|
4451
|
+
try {
|
|
4452
|
+
const status = await client.status();
|
|
4453
|
+
if (options.json) {
|
|
4454
|
+
console.log(renderStatusJson(status));
|
|
4455
|
+
} else {
|
|
4456
|
+
console.log(renderStatus(status));
|
|
4457
|
+
}
|
|
4458
|
+
} catch (error) {
|
|
4459
|
+
handleError(error);
|
|
4460
|
+
}
|
|
4461
|
+
} catch (_catch) {
|
|
4462
|
+
var _err = _catch, _hasErr = 1;
|
|
4463
|
+
} finally {
|
|
4464
|
+
__callDispose(__stack, _err, _hasErr);
|
|
4465
|
+
}
|
|
4466
|
+
});
|
|
4467
|
+
program2.command("edit").description("Open a stitch in your editor").argument("[id]", "Stitch ID to edit (defaults to current)").action(async (id) => {
|
|
4468
|
+
let __stack = [];
|
|
4469
|
+
try {
|
|
4470
|
+
const client = __using(__stack, new StitchClient, 0);
|
|
4471
|
+
try {
|
|
4472
|
+
await client.openInEditor(id);
|
|
4473
|
+
} catch (error) {
|
|
4474
|
+
handleError(error);
|
|
4475
|
+
}
|
|
4476
|
+
} catch (_catch) {
|
|
4477
|
+
var _err = _catch, _hasErr = 1;
|
|
4478
|
+
} finally {
|
|
4479
|
+
__callDispose(__stack, _err, _hasErr);
|
|
4480
|
+
}
|
|
4481
|
+
});
|
|
4482
|
+
program2.command("list").description("List all stitches").addOption(new Option("-s, --status <status>", "Filter by status").choices([
|
|
4483
|
+
"open",
|
|
4484
|
+
"closed",
|
|
4485
|
+
"superseded",
|
|
4486
|
+
"abandoned"
|
|
4487
|
+
])).option("-j, --json", "Output as JSON").action(async (options) => {
|
|
4488
|
+
let __stack = [];
|
|
4489
|
+
try {
|
|
4490
|
+
const client = __using(__stack, new StitchClient, 0);
|
|
4491
|
+
try {
|
|
4492
|
+
const filter = options.status ? { status: options.status } : undefined;
|
|
4493
|
+
const stitches = await client.list(filter);
|
|
4494
|
+
if (options.json) {
|
|
4495
|
+
console.log(renderStitchListJson(stitches));
|
|
4496
|
+
} else {
|
|
4497
|
+
console.log(renderStitchList(stitches));
|
|
4498
|
+
}
|
|
4499
|
+
} catch (error) {
|
|
4500
|
+
handleError(error);
|
|
4501
|
+
}
|
|
4502
|
+
} catch (_catch) {
|
|
4503
|
+
var _err = _catch, _hasErr = 1;
|
|
4504
|
+
} finally {
|
|
4505
|
+
__callDispose(__stack, _err, _hasErr);
|
|
4506
|
+
}
|
|
4507
|
+
});
|
|
4508
|
+
program2.command("show").description("Show details of a stitch").argument("<id>", "Stitch ID to show (use 'current' for the current stitch)").action(async (id) => {
|
|
4509
|
+
let __stack = [];
|
|
4510
|
+
try {
|
|
4511
|
+
const client = __using(__stack, new StitchClient, 0);
|
|
4512
|
+
try {
|
|
4513
|
+
const stitchId = id === "current" ? await client.requireCurrentId() : id;
|
|
4514
|
+
const doc = await client.get(stitchId);
|
|
4515
|
+
console.log(renderStitchDoc(doc));
|
|
4516
|
+
} catch (error) {
|
|
4517
|
+
handleError(error);
|
|
4518
|
+
}
|
|
4519
|
+
} catch (_catch) {
|
|
4520
|
+
var _err = _catch, _hasErr = 1;
|
|
4521
|
+
} finally {
|
|
4522
|
+
__callDispose(__stack, _err, _hasErr);
|
|
4523
|
+
}
|
|
4524
|
+
});
|
|
4525
|
+
program2.command("link").description("Link git commits or diffs to a stitch").option("-c, --commit <sha>", "Link a specific commit").option("-r, --range <range>", "Link a commit range (e.g., origin/main..HEAD)").option("-s, --staged", "Link the current staged diff fingerprint").option("-i, --id <id>", "Stitch ID to link to (defaults to current)").action(async (options) => {
|
|
4526
|
+
let __stack = [];
|
|
4527
|
+
try {
|
|
4528
|
+
const client = __using(__stack, new StitchClient, 0);
|
|
4529
|
+
try {
|
|
4530
|
+
const optionCount = [options.commit, options.range, options.staged].filter(Boolean).length;
|
|
4531
|
+
if (optionCount === 0) {
|
|
4532
|
+
console.error("Error: Must specify one of --commit, --range, or --staged");
|
|
4533
|
+
process.exit(1);
|
|
4534
|
+
}
|
|
4535
|
+
if (optionCount > 1) {
|
|
4536
|
+
console.error("Error: Cannot specify more than one of --commit, --range, or --staged");
|
|
4537
|
+
process.exit(1);
|
|
4538
|
+
}
|
|
4539
|
+
if (options.commit) {
|
|
4540
|
+
await client.linkCommit(options.commit, options.id);
|
|
4541
|
+
console.log(renderSuccess(`Linked commit: ${options.commit}`));
|
|
4542
|
+
} else if (options.range) {
|
|
4543
|
+
await client.linkRange(options.range, options.id);
|
|
4544
|
+
console.log(renderSuccess(`Linked range: ${options.range}`));
|
|
4545
|
+
} else if (options.staged) {
|
|
4546
|
+
const fingerprint = await client.linkStagedDiff(options.id);
|
|
4547
|
+
console.log(renderSuccess(`Linked staged diff`));
|
|
4548
|
+
console.log(`Fingerprint: ${fingerprint.value.slice(0, 16)}...`);
|
|
4549
|
+
}
|
|
4550
|
+
} catch (error) {
|
|
4551
|
+
handleError(error);
|
|
4552
|
+
}
|
|
4553
|
+
} catch (_catch) {
|
|
4554
|
+
var _err = _catch, _hasErr = 1;
|
|
4555
|
+
} finally {
|
|
4556
|
+
__callDispose(__stack, _err, _hasErr);
|
|
4557
|
+
}
|
|
4558
|
+
});
|
|
4559
|
+
program2.command("blame").description("Show stitch attribution for each line in a file").argument("<path>", "File path to blame").addOption(new Option("-f, --format <format>", "Output format").choices(["plain", "json"]).default("plain")).action(async (path, options) => {
|
|
4560
|
+
let __stack = [];
|
|
4561
|
+
try {
|
|
4562
|
+
const client = __using(__stack, new StitchClient, 0);
|
|
4563
|
+
try {
|
|
4564
|
+
const blameLines = await client.blame(path);
|
|
4565
|
+
if (options.format === "json") {
|
|
4566
|
+
console.log(renderBlameJson(blameLines));
|
|
4567
|
+
} else {
|
|
4568
|
+
console.log(renderBlamePlain(blameLines));
|
|
4569
|
+
}
|
|
4570
|
+
} catch (error) {
|
|
4571
|
+
handleError(error);
|
|
4572
|
+
}
|
|
4573
|
+
} catch (_catch) {
|
|
4574
|
+
var _err = _catch, _hasErr = 1;
|
|
4575
|
+
} finally {
|
|
4576
|
+
__callDispose(__stack, _err, _hasErr);
|
|
4577
|
+
}
|
|
4578
|
+
});
|
|
4579
|
+
program2.command("update").description("Update stitch to the latest version").option("-t, --target <version>", "Install a specific version").option("-p, --preview", "Install the latest preview version").option("-y, --yes", "Skip confirmation prompt").action(async (options) => {
|
|
4580
|
+
try {
|
|
4581
|
+
const current = packageJson.version;
|
|
4582
|
+
const installMethod = detectInstallMethod();
|
|
4583
|
+
let targetVersion;
|
|
4584
|
+
if (options.target) {
|
|
4585
|
+
targetVersion = options.target;
|
|
4586
|
+
} else if (options.preview) {
|
|
4587
|
+
console.log("Checking for latest preview version...");
|
|
4588
|
+
const versions = await getLatestVersion(true);
|
|
4589
|
+
if (!versions.preview) {
|
|
4590
|
+
console.error("Error: No preview version available.");
|
|
4591
|
+
process.exit(1);
|
|
4592
|
+
}
|
|
4593
|
+
targetVersion = versions.preview;
|
|
4594
|
+
} else {
|
|
4595
|
+
console.log("Checking for latest version...");
|
|
4596
|
+
const versions = await getLatestVersion(true);
|
|
4597
|
+
if (!versions.stable) {
|
|
4598
|
+
console.error("Error: Could not fetch latest version. Check your network connection.");
|
|
4599
|
+
process.exit(1);
|
|
4600
|
+
}
|
|
4601
|
+
targetVersion = versions.stable;
|
|
4602
|
+
}
|
|
4603
|
+
if (current === targetVersion) {
|
|
4604
|
+
console.log(`Already running stitch v${current}`);
|
|
4605
|
+
return;
|
|
4606
|
+
}
|
|
4607
|
+
console.log("");
|
|
4608
|
+
console.log(`Current version: ${current}`);
|
|
4609
|
+
console.log(`Target version: ${targetVersion}`);
|
|
4610
|
+
console.log(`Install method: ${getInstallMethodDescription(installMethod)}`);
|
|
4611
|
+
if (installMethod === "binary") {
|
|
4612
|
+
console.log(`Platform: ${detectPlatform()}`);
|
|
4613
|
+
}
|
|
4614
|
+
console.log("");
|
|
4615
|
+
if (!options.yes) {
|
|
4616
|
+
const answer = prompt("Proceed with update? [y/N]");
|
|
4617
|
+
if (answer?.toLowerCase() !== "y" && answer?.toLowerCase() !== "yes") {
|
|
4618
|
+
console.log("Update cancelled.");
|
|
4619
|
+
return;
|
|
4620
|
+
}
|
|
4621
|
+
}
|
|
4622
|
+
const result = await installUpdate(targetVersion, current, (message) => {
|
|
4623
|
+
console.log(message);
|
|
4624
|
+
});
|
|
4625
|
+
if (result.success) {
|
|
4626
|
+
console.log("");
|
|
4627
|
+
console.log(renderSuccess(`Updated stitch from v${current} to v${targetVersion}`));
|
|
4628
|
+
if (installMethod === "binary") {
|
|
4629
|
+
console.log("Please restart your terminal for changes to take effect.");
|
|
4630
|
+
}
|
|
4631
|
+
} else {
|
|
4632
|
+
console.error("");
|
|
4633
|
+
console.error(`Error: Update failed: ${result.error}`);
|
|
4634
|
+
process.exit(1);
|
|
4635
|
+
}
|
|
4636
|
+
} catch (error) {
|
|
4637
|
+
handleError(error);
|
|
4638
|
+
}
|
|
4639
|
+
});
|
|
4640
|
+
function handleError(error) {
|
|
4641
|
+
if (error instanceof NotInitializedError) {
|
|
4642
|
+
console.error(`Error: ${error.message}`);
|
|
4643
|
+
process.exit(1);
|
|
4644
|
+
}
|
|
4645
|
+
if (error instanceof NoCurrentStitchError) {
|
|
4646
|
+
console.error(`Error: ${error.message}`);
|
|
4647
|
+
process.exit(1);
|
|
4648
|
+
}
|
|
4649
|
+
if (error instanceof StitchError) {
|
|
4650
|
+
console.error(`Error: ${error.message}`);
|
|
4651
|
+
process.exit(1);
|
|
4652
|
+
}
|
|
4653
|
+
if (error instanceof Error) {
|
|
4654
|
+
console.error(`Error: ${error.message}`);
|
|
4655
|
+
if (process.env["DEBUG"] === "1") {
|
|
4656
|
+
console.error(error.stack);
|
|
4657
|
+
}
|
|
4658
|
+
process.exit(1);
|
|
4659
|
+
}
|
|
4660
|
+
console.error("An unexpected error occurred");
|
|
4661
|
+
process.exit(1);
|
|
4662
|
+
}
|
|
4663
|
+
process.on("SIGINT", () => {
|
|
4664
|
+
console.log(`
|
|
4665
|
+
Interrupted.`);
|
|
4666
|
+
process.exit(130);
|
|
4667
|
+
});
|
|
4668
|
+
program2.parse();
|