@desplega.ai/wts 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +273 -0
- package/dist/index.js +3437 -0
- package/package.json +51 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,3437 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// @bun
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
var __create = Object.create;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __defProp = Object.defineProperty;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
10
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
11
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
12
|
+
for (let key of __getOwnPropNames(mod))
|
|
13
|
+
if (!__hasOwnProp.call(to, key))
|
|
14
|
+
__defProp(to, key, {
|
|
15
|
+
get: () => mod[key],
|
|
16
|
+
enumerable: true
|
|
17
|
+
});
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
21
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
22
|
+
|
|
23
|
+
// node_modules/commander/lib/error.js
|
|
24
|
+
var require_error = __commonJS((exports) => {
|
|
25
|
+
class CommanderError extends Error {
|
|
26
|
+
constructor(exitCode, code, message) {
|
|
27
|
+
super(message);
|
|
28
|
+
Error.captureStackTrace(this, this.constructor);
|
|
29
|
+
this.name = this.constructor.name;
|
|
30
|
+
this.code = code;
|
|
31
|
+
this.exitCode = exitCode;
|
|
32
|
+
this.nestedError = undefined;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
class InvalidArgumentError extends CommanderError {
|
|
37
|
+
constructor(message) {
|
|
38
|
+
super(1, "commander.invalidArgument", message);
|
|
39
|
+
Error.captureStackTrace(this, this.constructor);
|
|
40
|
+
this.name = this.constructor.name;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.CommanderError = CommanderError;
|
|
44
|
+
exports.InvalidArgumentError = InvalidArgumentError;
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// node_modules/commander/lib/argument.js
|
|
48
|
+
var require_argument = __commonJS((exports) => {
|
|
49
|
+
var { InvalidArgumentError } = require_error();
|
|
50
|
+
|
|
51
|
+
class Argument {
|
|
52
|
+
constructor(name, description) {
|
|
53
|
+
this.description = description || "";
|
|
54
|
+
this.variadic = false;
|
|
55
|
+
this.parseArg = undefined;
|
|
56
|
+
this.defaultValue = undefined;
|
|
57
|
+
this.defaultValueDescription = undefined;
|
|
58
|
+
this.argChoices = undefined;
|
|
59
|
+
switch (name[0]) {
|
|
60
|
+
case "<":
|
|
61
|
+
this.required = true;
|
|
62
|
+
this._name = name.slice(1, -1);
|
|
63
|
+
break;
|
|
64
|
+
case "[":
|
|
65
|
+
this.required = false;
|
|
66
|
+
this._name = name.slice(1, -1);
|
|
67
|
+
break;
|
|
68
|
+
default:
|
|
69
|
+
this.required = true;
|
|
70
|
+
this._name = name;
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
if (this._name.length > 3 && this._name.slice(-3) === "...") {
|
|
74
|
+
this.variadic = true;
|
|
75
|
+
this._name = this._name.slice(0, -3);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
name() {
|
|
79
|
+
return this._name;
|
|
80
|
+
}
|
|
81
|
+
_concatValue(value, previous) {
|
|
82
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
83
|
+
return [value];
|
|
84
|
+
}
|
|
85
|
+
return previous.concat(value);
|
|
86
|
+
}
|
|
87
|
+
default(value, description) {
|
|
88
|
+
this.defaultValue = value;
|
|
89
|
+
this.defaultValueDescription = description;
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
argParser(fn) {
|
|
93
|
+
this.parseArg = fn;
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
choices(values) {
|
|
97
|
+
this.argChoices = values.slice();
|
|
98
|
+
this.parseArg = (arg, previous) => {
|
|
99
|
+
if (!this.argChoices.includes(arg)) {
|
|
100
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
101
|
+
}
|
|
102
|
+
if (this.variadic) {
|
|
103
|
+
return this._concatValue(arg, previous);
|
|
104
|
+
}
|
|
105
|
+
return arg;
|
|
106
|
+
};
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
argRequired() {
|
|
110
|
+
this.required = true;
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
argOptional() {
|
|
114
|
+
this.required = false;
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function humanReadableArgName(arg) {
|
|
119
|
+
const nameOutput = arg.name() + (arg.variadic === true ? "..." : "");
|
|
120
|
+
return arg.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
|
|
121
|
+
}
|
|
122
|
+
exports.Argument = Argument;
|
|
123
|
+
exports.humanReadableArgName = humanReadableArgName;
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// node_modules/commander/lib/help.js
|
|
127
|
+
var require_help = __commonJS((exports) => {
|
|
128
|
+
var { humanReadableArgName } = require_argument();
|
|
129
|
+
|
|
130
|
+
class Help {
|
|
131
|
+
constructor() {
|
|
132
|
+
this.helpWidth = undefined;
|
|
133
|
+
this.sortSubcommands = false;
|
|
134
|
+
this.sortOptions = false;
|
|
135
|
+
this.showGlobalOptions = false;
|
|
136
|
+
}
|
|
137
|
+
visibleCommands(cmd) {
|
|
138
|
+
const visibleCommands = cmd.commands.filter((cmd2) => !cmd2._hidden);
|
|
139
|
+
const helpCommand = cmd._getHelpCommand();
|
|
140
|
+
if (helpCommand && !helpCommand._hidden) {
|
|
141
|
+
visibleCommands.push(helpCommand);
|
|
142
|
+
}
|
|
143
|
+
if (this.sortSubcommands) {
|
|
144
|
+
visibleCommands.sort((a, b) => {
|
|
145
|
+
return a.name().localeCompare(b.name());
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
return visibleCommands;
|
|
149
|
+
}
|
|
150
|
+
compareOptions(a, b) {
|
|
151
|
+
const getSortKey = (option) => {
|
|
152
|
+
return option.short ? option.short.replace(/^-/, "") : option.long.replace(/^--/, "");
|
|
153
|
+
};
|
|
154
|
+
return getSortKey(a).localeCompare(getSortKey(b));
|
|
155
|
+
}
|
|
156
|
+
visibleOptions(cmd) {
|
|
157
|
+
const visibleOptions = cmd.options.filter((option) => !option.hidden);
|
|
158
|
+
const helpOption = cmd._getHelpOption();
|
|
159
|
+
if (helpOption && !helpOption.hidden) {
|
|
160
|
+
const removeShort = helpOption.short && cmd._findOption(helpOption.short);
|
|
161
|
+
const removeLong = helpOption.long && cmd._findOption(helpOption.long);
|
|
162
|
+
if (!removeShort && !removeLong) {
|
|
163
|
+
visibleOptions.push(helpOption);
|
|
164
|
+
} else if (helpOption.long && !removeLong) {
|
|
165
|
+
visibleOptions.push(cmd.createOption(helpOption.long, helpOption.description));
|
|
166
|
+
} else if (helpOption.short && !removeShort) {
|
|
167
|
+
visibleOptions.push(cmd.createOption(helpOption.short, helpOption.description));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (this.sortOptions) {
|
|
171
|
+
visibleOptions.sort(this.compareOptions);
|
|
172
|
+
}
|
|
173
|
+
return visibleOptions;
|
|
174
|
+
}
|
|
175
|
+
visibleGlobalOptions(cmd) {
|
|
176
|
+
if (!this.showGlobalOptions)
|
|
177
|
+
return [];
|
|
178
|
+
const globalOptions = [];
|
|
179
|
+
for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
180
|
+
const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
|
|
181
|
+
globalOptions.push(...visibleOptions);
|
|
182
|
+
}
|
|
183
|
+
if (this.sortOptions) {
|
|
184
|
+
globalOptions.sort(this.compareOptions);
|
|
185
|
+
}
|
|
186
|
+
return globalOptions;
|
|
187
|
+
}
|
|
188
|
+
visibleArguments(cmd) {
|
|
189
|
+
if (cmd._argsDescription) {
|
|
190
|
+
cmd.registeredArguments.forEach((argument) => {
|
|
191
|
+
argument.description = argument.description || cmd._argsDescription[argument.name()] || "";
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
if (cmd.registeredArguments.find((argument) => argument.description)) {
|
|
195
|
+
return cmd.registeredArguments;
|
|
196
|
+
}
|
|
197
|
+
return [];
|
|
198
|
+
}
|
|
199
|
+
subcommandTerm(cmd) {
|
|
200
|
+
const args = cmd.registeredArguments.map((arg) => humanReadableArgName(arg)).join(" ");
|
|
201
|
+
return cmd._name + (cmd._aliases[0] ? "|" + cmd._aliases[0] : "") + (cmd.options.length ? " [options]" : "") + (args ? " " + args : "");
|
|
202
|
+
}
|
|
203
|
+
optionTerm(option) {
|
|
204
|
+
return option.flags;
|
|
205
|
+
}
|
|
206
|
+
argumentTerm(argument) {
|
|
207
|
+
return argument.name();
|
|
208
|
+
}
|
|
209
|
+
longestSubcommandTermLength(cmd, helper) {
|
|
210
|
+
return helper.visibleCommands(cmd).reduce((max, command) => {
|
|
211
|
+
return Math.max(max, helper.subcommandTerm(command).length);
|
|
212
|
+
}, 0);
|
|
213
|
+
}
|
|
214
|
+
longestOptionTermLength(cmd, helper) {
|
|
215
|
+
return helper.visibleOptions(cmd).reduce((max, option) => {
|
|
216
|
+
return Math.max(max, helper.optionTerm(option).length);
|
|
217
|
+
}, 0);
|
|
218
|
+
}
|
|
219
|
+
longestGlobalOptionTermLength(cmd, helper) {
|
|
220
|
+
return helper.visibleGlobalOptions(cmd).reduce((max, option) => {
|
|
221
|
+
return Math.max(max, helper.optionTerm(option).length);
|
|
222
|
+
}, 0);
|
|
223
|
+
}
|
|
224
|
+
longestArgumentTermLength(cmd, helper) {
|
|
225
|
+
return helper.visibleArguments(cmd).reduce((max, argument) => {
|
|
226
|
+
return Math.max(max, helper.argumentTerm(argument).length);
|
|
227
|
+
}, 0);
|
|
228
|
+
}
|
|
229
|
+
commandUsage(cmd) {
|
|
230
|
+
let cmdName = cmd._name;
|
|
231
|
+
if (cmd._aliases[0]) {
|
|
232
|
+
cmdName = cmdName + "|" + cmd._aliases[0];
|
|
233
|
+
}
|
|
234
|
+
let ancestorCmdNames = "";
|
|
235
|
+
for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
236
|
+
ancestorCmdNames = ancestorCmd.name() + " " + ancestorCmdNames;
|
|
237
|
+
}
|
|
238
|
+
return ancestorCmdNames + cmdName + " " + cmd.usage();
|
|
239
|
+
}
|
|
240
|
+
commandDescription(cmd) {
|
|
241
|
+
return cmd.description();
|
|
242
|
+
}
|
|
243
|
+
subcommandDescription(cmd) {
|
|
244
|
+
return cmd.summary() || cmd.description();
|
|
245
|
+
}
|
|
246
|
+
optionDescription(option) {
|
|
247
|
+
const extraInfo = [];
|
|
248
|
+
if (option.argChoices) {
|
|
249
|
+
extraInfo.push(`choices: ${option.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
250
|
+
}
|
|
251
|
+
if (option.defaultValue !== undefined) {
|
|
252
|
+
const showDefault = option.required || option.optional || option.isBoolean() && typeof option.defaultValue === "boolean";
|
|
253
|
+
if (showDefault) {
|
|
254
|
+
extraInfo.push(`default: ${option.defaultValueDescription || JSON.stringify(option.defaultValue)}`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (option.presetArg !== undefined && option.optional) {
|
|
258
|
+
extraInfo.push(`preset: ${JSON.stringify(option.presetArg)}`);
|
|
259
|
+
}
|
|
260
|
+
if (option.envVar !== undefined) {
|
|
261
|
+
extraInfo.push(`env: ${option.envVar}`);
|
|
262
|
+
}
|
|
263
|
+
if (extraInfo.length > 0) {
|
|
264
|
+
return `${option.description} (${extraInfo.join(", ")})`;
|
|
265
|
+
}
|
|
266
|
+
return option.description;
|
|
267
|
+
}
|
|
268
|
+
argumentDescription(argument) {
|
|
269
|
+
const extraInfo = [];
|
|
270
|
+
if (argument.argChoices) {
|
|
271
|
+
extraInfo.push(`choices: ${argument.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
272
|
+
}
|
|
273
|
+
if (argument.defaultValue !== undefined) {
|
|
274
|
+
extraInfo.push(`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`);
|
|
275
|
+
}
|
|
276
|
+
if (extraInfo.length > 0) {
|
|
277
|
+
const extraDescripton = `(${extraInfo.join(", ")})`;
|
|
278
|
+
if (argument.description) {
|
|
279
|
+
return `${argument.description} ${extraDescripton}`;
|
|
280
|
+
}
|
|
281
|
+
return extraDescripton;
|
|
282
|
+
}
|
|
283
|
+
return argument.description;
|
|
284
|
+
}
|
|
285
|
+
formatHelp(cmd, helper) {
|
|
286
|
+
const termWidth = helper.padWidth(cmd, helper);
|
|
287
|
+
const helpWidth = helper.helpWidth || 80;
|
|
288
|
+
const itemIndentWidth = 2;
|
|
289
|
+
const itemSeparatorWidth = 2;
|
|
290
|
+
function formatItem(term, description) {
|
|
291
|
+
if (description) {
|
|
292
|
+
const fullText = `${term.padEnd(termWidth + itemSeparatorWidth)}${description}`;
|
|
293
|
+
return helper.wrap(fullText, helpWidth - itemIndentWidth, termWidth + itemSeparatorWidth);
|
|
294
|
+
}
|
|
295
|
+
return term;
|
|
296
|
+
}
|
|
297
|
+
function formatList(textArray) {
|
|
298
|
+
return textArray.join(`
|
|
299
|
+
`).replace(/^/gm, " ".repeat(itemIndentWidth));
|
|
300
|
+
}
|
|
301
|
+
let output = [`Usage: ${helper.commandUsage(cmd)}`, ""];
|
|
302
|
+
const commandDescription = helper.commandDescription(cmd);
|
|
303
|
+
if (commandDescription.length > 0) {
|
|
304
|
+
output = output.concat([
|
|
305
|
+
helper.wrap(commandDescription, helpWidth, 0),
|
|
306
|
+
""
|
|
307
|
+
]);
|
|
308
|
+
}
|
|
309
|
+
const argumentList = helper.visibleArguments(cmd).map((argument) => {
|
|
310
|
+
return formatItem(helper.argumentTerm(argument), helper.argumentDescription(argument));
|
|
311
|
+
});
|
|
312
|
+
if (argumentList.length > 0) {
|
|
313
|
+
output = output.concat(["Arguments:", formatList(argumentList), ""]);
|
|
314
|
+
}
|
|
315
|
+
const optionList = helper.visibleOptions(cmd).map((option) => {
|
|
316
|
+
return formatItem(helper.optionTerm(option), helper.optionDescription(option));
|
|
317
|
+
});
|
|
318
|
+
if (optionList.length > 0) {
|
|
319
|
+
output = output.concat(["Options:", formatList(optionList), ""]);
|
|
320
|
+
}
|
|
321
|
+
if (this.showGlobalOptions) {
|
|
322
|
+
const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
|
|
323
|
+
return formatItem(helper.optionTerm(option), helper.optionDescription(option));
|
|
324
|
+
});
|
|
325
|
+
if (globalOptionList.length > 0) {
|
|
326
|
+
output = output.concat([
|
|
327
|
+
"Global Options:",
|
|
328
|
+
formatList(globalOptionList),
|
|
329
|
+
""
|
|
330
|
+
]);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
const commandList = helper.visibleCommands(cmd).map((cmd2) => {
|
|
334
|
+
return formatItem(helper.subcommandTerm(cmd2), helper.subcommandDescription(cmd2));
|
|
335
|
+
});
|
|
336
|
+
if (commandList.length > 0) {
|
|
337
|
+
output = output.concat(["Commands:", formatList(commandList), ""]);
|
|
338
|
+
}
|
|
339
|
+
return output.join(`
|
|
340
|
+
`);
|
|
341
|
+
}
|
|
342
|
+
padWidth(cmd, helper) {
|
|
343
|
+
return Math.max(helper.longestOptionTermLength(cmd, helper), helper.longestGlobalOptionTermLength(cmd, helper), helper.longestSubcommandTermLength(cmd, helper), helper.longestArgumentTermLength(cmd, helper));
|
|
344
|
+
}
|
|
345
|
+
wrap(str, width, indent, minColumnWidth = 40) {
|
|
346
|
+
const indents = " \\f\\t\\v - \uFEFF";
|
|
347
|
+
const manualIndent = new RegExp(`[\\n][${indents}]+`);
|
|
348
|
+
if (str.match(manualIndent))
|
|
349
|
+
return str;
|
|
350
|
+
const columnWidth = width - indent;
|
|
351
|
+
if (columnWidth < minColumnWidth)
|
|
352
|
+
return str;
|
|
353
|
+
const leadingStr = str.slice(0, indent);
|
|
354
|
+
const columnText = str.slice(indent).replace(`\r
|
|
355
|
+
`, `
|
|
356
|
+
`);
|
|
357
|
+
const indentString = " ".repeat(indent);
|
|
358
|
+
const zeroWidthSpace = "";
|
|
359
|
+
const breaks = `\\s${zeroWidthSpace}`;
|
|
360
|
+
const regex = new RegExp(`
|
|
361
|
+
|.{1,${columnWidth - 1}}([${breaks}]|$)|[^${breaks}]+?([${breaks}]|$)`, "g");
|
|
362
|
+
const lines = columnText.match(regex) || [];
|
|
363
|
+
return leadingStr + lines.map((line, i) => {
|
|
364
|
+
if (line === `
|
|
365
|
+
`)
|
|
366
|
+
return "";
|
|
367
|
+
return (i > 0 ? indentString : "") + line.trimEnd();
|
|
368
|
+
}).join(`
|
|
369
|
+
`);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
exports.Help = Help;
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
// node_modules/commander/lib/option.js
|
|
376
|
+
var require_option = __commonJS((exports) => {
|
|
377
|
+
var { InvalidArgumentError } = require_error();
|
|
378
|
+
|
|
379
|
+
class Option {
|
|
380
|
+
constructor(flags, description) {
|
|
381
|
+
this.flags = flags;
|
|
382
|
+
this.description = description || "";
|
|
383
|
+
this.required = flags.includes("<");
|
|
384
|
+
this.optional = flags.includes("[");
|
|
385
|
+
this.variadic = /\w\.\.\.[>\]]$/.test(flags);
|
|
386
|
+
this.mandatory = false;
|
|
387
|
+
const optionFlags = splitOptionFlags(flags);
|
|
388
|
+
this.short = optionFlags.shortFlag;
|
|
389
|
+
this.long = optionFlags.longFlag;
|
|
390
|
+
this.negate = false;
|
|
391
|
+
if (this.long) {
|
|
392
|
+
this.negate = this.long.startsWith("--no-");
|
|
393
|
+
}
|
|
394
|
+
this.defaultValue = undefined;
|
|
395
|
+
this.defaultValueDescription = undefined;
|
|
396
|
+
this.presetArg = undefined;
|
|
397
|
+
this.envVar = undefined;
|
|
398
|
+
this.parseArg = undefined;
|
|
399
|
+
this.hidden = false;
|
|
400
|
+
this.argChoices = undefined;
|
|
401
|
+
this.conflictsWith = [];
|
|
402
|
+
this.implied = undefined;
|
|
403
|
+
}
|
|
404
|
+
default(value, description) {
|
|
405
|
+
this.defaultValue = value;
|
|
406
|
+
this.defaultValueDescription = description;
|
|
407
|
+
return this;
|
|
408
|
+
}
|
|
409
|
+
preset(arg) {
|
|
410
|
+
this.presetArg = arg;
|
|
411
|
+
return this;
|
|
412
|
+
}
|
|
413
|
+
conflicts(names) {
|
|
414
|
+
this.conflictsWith = this.conflictsWith.concat(names);
|
|
415
|
+
return this;
|
|
416
|
+
}
|
|
417
|
+
implies(impliedOptionValues) {
|
|
418
|
+
let newImplied = impliedOptionValues;
|
|
419
|
+
if (typeof impliedOptionValues === "string") {
|
|
420
|
+
newImplied = { [impliedOptionValues]: true };
|
|
421
|
+
}
|
|
422
|
+
this.implied = Object.assign(this.implied || {}, newImplied);
|
|
423
|
+
return this;
|
|
424
|
+
}
|
|
425
|
+
env(name) {
|
|
426
|
+
this.envVar = name;
|
|
427
|
+
return this;
|
|
428
|
+
}
|
|
429
|
+
argParser(fn) {
|
|
430
|
+
this.parseArg = fn;
|
|
431
|
+
return this;
|
|
432
|
+
}
|
|
433
|
+
makeOptionMandatory(mandatory = true) {
|
|
434
|
+
this.mandatory = !!mandatory;
|
|
435
|
+
return this;
|
|
436
|
+
}
|
|
437
|
+
hideHelp(hide = true) {
|
|
438
|
+
this.hidden = !!hide;
|
|
439
|
+
return this;
|
|
440
|
+
}
|
|
441
|
+
_concatValue(value, previous) {
|
|
442
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
443
|
+
return [value];
|
|
444
|
+
}
|
|
445
|
+
return previous.concat(value);
|
|
446
|
+
}
|
|
447
|
+
choices(values) {
|
|
448
|
+
this.argChoices = values.slice();
|
|
449
|
+
this.parseArg = (arg, previous) => {
|
|
450
|
+
if (!this.argChoices.includes(arg)) {
|
|
451
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
452
|
+
}
|
|
453
|
+
if (this.variadic) {
|
|
454
|
+
return this._concatValue(arg, previous);
|
|
455
|
+
}
|
|
456
|
+
return arg;
|
|
457
|
+
};
|
|
458
|
+
return this;
|
|
459
|
+
}
|
|
460
|
+
name() {
|
|
461
|
+
if (this.long) {
|
|
462
|
+
return this.long.replace(/^--/, "");
|
|
463
|
+
}
|
|
464
|
+
return this.short.replace(/^-/, "");
|
|
465
|
+
}
|
|
466
|
+
attributeName() {
|
|
467
|
+
return camelcase(this.name().replace(/^no-/, ""));
|
|
468
|
+
}
|
|
469
|
+
is(arg) {
|
|
470
|
+
return this.short === arg || this.long === arg;
|
|
471
|
+
}
|
|
472
|
+
isBoolean() {
|
|
473
|
+
return !this.required && !this.optional && !this.negate;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
class DualOptions {
|
|
478
|
+
constructor(options) {
|
|
479
|
+
this.positiveOptions = new Map;
|
|
480
|
+
this.negativeOptions = new Map;
|
|
481
|
+
this.dualOptions = new Set;
|
|
482
|
+
options.forEach((option) => {
|
|
483
|
+
if (option.negate) {
|
|
484
|
+
this.negativeOptions.set(option.attributeName(), option);
|
|
485
|
+
} else {
|
|
486
|
+
this.positiveOptions.set(option.attributeName(), option);
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
this.negativeOptions.forEach((value, key) => {
|
|
490
|
+
if (this.positiveOptions.has(key)) {
|
|
491
|
+
this.dualOptions.add(key);
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
valueFromOption(value, option) {
|
|
496
|
+
const optionKey = option.attributeName();
|
|
497
|
+
if (!this.dualOptions.has(optionKey))
|
|
498
|
+
return true;
|
|
499
|
+
const preset = this.negativeOptions.get(optionKey).presetArg;
|
|
500
|
+
const negativeValue = preset !== undefined ? preset : false;
|
|
501
|
+
return option.negate === (negativeValue === value);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
function camelcase(str) {
|
|
505
|
+
return str.split("-").reduce((str2, word) => {
|
|
506
|
+
return str2 + word[0].toUpperCase() + word.slice(1);
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
function splitOptionFlags(flags) {
|
|
510
|
+
let shortFlag;
|
|
511
|
+
let longFlag;
|
|
512
|
+
const flagParts = flags.split(/[ |,]+/);
|
|
513
|
+
if (flagParts.length > 1 && !/^[[<]/.test(flagParts[1]))
|
|
514
|
+
shortFlag = flagParts.shift();
|
|
515
|
+
longFlag = flagParts.shift();
|
|
516
|
+
if (!shortFlag && /^-[^-]$/.test(longFlag)) {
|
|
517
|
+
shortFlag = longFlag;
|
|
518
|
+
longFlag = undefined;
|
|
519
|
+
}
|
|
520
|
+
return { shortFlag, longFlag };
|
|
521
|
+
}
|
|
522
|
+
exports.Option = Option;
|
|
523
|
+
exports.DualOptions = DualOptions;
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
// node_modules/commander/lib/suggestSimilar.js
|
|
527
|
+
var require_suggestSimilar = __commonJS((exports) => {
|
|
528
|
+
var maxDistance = 3;
|
|
529
|
+
function editDistance(a, b) {
|
|
530
|
+
if (Math.abs(a.length - b.length) > maxDistance)
|
|
531
|
+
return Math.max(a.length, b.length);
|
|
532
|
+
const d = [];
|
|
533
|
+
for (let i = 0;i <= a.length; i++) {
|
|
534
|
+
d[i] = [i];
|
|
535
|
+
}
|
|
536
|
+
for (let j = 0;j <= b.length; j++) {
|
|
537
|
+
d[0][j] = j;
|
|
538
|
+
}
|
|
539
|
+
for (let j = 1;j <= b.length; j++) {
|
|
540
|
+
for (let i = 1;i <= a.length; i++) {
|
|
541
|
+
let cost = 1;
|
|
542
|
+
if (a[i - 1] === b[j - 1]) {
|
|
543
|
+
cost = 0;
|
|
544
|
+
} else {
|
|
545
|
+
cost = 1;
|
|
546
|
+
}
|
|
547
|
+
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
|
|
548
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
549
|
+
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
return d[a.length][b.length];
|
|
554
|
+
}
|
|
555
|
+
function suggestSimilar(word, candidates) {
|
|
556
|
+
if (!candidates || candidates.length === 0)
|
|
557
|
+
return "";
|
|
558
|
+
candidates = Array.from(new Set(candidates));
|
|
559
|
+
const searchingOptions = word.startsWith("--");
|
|
560
|
+
if (searchingOptions) {
|
|
561
|
+
word = word.slice(2);
|
|
562
|
+
candidates = candidates.map((candidate) => candidate.slice(2));
|
|
563
|
+
}
|
|
564
|
+
let similar = [];
|
|
565
|
+
let bestDistance = maxDistance;
|
|
566
|
+
const minSimilarity = 0.4;
|
|
567
|
+
candidates.forEach((candidate) => {
|
|
568
|
+
if (candidate.length <= 1)
|
|
569
|
+
return;
|
|
570
|
+
const distance = editDistance(word, candidate);
|
|
571
|
+
const length = Math.max(word.length, candidate.length);
|
|
572
|
+
const similarity = (length - distance) / length;
|
|
573
|
+
if (similarity > minSimilarity) {
|
|
574
|
+
if (distance < bestDistance) {
|
|
575
|
+
bestDistance = distance;
|
|
576
|
+
similar = [candidate];
|
|
577
|
+
} else if (distance === bestDistance) {
|
|
578
|
+
similar.push(candidate);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
similar.sort((a, b) => a.localeCompare(b));
|
|
583
|
+
if (searchingOptions) {
|
|
584
|
+
similar = similar.map((candidate) => `--${candidate}`);
|
|
585
|
+
}
|
|
586
|
+
if (similar.length > 1) {
|
|
587
|
+
return `
|
|
588
|
+
(Did you mean one of ${similar.join(", ")}?)`;
|
|
589
|
+
}
|
|
590
|
+
if (similar.length === 1) {
|
|
591
|
+
return `
|
|
592
|
+
(Did you mean ${similar[0]}?)`;
|
|
593
|
+
}
|
|
594
|
+
return "";
|
|
595
|
+
}
|
|
596
|
+
exports.suggestSimilar = suggestSimilar;
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
// node_modules/commander/lib/command.js
|
|
600
|
+
var require_command = __commonJS((exports) => {
|
|
601
|
+
var EventEmitter = __require("node:events").EventEmitter;
|
|
602
|
+
var childProcess = __require("node:child_process");
|
|
603
|
+
var path = __require("node:path");
|
|
604
|
+
var fs = __require("node:fs");
|
|
605
|
+
var process2 = __require("node:process");
|
|
606
|
+
var { Argument, humanReadableArgName } = require_argument();
|
|
607
|
+
var { CommanderError } = require_error();
|
|
608
|
+
var { Help } = require_help();
|
|
609
|
+
var { Option, DualOptions } = require_option();
|
|
610
|
+
var { suggestSimilar } = require_suggestSimilar();
|
|
611
|
+
|
|
612
|
+
class Command extends EventEmitter {
|
|
613
|
+
constructor(name) {
|
|
614
|
+
super();
|
|
615
|
+
this.commands = [];
|
|
616
|
+
this.options = [];
|
|
617
|
+
this.parent = null;
|
|
618
|
+
this._allowUnknownOption = false;
|
|
619
|
+
this._allowExcessArguments = true;
|
|
620
|
+
this.registeredArguments = [];
|
|
621
|
+
this._args = this.registeredArguments;
|
|
622
|
+
this.args = [];
|
|
623
|
+
this.rawArgs = [];
|
|
624
|
+
this.processedArgs = [];
|
|
625
|
+
this._scriptPath = null;
|
|
626
|
+
this._name = name || "";
|
|
627
|
+
this._optionValues = {};
|
|
628
|
+
this._optionValueSources = {};
|
|
629
|
+
this._storeOptionsAsProperties = false;
|
|
630
|
+
this._actionHandler = null;
|
|
631
|
+
this._executableHandler = false;
|
|
632
|
+
this._executableFile = null;
|
|
633
|
+
this._executableDir = null;
|
|
634
|
+
this._defaultCommandName = null;
|
|
635
|
+
this._exitCallback = null;
|
|
636
|
+
this._aliases = [];
|
|
637
|
+
this._combineFlagAndOptionalValue = true;
|
|
638
|
+
this._description = "";
|
|
639
|
+
this._summary = "";
|
|
640
|
+
this._argsDescription = undefined;
|
|
641
|
+
this._enablePositionalOptions = false;
|
|
642
|
+
this._passThroughOptions = false;
|
|
643
|
+
this._lifeCycleHooks = {};
|
|
644
|
+
this._showHelpAfterError = false;
|
|
645
|
+
this._showSuggestionAfterError = true;
|
|
646
|
+
this._outputConfiguration = {
|
|
647
|
+
writeOut: (str) => process2.stdout.write(str),
|
|
648
|
+
writeErr: (str) => process2.stderr.write(str),
|
|
649
|
+
getOutHelpWidth: () => process2.stdout.isTTY ? process2.stdout.columns : undefined,
|
|
650
|
+
getErrHelpWidth: () => process2.stderr.isTTY ? process2.stderr.columns : undefined,
|
|
651
|
+
outputError: (str, write) => write(str)
|
|
652
|
+
};
|
|
653
|
+
this._hidden = false;
|
|
654
|
+
this._helpOption = undefined;
|
|
655
|
+
this._addImplicitHelpCommand = undefined;
|
|
656
|
+
this._helpCommand = undefined;
|
|
657
|
+
this._helpConfiguration = {};
|
|
658
|
+
}
|
|
659
|
+
copyInheritedSettings(sourceCommand) {
|
|
660
|
+
this._outputConfiguration = sourceCommand._outputConfiguration;
|
|
661
|
+
this._helpOption = sourceCommand._helpOption;
|
|
662
|
+
this._helpCommand = sourceCommand._helpCommand;
|
|
663
|
+
this._helpConfiguration = sourceCommand._helpConfiguration;
|
|
664
|
+
this._exitCallback = sourceCommand._exitCallback;
|
|
665
|
+
this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
|
|
666
|
+
this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
|
|
667
|
+
this._allowExcessArguments = sourceCommand._allowExcessArguments;
|
|
668
|
+
this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
|
|
669
|
+
this._showHelpAfterError = sourceCommand._showHelpAfterError;
|
|
670
|
+
this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;
|
|
671
|
+
return this;
|
|
672
|
+
}
|
|
673
|
+
_getCommandAndAncestors() {
|
|
674
|
+
const result = [];
|
|
675
|
+
for (let command = this;command; command = command.parent) {
|
|
676
|
+
result.push(command);
|
|
677
|
+
}
|
|
678
|
+
return result;
|
|
679
|
+
}
|
|
680
|
+
command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
|
|
681
|
+
let desc = actionOptsOrExecDesc;
|
|
682
|
+
let opts = execOpts;
|
|
683
|
+
if (typeof desc === "object" && desc !== null) {
|
|
684
|
+
opts = desc;
|
|
685
|
+
desc = null;
|
|
686
|
+
}
|
|
687
|
+
opts = opts || {};
|
|
688
|
+
const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
|
|
689
|
+
const cmd = this.createCommand(name);
|
|
690
|
+
if (desc) {
|
|
691
|
+
cmd.description(desc);
|
|
692
|
+
cmd._executableHandler = true;
|
|
693
|
+
}
|
|
694
|
+
if (opts.isDefault)
|
|
695
|
+
this._defaultCommandName = cmd._name;
|
|
696
|
+
cmd._hidden = !!(opts.noHelp || opts.hidden);
|
|
697
|
+
cmd._executableFile = opts.executableFile || null;
|
|
698
|
+
if (args)
|
|
699
|
+
cmd.arguments(args);
|
|
700
|
+
this._registerCommand(cmd);
|
|
701
|
+
cmd.parent = this;
|
|
702
|
+
cmd.copyInheritedSettings(this);
|
|
703
|
+
if (desc)
|
|
704
|
+
return this;
|
|
705
|
+
return cmd;
|
|
706
|
+
}
|
|
707
|
+
createCommand(name) {
|
|
708
|
+
return new Command(name);
|
|
709
|
+
}
|
|
710
|
+
createHelp() {
|
|
711
|
+
return Object.assign(new Help, this.configureHelp());
|
|
712
|
+
}
|
|
713
|
+
configureHelp(configuration) {
|
|
714
|
+
if (configuration === undefined)
|
|
715
|
+
return this._helpConfiguration;
|
|
716
|
+
this._helpConfiguration = configuration;
|
|
717
|
+
return this;
|
|
718
|
+
}
|
|
719
|
+
configureOutput(configuration) {
|
|
720
|
+
if (configuration === undefined)
|
|
721
|
+
return this._outputConfiguration;
|
|
722
|
+
Object.assign(this._outputConfiguration, configuration);
|
|
723
|
+
return this;
|
|
724
|
+
}
|
|
725
|
+
showHelpAfterError(displayHelp = true) {
|
|
726
|
+
if (typeof displayHelp !== "string")
|
|
727
|
+
displayHelp = !!displayHelp;
|
|
728
|
+
this._showHelpAfterError = displayHelp;
|
|
729
|
+
return this;
|
|
730
|
+
}
|
|
731
|
+
showSuggestionAfterError(displaySuggestion = true) {
|
|
732
|
+
this._showSuggestionAfterError = !!displaySuggestion;
|
|
733
|
+
return this;
|
|
734
|
+
}
|
|
735
|
+
addCommand(cmd, opts) {
|
|
736
|
+
if (!cmd._name) {
|
|
737
|
+
throw new Error(`Command passed to .addCommand() must have a name
|
|
738
|
+
- specify the name in Command constructor or using .name()`);
|
|
739
|
+
}
|
|
740
|
+
opts = opts || {};
|
|
741
|
+
if (opts.isDefault)
|
|
742
|
+
this._defaultCommandName = cmd._name;
|
|
743
|
+
if (opts.noHelp || opts.hidden)
|
|
744
|
+
cmd._hidden = true;
|
|
745
|
+
this._registerCommand(cmd);
|
|
746
|
+
cmd.parent = this;
|
|
747
|
+
cmd._checkForBrokenPassThrough();
|
|
748
|
+
return this;
|
|
749
|
+
}
|
|
750
|
+
createArgument(name, description) {
|
|
751
|
+
return new Argument(name, description);
|
|
752
|
+
}
|
|
753
|
+
argument(name, description, fn, defaultValue) {
|
|
754
|
+
const argument = this.createArgument(name, description);
|
|
755
|
+
if (typeof fn === "function") {
|
|
756
|
+
argument.default(defaultValue).argParser(fn);
|
|
757
|
+
} else {
|
|
758
|
+
argument.default(fn);
|
|
759
|
+
}
|
|
760
|
+
this.addArgument(argument);
|
|
761
|
+
return this;
|
|
762
|
+
}
|
|
763
|
+
arguments(names) {
|
|
764
|
+
names.trim().split(/ +/).forEach((detail) => {
|
|
765
|
+
this.argument(detail);
|
|
766
|
+
});
|
|
767
|
+
return this;
|
|
768
|
+
}
|
|
769
|
+
addArgument(argument) {
|
|
770
|
+
const previousArgument = this.registeredArguments.slice(-1)[0];
|
|
771
|
+
if (previousArgument && previousArgument.variadic) {
|
|
772
|
+
throw new Error(`only the last argument can be variadic '${previousArgument.name()}'`);
|
|
773
|
+
}
|
|
774
|
+
if (argument.required && argument.defaultValue !== undefined && argument.parseArg === undefined) {
|
|
775
|
+
throw new Error(`a default value for a required argument is never used: '${argument.name()}'`);
|
|
776
|
+
}
|
|
777
|
+
this.registeredArguments.push(argument);
|
|
778
|
+
return this;
|
|
779
|
+
}
|
|
780
|
+
helpCommand(enableOrNameAndArgs, description) {
|
|
781
|
+
if (typeof enableOrNameAndArgs === "boolean") {
|
|
782
|
+
this._addImplicitHelpCommand = enableOrNameAndArgs;
|
|
783
|
+
return this;
|
|
784
|
+
}
|
|
785
|
+
enableOrNameAndArgs = enableOrNameAndArgs ?? "help [command]";
|
|
786
|
+
const [, helpName, helpArgs] = enableOrNameAndArgs.match(/([^ ]+) *(.*)/);
|
|
787
|
+
const helpDescription = description ?? "display help for command";
|
|
788
|
+
const helpCommand = this.createCommand(helpName);
|
|
789
|
+
helpCommand.helpOption(false);
|
|
790
|
+
if (helpArgs)
|
|
791
|
+
helpCommand.arguments(helpArgs);
|
|
792
|
+
if (helpDescription)
|
|
793
|
+
helpCommand.description(helpDescription);
|
|
794
|
+
this._addImplicitHelpCommand = true;
|
|
795
|
+
this._helpCommand = helpCommand;
|
|
796
|
+
return this;
|
|
797
|
+
}
|
|
798
|
+
addHelpCommand(helpCommand, deprecatedDescription) {
|
|
799
|
+
if (typeof helpCommand !== "object") {
|
|
800
|
+
this.helpCommand(helpCommand, deprecatedDescription);
|
|
801
|
+
return this;
|
|
802
|
+
}
|
|
803
|
+
this._addImplicitHelpCommand = true;
|
|
804
|
+
this._helpCommand = helpCommand;
|
|
805
|
+
return this;
|
|
806
|
+
}
|
|
807
|
+
_getHelpCommand() {
|
|
808
|
+
const hasImplicitHelpCommand = this._addImplicitHelpCommand ?? (this.commands.length && !this._actionHandler && !this._findCommand("help"));
|
|
809
|
+
if (hasImplicitHelpCommand) {
|
|
810
|
+
if (this._helpCommand === undefined) {
|
|
811
|
+
this.helpCommand(undefined, undefined);
|
|
812
|
+
}
|
|
813
|
+
return this._helpCommand;
|
|
814
|
+
}
|
|
815
|
+
return null;
|
|
816
|
+
}
|
|
817
|
+
hook(event, listener) {
|
|
818
|
+
const allowedValues = ["preSubcommand", "preAction", "postAction"];
|
|
819
|
+
if (!allowedValues.includes(event)) {
|
|
820
|
+
throw new Error(`Unexpected value for event passed to hook : '${event}'.
|
|
821
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
822
|
+
}
|
|
823
|
+
if (this._lifeCycleHooks[event]) {
|
|
824
|
+
this._lifeCycleHooks[event].push(listener);
|
|
825
|
+
} else {
|
|
826
|
+
this._lifeCycleHooks[event] = [listener];
|
|
827
|
+
}
|
|
828
|
+
return this;
|
|
829
|
+
}
|
|
830
|
+
exitOverride(fn) {
|
|
831
|
+
if (fn) {
|
|
832
|
+
this._exitCallback = fn;
|
|
833
|
+
} else {
|
|
834
|
+
this._exitCallback = (err) => {
|
|
835
|
+
if (err.code !== "commander.executeSubCommandAsync") {
|
|
836
|
+
throw err;
|
|
837
|
+
} else {}
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
return this;
|
|
841
|
+
}
|
|
842
|
+
_exit(exitCode, code, message) {
|
|
843
|
+
if (this._exitCallback) {
|
|
844
|
+
this._exitCallback(new CommanderError(exitCode, code, message));
|
|
845
|
+
}
|
|
846
|
+
process2.exit(exitCode);
|
|
847
|
+
}
|
|
848
|
+
action(fn) {
|
|
849
|
+
const listener = (args) => {
|
|
850
|
+
const expectedArgsCount = this.registeredArguments.length;
|
|
851
|
+
const actionArgs = args.slice(0, expectedArgsCount);
|
|
852
|
+
if (this._storeOptionsAsProperties) {
|
|
853
|
+
actionArgs[expectedArgsCount] = this;
|
|
854
|
+
} else {
|
|
855
|
+
actionArgs[expectedArgsCount] = this.opts();
|
|
856
|
+
}
|
|
857
|
+
actionArgs.push(this);
|
|
858
|
+
return fn.apply(this, actionArgs);
|
|
859
|
+
};
|
|
860
|
+
this._actionHandler = listener;
|
|
861
|
+
return this;
|
|
862
|
+
}
|
|
863
|
+
createOption(flags, description) {
|
|
864
|
+
return new Option(flags, description);
|
|
865
|
+
}
|
|
866
|
+
_callParseArg(target, value, previous, invalidArgumentMessage) {
|
|
867
|
+
try {
|
|
868
|
+
return target.parseArg(value, previous);
|
|
869
|
+
} catch (err) {
|
|
870
|
+
if (err.code === "commander.invalidArgument") {
|
|
871
|
+
const message = `${invalidArgumentMessage} ${err.message}`;
|
|
872
|
+
this.error(message, { exitCode: err.exitCode, code: err.code });
|
|
873
|
+
}
|
|
874
|
+
throw err;
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
_registerOption(option) {
|
|
878
|
+
const matchingOption = option.short && this._findOption(option.short) || option.long && this._findOption(option.long);
|
|
879
|
+
if (matchingOption) {
|
|
880
|
+
const matchingFlag = option.long && this._findOption(option.long) ? option.long : option.short;
|
|
881
|
+
throw new Error(`Cannot add option '${option.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'
|
|
882
|
+
- already used by option '${matchingOption.flags}'`);
|
|
883
|
+
}
|
|
884
|
+
this.options.push(option);
|
|
885
|
+
}
|
|
886
|
+
_registerCommand(command) {
|
|
887
|
+
const knownBy = (cmd) => {
|
|
888
|
+
return [cmd.name()].concat(cmd.aliases());
|
|
889
|
+
};
|
|
890
|
+
const alreadyUsed = knownBy(command).find((name) => this._findCommand(name));
|
|
891
|
+
if (alreadyUsed) {
|
|
892
|
+
const existingCmd = knownBy(this._findCommand(alreadyUsed)).join("|");
|
|
893
|
+
const newCmd = knownBy(command).join("|");
|
|
894
|
+
throw new Error(`cannot add command '${newCmd}' as already have command '${existingCmd}'`);
|
|
895
|
+
}
|
|
896
|
+
this.commands.push(command);
|
|
897
|
+
}
|
|
898
|
+
addOption(option) {
|
|
899
|
+
this._registerOption(option);
|
|
900
|
+
const oname = option.name();
|
|
901
|
+
const name = option.attributeName();
|
|
902
|
+
if (option.negate) {
|
|
903
|
+
const positiveLongFlag = option.long.replace(/^--no-/, "--");
|
|
904
|
+
if (!this._findOption(positiveLongFlag)) {
|
|
905
|
+
this.setOptionValueWithSource(name, option.defaultValue === undefined ? true : option.defaultValue, "default");
|
|
906
|
+
}
|
|
907
|
+
} else if (option.defaultValue !== undefined) {
|
|
908
|
+
this.setOptionValueWithSource(name, option.defaultValue, "default");
|
|
909
|
+
}
|
|
910
|
+
const handleOptionValue = (val, invalidValueMessage, valueSource) => {
|
|
911
|
+
if (val == null && option.presetArg !== undefined) {
|
|
912
|
+
val = option.presetArg;
|
|
913
|
+
}
|
|
914
|
+
const oldValue = this.getOptionValue(name);
|
|
915
|
+
if (val !== null && option.parseArg) {
|
|
916
|
+
val = this._callParseArg(option, val, oldValue, invalidValueMessage);
|
|
917
|
+
} else if (val !== null && option.variadic) {
|
|
918
|
+
val = option._concatValue(val, oldValue);
|
|
919
|
+
}
|
|
920
|
+
if (val == null) {
|
|
921
|
+
if (option.negate) {
|
|
922
|
+
val = false;
|
|
923
|
+
} else if (option.isBoolean() || option.optional) {
|
|
924
|
+
val = true;
|
|
925
|
+
} else {
|
|
926
|
+
val = "";
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
this.setOptionValueWithSource(name, val, valueSource);
|
|
930
|
+
};
|
|
931
|
+
this.on("option:" + oname, (val) => {
|
|
932
|
+
const invalidValueMessage = `error: option '${option.flags}' argument '${val}' is invalid.`;
|
|
933
|
+
handleOptionValue(val, invalidValueMessage, "cli");
|
|
934
|
+
});
|
|
935
|
+
if (option.envVar) {
|
|
936
|
+
this.on("optionEnv:" + oname, (val) => {
|
|
937
|
+
const invalidValueMessage = `error: option '${option.flags}' value '${val}' from env '${option.envVar}' is invalid.`;
|
|
938
|
+
handleOptionValue(val, invalidValueMessage, "env");
|
|
939
|
+
});
|
|
940
|
+
}
|
|
941
|
+
return this;
|
|
942
|
+
}
|
|
943
|
+
_optionEx(config, flags, description, fn, defaultValue) {
|
|
944
|
+
if (typeof flags === "object" && flags instanceof Option) {
|
|
945
|
+
throw new Error("To add an Option object use addOption() instead of option() or requiredOption()");
|
|
946
|
+
}
|
|
947
|
+
const option = this.createOption(flags, description);
|
|
948
|
+
option.makeOptionMandatory(!!config.mandatory);
|
|
949
|
+
if (typeof fn === "function") {
|
|
950
|
+
option.default(defaultValue).argParser(fn);
|
|
951
|
+
} else if (fn instanceof RegExp) {
|
|
952
|
+
const regex = fn;
|
|
953
|
+
fn = (val, def) => {
|
|
954
|
+
const m = regex.exec(val);
|
|
955
|
+
return m ? m[0] : def;
|
|
956
|
+
};
|
|
957
|
+
option.default(defaultValue).argParser(fn);
|
|
958
|
+
} else {
|
|
959
|
+
option.default(fn);
|
|
960
|
+
}
|
|
961
|
+
return this.addOption(option);
|
|
962
|
+
}
|
|
963
|
+
option(flags, description, parseArg, defaultValue) {
|
|
964
|
+
return this._optionEx({}, flags, description, parseArg, defaultValue);
|
|
965
|
+
}
|
|
966
|
+
requiredOption(flags, description, parseArg, defaultValue) {
|
|
967
|
+
return this._optionEx({ mandatory: true }, flags, description, parseArg, defaultValue);
|
|
968
|
+
}
|
|
969
|
+
combineFlagAndOptionalValue(combine = true) {
|
|
970
|
+
this._combineFlagAndOptionalValue = !!combine;
|
|
971
|
+
return this;
|
|
972
|
+
}
|
|
973
|
+
allowUnknownOption(allowUnknown = true) {
|
|
974
|
+
this._allowUnknownOption = !!allowUnknown;
|
|
975
|
+
return this;
|
|
976
|
+
}
|
|
977
|
+
allowExcessArguments(allowExcess = true) {
|
|
978
|
+
this._allowExcessArguments = !!allowExcess;
|
|
979
|
+
return this;
|
|
980
|
+
}
|
|
981
|
+
enablePositionalOptions(positional = true) {
|
|
982
|
+
this._enablePositionalOptions = !!positional;
|
|
983
|
+
return this;
|
|
984
|
+
}
|
|
985
|
+
passThroughOptions(passThrough = true) {
|
|
986
|
+
this._passThroughOptions = !!passThrough;
|
|
987
|
+
this._checkForBrokenPassThrough();
|
|
988
|
+
return this;
|
|
989
|
+
}
|
|
990
|
+
_checkForBrokenPassThrough() {
|
|
991
|
+
if (this.parent && this._passThroughOptions && !this.parent._enablePositionalOptions) {
|
|
992
|
+
throw new Error(`passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`);
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
storeOptionsAsProperties(storeAsProperties = true) {
|
|
996
|
+
if (this.options.length) {
|
|
997
|
+
throw new Error("call .storeOptionsAsProperties() before adding options");
|
|
998
|
+
}
|
|
999
|
+
if (Object.keys(this._optionValues).length) {
|
|
1000
|
+
throw new Error("call .storeOptionsAsProperties() before setting option values");
|
|
1001
|
+
}
|
|
1002
|
+
this._storeOptionsAsProperties = !!storeAsProperties;
|
|
1003
|
+
return this;
|
|
1004
|
+
}
|
|
1005
|
+
getOptionValue(key) {
|
|
1006
|
+
if (this._storeOptionsAsProperties) {
|
|
1007
|
+
return this[key];
|
|
1008
|
+
}
|
|
1009
|
+
return this._optionValues[key];
|
|
1010
|
+
}
|
|
1011
|
+
setOptionValue(key, value) {
|
|
1012
|
+
return this.setOptionValueWithSource(key, value, undefined);
|
|
1013
|
+
}
|
|
1014
|
+
setOptionValueWithSource(key, value, source) {
|
|
1015
|
+
if (this._storeOptionsAsProperties) {
|
|
1016
|
+
this[key] = value;
|
|
1017
|
+
} else {
|
|
1018
|
+
this._optionValues[key] = value;
|
|
1019
|
+
}
|
|
1020
|
+
this._optionValueSources[key] = source;
|
|
1021
|
+
return this;
|
|
1022
|
+
}
|
|
1023
|
+
getOptionValueSource(key) {
|
|
1024
|
+
return this._optionValueSources[key];
|
|
1025
|
+
}
|
|
1026
|
+
getOptionValueSourceWithGlobals(key) {
|
|
1027
|
+
let source;
|
|
1028
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1029
|
+
if (cmd.getOptionValueSource(key) !== undefined) {
|
|
1030
|
+
source = cmd.getOptionValueSource(key);
|
|
1031
|
+
}
|
|
1032
|
+
});
|
|
1033
|
+
return source;
|
|
1034
|
+
}
|
|
1035
|
+
_prepareUserArgs(argv, parseOptions) {
|
|
1036
|
+
if (argv !== undefined && !Array.isArray(argv)) {
|
|
1037
|
+
throw new Error("first parameter to parse must be array or undefined");
|
|
1038
|
+
}
|
|
1039
|
+
parseOptions = parseOptions || {};
|
|
1040
|
+
if (argv === undefined && parseOptions.from === undefined) {
|
|
1041
|
+
if (process2.versions?.electron) {
|
|
1042
|
+
parseOptions.from = "electron";
|
|
1043
|
+
}
|
|
1044
|
+
const execArgv = process2.execArgv ?? [];
|
|
1045
|
+
if (execArgv.includes("-e") || execArgv.includes("--eval") || execArgv.includes("-p") || execArgv.includes("--print")) {
|
|
1046
|
+
parseOptions.from = "eval";
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
if (argv === undefined) {
|
|
1050
|
+
argv = process2.argv;
|
|
1051
|
+
}
|
|
1052
|
+
this.rawArgs = argv.slice();
|
|
1053
|
+
let userArgs;
|
|
1054
|
+
switch (parseOptions.from) {
|
|
1055
|
+
case undefined:
|
|
1056
|
+
case "node":
|
|
1057
|
+
this._scriptPath = argv[1];
|
|
1058
|
+
userArgs = argv.slice(2);
|
|
1059
|
+
break;
|
|
1060
|
+
case "electron":
|
|
1061
|
+
if (process2.defaultApp) {
|
|
1062
|
+
this._scriptPath = argv[1];
|
|
1063
|
+
userArgs = argv.slice(2);
|
|
1064
|
+
} else {
|
|
1065
|
+
userArgs = argv.slice(1);
|
|
1066
|
+
}
|
|
1067
|
+
break;
|
|
1068
|
+
case "user":
|
|
1069
|
+
userArgs = argv.slice(0);
|
|
1070
|
+
break;
|
|
1071
|
+
case "eval":
|
|
1072
|
+
userArgs = argv.slice(1);
|
|
1073
|
+
break;
|
|
1074
|
+
default:
|
|
1075
|
+
throw new Error(`unexpected parse option { from: '${parseOptions.from}' }`);
|
|
1076
|
+
}
|
|
1077
|
+
if (!this._name && this._scriptPath)
|
|
1078
|
+
this.nameFromFilename(this._scriptPath);
|
|
1079
|
+
this._name = this._name || "program";
|
|
1080
|
+
return userArgs;
|
|
1081
|
+
}
|
|
1082
|
+
parse(argv, parseOptions) {
|
|
1083
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1084
|
+
this._parseCommand([], userArgs);
|
|
1085
|
+
return this;
|
|
1086
|
+
}
|
|
1087
|
+
async parseAsync(argv, parseOptions) {
|
|
1088
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1089
|
+
await this._parseCommand([], userArgs);
|
|
1090
|
+
return this;
|
|
1091
|
+
}
|
|
1092
|
+
_executeSubCommand(subcommand, args) {
|
|
1093
|
+
args = args.slice();
|
|
1094
|
+
let launchWithNode = false;
|
|
1095
|
+
const sourceExt = [".js", ".ts", ".tsx", ".mjs", ".cjs"];
|
|
1096
|
+
function findFile(baseDir, baseName) {
|
|
1097
|
+
const localBin = path.resolve(baseDir, baseName);
|
|
1098
|
+
if (fs.existsSync(localBin))
|
|
1099
|
+
return localBin;
|
|
1100
|
+
if (sourceExt.includes(path.extname(baseName)))
|
|
1101
|
+
return;
|
|
1102
|
+
const foundExt = sourceExt.find((ext) => fs.existsSync(`${localBin}${ext}`));
|
|
1103
|
+
if (foundExt)
|
|
1104
|
+
return `${localBin}${foundExt}`;
|
|
1105
|
+
return;
|
|
1106
|
+
}
|
|
1107
|
+
this._checkForMissingMandatoryOptions();
|
|
1108
|
+
this._checkForConflictingOptions();
|
|
1109
|
+
let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;
|
|
1110
|
+
let executableDir = this._executableDir || "";
|
|
1111
|
+
if (this._scriptPath) {
|
|
1112
|
+
let resolvedScriptPath;
|
|
1113
|
+
try {
|
|
1114
|
+
resolvedScriptPath = fs.realpathSync(this._scriptPath);
|
|
1115
|
+
} catch (err) {
|
|
1116
|
+
resolvedScriptPath = this._scriptPath;
|
|
1117
|
+
}
|
|
1118
|
+
executableDir = path.resolve(path.dirname(resolvedScriptPath), executableDir);
|
|
1119
|
+
}
|
|
1120
|
+
if (executableDir) {
|
|
1121
|
+
let localFile = findFile(executableDir, executableFile);
|
|
1122
|
+
if (!localFile && !subcommand._executableFile && this._scriptPath) {
|
|
1123
|
+
const legacyName = path.basename(this._scriptPath, path.extname(this._scriptPath));
|
|
1124
|
+
if (legacyName !== this._name) {
|
|
1125
|
+
localFile = findFile(executableDir, `${legacyName}-${subcommand._name}`);
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
executableFile = localFile || executableFile;
|
|
1129
|
+
}
|
|
1130
|
+
launchWithNode = sourceExt.includes(path.extname(executableFile));
|
|
1131
|
+
let proc;
|
|
1132
|
+
if (process2.platform !== "win32") {
|
|
1133
|
+
if (launchWithNode) {
|
|
1134
|
+
args.unshift(executableFile);
|
|
1135
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1136
|
+
proc = childProcess.spawn(process2.argv[0], args, { stdio: "inherit" });
|
|
1137
|
+
} else {
|
|
1138
|
+
proc = childProcess.spawn(executableFile, args, { stdio: "inherit" });
|
|
1139
|
+
}
|
|
1140
|
+
} else {
|
|
1141
|
+
args.unshift(executableFile);
|
|
1142
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1143
|
+
proc = childProcess.spawn(process2.execPath, args, { stdio: "inherit" });
|
|
1144
|
+
}
|
|
1145
|
+
if (!proc.killed) {
|
|
1146
|
+
const signals = ["SIGUSR1", "SIGUSR2", "SIGTERM", "SIGINT", "SIGHUP"];
|
|
1147
|
+
signals.forEach((signal) => {
|
|
1148
|
+
process2.on(signal, () => {
|
|
1149
|
+
if (proc.killed === false && proc.exitCode === null) {
|
|
1150
|
+
proc.kill(signal);
|
|
1151
|
+
}
|
|
1152
|
+
});
|
|
1153
|
+
});
|
|
1154
|
+
}
|
|
1155
|
+
const exitCallback = this._exitCallback;
|
|
1156
|
+
proc.on("close", (code) => {
|
|
1157
|
+
code = code ?? 1;
|
|
1158
|
+
if (!exitCallback) {
|
|
1159
|
+
process2.exit(code);
|
|
1160
|
+
} else {
|
|
1161
|
+
exitCallback(new CommanderError(code, "commander.executeSubCommandAsync", "(close)"));
|
|
1162
|
+
}
|
|
1163
|
+
});
|
|
1164
|
+
proc.on("error", (err) => {
|
|
1165
|
+
if (err.code === "ENOENT") {
|
|
1166
|
+
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";
|
|
1167
|
+
const executableMissing = `'${executableFile}' does not exist
|
|
1168
|
+
- if '${subcommand._name}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
|
|
1169
|
+
- if the default executable name is not suitable, use the executableFile option to supply a custom name or path
|
|
1170
|
+
- ${executableDirMessage}`;
|
|
1171
|
+
throw new Error(executableMissing);
|
|
1172
|
+
} else if (err.code === "EACCES") {
|
|
1173
|
+
throw new Error(`'${executableFile}' not executable`);
|
|
1174
|
+
}
|
|
1175
|
+
if (!exitCallback) {
|
|
1176
|
+
process2.exit(1);
|
|
1177
|
+
} else {
|
|
1178
|
+
const wrappedError = new CommanderError(1, "commander.executeSubCommandAsync", "(error)");
|
|
1179
|
+
wrappedError.nestedError = err;
|
|
1180
|
+
exitCallback(wrappedError);
|
|
1181
|
+
}
|
|
1182
|
+
});
|
|
1183
|
+
this.runningCommand = proc;
|
|
1184
|
+
}
|
|
1185
|
+
_dispatchSubcommand(commandName, operands, unknown) {
|
|
1186
|
+
const subCommand = this._findCommand(commandName);
|
|
1187
|
+
if (!subCommand)
|
|
1188
|
+
this.help({ error: true });
|
|
1189
|
+
let promiseChain;
|
|
1190
|
+
promiseChain = this._chainOrCallSubCommandHook(promiseChain, subCommand, "preSubcommand");
|
|
1191
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
1192
|
+
if (subCommand._executableHandler) {
|
|
1193
|
+
this._executeSubCommand(subCommand, operands.concat(unknown));
|
|
1194
|
+
} else {
|
|
1195
|
+
return subCommand._parseCommand(operands, unknown);
|
|
1196
|
+
}
|
|
1197
|
+
});
|
|
1198
|
+
return promiseChain;
|
|
1199
|
+
}
|
|
1200
|
+
_dispatchHelpCommand(subcommandName) {
|
|
1201
|
+
if (!subcommandName) {
|
|
1202
|
+
this.help();
|
|
1203
|
+
}
|
|
1204
|
+
const subCommand = this._findCommand(subcommandName);
|
|
1205
|
+
if (subCommand && !subCommand._executableHandler) {
|
|
1206
|
+
subCommand.help();
|
|
1207
|
+
}
|
|
1208
|
+
return this._dispatchSubcommand(subcommandName, [], [this._getHelpOption()?.long ?? this._getHelpOption()?.short ?? "--help"]);
|
|
1209
|
+
}
|
|
1210
|
+
_checkNumberOfArguments() {
|
|
1211
|
+
this.registeredArguments.forEach((arg, i) => {
|
|
1212
|
+
if (arg.required && this.args[i] == null) {
|
|
1213
|
+
this.missingArgument(arg.name());
|
|
1214
|
+
}
|
|
1215
|
+
});
|
|
1216
|
+
if (this.registeredArguments.length > 0 && this.registeredArguments[this.registeredArguments.length - 1].variadic) {
|
|
1217
|
+
return;
|
|
1218
|
+
}
|
|
1219
|
+
if (this.args.length > this.registeredArguments.length) {
|
|
1220
|
+
this._excessArguments(this.args);
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
_processArguments() {
|
|
1224
|
+
const myParseArg = (argument, value, previous) => {
|
|
1225
|
+
let parsedValue = value;
|
|
1226
|
+
if (value !== null && argument.parseArg) {
|
|
1227
|
+
const invalidValueMessage = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'.`;
|
|
1228
|
+
parsedValue = this._callParseArg(argument, value, previous, invalidValueMessage);
|
|
1229
|
+
}
|
|
1230
|
+
return parsedValue;
|
|
1231
|
+
};
|
|
1232
|
+
this._checkNumberOfArguments();
|
|
1233
|
+
const processedArgs = [];
|
|
1234
|
+
this.registeredArguments.forEach((declaredArg, index) => {
|
|
1235
|
+
let value = declaredArg.defaultValue;
|
|
1236
|
+
if (declaredArg.variadic) {
|
|
1237
|
+
if (index < this.args.length) {
|
|
1238
|
+
value = this.args.slice(index);
|
|
1239
|
+
if (declaredArg.parseArg) {
|
|
1240
|
+
value = value.reduce((processed, v) => {
|
|
1241
|
+
return myParseArg(declaredArg, v, processed);
|
|
1242
|
+
}, declaredArg.defaultValue);
|
|
1243
|
+
}
|
|
1244
|
+
} else if (value === undefined) {
|
|
1245
|
+
value = [];
|
|
1246
|
+
}
|
|
1247
|
+
} else if (index < this.args.length) {
|
|
1248
|
+
value = this.args[index];
|
|
1249
|
+
if (declaredArg.parseArg) {
|
|
1250
|
+
value = myParseArg(declaredArg, value, declaredArg.defaultValue);
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
processedArgs[index] = value;
|
|
1254
|
+
});
|
|
1255
|
+
this.processedArgs = processedArgs;
|
|
1256
|
+
}
|
|
1257
|
+
_chainOrCall(promise, fn) {
|
|
1258
|
+
if (promise && promise.then && typeof promise.then === "function") {
|
|
1259
|
+
return promise.then(() => fn());
|
|
1260
|
+
}
|
|
1261
|
+
return fn();
|
|
1262
|
+
}
|
|
1263
|
+
_chainOrCallHooks(promise, event) {
|
|
1264
|
+
let result = promise;
|
|
1265
|
+
const hooks = [];
|
|
1266
|
+
this._getCommandAndAncestors().reverse().filter((cmd) => cmd._lifeCycleHooks[event] !== undefined).forEach((hookedCommand) => {
|
|
1267
|
+
hookedCommand._lifeCycleHooks[event].forEach((callback) => {
|
|
1268
|
+
hooks.push({ hookedCommand, callback });
|
|
1269
|
+
});
|
|
1270
|
+
});
|
|
1271
|
+
if (event === "postAction") {
|
|
1272
|
+
hooks.reverse();
|
|
1273
|
+
}
|
|
1274
|
+
hooks.forEach((hookDetail) => {
|
|
1275
|
+
result = this._chainOrCall(result, () => {
|
|
1276
|
+
return hookDetail.callback(hookDetail.hookedCommand, this);
|
|
1277
|
+
});
|
|
1278
|
+
});
|
|
1279
|
+
return result;
|
|
1280
|
+
}
|
|
1281
|
+
_chainOrCallSubCommandHook(promise, subCommand, event) {
|
|
1282
|
+
let result = promise;
|
|
1283
|
+
if (this._lifeCycleHooks[event] !== undefined) {
|
|
1284
|
+
this._lifeCycleHooks[event].forEach((hook) => {
|
|
1285
|
+
result = this._chainOrCall(result, () => {
|
|
1286
|
+
return hook(this, subCommand);
|
|
1287
|
+
});
|
|
1288
|
+
});
|
|
1289
|
+
}
|
|
1290
|
+
return result;
|
|
1291
|
+
}
|
|
1292
|
+
_parseCommand(operands, unknown) {
|
|
1293
|
+
const parsed = this.parseOptions(unknown);
|
|
1294
|
+
this._parseOptionsEnv();
|
|
1295
|
+
this._parseOptionsImplied();
|
|
1296
|
+
operands = operands.concat(parsed.operands);
|
|
1297
|
+
unknown = parsed.unknown;
|
|
1298
|
+
this.args = operands.concat(unknown);
|
|
1299
|
+
if (operands && this._findCommand(operands[0])) {
|
|
1300
|
+
return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
|
|
1301
|
+
}
|
|
1302
|
+
if (this._getHelpCommand() && operands[0] === this._getHelpCommand().name()) {
|
|
1303
|
+
return this._dispatchHelpCommand(operands[1]);
|
|
1304
|
+
}
|
|
1305
|
+
if (this._defaultCommandName) {
|
|
1306
|
+
this._outputHelpIfRequested(unknown);
|
|
1307
|
+
return this._dispatchSubcommand(this._defaultCommandName, operands, unknown);
|
|
1308
|
+
}
|
|
1309
|
+
if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) {
|
|
1310
|
+
this.help({ error: true });
|
|
1311
|
+
}
|
|
1312
|
+
this._outputHelpIfRequested(parsed.unknown);
|
|
1313
|
+
this._checkForMissingMandatoryOptions();
|
|
1314
|
+
this._checkForConflictingOptions();
|
|
1315
|
+
const checkForUnknownOptions = () => {
|
|
1316
|
+
if (parsed.unknown.length > 0) {
|
|
1317
|
+
this.unknownOption(parsed.unknown[0]);
|
|
1318
|
+
}
|
|
1319
|
+
};
|
|
1320
|
+
const commandEvent = `command:${this.name()}`;
|
|
1321
|
+
if (this._actionHandler) {
|
|
1322
|
+
checkForUnknownOptions();
|
|
1323
|
+
this._processArguments();
|
|
1324
|
+
let promiseChain;
|
|
1325
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "preAction");
|
|
1326
|
+
promiseChain = this._chainOrCall(promiseChain, () => this._actionHandler(this.processedArgs));
|
|
1327
|
+
if (this.parent) {
|
|
1328
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
1329
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1330
|
+
});
|
|
1331
|
+
}
|
|
1332
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "postAction");
|
|
1333
|
+
return promiseChain;
|
|
1334
|
+
}
|
|
1335
|
+
if (this.parent && this.parent.listenerCount(commandEvent)) {
|
|
1336
|
+
checkForUnknownOptions();
|
|
1337
|
+
this._processArguments();
|
|
1338
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1339
|
+
} else if (operands.length) {
|
|
1340
|
+
if (this._findCommand("*")) {
|
|
1341
|
+
return this._dispatchSubcommand("*", operands, unknown);
|
|
1342
|
+
}
|
|
1343
|
+
if (this.listenerCount("command:*")) {
|
|
1344
|
+
this.emit("command:*", operands, unknown);
|
|
1345
|
+
} else if (this.commands.length) {
|
|
1346
|
+
this.unknownCommand();
|
|
1347
|
+
} else {
|
|
1348
|
+
checkForUnknownOptions();
|
|
1349
|
+
this._processArguments();
|
|
1350
|
+
}
|
|
1351
|
+
} else if (this.commands.length) {
|
|
1352
|
+
checkForUnknownOptions();
|
|
1353
|
+
this.help({ error: true });
|
|
1354
|
+
} else {
|
|
1355
|
+
checkForUnknownOptions();
|
|
1356
|
+
this._processArguments();
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
_findCommand(name) {
|
|
1360
|
+
if (!name)
|
|
1361
|
+
return;
|
|
1362
|
+
return this.commands.find((cmd) => cmd._name === name || cmd._aliases.includes(name));
|
|
1363
|
+
}
|
|
1364
|
+
_findOption(arg) {
|
|
1365
|
+
return this.options.find((option) => option.is(arg));
|
|
1366
|
+
}
|
|
1367
|
+
_checkForMissingMandatoryOptions() {
|
|
1368
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1369
|
+
cmd.options.forEach((anOption) => {
|
|
1370
|
+
if (anOption.mandatory && cmd.getOptionValue(anOption.attributeName()) === undefined) {
|
|
1371
|
+
cmd.missingMandatoryOptionValue(anOption);
|
|
1372
|
+
}
|
|
1373
|
+
});
|
|
1374
|
+
});
|
|
1375
|
+
}
|
|
1376
|
+
_checkForConflictingLocalOptions() {
|
|
1377
|
+
const definedNonDefaultOptions = this.options.filter((option) => {
|
|
1378
|
+
const optionKey = option.attributeName();
|
|
1379
|
+
if (this.getOptionValue(optionKey) === undefined) {
|
|
1380
|
+
return false;
|
|
1381
|
+
}
|
|
1382
|
+
return this.getOptionValueSource(optionKey) !== "default";
|
|
1383
|
+
});
|
|
1384
|
+
const optionsWithConflicting = definedNonDefaultOptions.filter((option) => option.conflictsWith.length > 0);
|
|
1385
|
+
optionsWithConflicting.forEach((option) => {
|
|
1386
|
+
const conflictingAndDefined = definedNonDefaultOptions.find((defined) => option.conflictsWith.includes(defined.attributeName()));
|
|
1387
|
+
if (conflictingAndDefined) {
|
|
1388
|
+
this._conflictingOption(option, conflictingAndDefined);
|
|
1389
|
+
}
|
|
1390
|
+
});
|
|
1391
|
+
}
|
|
1392
|
+
_checkForConflictingOptions() {
|
|
1393
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1394
|
+
cmd._checkForConflictingLocalOptions();
|
|
1395
|
+
});
|
|
1396
|
+
}
|
|
1397
|
+
parseOptions(argv) {
|
|
1398
|
+
const operands = [];
|
|
1399
|
+
const unknown = [];
|
|
1400
|
+
let dest = operands;
|
|
1401
|
+
const args = argv.slice();
|
|
1402
|
+
function maybeOption(arg) {
|
|
1403
|
+
return arg.length > 1 && arg[0] === "-";
|
|
1404
|
+
}
|
|
1405
|
+
let activeVariadicOption = null;
|
|
1406
|
+
while (args.length) {
|
|
1407
|
+
const arg = args.shift();
|
|
1408
|
+
if (arg === "--") {
|
|
1409
|
+
if (dest === unknown)
|
|
1410
|
+
dest.push(arg);
|
|
1411
|
+
dest.push(...args);
|
|
1412
|
+
break;
|
|
1413
|
+
}
|
|
1414
|
+
if (activeVariadicOption && !maybeOption(arg)) {
|
|
1415
|
+
this.emit(`option:${activeVariadicOption.name()}`, arg);
|
|
1416
|
+
continue;
|
|
1417
|
+
}
|
|
1418
|
+
activeVariadicOption = null;
|
|
1419
|
+
if (maybeOption(arg)) {
|
|
1420
|
+
const option = this._findOption(arg);
|
|
1421
|
+
if (option) {
|
|
1422
|
+
if (option.required) {
|
|
1423
|
+
const value = args.shift();
|
|
1424
|
+
if (value === undefined)
|
|
1425
|
+
this.optionMissingArgument(option);
|
|
1426
|
+
this.emit(`option:${option.name()}`, value);
|
|
1427
|
+
} else if (option.optional) {
|
|
1428
|
+
let value = null;
|
|
1429
|
+
if (args.length > 0 && !maybeOption(args[0])) {
|
|
1430
|
+
value = args.shift();
|
|
1431
|
+
}
|
|
1432
|
+
this.emit(`option:${option.name()}`, value);
|
|
1433
|
+
} else {
|
|
1434
|
+
this.emit(`option:${option.name()}`);
|
|
1435
|
+
}
|
|
1436
|
+
activeVariadicOption = option.variadic ? option : null;
|
|
1437
|
+
continue;
|
|
1438
|
+
}
|
|
1439
|
+
}
|
|
1440
|
+
if (arg.length > 2 && arg[0] === "-" && arg[1] !== "-") {
|
|
1441
|
+
const option = this._findOption(`-${arg[1]}`);
|
|
1442
|
+
if (option) {
|
|
1443
|
+
if (option.required || option.optional && this._combineFlagAndOptionalValue) {
|
|
1444
|
+
this.emit(`option:${option.name()}`, arg.slice(2));
|
|
1445
|
+
} else {
|
|
1446
|
+
this.emit(`option:${option.name()}`);
|
|
1447
|
+
args.unshift(`-${arg.slice(2)}`);
|
|
1448
|
+
}
|
|
1449
|
+
continue;
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
if (/^--[^=]+=/.test(arg)) {
|
|
1453
|
+
const index = arg.indexOf("=");
|
|
1454
|
+
const option = this._findOption(arg.slice(0, index));
|
|
1455
|
+
if (option && (option.required || option.optional)) {
|
|
1456
|
+
this.emit(`option:${option.name()}`, arg.slice(index + 1));
|
|
1457
|
+
continue;
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
if (maybeOption(arg)) {
|
|
1461
|
+
dest = unknown;
|
|
1462
|
+
}
|
|
1463
|
+
if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
|
|
1464
|
+
if (this._findCommand(arg)) {
|
|
1465
|
+
operands.push(arg);
|
|
1466
|
+
if (args.length > 0)
|
|
1467
|
+
unknown.push(...args);
|
|
1468
|
+
break;
|
|
1469
|
+
} else if (this._getHelpCommand() && arg === this._getHelpCommand().name()) {
|
|
1470
|
+
operands.push(arg);
|
|
1471
|
+
if (args.length > 0)
|
|
1472
|
+
operands.push(...args);
|
|
1473
|
+
break;
|
|
1474
|
+
} else if (this._defaultCommandName) {
|
|
1475
|
+
unknown.push(arg);
|
|
1476
|
+
if (args.length > 0)
|
|
1477
|
+
unknown.push(...args);
|
|
1478
|
+
break;
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
if (this._passThroughOptions) {
|
|
1482
|
+
dest.push(arg);
|
|
1483
|
+
if (args.length > 0)
|
|
1484
|
+
dest.push(...args);
|
|
1485
|
+
break;
|
|
1486
|
+
}
|
|
1487
|
+
dest.push(arg);
|
|
1488
|
+
}
|
|
1489
|
+
return { operands, unknown };
|
|
1490
|
+
}
|
|
1491
|
+
opts() {
|
|
1492
|
+
if (this._storeOptionsAsProperties) {
|
|
1493
|
+
const result = {};
|
|
1494
|
+
const len = this.options.length;
|
|
1495
|
+
for (let i = 0;i < len; i++) {
|
|
1496
|
+
const key = this.options[i].attributeName();
|
|
1497
|
+
result[key] = key === this._versionOptionName ? this._version : this[key];
|
|
1498
|
+
}
|
|
1499
|
+
return result;
|
|
1500
|
+
}
|
|
1501
|
+
return this._optionValues;
|
|
1502
|
+
}
|
|
1503
|
+
optsWithGlobals() {
|
|
1504
|
+
return this._getCommandAndAncestors().reduce((combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()), {});
|
|
1505
|
+
}
|
|
1506
|
+
error(message, errorOptions) {
|
|
1507
|
+
this._outputConfiguration.outputError(`${message}
|
|
1508
|
+
`, this._outputConfiguration.writeErr);
|
|
1509
|
+
if (typeof this._showHelpAfterError === "string") {
|
|
1510
|
+
this._outputConfiguration.writeErr(`${this._showHelpAfterError}
|
|
1511
|
+
`);
|
|
1512
|
+
} else if (this._showHelpAfterError) {
|
|
1513
|
+
this._outputConfiguration.writeErr(`
|
|
1514
|
+
`);
|
|
1515
|
+
this.outputHelp({ error: true });
|
|
1516
|
+
}
|
|
1517
|
+
const config = errorOptions || {};
|
|
1518
|
+
const exitCode = config.exitCode || 1;
|
|
1519
|
+
const code = config.code || "commander.error";
|
|
1520
|
+
this._exit(exitCode, code, message);
|
|
1521
|
+
}
|
|
1522
|
+
_parseOptionsEnv() {
|
|
1523
|
+
this.options.forEach((option) => {
|
|
1524
|
+
if (option.envVar && option.envVar in process2.env) {
|
|
1525
|
+
const optionKey = option.attributeName();
|
|
1526
|
+
if (this.getOptionValue(optionKey) === undefined || ["default", "config", "env"].includes(this.getOptionValueSource(optionKey))) {
|
|
1527
|
+
if (option.required || option.optional) {
|
|
1528
|
+
this.emit(`optionEnv:${option.name()}`, process2.env[option.envVar]);
|
|
1529
|
+
} else {
|
|
1530
|
+
this.emit(`optionEnv:${option.name()}`);
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
});
|
|
1535
|
+
}
|
|
1536
|
+
_parseOptionsImplied() {
|
|
1537
|
+
const dualHelper = new DualOptions(this.options);
|
|
1538
|
+
const hasCustomOptionValue = (optionKey) => {
|
|
1539
|
+
return this.getOptionValue(optionKey) !== undefined && !["default", "implied"].includes(this.getOptionValueSource(optionKey));
|
|
1540
|
+
};
|
|
1541
|
+
this.options.filter((option) => option.implied !== undefined && hasCustomOptionValue(option.attributeName()) && dualHelper.valueFromOption(this.getOptionValue(option.attributeName()), option)).forEach((option) => {
|
|
1542
|
+
Object.keys(option.implied).filter((impliedKey) => !hasCustomOptionValue(impliedKey)).forEach((impliedKey) => {
|
|
1543
|
+
this.setOptionValueWithSource(impliedKey, option.implied[impliedKey], "implied");
|
|
1544
|
+
});
|
|
1545
|
+
});
|
|
1546
|
+
}
|
|
1547
|
+
missingArgument(name) {
|
|
1548
|
+
const message = `error: missing required argument '${name}'`;
|
|
1549
|
+
this.error(message, { code: "commander.missingArgument" });
|
|
1550
|
+
}
|
|
1551
|
+
optionMissingArgument(option) {
|
|
1552
|
+
const message = `error: option '${option.flags}' argument missing`;
|
|
1553
|
+
this.error(message, { code: "commander.optionMissingArgument" });
|
|
1554
|
+
}
|
|
1555
|
+
missingMandatoryOptionValue(option) {
|
|
1556
|
+
const message = `error: required option '${option.flags}' not specified`;
|
|
1557
|
+
this.error(message, { code: "commander.missingMandatoryOptionValue" });
|
|
1558
|
+
}
|
|
1559
|
+
_conflictingOption(option, conflictingOption) {
|
|
1560
|
+
const findBestOptionFromValue = (option2) => {
|
|
1561
|
+
const optionKey = option2.attributeName();
|
|
1562
|
+
const optionValue = this.getOptionValue(optionKey);
|
|
1563
|
+
const negativeOption = this.options.find((target) => target.negate && optionKey === target.attributeName());
|
|
1564
|
+
const positiveOption = this.options.find((target) => !target.negate && optionKey === target.attributeName());
|
|
1565
|
+
if (negativeOption && (negativeOption.presetArg === undefined && optionValue === false || negativeOption.presetArg !== undefined && optionValue === negativeOption.presetArg)) {
|
|
1566
|
+
return negativeOption;
|
|
1567
|
+
}
|
|
1568
|
+
return positiveOption || option2;
|
|
1569
|
+
};
|
|
1570
|
+
const getErrorMessage = (option2) => {
|
|
1571
|
+
const bestOption = findBestOptionFromValue(option2);
|
|
1572
|
+
const optionKey = bestOption.attributeName();
|
|
1573
|
+
const source = this.getOptionValueSource(optionKey);
|
|
1574
|
+
if (source === "env") {
|
|
1575
|
+
return `environment variable '${bestOption.envVar}'`;
|
|
1576
|
+
}
|
|
1577
|
+
return `option '${bestOption.flags}'`;
|
|
1578
|
+
};
|
|
1579
|
+
const message = `error: ${getErrorMessage(option)} cannot be used with ${getErrorMessage(conflictingOption)}`;
|
|
1580
|
+
this.error(message, { code: "commander.conflictingOption" });
|
|
1581
|
+
}
|
|
1582
|
+
unknownOption(flag) {
|
|
1583
|
+
if (this._allowUnknownOption)
|
|
1584
|
+
return;
|
|
1585
|
+
let suggestion = "";
|
|
1586
|
+
if (flag.startsWith("--") && this._showSuggestionAfterError) {
|
|
1587
|
+
let candidateFlags = [];
|
|
1588
|
+
let command = this;
|
|
1589
|
+
do {
|
|
1590
|
+
const moreFlags = command.createHelp().visibleOptions(command).filter((option) => option.long).map((option) => option.long);
|
|
1591
|
+
candidateFlags = candidateFlags.concat(moreFlags);
|
|
1592
|
+
command = command.parent;
|
|
1593
|
+
} while (command && !command._enablePositionalOptions);
|
|
1594
|
+
suggestion = suggestSimilar(flag, candidateFlags);
|
|
1595
|
+
}
|
|
1596
|
+
const message = `error: unknown option '${flag}'${suggestion}`;
|
|
1597
|
+
this.error(message, { code: "commander.unknownOption" });
|
|
1598
|
+
}
|
|
1599
|
+
_excessArguments(receivedArgs) {
|
|
1600
|
+
if (this._allowExcessArguments)
|
|
1601
|
+
return;
|
|
1602
|
+
const expected = this.registeredArguments.length;
|
|
1603
|
+
const s = expected === 1 ? "" : "s";
|
|
1604
|
+
const forSubcommand = this.parent ? ` for '${this.name()}'` : "";
|
|
1605
|
+
const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;
|
|
1606
|
+
this.error(message, { code: "commander.excessArguments" });
|
|
1607
|
+
}
|
|
1608
|
+
unknownCommand() {
|
|
1609
|
+
const unknownName = this.args[0];
|
|
1610
|
+
let suggestion = "";
|
|
1611
|
+
if (this._showSuggestionAfterError) {
|
|
1612
|
+
const candidateNames = [];
|
|
1613
|
+
this.createHelp().visibleCommands(this).forEach((command) => {
|
|
1614
|
+
candidateNames.push(command.name());
|
|
1615
|
+
if (command.alias())
|
|
1616
|
+
candidateNames.push(command.alias());
|
|
1617
|
+
});
|
|
1618
|
+
suggestion = suggestSimilar(unknownName, candidateNames);
|
|
1619
|
+
}
|
|
1620
|
+
const message = `error: unknown command '${unknownName}'${suggestion}`;
|
|
1621
|
+
this.error(message, { code: "commander.unknownCommand" });
|
|
1622
|
+
}
|
|
1623
|
+
version(str, flags, description) {
|
|
1624
|
+
if (str === undefined)
|
|
1625
|
+
return this._version;
|
|
1626
|
+
this._version = str;
|
|
1627
|
+
flags = flags || "-V, --version";
|
|
1628
|
+
description = description || "output the version number";
|
|
1629
|
+
const versionOption = this.createOption(flags, description);
|
|
1630
|
+
this._versionOptionName = versionOption.attributeName();
|
|
1631
|
+
this._registerOption(versionOption);
|
|
1632
|
+
this.on("option:" + versionOption.name(), () => {
|
|
1633
|
+
this._outputConfiguration.writeOut(`${str}
|
|
1634
|
+
`);
|
|
1635
|
+
this._exit(0, "commander.version", str);
|
|
1636
|
+
});
|
|
1637
|
+
return this;
|
|
1638
|
+
}
|
|
1639
|
+
description(str, argsDescription) {
|
|
1640
|
+
if (str === undefined && argsDescription === undefined)
|
|
1641
|
+
return this._description;
|
|
1642
|
+
this._description = str;
|
|
1643
|
+
if (argsDescription) {
|
|
1644
|
+
this._argsDescription = argsDescription;
|
|
1645
|
+
}
|
|
1646
|
+
return this;
|
|
1647
|
+
}
|
|
1648
|
+
summary(str) {
|
|
1649
|
+
if (str === undefined)
|
|
1650
|
+
return this._summary;
|
|
1651
|
+
this._summary = str;
|
|
1652
|
+
return this;
|
|
1653
|
+
}
|
|
1654
|
+
alias(alias) {
|
|
1655
|
+
if (alias === undefined)
|
|
1656
|
+
return this._aliases[0];
|
|
1657
|
+
let command = this;
|
|
1658
|
+
if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
|
|
1659
|
+
command = this.commands[this.commands.length - 1];
|
|
1660
|
+
}
|
|
1661
|
+
if (alias === command._name)
|
|
1662
|
+
throw new Error("Command alias can't be the same as its name");
|
|
1663
|
+
const matchingCommand = this.parent?._findCommand(alias);
|
|
1664
|
+
if (matchingCommand) {
|
|
1665
|
+
const existingCmd = [matchingCommand.name()].concat(matchingCommand.aliases()).join("|");
|
|
1666
|
+
throw new Error(`cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`);
|
|
1667
|
+
}
|
|
1668
|
+
command._aliases.push(alias);
|
|
1669
|
+
return this;
|
|
1670
|
+
}
|
|
1671
|
+
aliases(aliases) {
|
|
1672
|
+
if (aliases === undefined)
|
|
1673
|
+
return this._aliases;
|
|
1674
|
+
aliases.forEach((alias) => this.alias(alias));
|
|
1675
|
+
return this;
|
|
1676
|
+
}
|
|
1677
|
+
usage(str) {
|
|
1678
|
+
if (str === undefined) {
|
|
1679
|
+
if (this._usage)
|
|
1680
|
+
return this._usage;
|
|
1681
|
+
const args = this.registeredArguments.map((arg) => {
|
|
1682
|
+
return humanReadableArgName(arg);
|
|
1683
|
+
});
|
|
1684
|
+
return [].concat(this.options.length || this._helpOption !== null ? "[options]" : [], this.commands.length ? "[command]" : [], this.registeredArguments.length ? args : []).join(" ");
|
|
1685
|
+
}
|
|
1686
|
+
this._usage = str;
|
|
1687
|
+
return this;
|
|
1688
|
+
}
|
|
1689
|
+
name(str) {
|
|
1690
|
+
if (str === undefined)
|
|
1691
|
+
return this._name;
|
|
1692
|
+
this._name = str;
|
|
1693
|
+
return this;
|
|
1694
|
+
}
|
|
1695
|
+
nameFromFilename(filename) {
|
|
1696
|
+
this._name = path.basename(filename, path.extname(filename));
|
|
1697
|
+
return this;
|
|
1698
|
+
}
|
|
1699
|
+
executableDir(path2) {
|
|
1700
|
+
if (path2 === undefined)
|
|
1701
|
+
return this._executableDir;
|
|
1702
|
+
this._executableDir = path2;
|
|
1703
|
+
return this;
|
|
1704
|
+
}
|
|
1705
|
+
helpInformation(contextOptions) {
|
|
1706
|
+
const helper = this.createHelp();
|
|
1707
|
+
if (helper.helpWidth === undefined) {
|
|
1708
|
+
helper.helpWidth = contextOptions && contextOptions.error ? this._outputConfiguration.getErrHelpWidth() : this._outputConfiguration.getOutHelpWidth();
|
|
1709
|
+
}
|
|
1710
|
+
return helper.formatHelp(this, helper);
|
|
1711
|
+
}
|
|
1712
|
+
_getHelpContext(contextOptions) {
|
|
1713
|
+
contextOptions = contextOptions || {};
|
|
1714
|
+
const context = { error: !!contextOptions.error };
|
|
1715
|
+
let write;
|
|
1716
|
+
if (context.error) {
|
|
1717
|
+
write = (arg) => this._outputConfiguration.writeErr(arg);
|
|
1718
|
+
} else {
|
|
1719
|
+
write = (arg) => this._outputConfiguration.writeOut(arg);
|
|
1720
|
+
}
|
|
1721
|
+
context.write = contextOptions.write || write;
|
|
1722
|
+
context.command = this;
|
|
1723
|
+
return context;
|
|
1724
|
+
}
|
|
1725
|
+
outputHelp(contextOptions) {
|
|
1726
|
+
let deprecatedCallback;
|
|
1727
|
+
if (typeof contextOptions === "function") {
|
|
1728
|
+
deprecatedCallback = contextOptions;
|
|
1729
|
+
contextOptions = undefined;
|
|
1730
|
+
}
|
|
1731
|
+
const context = this._getHelpContext(contextOptions);
|
|
1732
|
+
this._getCommandAndAncestors().reverse().forEach((command) => command.emit("beforeAllHelp", context));
|
|
1733
|
+
this.emit("beforeHelp", context);
|
|
1734
|
+
let helpInformation = this.helpInformation(context);
|
|
1735
|
+
if (deprecatedCallback) {
|
|
1736
|
+
helpInformation = deprecatedCallback(helpInformation);
|
|
1737
|
+
if (typeof helpInformation !== "string" && !Buffer.isBuffer(helpInformation)) {
|
|
1738
|
+
throw new Error("outputHelp callback must return a string or a Buffer");
|
|
1739
|
+
}
|
|
1740
|
+
}
|
|
1741
|
+
context.write(helpInformation);
|
|
1742
|
+
if (this._getHelpOption()?.long) {
|
|
1743
|
+
this.emit(this._getHelpOption().long);
|
|
1744
|
+
}
|
|
1745
|
+
this.emit("afterHelp", context);
|
|
1746
|
+
this._getCommandAndAncestors().forEach((command) => command.emit("afterAllHelp", context));
|
|
1747
|
+
}
|
|
1748
|
+
helpOption(flags, description) {
|
|
1749
|
+
if (typeof flags === "boolean") {
|
|
1750
|
+
if (flags) {
|
|
1751
|
+
this._helpOption = this._helpOption ?? undefined;
|
|
1752
|
+
} else {
|
|
1753
|
+
this._helpOption = null;
|
|
1754
|
+
}
|
|
1755
|
+
return this;
|
|
1756
|
+
}
|
|
1757
|
+
flags = flags ?? "-h, --help";
|
|
1758
|
+
description = description ?? "display help for command";
|
|
1759
|
+
this._helpOption = this.createOption(flags, description);
|
|
1760
|
+
return this;
|
|
1761
|
+
}
|
|
1762
|
+
_getHelpOption() {
|
|
1763
|
+
if (this._helpOption === undefined) {
|
|
1764
|
+
this.helpOption(undefined, undefined);
|
|
1765
|
+
}
|
|
1766
|
+
return this._helpOption;
|
|
1767
|
+
}
|
|
1768
|
+
addHelpOption(option) {
|
|
1769
|
+
this._helpOption = option;
|
|
1770
|
+
return this;
|
|
1771
|
+
}
|
|
1772
|
+
help(contextOptions) {
|
|
1773
|
+
this.outputHelp(contextOptions);
|
|
1774
|
+
let exitCode = process2.exitCode || 0;
|
|
1775
|
+
if (exitCode === 0 && contextOptions && typeof contextOptions !== "function" && contextOptions.error) {
|
|
1776
|
+
exitCode = 1;
|
|
1777
|
+
}
|
|
1778
|
+
this._exit(exitCode, "commander.help", "(outputHelp)");
|
|
1779
|
+
}
|
|
1780
|
+
addHelpText(position, text) {
|
|
1781
|
+
const allowedValues = ["beforeAll", "before", "after", "afterAll"];
|
|
1782
|
+
if (!allowedValues.includes(position)) {
|
|
1783
|
+
throw new Error(`Unexpected value for position to addHelpText.
|
|
1784
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
1785
|
+
}
|
|
1786
|
+
const helpEvent = `${position}Help`;
|
|
1787
|
+
this.on(helpEvent, (context) => {
|
|
1788
|
+
let helpStr;
|
|
1789
|
+
if (typeof text === "function") {
|
|
1790
|
+
helpStr = text({ error: context.error, command: context.command });
|
|
1791
|
+
} else {
|
|
1792
|
+
helpStr = text;
|
|
1793
|
+
}
|
|
1794
|
+
if (helpStr) {
|
|
1795
|
+
context.write(`${helpStr}
|
|
1796
|
+
`);
|
|
1797
|
+
}
|
|
1798
|
+
});
|
|
1799
|
+
return this;
|
|
1800
|
+
}
|
|
1801
|
+
_outputHelpIfRequested(args) {
|
|
1802
|
+
const helpOption = this._getHelpOption();
|
|
1803
|
+
const helpRequested = helpOption && args.find((arg) => helpOption.is(arg));
|
|
1804
|
+
if (helpRequested) {
|
|
1805
|
+
this.outputHelp();
|
|
1806
|
+
this._exit(0, "commander.helpDisplayed", "(outputHelp)");
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
function incrementNodeInspectorPort(args) {
|
|
1811
|
+
return args.map((arg) => {
|
|
1812
|
+
if (!arg.startsWith("--inspect")) {
|
|
1813
|
+
return arg;
|
|
1814
|
+
}
|
|
1815
|
+
let debugOption;
|
|
1816
|
+
let debugHost = "127.0.0.1";
|
|
1817
|
+
let debugPort = "9229";
|
|
1818
|
+
let match;
|
|
1819
|
+
if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
|
|
1820
|
+
debugOption = match[1];
|
|
1821
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
|
|
1822
|
+
debugOption = match[1];
|
|
1823
|
+
if (/^\d+$/.test(match[3])) {
|
|
1824
|
+
debugPort = match[3];
|
|
1825
|
+
} else {
|
|
1826
|
+
debugHost = match[3];
|
|
1827
|
+
}
|
|
1828
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) {
|
|
1829
|
+
debugOption = match[1];
|
|
1830
|
+
debugHost = match[3];
|
|
1831
|
+
debugPort = match[4];
|
|
1832
|
+
}
|
|
1833
|
+
if (debugOption && debugPort !== "0") {
|
|
1834
|
+
return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
|
|
1835
|
+
}
|
|
1836
|
+
return arg;
|
|
1837
|
+
});
|
|
1838
|
+
}
|
|
1839
|
+
exports.Command = Command;
|
|
1840
|
+
});
|
|
1841
|
+
|
|
1842
|
+
// node_modules/commander/index.js
|
|
1843
|
+
var require_commander = __commonJS((exports) => {
|
|
1844
|
+
var { Argument } = require_argument();
|
|
1845
|
+
var { Command } = require_command();
|
|
1846
|
+
var { CommanderError, InvalidArgumentError } = require_error();
|
|
1847
|
+
var { Help } = require_help();
|
|
1848
|
+
var { Option } = require_option();
|
|
1849
|
+
exports.program = new Command;
|
|
1850
|
+
exports.createCommand = (name) => new Command(name);
|
|
1851
|
+
exports.createOption = (flags, description) => new Option(flags, description);
|
|
1852
|
+
exports.createArgument = (name, description) => new Argument(name, description);
|
|
1853
|
+
exports.Command = Command;
|
|
1854
|
+
exports.Option = Option;
|
|
1855
|
+
exports.Argument = Argument;
|
|
1856
|
+
exports.Help = Help;
|
|
1857
|
+
exports.CommanderError = CommanderError;
|
|
1858
|
+
exports.InvalidArgumentError = InvalidArgumentError;
|
|
1859
|
+
exports.InvalidOptionArgumentError = InvalidArgumentError;
|
|
1860
|
+
});
|
|
1861
|
+
|
|
1862
|
+
// node_modules/commander/esm.mjs
|
|
1863
|
+
var import__ = __toESM(require_commander(), 1);
|
|
1864
|
+
var {
|
|
1865
|
+
program,
|
|
1866
|
+
createCommand,
|
|
1867
|
+
createArgument,
|
|
1868
|
+
createOption,
|
|
1869
|
+
CommanderError,
|
|
1870
|
+
InvalidArgumentError,
|
|
1871
|
+
InvalidOptionArgumentError,
|
|
1872
|
+
Command,
|
|
1873
|
+
Argument,
|
|
1874
|
+
Option,
|
|
1875
|
+
Help
|
|
1876
|
+
} = import__.default;
|
|
1877
|
+
|
|
1878
|
+
// node_modules/commander/esm.mjs
|
|
1879
|
+
var import__2 = __toESM(require_commander(), 1);
|
|
1880
|
+
var {
|
|
1881
|
+
program: program2,
|
|
1882
|
+
createCommand: createCommand2,
|
|
1883
|
+
createArgument: createArgument2,
|
|
1884
|
+
createOption: createOption2,
|
|
1885
|
+
CommanderError: CommanderError2,
|
|
1886
|
+
InvalidArgumentError: InvalidArgumentError2,
|
|
1887
|
+
InvalidOptionArgumentError: InvalidOptionArgumentError2,
|
|
1888
|
+
Command: Command2,
|
|
1889
|
+
Argument: Argument2,
|
|
1890
|
+
Option: Option2,
|
|
1891
|
+
Help: Help2
|
|
1892
|
+
} = import__2.default;
|
|
1893
|
+
|
|
1894
|
+
// src/utils/git.ts
|
|
1895
|
+
import { join } from "node:path";
|
|
1896
|
+
|
|
1897
|
+
// src/utils/paths.ts
|
|
1898
|
+
import { homedir } from "node:os";
|
|
1899
|
+
import { basename } from "node:path";
|
|
1900
|
+
function getProjectName(gitRoot) {
|
|
1901
|
+
return basename(gitRoot);
|
|
1902
|
+
}
|
|
1903
|
+
function formatPath(path) {
|
|
1904
|
+
const home = homedir();
|
|
1905
|
+
if (path.startsWith(home)) {
|
|
1906
|
+
return `~${path.slice(home.length)}`;
|
|
1907
|
+
}
|
|
1908
|
+
return path;
|
|
1909
|
+
}
|
|
1910
|
+
function getTodayDate() {
|
|
1911
|
+
const now = new Date;
|
|
1912
|
+
const year = now.getFullYear();
|
|
1913
|
+
const month = String(now.getMonth() + 1).padStart(2, "0");
|
|
1914
|
+
const day = String(now.getDate()).padStart(2, "0");
|
|
1915
|
+
return `${year}-${month}-${day}`;
|
|
1916
|
+
}
|
|
1917
|
+
function generateWorktreeDirName(alias) {
|
|
1918
|
+
return `${getTodayDate()}-${alias}`;
|
|
1919
|
+
}
|
|
1920
|
+
function parseWorktreeAlias(dirName) {
|
|
1921
|
+
const match = dirName.match(/^\d{4}-\d{2}-\d{2}-(.+)$/);
|
|
1922
|
+
return match?.[1];
|
|
1923
|
+
}
|
|
1924
|
+
|
|
1925
|
+
// src/utils/git.ts
|
|
1926
|
+
async function getGitRoot(cwd) {
|
|
1927
|
+
try {
|
|
1928
|
+
const result = await Bun.$`git rev-parse --show-toplevel`.cwd(cwd ?? process.cwd()).quiet();
|
|
1929
|
+
return result.stdout.toString().trim();
|
|
1930
|
+
} catch {
|
|
1931
|
+
return;
|
|
1932
|
+
}
|
|
1933
|
+
}
|
|
1934
|
+
async function getCurrentBranch(cwd) {
|
|
1935
|
+
const result = await Bun.$`git rev-parse --abbrev-ref HEAD`.cwd(cwd ?? process.cwd()).quiet();
|
|
1936
|
+
return result.stdout.toString().trim();
|
|
1937
|
+
}
|
|
1938
|
+
async function getDefaultBranch(cwd) {
|
|
1939
|
+
try {
|
|
1940
|
+
const result = await Bun.$`git symbolic-ref refs/remotes/origin/HEAD --short`.cwd(cwd ?? process.cwd()).quiet();
|
|
1941
|
+
const branch = result.stdout.toString().trim();
|
|
1942
|
+
return branch.replace(/^origin\//, "");
|
|
1943
|
+
} catch {
|
|
1944
|
+
const branches = await listBranches(cwd);
|
|
1945
|
+
if (branches.includes("main"))
|
|
1946
|
+
return "main";
|
|
1947
|
+
if (branches.includes("master"))
|
|
1948
|
+
return "master";
|
|
1949
|
+
return "main";
|
|
1950
|
+
}
|
|
1951
|
+
}
|
|
1952
|
+
async function listBranches(cwd) {
|
|
1953
|
+
const result = await Bun.$`git branch --format='%(refname:short)'`.cwd(cwd ?? process.cwd()).quiet();
|
|
1954
|
+
return result.stdout.toString().split(`
|
|
1955
|
+
`).map((b) => b.trim()).filter(Boolean);
|
|
1956
|
+
}
|
|
1957
|
+
async function branchExists(branch, cwd) {
|
|
1958
|
+
try {
|
|
1959
|
+
await Bun.$`git show-ref --verify --quiet refs/heads/${branch}`.cwd(cwd ?? process.cwd());
|
|
1960
|
+
return true;
|
|
1961
|
+
} catch {
|
|
1962
|
+
return false;
|
|
1963
|
+
}
|
|
1964
|
+
}
|
|
1965
|
+
async function isBranchMerged(branch, cwd) {
|
|
1966
|
+
try {
|
|
1967
|
+
const defaultBranch = await getDefaultBranch(cwd);
|
|
1968
|
+
const result = await Bun.$`git branch --merged ${defaultBranch} --format='%(refname:short)'`.cwd(cwd ?? process.cwd()).quiet();
|
|
1969
|
+
const mergedBranches = result.stdout.toString().split(`
|
|
1970
|
+
`).map((b) => b.trim());
|
|
1971
|
+
return mergedBranches.includes(branch);
|
|
1972
|
+
} catch {
|
|
1973
|
+
return false;
|
|
1974
|
+
}
|
|
1975
|
+
}
|
|
1976
|
+
function parseWorktreeListOutput(output, projectName) {
|
|
1977
|
+
const worktrees = [];
|
|
1978
|
+
const blocks = output.trim().split(`
|
|
1979
|
+
|
|
1980
|
+
`);
|
|
1981
|
+
for (const block of blocks) {
|
|
1982
|
+
if (!block.trim())
|
|
1983
|
+
continue;
|
|
1984
|
+
const lines = block.split(`
|
|
1985
|
+
`);
|
|
1986
|
+
let path = "";
|
|
1987
|
+
let head = "";
|
|
1988
|
+
let branch = "";
|
|
1989
|
+
let isMain = false;
|
|
1990
|
+
for (const line of lines) {
|
|
1991
|
+
if (line.startsWith("worktree ")) {
|
|
1992
|
+
path = line.slice(9);
|
|
1993
|
+
} else if (line.startsWith("HEAD ")) {
|
|
1994
|
+
head = line.slice(5);
|
|
1995
|
+
} else if (line.startsWith("branch ")) {
|
|
1996
|
+
branch = line.slice(7).replace("refs/heads/", "");
|
|
1997
|
+
} else if (line === "bare") {
|
|
1998
|
+
isMain = true;
|
|
1999
|
+
} else if (line === "detached") {
|
|
2000
|
+
branch = "detached";
|
|
2001
|
+
}
|
|
2002
|
+
}
|
|
2003
|
+
if (worktrees.length === 0) {
|
|
2004
|
+
isMain = true;
|
|
2005
|
+
}
|
|
2006
|
+
const dirName = path.split("/").pop() ?? "";
|
|
2007
|
+
const alias = parseWorktreeAlias(dirName);
|
|
2008
|
+
worktrees.push({
|
|
2009
|
+
path,
|
|
2010
|
+
head,
|
|
2011
|
+
branch,
|
|
2012
|
+
isMain,
|
|
2013
|
+
alias,
|
|
2014
|
+
projectName
|
|
2015
|
+
});
|
|
2016
|
+
}
|
|
2017
|
+
return worktrees;
|
|
2018
|
+
}
|
|
2019
|
+
async function listWorktrees(cwd, projectName) {
|
|
2020
|
+
const result = await Bun.$`git worktree list --porcelain`.cwd(cwd ?? process.cwd()).quiet();
|
|
2021
|
+
return parseWorktreeListOutput(result.stdout.toString(), projectName);
|
|
2022
|
+
}
|
|
2023
|
+
async function createWorktree(path, options, cwd) {
|
|
2024
|
+
const args = ["worktree", "add"];
|
|
2025
|
+
if (options.newBranch) {
|
|
2026
|
+
args.push("-b", options.newBranch);
|
|
2027
|
+
args.push(path);
|
|
2028
|
+
if (options.baseBranch) {
|
|
2029
|
+
args.push(options.baseBranch);
|
|
2030
|
+
}
|
|
2031
|
+
} else if (options.branch) {
|
|
2032
|
+
args.push(path, options.branch);
|
|
2033
|
+
} else {
|
|
2034
|
+
args.push("--detach", path);
|
|
2035
|
+
}
|
|
2036
|
+
await Bun.$`git ${args}`.cwd(cwd ?? process.cwd());
|
|
2037
|
+
}
|
|
2038
|
+
async function removeWorktree(path, force = false, cwd) {
|
|
2039
|
+
const args = ["worktree", "remove"];
|
|
2040
|
+
if (force) {
|
|
2041
|
+
args.push("--force");
|
|
2042
|
+
}
|
|
2043
|
+
args.push(path);
|
|
2044
|
+
await Bun.$`git ${args}`.cwd(cwd ?? process.cwd());
|
|
2045
|
+
}
|
|
2046
|
+
async function pruneWorktrees(cwd) {
|
|
2047
|
+
await Bun.$`git worktree prune`.cwd(cwd ?? process.cwd());
|
|
2048
|
+
}
|
|
2049
|
+
function generateWorktreePath(baseDir, alias) {
|
|
2050
|
+
const dirName = generateWorktreeDirName(alias);
|
|
2051
|
+
return join(baseDir, dirName);
|
|
2052
|
+
}
|
|
2053
|
+
async function findWorktreeByAlias(alias, cwd) {
|
|
2054
|
+
const worktrees = await listWorktrees(cwd);
|
|
2055
|
+
return worktrees.find((wt) => wt.alias === alias);
|
|
2056
|
+
}
|
|
2057
|
+
|
|
2058
|
+
// src/commands/cd.ts
|
|
2059
|
+
var cdCommand = new Command2("cd").description("Print worktree path for shell integration").argument("<alias>", "Alias of the worktree").action(async (alias) => {
|
|
2060
|
+
const gitRoot = await getGitRoot();
|
|
2061
|
+
if (!gitRoot) {
|
|
2062
|
+
process.exit(1);
|
|
2063
|
+
}
|
|
2064
|
+
const worktree = await findWorktreeByAlias(alias, gitRoot);
|
|
2065
|
+
if (!worktree) {
|
|
2066
|
+
process.exit(1);
|
|
2067
|
+
}
|
|
2068
|
+
console.log(worktree.path);
|
|
2069
|
+
});
|
|
2070
|
+
|
|
2071
|
+
// node_modules/chalk/source/vendor/ansi-styles/index.js
|
|
2072
|
+
var ANSI_BACKGROUND_OFFSET = 10;
|
|
2073
|
+
var wrapAnsi16 = (offset = 0) => (code) => `\x1B[${code + offset}m`;
|
|
2074
|
+
var wrapAnsi256 = (offset = 0) => (code) => `\x1B[${38 + offset};5;${code}m`;
|
|
2075
|
+
var wrapAnsi16m = (offset = 0) => (red, green, blue) => `\x1B[${38 + offset};2;${red};${green};${blue}m`;
|
|
2076
|
+
var styles = {
|
|
2077
|
+
modifier: {
|
|
2078
|
+
reset: [0, 0],
|
|
2079
|
+
bold: [1, 22],
|
|
2080
|
+
dim: [2, 22],
|
|
2081
|
+
italic: [3, 23],
|
|
2082
|
+
underline: [4, 24],
|
|
2083
|
+
overline: [53, 55],
|
|
2084
|
+
inverse: [7, 27],
|
|
2085
|
+
hidden: [8, 28],
|
|
2086
|
+
strikethrough: [9, 29]
|
|
2087
|
+
},
|
|
2088
|
+
color: {
|
|
2089
|
+
black: [30, 39],
|
|
2090
|
+
red: [31, 39],
|
|
2091
|
+
green: [32, 39],
|
|
2092
|
+
yellow: [33, 39],
|
|
2093
|
+
blue: [34, 39],
|
|
2094
|
+
magenta: [35, 39],
|
|
2095
|
+
cyan: [36, 39],
|
|
2096
|
+
white: [37, 39],
|
|
2097
|
+
blackBright: [90, 39],
|
|
2098
|
+
gray: [90, 39],
|
|
2099
|
+
grey: [90, 39],
|
|
2100
|
+
redBright: [91, 39],
|
|
2101
|
+
greenBright: [92, 39],
|
|
2102
|
+
yellowBright: [93, 39],
|
|
2103
|
+
blueBright: [94, 39],
|
|
2104
|
+
magentaBright: [95, 39],
|
|
2105
|
+
cyanBright: [96, 39],
|
|
2106
|
+
whiteBright: [97, 39]
|
|
2107
|
+
},
|
|
2108
|
+
bgColor: {
|
|
2109
|
+
bgBlack: [40, 49],
|
|
2110
|
+
bgRed: [41, 49],
|
|
2111
|
+
bgGreen: [42, 49],
|
|
2112
|
+
bgYellow: [43, 49],
|
|
2113
|
+
bgBlue: [44, 49],
|
|
2114
|
+
bgMagenta: [45, 49],
|
|
2115
|
+
bgCyan: [46, 49],
|
|
2116
|
+
bgWhite: [47, 49],
|
|
2117
|
+
bgBlackBright: [100, 49],
|
|
2118
|
+
bgGray: [100, 49],
|
|
2119
|
+
bgGrey: [100, 49],
|
|
2120
|
+
bgRedBright: [101, 49],
|
|
2121
|
+
bgGreenBright: [102, 49],
|
|
2122
|
+
bgYellowBright: [103, 49],
|
|
2123
|
+
bgBlueBright: [104, 49],
|
|
2124
|
+
bgMagentaBright: [105, 49],
|
|
2125
|
+
bgCyanBright: [106, 49],
|
|
2126
|
+
bgWhiteBright: [107, 49]
|
|
2127
|
+
}
|
|
2128
|
+
};
|
|
2129
|
+
var modifierNames = Object.keys(styles.modifier);
|
|
2130
|
+
var foregroundColorNames = Object.keys(styles.color);
|
|
2131
|
+
var backgroundColorNames = Object.keys(styles.bgColor);
|
|
2132
|
+
var colorNames = [...foregroundColorNames, ...backgroundColorNames];
|
|
2133
|
+
function assembleStyles() {
|
|
2134
|
+
const codes = new Map;
|
|
2135
|
+
for (const [groupName, group] of Object.entries(styles)) {
|
|
2136
|
+
for (const [styleName, style] of Object.entries(group)) {
|
|
2137
|
+
styles[styleName] = {
|
|
2138
|
+
open: `\x1B[${style[0]}m`,
|
|
2139
|
+
close: `\x1B[${style[1]}m`
|
|
2140
|
+
};
|
|
2141
|
+
group[styleName] = styles[styleName];
|
|
2142
|
+
codes.set(style[0], style[1]);
|
|
2143
|
+
}
|
|
2144
|
+
Object.defineProperty(styles, groupName, {
|
|
2145
|
+
value: group,
|
|
2146
|
+
enumerable: false
|
|
2147
|
+
});
|
|
2148
|
+
}
|
|
2149
|
+
Object.defineProperty(styles, "codes", {
|
|
2150
|
+
value: codes,
|
|
2151
|
+
enumerable: false
|
|
2152
|
+
});
|
|
2153
|
+
styles.color.close = "\x1B[39m";
|
|
2154
|
+
styles.bgColor.close = "\x1B[49m";
|
|
2155
|
+
styles.color.ansi = wrapAnsi16();
|
|
2156
|
+
styles.color.ansi256 = wrapAnsi256();
|
|
2157
|
+
styles.color.ansi16m = wrapAnsi16m();
|
|
2158
|
+
styles.bgColor.ansi = wrapAnsi16(ANSI_BACKGROUND_OFFSET);
|
|
2159
|
+
styles.bgColor.ansi256 = wrapAnsi256(ANSI_BACKGROUND_OFFSET);
|
|
2160
|
+
styles.bgColor.ansi16m = wrapAnsi16m(ANSI_BACKGROUND_OFFSET);
|
|
2161
|
+
Object.defineProperties(styles, {
|
|
2162
|
+
rgbToAnsi256: {
|
|
2163
|
+
value(red, green, blue) {
|
|
2164
|
+
if (red === green && green === blue) {
|
|
2165
|
+
if (red < 8) {
|
|
2166
|
+
return 16;
|
|
2167
|
+
}
|
|
2168
|
+
if (red > 248) {
|
|
2169
|
+
return 231;
|
|
2170
|
+
}
|
|
2171
|
+
return Math.round((red - 8) / 247 * 24) + 232;
|
|
2172
|
+
}
|
|
2173
|
+
return 16 + 36 * Math.round(red / 255 * 5) + 6 * Math.round(green / 255 * 5) + Math.round(blue / 255 * 5);
|
|
2174
|
+
},
|
|
2175
|
+
enumerable: false
|
|
2176
|
+
},
|
|
2177
|
+
hexToRgb: {
|
|
2178
|
+
value(hex) {
|
|
2179
|
+
const matches = /[a-f\d]{6}|[a-f\d]{3}/i.exec(hex.toString(16));
|
|
2180
|
+
if (!matches) {
|
|
2181
|
+
return [0, 0, 0];
|
|
2182
|
+
}
|
|
2183
|
+
let [colorString] = matches;
|
|
2184
|
+
if (colorString.length === 3) {
|
|
2185
|
+
colorString = [...colorString].map((character) => character + character).join("");
|
|
2186
|
+
}
|
|
2187
|
+
const integer = Number.parseInt(colorString, 16);
|
|
2188
|
+
return [
|
|
2189
|
+
integer >> 16 & 255,
|
|
2190
|
+
integer >> 8 & 255,
|
|
2191
|
+
integer & 255
|
|
2192
|
+
];
|
|
2193
|
+
},
|
|
2194
|
+
enumerable: false
|
|
2195
|
+
},
|
|
2196
|
+
hexToAnsi256: {
|
|
2197
|
+
value: (hex) => styles.rgbToAnsi256(...styles.hexToRgb(hex)),
|
|
2198
|
+
enumerable: false
|
|
2199
|
+
},
|
|
2200
|
+
ansi256ToAnsi: {
|
|
2201
|
+
value(code) {
|
|
2202
|
+
if (code < 8) {
|
|
2203
|
+
return 30 + code;
|
|
2204
|
+
}
|
|
2205
|
+
if (code < 16) {
|
|
2206
|
+
return 90 + (code - 8);
|
|
2207
|
+
}
|
|
2208
|
+
let red;
|
|
2209
|
+
let green;
|
|
2210
|
+
let blue;
|
|
2211
|
+
if (code >= 232) {
|
|
2212
|
+
red = ((code - 232) * 10 + 8) / 255;
|
|
2213
|
+
green = red;
|
|
2214
|
+
blue = red;
|
|
2215
|
+
} else {
|
|
2216
|
+
code -= 16;
|
|
2217
|
+
const remainder = code % 36;
|
|
2218
|
+
red = Math.floor(code / 36) / 5;
|
|
2219
|
+
green = Math.floor(remainder / 6) / 5;
|
|
2220
|
+
blue = remainder % 6 / 5;
|
|
2221
|
+
}
|
|
2222
|
+
const value = Math.max(red, green, blue) * 2;
|
|
2223
|
+
if (value === 0) {
|
|
2224
|
+
return 30;
|
|
2225
|
+
}
|
|
2226
|
+
let result = 30 + (Math.round(blue) << 2 | Math.round(green) << 1 | Math.round(red));
|
|
2227
|
+
if (value === 2) {
|
|
2228
|
+
result += 60;
|
|
2229
|
+
}
|
|
2230
|
+
return result;
|
|
2231
|
+
},
|
|
2232
|
+
enumerable: false
|
|
2233
|
+
},
|
|
2234
|
+
rgbToAnsi: {
|
|
2235
|
+
value: (red, green, blue) => styles.ansi256ToAnsi(styles.rgbToAnsi256(red, green, blue)),
|
|
2236
|
+
enumerable: false
|
|
2237
|
+
},
|
|
2238
|
+
hexToAnsi: {
|
|
2239
|
+
value: (hex) => styles.ansi256ToAnsi(styles.hexToAnsi256(hex)),
|
|
2240
|
+
enumerable: false
|
|
2241
|
+
}
|
|
2242
|
+
});
|
|
2243
|
+
return styles;
|
|
2244
|
+
}
|
|
2245
|
+
var ansiStyles = assembleStyles();
|
|
2246
|
+
var ansi_styles_default = ansiStyles;
|
|
2247
|
+
|
|
2248
|
+
// node_modules/chalk/source/vendor/supports-color/index.js
|
|
2249
|
+
import process2 from "node:process";
|
|
2250
|
+
import os from "node:os";
|
|
2251
|
+
import tty from "node:tty";
|
|
2252
|
+
function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : process2.argv) {
|
|
2253
|
+
const prefix = flag.startsWith("-") ? "" : flag.length === 1 ? "-" : "--";
|
|
2254
|
+
const position = argv.indexOf(prefix + flag);
|
|
2255
|
+
const terminatorPosition = argv.indexOf("--");
|
|
2256
|
+
return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
|
|
2257
|
+
}
|
|
2258
|
+
var { env } = process2;
|
|
2259
|
+
var flagForceColor;
|
|
2260
|
+
if (hasFlag("no-color") || hasFlag("no-colors") || hasFlag("color=false") || hasFlag("color=never")) {
|
|
2261
|
+
flagForceColor = 0;
|
|
2262
|
+
} else if (hasFlag("color") || hasFlag("colors") || hasFlag("color=true") || hasFlag("color=always")) {
|
|
2263
|
+
flagForceColor = 1;
|
|
2264
|
+
}
|
|
2265
|
+
function envForceColor() {
|
|
2266
|
+
if ("FORCE_COLOR" in env) {
|
|
2267
|
+
if (env.FORCE_COLOR === "true") {
|
|
2268
|
+
return 1;
|
|
2269
|
+
}
|
|
2270
|
+
if (env.FORCE_COLOR === "false") {
|
|
2271
|
+
return 0;
|
|
2272
|
+
}
|
|
2273
|
+
return env.FORCE_COLOR.length === 0 ? 1 : Math.min(Number.parseInt(env.FORCE_COLOR, 10), 3);
|
|
2274
|
+
}
|
|
2275
|
+
}
|
|
2276
|
+
function translateLevel(level) {
|
|
2277
|
+
if (level === 0) {
|
|
2278
|
+
return false;
|
|
2279
|
+
}
|
|
2280
|
+
return {
|
|
2281
|
+
level,
|
|
2282
|
+
hasBasic: true,
|
|
2283
|
+
has256: level >= 2,
|
|
2284
|
+
has16m: level >= 3
|
|
2285
|
+
};
|
|
2286
|
+
}
|
|
2287
|
+
function _supportsColor(haveStream, { streamIsTTY, sniffFlags = true } = {}) {
|
|
2288
|
+
const noFlagForceColor = envForceColor();
|
|
2289
|
+
if (noFlagForceColor !== undefined) {
|
|
2290
|
+
flagForceColor = noFlagForceColor;
|
|
2291
|
+
}
|
|
2292
|
+
const forceColor = sniffFlags ? flagForceColor : noFlagForceColor;
|
|
2293
|
+
if (forceColor === 0) {
|
|
2294
|
+
return 0;
|
|
2295
|
+
}
|
|
2296
|
+
if (sniffFlags) {
|
|
2297
|
+
if (hasFlag("color=16m") || hasFlag("color=full") || hasFlag("color=truecolor")) {
|
|
2298
|
+
return 3;
|
|
2299
|
+
}
|
|
2300
|
+
if (hasFlag("color=256")) {
|
|
2301
|
+
return 2;
|
|
2302
|
+
}
|
|
2303
|
+
}
|
|
2304
|
+
if ("TF_BUILD" in env && "AGENT_NAME" in env) {
|
|
2305
|
+
return 1;
|
|
2306
|
+
}
|
|
2307
|
+
if (haveStream && !streamIsTTY && forceColor === undefined) {
|
|
2308
|
+
return 0;
|
|
2309
|
+
}
|
|
2310
|
+
const min = forceColor || 0;
|
|
2311
|
+
if (env.TERM === "dumb") {
|
|
2312
|
+
return min;
|
|
2313
|
+
}
|
|
2314
|
+
if (process2.platform === "win32") {
|
|
2315
|
+
const osRelease = os.release().split(".");
|
|
2316
|
+
if (Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) {
|
|
2317
|
+
return Number(osRelease[2]) >= 14931 ? 3 : 2;
|
|
2318
|
+
}
|
|
2319
|
+
return 1;
|
|
2320
|
+
}
|
|
2321
|
+
if ("CI" in env) {
|
|
2322
|
+
if (["GITHUB_ACTIONS", "GITEA_ACTIONS", "CIRCLECI"].some((key) => (key in env))) {
|
|
2323
|
+
return 3;
|
|
2324
|
+
}
|
|
2325
|
+
if (["TRAVIS", "APPVEYOR", "GITLAB_CI", "BUILDKITE", "DRONE"].some((sign) => (sign in env)) || env.CI_NAME === "codeship") {
|
|
2326
|
+
return 1;
|
|
2327
|
+
}
|
|
2328
|
+
return min;
|
|
2329
|
+
}
|
|
2330
|
+
if ("TEAMCITY_VERSION" in env) {
|
|
2331
|
+
return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
|
|
2332
|
+
}
|
|
2333
|
+
if (env.COLORTERM === "truecolor") {
|
|
2334
|
+
return 3;
|
|
2335
|
+
}
|
|
2336
|
+
if (env.TERM === "xterm-kitty") {
|
|
2337
|
+
return 3;
|
|
2338
|
+
}
|
|
2339
|
+
if (env.TERM === "xterm-ghostty") {
|
|
2340
|
+
return 3;
|
|
2341
|
+
}
|
|
2342
|
+
if (env.TERM === "wezterm") {
|
|
2343
|
+
return 3;
|
|
2344
|
+
}
|
|
2345
|
+
if ("TERM_PROGRAM" in env) {
|
|
2346
|
+
const version = Number.parseInt((env.TERM_PROGRAM_VERSION || "").split(".")[0], 10);
|
|
2347
|
+
switch (env.TERM_PROGRAM) {
|
|
2348
|
+
case "iTerm.app": {
|
|
2349
|
+
return version >= 3 ? 3 : 2;
|
|
2350
|
+
}
|
|
2351
|
+
case "Apple_Terminal": {
|
|
2352
|
+
return 2;
|
|
2353
|
+
}
|
|
2354
|
+
}
|
|
2355
|
+
}
|
|
2356
|
+
if (/-256(color)?$/i.test(env.TERM)) {
|
|
2357
|
+
return 2;
|
|
2358
|
+
}
|
|
2359
|
+
if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
|
|
2360
|
+
return 1;
|
|
2361
|
+
}
|
|
2362
|
+
if ("COLORTERM" in env) {
|
|
2363
|
+
return 1;
|
|
2364
|
+
}
|
|
2365
|
+
return min;
|
|
2366
|
+
}
|
|
2367
|
+
function createSupportsColor(stream, options = {}) {
|
|
2368
|
+
const level = _supportsColor(stream, {
|
|
2369
|
+
streamIsTTY: stream && stream.isTTY,
|
|
2370
|
+
...options
|
|
2371
|
+
});
|
|
2372
|
+
return translateLevel(level);
|
|
2373
|
+
}
|
|
2374
|
+
var supportsColor = {
|
|
2375
|
+
stdout: createSupportsColor({ isTTY: tty.isatty(1) }),
|
|
2376
|
+
stderr: createSupportsColor({ isTTY: tty.isatty(2) })
|
|
2377
|
+
};
|
|
2378
|
+
var supports_color_default = supportsColor;
|
|
2379
|
+
|
|
2380
|
+
// node_modules/chalk/source/utilities.js
|
|
2381
|
+
function stringReplaceAll(string, substring, replacer) {
|
|
2382
|
+
let index = string.indexOf(substring);
|
|
2383
|
+
if (index === -1) {
|
|
2384
|
+
return string;
|
|
2385
|
+
}
|
|
2386
|
+
const substringLength = substring.length;
|
|
2387
|
+
let endIndex = 0;
|
|
2388
|
+
let returnValue = "";
|
|
2389
|
+
do {
|
|
2390
|
+
returnValue += string.slice(endIndex, index) + substring + replacer;
|
|
2391
|
+
endIndex = index + substringLength;
|
|
2392
|
+
index = string.indexOf(substring, endIndex);
|
|
2393
|
+
} while (index !== -1);
|
|
2394
|
+
returnValue += string.slice(endIndex);
|
|
2395
|
+
return returnValue;
|
|
2396
|
+
}
|
|
2397
|
+
function stringEncaseCRLFWithFirstIndex(string, prefix, postfix, index) {
|
|
2398
|
+
let endIndex = 0;
|
|
2399
|
+
let returnValue = "";
|
|
2400
|
+
do {
|
|
2401
|
+
const gotCR = string[index - 1] === "\r";
|
|
2402
|
+
returnValue += string.slice(endIndex, gotCR ? index - 1 : index) + prefix + (gotCR ? `\r
|
|
2403
|
+
` : `
|
|
2404
|
+
`) + postfix;
|
|
2405
|
+
endIndex = index + 1;
|
|
2406
|
+
index = string.indexOf(`
|
|
2407
|
+
`, endIndex);
|
|
2408
|
+
} while (index !== -1);
|
|
2409
|
+
returnValue += string.slice(endIndex);
|
|
2410
|
+
return returnValue;
|
|
2411
|
+
}
|
|
2412
|
+
|
|
2413
|
+
// node_modules/chalk/source/index.js
|
|
2414
|
+
var { stdout: stdoutColor, stderr: stderrColor } = supports_color_default;
|
|
2415
|
+
var GENERATOR = Symbol("GENERATOR");
|
|
2416
|
+
var STYLER = Symbol("STYLER");
|
|
2417
|
+
var IS_EMPTY = Symbol("IS_EMPTY");
|
|
2418
|
+
var levelMapping = [
|
|
2419
|
+
"ansi",
|
|
2420
|
+
"ansi",
|
|
2421
|
+
"ansi256",
|
|
2422
|
+
"ansi16m"
|
|
2423
|
+
];
|
|
2424
|
+
var styles2 = Object.create(null);
|
|
2425
|
+
var applyOptions = (object, options = {}) => {
|
|
2426
|
+
if (options.level && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) {
|
|
2427
|
+
throw new Error("The `level` option should be an integer from 0 to 3");
|
|
2428
|
+
}
|
|
2429
|
+
const colorLevel = stdoutColor ? stdoutColor.level : 0;
|
|
2430
|
+
object.level = options.level === undefined ? colorLevel : options.level;
|
|
2431
|
+
};
|
|
2432
|
+
var chalkFactory = (options) => {
|
|
2433
|
+
const chalk = (...strings) => strings.join(" ");
|
|
2434
|
+
applyOptions(chalk, options);
|
|
2435
|
+
Object.setPrototypeOf(chalk, createChalk.prototype);
|
|
2436
|
+
return chalk;
|
|
2437
|
+
};
|
|
2438
|
+
function createChalk(options) {
|
|
2439
|
+
return chalkFactory(options);
|
|
2440
|
+
}
|
|
2441
|
+
Object.setPrototypeOf(createChalk.prototype, Function.prototype);
|
|
2442
|
+
for (const [styleName, style] of Object.entries(ansi_styles_default)) {
|
|
2443
|
+
styles2[styleName] = {
|
|
2444
|
+
get() {
|
|
2445
|
+
const builder = createBuilder(this, createStyler(style.open, style.close, this[STYLER]), this[IS_EMPTY]);
|
|
2446
|
+
Object.defineProperty(this, styleName, { value: builder });
|
|
2447
|
+
return builder;
|
|
2448
|
+
}
|
|
2449
|
+
};
|
|
2450
|
+
}
|
|
2451
|
+
styles2.visible = {
|
|
2452
|
+
get() {
|
|
2453
|
+
const builder = createBuilder(this, this[STYLER], true);
|
|
2454
|
+
Object.defineProperty(this, "visible", { value: builder });
|
|
2455
|
+
return builder;
|
|
2456
|
+
}
|
|
2457
|
+
};
|
|
2458
|
+
var getModelAnsi = (model, level, type, ...arguments_) => {
|
|
2459
|
+
if (model === "rgb") {
|
|
2460
|
+
if (level === "ansi16m") {
|
|
2461
|
+
return ansi_styles_default[type].ansi16m(...arguments_);
|
|
2462
|
+
}
|
|
2463
|
+
if (level === "ansi256") {
|
|
2464
|
+
return ansi_styles_default[type].ansi256(ansi_styles_default.rgbToAnsi256(...arguments_));
|
|
2465
|
+
}
|
|
2466
|
+
return ansi_styles_default[type].ansi(ansi_styles_default.rgbToAnsi(...arguments_));
|
|
2467
|
+
}
|
|
2468
|
+
if (model === "hex") {
|
|
2469
|
+
return getModelAnsi("rgb", level, type, ...ansi_styles_default.hexToRgb(...arguments_));
|
|
2470
|
+
}
|
|
2471
|
+
return ansi_styles_default[type][model](...arguments_);
|
|
2472
|
+
};
|
|
2473
|
+
var usedModels = ["rgb", "hex", "ansi256"];
|
|
2474
|
+
for (const model of usedModels) {
|
|
2475
|
+
styles2[model] = {
|
|
2476
|
+
get() {
|
|
2477
|
+
const { level } = this;
|
|
2478
|
+
return function(...arguments_) {
|
|
2479
|
+
const styler = createStyler(getModelAnsi(model, levelMapping[level], "color", ...arguments_), ansi_styles_default.color.close, this[STYLER]);
|
|
2480
|
+
return createBuilder(this, styler, this[IS_EMPTY]);
|
|
2481
|
+
};
|
|
2482
|
+
}
|
|
2483
|
+
};
|
|
2484
|
+
const bgModel = "bg" + model[0].toUpperCase() + model.slice(1);
|
|
2485
|
+
styles2[bgModel] = {
|
|
2486
|
+
get() {
|
|
2487
|
+
const { level } = this;
|
|
2488
|
+
return function(...arguments_) {
|
|
2489
|
+
const styler = createStyler(getModelAnsi(model, levelMapping[level], "bgColor", ...arguments_), ansi_styles_default.bgColor.close, this[STYLER]);
|
|
2490
|
+
return createBuilder(this, styler, this[IS_EMPTY]);
|
|
2491
|
+
};
|
|
2492
|
+
}
|
|
2493
|
+
};
|
|
2494
|
+
}
|
|
2495
|
+
var proto = Object.defineProperties(() => {}, {
|
|
2496
|
+
...styles2,
|
|
2497
|
+
level: {
|
|
2498
|
+
enumerable: true,
|
|
2499
|
+
get() {
|
|
2500
|
+
return this[GENERATOR].level;
|
|
2501
|
+
},
|
|
2502
|
+
set(level) {
|
|
2503
|
+
this[GENERATOR].level = level;
|
|
2504
|
+
}
|
|
2505
|
+
}
|
|
2506
|
+
});
|
|
2507
|
+
var createStyler = (open, close, parent) => {
|
|
2508
|
+
let openAll;
|
|
2509
|
+
let closeAll;
|
|
2510
|
+
if (parent === undefined) {
|
|
2511
|
+
openAll = open;
|
|
2512
|
+
closeAll = close;
|
|
2513
|
+
} else {
|
|
2514
|
+
openAll = parent.openAll + open;
|
|
2515
|
+
closeAll = close + parent.closeAll;
|
|
2516
|
+
}
|
|
2517
|
+
return {
|
|
2518
|
+
open,
|
|
2519
|
+
close,
|
|
2520
|
+
openAll,
|
|
2521
|
+
closeAll,
|
|
2522
|
+
parent
|
|
2523
|
+
};
|
|
2524
|
+
};
|
|
2525
|
+
var createBuilder = (self, _styler, _isEmpty) => {
|
|
2526
|
+
const builder = (...arguments_) => applyStyle(builder, arguments_.length === 1 ? "" + arguments_[0] : arguments_.join(" "));
|
|
2527
|
+
Object.setPrototypeOf(builder, proto);
|
|
2528
|
+
builder[GENERATOR] = self;
|
|
2529
|
+
builder[STYLER] = _styler;
|
|
2530
|
+
builder[IS_EMPTY] = _isEmpty;
|
|
2531
|
+
return builder;
|
|
2532
|
+
};
|
|
2533
|
+
var applyStyle = (self, string) => {
|
|
2534
|
+
if (self.level <= 0 || !string) {
|
|
2535
|
+
return self[IS_EMPTY] ? "" : string;
|
|
2536
|
+
}
|
|
2537
|
+
let styler = self[STYLER];
|
|
2538
|
+
if (styler === undefined) {
|
|
2539
|
+
return string;
|
|
2540
|
+
}
|
|
2541
|
+
const { openAll, closeAll } = styler;
|
|
2542
|
+
if (string.includes("\x1B")) {
|
|
2543
|
+
while (styler !== undefined) {
|
|
2544
|
+
string = stringReplaceAll(string, styler.close, styler.open);
|
|
2545
|
+
styler = styler.parent;
|
|
2546
|
+
}
|
|
2547
|
+
}
|
|
2548
|
+
const lfIndex = string.indexOf(`
|
|
2549
|
+
`);
|
|
2550
|
+
if (lfIndex !== -1) {
|
|
2551
|
+
string = stringEncaseCRLFWithFirstIndex(string, closeAll, openAll, lfIndex);
|
|
2552
|
+
}
|
|
2553
|
+
return openAll + string + closeAll;
|
|
2554
|
+
};
|
|
2555
|
+
Object.defineProperties(createChalk.prototype, styles2);
|
|
2556
|
+
var chalk = createChalk();
|
|
2557
|
+
var chalkStderr = createChalk({ level: stderrColor ? stderrColor.level : 0 });
|
|
2558
|
+
var source_default = chalk;
|
|
2559
|
+
|
|
2560
|
+
// src/config/local.ts
|
|
2561
|
+
import { join as join3 } from "node:path";
|
|
2562
|
+
|
|
2563
|
+
// src/config/global.ts
|
|
2564
|
+
import { homedir as homedir2 } from "node:os";
|
|
2565
|
+
import { join as join2 } from "node:path";
|
|
2566
|
+
|
|
2567
|
+
// src/config/types.ts
|
|
2568
|
+
var DEFAULT_GLOBAL_CONFIG = {
|
|
2569
|
+
projects: {},
|
|
2570
|
+
defaults: {
|
|
2571
|
+
worktreeDir: ".worktrees",
|
|
2572
|
+
tmuxWindowTemplate: "{project}-{alias}",
|
|
2573
|
+
autoTmux: false,
|
|
2574
|
+
autoClaude: false
|
|
2575
|
+
}
|
|
2576
|
+
};
|
|
2577
|
+
|
|
2578
|
+
// src/config/global.ts
|
|
2579
|
+
var GLOBAL_CONFIG_PATH = join2(homedir2(), ".wts.json");
|
|
2580
|
+
async function loadGlobalConfig() {
|
|
2581
|
+
const file = Bun.file(GLOBAL_CONFIG_PATH);
|
|
2582
|
+
if (!await file.exists()) {
|
|
2583
|
+
return DEFAULT_GLOBAL_CONFIG;
|
|
2584
|
+
}
|
|
2585
|
+
try {
|
|
2586
|
+
const content = await file.json();
|
|
2587
|
+
return {
|
|
2588
|
+
...DEFAULT_GLOBAL_CONFIG,
|
|
2589
|
+
...content,
|
|
2590
|
+
defaults: {
|
|
2591
|
+
...DEFAULT_GLOBAL_CONFIG.defaults,
|
|
2592
|
+
...content.defaults
|
|
2593
|
+
}
|
|
2594
|
+
};
|
|
2595
|
+
} catch {
|
|
2596
|
+
console.error(`Warning: Failed to parse ${GLOBAL_CONFIG_PATH}, using defaults`);
|
|
2597
|
+
return DEFAULT_GLOBAL_CONFIG;
|
|
2598
|
+
}
|
|
2599
|
+
}
|
|
2600
|
+
async function saveGlobalConfig(config) {
|
|
2601
|
+
await Bun.write(GLOBAL_CONFIG_PATH, JSON.stringify(config, null, 2));
|
|
2602
|
+
}
|
|
2603
|
+
async function registerProject(projectName, projectPath) {
|
|
2604
|
+
const config = await loadGlobalConfig();
|
|
2605
|
+
const entry = {
|
|
2606
|
+
path: projectPath,
|
|
2607
|
+
registeredAt: new Date().toISOString()
|
|
2608
|
+
};
|
|
2609
|
+
config.projects[projectName] = entry;
|
|
2610
|
+
await saveGlobalConfig(config);
|
|
2611
|
+
}
|
|
2612
|
+
async function getTrackedProjects() {
|
|
2613
|
+
const config = await loadGlobalConfig();
|
|
2614
|
+
return config.projects;
|
|
2615
|
+
}
|
|
2616
|
+
async function isProjectRegistered(projectName) {
|
|
2617
|
+
const config = await loadGlobalConfig();
|
|
2618
|
+
return projectName in config.projects;
|
|
2619
|
+
}
|
|
2620
|
+
|
|
2621
|
+
// src/config/local.ts
|
|
2622
|
+
var LOCAL_CONFIG_FILENAME = ".wts-config.json";
|
|
2623
|
+
async function loadLocalConfig(gitRoot) {
|
|
2624
|
+
const configPath = join3(gitRoot, LOCAL_CONFIG_FILENAME);
|
|
2625
|
+
const file = Bun.file(configPath);
|
|
2626
|
+
if (!await file.exists()) {
|
|
2627
|
+
return;
|
|
2628
|
+
}
|
|
2629
|
+
try {
|
|
2630
|
+
return await file.json();
|
|
2631
|
+
} catch {
|
|
2632
|
+
console.error(`Warning: Failed to parse ${configPath}`);
|
|
2633
|
+
return;
|
|
2634
|
+
}
|
|
2635
|
+
}
|
|
2636
|
+
async function saveLocalConfig(gitRoot, config) {
|
|
2637
|
+
const configPath = join3(gitRoot, LOCAL_CONFIG_FILENAME);
|
|
2638
|
+
await Bun.write(configPath, JSON.stringify(config, null, 2));
|
|
2639
|
+
}
|
|
2640
|
+
async function resolveConfig(gitRoot) {
|
|
2641
|
+
const globalConfig = await loadGlobalConfig();
|
|
2642
|
+
const localConfig = await loadLocalConfig(gitRoot);
|
|
2643
|
+
const projectName = getProjectName(gitRoot);
|
|
2644
|
+
return {
|
|
2645
|
+
projectName,
|
|
2646
|
+
gitRoot,
|
|
2647
|
+
worktreeDir: localConfig?.worktreeDir ?? globalConfig.defaults.worktreeDir,
|
|
2648
|
+
tmuxWindowTemplate: localConfig?.tmuxWindowTemplate ?? globalConfig.defaults.tmuxWindowTemplate,
|
|
2649
|
+
autoTmux: localConfig?.autoTmux ?? globalConfig.defaults.autoTmux,
|
|
2650
|
+
autoClaude: localConfig?.autoClaude ?? globalConfig.defaults.autoClaude,
|
|
2651
|
+
setupScript: localConfig?.setupScript
|
|
2652
|
+
};
|
|
2653
|
+
}
|
|
2654
|
+
function getWorktreeBaseDir(config) {
|
|
2655
|
+
return join3(config.gitRoot, config.worktreeDir, config.projectName);
|
|
2656
|
+
}
|
|
2657
|
+
|
|
2658
|
+
// src/utils/prompts.ts
|
|
2659
|
+
import * as readline from "node:readline";
|
|
2660
|
+
async function prompt(question, defaultValue) {
|
|
2661
|
+
const rl = readline.createInterface({
|
|
2662
|
+
input: process.stdin,
|
|
2663
|
+
output: process.stdout
|
|
2664
|
+
});
|
|
2665
|
+
const suffix = defaultValue ? ` (${defaultValue})` : "";
|
|
2666
|
+
return new Promise((resolve) => {
|
|
2667
|
+
rl.question(`${question}${suffix}: `, (answer) => {
|
|
2668
|
+
rl.close();
|
|
2669
|
+
resolve(answer.trim() || defaultValue || "");
|
|
2670
|
+
});
|
|
2671
|
+
});
|
|
2672
|
+
}
|
|
2673
|
+
async function confirm(question, defaultValue = false) {
|
|
2674
|
+
const hint = defaultValue ? "[Y/n]" : "[y/N]";
|
|
2675
|
+
const answer = await prompt(`${question} ${hint}`, "");
|
|
2676
|
+
if (!answer) {
|
|
2677
|
+
return defaultValue;
|
|
2678
|
+
}
|
|
2679
|
+
return answer.toLowerCase().startsWith("y");
|
|
2680
|
+
}
|
|
2681
|
+
async function select(question, options, defaultValue) {
|
|
2682
|
+
console.log(question);
|
|
2683
|
+
for (let i = 0;i < options.length; i++) {
|
|
2684
|
+
const opt = options[i];
|
|
2685
|
+
if (!opt)
|
|
2686
|
+
continue;
|
|
2687
|
+
const marker = opt.value === defaultValue ? "*" : " ";
|
|
2688
|
+
console.log(` ${marker}${i + 1}. ${opt.label}`);
|
|
2689
|
+
}
|
|
2690
|
+
const answer = await prompt("Enter number", defaultValue ? undefined : "1");
|
|
2691
|
+
const index = parseInt(answer, 10) - 1;
|
|
2692
|
+
const selected = options[index];
|
|
2693
|
+
if (selected) {
|
|
2694
|
+
return selected.value;
|
|
2695
|
+
}
|
|
2696
|
+
const match = options.find((o) => o.value === answer);
|
|
2697
|
+
if (match) {
|
|
2698
|
+
return match.value;
|
|
2699
|
+
}
|
|
2700
|
+
const first = options[0];
|
|
2701
|
+
return defaultValue ?? first?.value;
|
|
2702
|
+
}
|
|
2703
|
+
|
|
2704
|
+
// src/commands/cleanup.ts
|
|
2705
|
+
function parseWorktreeDate(dirName) {
|
|
2706
|
+
const match = dirName.match(/^(\d{4}-\d{2}-\d{2})-/);
|
|
2707
|
+
if (!match?.[1]) {
|
|
2708
|
+
return;
|
|
2709
|
+
}
|
|
2710
|
+
const date = new Date(match[1]);
|
|
2711
|
+
return Number.isNaN(date.getTime()) ? undefined : date;
|
|
2712
|
+
}
|
|
2713
|
+
function isOlderThan(worktreePath, days) {
|
|
2714
|
+
const dirName = worktreePath.split("/").pop() ?? "";
|
|
2715
|
+
const date = parseWorktreeDate(dirName);
|
|
2716
|
+
if (!date) {
|
|
2717
|
+
return false;
|
|
2718
|
+
}
|
|
2719
|
+
const now = new Date;
|
|
2720
|
+
const ageMs = now.getTime() - date.getTime();
|
|
2721
|
+
const ageDays = ageMs / (1000 * 60 * 60 * 24);
|
|
2722
|
+
return ageDays > days;
|
|
2723
|
+
}
|
|
2724
|
+
async function categorizeWorktrees(worktrees, gitRoot, options) {
|
|
2725
|
+
const merged = [];
|
|
2726
|
+
const unmerged = [];
|
|
2727
|
+
const stale = [];
|
|
2728
|
+
const active = [];
|
|
2729
|
+
for (const wt of worktrees) {
|
|
2730
|
+
if (wt.isMain) {
|
|
2731
|
+
continue;
|
|
2732
|
+
}
|
|
2733
|
+
const isMerged = await isBranchMerged(wt.branch, gitRoot);
|
|
2734
|
+
if (isMerged) {
|
|
2735
|
+
merged.push(wt);
|
|
2736
|
+
continue;
|
|
2737
|
+
}
|
|
2738
|
+
if (options.olderThanDays !== undefined && isOlderThan(wt.path, options.olderThanDays)) {
|
|
2739
|
+
stale.push(wt);
|
|
2740
|
+
continue;
|
|
2741
|
+
}
|
|
2742
|
+
if (options.includeUnmerged) {
|
|
2743
|
+
unmerged.push(wt);
|
|
2744
|
+
continue;
|
|
2745
|
+
}
|
|
2746
|
+
active.push(wt);
|
|
2747
|
+
}
|
|
2748
|
+
return { merged, unmerged, stale, active };
|
|
2749
|
+
}
|
|
2750
|
+
var cleanupCommand = new Command2("cleanup").description("Remove merged or stale worktrees").option("--dry-run", "Show what would be removed without removing").option("-f, --force", "Force removal without confirmation").option("--older-than <days>", "Include worktrees older than N days").option("--unmerged", "Include all unmerged worktrees").action(async (options) => {
|
|
2751
|
+
const gitRoot = await getGitRoot();
|
|
2752
|
+
if (!gitRoot) {
|
|
2753
|
+
console.error(source_default.red("Error: Not in a git repository"));
|
|
2754
|
+
process.exit(1);
|
|
2755
|
+
}
|
|
2756
|
+
const config = await resolveConfig(gitRoot);
|
|
2757
|
+
const worktrees = await listWorktrees(gitRoot, config.projectName);
|
|
2758
|
+
let olderThanDays;
|
|
2759
|
+
if (options.olderThan) {
|
|
2760
|
+
olderThanDays = Number.parseInt(options.olderThan, 10);
|
|
2761
|
+
if (Number.isNaN(olderThanDays) || olderThanDays < 0) {
|
|
2762
|
+
console.error(source_default.red("Error: --older-than must be a positive number"));
|
|
2763
|
+
process.exit(1);
|
|
2764
|
+
}
|
|
2765
|
+
}
|
|
2766
|
+
await pruneWorktrees(gitRoot);
|
|
2767
|
+
const { merged, unmerged, stale, active } = await categorizeWorktrees(worktrees, gitRoot, {
|
|
2768
|
+
olderThanDays,
|
|
2769
|
+
includeUnmerged: options.unmerged
|
|
2770
|
+
});
|
|
2771
|
+
const toRemove = [...merged, ...unmerged, ...stale];
|
|
2772
|
+
if (toRemove.length === 0) {
|
|
2773
|
+
console.log(source_default.green("No worktrees to clean up"));
|
|
2774
|
+
if (active.length > 0) {
|
|
2775
|
+
console.log(source_default.dim(`${active.length} active worktree(s) remain`));
|
|
2776
|
+
}
|
|
2777
|
+
return;
|
|
2778
|
+
}
|
|
2779
|
+
console.log(source_default.bold(`Worktrees to remove:
|
|
2780
|
+
`));
|
|
2781
|
+
if (merged.length > 0) {
|
|
2782
|
+
console.log(source_default.yellow("Merged branches:"));
|
|
2783
|
+
for (const wt of merged) {
|
|
2784
|
+
console.log(` ${wt.alias ?? wt.branch} ${source_default.dim(`(${formatPath(wt.path)})`)}`);
|
|
2785
|
+
}
|
|
2786
|
+
console.log();
|
|
2787
|
+
}
|
|
2788
|
+
if (unmerged.length > 0) {
|
|
2789
|
+
console.log(source_default.yellow("Unmerged branches:"));
|
|
2790
|
+
for (const wt of unmerged) {
|
|
2791
|
+
console.log(` ${wt.alias ?? wt.branch} ${source_default.dim(`(${formatPath(wt.path)})`)}`);
|
|
2792
|
+
}
|
|
2793
|
+
console.log();
|
|
2794
|
+
}
|
|
2795
|
+
if (stale.length > 0) {
|
|
2796
|
+
console.log(source_default.yellow(`Older than ${olderThanDays} days:`));
|
|
2797
|
+
for (const wt of stale) {
|
|
2798
|
+
console.log(` ${wt.alias ?? wt.branch} ${source_default.dim(`(${formatPath(wt.path)})`)}`);
|
|
2799
|
+
}
|
|
2800
|
+
console.log();
|
|
2801
|
+
}
|
|
2802
|
+
if (options.dryRun) {
|
|
2803
|
+
console.log(source_default.dim("(dry run - no changes made)"));
|
|
2804
|
+
return;
|
|
2805
|
+
}
|
|
2806
|
+
if (!options.force) {
|
|
2807
|
+
const shouldProceed = await confirm(`Remove ${toRemove.length} worktree(s)?`, false);
|
|
2808
|
+
if (!shouldProceed) {
|
|
2809
|
+
console.log(source_default.dim("Cancelled"));
|
|
2810
|
+
return;
|
|
2811
|
+
}
|
|
2812
|
+
}
|
|
2813
|
+
let removed = 0;
|
|
2814
|
+
let failed = 0;
|
|
2815
|
+
for (const wt of toRemove) {
|
|
2816
|
+
try {
|
|
2817
|
+
console.log(source_default.dim(`Removing ${wt.alias ?? wt.branch}...`));
|
|
2818
|
+
await removeWorktree(wt.path, true, gitRoot);
|
|
2819
|
+
removed++;
|
|
2820
|
+
} catch (error) {
|
|
2821
|
+
console.error(source_default.red(` Failed to remove ${wt.alias ?? wt.branch}:`));
|
|
2822
|
+
console.error(source_default.dim(` ${error instanceof Error ? error.message : String(error)}`));
|
|
2823
|
+
failed++;
|
|
2824
|
+
}
|
|
2825
|
+
}
|
|
2826
|
+
console.log();
|
|
2827
|
+
if (removed > 0) {
|
|
2828
|
+
console.log(source_default.green(`Removed ${removed} worktree(s)`));
|
|
2829
|
+
}
|
|
2830
|
+
if (failed > 0) {
|
|
2831
|
+
console.log(source_default.red(`Failed to remove ${failed} worktree(s)`));
|
|
2832
|
+
}
|
|
2833
|
+
if (active.length > 0) {
|
|
2834
|
+
console.log(source_default.dim(`${active.length} active worktree(s) remain`));
|
|
2835
|
+
}
|
|
2836
|
+
});
|
|
2837
|
+
|
|
2838
|
+
// src/commands/create.ts
|
|
2839
|
+
import { mkdir } from "node:fs/promises";
|
|
2840
|
+
import { dirname } from "node:path";
|
|
2841
|
+
|
|
2842
|
+
// src/integrations/fzf.ts
|
|
2843
|
+
async function isFzfAvailable() {
|
|
2844
|
+
try {
|
|
2845
|
+
await Bun.$`which fzf`.quiet();
|
|
2846
|
+
return true;
|
|
2847
|
+
} catch {
|
|
2848
|
+
return false;
|
|
2849
|
+
}
|
|
2850
|
+
}
|
|
2851
|
+
async function fzfSelect(items, options = {}) {
|
|
2852
|
+
const args = [];
|
|
2853
|
+
if (options.prompt) {
|
|
2854
|
+
args.push("--prompt", options.prompt);
|
|
2855
|
+
}
|
|
2856
|
+
if (options.header) {
|
|
2857
|
+
args.push("--header", options.header);
|
|
2858
|
+
}
|
|
2859
|
+
if (options.preview) {
|
|
2860
|
+
args.push("--preview", options.preview);
|
|
2861
|
+
}
|
|
2862
|
+
if (options.multi) {
|
|
2863
|
+
args.push("--multi");
|
|
2864
|
+
}
|
|
2865
|
+
if (options.height) {
|
|
2866
|
+
args.push("--height", options.height);
|
|
2867
|
+
}
|
|
2868
|
+
args.push("--border", "--reverse");
|
|
2869
|
+
try {
|
|
2870
|
+
const input = items.join(`
|
|
2871
|
+
`);
|
|
2872
|
+
const proc = Bun.spawn(["fzf", ...args], {
|
|
2873
|
+
stdin: "pipe",
|
|
2874
|
+
stdout: "pipe",
|
|
2875
|
+
stderr: "inherit"
|
|
2876
|
+
});
|
|
2877
|
+
proc.stdin.write(input);
|
|
2878
|
+
proc.stdin.end();
|
|
2879
|
+
const output = await new Response(proc.stdout).text();
|
|
2880
|
+
const exitCode = await proc.exited;
|
|
2881
|
+
if (exitCode !== 0) {
|
|
2882
|
+
return;
|
|
2883
|
+
}
|
|
2884
|
+
return output.trim().split(`
|
|
2885
|
+
`).filter((s) => s.length > 0);
|
|
2886
|
+
} catch {
|
|
2887
|
+
return;
|
|
2888
|
+
}
|
|
2889
|
+
}
|
|
2890
|
+
function formatWorktreeForFzf(wt) {
|
|
2891
|
+
const alias = wt.alias ?? wt.branch;
|
|
2892
|
+
const path = formatPath(wt.path);
|
|
2893
|
+
return `${alias} ${wt.branch} ${path}`;
|
|
2894
|
+
}
|
|
2895
|
+
function parseWorktreeFromFzf(selection) {
|
|
2896
|
+
const parts = selection.split("\t");
|
|
2897
|
+
return parts[0] ?? selection;
|
|
2898
|
+
}
|
|
2899
|
+
async function selectWorktree(worktrees, options = {}) {
|
|
2900
|
+
const filteredWorktrees = options.excludeMain ? worktrees.filter((wt) => !wt.isMain) : worktrees;
|
|
2901
|
+
if (filteredWorktrees.length === 0) {
|
|
2902
|
+
return;
|
|
2903
|
+
}
|
|
2904
|
+
const items = filteredWorktrees.map(formatWorktreeForFzf);
|
|
2905
|
+
const result = await fzfSelect(items, {
|
|
2906
|
+
prompt: options.prompt ?? "Select worktree > ",
|
|
2907
|
+
header: "ALIAS\tBRANCH\tPATH",
|
|
2908
|
+
preview: "ls -la {3} 2>/dev/null || echo 'Directory not found'",
|
|
2909
|
+
height: "50%"
|
|
2910
|
+
});
|
|
2911
|
+
if (!result || result.length === 0) {
|
|
2912
|
+
return;
|
|
2913
|
+
}
|
|
2914
|
+
const firstResult = result[0];
|
|
2915
|
+
if (!firstResult) {
|
|
2916
|
+
return;
|
|
2917
|
+
}
|
|
2918
|
+
const selectedAlias = parseWorktreeFromFzf(firstResult);
|
|
2919
|
+
return filteredWorktrees.find((wt) => (wt.alias ?? wt.branch) === selectedAlias);
|
|
2920
|
+
}
|
|
2921
|
+
|
|
2922
|
+
// src/integrations/tmux.ts
|
|
2923
|
+
function isInsideTmux() {
|
|
2924
|
+
return !!process.env.TMUX;
|
|
2925
|
+
}
|
|
2926
|
+
async function windowExists(name) {
|
|
2927
|
+
try {
|
|
2928
|
+
const result = await Bun.$`tmux list-windows -F '#W'`.quiet();
|
|
2929
|
+
const windows = result.stdout.toString().trim().split(`
|
|
2930
|
+
`);
|
|
2931
|
+
return windows.includes(name);
|
|
2932
|
+
} catch {
|
|
2933
|
+
return false;
|
|
2934
|
+
}
|
|
2935
|
+
}
|
|
2936
|
+
async function createWindow(name, cwd) {
|
|
2937
|
+
try {
|
|
2938
|
+
await Bun.$`tmux new-window -n ${name} -c ${cwd}`;
|
|
2939
|
+
return true;
|
|
2940
|
+
} catch {
|
|
2941
|
+
return false;
|
|
2942
|
+
}
|
|
2943
|
+
}
|
|
2944
|
+
async function switchToWindow(name) {
|
|
2945
|
+
try {
|
|
2946
|
+
await Bun.$`tmux select-window -t ${name}`;
|
|
2947
|
+
return true;
|
|
2948
|
+
} catch {
|
|
2949
|
+
return false;
|
|
2950
|
+
}
|
|
2951
|
+
}
|
|
2952
|
+
async function sendKeys(keys, target) {
|
|
2953
|
+
try {
|
|
2954
|
+
if (target) {
|
|
2955
|
+
await Bun.$`tmux send-keys -t ${target} ${keys} Enter`;
|
|
2956
|
+
} else {
|
|
2957
|
+
await Bun.$`tmux send-keys ${keys} Enter`;
|
|
2958
|
+
}
|
|
2959
|
+
return true;
|
|
2960
|
+
} catch {
|
|
2961
|
+
return false;
|
|
2962
|
+
}
|
|
2963
|
+
}
|
|
2964
|
+
function resolveWindowName(template, project, alias) {
|
|
2965
|
+
return template.replace("{project}", project).replace("{alias}", alias);
|
|
2966
|
+
}
|
|
2967
|
+
async function createWorktreeWindow(options) {
|
|
2968
|
+
const { windowName, worktreePath, launchClaude } = options;
|
|
2969
|
+
const exists = await windowExists(windowName);
|
|
2970
|
+
if (exists) {
|
|
2971
|
+
return switchToWindow(windowName);
|
|
2972
|
+
}
|
|
2973
|
+
const created = await createWindow(windowName, worktreePath);
|
|
2974
|
+
if (!created) {
|
|
2975
|
+
return false;
|
|
2976
|
+
}
|
|
2977
|
+
if (launchClaude) {
|
|
2978
|
+
await Bun.sleep(100);
|
|
2979
|
+
await sendKeys("claude");
|
|
2980
|
+
}
|
|
2981
|
+
return true;
|
|
2982
|
+
}
|
|
2983
|
+
|
|
2984
|
+
// src/setup/runner.ts
|
|
2985
|
+
import { access } from "node:fs/promises";
|
|
2986
|
+
import { join as join4 } from "node:path";
|
|
2987
|
+
async function runSetupScript(worktreePath, scriptPath, gitRoot) {
|
|
2988
|
+
const fullScriptPath = join4(gitRoot, scriptPath);
|
|
2989
|
+
try {
|
|
2990
|
+
await access(fullScriptPath);
|
|
2991
|
+
} catch {
|
|
2992
|
+
console.log(source_default.yellow(`Setup script not found: ${scriptPath}`));
|
|
2993
|
+
return;
|
|
2994
|
+
}
|
|
2995
|
+
const ext = scriptPath.split(".").pop()?.toLowerCase();
|
|
2996
|
+
try {
|
|
2997
|
+
if (ext === "ts") {
|
|
2998
|
+
await Bun.$`bun ${fullScriptPath}`.cwd(worktreePath).env({ ...process.env, WTS_WORKTREE_PATH: worktreePath });
|
|
2999
|
+
} else if (ext === "sh") {
|
|
3000
|
+
await Bun.$`bash ${fullScriptPath}`.cwd(worktreePath).env({ ...process.env, WTS_WORKTREE_PATH: worktreePath });
|
|
3001
|
+
} else if (ext === "js" || ext === "mjs") {
|
|
3002
|
+
await Bun.$`bun ${fullScriptPath}`.cwd(worktreePath).env({ ...process.env, WTS_WORKTREE_PATH: worktreePath });
|
|
3003
|
+
} else {
|
|
3004
|
+
await Bun.$`${fullScriptPath}`.cwd(worktreePath).env({ ...process.env, WTS_WORKTREE_PATH: worktreePath });
|
|
3005
|
+
}
|
|
3006
|
+
console.log(source_default.green("Setup script completed"));
|
|
3007
|
+
} catch (error) {
|
|
3008
|
+
console.error(source_default.red("Setup script failed:"));
|
|
3009
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
3010
|
+
}
|
|
3011
|
+
}
|
|
3012
|
+
|
|
3013
|
+
// src/commands/create.ts
|
|
3014
|
+
var createCommand3 = new Command2("create").description("Create a new worktree").argument("<alias>", "Short name for the worktree").option("-b, --branch <branch>", "Use existing branch (interactive if not specified)").option("-n, --new-branch", "Create a new branch with the alias name").option("--base <branch>", "Base branch for new branch (defaults to main/master)").option("--no-setup", "Skip running setup script").option("--tmux", "Open in a new tmux window after creation").option("--no-tmux", "Do not open tmux window (overrides config)").option("--claude", "Launch Claude Code in the tmux window").action(async (alias, options) => {
|
|
3015
|
+
const gitRoot = await getGitRoot();
|
|
3016
|
+
if (!gitRoot) {
|
|
3017
|
+
console.error(source_default.red("Error: Not in a git repository"));
|
|
3018
|
+
process.exit(1);
|
|
3019
|
+
}
|
|
3020
|
+
const existing = await findWorktreeByAlias(alias, gitRoot);
|
|
3021
|
+
if (existing) {
|
|
3022
|
+
console.error(source_default.red(`Error: Worktree with alias "${alias}" already exists`));
|
|
3023
|
+
console.error(source_default.dim(` Path: ${existing.path}`));
|
|
3024
|
+
process.exit(1);
|
|
3025
|
+
}
|
|
3026
|
+
const config = await resolveConfig(gitRoot);
|
|
3027
|
+
const baseDir = getWorktreeBaseDir(config);
|
|
3028
|
+
const worktreePath = generateWorktreePath(baseDir, alias);
|
|
3029
|
+
await mkdir(dirname(worktreePath), { recursive: true });
|
|
3030
|
+
let branchConfig = {};
|
|
3031
|
+
if (options.branch) {
|
|
3032
|
+
const exists = await branchExists(options.branch, gitRoot);
|
|
3033
|
+
if (!exists) {
|
|
3034
|
+
console.error(source_default.red(`Error: Branch "${options.branch}" does not exist`));
|
|
3035
|
+
process.exit(1);
|
|
3036
|
+
}
|
|
3037
|
+
branchConfig = { branch: options.branch };
|
|
3038
|
+
} else if (options.newBranch) {
|
|
3039
|
+
const baseBranch = options.base ?? await getDefaultBranch(gitRoot);
|
|
3040
|
+
branchConfig = { newBranch: alias, baseBranch };
|
|
3041
|
+
} else {
|
|
3042
|
+
const baseBranch = options.base ?? await getDefaultBranch(gitRoot);
|
|
3043
|
+
branchConfig = { newBranch: alias, baseBranch };
|
|
3044
|
+
}
|
|
3045
|
+
console.log(source_default.dim(`Creating worktree at ${worktreePath}...`));
|
|
3046
|
+
try {
|
|
3047
|
+
await createWorktree(worktreePath, branchConfig, gitRoot);
|
|
3048
|
+
} catch (error) {
|
|
3049
|
+
console.error(source_default.red("Error creating worktree:"));
|
|
3050
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
3051
|
+
process.exit(1);
|
|
3052
|
+
}
|
|
3053
|
+
console.log(source_default.green(`Created worktree "${alias}"`));
|
|
3054
|
+
console.log(source_default.dim(` Path: ${worktreePath}`));
|
|
3055
|
+
console.log(source_default.dim(` Branch: ${branchConfig.newBranch ?? branchConfig.branch}`));
|
|
3056
|
+
if (options.noSetup !== true && config.setupScript) {
|
|
3057
|
+
console.log(source_default.dim(`
|
|
3058
|
+
Running setup script...`));
|
|
3059
|
+
await runSetupScript(worktreePath, config.setupScript, gitRoot);
|
|
3060
|
+
}
|
|
3061
|
+
const useTmux = options.noTmux === true ? false : options.tmux ?? config.autoTmux;
|
|
3062
|
+
const launchClaude = options.claude ?? config.autoClaude;
|
|
3063
|
+
if (useTmux) {
|
|
3064
|
+
if (!isInsideTmux()) {
|
|
3065
|
+
console.log(source_default.dim(`
|
|
3066
|
+
(Note: tmux requested but not inside tmux session)`));
|
|
3067
|
+
} else {
|
|
3068
|
+
const windowName = resolveWindowName(config.tmuxWindowTemplate, config.projectName, alias);
|
|
3069
|
+
console.log(source_default.dim(`
|
|
3070
|
+
Opening tmux window "${windowName}"...`));
|
|
3071
|
+
const success = await createWorktreeWindow({
|
|
3072
|
+
windowName,
|
|
3073
|
+
worktreePath,
|
|
3074
|
+
launchClaude
|
|
3075
|
+
});
|
|
3076
|
+
if (success) {
|
|
3077
|
+
console.log(source_default.green(`Opened in tmux window "${windowName}"`));
|
|
3078
|
+
} else {
|
|
3079
|
+
console.error(source_default.yellow("Warning: Failed to create tmux window"));
|
|
3080
|
+
}
|
|
3081
|
+
}
|
|
3082
|
+
}
|
|
3083
|
+
});
|
|
3084
|
+
|
|
3085
|
+
// src/commands/delete.ts
|
|
3086
|
+
var deleteCommand = new Command2("delete").alias("rm").description("Remove a worktree").argument("<alias>", "Alias of the worktree to remove").option("-f, --force", "Force removal even with uncommitted changes").action(async (alias, options) => {
|
|
3087
|
+
const gitRoot = await getGitRoot();
|
|
3088
|
+
if (!gitRoot) {
|
|
3089
|
+
console.error(source_default.red("Error: Not in a git repository"));
|
|
3090
|
+
process.exit(1);
|
|
3091
|
+
}
|
|
3092
|
+
const worktree = await findWorktreeByAlias(alias, gitRoot);
|
|
3093
|
+
if (!worktree) {
|
|
3094
|
+
console.error(source_default.red(`Error: No worktree found with alias "${alias}"`));
|
|
3095
|
+
process.exit(1);
|
|
3096
|
+
}
|
|
3097
|
+
if (worktree.isMain) {
|
|
3098
|
+
console.error(source_default.red("Error: Cannot delete the main worktree"));
|
|
3099
|
+
process.exit(1);
|
|
3100
|
+
}
|
|
3101
|
+
console.log(source_default.dim(`Removing worktree at ${worktree.path}...`));
|
|
3102
|
+
try {
|
|
3103
|
+
await removeWorktree(worktree.path, options.force, gitRoot);
|
|
3104
|
+
console.log(source_default.green(`Removed worktree "${alias}"`));
|
|
3105
|
+
} catch (error) {
|
|
3106
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
3107
|
+
if (message.includes("contains modified or untracked files")) {
|
|
3108
|
+
console.error(source_default.red("Error: Worktree has uncommitted changes"));
|
|
3109
|
+
console.error(source_default.dim("Use --force to remove anyway"));
|
|
3110
|
+
} else {
|
|
3111
|
+
console.error(source_default.red("Error removing worktree:"));
|
|
3112
|
+
console.error(message);
|
|
3113
|
+
}
|
|
3114
|
+
process.exit(1);
|
|
3115
|
+
}
|
|
3116
|
+
});
|
|
3117
|
+
|
|
3118
|
+
// src/commands/init.ts
|
|
3119
|
+
var initCommand = new Command2("init").description("Register current project for worktree management").option("-y, --yes", "Skip prompts and use defaults").action(async (options) => {
|
|
3120
|
+
const gitRoot = await getGitRoot();
|
|
3121
|
+
if (!gitRoot) {
|
|
3122
|
+
console.error(source_default.red("Error: Not in a git repository"));
|
|
3123
|
+
process.exit(1);
|
|
3124
|
+
}
|
|
3125
|
+
const projectName = getProjectName(gitRoot);
|
|
3126
|
+
const alreadyRegistered = await isProjectRegistered(projectName);
|
|
3127
|
+
const existingLocal = await loadLocalConfig(gitRoot);
|
|
3128
|
+
if (alreadyRegistered && existingLocal) {
|
|
3129
|
+
console.log(source_default.yellow(`Project "${projectName}" is already initialized`));
|
|
3130
|
+
const reconfigure = options.yes ? false : await confirm("Reconfigure?", false);
|
|
3131
|
+
if (!reconfigure) {
|
|
3132
|
+
return;
|
|
3133
|
+
}
|
|
3134
|
+
}
|
|
3135
|
+
console.log(source_default.bold(`
|
|
3136
|
+
Initializing wts for ${source_default.cyan(projectName)}
|
|
3137
|
+
`));
|
|
3138
|
+
if (!alreadyRegistered) {
|
|
3139
|
+
await registerProject(projectName, gitRoot);
|
|
3140
|
+
console.log(source_default.dim(`Registered in ~/.wts.json`));
|
|
3141
|
+
}
|
|
3142
|
+
if (options.yes) {
|
|
3143
|
+
await saveLocalConfig(gitRoot, {});
|
|
3144
|
+
console.log(source_default.dim(`Created .wts-config.json with defaults`));
|
|
3145
|
+
} else {
|
|
3146
|
+
const localConfig = await promptForConfig(existingLocal);
|
|
3147
|
+
await saveLocalConfig(gitRoot, localConfig);
|
|
3148
|
+
console.log(source_default.dim(`Created .wts-config.json`));
|
|
3149
|
+
}
|
|
3150
|
+
console.log(source_default.green(`
|
|
3151
|
+
✓ Project "${projectName}" initialized`));
|
|
3152
|
+
console.log(source_default.dim(`
|
|
3153
|
+
Next steps:`));
|
|
3154
|
+
console.log(source_default.dim(` wts create <alias> - Create a new worktree`));
|
|
3155
|
+
console.log(source_default.dim(` wts list - List worktrees`));
|
|
3156
|
+
});
|
|
3157
|
+
async function promptForConfig(existing) {
|
|
3158
|
+
const config = {};
|
|
3159
|
+
const worktreeDir = await prompt("Worktree directory", existing?.worktreeDir ?? ".worktrees");
|
|
3160
|
+
if (worktreeDir !== ".worktrees") {
|
|
3161
|
+
config.worktreeDir = worktreeDir;
|
|
3162
|
+
}
|
|
3163
|
+
const autoTmux = await confirm("Auto-open tmux window on create?", existing?.autoTmux ?? false);
|
|
3164
|
+
if (autoTmux) {
|
|
3165
|
+
config.autoTmux = true;
|
|
3166
|
+
const tmuxTemplate = await prompt("Tmux window name template", existing?.tmuxWindowTemplate ?? "{project}-{alias}");
|
|
3167
|
+
if (tmuxTemplate !== "{project}-{alias}") {
|
|
3168
|
+
config.tmuxWindowTemplate = tmuxTemplate;
|
|
3169
|
+
}
|
|
3170
|
+
}
|
|
3171
|
+
const autoClaude = await confirm("Auto-launch Claude Code on create?", existing?.autoClaude ?? false);
|
|
3172
|
+
if (autoClaude) {
|
|
3173
|
+
config.autoClaude = true;
|
|
3174
|
+
}
|
|
3175
|
+
const hasSetup = await confirm("Configure a setup script?", !!existing?.setupScript);
|
|
3176
|
+
if (hasSetup) {
|
|
3177
|
+
const setupScript = await prompt("Setup script path (relative to repo root)", existing?.setupScript ?? ".wts-setup.sh");
|
|
3178
|
+
config.setupScript = setupScript;
|
|
3179
|
+
}
|
|
3180
|
+
return config;
|
|
3181
|
+
}
|
|
3182
|
+
|
|
3183
|
+
// src/commands/list.ts
|
|
3184
|
+
var listCommand = new Command2("list").alias("ls").description("List worktrees").option("-a, --all", "List worktrees across all tracked projects").option("--json", "Output as JSON").action(async (options) => {
|
|
3185
|
+
if (options.all) {
|
|
3186
|
+
await listAllProjects(options.json);
|
|
3187
|
+
} else {
|
|
3188
|
+
await listCurrentProject(options.json);
|
|
3189
|
+
}
|
|
3190
|
+
});
|
|
3191
|
+
async function listCurrentProject(jsonOutput) {
|
|
3192
|
+
const gitRoot = await getGitRoot();
|
|
3193
|
+
if (!gitRoot) {
|
|
3194
|
+
console.error(source_default.red("Error: Not in a git repository"));
|
|
3195
|
+
process.exit(1);
|
|
3196
|
+
}
|
|
3197
|
+
const config = await resolveConfig(gitRoot);
|
|
3198
|
+
const worktrees = await listWorktrees(gitRoot, config.projectName);
|
|
3199
|
+
if (jsonOutput) {
|
|
3200
|
+
console.log(JSON.stringify(worktrees, null, 2));
|
|
3201
|
+
return;
|
|
3202
|
+
}
|
|
3203
|
+
printWorktreeTable(worktrees, config.projectName);
|
|
3204
|
+
}
|
|
3205
|
+
async function listAllProjects(jsonOutput) {
|
|
3206
|
+
const projects = await getTrackedProjects();
|
|
3207
|
+
const projectNames = Object.keys(projects);
|
|
3208
|
+
if (projectNames.length === 0) {
|
|
3209
|
+
console.log(source_default.yellow("No projects registered. Run 'wts init' in a git repository."));
|
|
3210
|
+
return;
|
|
3211
|
+
}
|
|
3212
|
+
const allWorktrees = [];
|
|
3213
|
+
for (const projectName of projectNames) {
|
|
3214
|
+
const project = projects[projectName];
|
|
3215
|
+
if (!project)
|
|
3216
|
+
continue;
|
|
3217
|
+
try {
|
|
3218
|
+
const worktrees = await listWorktrees(project.path, projectName);
|
|
3219
|
+
allWorktrees.push(...worktrees);
|
|
3220
|
+
} catch {
|
|
3221
|
+
console.error(source_default.yellow(`Warning: Could not list worktrees for ${projectName}`));
|
|
3222
|
+
}
|
|
3223
|
+
}
|
|
3224
|
+
if (jsonOutput) {
|
|
3225
|
+
console.log(JSON.stringify(allWorktrees, null, 2));
|
|
3226
|
+
return;
|
|
3227
|
+
}
|
|
3228
|
+
for (const projectName of projectNames) {
|
|
3229
|
+
const projectWorktrees = allWorktrees.filter((wt) => wt.projectName === projectName);
|
|
3230
|
+
if (projectWorktrees.length > 0) {
|
|
3231
|
+
console.log();
|
|
3232
|
+
printWorktreeTable(projectWorktrees, projectName);
|
|
3233
|
+
}
|
|
3234
|
+
}
|
|
3235
|
+
}
|
|
3236
|
+
function printWorktreeTable(worktrees, projectName) {
|
|
3237
|
+
console.log(source_default.bold.blue(`${projectName}`));
|
|
3238
|
+
console.log(source_default.dim("─".repeat(60)));
|
|
3239
|
+
if (worktrees.length === 0) {
|
|
3240
|
+
console.log(source_default.dim(" No worktrees"));
|
|
3241
|
+
return;
|
|
3242
|
+
}
|
|
3243
|
+
const aliasWidth = Math.max(8, ...worktrees.map((wt) => (wt.alias ?? (wt.isMain ? "(main)" : "")).length));
|
|
3244
|
+
const branchWidth = Math.max(8, ...worktrees.map((wt) => wt.branch.length));
|
|
3245
|
+
console.log(source_default.dim(` ${"Alias".padEnd(aliasWidth)} ${"Branch".padEnd(branchWidth)} Path`));
|
|
3246
|
+
for (const wt of worktrees) {
|
|
3247
|
+
const aliasStr = wt.isMain ? "(main)" : wt.alias ?? "-";
|
|
3248
|
+
const path = formatPath(wt.path);
|
|
3249
|
+
console.log(` ${aliasStr.padEnd(aliasWidth)} ${wt.branch.padEnd(branchWidth)} ${source_default.dim(path)}`);
|
|
3250
|
+
}
|
|
3251
|
+
}
|
|
3252
|
+
|
|
3253
|
+
// src/commands/pr.ts
|
|
3254
|
+
async function isGhAvailable() {
|
|
3255
|
+
try {
|
|
3256
|
+
await Bun.$`which gh`.quiet();
|
|
3257
|
+
return true;
|
|
3258
|
+
} catch {
|
|
3259
|
+
return false;
|
|
3260
|
+
}
|
|
3261
|
+
}
|
|
3262
|
+
async function isGhAuthenticated() {
|
|
3263
|
+
try {
|
|
3264
|
+
await Bun.$`gh auth status`.quiet();
|
|
3265
|
+
return true;
|
|
3266
|
+
} catch {
|
|
3267
|
+
return false;
|
|
3268
|
+
}
|
|
3269
|
+
}
|
|
3270
|
+
async function detectCurrentWorktreeAlias(gitRoot) {
|
|
3271
|
+
const cwd = process.cwd();
|
|
3272
|
+
const worktrees = await listWorktrees(gitRoot);
|
|
3273
|
+
for (const wt of worktrees) {
|
|
3274
|
+
if (cwd === wt.path || cwd.startsWith(`${wt.path}/`)) {
|
|
3275
|
+
return wt.alias;
|
|
3276
|
+
}
|
|
3277
|
+
}
|
|
3278
|
+
const dirName = cwd.split("/").pop() ?? "";
|
|
3279
|
+
const alias = parseWorktreeAlias(dirName);
|
|
3280
|
+
if (alias) {
|
|
3281
|
+
return alias;
|
|
3282
|
+
}
|
|
3283
|
+
return;
|
|
3284
|
+
}
|
|
3285
|
+
var prCommand = new Command2("pr").description("Create a pull request from a worktree branch").argument("[alias]", "Worktree alias (auto-detected if in worktree)").option("--draft", "Create as draft PR").option("--web", "Open PR in browser after creation").option("-t, --title <title>", "PR title").option("-b, --body <body>", "PR body/description").action(async (alias, options) => {
|
|
3286
|
+
const hasGh = await isGhAvailable();
|
|
3287
|
+
if (!hasGh) {
|
|
3288
|
+
console.error(source_default.red("Error: GitHub CLI (gh) is required but not installed"));
|
|
3289
|
+
console.error(source_default.dim("Install it from: https://cli.github.com/"));
|
|
3290
|
+
process.exit(1);
|
|
3291
|
+
}
|
|
3292
|
+
const isAuthed = await isGhAuthenticated();
|
|
3293
|
+
if (!isAuthed) {
|
|
3294
|
+
console.error(source_default.red("Error: GitHub CLI is not authenticated"));
|
|
3295
|
+
console.error(source_default.dim("Run: gh auth login"));
|
|
3296
|
+
process.exit(1);
|
|
3297
|
+
}
|
|
3298
|
+
const gitRoot = await getGitRoot();
|
|
3299
|
+
if (!gitRoot) {
|
|
3300
|
+
console.error(source_default.red("Error: Not in a git repository"));
|
|
3301
|
+
process.exit(1);
|
|
3302
|
+
}
|
|
3303
|
+
let targetAlias = alias;
|
|
3304
|
+
let targetPath;
|
|
3305
|
+
let targetBranch;
|
|
3306
|
+
if (!targetAlias) {
|
|
3307
|
+
targetAlias = await detectCurrentWorktreeAlias(gitRoot);
|
|
3308
|
+
}
|
|
3309
|
+
if (targetAlias) {
|
|
3310
|
+
const worktree = await findWorktreeByAlias(targetAlias, gitRoot);
|
|
3311
|
+
if (!worktree) {
|
|
3312
|
+
console.error(source_default.red(`Error: Worktree "${targetAlias}" not found`));
|
|
3313
|
+
process.exit(1);
|
|
3314
|
+
}
|
|
3315
|
+
targetPath = worktree.path;
|
|
3316
|
+
targetBranch = worktree.branch;
|
|
3317
|
+
} else {
|
|
3318
|
+
targetPath = process.cwd();
|
|
3319
|
+
targetBranch = await getCurrentBranch(targetPath);
|
|
3320
|
+
}
|
|
3321
|
+
if (targetBranch === "main" || targetBranch === "master") {
|
|
3322
|
+
console.error(source_default.red("Error: Cannot create PR from main/master branch"));
|
|
3323
|
+
process.exit(1);
|
|
3324
|
+
}
|
|
3325
|
+
console.log(source_default.dim(`Creating PR for branch: ${targetBranch}`));
|
|
3326
|
+
const args = ["pr", "create"];
|
|
3327
|
+
if (options.draft) {
|
|
3328
|
+
args.push("--draft");
|
|
3329
|
+
}
|
|
3330
|
+
if (options.web) {
|
|
3331
|
+
args.push("--web");
|
|
3332
|
+
}
|
|
3333
|
+
if (options.title) {
|
|
3334
|
+
args.push("--title", options.title);
|
|
3335
|
+
}
|
|
3336
|
+
if (options.body) {
|
|
3337
|
+
args.push("--body", options.body);
|
|
3338
|
+
}
|
|
3339
|
+
if (!options.title && !options.web) {
|
|
3340
|
+
args.push("--fill");
|
|
3341
|
+
}
|
|
3342
|
+
try {
|
|
3343
|
+
console.log(source_default.dim("Pushing branch to remote..."));
|
|
3344
|
+
await Bun.$`git push -u origin ${targetBranch}`.cwd(targetPath);
|
|
3345
|
+
console.log(source_default.dim("Creating pull request..."));
|
|
3346
|
+
const result = await Bun.$`gh ${args}`.cwd(targetPath);
|
|
3347
|
+
const output = result.stdout.toString().trim();
|
|
3348
|
+
if (output) {
|
|
3349
|
+
console.log(source_default.green("Pull request created:"));
|
|
3350
|
+
console.log(output);
|
|
3351
|
+
}
|
|
3352
|
+
} catch (error) {
|
|
3353
|
+
console.error(source_default.red("Error creating pull request:"));
|
|
3354
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
3355
|
+
process.exit(1);
|
|
3356
|
+
}
|
|
3357
|
+
});
|
|
3358
|
+
|
|
3359
|
+
// src/commands/switch.ts
|
|
3360
|
+
var switchCommand = new Command2("switch").description("Switch to a worktree (interactive or by alias)").argument("[alias]", "Worktree alias to switch to").option("--tmux", "Open in a new tmux window").option("--claude", "Launch Claude Code in the tmux window").action(async (alias, options) => {
|
|
3361
|
+
const gitRoot = await getGitRoot();
|
|
3362
|
+
if (!gitRoot) {
|
|
3363
|
+
console.error(source_default.red("Error: Not in a git repository"));
|
|
3364
|
+
process.exit(1);
|
|
3365
|
+
}
|
|
3366
|
+
const config = await resolveConfig(gitRoot);
|
|
3367
|
+
const worktrees = await listWorktrees(gitRoot, config.projectName);
|
|
3368
|
+
const nonMainWorktrees = worktrees.filter((wt) => !wt.isMain);
|
|
3369
|
+
if (nonMainWorktrees.length === 0) {
|
|
3370
|
+
console.error(source_default.yellow("No worktrees found (excluding main)"));
|
|
3371
|
+
console.error(source_default.dim("Create one with: wts create <alias>"));
|
|
3372
|
+
process.exit(1);
|
|
3373
|
+
}
|
|
3374
|
+
let selectedWorktree;
|
|
3375
|
+
if (alias) {
|
|
3376
|
+
selectedWorktree = await findWorktreeByAlias(alias, gitRoot);
|
|
3377
|
+
if (!selectedWorktree) {
|
|
3378
|
+
console.error(source_default.red(`Error: Worktree "${alias}" not found`));
|
|
3379
|
+
process.exit(1);
|
|
3380
|
+
}
|
|
3381
|
+
} else {
|
|
3382
|
+
const hasFzf = await isFzfAvailable();
|
|
3383
|
+
if (hasFzf) {
|
|
3384
|
+
selectedWorktree = await selectWorktree(worktrees, {
|
|
3385
|
+
prompt: "Switch to > ",
|
|
3386
|
+
excludeMain: true
|
|
3387
|
+
});
|
|
3388
|
+
} else {
|
|
3389
|
+
console.log(source_default.dim("(fzf not found, using basic selection)"));
|
|
3390
|
+
const worktreeOptions = nonMainWorktrees.map((wt) => ({
|
|
3391
|
+
value: wt.alias ?? wt.branch,
|
|
3392
|
+
label: `${wt.alias ?? wt.branch} (${wt.branch}) - ${wt.path}`
|
|
3393
|
+
}));
|
|
3394
|
+
const selected = await select("Select worktree:", worktreeOptions, worktreeOptions[0]?.value);
|
|
3395
|
+
selectedWorktree = nonMainWorktrees.find((wt) => (wt.alias ?? wt.branch) === selected);
|
|
3396
|
+
}
|
|
3397
|
+
if (!selectedWorktree) {
|
|
3398
|
+
process.exit(0);
|
|
3399
|
+
}
|
|
3400
|
+
}
|
|
3401
|
+
const useTmux = options.tmux ?? config.autoTmux;
|
|
3402
|
+
const launchClaude = options.claude ?? config.autoClaude;
|
|
3403
|
+
if (useTmux && isInsideTmux()) {
|
|
3404
|
+
const windowName = resolveWindowName(config.tmuxWindowTemplate, config.projectName, selectedWorktree.alias ?? selectedWorktree.branch);
|
|
3405
|
+
console.log(source_default.dim(`Opening tmux window "${windowName}"...`));
|
|
3406
|
+
const success = await createWorktreeWindow({
|
|
3407
|
+
windowName,
|
|
3408
|
+
worktreePath: selectedWorktree.path,
|
|
3409
|
+
launchClaude
|
|
3410
|
+
});
|
|
3411
|
+
if (success) {
|
|
3412
|
+
console.log(source_default.green(`Switched to "${windowName}"`));
|
|
3413
|
+
} else {
|
|
3414
|
+
console.error(source_default.red("Failed to create/switch tmux window"));
|
|
3415
|
+
process.exit(1);
|
|
3416
|
+
}
|
|
3417
|
+
} else {
|
|
3418
|
+
console.log(selectedWorktree.path);
|
|
3419
|
+
if (useTmux && !isInsideTmux()) {
|
|
3420
|
+
console.error(source_default.dim(`
|
|
3421
|
+
(Note: --tmux specified but not inside tmux session)`));
|
|
3422
|
+
}
|
|
3423
|
+
}
|
|
3424
|
+
});
|
|
3425
|
+
|
|
3426
|
+
// src/index.ts
|
|
3427
|
+
var program3 = new Command;
|
|
3428
|
+
program3.name("wts").description("Git worktree manager with tmux integration").version("0.1.0");
|
|
3429
|
+
program3.addCommand(initCommand);
|
|
3430
|
+
program3.addCommand(listCommand);
|
|
3431
|
+
program3.addCommand(createCommand3);
|
|
3432
|
+
program3.addCommand(deleteCommand);
|
|
3433
|
+
program3.addCommand(cdCommand);
|
|
3434
|
+
program3.addCommand(switchCommand);
|
|
3435
|
+
program3.addCommand(prCommand);
|
|
3436
|
+
program3.addCommand(cleanupCommand);
|
|
3437
|
+
program3.parse();
|