trellis 1.0.7 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +533 -82
- package/bin/trellis.mjs +2 -0
- package/dist/cli/index.js +4718 -0
- package/dist/core/index.js +12 -0
- package/dist/decisions/index.js +19 -0
- package/dist/embeddings/index.js +43 -0
- package/dist/index-1j1anhmr.js +4038 -0
- package/dist/index-3s0eak0p.js +1556 -0
- package/dist/index-8pce39mh.js +272 -0
- package/dist/index-a76rekgs.js +67 -0
- package/dist/index-cy9k1g6v.js +684 -0
- package/dist/index-fd4e26s4.js +69 -0
- package/dist/{store/eav-store.js → index-gkvhzm9f.js} +4 -6
- package/dist/index-gnw8d7d6.js +51 -0
- package/dist/index-vkpkfwhq.js +817 -0
- package/dist/index.js +118 -2876
- package/dist/links/index.js +55 -0
- package/dist/transformers-m9je15kg.js +32491 -0
- package/dist/vcs/index.js +110 -0
- package/logo.png +0 -0
- package/logo.svg +9 -0
- package/package.json +79 -76
- package/src/cli/index.ts +2340 -0
- package/src/core/index.ts +35 -0
- package/src/core/kernel/middleware.ts +44 -0
- package/src/core/persist/backend.ts +64 -0
- package/src/core/store/eav-store.ts +467 -0
- package/src/decisions/auto-capture.ts +136 -0
- package/src/decisions/hooks.ts +163 -0
- package/src/decisions/index.ts +261 -0
- package/src/decisions/types.ts +103 -0
- package/src/embeddings/chunker.ts +327 -0
- package/src/embeddings/index.ts +41 -0
- package/src/embeddings/model.ts +95 -0
- package/src/embeddings/search.ts +305 -0
- package/src/embeddings/store.ts +313 -0
- package/src/embeddings/types.ts +85 -0
- package/src/engine.ts +1083 -0
- package/src/garden/cluster.ts +330 -0
- package/src/garden/garden.ts +306 -0
- package/src/garden/index.ts +29 -0
- package/src/git/git-exporter.ts +286 -0
- package/src/git/git-importer.ts +329 -0
- package/src/git/git-reader.ts +189 -0
- package/src/git/index.ts +22 -0
- package/src/identity/governance.ts +211 -0
- package/src/identity/identity.ts +224 -0
- package/src/identity/index.ts +30 -0
- package/src/identity/signing-middleware.ts +97 -0
- package/src/index.ts +20 -0
- package/src/links/index.ts +49 -0
- package/src/links/lifecycle.ts +400 -0
- package/src/links/parser.ts +484 -0
- package/src/links/ref-index.ts +186 -0
- package/src/links/resolver.ts +314 -0
- package/src/links/types.ts +108 -0
- package/src/mcp/index.ts +22 -0
- package/src/mcp/server.ts +1278 -0
- package/src/semantic/csharp-parser.ts +493 -0
- package/src/semantic/go-parser.ts +585 -0
- package/src/semantic/index.ts +34 -0
- package/src/semantic/java-parser.ts +456 -0
- package/src/semantic/python-parser.ts +659 -0
- package/src/semantic/ruby-parser.ts +446 -0
- package/src/semantic/rust-parser.ts +784 -0
- package/src/semantic/semantic-merge.ts +210 -0
- package/src/semantic/ts-parser.ts +681 -0
- package/src/semantic/types.ts +175 -0
- package/src/sync/index.ts +32 -0
- package/src/sync/memory-transport.ts +66 -0
- package/src/sync/reconciler.ts +237 -0
- package/src/sync/sync-engine.ts +258 -0
- package/src/sync/types.ts +104 -0
- package/src/vcs/blob-store.ts +124 -0
- package/src/vcs/branch.ts +150 -0
- package/src/vcs/checkpoint.ts +64 -0
- package/src/vcs/decompose.ts +469 -0
- package/src/vcs/diff.ts +409 -0
- package/src/vcs/engine-context.ts +26 -0
- package/src/vcs/index.ts +23 -0
- package/src/vcs/issue.ts +800 -0
- package/src/vcs/merge.ts +425 -0
- package/src/vcs/milestone.ts +124 -0
- package/src/vcs/ops.ts +59 -0
- package/src/vcs/types.ts +213 -0
- package/src/vcs/vcs-middleware.ts +81 -0
- package/src/watcher/fs-watcher.ts +217 -0
- package/src/watcher/index.ts +9 -0
- package/src/watcher/ingestion.ts +116 -0
- package/dist/ai/index.js +0 -688
- package/dist/cli/server.js +0 -3321
- package/dist/cli/tql.js +0 -5282
- package/dist/client/tql-client.js +0 -108
- package/dist/graph/index.js +0 -2248
- package/dist/kernel/logic-middleware.js +0 -179
- package/dist/kernel/middleware.js +0 -0
- package/dist/kernel/operations.js +0 -32
- package/dist/kernel/schema-middleware.js +0 -34
- package/dist/kernel/security-middleware.js +0 -53
- package/dist/kernel/trellis-kernel.js +0 -2239
- package/dist/kernel/workspace.js +0 -91
- package/dist/persist/backend.js +0 -0
- package/dist/persist/sqlite-backend.js +0 -123
- package/dist/query/index.js +0 -1643
- package/dist/server/index.js +0 -3309
- package/dist/workflows/index.js +0 -3160
|
@@ -0,0 +1,4718 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// @bun
|
|
3
|
+
import {
|
|
4
|
+
TrellisVcsEngine
|
|
5
|
+
} from "../index-1j1anhmr.js";
|
|
6
|
+
import"../index-gkvhzm9f.js";
|
|
7
|
+
import"../index-3s0eak0p.js";
|
|
8
|
+
import {
|
|
9
|
+
exports_embeddings,
|
|
10
|
+
init_embeddings
|
|
11
|
+
} from "../index-cy9k1g6v.js";
|
|
12
|
+
import {
|
|
13
|
+
exports_links,
|
|
14
|
+
init_links
|
|
15
|
+
} from "../index-vkpkfwhq.js";
|
|
16
|
+
import"../index-8pce39mh.js";
|
|
17
|
+
import {
|
|
18
|
+
createVcsOp
|
|
19
|
+
} from "../index-fd4e26s4.js";
|
|
20
|
+
import {
|
|
21
|
+
__commonJS,
|
|
22
|
+
__esm,
|
|
23
|
+
__export,
|
|
24
|
+
__require,
|
|
25
|
+
__toCommonJS,
|
|
26
|
+
__toESM
|
|
27
|
+
} from "../index-a76rekgs.js";
|
|
28
|
+
|
|
29
|
+
// node_modules/commander/lib/error.js
|
|
30
|
+
var require_error = __commonJS((exports) => {
|
|
31
|
+
class CommanderError extends Error {
|
|
32
|
+
constructor(exitCode, code, message) {
|
|
33
|
+
super(message);
|
|
34
|
+
Error.captureStackTrace(this, this.constructor);
|
|
35
|
+
this.name = this.constructor.name;
|
|
36
|
+
this.code = code;
|
|
37
|
+
this.exitCode = exitCode;
|
|
38
|
+
this.nestedError = undefined;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
class InvalidArgumentError extends CommanderError {
|
|
43
|
+
constructor(message) {
|
|
44
|
+
super(1, "commander.invalidArgument", message);
|
|
45
|
+
Error.captureStackTrace(this, this.constructor);
|
|
46
|
+
this.name = this.constructor.name;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.CommanderError = CommanderError;
|
|
50
|
+
exports.InvalidArgumentError = InvalidArgumentError;
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// node_modules/commander/lib/argument.js
|
|
54
|
+
var require_argument = __commonJS((exports) => {
|
|
55
|
+
var { InvalidArgumentError } = require_error();
|
|
56
|
+
|
|
57
|
+
class Argument {
|
|
58
|
+
constructor(name, description) {
|
|
59
|
+
this.description = description || "";
|
|
60
|
+
this.variadic = false;
|
|
61
|
+
this.parseArg = undefined;
|
|
62
|
+
this.defaultValue = undefined;
|
|
63
|
+
this.defaultValueDescription = undefined;
|
|
64
|
+
this.argChoices = undefined;
|
|
65
|
+
switch (name[0]) {
|
|
66
|
+
case "<":
|
|
67
|
+
this.required = true;
|
|
68
|
+
this._name = name.slice(1, -1);
|
|
69
|
+
break;
|
|
70
|
+
case "[":
|
|
71
|
+
this.required = false;
|
|
72
|
+
this._name = name.slice(1, -1);
|
|
73
|
+
break;
|
|
74
|
+
default:
|
|
75
|
+
this.required = true;
|
|
76
|
+
this._name = name;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
if (this._name.endsWith("...")) {
|
|
80
|
+
this.variadic = true;
|
|
81
|
+
this._name = this._name.slice(0, -3);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
name() {
|
|
85
|
+
return this._name;
|
|
86
|
+
}
|
|
87
|
+
_collectValue(value, previous) {
|
|
88
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
89
|
+
return [value];
|
|
90
|
+
}
|
|
91
|
+
previous.push(value);
|
|
92
|
+
return previous;
|
|
93
|
+
}
|
|
94
|
+
default(value, description) {
|
|
95
|
+
this.defaultValue = value;
|
|
96
|
+
this.defaultValueDescription = description;
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
argParser(fn) {
|
|
100
|
+
this.parseArg = fn;
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
choices(values) {
|
|
104
|
+
this.argChoices = values.slice();
|
|
105
|
+
this.parseArg = (arg, previous) => {
|
|
106
|
+
if (!this.argChoices.includes(arg)) {
|
|
107
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
108
|
+
}
|
|
109
|
+
if (this.variadic) {
|
|
110
|
+
return this._collectValue(arg, previous);
|
|
111
|
+
}
|
|
112
|
+
return arg;
|
|
113
|
+
};
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
argRequired() {
|
|
117
|
+
this.required = true;
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
argOptional() {
|
|
121
|
+
this.required = false;
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
function humanReadableArgName(arg) {
|
|
126
|
+
const nameOutput = arg.name() + (arg.variadic === true ? "..." : "");
|
|
127
|
+
return arg.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
|
|
128
|
+
}
|
|
129
|
+
exports.Argument = Argument;
|
|
130
|
+
exports.humanReadableArgName = humanReadableArgName;
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// node_modules/commander/lib/help.js
|
|
134
|
+
var require_help = __commonJS((exports) => {
|
|
135
|
+
var { humanReadableArgName } = require_argument();
|
|
136
|
+
|
|
137
|
+
class Help {
|
|
138
|
+
constructor() {
|
|
139
|
+
this.helpWidth = undefined;
|
|
140
|
+
this.minWidthToWrap = 40;
|
|
141
|
+
this.sortSubcommands = false;
|
|
142
|
+
this.sortOptions = false;
|
|
143
|
+
this.showGlobalOptions = false;
|
|
144
|
+
}
|
|
145
|
+
prepareContext(contextOptions) {
|
|
146
|
+
this.helpWidth = this.helpWidth ?? contextOptions.helpWidth ?? 80;
|
|
147
|
+
}
|
|
148
|
+
visibleCommands(cmd) {
|
|
149
|
+
const visibleCommands = cmd.commands.filter((cmd2) => !cmd2._hidden);
|
|
150
|
+
const helpCommand = cmd._getHelpCommand();
|
|
151
|
+
if (helpCommand && !helpCommand._hidden) {
|
|
152
|
+
visibleCommands.push(helpCommand);
|
|
153
|
+
}
|
|
154
|
+
if (this.sortSubcommands) {
|
|
155
|
+
visibleCommands.sort((a, b) => {
|
|
156
|
+
return a.name().localeCompare(b.name());
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
return visibleCommands;
|
|
160
|
+
}
|
|
161
|
+
compareOptions(a, b) {
|
|
162
|
+
const getSortKey = (option) => {
|
|
163
|
+
return option.short ? option.short.replace(/^-/, "") : option.long.replace(/^--/, "");
|
|
164
|
+
};
|
|
165
|
+
return getSortKey(a).localeCompare(getSortKey(b));
|
|
166
|
+
}
|
|
167
|
+
visibleOptions(cmd) {
|
|
168
|
+
const visibleOptions = cmd.options.filter((option) => !option.hidden);
|
|
169
|
+
const helpOption = cmd._getHelpOption();
|
|
170
|
+
if (helpOption && !helpOption.hidden) {
|
|
171
|
+
const removeShort = helpOption.short && cmd._findOption(helpOption.short);
|
|
172
|
+
const removeLong = helpOption.long && cmd._findOption(helpOption.long);
|
|
173
|
+
if (!removeShort && !removeLong) {
|
|
174
|
+
visibleOptions.push(helpOption);
|
|
175
|
+
} else if (helpOption.long && !removeLong) {
|
|
176
|
+
visibleOptions.push(cmd.createOption(helpOption.long, helpOption.description));
|
|
177
|
+
} else if (helpOption.short && !removeShort) {
|
|
178
|
+
visibleOptions.push(cmd.createOption(helpOption.short, helpOption.description));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (this.sortOptions) {
|
|
182
|
+
visibleOptions.sort(this.compareOptions);
|
|
183
|
+
}
|
|
184
|
+
return visibleOptions;
|
|
185
|
+
}
|
|
186
|
+
visibleGlobalOptions(cmd) {
|
|
187
|
+
if (!this.showGlobalOptions)
|
|
188
|
+
return [];
|
|
189
|
+
const globalOptions = [];
|
|
190
|
+
for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
191
|
+
const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
|
|
192
|
+
globalOptions.push(...visibleOptions);
|
|
193
|
+
}
|
|
194
|
+
if (this.sortOptions) {
|
|
195
|
+
globalOptions.sort(this.compareOptions);
|
|
196
|
+
}
|
|
197
|
+
return globalOptions;
|
|
198
|
+
}
|
|
199
|
+
visibleArguments(cmd) {
|
|
200
|
+
if (cmd._argsDescription) {
|
|
201
|
+
cmd.registeredArguments.forEach((argument) => {
|
|
202
|
+
argument.description = argument.description || cmd._argsDescription[argument.name()] || "";
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
if (cmd.registeredArguments.find((argument) => argument.description)) {
|
|
206
|
+
return cmd.registeredArguments;
|
|
207
|
+
}
|
|
208
|
+
return [];
|
|
209
|
+
}
|
|
210
|
+
subcommandTerm(cmd) {
|
|
211
|
+
const args = cmd.registeredArguments.map((arg) => humanReadableArgName(arg)).join(" ");
|
|
212
|
+
return cmd._name + (cmd._aliases[0] ? "|" + cmd._aliases[0] : "") + (cmd.options.length ? " [options]" : "") + (args ? " " + args : "");
|
|
213
|
+
}
|
|
214
|
+
optionTerm(option) {
|
|
215
|
+
return option.flags;
|
|
216
|
+
}
|
|
217
|
+
argumentTerm(argument) {
|
|
218
|
+
return argument.name();
|
|
219
|
+
}
|
|
220
|
+
longestSubcommandTermLength(cmd, helper) {
|
|
221
|
+
return helper.visibleCommands(cmd).reduce((max, command) => {
|
|
222
|
+
return Math.max(max, this.displayWidth(helper.styleSubcommandTerm(helper.subcommandTerm(command))));
|
|
223
|
+
}, 0);
|
|
224
|
+
}
|
|
225
|
+
longestOptionTermLength(cmd, helper) {
|
|
226
|
+
return helper.visibleOptions(cmd).reduce((max, option) => {
|
|
227
|
+
return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
|
|
228
|
+
}, 0);
|
|
229
|
+
}
|
|
230
|
+
longestGlobalOptionTermLength(cmd, helper) {
|
|
231
|
+
return helper.visibleGlobalOptions(cmd).reduce((max, option) => {
|
|
232
|
+
return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
|
|
233
|
+
}, 0);
|
|
234
|
+
}
|
|
235
|
+
longestArgumentTermLength(cmd, helper) {
|
|
236
|
+
return helper.visibleArguments(cmd).reduce((max, argument) => {
|
|
237
|
+
return Math.max(max, this.displayWidth(helper.styleArgumentTerm(helper.argumentTerm(argument))));
|
|
238
|
+
}, 0);
|
|
239
|
+
}
|
|
240
|
+
commandUsage(cmd) {
|
|
241
|
+
let cmdName = cmd._name;
|
|
242
|
+
if (cmd._aliases[0]) {
|
|
243
|
+
cmdName = cmdName + "|" + cmd._aliases[0];
|
|
244
|
+
}
|
|
245
|
+
let ancestorCmdNames = "";
|
|
246
|
+
for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
247
|
+
ancestorCmdNames = ancestorCmd.name() + " " + ancestorCmdNames;
|
|
248
|
+
}
|
|
249
|
+
return ancestorCmdNames + cmdName + " " + cmd.usage();
|
|
250
|
+
}
|
|
251
|
+
commandDescription(cmd) {
|
|
252
|
+
return cmd.description();
|
|
253
|
+
}
|
|
254
|
+
subcommandDescription(cmd) {
|
|
255
|
+
return cmd.summary() || cmd.description();
|
|
256
|
+
}
|
|
257
|
+
optionDescription(option) {
|
|
258
|
+
const extraInfo = [];
|
|
259
|
+
if (option.argChoices) {
|
|
260
|
+
extraInfo.push(`choices: ${option.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
261
|
+
}
|
|
262
|
+
if (option.defaultValue !== undefined) {
|
|
263
|
+
const showDefault = option.required || option.optional || option.isBoolean() && typeof option.defaultValue === "boolean";
|
|
264
|
+
if (showDefault) {
|
|
265
|
+
extraInfo.push(`default: ${option.defaultValueDescription || JSON.stringify(option.defaultValue)}`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
if (option.presetArg !== undefined && option.optional) {
|
|
269
|
+
extraInfo.push(`preset: ${JSON.stringify(option.presetArg)}`);
|
|
270
|
+
}
|
|
271
|
+
if (option.envVar !== undefined) {
|
|
272
|
+
extraInfo.push(`env: ${option.envVar}`);
|
|
273
|
+
}
|
|
274
|
+
if (extraInfo.length > 0) {
|
|
275
|
+
const extraDescription = `(${extraInfo.join(", ")})`;
|
|
276
|
+
if (option.description) {
|
|
277
|
+
return `${option.description} ${extraDescription}`;
|
|
278
|
+
}
|
|
279
|
+
return extraDescription;
|
|
280
|
+
}
|
|
281
|
+
return option.description;
|
|
282
|
+
}
|
|
283
|
+
argumentDescription(argument) {
|
|
284
|
+
const extraInfo = [];
|
|
285
|
+
if (argument.argChoices) {
|
|
286
|
+
extraInfo.push(`choices: ${argument.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
287
|
+
}
|
|
288
|
+
if (argument.defaultValue !== undefined) {
|
|
289
|
+
extraInfo.push(`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`);
|
|
290
|
+
}
|
|
291
|
+
if (extraInfo.length > 0) {
|
|
292
|
+
const extraDescription = `(${extraInfo.join(", ")})`;
|
|
293
|
+
if (argument.description) {
|
|
294
|
+
return `${argument.description} ${extraDescription}`;
|
|
295
|
+
}
|
|
296
|
+
return extraDescription;
|
|
297
|
+
}
|
|
298
|
+
return argument.description;
|
|
299
|
+
}
|
|
300
|
+
formatItemList(heading, items, helper) {
|
|
301
|
+
if (items.length === 0)
|
|
302
|
+
return [];
|
|
303
|
+
return [helper.styleTitle(heading), ...items, ""];
|
|
304
|
+
}
|
|
305
|
+
groupItems(unsortedItems, visibleItems, getGroup) {
|
|
306
|
+
const result = new Map;
|
|
307
|
+
unsortedItems.forEach((item) => {
|
|
308
|
+
const group = getGroup(item);
|
|
309
|
+
if (!result.has(group))
|
|
310
|
+
result.set(group, []);
|
|
311
|
+
});
|
|
312
|
+
visibleItems.forEach((item) => {
|
|
313
|
+
const group = getGroup(item);
|
|
314
|
+
if (!result.has(group)) {
|
|
315
|
+
result.set(group, []);
|
|
316
|
+
}
|
|
317
|
+
result.get(group).push(item);
|
|
318
|
+
});
|
|
319
|
+
return result;
|
|
320
|
+
}
|
|
321
|
+
formatHelp(cmd, helper) {
|
|
322
|
+
const termWidth = helper.padWidth(cmd, helper);
|
|
323
|
+
const helpWidth = helper.helpWidth ?? 80;
|
|
324
|
+
function callFormatItem(term, description) {
|
|
325
|
+
return helper.formatItem(term, termWidth, description, helper);
|
|
326
|
+
}
|
|
327
|
+
let output = [
|
|
328
|
+
`${helper.styleTitle("Usage:")} ${helper.styleUsage(helper.commandUsage(cmd))}`,
|
|
329
|
+
""
|
|
330
|
+
];
|
|
331
|
+
const commandDescription = helper.commandDescription(cmd);
|
|
332
|
+
if (commandDescription.length > 0) {
|
|
333
|
+
output = output.concat([
|
|
334
|
+
helper.boxWrap(helper.styleCommandDescription(commandDescription), helpWidth),
|
|
335
|
+
""
|
|
336
|
+
]);
|
|
337
|
+
}
|
|
338
|
+
const argumentList = helper.visibleArguments(cmd).map((argument) => {
|
|
339
|
+
return callFormatItem(helper.styleArgumentTerm(helper.argumentTerm(argument)), helper.styleArgumentDescription(helper.argumentDescription(argument)));
|
|
340
|
+
});
|
|
341
|
+
output = output.concat(this.formatItemList("Arguments:", argumentList, helper));
|
|
342
|
+
const optionGroups = this.groupItems(cmd.options, helper.visibleOptions(cmd), (option) => option.helpGroupHeading ?? "Options:");
|
|
343
|
+
optionGroups.forEach((options, group) => {
|
|
344
|
+
const optionList = options.map((option) => {
|
|
345
|
+
return callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option)));
|
|
346
|
+
});
|
|
347
|
+
output = output.concat(this.formatItemList(group, optionList, helper));
|
|
348
|
+
});
|
|
349
|
+
if (helper.showGlobalOptions) {
|
|
350
|
+
const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
|
|
351
|
+
return callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option)));
|
|
352
|
+
});
|
|
353
|
+
output = output.concat(this.formatItemList("Global Options:", globalOptionList, helper));
|
|
354
|
+
}
|
|
355
|
+
const commandGroups = this.groupItems(cmd.commands, helper.visibleCommands(cmd), (sub) => sub.helpGroup() || "Commands:");
|
|
356
|
+
commandGroups.forEach((commands, group) => {
|
|
357
|
+
const commandList = commands.map((sub) => {
|
|
358
|
+
return callFormatItem(helper.styleSubcommandTerm(helper.subcommandTerm(sub)), helper.styleSubcommandDescription(helper.subcommandDescription(sub)));
|
|
359
|
+
});
|
|
360
|
+
output = output.concat(this.formatItemList(group, commandList, helper));
|
|
361
|
+
});
|
|
362
|
+
return output.join(`
|
|
363
|
+
`);
|
|
364
|
+
}
|
|
365
|
+
displayWidth(str) {
|
|
366
|
+
return stripColor(str).length;
|
|
367
|
+
}
|
|
368
|
+
styleTitle(str) {
|
|
369
|
+
return str;
|
|
370
|
+
}
|
|
371
|
+
styleUsage(str) {
|
|
372
|
+
return str.split(" ").map((word) => {
|
|
373
|
+
if (word === "[options]")
|
|
374
|
+
return this.styleOptionText(word);
|
|
375
|
+
if (word === "[command]")
|
|
376
|
+
return this.styleSubcommandText(word);
|
|
377
|
+
if (word[0] === "[" || word[0] === "<")
|
|
378
|
+
return this.styleArgumentText(word);
|
|
379
|
+
return this.styleCommandText(word);
|
|
380
|
+
}).join(" ");
|
|
381
|
+
}
|
|
382
|
+
styleCommandDescription(str) {
|
|
383
|
+
return this.styleDescriptionText(str);
|
|
384
|
+
}
|
|
385
|
+
styleOptionDescription(str) {
|
|
386
|
+
return this.styleDescriptionText(str);
|
|
387
|
+
}
|
|
388
|
+
styleSubcommandDescription(str) {
|
|
389
|
+
return this.styleDescriptionText(str);
|
|
390
|
+
}
|
|
391
|
+
styleArgumentDescription(str) {
|
|
392
|
+
return this.styleDescriptionText(str);
|
|
393
|
+
}
|
|
394
|
+
styleDescriptionText(str) {
|
|
395
|
+
return str;
|
|
396
|
+
}
|
|
397
|
+
styleOptionTerm(str) {
|
|
398
|
+
return this.styleOptionText(str);
|
|
399
|
+
}
|
|
400
|
+
styleSubcommandTerm(str) {
|
|
401
|
+
return str.split(" ").map((word) => {
|
|
402
|
+
if (word === "[options]")
|
|
403
|
+
return this.styleOptionText(word);
|
|
404
|
+
if (word[0] === "[" || word[0] === "<")
|
|
405
|
+
return this.styleArgumentText(word);
|
|
406
|
+
return this.styleSubcommandText(word);
|
|
407
|
+
}).join(" ");
|
|
408
|
+
}
|
|
409
|
+
styleArgumentTerm(str) {
|
|
410
|
+
return this.styleArgumentText(str);
|
|
411
|
+
}
|
|
412
|
+
styleOptionText(str) {
|
|
413
|
+
return str;
|
|
414
|
+
}
|
|
415
|
+
styleArgumentText(str) {
|
|
416
|
+
return str;
|
|
417
|
+
}
|
|
418
|
+
styleSubcommandText(str) {
|
|
419
|
+
return str;
|
|
420
|
+
}
|
|
421
|
+
styleCommandText(str) {
|
|
422
|
+
return str;
|
|
423
|
+
}
|
|
424
|
+
padWidth(cmd, helper) {
|
|
425
|
+
return Math.max(helper.longestOptionTermLength(cmd, helper), helper.longestGlobalOptionTermLength(cmd, helper), helper.longestSubcommandTermLength(cmd, helper), helper.longestArgumentTermLength(cmd, helper));
|
|
426
|
+
}
|
|
427
|
+
preformatted(str) {
|
|
428
|
+
return /\n[^\S\r\n]/.test(str);
|
|
429
|
+
}
|
|
430
|
+
formatItem(term, termWidth, description, helper) {
|
|
431
|
+
const itemIndent = 2;
|
|
432
|
+
const itemIndentStr = " ".repeat(itemIndent);
|
|
433
|
+
if (!description)
|
|
434
|
+
return itemIndentStr + term;
|
|
435
|
+
const paddedTerm = term.padEnd(termWidth + term.length - helper.displayWidth(term));
|
|
436
|
+
const spacerWidth = 2;
|
|
437
|
+
const helpWidth = this.helpWidth ?? 80;
|
|
438
|
+
const remainingWidth = helpWidth - termWidth - spacerWidth - itemIndent;
|
|
439
|
+
let formattedDescription;
|
|
440
|
+
if (remainingWidth < this.minWidthToWrap || helper.preformatted(description)) {
|
|
441
|
+
formattedDescription = description;
|
|
442
|
+
} else {
|
|
443
|
+
const wrappedDescription = helper.boxWrap(description, remainingWidth);
|
|
444
|
+
formattedDescription = wrappedDescription.replace(/\n/g, `
|
|
445
|
+
` + " ".repeat(termWidth + spacerWidth));
|
|
446
|
+
}
|
|
447
|
+
return itemIndentStr + paddedTerm + " ".repeat(spacerWidth) + formattedDescription.replace(/\n/g, `
|
|
448
|
+
${itemIndentStr}`);
|
|
449
|
+
}
|
|
450
|
+
boxWrap(str, width) {
|
|
451
|
+
if (width < this.minWidthToWrap)
|
|
452
|
+
return str;
|
|
453
|
+
const rawLines = str.split(/\r\n|\n/);
|
|
454
|
+
const chunkPattern = /[\s]*[^\s]+/g;
|
|
455
|
+
const wrappedLines = [];
|
|
456
|
+
rawLines.forEach((line) => {
|
|
457
|
+
const chunks = line.match(chunkPattern);
|
|
458
|
+
if (chunks === null) {
|
|
459
|
+
wrappedLines.push("");
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
let sumChunks = [chunks.shift()];
|
|
463
|
+
let sumWidth = this.displayWidth(sumChunks[0]);
|
|
464
|
+
chunks.forEach((chunk) => {
|
|
465
|
+
const visibleWidth = this.displayWidth(chunk);
|
|
466
|
+
if (sumWidth + visibleWidth <= width) {
|
|
467
|
+
sumChunks.push(chunk);
|
|
468
|
+
sumWidth += visibleWidth;
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
wrappedLines.push(sumChunks.join(""));
|
|
472
|
+
const nextChunk = chunk.trimStart();
|
|
473
|
+
sumChunks = [nextChunk];
|
|
474
|
+
sumWidth = this.displayWidth(nextChunk);
|
|
475
|
+
});
|
|
476
|
+
wrappedLines.push(sumChunks.join(""));
|
|
477
|
+
});
|
|
478
|
+
return wrappedLines.join(`
|
|
479
|
+
`);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
function stripColor(str) {
|
|
483
|
+
const sgrPattern = /\x1b\[\d*(;\d*)*m/g;
|
|
484
|
+
return str.replace(sgrPattern, "");
|
|
485
|
+
}
|
|
486
|
+
exports.Help = Help;
|
|
487
|
+
exports.stripColor = stripColor;
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
// node_modules/commander/lib/option.js
|
|
491
|
+
var require_option = __commonJS((exports) => {
|
|
492
|
+
var { InvalidArgumentError } = require_error();
|
|
493
|
+
|
|
494
|
+
class Option {
|
|
495
|
+
constructor(flags, description) {
|
|
496
|
+
this.flags = flags;
|
|
497
|
+
this.description = description || "";
|
|
498
|
+
this.required = flags.includes("<");
|
|
499
|
+
this.optional = flags.includes("[");
|
|
500
|
+
this.variadic = /\w\.\.\.[>\]]$/.test(flags);
|
|
501
|
+
this.mandatory = false;
|
|
502
|
+
const optionFlags = splitOptionFlags(flags);
|
|
503
|
+
this.short = optionFlags.shortFlag;
|
|
504
|
+
this.long = optionFlags.longFlag;
|
|
505
|
+
this.negate = false;
|
|
506
|
+
if (this.long) {
|
|
507
|
+
this.negate = this.long.startsWith("--no-");
|
|
508
|
+
}
|
|
509
|
+
this.defaultValue = undefined;
|
|
510
|
+
this.defaultValueDescription = undefined;
|
|
511
|
+
this.presetArg = undefined;
|
|
512
|
+
this.envVar = undefined;
|
|
513
|
+
this.parseArg = undefined;
|
|
514
|
+
this.hidden = false;
|
|
515
|
+
this.argChoices = undefined;
|
|
516
|
+
this.conflictsWith = [];
|
|
517
|
+
this.implied = undefined;
|
|
518
|
+
this.helpGroupHeading = undefined;
|
|
519
|
+
}
|
|
520
|
+
default(value, description) {
|
|
521
|
+
this.defaultValue = value;
|
|
522
|
+
this.defaultValueDescription = description;
|
|
523
|
+
return this;
|
|
524
|
+
}
|
|
525
|
+
preset(arg) {
|
|
526
|
+
this.presetArg = arg;
|
|
527
|
+
return this;
|
|
528
|
+
}
|
|
529
|
+
conflicts(names) {
|
|
530
|
+
this.conflictsWith = this.conflictsWith.concat(names);
|
|
531
|
+
return this;
|
|
532
|
+
}
|
|
533
|
+
implies(impliedOptionValues) {
|
|
534
|
+
let newImplied = impliedOptionValues;
|
|
535
|
+
if (typeof impliedOptionValues === "string") {
|
|
536
|
+
newImplied = { [impliedOptionValues]: true };
|
|
537
|
+
}
|
|
538
|
+
this.implied = Object.assign(this.implied || {}, newImplied);
|
|
539
|
+
return this;
|
|
540
|
+
}
|
|
541
|
+
env(name) {
|
|
542
|
+
this.envVar = name;
|
|
543
|
+
return this;
|
|
544
|
+
}
|
|
545
|
+
argParser(fn) {
|
|
546
|
+
this.parseArg = fn;
|
|
547
|
+
return this;
|
|
548
|
+
}
|
|
549
|
+
makeOptionMandatory(mandatory = true) {
|
|
550
|
+
this.mandatory = !!mandatory;
|
|
551
|
+
return this;
|
|
552
|
+
}
|
|
553
|
+
hideHelp(hide = true) {
|
|
554
|
+
this.hidden = !!hide;
|
|
555
|
+
return this;
|
|
556
|
+
}
|
|
557
|
+
_collectValue(value, previous) {
|
|
558
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
559
|
+
return [value];
|
|
560
|
+
}
|
|
561
|
+
previous.push(value);
|
|
562
|
+
return previous;
|
|
563
|
+
}
|
|
564
|
+
choices(values) {
|
|
565
|
+
this.argChoices = values.slice();
|
|
566
|
+
this.parseArg = (arg, previous) => {
|
|
567
|
+
if (!this.argChoices.includes(arg)) {
|
|
568
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
569
|
+
}
|
|
570
|
+
if (this.variadic) {
|
|
571
|
+
return this._collectValue(arg, previous);
|
|
572
|
+
}
|
|
573
|
+
return arg;
|
|
574
|
+
};
|
|
575
|
+
return this;
|
|
576
|
+
}
|
|
577
|
+
name() {
|
|
578
|
+
if (this.long) {
|
|
579
|
+
return this.long.replace(/^--/, "");
|
|
580
|
+
}
|
|
581
|
+
return this.short.replace(/^-/, "");
|
|
582
|
+
}
|
|
583
|
+
attributeName() {
|
|
584
|
+
if (this.negate) {
|
|
585
|
+
return camelcase(this.name().replace(/^no-/, ""));
|
|
586
|
+
}
|
|
587
|
+
return camelcase(this.name());
|
|
588
|
+
}
|
|
589
|
+
helpGroup(heading) {
|
|
590
|
+
this.helpGroupHeading = heading;
|
|
591
|
+
return this;
|
|
592
|
+
}
|
|
593
|
+
is(arg) {
|
|
594
|
+
return this.short === arg || this.long === arg;
|
|
595
|
+
}
|
|
596
|
+
isBoolean() {
|
|
597
|
+
return !this.required && !this.optional && !this.negate;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
class DualOptions {
|
|
602
|
+
constructor(options) {
|
|
603
|
+
this.positiveOptions = new Map;
|
|
604
|
+
this.negativeOptions = new Map;
|
|
605
|
+
this.dualOptions = new Set;
|
|
606
|
+
options.forEach((option) => {
|
|
607
|
+
if (option.negate) {
|
|
608
|
+
this.negativeOptions.set(option.attributeName(), option);
|
|
609
|
+
} else {
|
|
610
|
+
this.positiveOptions.set(option.attributeName(), option);
|
|
611
|
+
}
|
|
612
|
+
});
|
|
613
|
+
this.negativeOptions.forEach((value, key) => {
|
|
614
|
+
if (this.positiveOptions.has(key)) {
|
|
615
|
+
this.dualOptions.add(key);
|
|
616
|
+
}
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
valueFromOption(value, option) {
|
|
620
|
+
const optionKey = option.attributeName();
|
|
621
|
+
if (!this.dualOptions.has(optionKey))
|
|
622
|
+
return true;
|
|
623
|
+
const preset = this.negativeOptions.get(optionKey).presetArg;
|
|
624
|
+
const negativeValue = preset !== undefined ? preset : false;
|
|
625
|
+
return option.negate === (negativeValue === value);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
function camelcase(str) {
|
|
629
|
+
return str.split("-").reduce((str2, word) => {
|
|
630
|
+
return str2 + word[0].toUpperCase() + word.slice(1);
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
function splitOptionFlags(flags) {
|
|
634
|
+
let shortFlag;
|
|
635
|
+
let longFlag;
|
|
636
|
+
const shortFlagExp = /^-[^-]$/;
|
|
637
|
+
const longFlagExp = /^--[^-]/;
|
|
638
|
+
const flagParts = flags.split(/[ |,]+/).concat("guard");
|
|
639
|
+
if (shortFlagExp.test(flagParts[0]))
|
|
640
|
+
shortFlag = flagParts.shift();
|
|
641
|
+
if (longFlagExp.test(flagParts[0]))
|
|
642
|
+
longFlag = flagParts.shift();
|
|
643
|
+
if (!shortFlag && shortFlagExp.test(flagParts[0]))
|
|
644
|
+
shortFlag = flagParts.shift();
|
|
645
|
+
if (!shortFlag && longFlagExp.test(flagParts[0])) {
|
|
646
|
+
shortFlag = longFlag;
|
|
647
|
+
longFlag = flagParts.shift();
|
|
648
|
+
}
|
|
649
|
+
if (flagParts[0].startsWith("-")) {
|
|
650
|
+
const unsupportedFlag = flagParts[0];
|
|
651
|
+
const baseError = `option creation failed due to '${unsupportedFlag}' in option flags '${flags}'`;
|
|
652
|
+
if (/^-[^-][^-]/.test(unsupportedFlag))
|
|
653
|
+
throw new Error(`${baseError}
|
|
654
|
+
- a short flag is a single dash and a single character
|
|
655
|
+
- either use a single dash and a single character (for a short flag)
|
|
656
|
+
- or use a double dash for a long option (and can have two, like '--ws, --workspace')`);
|
|
657
|
+
if (shortFlagExp.test(unsupportedFlag))
|
|
658
|
+
throw new Error(`${baseError}
|
|
659
|
+
- too many short flags`);
|
|
660
|
+
if (longFlagExp.test(unsupportedFlag))
|
|
661
|
+
throw new Error(`${baseError}
|
|
662
|
+
- too many long flags`);
|
|
663
|
+
throw new Error(`${baseError}
|
|
664
|
+
- unrecognised flag format`);
|
|
665
|
+
}
|
|
666
|
+
if (shortFlag === undefined && longFlag === undefined)
|
|
667
|
+
throw new Error(`option creation failed due to no flags found in '${flags}'.`);
|
|
668
|
+
return { shortFlag, longFlag };
|
|
669
|
+
}
|
|
670
|
+
exports.Option = Option;
|
|
671
|
+
exports.DualOptions = DualOptions;
|
|
672
|
+
});
|
|
673
|
+
|
|
674
|
+
// node_modules/commander/lib/suggestSimilar.js
|
|
675
|
+
var require_suggestSimilar = __commonJS((exports) => {
|
|
676
|
+
var maxDistance = 3;
|
|
677
|
+
function editDistance(a, b) {
|
|
678
|
+
if (Math.abs(a.length - b.length) > maxDistance)
|
|
679
|
+
return Math.max(a.length, b.length);
|
|
680
|
+
const d = [];
|
|
681
|
+
for (let i = 0;i <= a.length; i++) {
|
|
682
|
+
d[i] = [i];
|
|
683
|
+
}
|
|
684
|
+
for (let j = 0;j <= b.length; j++) {
|
|
685
|
+
d[0][j] = j;
|
|
686
|
+
}
|
|
687
|
+
for (let j = 1;j <= b.length; j++) {
|
|
688
|
+
for (let i = 1;i <= a.length; i++) {
|
|
689
|
+
let cost = 1;
|
|
690
|
+
if (a[i - 1] === b[j - 1]) {
|
|
691
|
+
cost = 0;
|
|
692
|
+
} else {
|
|
693
|
+
cost = 1;
|
|
694
|
+
}
|
|
695
|
+
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
|
|
696
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
697
|
+
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
return d[a.length][b.length];
|
|
702
|
+
}
|
|
703
|
+
function suggestSimilar(word, candidates) {
|
|
704
|
+
if (!candidates || candidates.length === 0)
|
|
705
|
+
return "";
|
|
706
|
+
candidates = Array.from(new Set(candidates));
|
|
707
|
+
const searchingOptions = word.startsWith("--");
|
|
708
|
+
if (searchingOptions) {
|
|
709
|
+
word = word.slice(2);
|
|
710
|
+
candidates = candidates.map((candidate) => candidate.slice(2));
|
|
711
|
+
}
|
|
712
|
+
let similar = [];
|
|
713
|
+
let bestDistance = maxDistance;
|
|
714
|
+
const minSimilarity = 0.4;
|
|
715
|
+
candidates.forEach((candidate) => {
|
|
716
|
+
if (candidate.length <= 1)
|
|
717
|
+
return;
|
|
718
|
+
const distance = editDistance(word, candidate);
|
|
719
|
+
const length = Math.max(word.length, candidate.length);
|
|
720
|
+
const similarity = (length - distance) / length;
|
|
721
|
+
if (similarity > minSimilarity) {
|
|
722
|
+
if (distance < bestDistance) {
|
|
723
|
+
bestDistance = distance;
|
|
724
|
+
similar = [candidate];
|
|
725
|
+
} else if (distance === bestDistance) {
|
|
726
|
+
similar.push(candidate);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
});
|
|
730
|
+
similar.sort((a, b) => a.localeCompare(b));
|
|
731
|
+
if (searchingOptions) {
|
|
732
|
+
similar = similar.map((candidate) => `--${candidate}`);
|
|
733
|
+
}
|
|
734
|
+
if (similar.length > 1) {
|
|
735
|
+
return `
|
|
736
|
+
(Did you mean one of ${similar.join(", ")}?)`;
|
|
737
|
+
}
|
|
738
|
+
if (similar.length === 1) {
|
|
739
|
+
return `
|
|
740
|
+
(Did you mean ${similar[0]}?)`;
|
|
741
|
+
}
|
|
742
|
+
return "";
|
|
743
|
+
}
|
|
744
|
+
exports.suggestSimilar = suggestSimilar;
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
// node_modules/commander/lib/command.js
|
|
748
|
+
var require_command = __commonJS((exports) => {
|
|
749
|
+
var EventEmitter = __require("events").EventEmitter;
|
|
750
|
+
var childProcess = __require("child_process");
|
|
751
|
+
var path = __require("path");
|
|
752
|
+
var fs = __require("fs");
|
|
753
|
+
var process2 = __require("process");
|
|
754
|
+
var { Argument, humanReadableArgName } = require_argument();
|
|
755
|
+
var { CommanderError } = require_error();
|
|
756
|
+
var { Help, stripColor } = require_help();
|
|
757
|
+
var { Option, DualOptions } = require_option();
|
|
758
|
+
var { suggestSimilar } = require_suggestSimilar();
|
|
759
|
+
|
|
760
|
+
class Command extends EventEmitter {
|
|
761
|
+
constructor(name) {
|
|
762
|
+
super();
|
|
763
|
+
this.commands = [];
|
|
764
|
+
this.options = [];
|
|
765
|
+
this.parent = null;
|
|
766
|
+
this._allowUnknownOption = false;
|
|
767
|
+
this._allowExcessArguments = false;
|
|
768
|
+
this.registeredArguments = [];
|
|
769
|
+
this._args = this.registeredArguments;
|
|
770
|
+
this.args = [];
|
|
771
|
+
this.rawArgs = [];
|
|
772
|
+
this.processedArgs = [];
|
|
773
|
+
this._scriptPath = null;
|
|
774
|
+
this._name = name || "";
|
|
775
|
+
this._optionValues = {};
|
|
776
|
+
this._optionValueSources = {};
|
|
777
|
+
this._storeOptionsAsProperties = false;
|
|
778
|
+
this._actionHandler = null;
|
|
779
|
+
this._executableHandler = false;
|
|
780
|
+
this._executableFile = null;
|
|
781
|
+
this._executableDir = null;
|
|
782
|
+
this._defaultCommandName = null;
|
|
783
|
+
this._exitCallback = null;
|
|
784
|
+
this._aliases = [];
|
|
785
|
+
this._combineFlagAndOptionalValue = true;
|
|
786
|
+
this._description = "";
|
|
787
|
+
this._summary = "";
|
|
788
|
+
this._argsDescription = undefined;
|
|
789
|
+
this._enablePositionalOptions = false;
|
|
790
|
+
this._passThroughOptions = false;
|
|
791
|
+
this._lifeCycleHooks = {};
|
|
792
|
+
this._showHelpAfterError = false;
|
|
793
|
+
this._showSuggestionAfterError = true;
|
|
794
|
+
this._savedState = null;
|
|
795
|
+
this._outputConfiguration = {
|
|
796
|
+
writeOut: (str) => process2.stdout.write(str),
|
|
797
|
+
writeErr: (str) => process2.stderr.write(str),
|
|
798
|
+
outputError: (str, write) => write(str),
|
|
799
|
+
getOutHelpWidth: () => process2.stdout.isTTY ? process2.stdout.columns : undefined,
|
|
800
|
+
getErrHelpWidth: () => process2.stderr.isTTY ? process2.stderr.columns : undefined,
|
|
801
|
+
getOutHasColors: () => useColor() ?? (process2.stdout.isTTY && process2.stdout.hasColors?.()),
|
|
802
|
+
getErrHasColors: () => useColor() ?? (process2.stderr.isTTY && process2.stderr.hasColors?.()),
|
|
803
|
+
stripColor: (str) => stripColor(str)
|
|
804
|
+
};
|
|
805
|
+
this._hidden = false;
|
|
806
|
+
this._helpOption = undefined;
|
|
807
|
+
this._addImplicitHelpCommand = undefined;
|
|
808
|
+
this._helpCommand = undefined;
|
|
809
|
+
this._helpConfiguration = {};
|
|
810
|
+
this._helpGroupHeading = undefined;
|
|
811
|
+
this._defaultCommandGroup = undefined;
|
|
812
|
+
this._defaultOptionGroup = undefined;
|
|
813
|
+
}
|
|
814
|
+
copyInheritedSettings(sourceCommand) {
|
|
815
|
+
this._outputConfiguration = sourceCommand._outputConfiguration;
|
|
816
|
+
this._helpOption = sourceCommand._helpOption;
|
|
817
|
+
this._helpCommand = sourceCommand._helpCommand;
|
|
818
|
+
this._helpConfiguration = sourceCommand._helpConfiguration;
|
|
819
|
+
this._exitCallback = sourceCommand._exitCallback;
|
|
820
|
+
this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
|
|
821
|
+
this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
|
|
822
|
+
this._allowExcessArguments = sourceCommand._allowExcessArguments;
|
|
823
|
+
this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
|
|
824
|
+
this._showHelpAfterError = sourceCommand._showHelpAfterError;
|
|
825
|
+
this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;
|
|
826
|
+
return this;
|
|
827
|
+
}
|
|
828
|
+
_getCommandAndAncestors() {
|
|
829
|
+
const result = [];
|
|
830
|
+
for (let command = this;command; command = command.parent) {
|
|
831
|
+
result.push(command);
|
|
832
|
+
}
|
|
833
|
+
return result;
|
|
834
|
+
}
|
|
835
|
+
command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
|
|
836
|
+
let desc = actionOptsOrExecDesc;
|
|
837
|
+
let opts = execOpts;
|
|
838
|
+
if (typeof desc === "object" && desc !== null) {
|
|
839
|
+
opts = desc;
|
|
840
|
+
desc = null;
|
|
841
|
+
}
|
|
842
|
+
opts = opts || {};
|
|
843
|
+
const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
|
|
844
|
+
const cmd = this.createCommand(name);
|
|
845
|
+
if (desc) {
|
|
846
|
+
cmd.description(desc);
|
|
847
|
+
cmd._executableHandler = true;
|
|
848
|
+
}
|
|
849
|
+
if (opts.isDefault)
|
|
850
|
+
this._defaultCommandName = cmd._name;
|
|
851
|
+
cmd._hidden = !!(opts.noHelp || opts.hidden);
|
|
852
|
+
cmd._executableFile = opts.executableFile || null;
|
|
853
|
+
if (args)
|
|
854
|
+
cmd.arguments(args);
|
|
855
|
+
this._registerCommand(cmd);
|
|
856
|
+
cmd.parent = this;
|
|
857
|
+
cmd.copyInheritedSettings(this);
|
|
858
|
+
if (desc)
|
|
859
|
+
return this;
|
|
860
|
+
return cmd;
|
|
861
|
+
}
|
|
862
|
+
createCommand(name) {
|
|
863
|
+
return new Command(name);
|
|
864
|
+
}
|
|
865
|
+
createHelp() {
|
|
866
|
+
return Object.assign(new Help, this.configureHelp());
|
|
867
|
+
}
|
|
868
|
+
configureHelp(configuration) {
|
|
869
|
+
if (configuration === undefined)
|
|
870
|
+
return this._helpConfiguration;
|
|
871
|
+
this._helpConfiguration = configuration;
|
|
872
|
+
return this;
|
|
873
|
+
}
|
|
874
|
+
configureOutput(configuration) {
|
|
875
|
+
if (configuration === undefined)
|
|
876
|
+
return this._outputConfiguration;
|
|
877
|
+
this._outputConfiguration = {
|
|
878
|
+
...this._outputConfiguration,
|
|
879
|
+
...configuration
|
|
880
|
+
};
|
|
881
|
+
return this;
|
|
882
|
+
}
|
|
883
|
+
showHelpAfterError(displayHelp = true) {
|
|
884
|
+
if (typeof displayHelp !== "string")
|
|
885
|
+
displayHelp = !!displayHelp;
|
|
886
|
+
this._showHelpAfterError = displayHelp;
|
|
887
|
+
return this;
|
|
888
|
+
}
|
|
889
|
+
showSuggestionAfterError(displaySuggestion = true) {
|
|
890
|
+
this._showSuggestionAfterError = !!displaySuggestion;
|
|
891
|
+
return this;
|
|
892
|
+
}
|
|
893
|
+
addCommand(cmd, opts) {
|
|
894
|
+
if (!cmd._name) {
|
|
895
|
+
throw new Error(`Command passed to .addCommand() must have a name
|
|
896
|
+
- specify the name in Command constructor or using .name()`);
|
|
897
|
+
}
|
|
898
|
+
opts = opts || {};
|
|
899
|
+
if (opts.isDefault)
|
|
900
|
+
this._defaultCommandName = cmd._name;
|
|
901
|
+
if (opts.noHelp || opts.hidden)
|
|
902
|
+
cmd._hidden = true;
|
|
903
|
+
this._registerCommand(cmd);
|
|
904
|
+
cmd.parent = this;
|
|
905
|
+
cmd._checkForBrokenPassThrough();
|
|
906
|
+
return this;
|
|
907
|
+
}
|
|
908
|
+
createArgument(name, description) {
|
|
909
|
+
return new Argument(name, description);
|
|
910
|
+
}
|
|
911
|
+
argument(name, description, parseArg, defaultValue) {
|
|
912
|
+
const argument = this.createArgument(name, description);
|
|
913
|
+
if (typeof parseArg === "function") {
|
|
914
|
+
argument.default(defaultValue).argParser(parseArg);
|
|
915
|
+
} else {
|
|
916
|
+
argument.default(parseArg);
|
|
917
|
+
}
|
|
918
|
+
this.addArgument(argument);
|
|
919
|
+
return this;
|
|
920
|
+
}
|
|
921
|
+
arguments(names) {
|
|
922
|
+
names.trim().split(/ +/).forEach((detail) => {
|
|
923
|
+
this.argument(detail);
|
|
924
|
+
});
|
|
925
|
+
return this;
|
|
926
|
+
}
|
|
927
|
+
addArgument(argument) {
|
|
928
|
+
const previousArgument = this.registeredArguments.slice(-1)[0];
|
|
929
|
+
if (previousArgument?.variadic) {
|
|
930
|
+
throw new Error(`only the last argument can be variadic '${previousArgument.name()}'`);
|
|
931
|
+
}
|
|
932
|
+
if (argument.required && argument.defaultValue !== undefined && argument.parseArg === undefined) {
|
|
933
|
+
throw new Error(`a default value for a required argument is never used: '${argument.name()}'`);
|
|
934
|
+
}
|
|
935
|
+
this.registeredArguments.push(argument);
|
|
936
|
+
return this;
|
|
937
|
+
}
|
|
938
|
+
helpCommand(enableOrNameAndArgs, description) {
|
|
939
|
+
if (typeof enableOrNameAndArgs === "boolean") {
|
|
940
|
+
this._addImplicitHelpCommand = enableOrNameAndArgs;
|
|
941
|
+
if (enableOrNameAndArgs && this._defaultCommandGroup) {
|
|
942
|
+
this._initCommandGroup(this._getHelpCommand());
|
|
943
|
+
}
|
|
944
|
+
return this;
|
|
945
|
+
}
|
|
946
|
+
const nameAndArgs = enableOrNameAndArgs ?? "help [command]";
|
|
947
|
+
const [, helpName, helpArgs] = nameAndArgs.match(/([^ ]+) *(.*)/);
|
|
948
|
+
const helpDescription = description ?? "display help for command";
|
|
949
|
+
const helpCommand = this.createCommand(helpName);
|
|
950
|
+
helpCommand.helpOption(false);
|
|
951
|
+
if (helpArgs)
|
|
952
|
+
helpCommand.arguments(helpArgs);
|
|
953
|
+
if (helpDescription)
|
|
954
|
+
helpCommand.description(helpDescription);
|
|
955
|
+
this._addImplicitHelpCommand = true;
|
|
956
|
+
this._helpCommand = helpCommand;
|
|
957
|
+
if (enableOrNameAndArgs || description)
|
|
958
|
+
this._initCommandGroup(helpCommand);
|
|
959
|
+
return this;
|
|
960
|
+
}
|
|
961
|
+
addHelpCommand(helpCommand, deprecatedDescription) {
|
|
962
|
+
if (typeof helpCommand !== "object") {
|
|
963
|
+
this.helpCommand(helpCommand, deprecatedDescription);
|
|
964
|
+
return this;
|
|
965
|
+
}
|
|
966
|
+
this._addImplicitHelpCommand = true;
|
|
967
|
+
this._helpCommand = helpCommand;
|
|
968
|
+
this._initCommandGroup(helpCommand);
|
|
969
|
+
return this;
|
|
970
|
+
}
|
|
971
|
+
_getHelpCommand() {
|
|
972
|
+
const hasImplicitHelpCommand = this._addImplicitHelpCommand ?? (this.commands.length && !this._actionHandler && !this._findCommand("help"));
|
|
973
|
+
if (hasImplicitHelpCommand) {
|
|
974
|
+
if (this._helpCommand === undefined) {
|
|
975
|
+
this.helpCommand(undefined, undefined);
|
|
976
|
+
}
|
|
977
|
+
return this._helpCommand;
|
|
978
|
+
}
|
|
979
|
+
return null;
|
|
980
|
+
}
|
|
981
|
+
hook(event, listener) {
|
|
982
|
+
const allowedValues = ["preSubcommand", "preAction", "postAction"];
|
|
983
|
+
if (!allowedValues.includes(event)) {
|
|
984
|
+
throw new Error(`Unexpected value for event passed to hook : '${event}'.
|
|
985
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
986
|
+
}
|
|
987
|
+
if (this._lifeCycleHooks[event]) {
|
|
988
|
+
this._lifeCycleHooks[event].push(listener);
|
|
989
|
+
} else {
|
|
990
|
+
this._lifeCycleHooks[event] = [listener];
|
|
991
|
+
}
|
|
992
|
+
return this;
|
|
993
|
+
}
|
|
994
|
+
exitOverride(fn) {
|
|
995
|
+
if (fn) {
|
|
996
|
+
this._exitCallback = fn;
|
|
997
|
+
} else {
|
|
998
|
+
this._exitCallback = (err) => {
|
|
999
|
+
if (err.code !== "commander.executeSubCommandAsync") {
|
|
1000
|
+
throw err;
|
|
1001
|
+
} else {}
|
|
1002
|
+
};
|
|
1003
|
+
}
|
|
1004
|
+
return this;
|
|
1005
|
+
}
|
|
1006
|
+
_exit(exitCode, code, message) {
|
|
1007
|
+
if (this._exitCallback) {
|
|
1008
|
+
this._exitCallback(new CommanderError(exitCode, code, message));
|
|
1009
|
+
}
|
|
1010
|
+
process2.exit(exitCode);
|
|
1011
|
+
}
|
|
1012
|
+
action(fn) {
|
|
1013
|
+
const listener = (args) => {
|
|
1014
|
+
const expectedArgsCount = this.registeredArguments.length;
|
|
1015
|
+
const actionArgs = args.slice(0, expectedArgsCount);
|
|
1016
|
+
if (this._storeOptionsAsProperties) {
|
|
1017
|
+
actionArgs[expectedArgsCount] = this;
|
|
1018
|
+
} else {
|
|
1019
|
+
actionArgs[expectedArgsCount] = this.opts();
|
|
1020
|
+
}
|
|
1021
|
+
actionArgs.push(this);
|
|
1022
|
+
return fn.apply(this, actionArgs);
|
|
1023
|
+
};
|
|
1024
|
+
this._actionHandler = listener;
|
|
1025
|
+
return this;
|
|
1026
|
+
}
|
|
1027
|
+
createOption(flags, description) {
|
|
1028
|
+
return new Option(flags, description);
|
|
1029
|
+
}
|
|
1030
|
+
_callParseArg(target, value, previous, invalidArgumentMessage) {
|
|
1031
|
+
try {
|
|
1032
|
+
return target.parseArg(value, previous);
|
|
1033
|
+
} catch (err) {
|
|
1034
|
+
if (err.code === "commander.invalidArgument") {
|
|
1035
|
+
const message = `${invalidArgumentMessage} ${err.message}`;
|
|
1036
|
+
this.error(message, { exitCode: err.exitCode, code: err.code });
|
|
1037
|
+
}
|
|
1038
|
+
throw err;
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
_registerOption(option) {
|
|
1042
|
+
const matchingOption = option.short && this._findOption(option.short) || option.long && this._findOption(option.long);
|
|
1043
|
+
if (matchingOption) {
|
|
1044
|
+
const matchingFlag = option.long && this._findOption(option.long) ? option.long : option.short;
|
|
1045
|
+
throw new Error(`Cannot add option '${option.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'
|
|
1046
|
+
- already used by option '${matchingOption.flags}'`);
|
|
1047
|
+
}
|
|
1048
|
+
this._initOptionGroup(option);
|
|
1049
|
+
this.options.push(option);
|
|
1050
|
+
}
|
|
1051
|
+
_registerCommand(command) {
|
|
1052
|
+
const knownBy = (cmd) => {
|
|
1053
|
+
return [cmd.name()].concat(cmd.aliases());
|
|
1054
|
+
};
|
|
1055
|
+
const alreadyUsed = knownBy(command).find((name) => this._findCommand(name));
|
|
1056
|
+
if (alreadyUsed) {
|
|
1057
|
+
const existingCmd = knownBy(this._findCommand(alreadyUsed)).join("|");
|
|
1058
|
+
const newCmd = knownBy(command).join("|");
|
|
1059
|
+
throw new Error(`cannot add command '${newCmd}' as already have command '${existingCmd}'`);
|
|
1060
|
+
}
|
|
1061
|
+
this._initCommandGroup(command);
|
|
1062
|
+
this.commands.push(command);
|
|
1063
|
+
}
|
|
1064
|
+
addOption(option) {
|
|
1065
|
+
this._registerOption(option);
|
|
1066
|
+
const oname = option.name();
|
|
1067
|
+
const name = option.attributeName();
|
|
1068
|
+
if (option.negate) {
|
|
1069
|
+
const positiveLongFlag = option.long.replace(/^--no-/, "--");
|
|
1070
|
+
if (!this._findOption(positiveLongFlag)) {
|
|
1071
|
+
this.setOptionValueWithSource(name, option.defaultValue === undefined ? true : option.defaultValue, "default");
|
|
1072
|
+
}
|
|
1073
|
+
} else if (option.defaultValue !== undefined) {
|
|
1074
|
+
this.setOptionValueWithSource(name, option.defaultValue, "default");
|
|
1075
|
+
}
|
|
1076
|
+
const handleOptionValue = (val, invalidValueMessage, valueSource) => {
|
|
1077
|
+
if (val == null && option.presetArg !== undefined) {
|
|
1078
|
+
val = option.presetArg;
|
|
1079
|
+
}
|
|
1080
|
+
const oldValue = this.getOptionValue(name);
|
|
1081
|
+
if (val !== null && option.parseArg) {
|
|
1082
|
+
val = this._callParseArg(option, val, oldValue, invalidValueMessage);
|
|
1083
|
+
} else if (val !== null && option.variadic) {
|
|
1084
|
+
val = option._collectValue(val, oldValue);
|
|
1085
|
+
}
|
|
1086
|
+
if (val == null) {
|
|
1087
|
+
if (option.negate) {
|
|
1088
|
+
val = false;
|
|
1089
|
+
} else if (option.isBoolean() || option.optional) {
|
|
1090
|
+
val = true;
|
|
1091
|
+
} else {
|
|
1092
|
+
val = "";
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
this.setOptionValueWithSource(name, val, valueSource);
|
|
1096
|
+
};
|
|
1097
|
+
this.on("option:" + oname, (val) => {
|
|
1098
|
+
const invalidValueMessage = `error: option '${option.flags}' argument '${val}' is invalid.`;
|
|
1099
|
+
handleOptionValue(val, invalidValueMessage, "cli");
|
|
1100
|
+
});
|
|
1101
|
+
if (option.envVar) {
|
|
1102
|
+
this.on("optionEnv:" + oname, (val) => {
|
|
1103
|
+
const invalidValueMessage = `error: option '${option.flags}' value '${val}' from env '${option.envVar}' is invalid.`;
|
|
1104
|
+
handleOptionValue(val, invalidValueMessage, "env");
|
|
1105
|
+
});
|
|
1106
|
+
}
|
|
1107
|
+
return this;
|
|
1108
|
+
}
|
|
1109
|
+
_optionEx(config, flags, description, fn, defaultValue) {
|
|
1110
|
+
if (typeof flags === "object" && flags instanceof Option) {
|
|
1111
|
+
throw new Error("To add an Option object use addOption() instead of option() or requiredOption()");
|
|
1112
|
+
}
|
|
1113
|
+
const option = this.createOption(flags, description);
|
|
1114
|
+
option.makeOptionMandatory(!!config.mandatory);
|
|
1115
|
+
if (typeof fn === "function") {
|
|
1116
|
+
option.default(defaultValue).argParser(fn);
|
|
1117
|
+
} else if (fn instanceof RegExp) {
|
|
1118
|
+
const regex = fn;
|
|
1119
|
+
fn = (val, def) => {
|
|
1120
|
+
const m = regex.exec(val);
|
|
1121
|
+
return m ? m[0] : def;
|
|
1122
|
+
};
|
|
1123
|
+
option.default(defaultValue).argParser(fn);
|
|
1124
|
+
} else {
|
|
1125
|
+
option.default(fn);
|
|
1126
|
+
}
|
|
1127
|
+
return this.addOption(option);
|
|
1128
|
+
}
|
|
1129
|
+
option(flags, description, parseArg, defaultValue) {
|
|
1130
|
+
return this._optionEx({}, flags, description, parseArg, defaultValue);
|
|
1131
|
+
}
|
|
1132
|
+
requiredOption(flags, description, parseArg, defaultValue) {
|
|
1133
|
+
return this._optionEx({ mandatory: true }, flags, description, parseArg, defaultValue);
|
|
1134
|
+
}
|
|
1135
|
+
combineFlagAndOptionalValue(combine = true) {
|
|
1136
|
+
this._combineFlagAndOptionalValue = !!combine;
|
|
1137
|
+
return this;
|
|
1138
|
+
}
|
|
1139
|
+
allowUnknownOption(allowUnknown = true) {
|
|
1140
|
+
this._allowUnknownOption = !!allowUnknown;
|
|
1141
|
+
return this;
|
|
1142
|
+
}
|
|
1143
|
+
allowExcessArguments(allowExcess = true) {
|
|
1144
|
+
this._allowExcessArguments = !!allowExcess;
|
|
1145
|
+
return this;
|
|
1146
|
+
}
|
|
1147
|
+
enablePositionalOptions(positional = true) {
|
|
1148
|
+
this._enablePositionalOptions = !!positional;
|
|
1149
|
+
return this;
|
|
1150
|
+
}
|
|
1151
|
+
passThroughOptions(passThrough = true) {
|
|
1152
|
+
this._passThroughOptions = !!passThrough;
|
|
1153
|
+
this._checkForBrokenPassThrough();
|
|
1154
|
+
return this;
|
|
1155
|
+
}
|
|
1156
|
+
_checkForBrokenPassThrough() {
|
|
1157
|
+
if (this.parent && this._passThroughOptions && !this.parent._enablePositionalOptions) {
|
|
1158
|
+
throw new Error(`passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`);
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
storeOptionsAsProperties(storeAsProperties = true) {
|
|
1162
|
+
if (this.options.length) {
|
|
1163
|
+
throw new Error("call .storeOptionsAsProperties() before adding options");
|
|
1164
|
+
}
|
|
1165
|
+
if (Object.keys(this._optionValues).length) {
|
|
1166
|
+
throw new Error("call .storeOptionsAsProperties() before setting option values");
|
|
1167
|
+
}
|
|
1168
|
+
this._storeOptionsAsProperties = !!storeAsProperties;
|
|
1169
|
+
return this;
|
|
1170
|
+
}
|
|
1171
|
+
getOptionValue(key) {
|
|
1172
|
+
if (this._storeOptionsAsProperties) {
|
|
1173
|
+
return this[key];
|
|
1174
|
+
}
|
|
1175
|
+
return this._optionValues[key];
|
|
1176
|
+
}
|
|
1177
|
+
setOptionValue(key, value) {
|
|
1178
|
+
return this.setOptionValueWithSource(key, value, undefined);
|
|
1179
|
+
}
|
|
1180
|
+
setOptionValueWithSource(key, value, source) {
|
|
1181
|
+
if (this._storeOptionsAsProperties) {
|
|
1182
|
+
this[key] = value;
|
|
1183
|
+
} else {
|
|
1184
|
+
this._optionValues[key] = value;
|
|
1185
|
+
}
|
|
1186
|
+
this._optionValueSources[key] = source;
|
|
1187
|
+
return this;
|
|
1188
|
+
}
|
|
1189
|
+
getOptionValueSource(key) {
|
|
1190
|
+
return this._optionValueSources[key];
|
|
1191
|
+
}
|
|
1192
|
+
getOptionValueSourceWithGlobals(key) {
|
|
1193
|
+
let source;
|
|
1194
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1195
|
+
if (cmd.getOptionValueSource(key) !== undefined) {
|
|
1196
|
+
source = cmd.getOptionValueSource(key);
|
|
1197
|
+
}
|
|
1198
|
+
});
|
|
1199
|
+
return source;
|
|
1200
|
+
}
|
|
1201
|
+
_prepareUserArgs(argv, parseOptions) {
|
|
1202
|
+
if (argv !== undefined && !Array.isArray(argv)) {
|
|
1203
|
+
throw new Error("first parameter to parse must be array or undefined");
|
|
1204
|
+
}
|
|
1205
|
+
parseOptions = parseOptions || {};
|
|
1206
|
+
if (argv === undefined && parseOptions.from === undefined) {
|
|
1207
|
+
if (process2.versions?.electron) {
|
|
1208
|
+
parseOptions.from = "electron";
|
|
1209
|
+
}
|
|
1210
|
+
const execArgv = process2.execArgv ?? [];
|
|
1211
|
+
if (execArgv.includes("-e") || execArgv.includes("--eval") || execArgv.includes("-p") || execArgv.includes("--print")) {
|
|
1212
|
+
parseOptions.from = "eval";
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
if (argv === undefined) {
|
|
1216
|
+
argv = process2.argv;
|
|
1217
|
+
}
|
|
1218
|
+
this.rawArgs = argv.slice();
|
|
1219
|
+
let userArgs;
|
|
1220
|
+
switch (parseOptions.from) {
|
|
1221
|
+
case undefined:
|
|
1222
|
+
case "node":
|
|
1223
|
+
this._scriptPath = argv[1];
|
|
1224
|
+
userArgs = argv.slice(2);
|
|
1225
|
+
break;
|
|
1226
|
+
case "electron":
|
|
1227
|
+
if (process2.defaultApp) {
|
|
1228
|
+
this._scriptPath = argv[1];
|
|
1229
|
+
userArgs = argv.slice(2);
|
|
1230
|
+
} else {
|
|
1231
|
+
userArgs = argv.slice(1);
|
|
1232
|
+
}
|
|
1233
|
+
break;
|
|
1234
|
+
case "user":
|
|
1235
|
+
userArgs = argv.slice(0);
|
|
1236
|
+
break;
|
|
1237
|
+
case "eval":
|
|
1238
|
+
userArgs = argv.slice(1);
|
|
1239
|
+
break;
|
|
1240
|
+
default:
|
|
1241
|
+
throw new Error(`unexpected parse option { from: '${parseOptions.from}' }`);
|
|
1242
|
+
}
|
|
1243
|
+
if (!this._name && this._scriptPath)
|
|
1244
|
+
this.nameFromFilename(this._scriptPath);
|
|
1245
|
+
this._name = this._name || "program";
|
|
1246
|
+
return userArgs;
|
|
1247
|
+
}
|
|
1248
|
+
parse(argv, parseOptions) {
|
|
1249
|
+
this._prepareForParse();
|
|
1250
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1251
|
+
this._parseCommand([], userArgs);
|
|
1252
|
+
return this;
|
|
1253
|
+
}
|
|
1254
|
+
async parseAsync(argv, parseOptions) {
|
|
1255
|
+
this._prepareForParse();
|
|
1256
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1257
|
+
await this._parseCommand([], userArgs);
|
|
1258
|
+
return this;
|
|
1259
|
+
}
|
|
1260
|
+
_prepareForParse() {
|
|
1261
|
+
if (this._savedState === null) {
|
|
1262
|
+
this.saveStateBeforeParse();
|
|
1263
|
+
} else {
|
|
1264
|
+
this.restoreStateBeforeParse();
|
|
1265
|
+
}
|
|
1266
|
+
}
|
|
1267
|
+
saveStateBeforeParse() {
|
|
1268
|
+
this._savedState = {
|
|
1269
|
+
_name: this._name,
|
|
1270
|
+
_optionValues: { ...this._optionValues },
|
|
1271
|
+
_optionValueSources: { ...this._optionValueSources }
|
|
1272
|
+
};
|
|
1273
|
+
}
|
|
1274
|
+
restoreStateBeforeParse() {
|
|
1275
|
+
if (this._storeOptionsAsProperties)
|
|
1276
|
+
throw new Error(`Can not call parse again when storeOptionsAsProperties is true.
|
|
1277
|
+
- either make a new Command for each call to parse, or stop storing options as properties`);
|
|
1278
|
+
this._name = this._savedState._name;
|
|
1279
|
+
this._scriptPath = null;
|
|
1280
|
+
this.rawArgs = [];
|
|
1281
|
+
this._optionValues = { ...this._savedState._optionValues };
|
|
1282
|
+
this._optionValueSources = { ...this._savedState._optionValueSources };
|
|
1283
|
+
this.args = [];
|
|
1284
|
+
this.processedArgs = [];
|
|
1285
|
+
}
|
|
1286
|
+
_checkForMissingExecutable(executableFile, executableDir, subcommandName) {
|
|
1287
|
+
if (fs.existsSync(executableFile))
|
|
1288
|
+
return;
|
|
1289
|
+
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";
|
|
1290
|
+
const executableMissing = `'${executableFile}' does not exist
|
|
1291
|
+
- if '${subcommandName}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
|
|
1292
|
+
- if the default executable name is not suitable, use the executableFile option to supply a custom name or path
|
|
1293
|
+
- ${executableDirMessage}`;
|
|
1294
|
+
throw new Error(executableMissing);
|
|
1295
|
+
}
|
|
1296
|
+
_executeSubCommand(subcommand, args) {
|
|
1297
|
+
args = args.slice();
|
|
1298
|
+
let launchWithNode = false;
|
|
1299
|
+
const sourceExt = [".js", ".ts", ".tsx", ".mjs", ".cjs"];
|
|
1300
|
+
function findFile(baseDir, baseName) {
|
|
1301
|
+
const localBin = path.resolve(baseDir, baseName);
|
|
1302
|
+
if (fs.existsSync(localBin))
|
|
1303
|
+
return localBin;
|
|
1304
|
+
if (sourceExt.includes(path.extname(baseName)))
|
|
1305
|
+
return;
|
|
1306
|
+
const foundExt = sourceExt.find((ext) => fs.existsSync(`${localBin}${ext}`));
|
|
1307
|
+
if (foundExt)
|
|
1308
|
+
return `${localBin}${foundExt}`;
|
|
1309
|
+
return;
|
|
1310
|
+
}
|
|
1311
|
+
this._checkForMissingMandatoryOptions();
|
|
1312
|
+
this._checkForConflictingOptions();
|
|
1313
|
+
let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;
|
|
1314
|
+
let executableDir = this._executableDir || "";
|
|
1315
|
+
if (this._scriptPath) {
|
|
1316
|
+
let resolvedScriptPath;
|
|
1317
|
+
try {
|
|
1318
|
+
resolvedScriptPath = fs.realpathSync(this._scriptPath);
|
|
1319
|
+
} catch {
|
|
1320
|
+
resolvedScriptPath = this._scriptPath;
|
|
1321
|
+
}
|
|
1322
|
+
executableDir = path.resolve(path.dirname(resolvedScriptPath), executableDir);
|
|
1323
|
+
}
|
|
1324
|
+
if (executableDir) {
|
|
1325
|
+
let localFile = findFile(executableDir, executableFile);
|
|
1326
|
+
if (!localFile && !subcommand._executableFile && this._scriptPath) {
|
|
1327
|
+
const legacyName = path.basename(this._scriptPath, path.extname(this._scriptPath));
|
|
1328
|
+
if (legacyName !== this._name) {
|
|
1329
|
+
localFile = findFile(executableDir, `${legacyName}-${subcommand._name}`);
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
executableFile = localFile || executableFile;
|
|
1333
|
+
}
|
|
1334
|
+
launchWithNode = sourceExt.includes(path.extname(executableFile));
|
|
1335
|
+
let proc;
|
|
1336
|
+
if (process2.platform !== "win32") {
|
|
1337
|
+
if (launchWithNode) {
|
|
1338
|
+
args.unshift(executableFile);
|
|
1339
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1340
|
+
proc = childProcess.spawn(process2.argv[0], args, { stdio: "inherit" });
|
|
1341
|
+
} else {
|
|
1342
|
+
proc = childProcess.spawn(executableFile, args, { stdio: "inherit" });
|
|
1343
|
+
}
|
|
1344
|
+
} else {
|
|
1345
|
+
this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
|
|
1346
|
+
args.unshift(executableFile);
|
|
1347
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1348
|
+
proc = childProcess.spawn(process2.execPath, args, { stdio: "inherit" });
|
|
1349
|
+
}
|
|
1350
|
+
if (!proc.killed) {
|
|
1351
|
+
const signals = ["SIGUSR1", "SIGUSR2", "SIGTERM", "SIGINT", "SIGHUP"];
|
|
1352
|
+
signals.forEach((signal) => {
|
|
1353
|
+
process2.on(signal, () => {
|
|
1354
|
+
if (proc.killed === false && proc.exitCode === null) {
|
|
1355
|
+
proc.kill(signal);
|
|
1356
|
+
}
|
|
1357
|
+
});
|
|
1358
|
+
});
|
|
1359
|
+
}
|
|
1360
|
+
const exitCallback = this._exitCallback;
|
|
1361
|
+
proc.on("close", (code) => {
|
|
1362
|
+
code = code ?? 1;
|
|
1363
|
+
if (!exitCallback) {
|
|
1364
|
+
process2.exit(code);
|
|
1365
|
+
} else {
|
|
1366
|
+
exitCallback(new CommanderError(code, "commander.executeSubCommandAsync", "(close)"));
|
|
1367
|
+
}
|
|
1368
|
+
});
|
|
1369
|
+
proc.on("error", (err) => {
|
|
1370
|
+
if (err.code === "ENOENT") {
|
|
1371
|
+
this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
|
|
1372
|
+
} else if (err.code === "EACCES") {
|
|
1373
|
+
throw new Error(`'${executableFile}' not executable`);
|
|
1374
|
+
}
|
|
1375
|
+
if (!exitCallback) {
|
|
1376
|
+
process2.exit(1);
|
|
1377
|
+
} else {
|
|
1378
|
+
const wrappedError = new CommanderError(1, "commander.executeSubCommandAsync", "(error)");
|
|
1379
|
+
wrappedError.nestedError = err;
|
|
1380
|
+
exitCallback(wrappedError);
|
|
1381
|
+
}
|
|
1382
|
+
});
|
|
1383
|
+
this.runningCommand = proc;
|
|
1384
|
+
}
|
|
1385
|
+
_dispatchSubcommand(commandName, operands, unknown) {
|
|
1386
|
+
const subCommand = this._findCommand(commandName);
|
|
1387
|
+
if (!subCommand)
|
|
1388
|
+
this.help({ error: true });
|
|
1389
|
+
subCommand._prepareForParse();
|
|
1390
|
+
let promiseChain;
|
|
1391
|
+
promiseChain = this._chainOrCallSubCommandHook(promiseChain, subCommand, "preSubcommand");
|
|
1392
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
1393
|
+
if (subCommand._executableHandler) {
|
|
1394
|
+
this._executeSubCommand(subCommand, operands.concat(unknown));
|
|
1395
|
+
} else {
|
|
1396
|
+
return subCommand._parseCommand(operands, unknown);
|
|
1397
|
+
}
|
|
1398
|
+
});
|
|
1399
|
+
return promiseChain;
|
|
1400
|
+
}
|
|
1401
|
+
_dispatchHelpCommand(subcommandName) {
|
|
1402
|
+
if (!subcommandName) {
|
|
1403
|
+
this.help();
|
|
1404
|
+
}
|
|
1405
|
+
const subCommand = this._findCommand(subcommandName);
|
|
1406
|
+
if (subCommand && !subCommand._executableHandler) {
|
|
1407
|
+
subCommand.help();
|
|
1408
|
+
}
|
|
1409
|
+
return this._dispatchSubcommand(subcommandName, [], [this._getHelpOption()?.long ?? this._getHelpOption()?.short ?? "--help"]);
|
|
1410
|
+
}
|
|
1411
|
+
_checkNumberOfArguments() {
|
|
1412
|
+
this.registeredArguments.forEach((arg, i) => {
|
|
1413
|
+
if (arg.required && this.args[i] == null) {
|
|
1414
|
+
this.missingArgument(arg.name());
|
|
1415
|
+
}
|
|
1416
|
+
});
|
|
1417
|
+
if (this.registeredArguments.length > 0 && this.registeredArguments[this.registeredArguments.length - 1].variadic) {
|
|
1418
|
+
return;
|
|
1419
|
+
}
|
|
1420
|
+
if (this.args.length > this.registeredArguments.length) {
|
|
1421
|
+
this._excessArguments(this.args);
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
_processArguments() {
|
|
1425
|
+
const myParseArg = (argument, value, previous) => {
|
|
1426
|
+
let parsedValue = value;
|
|
1427
|
+
if (value !== null && argument.parseArg) {
|
|
1428
|
+
const invalidValueMessage = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'.`;
|
|
1429
|
+
parsedValue = this._callParseArg(argument, value, previous, invalidValueMessage);
|
|
1430
|
+
}
|
|
1431
|
+
return parsedValue;
|
|
1432
|
+
};
|
|
1433
|
+
this._checkNumberOfArguments();
|
|
1434
|
+
const processedArgs = [];
|
|
1435
|
+
this.registeredArguments.forEach((declaredArg, index) => {
|
|
1436
|
+
let value = declaredArg.defaultValue;
|
|
1437
|
+
if (declaredArg.variadic) {
|
|
1438
|
+
if (index < this.args.length) {
|
|
1439
|
+
value = this.args.slice(index);
|
|
1440
|
+
if (declaredArg.parseArg) {
|
|
1441
|
+
value = value.reduce((processed, v) => {
|
|
1442
|
+
return myParseArg(declaredArg, v, processed);
|
|
1443
|
+
}, declaredArg.defaultValue);
|
|
1444
|
+
}
|
|
1445
|
+
} else if (value === undefined) {
|
|
1446
|
+
value = [];
|
|
1447
|
+
}
|
|
1448
|
+
} else if (index < this.args.length) {
|
|
1449
|
+
value = this.args[index];
|
|
1450
|
+
if (declaredArg.parseArg) {
|
|
1451
|
+
value = myParseArg(declaredArg, value, declaredArg.defaultValue);
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
processedArgs[index] = value;
|
|
1455
|
+
});
|
|
1456
|
+
this.processedArgs = processedArgs;
|
|
1457
|
+
}
|
|
1458
|
+
_chainOrCall(promise, fn) {
|
|
1459
|
+
if (promise?.then && typeof promise.then === "function") {
|
|
1460
|
+
return promise.then(() => fn());
|
|
1461
|
+
}
|
|
1462
|
+
return fn();
|
|
1463
|
+
}
|
|
1464
|
+
_chainOrCallHooks(promise, event) {
|
|
1465
|
+
let result = promise;
|
|
1466
|
+
const hooks = [];
|
|
1467
|
+
this._getCommandAndAncestors().reverse().filter((cmd) => cmd._lifeCycleHooks[event] !== undefined).forEach((hookedCommand) => {
|
|
1468
|
+
hookedCommand._lifeCycleHooks[event].forEach((callback) => {
|
|
1469
|
+
hooks.push({ hookedCommand, callback });
|
|
1470
|
+
});
|
|
1471
|
+
});
|
|
1472
|
+
if (event === "postAction") {
|
|
1473
|
+
hooks.reverse();
|
|
1474
|
+
}
|
|
1475
|
+
hooks.forEach((hookDetail) => {
|
|
1476
|
+
result = this._chainOrCall(result, () => {
|
|
1477
|
+
return hookDetail.callback(hookDetail.hookedCommand, this);
|
|
1478
|
+
});
|
|
1479
|
+
});
|
|
1480
|
+
return result;
|
|
1481
|
+
}
|
|
1482
|
+
_chainOrCallSubCommandHook(promise, subCommand, event) {
|
|
1483
|
+
let result = promise;
|
|
1484
|
+
if (this._lifeCycleHooks[event] !== undefined) {
|
|
1485
|
+
this._lifeCycleHooks[event].forEach((hook) => {
|
|
1486
|
+
result = this._chainOrCall(result, () => {
|
|
1487
|
+
return hook(this, subCommand);
|
|
1488
|
+
});
|
|
1489
|
+
});
|
|
1490
|
+
}
|
|
1491
|
+
return result;
|
|
1492
|
+
}
|
|
1493
|
+
_parseCommand(operands, unknown) {
|
|
1494
|
+
const parsed = this.parseOptions(unknown);
|
|
1495
|
+
this._parseOptionsEnv();
|
|
1496
|
+
this._parseOptionsImplied();
|
|
1497
|
+
operands = operands.concat(parsed.operands);
|
|
1498
|
+
unknown = parsed.unknown;
|
|
1499
|
+
this.args = operands.concat(unknown);
|
|
1500
|
+
if (operands && this._findCommand(operands[0])) {
|
|
1501
|
+
return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
|
|
1502
|
+
}
|
|
1503
|
+
if (this._getHelpCommand() && operands[0] === this._getHelpCommand().name()) {
|
|
1504
|
+
return this._dispatchHelpCommand(operands[1]);
|
|
1505
|
+
}
|
|
1506
|
+
if (this._defaultCommandName) {
|
|
1507
|
+
this._outputHelpIfRequested(unknown);
|
|
1508
|
+
return this._dispatchSubcommand(this._defaultCommandName, operands, unknown);
|
|
1509
|
+
}
|
|
1510
|
+
if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) {
|
|
1511
|
+
this.help({ error: true });
|
|
1512
|
+
}
|
|
1513
|
+
this._outputHelpIfRequested(parsed.unknown);
|
|
1514
|
+
this._checkForMissingMandatoryOptions();
|
|
1515
|
+
this._checkForConflictingOptions();
|
|
1516
|
+
const checkForUnknownOptions = () => {
|
|
1517
|
+
if (parsed.unknown.length > 0) {
|
|
1518
|
+
this.unknownOption(parsed.unknown[0]);
|
|
1519
|
+
}
|
|
1520
|
+
};
|
|
1521
|
+
const commandEvent = `command:${this.name()}`;
|
|
1522
|
+
if (this._actionHandler) {
|
|
1523
|
+
checkForUnknownOptions();
|
|
1524
|
+
this._processArguments();
|
|
1525
|
+
let promiseChain;
|
|
1526
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "preAction");
|
|
1527
|
+
promiseChain = this._chainOrCall(promiseChain, () => this._actionHandler(this.processedArgs));
|
|
1528
|
+
if (this.parent) {
|
|
1529
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
1530
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1531
|
+
});
|
|
1532
|
+
}
|
|
1533
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "postAction");
|
|
1534
|
+
return promiseChain;
|
|
1535
|
+
}
|
|
1536
|
+
if (this.parent?.listenerCount(commandEvent)) {
|
|
1537
|
+
checkForUnknownOptions();
|
|
1538
|
+
this._processArguments();
|
|
1539
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1540
|
+
} else if (operands.length) {
|
|
1541
|
+
if (this._findCommand("*")) {
|
|
1542
|
+
return this._dispatchSubcommand("*", operands, unknown);
|
|
1543
|
+
}
|
|
1544
|
+
if (this.listenerCount("command:*")) {
|
|
1545
|
+
this.emit("command:*", operands, unknown);
|
|
1546
|
+
} else if (this.commands.length) {
|
|
1547
|
+
this.unknownCommand();
|
|
1548
|
+
} else {
|
|
1549
|
+
checkForUnknownOptions();
|
|
1550
|
+
this._processArguments();
|
|
1551
|
+
}
|
|
1552
|
+
} else if (this.commands.length) {
|
|
1553
|
+
checkForUnknownOptions();
|
|
1554
|
+
this.help({ error: true });
|
|
1555
|
+
} else {
|
|
1556
|
+
checkForUnknownOptions();
|
|
1557
|
+
this._processArguments();
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1560
|
+
_findCommand(name) {
|
|
1561
|
+
if (!name)
|
|
1562
|
+
return;
|
|
1563
|
+
return this.commands.find((cmd) => cmd._name === name || cmd._aliases.includes(name));
|
|
1564
|
+
}
|
|
1565
|
+
_findOption(arg) {
|
|
1566
|
+
return this.options.find((option) => option.is(arg));
|
|
1567
|
+
}
|
|
1568
|
+
_checkForMissingMandatoryOptions() {
|
|
1569
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1570
|
+
cmd.options.forEach((anOption) => {
|
|
1571
|
+
if (anOption.mandatory && cmd.getOptionValue(anOption.attributeName()) === undefined) {
|
|
1572
|
+
cmd.missingMandatoryOptionValue(anOption);
|
|
1573
|
+
}
|
|
1574
|
+
});
|
|
1575
|
+
});
|
|
1576
|
+
}
|
|
1577
|
+
_checkForConflictingLocalOptions() {
|
|
1578
|
+
const definedNonDefaultOptions = this.options.filter((option) => {
|
|
1579
|
+
const optionKey = option.attributeName();
|
|
1580
|
+
if (this.getOptionValue(optionKey) === undefined) {
|
|
1581
|
+
return false;
|
|
1582
|
+
}
|
|
1583
|
+
return this.getOptionValueSource(optionKey) !== "default";
|
|
1584
|
+
});
|
|
1585
|
+
const optionsWithConflicting = definedNonDefaultOptions.filter((option) => option.conflictsWith.length > 0);
|
|
1586
|
+
optionsWithConflicting.forEach((option) => {
|
|
1587
|
+
const conflictingAndDefined = definedNonDefaultOptions.find((defined) => option.conflictsWith.includes(defined.attributeName()));
|
|
1588
|
+
if (conflictingAndDefined) {
|
|
1589
|
+
this._conflictingOption(option, conflictingAndDefined);
|
|
1590
|
+
}
|
|
1591
|
+
});
|
|
1592
|
+
}
|
|
1593
|
+
_checkForConflictingOptions() {
|
|
1594
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1595
|
+
cmd._checkForConflictingLocalOptions();
|
|
1596
|
+
});
|
|
1597
|
+
}
|
|
1598
|
+
parseOptions(args) {
|
|
1599
|
+
const operands = [];
|
|
1600
|
+
const unknown = [];
|
|
1601
|
+
let dest = operands;
|
|
1602
|
+
function maybeOption(arg) {
|
|
1603
|
+
return arg.length > 1 && arg[0] === "-";
|
|
1604
|
+
}
|
|
1605
|
+
const negativeNumberArg = (arg) => {
|
|
1606
|
+
if (!/^-(\d+|\d*\.\d+)(e[+-]?\d+)?$/.test(arg))
|
|
1607
|
+
return false;
|
|
1608
|
+
return !this._getCommandAndAncestors().some((cmd) => cmd.options.map((opt) => opt.short).some((short) => /^-\d$/.test(short)));
|
|
1609
|
+
};
|
|
1610
|
+
let activeVariadicOption = null;
|
|
1611
|
+
let activeGroup = null;
|
|
1612
|
+
let i = 0;
|
|
1613
|
+
while (i < args.length || activeGroup) {
|
|
1614
|
+
const arg = activeGroup ?? args[i++];
|
|
1615
|
+
activeGroup = null;
|
|
1616
|
+
if (arg === "--") {
|
|
1617
|
+
if (dest === unknown)
|
|
1618
|
+
dest.push(arg);
|
|
1619
|
+
dest.push(...args.slice(i));
|
|
1620
|
+
break;
|
|
1621
|
+
}
|
|
1622
|
+
if (activeVariadicOption && (!maybeOption(arg) || negativeNumberArg(arg))) {
|
|
1623
|
+
this.emit(`option:${activeVariadicOption.name()}`, arg);
|
|
1624
|
+
continue;
|
|
1625
|
+
}
|
|
1626
|
+
activeVariadicOption = null;
|
|
1627
|
+
if (maybeOption(arg)) {
|
|
1628
|
+
const option = this._findOption(arg);
|
|
1629
|
+
if (option) {
|
|
1630
|
+
if (option.required) {
|
|
1631
|
+
const value = args[i++];
|
|
1632
|
+
if (value === undefined)
|
|
1633
|
+
this.optionMissingArgument(option);
|
|
1634
|
+
this.emit(`option:${option.name()}`, value);
|
|
1635
|
+
} else if (option.optional) {
|
|
1636
|
+
let value = null;
|
|
1637
|
+
if (i < args.length && (!maybeOption(args[i]) || negativeNumberArg(args[i]))) {
|
|
1638
|
+
value = args[i++];
|
|
1639
|
+
}
|
|
1640
|
+
this.emit(`option:${option.name()}`, value);
|
|
1641
|
+
} else {
|
|
1642
|
+
this.emit(`option:${option.name()}`);
|
|
1643
|
+
}
|
|
1644
|
+
activeVariadicOption = option.variadic ? option : null;
|
|
1645
|
+
continue;
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
if (arg.length > 2 && arg[0] === "-" && arg[1] !== "-") {
|
|
1649
|
+
const option = this._findOption(`-${arg[1]}`);
|
|
1650
|
+
if (option) {
|
|
1651
|
+
if (option.required || option.optional && this._combineFlagAndOptionalValue) {
|
|
1652
|
+
this.emit(`option:${option.name()}`, arg.slice(2));
|
|
1653
|
+
} else {
|
|
1654
|
+
this.emit(`option:${option.name()}`);
|
|
1655
|
+
activeGroup = `-${arg.slice(2)}`;
|
|
1656
|
+
}
|
|
1657
|
+
continue;
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
if (/^--[^=]+=/.test(arg)) {
|
|
1661
|
+
const index = arg.indexOf("=");
|
|
1662
|
+
const option = this._findOption(arg.slice(0, index));
|
|
1663
|
+
if (option && (option.required || option.optional)) {
|
|
1664
|
+
this.emit(`option:${option.name()}`, arg.slice(index + 1));
|
|
1665
|
+
continue;
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
if (dest === operands && maybeOption(arg) && !(this.commands.length === 0 && negativeNumberArg(arg))) {
|
|
1669
|
+
dest = unknown;
|
|
1670
|
+
}
|
|
1671
|
+
if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
|
|
1672
|
+
if (this._findCommand(arg)) {
|
|
1673
|
+
operands.push(arg);
|
|
1674
|
+
unknown.push(...args.slice(i));
|
|
1675
|
+
break;
|
|
1676
|
+
} else if (this._getHelpCommand() && arg === this._getHelpCommand().name()) {
|
|
1677
|
+
operands.push(arg, ...args.slice(i));
|
|
1678
|
+
break;
|
|
1679
|
+
} else if (this._defaultCommandName) {
|
|
1680
|
+
unknown.push(arg, ...args.slice(i));
|
|
1681
|
+
break;
|
|
1682
|
+
}
|
|
1683
|
+
}
|
|
1684
|
+
if (this._passThroughOptions) {
|
|
1685
|
+
dest.push(arg, ...args.slice(i));
|
|
1686
|
+
break;
|
|
1687
|
+
}
|
|
1688
|
+
dest.push(arg);
|
|
1689
|
+
}
|
|
1690
|
+
return { operands, unknown };
|
|
1691
|
+
}
|
|
1692
|
+
opts() {
|
|
1693
|
+
if (this._storeOptionsAsProperties) {
|
|
1694
|
+
const result = {};
|
|
1695
|
+
const len = this.options.length;
|
|
1696
|
+
for (let i = 0;i < len; i++) {
|
|
1697
|
+
const key = this.options[i].attributeName();
|
|
1698
|
+
result[key] = key === this._versionOptionName ? this._version : this[key];
|
|
1699
|
+
}
|
|
1700
|
+
return result;
|
|
1701
|
+
}
|
|
1702
|
+
return this._optionValues;
|
|
1703
|
+
}
|
|
1704
|
+
optsWithGlobals() {
|
|
1705
|
+
return this._getCommandAndAncestors().reduce((combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()), {});
|
|
1706
|
+
}
|
|
1707
|
+
error(message, errorOptions) {
|
|
1708
|
+
this._outputConfiguration.outputError(`${message}
|
|
1709
|
+
`, this._outputConfiguration.writeErr);
|
|
1710
|
+
if (typeof this._showHelpAfterError === "string") {
|
|
1711
|
+
this._outputConfiguration.writeErr(`${this._showHelpAfterError}
|
|
1712
|
+
`);
|
|
1713
|
+
} else if (this._showHelpAfterError) {
|
|
1714
|
+
this._outputConfiguration.writeErr(`
|
|
1715
|
+
`);
|
|
1716
|
+
this.outputHelp({ error: true });
|
|
1717
|
+
}
|
|
1718
|
+
const config = errorOptions || {};
|
|
1719
|
+
const exitCode = config.exitCode || 1;
|
|
1720
|
+
const code = config.code || "commander.error";
|
|
1721
|
+
this._exit(exitCode, code, message);
|
|
1722
|
+
}
|
|
1723
|
+
_parseOptionsEnv() {
|
|
1724
|
+
this.options.forEach((option) => {
|
|
1725
|
+
if (option.envVar && option.envVar in process2.env) {
|
|
1726
|
+
const optionKey = option.attributeName();
|
|
1727
|
+
if (this.getOptionValue(optionKey) === undefined || ["default", "config", "env"].includes(this.getOptionValueSource(optionKey))) {
|
|
1728
|
+
if (option.required || option.optional) {
|
|
1729
|
+
this.emit(`optionEnv:${option.name()}`, process2.env[option.envVar]);
|
|
1730
|
+
} else {
|
|
1731
|
+
this.emit(`optionEnv:${option.name()}`);
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
}
|
|
1735
|
+
});
|
|
1736
|
+
}
|
|
1737
|
+
_parseOptionsImplied() {
|
|
1738
|
+
const dualHelper = new DualOptions(this.options);
|
|
1739
|
+
const hasCustomOptionValue = (optionKey) => {
|
|
1740
|
+
return this.getOptionValue(optionKey) !== undefined && !["default", "implied"].includes(this.getOptionValueSource(optionKey));
|
|
1741
|
+
};
|
|
1742
|
+
this.options.filter((option) => option.implied !== undefined && hasCustomOptionValue(option.attributeName()) && dualHelper.valueFromOption(this.getOptionValue(option.attributeName()), option)).forEach((option) => {
|
|
1743
|
+
Object.keys(option.implied).filter((impliedKey) => !hasCustomOptionValue(impliedKey)).forEach((impliedKey) => {
|
|
1744
|
+
this.setOptionValueWithSource(impliedKey, option.implied[impliedKey], "implied");
|
|
1745
|
+
});
|
|
1746
|
+
});
|
|
1747
|
+
}
|
|
1748
|
+
missingArgument(name) {
|
|
1749
|
+
const message = `error: missing required argument '${name}'`;
|
|
1750
|
+
this.error(message, { code: "commander.missingArgument" });
|
|
1751
|
+
}
|
|
1752
|
+
optionMissingArgument(option) {
|
|
1753
|
+
const message = `error: option '${option.flags}' argument missing`;
|
|
1754
|
+
this.error(message, { code: "commander.optionMissingArgument" });
|
|
1755
|
+
}
|
|
1756
|
+
missingMandatoryOptionValue(option) {
|
|
1757
|
+
const message = `error: required option '${option.flags}' not specified`;
|
|
1758
|
+
this.error(message, { code: "commander.missingMandatoryOptionValue" });
|
|
1759
|
+
}
|
|
1760
|
+
_conflictingOption(option, conflictingOption) {
|
|
1761
|
+
const findBestOptionFromValue = (option2) => {
|
|
1762
|
+
const optionKey = option2.attributeName();
|
|
1763
|
+
const optionValue = this.getOptionValue(optionKey);
|
|
1764
|
+
const negativeOption = this.options.find((target) => target.negate && optionKey === target.attributeName());
|
|
1765
|
+
const positiveOption = this.options.find((target) => !target.negate && optionKey === target.attributeName());
|
|
1766
|
+
if (negativeOption && (negativeOption.presetArg === undefined && optionValue === false || negativeOption.presetArg !== undefined && optionValue === negativeOption.presetArg)) {
|
|
1767
|
+
return negativeOption;
|
|
1768
|
+
}
|
|
1769
|
+
return positiveOption || option2;
|
|
1770
|
+
};
|
|
1771
|
+
const getErrorMessage = (option2) => {
|
|
1772
|
+
const bestOption = findBestOptionFromValue(option2);
|
|
1773
|
+
const optionKey = bestOption.attributeName();
|
|
1774
|
+
const source = this.getOptionValueSource(optionKey);
|
|
1775
|
+
if (source === "env") {
|
|
1776
|
+
return `environment variable '${bestOption.envVar}'`;
|
|
1777
|
+
}
|
|
1778
|
+
return `option '${bestOption.flags}'`;
|
|
1779
|
+
};
|
|
1780
|
+
const message = `error: ${getErrorMessage(option)} cannot be used with ${getErrorMessage(conflictingOption)}`;
|
|
1781
|
+
this.error(message, { code: "commander.conflictingOption" });
|
|
1782
|
+
}
|
|
1783
|
+
unknownOption(flag) {
|
|
1784
|
+
if (this._allowUnknownOption)
|
|
1785
|
+
return;
|
|
1786
|
+
let suggestion = "";
|
|
1787
|
+
if (flag.startsWith("--") && this._showSuggestionAfterError) {
|
|
1788
|
+
let candidateFlags = [];
|
|
1789
|
+
let command = this;
|
|
1790
|
+
do {
|
|
1791
|
+
const moreFlags = command.createHelp().visibleOptions(command).filter((option) => option.long).map((option) => option.long);
|
|
1792
|
+
candidateFlags = candidateFlags.concat(moreFlags);
|
|
1793
|
+
command = command.parent;
|
|
1794
|
+
} while (command && !command._enablePositionalOptions);
|
|
1795
|
+
suggestion = suggestSimilar(flag, candidateFlags);
|
|
1796
|
+
}
|
|
1797
|
+
const message = `error: unknown option '${flag}'${suggestion}`;
|
|
1798
|
+
this.error(message, { code: "commander.unknownOption" });
|
|
1799
|
+
}
|
|
1800
|
+
_excessArguments(receivedArgs) {
|
|
1801
|
+
if (this._allowExcessArguments)
|
|
1802
|
+
return;
|
|
1803
|
+
const expected = this.registeredArguments.length;
|
|
1804
|
+
const s = expected === 1 ? "" : "s";
|
|
1805
|
+
const forSubcommand = this.parent ? ` for '${this.name()}'` : "";
|
|
1806
|
+
const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;
|
|
1807
|
+
this.error(message, { code: "commander.excessArguments" });
|
|
1808
|
+
}
|
|
1809
|
+
unknownCommand() {
|
|
1810
|
+
const unknownName = this.args[0];
|
|
1811
|
+
let suggestion = "";
|
|
1812
|
+
if (this._showSuggestionAfterError) {
|
|
1813
|
+
const candidateNames = [];
|
|
1814
|
+
this.createHelp().visibleCommands(this).forEach((command) => {
|
|
1815
|
+
candidateNames.push(command.name());
|
|
1816
|
+
if (command.alias())
|
|
1817
|
+
candidateNames.push(command.alias());
|
|
1818
|
+
});
|
|
1819
|
+
suggestion = suggestSimilar(unknownName, candidateNames);
|
|
1820
|
+
}
|
|
1821
|
+
const message = `error: unknown command '${unknownName}'${suggestion}`;
|
|
1822
|
+
this.error(message, { code: "commander.unknownCommand" });
|
|
1823
|
+
}
|
|
1824
|
+
version(str, flags, description) {
|
|
1825
|
+
if (str === undefined)
|
|
1826
|
+
return this._version;
|
|
1827
|
+
this._version = str;
|
|
1828
|
+
flags = flags || "-V, --version";
|
|
1829
|
+
description = description || "output the version number";
|
|
1830
|
+
const versionOption = this.createOption(flags, description);
|
|
1831
|
+
this._versionOptionName = versionOption.attributeName();
|
|
1832
|
+
this._registerOption(versionOption);
|
|
1833
|
+
this.on("option:" + versionOption.name(), () => {
|
|
1834
|
+
this._outputConfiguration.writeOut(`${str}
|
|
1835
|
+
`);
|
|
1836
|
+
this._exit(0, "commander.version", str);
|
|
1837
|
+
});
|
|
1838
|
+
return this;
|
|
1839
|
+
}
|
|
1840
|
+
description(str, argsDescription) {
|
|
1841
|
+
if (str === undefined && argsDescription === undefined)
|
|
1842
|
+
return this._description;
|
|
1843
|
+
this._description = str;
|
|
1844
|
+
if (argsDescription) {
|
|
1845
|
+
this._argsDescription = argsDescription;
|
|
1846
|
+
}
|
|
1847
|
+
return this;
|
|
1848
|
+
}
|
|
1849
|
+
summary(str) {
|
|
1850
|
+
if (str === undefined)
|
|
1851
|
+
return this._summary;
|
|
1852
|
+
this._summary = str;
|
|
1853
|
+
return this;
|
|
1854
|
+
}
|
|
1855
|
+
alias(alias) {
|
|
1856
|
+
if (alias === undefined)
|
|
1857
|
+
return this._aliases[0];
|
|
1858
|
+
let command = this;
|
|
1859
|
+
if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
|
|
1860
|
+
command = this.commands[this.commands.length - 1];
|
|
1861
|
+
}
|
|
1862
|
+
if (alias === command._name)
|
|
1863
|
+
throw new Error("Command alias can't be the same as its name");
|
|
1864
|
+
const matchingCommand = this.parent?._findCommand(alias);
|
|
1865
|
+
if (matchingCommand) {
|
|
1866
|
+
const existingCmd = [matchingCommand.name()].concat(matchingCommand.aliases()).join("|");
|
|
1867
|
+
throw new Error(`cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`);
|
|
1868
|
+
}
|
|
1869
|
+
command._aliases.push(alias);
|
|
1870
|
+
return this;
|
|
1871
|
+
}
|
|
1872
|
+
aliases(aliases) {
|
|
1873
|
+
if (aliases === undefined)
|
|
1874
|
+
return this._aliases;
|
|
1875
|
+
aliases.forEach((alias) => this.alias(alias));
|
|
1876
|
+
return this;
|
|
1877
|
+
}
|
|
1878
|
+
usage(str) {
|
|
1879
|
+
if (str === undefined) {
|
|
1880
|
+
if (this._usage)
|
|
1881
|
+
return this._usage;
|
|
1882
|
+
const args = this.registeredArguments.map((arg) => {
|
|
1883
|
+
return humanReadableArgName(arg);
|
|
1884
|
+
});
|
|
1885
|
+
return [].concat(this.options.length || this._helpOption !== null ? "[options]" : [], this.commands.length ? "[command]" : [], this.registeredArguments.length ? args : []).join(" ");
|
|
1886
|
+
}
|
|
1887
|
+
this._usage = str;
|
|
1888
|
+
return this;
|
|
1889
|
+
}
|
|
1890
|
+
name(str) {
|
|
1891
|
+
if (str === undefined)
|
|
1892
|
+
return this._name;
|
|
1893
|
+
this._name = str;
|
|
1894
|
+
return this;
|
|
1895
|
+
}
|
|
1896
|
+
helpGroup(heading) {
|
|
1897
|
+
if (heading === undefined)
|
|
1898
|
+
return this._helpGroupHeading ?? "";
|
|
1899
|
+
this._helpGroupHeading = heading;
|
|
1900
|
+
return this;
|
|
1901
|
+
}
|
|
1902
|
+
commandsGroup(heading) {
|
|
1903
|
+
if (heading === undefined)
|
|
1904
|
+
return this._defaultCommandGroup ?? "";
|
|
1905
|
+
this._defaultCommandGroup = heading;
|
|
1906
|
+
return this;
|
|
1907
|
+
}
|
|
1908
|
+
optionsGroup(heading) {
|
|
1909
|
+
if (heading === undefined)
|
|
1910
|
+
return this._defaultOptionGroup ?? "";
|
|
1911
|
+
this._defaultOptionGroup = heading;
|
|
1912
|
+
return this;
|
|
1913
|
+
}
|
|
1914
|
+
_initOptionGroup(option) {
|
|
1915
|
+
if (this._defaultOptionGroup && !option.helpGroupHeading)
|
|
1916
|
+
option.helpGroup(this._defaultOptionGroup);
|
|
1917
|
+
}
|
|
1918
|
+
_initCommandGroup(cmd) {
|
|
1919
|
+
if (this._defaultCommandGroup && !cmd.helpGroup())
|
|
1920
|
+
cmd.helpGroup(this._defaultCommandGroup);
|
|
1921
|
+
}
|
|
1922
|
+
nameFromFilename(filename) {
|
|
1923
|
+
this._name = path.basename(filename, path.extname(filename));
|
|
1924
|
+
return this;
|
|
1925
|
+
}
|
|
1926
|
+
executableDir(path2) {
|
|
1927
|
+
if (path2 === undefined)
|
|
1928
|
+
return this._executableDir;
|
|
1929
|
+
this._executableDir = path2;
|
|
1930
|
+
return this;
|
|
1931
|
+
}
|
|
1932
|
+
helpInformation(contextOptions) {
|
|
1933
|
+
const helper = this.createHelp();
|
|
1934
|
+
const context = this._getOutputContext(contextOptions);
|
|
1935
|
+
helper.prepareContext({
|
|
1936
|
+
error: context.error,
|
|
1937
|
+
helpWidth: context.helpWidth,
|
|
1938
|
+
outputHasColors: context.hasColors
|
|
1939
|
+
});
|
|
1940
|
+
const text = helper.formatHelp(this, helper);
|
|
1941
|
+
if (context.hasColors)
|
|
1942
|
+
return text;
|
|
1943
|
+
return this._outputConfiguration.stripColor(text);
|
|
1944
|
+
}
|
|
1945
|
+
_getOutputContext(contextOptions) {
|
|
1946
|
+
contextOptions = contextOptions || {};
|
|
1947
|
+
const error = !!contextOptions.error;
|
|
1948
|
+
let baseWrite;
|
|
1949
|
+
let hasColors;
|
|
1950
|
+
let helpWidth;
|
|
1951
|
+
if (error) {
|
|
1952
|
+
baseWrite = (str) => this._outputConfiguration.writeErr(str);
|
|
1953
|
+
hasColors = this._outputConfiguration.getErrHasColors();
|
|
1954
|
+
helpWidth = this._outputConfiguration.getErrHelpWidth();
|
|
1955
|
+
} else {
|
|
1956
|
+
baseWrite = (str) => this._outputConfiguration.writeOut(str);
|
|
1957
|
+
hasColors = this._outputConfiguration.getOutHasColors();
|
|
1958
|
+
helpWidth = this._outputConfiguration.getOutHelpWidth();
|
|
1959
|
+
}
|
|
1960
|
+
const write = (str) => {
|
|
1961
|
+
if (!hasColors)
|
|
1962
|
+
str = this._outputConfiguration.stripColor(str);
|
|
1963
|
+
return baseWrite(str);
|
|
1964
|
+
};
|
|
1965
|
+
return { error, write, hasColors, helpWidth };
|
|
1966
|
+
}
|
|
1967
|
+
outputHelp(contextOptions) {
|
|
1968
|
+
let deprecatedCallback;
|
|
1969
|
+
if (typeof contextOptions === "function") {
|
|
1970
|
+
deprecatedCallback = contextOptions;
|
|
1971
|
+
contextOptions = undefined;
|
|
1972
|
+
}
|
|
1973
|
+
const outputContext = this._getOutputContext(contextOptions);
|
|
1974
|
+
const eventContext = {
|
|
1975
|
+
error: outputContext.error,
|
|
1976
|
+
write: outputContext.write,
|
|
1977
|
+
command: this
|
|
1978
|
+
};
|
|
1979
|
+
this._getCommandAndAncestors().reverse().forEach((command) => command.emit("beforeAllHelp", eventContext));
|
|
1980
|
+
this.emit("beforeHelp", eventContext);
|
|
1981
|
+
let helpInformation = this.helpInformation({ error: outputContext.error });
|
|
1982
|
+
if (deprecatedCallback) {
|
|
1983
|
+
helpInformation = deprecatedCallback(helpInformation);
|
|
1984
|
+
if (typeof helpInformation !== "string" && !Buffer.isBuffer(helpInformation)) {
|
|
1985
|
+
throw new Error("outputHelp callback must return a string or a Buffer");
|
|
1986
|
+
}
|
|
1987
|
+
}
|
|
1988
|
+
outputContext.write(helpInformation);
|
|
1989
|
+
if (this._getHelpOption()?.long) {
|
|
1990
|
+
this.emit(this._getHelpOption().long);
|
|
1991
|
+
}
|
|
1992
|
+
this.emit("afterHelp", eventContext);
|
|
1993
|
+
this._getCommandAndAncestors().forEach((command) => command.emit("afterAllHelp", eventContext));
|
|
1994
|
+
}
|
|
1995
|
+
helpOption(flags, description) {
|
|
1996
|
+
if (typeof flags === "boolean") {
|
|
1997
|
+
if (flags) {
|
|
1998
|
+
if (this._helpOption === null)
|
|
1999
|
+
this._helpOption = undefined;
|
|
2000
|
+
if (this._defaultOptionGroup) {
|
|
2001
|
+
this._initOptionGroup(this._getHelpOption());
|
|
2002
|
+
}
|
|
2003
|
+
} else {
|
|
2004
|
+
this._helpOption = null;
|
|
2005
|
+
}
|
|
2006
|
+
return this;
|
|
2007
|
+
}
|
|
2008
|
+
this._helpOption = this.createOption(flags ?? "-h, --help", description ?? "display help for command");
|
|
2009
|
+
if (flags || description)
|
|
2010
|
+
this._initOptionGroup(this._helpOption);
|
|
2011
|
+
return this;
|
|
2012
|
+
}
|
|
2013
|
+
_getHelpOption() {
|
|
2014
|
+
if (this._helpOption === undefined) {
|
|
2015
|
+
this.helpOption(undefined, undefined);
|
|
2016
|
+
}
|
|
2017
|
+
return this._helpOption;
|
|
2018
|
+
}
|
|
2019
|
+
addHelpOption(option) {
|
|
2020
|
+
this._helpOption = option;
|
|
2021
|
+
this._initOptionGroup(option);
|
|
2022
|
+
return this;
|
|
2023
|
+
}
|
|
2024
|
+
help(contextOptions) {
|
|
2025
|
+
this.outputHelp(contextOptions);
|
|
2026
|
+
let exitCode = Number(process2.exitCode ?? 0);
|
|
2027
|
+
if (exitCode === 0 && contextOptions && typeof contextOptions !== "function" && contextOptions.error) {
|
|
2028
|
+
exitCode = 1;
|
|
2029
|
+
}
|
|
2030
|
+
this._exit(exitCode, "commander.help", "(outputHelp)");
|
|
2031
|
+
}
|
|
2032
|
+
addHelpText(position, text) {
|
|
2033
|
+
const allowedValues = ["beforeAll", "before", "after", "afterAll"];
|
|
2034
|
+
if (!allowedValues.includes(position)) {
|
|
2035
|
+
throw new Error(`Unexpected value for position to addHelpText.
|
|
2036
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
2037
|
+
}
|
|
2038
|
+
const helpEvent = `${position}Help`;
|
|
2039
|
+
this.on(helpEvent, (context) => {
|
|
2040
|
+
let helpStr;
|
|
2041
|
+
if (typeof text === "function") {
|
|
2042
|
+
helpStr = text({ error: context.error, command: context.command });
|
|
2043
|
+
} else {
|
|
2044
|
+
helpStr = text;
|
|
2045
|
+
}
|
|
2046
|
+
if (helpStr) {
|
|
2047
|
+
context.write(`${helpStr}
|
|
2048
|
+
`);
|
|
2049
|
+
}
|
|
2050
|
+
});
|
|
2051
|
+
return this;
|
|
2052
|
+
}
|
|
2053
|
+
_outputHelpIfRequested(args) {
|
|
2054
|
+
const helpOption = this._getHelpOption();
|
|
2055
|
+
const helpRequested = helpOption && args.find((arg) => helpOption.is(arg));
|
|
2056
|
+
if (helpRequested) {
|
|
2057
|
+
this.outputHelp();
|
|
2058
|
+
this._exit(0, "commander.helpDisplayed", "(outputHelp)");
|
|
2059
|
+
}
|
|
2060
|
+
}
|
|
2061
|
+
}
|
|
2062
|
+
function incrementNodeInspectorPort(args) {
|
|
2063
|
+
return args.map((arg) => {
|
|
2064
|
+
if (!arg.startsWith("--inspect")) {
|
|
2065
|
+
return arg;
|
|
2066
|
+
}
|
|
2067
|
+
let debugOption;
|
|
2068
|
+
let debugHost = "127.0.0.1";
|
|
2069
|
+
let debugPort = "9229";
|
|
2070
|
+
let match;
|
|
2071
|
+
if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
|
|
2072
|
+
debugOption = match[1];
|
|
2073
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
|
|
2074
|
+
debugOption = match[1];
|
|
2075
|
+
if (/^\d+$/.test(match[3])) {
|
|
2076
|
+
debugPort = match[3];
|
|
2077
|
+
} else {
|
|
2078
|
+
debugHost = match[3];
|
|
2079
|
+
}
|
|
2080
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) {
|
|
2081
|
+
debugOption = match[1];
|
|
2082
|
+
debugHost = match[3];
|
|
2083
|
+
debugPort = match[4];
|
|
2084
|
+
}
|
|
2085
|
+
if (debugOption && debugPort !== "0") {
|
|
2086
|
+
return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
|
|
2087
|
+
}
|
|
2088
|
+
return arg;
|
|
2089
|
+
});
|
|
2090
|
+
}
|
|
2091
|
+
function useColor() {
|
|
2092
|
+
if (process2.env.NO_COLOR || process2.env.FORCE_COLOR === "0" || process2.env.FORCE_COLOR === "false")
|
|
2093
|
+
return false;
|
|
2094
|
+
if (process2.env.FORCE_COLOR || process2.env.CLICOLOR_FORCE !== undefined)
|
|
2095
|
+
return true;
|
|
2096
|
+
return;
|
|
2097
|
+
}
|
|
2098
|
+
exports.Command = Command;
|
|
2099
|
+
exports.useColor = useColor;
|
|
2100
|
+
});
|
|
2101
|
+
|
|
2102
|
+
// node_modules/commander/index.js
|
|
2103
|
+
var require_commander = __commonJS((exports) => {
|
|
2104
|
+
var { Argument } = require_argument();
|
|
2105
|
+
var { Command } = require_command();
|
|
2106
|
+
var { CommanderError, InvalidArgumentError } = require_error();
|
|
2107
|
+
var { Help } = require_help();
|
|
2108
|
+
var { Option } = require_option();
|
|
2109
|
+
exports.program = new Command;
|
|
2110
|
+
exports.createCommand = (name) => new Command(name);
|
|
2111
|
+
exports.createOption = (flags, description) => new Option(flags, description);
|
|
2112
|
+
exports.createArgument = (name, description) => new Argument(name, description);
|
|
2113
|
+
exports.Command = Command;
|
|
2114
|
+
exports.Option = Option;
|
|
2115
|
+
exports.Argument = Argument;
|
|
2116
|
+
exports.Help = Help;
|
|
2117
|
+
exports.CommanderError = CommanderError;
|
|
2118
|
+
exports.InvalidArgumentError = InvalidArgumentError;
|
|
2119
|
+
exports.InvalidOptionArgumentError = InvalidArgumentError;
|
|
2120
|
+
});
|
|
2121
|
+
|
|
2122
|
+
// src/sync/reconciler.ts
|
|
2123
|
+
var exports_reconciler = {};
|
|
2124
|
+
__export(exports_reconciler, {
|
|
2125
|
+
reconcile: () => reconcile,
|
|
2126
|
+
findForkPoint: () => findForkPoint
|
|
2127
|
+
});
|
|
2128
|
+
function findForkPoint(opsA, opsB) {
|
|
2129
|
+
const hashesB = new Set(opsB.map((o) => o.hash));
|
|
2130
|
+
let forkPoint = null;
|
|
2131
|
+
for (const op of opsA) {
|
|
2132
|
+
if (hashesB.has(op.hash)) {
|
|
2133
|
+
forkPoint = op.hash;
|
|
2134
|
+
}
|
|
2135
|
+
}
|
|
2136
|
+
return forkPoint;
|
|
2137
|
+
}
|
|
2138
|
+
function reconcile(opsA, opsB) {
|
|
2139
|
+
const forkPoint = findForkPoint(opsA, opsB);
|
|
2140
|
+
const hashesA = new Set(opsA.map((o) => o.hash));
|
|
2141
|
+
const hashesB = new Set(opsB.map((o) => o.hash));
|
|
2142
|
+
const shared = [];
|
|
2143
|
+
const uniqueToA = [];
|
|
2144
|
+
const uniqueToB = [];
|
|
2145
|
+
for (const op of opsA) {
|
|
2146
|
+
if (hashesB.has(op.hash)) {
|
|
2147
|
+
shared.push(op);
|
|
2148
|
+
} else {
|
|
2149
|
+
uniqueToA.push(op);
|
|
2150
|
+
}
|
|
2151
|
+
}
|
|
2152
|
+
for (const op of opsB) {
|
|
2153
|
+
if (!hashesA.has(op.hash)) {
|
|
2154
|
+
uniqueToB.push(op);
|
|
2155
|
+
}
|
|
2156
|
+
}
|
|
2157
|
+
if (uniqueToA.length === 0) {
|
|
2158
|
+
return {
|
|
2159
|
+
merged: [...shared, ...uniqueToB],
|
|
2160
|
+
uniqueToA: [],
|
|
2161
|
+
uniqueToB,
|
|
2162
|
+
forkPoint,
|
|
2163
|
+
clean: true,
|
|
2164
|
+
conflicts: []
|
|
2165
|
+
};
|
|
2166
|
+
}
|
|
2167
|
+
if (uniqueToB.length === 0) {
|
|
2168
|
+
return {
|
|
2169
|
+
merged: [...shared, ...uniqueToA],
|
|
2170
|
+
uniqueToA,
|
|
2171
|
+
uniqueToB: [],
|
|
2172
|
+
forkPoint,
|
|
2173
|
+
clean: true,
|
|
2174
|
+
conflicts: []
|
|
2175
|
+
};
|
|
2176
|
+
}
|
|
2177
|
+
const conflicts = detectConflicts(uniqueToA, uniqueToB);
|
|
2178
|
+
const interleaved = interleaveByTimestamp(uniqueToA, uniqueToB);
|
|
2179
|
+
return {
|
|
2180
|
+
merged: [...shared, ...interleaved],
|
|
2181
|
+
uniqueToA,
|
|
2182
|
+
uniqueToB,
|
|
2183
|
+
forkPoint,
|
|
2184
|
+
clean: conflicts.length === 0,
|
|
2185
|
+
conflicts
|
|
2186
|
+
};
|
|
2187
|
+
}
|
|
2188
|
+
function detectConflicts(uniqueA, uniqueB) {
|
|
2189
|
+
const conflicts = [];
|
|
2190
|
+
const aMutations = new Map;
|
|
2191
|
+
for (const op of uniqueA) {
|
|
2192
|
+
if (!FILE_MUTATION_KINDS.has(op.kind) || !op.vcs?.filePath)
|
|
2193
|
+
continue;
|
|
2194
|
+
const path = op.vcs.filePath;
|
|
2195
|
+
if (!aMutations.has(path))
|
|
2196
|
+
aMutations.set(path, []);
|
|
2197
|
+
aMutations.get(path).push(op);
|
|
2198
|
+
}
|
|
2199
|
+
for (const op of uniqueB) {
|
|
2200
|
+
if (!FILE_MUTATION_KINDS.has(op.kind) || !op.vcs?.filePath)
|
|
2201
|
+
continue;
|
|
2202
|
+
const path = op.vcs.filePath;
|
|
2203
|
+
const aOps = aMutations.get(path);
|
|
2204
|
+
if (!aOps)
|
|
2205
|
+
continue;
|
|
2206
|
+
for (const aOp of aOps) {
|
|
2207
|
+
if (aOp.kind === "vcs:fileModify" && op.kind === "vcs:fileModify") {
|
|
2208
|
+
conflicts.push({
|
|
2209
|
+
opA: aOp,
|
|
2210
|
+
opB: op,
|
|
2211
|
+
filePath: path,
|
|
2212
|
+
reason: `Both sides modified ${path}`
|
|
2213
|
+
});
|
|
2214
|
+
} else if (aOp.kind === "vcs:fileDelete" && op.kind === "vcs:fileModify" || aOp.kind === "vcs:fileModify" && op.kind === "vcs:fileDelete") {
|
|
2215
|
+
conflicts.push({
|
|
2216
|
+
opA: aOp,
|
|
2217
|
+
opB: op,
|
|
2218
|
+
filePath: path,
|
|
2219
|
+
reason: `Delete/modify conflict on ${path}`
|
|
2220
|
+
});
|
|
2221
|
+
} else if (aOp.kind === "vcs:fileAdd" && op.kind === "vcs:fileAdd") {
|
|
2222
|
+
if (aOp.vcs?.contentHash !== op.vcs?.contentHash) {
|
|
2223
|
+
conflicts.push({
|
|
2224
|
+
opA: aOp,
|
|
2225
|
+
opB: op,
|
|
2226
|
+
filePath: path,
|
|
2227
|
+
reason: `Both sides added ${path} with different content`
|
|
2228
|
+
});
|
|
2229
|
+
}
|
|
2230
|
+
}
|
|
2231
|
+
}
|
|
2232
|
+
}
|
|
2233
|
+
return conflicts;
|
|
2234
|
+
}
|
|
2235
|
+
function interleaveByTimestamp(a, b) {
|
|
2236
|
+
const result = [];
|
|
2237
|
+
let ai = 0;
|
|
2238
|
+
let bi = 0;
|
|
2239
|
+
while (ai < a.length && bi < b.length) {
|
|
2240
|
+
const tA = new Date(a[ai].timestamp).getTime();
|
|
2241
|
+
const tB = new Date(b[bi].timestamp).getTime();
|
|
2242
|
+
if (tA <= tB) {
|
|
2243
|
+
result.push(a[ai++]);
|
|
2244
|
+
} else {
|
|
2245
|
+
result.push(b[bi++]);
|
|
2246
|
+
}
|
|
2247
|
+
}
|
|
2248
|
+
while (ai < a.length)
|
|
2249
|
+
result.push(a[ai++]);
|
|
2250
|
+
while (bi < b.length)
|
|
2251
|
+
result.push(b[bi++]);
|
|
2252
|
+
return result;
|
|
2253
|
+
}
|
|
2254
|
+
var FILE_MUTATION_KINDS;
|
|
2255
|
+
var init_reconciler = __esm(() => {
|
|
2256
|
+
FILE_MUTATION_KINDS = new Set([
|
|
2257
|
+
"vcs:fileAdd",
|
|
2258
|
+
"vcs:fileModify",
|
|
2259
|
+
"vcs:fileDelete",
|
|
2260
|
+
"vcs:fileRename"
|
|
2261
|
+
]);
|
|
2262
|
+
});
|
|
2263
|
+
|
|
2264
|
+
// node_modules/commander/esm.mjs
|
|
2265
|
+
var import__ = __toESM(require_commander(), 1);
|
|
2266
|
+
var {
|
|
2267
|
+
program,
|
|
2268
|
+
createCommand,
|
|
2269
|
+
createArgument,
|
|
2270
|
+
createOption,
|
|
2271
|
+
CommanderError,
|
|
2272
|
+
InvalidArgumentError,
|
|
2273
|
+
InvalidOptionArgumentError,
|
|
2274
|
+
Command,
|
|
2275
|
+
Argument,
|
|
2276
|
+
Option,
|
|
2277
|
+
Help
|
|
2278
|
+
} = import__.default;
|
|
2279
|
+
|
|
2280
|
+
// node_modules/chalk/source/vendor/ansi-styles/index.js
|
|
2281
|
+
var ANSI_BACKGROUND_OFFSET = 10;
|
|
2282
|
+
var wrapAnsi16 = (offset = 0) => (code) => `\x1B[${code + offset}m`;
|
|
2283
|
+
var wrapAnsi256 = (offset = 0) => (code) => `\x1B[${38 + offset};5;${code}m`;
|
|
2284
|
+
var wrapAnsi16m = (offset = 0) => (red, green, blue) => `\x1B[${38 + offset};2;${red};${green};${blue}m`;
|
|
2285
|
+
var styles = {
|
|
2286
|
+
modifier: {
|
|
2287
|
+
reset: [0, 0],
|
|
2288
|
+
bold: [1, 22],
|
|
2289
|
+
dim: [2, 22],
|
|
2290
|
+
italic: [3, 23],
|
|
2291
|
+
underline: [4, 24],
|
|
2292
|
+
overline: [53, 55],
|
|
2293
|
+
inverse: [7, 27],
|
|
2294
|
+
hidden: [8, 28],
|
|
2295
|
+
strikethrough: [9, 29]
|
|
2296
|
+
},
|
|
2297
|
+
color: {
|
|
2298
|
+
black: [30, 39],
|
|
2299
|
+
red: [31, 39],
|
|
2300
|
+
green: [32, 39],
|
|
2301
|
+
yellow: [33, 39],
|
|
2302
|
+
blue: [34, 39],
|
|
2303
|
+
magenta: [35, 39],
|
|
2304
|
+
cyan: [36, 39],
|
|
2305
|
+
white: [37, 39],
|
|
2306
|
+
blackBright: [90, 39],
|
|
2307
|
+
gray: [90, 39],
|
|
2308
|
+
grey: [90, 39],
|
|
2309
|
+
redBright: [91, 39],
|
|
2310
|
+
greenBright: [92, 39],
|
|
2311
|
+
yellowBright: [93, 39],
|
|
2312
|
+
blueBright: [94, 39],
|
|
2313
|
+
magentaBright: [95, 39],
|
|
2314
|
+
cyanBright: [96, 39],
|
|
2315
|
+
whiteBright: [97, 39]
|
|
2316
|
+
},
|
|
2317
|
+
bgColor: {
|
|
2318
|
+
bgBlack: [40, 49],
|
|
2319
|
+
bgRed: [41, 49],
|
|
2320
|
+
bgGreen: [42, 49],
|
|
2321
|
+
bgYellow: [43, 49],
|
|
2322
|
+
bgBlue: [44, 49],
|
|
2323
|
+
bgMagenta: [45, 49],
|
|
2324
|
+
bgCyan: [46, 49],
|
|
2325
|
+
bgWhite: [47, 49],
|
|
2326
|
+
bgBlackBright: [100, 49],
|
|
2327
|
+
bgGray: [100, 49],
|
|
2328
|
+
bgGrey: [100, 49],
|
|
2329
|
+
bgRedBright: [101, 49],
|
|
2330
|
+
bgGreenBright: [102, 49],
|
|
2331
|
+
bgYellowBright: [103, 49],
|
|
2332
|
+
bgBlueBright: [104, 49],
|
|
2333
|
+
bgMagentaBright: [105, 49],
|
|
2334
|
+
bgCyanBright: [106, 49],
|
|
2335
|
+
bgWhiteBright: [107, 49]
|
|
2336
|
+
}
|
|
2337
|
+
};
|
|
2338
|
+
var modifierNames = Object.keys(styles.modifier);
|
|
2339
|
+
var foregroundColorNames = Object.keys(styles.color);
|
|
2340
|
+
var backgroundColorNames = Object.keys(styles.bgColor);
|
|
2341
|
+
var colorNames = [...foregroundColorNames, ...backgroundColorNames];
|
|
2342
|
+
function assembleStyles() {
|
|
2343
|
+
const codes = new Map;
|
|
2344
|
+
for (const [groupName, group] of Object.entries(styles)) {
|
|
2345
|
+
for (const [styleName, style] of Object.entries(group)) {
|
|
2346
|
+
styles[styleName] = {
|
|
2347
|
+
open: `\x1B[${style[0]}m`,
|
|
2348
|
+
close: `\x1B[${style[1]}m`
|
|
2349
|
+
};
|
|
2350
|
+
group[styleName] = styles[styleName];
|
|
2351
|
+
codes.set(style[0], style[1]);
|
|
2352
|
+
}
|
|
2353
|
+
Object.defineProperty(styles, groupName, {
|
|
2354
|
+
value: group,
|
|
2355
|
+
enumerable: false
|
|
2356
|
+
});
|
|
2357
|
+
}
|
|
2358
|
+
Object.defineProperty(styles, "codes", {
|
|
2359
|
+
value: codes,
|
|
2360
|
+
enumerable: false
|
|
2361
|
+
});
|
|
2362
|
+
styles.color.close = "\x1B[39m";
|
|
2363
|
+
styles.bgColor.close = "\x1B[49m";
|
|
2364
|
+
styles.color.ansi = wrapAnsi16();
|
|
2365
|
+
styles.color.ansi256 = wrapAnsi256();
|
|
2366
|
+
styles.color.ansi16m = wrapAnsi16m();
|
|
2367
|
+
styles.bgColor.ansi = wrapAnsi16(ANSI_BACKGROUND_OFFSET);
|
|
2368
|
+
styles.bgColor.ansi256 = wrapAnsi256(ANSI_BACKGROUND_OFFSET);
|
|
2369
|
+
styles.bgColor.ansi16m = wrapAnsi16m(ANSI_BACKGROUND_OFFSET);
|
|
2370
|
+
Object.defineProperties(styles, {
|
|
2371
|
+
rgbToAnsi256: {
|
|
2372
|
+
value(red, green, blue) {
|
|
2373
|
+
if (red === green && green === blue) {
|
|
2374
|
+
if (red < 8) {
|
|
2375
|
+
return 16;
|
|
2376
|
+
}
|
|
2377
|
+
if (red > 248) {
|
|
2378
|
+
return 231;
|
|
2379
|
+
}
|
|
2380
|
+
return Math.round((red - 8) / 247 * 24) + 232;
|
|
2381
|
+
}
|
|
2382
|
+
return 16 + 36 * Math.round(red / 255 * 5) + 6 * Math.round(green / 255 * 5) + Math.round(blue / 255 * 5);
|
|
2383
|
+
},
|
|
2384
|
+
enumerable: false
|
|
2385
|
+
},
|
|
2386
|
+
hexToRgb: {
|
|
2387
|
+
value(hex) {
|
|
2388
|
+
const matches = /[a-f\d]{6}|[a-f\d]{3}/i.exec(hex.toString(16));
|
|
2389
|
+
if (!matches) {
|
|
2390
|
+
return [0, 0, 0];
|
|
2391
|
+
}
|
|
2392
|
+
let [colorString] = matches;
|
|
2393
|
+
if (colorString.length === 3) {
|
|
2394
|
+
colorString = [...colorString].map((character) => character + character).join("");
|
|
2395
|
+
}
|
|
2396
|
+
const integer = Number.parseInt(colorString, 16);
|
|
2397
|
+
return [
|
|
2398
|
+
integer >> 16 & 255,
|
|
2399
|
+
integer >> 8 & 255,
|
|
2400
|
+
integer & 255
|
|
2401
|
+
];
|
|
2402
|
+
},
|
|
2403
|
+
enumerable: false
|
|
2404
|
+
},
|
|
2405
|
+
hexToAnsi256: {
|
|
2406
|
+
value: (hex) => styles.rgbToAnsi256(...styles.hexToRgb(hex)),
|
|
2407
|
+
enumerable: false
|
|
2408
|
+
},
|
|
2409
|
+
ansi256ToAnsi: {
|
|
2410
|
+
value(code) {
|
|
2411
|
+
if (code < 8) {
|
|
2412
|
+
return 30 + code;
|
|
2413
|
+
}
|
|
2414
|
+
if (code < 16) {
|
|
2415
|
+
return 90 + (code - 8);
|
|
2416
|
+
}
|
|
2417
|
+
let red;
|
|
2418
|
+
let green;
|
|
2419
|
+
let blue;
|
|
2420
|
+
if (code >= 232) {
|
|
2421
|
+
red = ((code - 232) * 10 + 8) / 255;
|
|
2422
|
+
green = red;
|
|
2423
|
+
blue = red;
|
|
2424
|
+
} else {
|
|
2425
|
+
code -= 16;
|
|
2426
|
+
const remainder = code % 36;
|
|
2427
|
+
red = Math.floor(code / 36) / 5;
|
|
2428
|
+
green = Math.floor(remainder / 6) / 5;
|
|
2429
|
+
blue = remainder % 6 / 5;
|
|
2430
|
+
}
|
|
2431
|
+
const value = Math.max(red, green, blue) * 2;
|
|
2432
|
+
if (value === 0) {
|
|
2433
|
+
return 30;
|
|
2434
|
+
}
|
|
2435
|
+
let result = 30 + (Math.round(blue) << 2 | Math.round(green) << 1 | Math.round(red));
|
|
2436
|
+
if (value === 2) {
|
|
2437
|
+
result += 60;
|
|
2438
|
+
}
|
|
2439
|
+
return result;
|
|
2440
|
+
},
|
|
2441
|
+
enumerable: false
|
|
2442
|
+
},
|
|
2443
|
+
rgbToAnsi: {
|
|
2444
|
+
value: (red, green, blue) => styles.ansi256ToAnsi(styles.rgbToAnsi256(red, green, blue)),
|
|
2445
|
+
enumerable: false
|
|
2446
|
+
},
|
|
2447
|
+
hexToAnsi: {
|
|
2448
|
+
value: (hex) => styles.ansi256ToAnsi(styles.hexToAnsi256(hex)),
|
|
2449
|
+
enumerable: false
|
|
2450
|
+
}
|
|
2451
|
+
});
|
|
2452
|
+
return styles;
|
|
2453
|
+
}
|
|
2454
|
+
var ansiStyles = assembleStyles();
|
|
2455
|
+
var ansi_styles_default = ansiStyles;
|
|
2456
|
+
|
|
2457
|
+
// node_modules/chalk/source/vendor/supports-color/index.js
|
|
2458
|
+
import process2 from "process";
|
|
2459
|
+
import os from "os";
|
|
2460
|
+
import tty from "tty";
|
|
2461
|
+
function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : process2.argv) {
|
|
2462
|
+
const prefix = flag.startsWith("-") ? "" : flag.length === 1 ? "-" : "--";
|
|
2463
|
+
const position = argv.indexOf(prefix + flag);
|
|
2464
|
+
const terminatorPosition = argv.indexOf("--");
|
|
2465
|
+
return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
|
|
2466
|
+
}
|
|
2467
|
+
var { env } = process2;
|
|
2468
|
+
var flagForceColor;
|
|
2469
|
+
if (hasFlag("no-color") || hasFlag("no-colors") || hasFlag("color=false") || hasFlag("color=never")) {
|
|
2470
|
+
flagForceColor = 0;
|
|
2471
|
+
} else if (hasFlag("color") || hasFlag("colors") || hasFlag("color=true") || hasFlag("color=always")) {
|
|
2472
|
+
flagForceColor = 1;
|
|
2473
|
+
}
|
|
2474
|
+
function envForceColor() {
|
|
2475
|
+
if ("FORCE_COLOR" in env) {
|
|
2476
|
+
if (env.FORCE_COLOR === "true") {
|
|
2477
|
+
return 1;
|
|
2478
|
+
}
|
|
2479
|
+
if (env.FORCE_COLOR === "false") {
|
|
2480
|
+
return 0;
|
|
2481
|
+
}
|
|
2482
|
+
return env.FORCE_COLOR.length === 0 ? 1 : Math.min(Number.parseInt(env.FORCE_COLOR, 10), 3);
|
|
2483
|
+
}
|
|
2484
|
+
}
|
|
2485
|
+
function translateLevel(level) {
|
|
2486
|
+
if (level === 0) {
|
|
2487
|
+
return false;
|
|
2488
|
+
}
|
|
2489
|
+
return {
|
|
2490
|
+
level,
|
|
2491
|
+
hasBasic: true,
|
|
2492
|
+
has256: level >= 2,
|
|
2493
|
+
has16m: level >= 3
|
|
2494
|
+
};
|
|
2495
|
+
}
|
|
2496
|
+
function _supportsColor(haveStream, { streamIsTTY, sniffFlags = true } = {}) {
|
|
2497
|
+
const noFlagForceColor = envForceColor();
|
|
2498
|
+
if (noFlagForceColor !== undefined) {
|
|
2499
|
+
flagForceColor = noFlagForceColor;
|
|
2500
|
+
}
|
|
2501
|
+
const forceColor = sniffFlags ? flagForceColor : noFlagForceColor;
|
|
2502
|
+
if (forceColor === 0) {
|
|
2503
|
+
return 0;
|
|
2504
|
+
}
|
|
2505
|
+
if (sniffFlags) {
|
|
2506
|
+
if (hasFlag("color=16m") || hasFlag("color=full") || hasFlag("color=truecolor")) {
|
|
2507
|
+
return 3;
|
|
2508
|
+
}
|
|
2509
|
+
if (hasFlag("color=256")) {
|
|
2510
|
+
return 2;
|
|
2511
|
+
}
|
|
2512
|
+
}
|
|
2513
|
+
if ("TF_BUILD" in env && "AGENT_NAME" in env) {
|
|
2514
|
+
return 1;
|
|
2515
|
+
}
|
|
2516
|
+
if (haveStream && !streamIsTTY && forceColor === undefined) {
|
|
2517
|
+
return 0;
|
|
2518
|
+
}
|
|
2519
|
+
const min = forceColor || 0;
|
|
2520
|
+
if (env.TERM === "dumb") {
|
|
2521
|
+
return min;
|
|
2522
|
+
}
|
|
2523
|
+
if (process2.platform === "win32") {
|
|
2524
|
+
const osRelease = os.release().split(".");
|
|
2525
|
+
if (Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) {
|
|
2526
|
+
return Number(osRelease[2]) >= 14931 ? 3 : 2;
|
|
2527
|
+
}
|
|
2528
|
+
return 1;
|
|
2529
|
+
}
|
|
2530
|
+
if ("CI" in env) {
|
|
2531
|
+
if (["GITHUB_ACTIONS", "GITEA_ACTIONS", "CIRCLECI"].some((key) => (key in env))) {
|
|
2532
|
+
return 3;
|
|
2533
|
+
}
|
|
2534
|
+
if (["TRAVIS", "APPVEYOR", "GITLAB_CI", "BUILDKITE", "DRONE"].some((sign) => (sign in env)) || env.CI_NAME === "codeship") {
|
|
2535
|
+
return 1;
|
|
2536
|
+
}
|
|
2537
|
+
return min;
|
|
2538
|
+
}
|
|
2539
|
+
if ("TEAMCITY_VERSION" in env) {
|
|
2540
|
+
return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
|
|
2541
|
+
}
|
|
2542
|
+
if (env.COLORTERM === "truecolor") {
|
|
2543
|
+
return 3;
|
|
2544
|
+
}
|
|
2545
|
+
if (env.TERM === "xterm-kitty") {
|
|
2546
|
+
return 3;
|
|
2547
|
+
}
|
|
2548
|
+
if (env.TERM === "xterm-ghostty") {
|
|
2549
|
+
return 3;
|
|
2550
|
+
}
|
|
2551
|
+
if (env.TERM === "wezterm") {
|
|
2552
|
+
return 3;
|
|
2553
|
+
}
|
|
2554
|
+
if ("TERM_PROGRAM" in env) {
|
|
2555
|
+
const version = Number.parseInt((env.TERM_PROGRAM_VERSION || "").split(".")[0], 10);
|
|
2556
|
+
switch (env.TERM_PROGRAM) {
|
|
2557
|
+
case "iTerm.app": {
|
|
2558
|
+
return version >= 3 ? 3 : 2;
|
|
2559
|
+
}
|
|
2560
|
+
case "Apple_Terminal": {
|
|
2561
|
+
return 2;
|
|
2562
|
+
}
|
|
2563
|
+
}
|
|
2564
|
+
}
|
|
2565
|
+
if (/-256(color)?$/i.test(env.TERM)) {
|
|
2566
|
+
return 2;
|
|
2567
|
+
}
|
|
2568
|
+
if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
|
|
2569
|
+
return 1;
|
|
2570
|
+
}
|
|
2571
|
+
if ("COLORTERM" in env) {
|
|
2572
|
+
return 1;
|
|
2573
|
+
}
|
|
2574
|
+
return min;
|
|
2575
|
+
}
|
|
2576
|
+
function createSupportsColor(stream, options = {}) {
|
|
2577
|
+
const level = _supportsColor(stream, {
|
|
2578
|
+
streamIsTTY: stream && stream.isTTY,
|
|
2579
|
+
...options
|
|
2580
|
+
});
|
|
2581
|
+
return translateLevel(level);
|
|
2582
|
+
}
|
|
2583
|
+
var supportsColor = {
|
|
2584
|
+
stdout: createSupportsColor({ isTTY: tty.isatty(1) }),
|
|
2585
|
+
stderr: createSupportsColor({ isTTY: tty.isatty(2) })
|
|
2586
|
+
};
|
|
2587
|
+
var supports_color_default = supportsColor;
|
|
2588
|
+
|
|
2589
|
+
// node_modules/chalk/source/utilities.js
|
|
2590
|
+
function stringReplaceAll(string, substring, replacer) {
|
|
2591
|
+
let index = string.indexOf(substring);
|
|
2592
|
+
if (index === -1) {
|
|
2593
|
+
return string;
|
|
2594
|
+
}
|
|
2595
|
+
const substringLength = substring.length;
|
|
2596
|
+
let endIndex = 0;
|
|
2597
|
+
let returnValue = "";
|
|
2598
|
+
do {
|
|
2599
|
+
returnValue += string.slice(endIndex, index) + substring + replacer;
|
|
2600
|
+
endIndex = index + substringLength;
|
|
2601
|
+
index = string.indexOf(substring, endIndex);
|
|
2602
|
+
} while (index !== -1);
|
|
2603
|
+
returnValue += string.slice(endIndex);
|
|
2604
|
+
return returnValue;
|
|
2605
|
+
}
|
|
2606
|
+
function stringEncaseCRLFWithFirstIndex(string, prefix, postfix, index) {
|
|
2607
|
+
let endIndex = 0;
|
|
2608
|
+
let returnValue = "";
|
|
2609
|
+
do {
|
|
2610
|
+
const gotCR = string[index - 1] === "\r";
|
|
2611
|
+
returnValue += string.slice(endIndex, gotCR ? index - 1 : index) + prefix + (gotCR ? `\r
|
|
2612
|
+
` : `
|
|
2613
|
+
`) + postfix;
|
|
2614
|
+
endIndex = index + 1;
|
|
2615
|
+
index = string.indexOf(`
|
|
2616
|
+
`, endIndex);
|
|
2617
|
+
} while (index !== -1);
|
|
2618
|
+
returnValue += string.slice(endIndex);
|
|
2619
|
+
return returnValue;
|
|
2620
|
+
}
|
|
2621
|
+
|
|
2622
|
+
// node_modules/chalk/source/index.js
|
|
2623
|
+
var { stdout: stdoutColor, stderr: stderrColor } = supports_color_default;
|
|
2624
|
+
var GENERATOR = Symbol("GENERATOR");
|
|
2625
|
+
var STYLER = Symbol("STYLER");
|
|
2626
|
+
var IS_EMPTY = Symbol("IS_EMPTY");
|
|
2627
|
+
var levelMapping = [
|
|
2628
|
+
"ansi",
|
|
2629
|
+
"ansi",
|
|
2630
|
+
"ansi256",
|
|
2631
|
+
"ansi16m"
|
|
2632
|
+
];
|
|
2633
|
+
var styles2 = Object.create(null);
|
|
2634
|
+
var applyOptions = (object, options = {}) => {
|
|
2635
|
+
if (options.level && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) {
|
|
2636
|
+
throw new Error("The `level` option should be an integer from 0 to 3");
|
|
2637
|
+
}
|
|
2638
|
+
const colorLevel = stdoutColor ? stdoutColor.level : 0;
|
|
2639
|
+
object.level = options.level === undefined ? colorLevel : options.level;
|
|
2640
|
+
};
|
|
2641
|
+
var chalkFactory = (options) => {
|
|
2642
|
+
const chalk = (...strings) => strings.join(" ");
|
|
2643
|
+
applyOptions(chalk, options);
|
|
2644
|
+
Object.setPrototypeOf(chalk, createChalk.prototype);
|
|
2645
|
+
return chalk;
|
|
2646
|
+
};
|
|
2647
|
+
function createChalk(options) {
|
|
2648
|
+
return chalkFactory(options);
|
|
2649
|
+
}
|
|
2650
|
+
Object.setPrototypeOf(createChalk.prototype, Function.prototype);
|
|
2651
|
+
for (const [styleName, style] of Object.entries(ansi_styles_default)) {
|
|
2652
|
+
styles2[styleName] = {
|
|
2653
|
+
get() {
|
|
2654
|
+
const builder = createBuilder(this, createStyler(style.open, style.close, this[STYLER]), this[IS_EMPTY]);
|
|
2655
|
+
Object.defineProperty(this, styleName, { value: builder });
|
|
2656
|
+
return builder;
|
|
2657
|
+
}
|
|
2658
|
+
};
|
|
2659
|
+
}
|
|
2660
|
+
styles2.visible = {
|
|
2661
|
+
get() {
|
|
2662
|
+
const builder = createBuilder(this, this[STYLER], true);
|
|
2663
|
+
Object.defineProperty(this, "visible", { value: builder });
|
|
2664
|
+
return builder;
|
|
2665
|
+
}
|
|
2666
|
+
};
|
|
2667
|
+
var getModelAnsi = (model, level, type, ...arguments_) => {
|
|
2668
|
+
if (model === "rgb") {
|
|
2669
|
+
if (level === "ansi16m") {
|
|
2670
|
+
return ansi_styles_default[type].ansi16m(...arguments_);
|
|
2671
|
+
}
|
|
2672
|
+
if (level === "ansi256") {
|
|
2673
|
+
return ansi_styles_default[type].ansi256(ansi_styles_default.rgbToAnsi256(...arguments_));
|
|
2674
|
+
}
|
|
2675
|
+
return ansi_styles_default[type].ansi(ansi_styles_default.rgbToAnsi(...arguments_));
|
|
2676
|
+
}
|
|
2677
|
+
if (model === "hex") {
|
|
2678
|
+
return getModelAnsi("rgb", level, type, ...ansi_styles_default.hexToRgb(...arguments_));
|
|
2679
|
+
}
|
|
2680
|
+
return ansi_styles_default[type][model](...arguments_);
|
|
2681
|
+
};
|
|
2682
|
+
var usedModels = ["rgb", "hex", "ansi256"];
|
|
2683
|
+
for (const model of usedModels) {
|
|
2684
|
+
styles2[model] = {
|
|
2685
|
+
get() {
|
|
2686
|
+
const { level } = this;
|
|
2687
|
+
return function(...arguments_) {
|
|
2688
|
+
const styler = createStyler(getModelAnsi(model, levelMapping[level], "color", ...arguments_), ansi_styles_default.color.close, this[STYLER]);
|
|
2689
|
+
return createBuilder(this, styler, this[IS_EMPTY]);
|
|
2690
|
+
};
|
|
2691
|
+
}
|
|
2692
|
+
};
|
|
2693
|
+
const bgModel = "bg" + model[0].toUpperCase() + model.slice(1);
|
|
2694
|
+
styles2[bgModel] = {
|
|
2695
|
+
get() {
|
|
2696
|
+
const { level } = this;
|
|
2697
|
+
return function(...arguments_) {
|
|
2698
|
+
const styler = createStyler(getModelAnsi(model, levelMapping[level], "bgColor", ...arguments_), ansi_styles_default.bgColor.close, this[STYLER]);
|
|
2699
|
+
return createBuilder(this, styler, this[IS_EMPTY]);
|
|
2700
|
+
};
|
|
2701
|
+
}
|
|
2702
|
+
};
|
|
2703
|
+
}
|
|
2704
|
+
var proto = Object.defineProperties(() => {}, {
|
|
2705
|
+
...styles2,
|
|
2706
|
+
level: {
|
|
2707
|
+
enumerable: true,
|
|
2708
|
+
get() {
|
|
2709
|
+
return this[GENERATOR].level;
|
|
2710
|
+
},
|
|
2711
|
+
set(level) {
|
|
2712
|
+
this[GENERATOR].level = level;
|
|
2713
|
+
}
|
|
2714
|
+
}
|
|
2715
|
+
});
|
|
2716
|
+
var createStyler = (open, close, parent) => {
|
|
2717
|
+
let openAll;
|
|
2718
|
+
let closeAll;
|
|
2719
|
+
if (parent === undefined) {
|
|
2720
|
+
openAll = open;
|
|
2721
|
+
closeAll = close;
|
|
2722
|
+
} else {
|
|
2723
|
+
openAll = parent.openAll + open;
|
|
2724
|
+
closeAll = close + parent.closeAll;
|
|
2725
|
+
}
|
|
2726
|
+
return {
|
|
2727
|
+
open,
|
|
2728
|
+
close,
|
|
2729
|
+
openAll,
|
|
2730
|
+
closeAll,
|
|
2731
|
+
parent
|
|
2732
|
+
};
|
|
2733
|
+
};
|
|
2734
|
+
var createBuilder = (self, _styler, _isEmpty) => {
|
|
2735
|
+
const builder = (...arguments_) => applyStyle(builder, arguments_.length === 1 ? "" + arguments_[0] : arguments_.join(" "));
|
|
2736
|
+
Object.setPrototypeOf(builder, proto);
|
|
2737
|
+
builder[GENERATOR] = self;
|
|
2738
|
+
builder[STYLER] = _styler;
|
|
2739
|
+
builder[IS_EMPTY] = _isEmpty;
|
|
2740
|
+
return builder;
|
|
2741
|
+
};
|
|
2742
|
+
var applyStyle = (self, string) => {
|
|
2743
|
+
if (self.level <= 0 || !string) {
|
|
2744
|
+
return self[IS_EMPTY] ? "" : string;
|
|
2745
|
+
}
|
|
2746
|
+
let styler = self[STYLER];
|
|
2747
|
+
if (styler === undefined) {
|
|
2748
|
+
return string;
|
|
2749
|
+
}
|
|
2750
|
+
const { openAll, closeAll } = styler;
|
|
2751
|
+
if (string.includes("\x1B")) {
|
|
2752
|
+
while (styler !== undefined) {
|
|
2753
|
+
string = stringReplaceAll(string, styler.close, styler.open);
|
|
2754
|
+
styler = styler.parent;
|
|
2755
|
+
}
|
|
2756
|
+
}
|
|
2757
|
+
const lfIndex = string.indexOf(`
|
|
2758
|
+
`);
|
|
2759
|
+
if (lfIndex !== -1) {
|
|
2760
|
+
string = stringEncaseCRLFWithFirstIndex(string, closeAll, openAll, lfIndex);
|
|
2761
|
+
}
|
|
2762
|
+
return openAll + string + closeAll;
|
|
2763
|
+
};
|
|
2764
|
+
Object.defineProperties(createChalk.prototype, styles2);
|
|
2765
|
+
var chalk = createChalk();
|
|
2766
|
+
var chalkStderr = createChalk({ level: stderrColor ? stderrColor.level : 0 });
|
|
2767
|
+
var source_default = chalk;
|
|
2768
|
+
|
|
2769
|
+
// src/cli/index.ts
|
|
2770
|
+
import { resolve, join as join5 } from "path";
|
|
2771
|
+
|
|
2772
|
+
// src/git/git-reader.ts
|
|
2773
|
+
import { execSync } from "child_process";
|
|
2774
|
+
import { existsSync } from "fs";
|
|
2775
|
+
import { join } from "path";
|
|
2776
|
+
|
|
2777
|
+
class GitReader {
|
|
2778
|
+
repoPath;
|
|
2779
|
+
constructor(repoPath) {
|
|
2780
|
+
this.repoPath = repoPath;
|
|
2781
|
+
}
|
|
2782
|
+
isGitRepo() {
|
|
2783
|
+
return existsSync(join(this.repoPath, ".git"));
|
|
2784
|
+
}
|
|
2785
|
+
readCommits() {
|
|
2786
|
+
const SEP = "\u2016";
|
|
2787
|
+
const format = `%H${SEP}%an${SEP}%ae${SEP}%aI${SEP}%P${SEP}%s`;
|
|
2788
|
+
const raw = this.git(`log --all --reverse --format="${format}"`);
|
|
2789
|
+
if (!raw.trim()) {
|
|
2790
|
+
return [];
|
|
2791
|
+
}
|
|
2792
|
+
return raw.trim().split(`
|
|
2793
|
+
`).map((line) => {
|
|
2794
|
+
const parts = line.split(SEP);
|
|
2795
|
+
return {
|
|
2796
|
+
hash: parts[0],
|
|
2797
|
+
authorName: parts[1],
|
|
2798
|
+
authorEmail: parts[2],
|
|
2799
|
+
timestamp: parts[3],
|
|
2800
|
+
parentHashes: parts[4] ? parts[4].split(" ").filter(Boolean) : [],
|
|
2801
|
+
message: parts[5] ?? ""
|
|
2802
|
+
};
|
|
2803
|
+
});
|
|
2804
|
+
}
|
|
2805
|
+
readChanges(commitHash, parentHash) {
|
|
2806
|
+
let raw;
|
|
2807
|
+
if (parentHash) {
|
|
2808
|
+
raw = this.git(`diff-tree -r --name-status --no-commit-id -M ${parentHash} ${commitHash}`);
|
|
2809
|
+
} else {
|
|
2810
|
+
raw = this.git(`diff-tree -r --root --name-status --no-commit-id -M ${commitHash}`);
|
|
2811
|
+
}
|
|
2812
|
+
if (!raw.trim()) {
|
|
2813
|
+
return [];
|
|
2814
|
+
}
|
|
2815
|
+
return raw.trim().split(`
|
|
2816
|
+
`).map((line) => {
|
|
2817
|
+
const parts = line.split("\t");
|
|
2818
|
+
const statusCode = parts[0].charAt(0);
|
|
2819
|
+
if (statusCode === "R") {
|
|
2820
|
+
return { status: "R", oldPath: parts[1], path: parts[2] };
|
|
2821
|
+
}
|
|
2822
|
+
return { status: statusCode, path: parts[1] };
|
|
2823
|
+
});
|
|
2824
|
+
}
|
|
2825
|
+
readFileContent(commitHash, filePath) {
|
|
2826
|
+
try {
|
|
2827
|
+
return Buffer.from(this.gitBuffer(`show ${commitHash}:${filePath}`));
|
|
2828
|
+
} catch {
|
|
2829
|
+
return null;
|
|
2830
|
+
}
|
|
2831
|
+
}
|
|
2832
|
+
readFullHistory() {
|
|
2833
|
+
const commits = this.readCommits();
|
|
2834
|
+
return commits.map((commit) => {
|
|
2835
|
+
const parentHash = commit.parentHashes[0];
|
|
2836
|
+
const changes = this.readChanges(commit.hash, parentHash);
|
|
2837
|
+
return { ...commit, changes };
|
|
2838
|
+
});
|
|
2839
|
+
}
|
|
2840
|
+
commitCount() {
|
|
2841
|
+
const raw = this.git("rev-list --all --count");
|
|
2842
|
+
return parseInt(raw.trim(), 10) || 0;
|
|
2843
|
+
}
|
|
2844
|
+
currentBranch() {
|
|
2845
|
+
try {
|
|
2846
|
+
return this.git("rev-parse --abbrev-ref HEAD").trim();
|
|
2847
|
+
} catch {
|
|
2848
|
+
return "main";
|
|
2849
|
+
}
|
|
2850
|
+
}
|
|
2851
|
+
branches() {
|
|
2852
|
+
const raw = this.git('branch --format="%(refname:short)"');
|
|
2853
|
+
return raw.trim().split(`
|
|
2854
|
+
`).filter(Boolean);
|
|
2855
|
+
}
|
|
2856
|
+
git(args) {
|
|
2857
|
+
return execSync(`git -C "${this.repoPath}" ${args}`, {
|
|
2858
|
+
encoding: "utf-8",
|
|
2859
|
+
maxBuffer: 100 * 1024 * 1024
|
|
2860
|
+
});
|
|
2861
|
+
}
|
|
2862
|
+
gitBuffer(args) {
|
|
2863
|
+
return execSync(`git -C "${this.repoPath}" ${args}`, {
|
|
2864
|
+
maxBuffer: 100 * 1024 * 1024
|
|
2865
|
+
});
|
|
2866
|
+
}
|
|
2867
|
+
}
|
|
2868
|
+
|
|
2869
|
+
// src/git/git-importer.ts
|
|
2870
|
+
import { existsSync as existsSync2, mkdirSync } from "fs";
|
|
2871
|
+
import { join as join2 } from "path";
|
|
2872
|
+
async function importFromGit(opts) {
|
|
2873
|
+
const startTime = Date.now();
|
|
2874
|
+
const gitReader = new GitReader(opts.from);
|
|
2875
|
+
if (!gitReader.isGitRepo()) {
|
|
2876
|
+
throw new Error(`Not a Git repository: ${opts.from}`);
|
|
2877
|
+
}
|
|
2878
|
+
opts.onProgress?.({
|
|
2879
|
+
phase: "reading",
|
|
2880
|
+
current: 0,
|
|
2881
|
+
total: 0,
|
|
2882
|
+
message: "Reading Git history\u2026"
|
|
2883
|
+
});
|
|
2884
|
+
const history = gitReader.readFullHistory();
|
|
2885
|
+
const branches = gitReader.branches();
|
|
2886
|
+
const defaultBranch = gitReader.currentBranch();
|
|
2887
|
+
opts.onProgress?.({
|
|
2888
|
+
phase: "reading",
|
|
2889
|
+
current: history.length,
|
|
2890
|
+
total: history.length,
|
|
2891
|
+
message: `Read ${history.length} commits`
|
|
2892
|
+
});
|
|
2893
|
+
const engine = new TrellisVcsEngine({
|
|
2894
|
+
rootPath: opts.to,
|
|
2895
|
+
agentId: opts.agentId ?? `git-import:${opts.from}`,
|
|
2896
|
+
defaultBranch
|
|
2897
|
+
});
|
|
2898
|
+
const trellisDir = join2(opts.to, ".trellis");
|
|
2899
|
+
if (!existsSync2(trellisDir)) {
|
|
2900
|
+
mkdirSync(trellisDir, { recursive: true });
|
|
2901
|
+
}
|
|
2902
|
+
const importEngine = new ImportEngine(engine, opts);
|
|
2903
|
+
await importEngine.createBranch(defaultBranch);
|
|
2904
|
+
let opsCreated = 1;
|
|
2905
|
+
const trackedFiles = new Set;
|
|
2906
|
+
for (let i = 0;i < history.length; i++) {
|
|
2907
|
+
const commit = history[i];
|
|
2908
|
+
opts.onProgress?.({
|
|
2909
|
+
phase: "importing",
|
|
2910
|
+
current: i + 1,
|
|
2911
|
+
total: history.length,
|
|
2912
|
+
message: `Importing commit ${i + 1}/${history.length}: ${commit.message.slice(0, 60)}`
|
|
2913
|
+
});
|
|
2914
|
+
for (const change of commit.changes) {
|
|
2915
|
+
const op = await importEngine.convertChange(change, commit);
|
|
2916
|
+
opsCreated++;
|
|
2917
|
+
if (change.status === "A" || change.status === "M" || change.status === "R") {
|
|
2918
|
+
trackedFiles.add(change.path);
|
|
2919
|
+
}
|
|
2920
|
+
if (change.status === "D") {
|
|
2921
|
+
trackedFiles.delete(change.path);
|
|
2922
|
+
}
|
|
2923
|
+
if (change.status === "R" && change.oldPath) {
|
|
2924
|
+
trackedFiles.delete(change.oldPath);
|
|
2925
|
+
}
|
|
2926
|
+
}
|
|
2927
|
+
await importEngine.createMilestone(commit);
|
|
2928
|
+
opsCreated++;
|
|
2929
|
+
}
|
|
2930
|
+
opts.onProgress?.({
|
|
2931
|
+
phase: "done",
|
|
2932
|
+
current: history.length,
|
|
2933
|
+
total: history.length,
|
|
2934
|
+
message: `Imported ${history.length} commits \u2192 ${opsCreated} ops`
|
|
2935
|
+
});
|
|
2936
|
+
return {
|
|
2937
|
+
commitsImported: history.length,
|
|
2938
|
+
opsCreated,
|
|
2939
|
+
filesTracked: trackedFiles.size,
|
|
2940
|
+
branches,
|
|
2941
|
+
duration: Date.now() - startTime
|
|
2942
|
+
};
|
|
2943
|
+
}
|
|
2944
|
+
|
|
2945
|
+
class ImportEngine {
|
|
2946
|
+
engine;
|
|
2947
|
+
opts;
|
|
2948
|
+
lastOpHash;
|
|
2949
|
+
ops = [];
|
|
2950
|
+
constructor(engine, opts) {
|
|
2951
|
+
this.engine = engine;
|
|
2952
|
+
this.opts = opts;
|
|
2953
|
+
}
|
|
2954
|
+
async createBranch(name) {
|
|
2955
|
+
const op = await createVcsOp("vcs:branchCreate", {
|
|
2956
|
+
agentId: this.agentId(),
|
|
2957
|
+
previousHash: this.lastOpHash,
|
|
2958
|
+
vcs: { branchName: name }
|
|
2959
|
+
});
|
|
2960
|
+
this.append(op);
|
|
2961
|
+
}
|
|
2962
|
+
async convertChange(change, commit) {
|
|
2963
|
+
const agentId = `identity:${commit.authorEmail}`;
|
|
2964
|
+
let kind;
|
|
2965
|
+
switch (change.status) {
|
|
2966
|
+
case "A":
|
|
2967
|
+
kind = "vcs:fileAdd";
|
|
2968
|
+
break;
|
|
2969
|
+
case "M":
|
|
2970
|
+
kind = "vcs:fileModify";
|
|
2971
|
+
break;
|
|
2972
|
+
case "D":
|
|
2973
|
+
kind = "vcs:fileDelete";
|
|
2974
|
+
break;
|
|
2975
|
+
case "R":
|
|
2976
|
+
kind = "vcs:fileRename";
|
|
2977
|
+
break;
|
|
2978
|
+
}
|
|
2979
|
+
let contentHash;
|
|
2980
|
+
if (change.status !== "D") {
|
|
2981
|
+
contentHash = await this.hashFileAtCommit(commit.hash, change.path);
|
|
2982
|
+
}
|
|
2983
|
+
let oldContentHash;
|
|
2984
|
+
if (change.status === "M" && commit.parentHashes[0]) {
|
|
2985
|
+
oldContentHash = await this.hashFileAtCommit(commit.parentHashes[0], change.path);
|
|
2986
|
+
}
|
|
2987
|
+
const op = await createVcsOp(kind, {
|
|
2988
|
+
agentId,
|
|
2989
|
+
previousHash: this.lastOpHash,
|
|
2990
|
+
vcs: {
|
|
2991
|
+
filePath: change.path,
|
|
2992
|
+
oldFilePath: change.oldPath,
|
|
2993
|
+
contentHash,
|
|
2994
|
+
oldContentHash
|
|
2995
|
+
}
|
|
2996
|
+
});
|
|
2997
|
+
op.timestamp = commit.timestamp;
|
|
2998
|
+
this.append(op);
|
|
2999
|
+
return op;
|
|
3000
|
+
}
|
|
3001
|
+
async createMilestone(commit) {
|
|
3002
|
+
const agentId = `identity:${commit.authorEmail}`;
|
|
3003
|
+
const milestoneId = `milestone:git:${commit.hash.slice(0, 12)}`;
|
|
3004
|
+
const op = await createVcsOp("vcs:milestoneCreate", {
|
|
3005
|
+
agentId,
|
|
3006
|
+
previousHash: this.lastOpHash,
|
|
3007
|
+
vcs: {
|
|
3008
|
+
milestoneId,
|
|
3009
|
+
message: commit.message
|
|
3010
|
+
}
|
|
3011
|
+
});
|
|
3012
|
+
op.timestamp = commit.timestamp;
|
|
3013
|
+
this.append(op);
|
|
3014
|
+
}
|
|
3015
|
+
append(op) {
|
|
3016
|
+
this.lastOpHash = op.hash;
|
|
3017
|
+
this.ops.push(op);
|
|
3018
|
+
this.flushOps();
|
|
3019
|
+
}
|
|
3020
|
+
flushOps() {
|
|
3021
|
+
const opsPath = join2(this.opts.to, ".trellis", "ops.json");
|
|
3022
|
+
const configPath = join2(this.opts.to, ".trellis", "config.json");
|
|
3023
|
+
const trellisDir = join2(this.opts.to, ".trellis");
|
|
3024
|
+
if (!existsSync2(trellisDir)) {
|
|
3025
|
+
mkdirSync(trellisDir, { recursive: true });
|
|
3026
|
+
}
|
|
3027
|
+
if (!existsSync2(configPath)) {
|
|
3028
|
+
const config = {
|
|
3029
|
+
rootPath: this.opts.to,
|
|
3030
|
+
ignorePatterns: [
|
|
3031
|
+
"node_modules",
|
|
3032
|
+
".git",
|
|
3033
|
+
".trellis",
|
|
3034
|
+
"dist",
|
|
3035
|
+
"build",
|
|
3036
|
+
".DS_Store",
|
|
3037
|
+
"*.log"
|
|
3038
|
+
],
|
|
3039
|
+
debounceMs: 300,
|
|
3040
|
+
defaultBranch: "main",
|
|
3041
|
+
agentId: this.agentId(),
|
|
3042
|
+
createdAt: new Date().toISOString()
|
|
3043
|
+
};
|
|
3044
|
+
const { writeFileSync: writeFileSync2 } = __require("fs");
|
|
3045
|
+
writeFileSync2(configPath, JSON.stringify(config, null, 2));
|
|
3046
|
+
}
|
|
3047
|
+
const { writeFileSync } = __require("fs");
|
|
3048
|
+
writeFileSync(opsPath, JSON.stringify(this.ops, null, 2));
|
|
3049
|
+
}
|
|
3050
|
+
async hashFileAtCommit(commitHash, filePath) {
|
|
3051
|
+
try {
|
|
3052
|
+
const reader = new GitReader(this.opts.from);
|
|
3053
|
+
const content = reader.readFileContent(commitHash, filePath);
|
|
3054
|
+
if (!content) {
|
|
3055
|
+
return;
|
|
3056
|
+
}
|
|
3057
|
+
const hashBuffer = await crypto.subtle.digest("SHA-256", content);
|
|
3058
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
3059
|
+
return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
3060
|
+
} catch {
|
|
3061
|
+
return;
|
|
3062
|
+
}
|
|
3063
|
+
}
|
|
3064
|
+
agentId() {
|
|
3065
|
+
return this.opts.agentId ?? `git-import:${this.opts.from}`;
|
|
3066
|
+
}
|
|
3067
|
+
}
|
|
3068
|
+
|
|
3069
|
+
// src/git/git-exporter.ts
|
|
3070
|
+
import { execSync as execSync2 } from "child_process";
|
|
3071
|
+
import { existsSync as existsSync3, mkdirSync as mkdirSync2, writeFileSync, unlinkSync } from "fs";
|
|
3072
|
+
import { join as join3, dirname as dirname2 } from "path";
|
|
3073
|
+
async function exportToGit(opts) {
|
|
3074
|
+
const startTime = Date.now();
|
|
3075
|
+
const engine = new TrellisVcsEngine({ rootPath: opts.from });
|
|
3076
|
+
engine.open();
|
|
3077
|
+
const blobStore = engine.getBlobStore();
|
|
3078
|
+
if (!blobStore) {
|
|
3079
|
+
throw new Error("Blob store not available. Re-open the repo first.");
|
|
3080
|
+
}
|
|
3081
|
+
const milestones = engine.listMilestones();
|
|
3082
|
+
const allOps = engine.getOps();
|
|
3083
|
+
opts.onProgress?.({
|
|
3084
|
+
phase: "preparing",
|
|
3085
|
+
current: 0,
|
|
3086
|
+
total: milestones.length,
|
|
3087
|
+
message: `Found ${milestones.length} milestones to export`
|
|
3088
|
+
});
|
|
3089
|
+
if (milestones.length === 0) {
|
|
3090
|
+
return {
|
|
3091
|
+
milestonesExported: 0,
|
|
3092
|
+
commitsCreated: 0,
|
|
3093
|
+
duration: Date.now() - startTime
|
|
3094
|
+
};
|
|
3095
|
+
}
|
|
3096
|
+
if (!existsSync3(opts.to)) {
|
|
3097
|
+
mkdirSync2(opts.to, { recursive: true });
|
|
3098
|
+
}
|
|
3099
|
+
const isGitRepo = existsSync3(join3(opts.to, ".git"));
|
|
3100
|
+
if (!isGitRepo) {
|
|
3101
|
+
git(opts.to, "init");
|
|
3102
|
+
git(opts.to, `config user.email "${opts.authorEmail ?? "export@trellis.dev"}"`);
|
|
3103
|
+
git(opts.to, `config user.name "${opts.authorName ?? "TrellisVCS Export"}"`);
|
|
3104
|
+
}
|
|
3105
|
+
let commitsCreated = 0;
|
|
3106
|
+
const milestoneMap = new Map(milestones.map((m) => [m.id, m]));
|
|
3107
|
+
const fileStates = new Map;
|
|
3108
|
+
let pendingChanges = false;
|
|
3109
|
+
let milestoneIdx = 0;
|
|
3110
|
+
for (const op of allOps) {
|
|
3111
|
+
if (op.vcs?.filePath) {
|
|
3112
|
+
switch (op.kind) {
|
|
3113
|
+
case "vcs:fileAdd":
|
|
3114
|
+
case "vcs:fileModify":
|
|
3115
|
+
fileStates.set(op.vcs.filePath, { contentHash: op.vcs.contentHash });
|
|
3116
|
+
pendingChanges = true;
|
|
3117
|
+
break;
|
|
3118
|
+
case "vcs:fileDelete":
|
|
3119
|
+
fileStates.set(op.vcs.filePath, { deleted: true });
|
|
3120
|
+
pendingChanges = true;
|
|
3121
|
+
break;
|
|
3122
|
+
case "vcs:fileRename":
|
|
3123
|
+
if (op.vcs.oldFilePath) {
|
|
3124
|
+
fileStates.set(op.vcs.oldFilePath, { deleted: true });
|
|
3125
|
+
}
|
|
3126
|
+
fileStates.set(op.vcs.filePath, { contentHash: op.vcs.contentHash });
|
|
3127
|
+
pendingChanges = true;
|
|
3128
|
+
break;
|
|
3129
|
+
}
|
|
3130
|
+
}
|
|
3131
|
+
if (op.kind !== "vcs:milestoneCreate")
|
|
3132
|
+
continue;
|
|
3133
|
+
const milestoneId = op.vcs?.milestoneId;
|
|
3134
|
+
const milestone = milestoneId ? milestoneMap.get(milestoneId) : undefined;
|
|
3135
|
+
milestoneIdx++;
|
|
3136
|
+
opts.onProgress?.({
|
|
3137
|
+
phase: "exporting",
|
|
3138
|
+
current: milestoneIdx,
|
|
3139
|
+
total: milestones.length,
|
|
3140
|
+
message: `Exporting milestone ${milestoneIdx}/${milestones.length}: ${(op.vcs?.message ?? "").slice(0, 60)}`
|
|
3141
|
+
});
|
|
3142
|
+
for (const [filePath, state] of fileStates.entries()) {
|
|
3143
|
+
const absPath = join3(opts.to, filePath);
|
|
3144
|
+
if (state.deleted) {
|
|
3145
|
+
if (existsSync3(absPath)) {
|
|
3146
|
+
unlinkSync(absPath);
|
|
3147
|
+
}
|
|
3148
|
+
} else if (state.contentHash && blobStore) {
|
|
3149
|
+
const content = blobStore.get(state.contentHash);
|
|
3150
|
+
if (content) {
|
|
3151
|
+
const dir = dirname2(absPath);
|
|
3152
|
+
if (!existsSync3(dir)) {
|
|
3153
|
+
mkdirSync2(dir, { recursive: true });
|
|
3154
|
+
}
|
|
3155
|
+
writeFileSync(absPath, content);
|
|
3156
|
+
}
|
|
3157
|
+
}
|
|
3158
|
+
}
|
|
3159
|
+
git(opts.to, "add -A");
|
|
3160
|
+
const status = git(opts.to, "status --porcelain");
|
|
3161
|
+
if (status.trim().length === 0 && commitsCreated > 0) {
|
|
3162
|
+
continue;
|
|
3163
|
+
}
|
|
3164
|
+
const authorName = opts.authorName ?? extractAuthorName(milestone?.createdBy ?? op.agentId);
|
|
3165
|
+
const authorEmail = opts.authorEmail ?? extractAuthorEmail(milestone?.createdBy ?? op.agentId);
|
|
3166
|
+
const message = op.vcs?.message ?? milestone?.message ?? `Milestone ${milestoneId}`;
|
|
3167
|
+
const date = milestone?.createdAt ?? op.timestamp ?? new Date().toISOString();
|
|
3168
|
+
try {
|
|
3169
|
+
gitWithEnv(opts.to, `commit --allow-empty --author="${authorName} <${authorEmail}>" -m "${escapeMessage(message)}"`, {
|
|
3170
|
+
GIT_AUTHOR_DATE: date,
|
|
3171
|
+
GIT_COMMITTER_DATE: date
|
|
3172
|
+
});
|
|
3173
|
+
commitsCreated++;
|
|
3174
|
+
pendingChanges = false;
|
|
3175
|
+
} catch {}
|
|
3176
|
+
}
|
|
3177
|
+
opts.onProgress?.({
|
|
3178
|
+
phase: "done",
|
|
3179
|
+
current: milestones.length,
|
|
3180
|
+
total: milestones.length,
|
|
3181
|
+
message: `Exported ${commitsCreated} commits from ${milestones.length} milestones`
|
|
3182
|
+
});
|
|
3183
|
+
return {
|
|
3184
|
+
milestonesExported: milestones.length,
|
|
3185
|
+
commitsCreated,
|
|
3186
|
+
duration: Date.now() - startTime
|
|
3187
|
+
};
|
|
3188
|
+
}
|
|
3189
|
+
function git(repoPath, command) {
|
|
3190
|
+
try {
|
|
3191
|
+
return execSync2(`git -C "${repoPath}" ${command}`, {
|
|
3192
|
+
encoding: "utf-8",
|
|
3193
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
3194
|
+
});
|
|
3195
|
+
} catch (err) {
|
|
3196
|
+
if (err.stdout)
|
|
3197
|
+
return err.stdout;
|
|
3198
|
+
throw err;
|
|
3199
|
+
}
|
|
3200
|
+
}
|
|
3201
|
+
function gitWithEnv(repoPath, command, extraEnv) {
|
|
3202
|
+
return execSync2(`git -C "${repoPath}" ${command}`, {
|
|
3203
|
+
encoding: "utf-8",
|
|
3204
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
3205
|
+
env: { ...process.env, ...extraEnv }
|
|
3206
|
+
});
|
|
3207
|
+
}
|
|
3208
|
+
function escapeMessage(msg) {
|
|
3209
|
+
return msg.replace(/"/g, "\\\"").replace(/\n/g, "\\n");
|
|
3210
|
+
}
|
|
3211
|
+
function extractAuthorName(createdBy) {
|
|
3212
|
+
if (!createdBy)
|
|
3213
|
+
return "TrellisVCS Export";
|
|
3214
|
+
const id = createdBy.replace("identity:", "");
|
|
3215
|
+
if (id.includes("@")) {
|
|
3216
|
+
return id.split("@")[0];
|
|
3217
|
+
}
|
|
3218
|
+
return id;
|
|
3219
|
+
}
|
|
3220
|
+
function extractAuthorEmail(createdBy) {
|
|
3221
|
+
if (!createdBy)
|
|
3222
|
+
return "export@trellis.dev";
|
|
3223
|
+
const id = createdBy.replace("identity:", "");
|
|
3224
|
+
if (id.includes("@")) {
|
|
3225
|
+
return id;
|
|
3226
|
+
}
|
|
3227
|
+
return `${id}@trellis.dev`;
|
|
3228
|
+
}
|
|
3229
|
+
|
|
3230
|
+
// src/identity/identity.ts
|
|
3231
|
+
import {
|
|
3232
|
+
generateKeyPairSync,
|
|
3233
|
+
sign,
|
|
3234
|
+
verify,
|
|
3235
|
+
createPublicKey
|
|
3236
|
+
} from "crypto";
|
|
3237
|
+
import { existsSync as existsSync4, readFileSync as readFileSync2, writeFileSync as writeFileSync2, mkdirSync as mkdirSync3 } from "fs";
|
|
3238
|
+
import { join as join4, dirname as dirname3 } from "path";
|
|
3239
|
+
function createIdentity(opts) {
|
|
3240
|
+
const { publicKey, privateKey } = generateKeyPairSync("ed25519");
|
|
3241
|
+
const pubDer = publicKey.export({ type: "spki", format: "der" });
|
|
3242
|
+
const privDer = privateKey.export({ type: "pkcs8", format: "der" });
|
|
3243
|
+
const rawPub = pubDer.subarray(pubDer.length - 32);
|
|
3244
|
+
const did = deriveDid(rawPub);
|
|
3245
|
+
const entityId = `identity:${did}`;
|
|
3246
|
+
return {
|
|
3247
|
+
displayName: opts.displayName,
|
|
3248
|
+
email: opts.email,
|
|
3249
|
+
publicKey: pubDer.toString("base64"),
|
|
3250
|
+
privateKey: privDer.toString("base64"),
|
|
3251
|
+
did,
|
|
3252
|
+
entityId,
|
|
3253
|
+
createdAt: new Date().toISOString()
|
|
3254
|
+
};
|
|
3255
|
+
}
|
|
3256
|
+
function deriveDid(rawPublicKey) {
|
|
3257
|
+
const multicodec = Buffer.concat([
|
|
3258
|
+
Buffer.from([237, 1]),
|
|
3259
|
+
Buffer.from(rawPublicKey)
|
|
3260
|
+
]);
|
|
3261
|
+
const encoded = base58btcEncode(multicodec);
|
|
3262
|
+
return `did:key:z${encoded}`;
|
|
3263
|
+
}
|
|
3264
|
+
var IDENTITY_FILE = "identity.json";
|
|
3265
|
+
function saveIdentity(trellisDir, identity) {
|
|
3266
|
+
const filePath = join4(trellisDir, IDENTITY_FILE);
|
|
3267
|
+
if (!existsSync4(dirname3(filePath))) {
|
|
3268
|
+
mkdirSync3(dirname3(filePath), { recursive: true });
|
|
3269
|
+
}
|
|
3270
|
+
writeFileSync2(filePath, JSON.stringify(identity, null, 2), "utf-8");
|
|
3271
|
+
}
|
|
3272
|
+
function loadIdentity(trellisDir) {
|
|
3273
|
+
const filePath = join4(trellisDir, IDENTITY_FILE);
|
|
3274
|
+
if (!existsSync4(filePath))
|
|
3275
|
+
return null;
|
|
3276
|
+
try {
|
|
3277
|
+
return JSON.parse(readFileSync2(filePath, "utf-8"));
|
|
3278
|
+
} catch {
|
|
3279
|
+
return null;
|
|
3280
|
+
}
|
|
3281
|
+
}
|
|
3282
|
+
function hasIdentity(trellisDir) {
|
|
3283
|
+
return existsSync4(join4(trellisDir, IDENTITY_FILE));
|
|
3284
|
+
}
|
|
3285
|
+
function toPublicIdentity(identity) {
|
|
3286
|
+
return {
|
|
3287
|
+
displayName: identity.displayName,
|
|
3288
|
+
email: identity.email,
|
|
3289
|
+
publicKey: identity.publicKey,
|
|
3290
|
+
did: identity.did,
|
|
3291
|
+
entityId: identity.entityId,
|
|
3292
|
+
createdAt: identity.createdAt
|
|
3293
|
+
};
|
|
3294
|
+
}
|
|
3295
|
+
function base58btcEncode(buf) {
|
|
3296
|
+
const ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
3297
|
+
let num = BigInt(0);
|
|
3298
|
+
for (const byte of buf) {
|
|
3299
|
+
num = num * 256n + BigInt(byte);
|
|
3300
|
+
}
|
|
3301
|
+
let encoded = "";
|
|
3302
|
+
while (num > 0n) {
|
|
3303
|
+
const rem = Number(num % 58n);
|
|
3304
|
+
num = num / 58n;
|
|
3305
|
+
encoded = ALPHABET[rem] + encoded;
|
|
3306
|
+
}
|
|
3307
|
+
for (const byte of buf) {
|
|
3308
|
+
if (byte === 0) {
|
|
3309
|
+
encoded = "1" + encoded;
|
|
3310
|
+
} else {
|
|
3311
|
+
break;
|
|
3312
|
+
}
|
|
3313
|
+
}
|
|
3314
|
+
return encoded || "1";
|
|
3315
|
+
}
|
|
3316
|
+
// src/cli/index.ts
|
|
3317
|
+
var program2 = new Command;
|
|
3318
|
+
program2.name("trellis").description("TrellisVCS \u2014 graph-native, code-first version control").version("0.1.0");
|
|
3319
|
+
function requireRepo(rootPath) {
|
|
3320
|
+
if (!TrellisVcsEngine.isRepo(rootPath)) {
|
|
3321
|
+
console.error(source_default.red("Not a TrellisVCS repository. Run `trellis init` first."));
|
|
3322
|
+
process.exit(1);
|
|
3323
|
+
}
|
|
3324
|
+
}
|
|
3325
|
+
program2.command("init").description("Initialize a new TrellisVCS repository in the current directory").option("-p, --path <path>", "Path to initialize", ".").action(async (opts) => {
|
|
3326
|
+
const rootPath = resolve(opts.path);
|
|
3327
|
+
if (TrellisVcsEngine.isRepo(rootPath)) {
|
|
3328
|
+
console.log(source_default.yellow("Already a TrellisVCS repository."));
|
|
3329
|
+
return;
|
|
3330
|
+
}
|
|
3331
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
3332
|
+
const result = await engine.initRepo();
|
|
3333
|
+
console.log(source_default.green("\u2713 Initialized TrellisVCS repository"));
|
|
3334
|
+
console.log(` ${source_default.dim("Path:")} ${rootPath}`);
|
|
3335
|
+
console.log(` ${source_default.dim("Ops:")} ${result.opsCreated} initial operations recorded`);
|
|
3336
|
+
console.log(` ${source_default.dim("Config:")} .trellis/config.json`);
|
|
3337
|
+
console.log(` ${source_default.dim("Op log:")} .trellis/ops.json`);
|
|
3338
|
+
console.log();
|
|
3339
|
+
console.log(source_default.dim("The causal stream is now recording. Every file change will be tracked."));
|
|
3340
|
+
});
|
|
3341
|
+
program2.command("repair").description("Attempt to repair a corrupted .trellis/ops.json file").option("-p, --path <path>", "Repository path", ".").action((opts) => {
|
|
3342
|
+
const rootPath = resolve(opts.path);
|
|
3343
|
+
requireRepo(rootPath);
|
|
3344
|
+
console.log(source_default.yellow("Attempting to repair ops.json..."));
|
|
3345
|
+
const result = TrellisVcsEngine.repair(rootPath);
|
|
3346
|
+
if (result.lost === -1) {
|
|
3347
|
+
console.log(source_default.red("Could not recover any ops. A corrupted backup was saved as ops.json.corrupted"));
|
|
3348
|
+
} else if (result.recovered > 0) {
|
|
3349
|
+
console.log(source_default.green(`\u2713 Recovered ${result.recovered} ops.`));
|
|
3350
|
+
} else {
|
|
3351
|
+
console.log(source_default.green("ops.json is already valid. No repair needed."));
|
|
3352
|
+
}
|
|
3353
|
+
});
|
|
3354
|
+
program2.command("status").description("Show current repository status").option("-p, --path <path>", "Repository path", ".").action(async (opts) => {
|
|
3355
|
+
const rootPath = resolve(opts.path);
|
|
3356
|
+
if (!TrellisVcsEngine.isRepo(rootPath)) {
|
|
3357
|
+
console.log(source_default.red("Not a TrellisVCS repository. Run `trellis init` first."));
|
|
3358
|
+
process.exit(1);
|
|
3359
|
+
}
|
|
3360
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
3361
|
+
engine.open();
|
|
3362
|
+
const st = engine.status();
|
|
3363
|
+
console.log(source_default.bold("TrellisVCS Status"));
|
|
3364
|
+
console.log();
|
|
3365
|
+
console.log(` ${source_default.dim("Branch:")} ${source_default.cyan(st.branch)}`);
|
|
3366
|
+
console.log(` ${source_default.dim("Total ops:")} ${st.totalOps}`);
|
|
3367
|
+
console.log(` ${source_default.dim("Tracked files:")} ${st.trackedFiles}`);
|
|
3368
|
+
if (st.lastOp) {
|
|
3369
|
+
console.log();
|
|
3370
|
+
console.log(` ${source_default.dim("Last op:")} ${source_default.yellow(st.lastOp.kind)}`);
|
|
3371
|
+
console.log(` ${source_default.dim(" at:")} ${st.lastOp.timestamp}`);
|
|
3372
|
+
if (st.lastOp.vcs?.filePath) {
|
|
3373
|
+
console.log(` ${source_default.dim(" file:")} ${st.lastOp.vcs.filePath}`);
|
|
3374
|
+
}
|
|
3375
|
+
}
|
|
3376
|
+
if (st.recentOps.length > 0) {
|
|
3377
|
+
console.log();
|
|
3378
|
+
console.log(source_default.dim(" Recent activity:"));
|
|
3379
|
+
const display = st.recentOps.filter((op) => op.kind !== "vcs:branchCreate").slice(-5);
|
|
3380
|
+
for (const op of display) {
|
|
3381
|
+
const kind = formatOpKind(op.kind);
|
|
3382
|
+
const file = op.vcs?.filePath ?? "";
|
|
3383
|
+
const time = formatRelativeTime(op.timestamp);
|
|
3384
|
+
console.log(` ${kind} ${source_default.white(file)} ${source_default.dim(time)}`);
|
|
3385
|
+
}
|
|
3386
|
+
}
|
|
3387
|
+
});
|
|
3388
|
+
program2.command("log").description("Show operation history").option("-p, --path <path>", "Repository path", ".").option("-n, --limit <n>", "Number of ops to show", "20").option("-f, --file <file>", "Filter by file path").action(async (opts) => {
|
|
3389
|
+
const rootPath = resolve(opts.path);
|
|
3390
|
+
if (!TrellisVcsEngine.isRepo(rootPath)) {
|
|
3391
|
+
console.log(source_default.red("Not a TrellisVCS repository. Run `trellis init` first."));
|
|
3392
|
+
process.exit(1);
|
|
3393
|
+
}
|
|
3394
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
3395
|
+
engine.open();
|
|
3396
|
+
const ops = engine.log({
|
|
3397
|
+
limit: parseInt(opts.limit, 10),
|
|
3398
|
+
filePath: opts.file
|
|
3399
|
+
});
|
|
3400
|
+
if (ops.length === 0) {
|
|
3401
|
+
console.log(source_default.dim("No operations found."));
|
|
3402
|
+
return;
|
|
3403
|
+
}
|
|
3404
|
+
console.log(source_default.bold(`Causal Stream \u2014 ${ops.length} ops`));
|
|
3405
|
+
console.log();
|
|
3406
|
+
for (const op of ops.reverse()) {
|
|
3407
|
+
const kind = formatOpKind(op.kind);
|
|
3408
|
+
const hash = source_default.dim(op.hash.slice(0, 28) + "\u2026");
|
|
3409
|
+
const time = formatRelativeTime(op.timestamp);
|
|
3410
|
+
const file = op.vcs?.filePath ? source_default.white(op.vcs.filePath) : "";
|
|
3411
|
+
const rename = op.vcs?.oldFilePath ? source_default.dim(` (from ${op.vcs.oldFilePath})`) : "";
|
|
3412
|
+
console.log(` ${hash} ${kind} ${file}${rename} ${source_default.dim(time)}`);
|
|
3413
|
+
}
|
|
3414
|
+
});
|
|
3415
|
+
program2.command("files").description("List all tracked files").option("-p, --path <path>", "Repository path", ".").action(async (opts) => {
|
|
3416
|
+
const rootPath = resolve(opts.path);
|
|
3417
|
+
if (!TrellisVcsEngine.isRepo(rootPath)) {
|
|
3418
|
+
console.log(source_default.red("Not a TrellisVCS repository. Run `trellis init` first."));
|
|
3419
|
+
process.exit(1);
|
|
3420
|
+
}
|
|
3421
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
3422
|
+
engine.open();
|
|
3423
|
+
const files = engine.trackedFiles();
|
|
3424
|
+
if (files.length === 0) {
|
|
3425
|
+
console.log(source_default.dim("No tracked files."));
|
|
3426
|
+
return;
|
|
3427
|
+
}
|
|
3428
|
+
console.log(source_default.bold(`Tracked Files \u2014 ${files.length}`));
|
|
3429
|
+
console.log();
|
|
3430
|
+
for (const f of files.sort((a, b) => a.path.localeCompare(b.path))) {
|
|
3431
|
+
const hash = f.contentHash ? source_default.dim(f.contentHash.slice(0, 12)) : source_default.dim("(no hash)");
|
|
3432
|
+
console.log(` ${hash} ${f.path}`);
|
|
3433
|
+
}
|
|
3434
|
+
});
|
|
3435
|
+
program2.command("watch").description("Start file watcher (foreground, Ctrl+C to stop)").option("-p, --path <path>", "Repository path", ".").action(async (opts) => {
|
|
3436
|
+
const rootPath = resolve(opts.path);
|
|
3437
|
+
if (!TrellisVcsEngine.isRepo(rootPath)) {
|
|
3438
|
+
console.log(source_default.red("Not a TrellisVCS repository. Run `trellis init` first."));
|
|
3439
|
+
process.exit(1);
|
|
3440
|
+
}
|
|
3441
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
3442
|
+
engine.open();
|
|
3443
|
+
console.log(source_default.green("\u2713 Watching for changes\u2026") + source_default.dim(" (Ctrl+C to stop)"));
|
|
3444
|
+
console.log();
|
|
3445
|
+
const originalWatch = engine.watch.bind(engine);
|
|
3446
|
+
engine.watch();
|
|
3447
|
+
process.on("SIGINT", () => {
|
|
3448
|
+
engine.stop();
|
|
3449
|
+
console.log();
|
|
3450
|
+
console.log(source_default.dim("Watcher stopped."));
|
|
3451
|
+
process.exit(0);
|
|
3452
|
+
});
|
|
3453
|
+
});
|
|
3454
|
+
program2.command("import").description("Import from an existing Git repository").requiredOption("--from <path>", "Path to the Git repository to import from").option("-p, --path <path>", "Target TrellisVCS repository path", ".").action(async (opts) => {
|
|
3455
|
+
const from = resolve(opts.from);
|
|
3456
|
+
const to = resolve(opts.path);
|
|
3457
|
+
console.log(source_default.dim(`Importing from Git: ${from}`));
|
|
3458
|
+
console.log(source_default.dim(`Target: ${to}`));
|
|
3459
|
+
console.log();
|
|
3460
|
+
try {
|
|
3461
|
+
const result = await importFromGit({
|
|
3462
|
+
from,
|
|
3463
|
+
to,
|
|
3464
|
+
onProgress: (p) => {
|
|
3465
|
+
if (p.phase === "reading") {
|
|
3466
|
+
process.stdout.write(`\r ${source_default.dim("Reading\u2026")} ${p.message}`);
|
|
3467
|
+
} else if (p.phase === "importing") {
|
|
3468
|
+
process.stdout.write(`\r ${source_default.dim("Importing\u2026")} ${p.current}/${p.total} commits`);
|
|
3469
|
+
} else {
|
|
3470
|
+
process.stdout.write(`
|
|
3471
|
+
`);
|
|
3472
|
+
}
|
|
3473
|
+
}
|
|
3474
|
+
});
|
|
3475
|
+
console.log();
|
|
3476
|
+
console.log(source_default.green("\u2713 Git import complete"));
|
|
3477
|
+
console.log(` ${source_default.dim("Commits:")} ${result.commitsImported}`);
|
|
3478
|
+
console.log(` ${source_default.dim("Ops:")} ${result.opsCreated}`);
|
|
3479
|
+
console.log(` ${source_default.dim("Files:")} ${result.filesTracked}`);
|
|
3480
|
+
console.log(` ${source_default.dim("Branches:")} ${result.branches.join(", ")}`);
|
|
3481
|
+
console.log(` ${source_default.dim("Duration:")} ${(result.duration / 1000).toFixed(1)}s`);
|
|
3482
|
+
console.log();
|
|
3483
|
+
console.log(source_default.dim("Run `trellis status` or `trellis log` to explore the imported history."));
|
|
3484
|
+
} catch (err) {
|
|
3485
|
+
console.error(source_default.red(`
|
|
3486
|
+
Import failed: ${err.message}`));
|
|
3487
|
+
process.exit(1);
|
|
3488
|
+
}
|
|
3489
|
+
});
|
|
3490
|
+
program2.command("export").description("Export milestones to a Git repository").requiredOption("--to <path>", "Path to the target Git repository").option("-p, --path <path>", "Source TrellisVCS repository path", ".").option("--author-name <name>", "Author name for Git commits").option("--author-email <email>", "Author email for Git commits").action(async (opts) => {
|
|
3491
|
+
const from = resolve(opts.path);
|
|
3492
|
+
const to = resolve(opts.to);
|
|
3493
|
+
if (!TrellisVcsEngine.isRepo(from)) {
|
|
3494
|
+
console.error(source_default.red("Not a TrellisVCS repository. Run `trellis init` first."));
|
|
3495
|
+
process.exit(1);
|
|
3496
|
+
}
|
|
3497
|
+
console.log(source_default.dim(`Exporting from: ${from}`));
|
|
3498
|
+
console.log(source_default.dim(`Target Git repo: ${to}`));
|
|
3499
|
+
console.log();
|
|
3500
|
+
try {
|
|
3501
|
+
const result = await exportToGit({
|
|
3502
|
+
from,
|
|
3503
|
+
to,
|
|
3504
|
+
authorName: opts.authorName,
|
|
3505
|
+
authorEmail: opts.authorEmail,
|
|
3506
|
+
onProgress: (p) => {
|
|
3507
|
+
if (p.phase === "preparing") {
|
|
3508
|
+
console.log(` ${source_default.dim(p.message)}`);
|
|
3509
|
+
} else if (p.phase === "exporting") {
|
|
3510
|
+
process.stdout.write(`\r ${source_default.dim("Exporting\u2026")} ${p.current}/${p.total} milestones`);
|
|
3511
|
+
} else {
|
|
3512
|
+
process.stdout.write(`
|
|
3513
|
+
`);
|
|
3514
|
+
}
|
|
3515
|
+
}
|
|
3516
|
+
});
|
|
3517
|
+
console.log();
|
|
3518
|
+
console.log(source_default.green("\u2713 Git export complete"));
|
|
3519
|
+
console.log(` ${source_default.dim("Milestones:")} ${result.milestonesExported}`);
|
|
3520
|
+
console.log(` ${source_default.dim("Commits:")} ${result.commitsCreated}`);
|
|
3521
|
+
console.log(` ${source_default.dim("Duration:")} ${(result.duration / 1000).toFixed(1)}s`);
|
|
3522
|
+
} catch (err) {
|
|
3523
|
+
console.error(source_default.red(`
|
|
3524
|
+
Export failed: ${err.message}`));
|
|
3525
|
+
process.exit(1);
|
|
3526
|
+
}
|
|
3527
|
+
});
|
|
3528
|
+
program2.command("branch").description("Manage branches").argument("[name]", "Branch name to create or switch to").option("-d, --delete <name>", "Delete a branch").option("-l, --list", "List all branches").option("-p, --path <path>", "Repository path", ".").action(async (name, opts) => {
|
|
3529
|
+
const rootPath = resolve(opts.path);
|
|
3530
|
+
if (!TrellisVcsEngine.isRepo(rootPath)) {
|
|
3531
|
+
console.error(source_default.red("Not a TrellisVCS repository. Run `trellis init` first."));
|
|
3532
|
+
process.exit(1);
|
|
3533
|
+
}
|
|
3534
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
3535
|
+
engine.open();
|
|
3536
|
+
if (opts.delete) {
|
|
3537
|
+
try {
|
|
3538
|
+
await engine.deleteBranch(opts.delete);
|
|
3539
|
+
console.log(source_default.green(`\u2713 Deleted branch '${opts.delete}'`));
|
|
3540
|
+
} catch (err) {
|
|
3541
|
+
console.error(source_default.red(err.message));
|
|
3542
|
+
process.exit(1);
|
|
3543
|
+
}
|
|
3544
|
+
return;
|
|
3545
|
+
}
|
|
3546
|
+
if (name) {
|
|
3547
|
+
const branches2 = engine.listBranches();
|
|
3548
|
+
const exists = branches2.find((b) => b.name === name);
|
|
3549
|
+
if (exists) {
|
|
3550
|
+
try {
|
|
3551
|
+
engine.switchBranch(name);
|
|
3552
|
+
console.log(source_default.green(`\u2713 Switched to branch '${name}'`));
|
|
3553
|
+
} catch (err) {
|
|
3554
|
+
console.error(source_default.red(err.message));
|
|
3555
|
+
process.exit(1);
|
|
3556
|
+
}
|
|
3557
|
+
} else {
|
|
3558
|
+
try {
|
|
3559
|
+
await engine.createBranch(name);
|
|
3560
|
+
engine.switchBranch(name);
|
|
3561
|
+
console.log(source_default.green(`\u2713 Created and switched to branch '${name}'`));
|
|
3562
|
+
} catch (err) {
|
|
3563
|
+
console.error(source_default.red(err.message));
|
|
3564
|
+
process.exit(1);
|
|
3565
|
+
}
|
|
3566
|
+
}
|
|
3567
|
+
return;
|
|
3568
|
+
}
|
|
3569
|
+
const branches = engine.listBranches();
|
|
3570
|
+
if (branches.length === 0) {
|
|
3571
|
+
console.log(source_default.dim("No branches"));
|
|
3572
|
+
return;
|
|
3573
|
+
}
|
|
3574
|
+
console.log(source_default.bold(`Branches
|
|
3575
|
+
`));
|
|
3576
|
+
for (const b of branches) {
|
|
3577
|
+
const marker = b.isCurrent ? source_default.green("* ") : " ";
|
|
3578
|
+
const name2 = b.isCurrent ? source_default.green(b.name) : b.name;
|
|
3579
|
+
const age = b.createdAt ? source_default.dim(formatRelativeTime(b.createdAt)) : "";
|
|
3580
|
+
console.log(`${marker}${name2} ${age}`);
|
|
3581
|
+
}
|
|
3582
|
+
});
|
|
3583
|
+
program2.command("milestone").description("Create or list milestones").argument("[action]", '"create" or "list" (default: list)').option("-m, --message <message>", "Milestone message").option("--from <hash>", "Start op hash for the milestone range").option("--to <hash>", "End op hash for the milestone range").option("-p, --path <path>", "Repository path", ".").action(async (action, opts) => {
|
|
3584
|
+
const rootPath = resolve(opts.path);
|
|
3585
|
+
if (!TrellisVcsEngine.isRepo(rootPath)) {
|
|
3586
|
+
console.error(source_default.red("Not a TrellisVCS repository. Run `trellis init` first."));
|
|
3587
|
+
process.exit(1);
|
|
3588
|
+
}
|
|
3589
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
3590
|
+
engine.open();
|
|
3591
|
+
if (action === "create") {
|
|
3592
|
+
if (!opts.message) {
|
|
3593
|
+
console.error(source_default.red('Milestone message is required: --message "..."'));
|
|
3594
|
+
process.exit(1);
|
|
3595
|
+
}
|
|
3596
|
+
try {
|
|
3597
|
+
const op = await engine.createMilestone(opts.message, {
|
|
3598
|
+
fromOpHash: opts.from,
|
|
3599
|
+
toOpHash: opts.to
|
|
3600
|
+
});
|
|
3601
|
+
console.log(source_default.green(`\u2713 Milestone created`));
|
|
3602
|
+
console.log(` ${source_default.dim("ID:")} ${op.vcs?.milestoneId}`);
|
|
3603
|
+
console.log(` ${source_default.dim("Message:")} ${opts.message}`);
|
|
3604
|
+
console.log(` ${source_default.dim("Hash:")} ${op.hash.slice(0, 32)}\u2026`);
|
|
3605
|
+
} catch (err) {
|
|
3606
|
+
console.error(source_default.red(`Failed: ${err.message}`));
|
|
3607
|
+
process.exit(1);
|
|
3608
|
+
}
|
|
3609
|
+
return;
|
|
3610
|
+
}
|
|
3611
|
+
const milestones = engine.listMilestones();
|
|
3612
|
+
if (milestones.length === 0) {
|
|
3613
|
+
console.log(source_default.dim("No milestones"));
|
|
3614
|
+
return;
|
|
3615
|
+
}
|
|
3616
|
+
console.log(source_default.bold(`Milestones (${milestones.length})
|
|
3617
|
+
`));
|
|
3618
|
+
for (const m of milestones) {
|
|
3619
|
+
const age = m.createdAt ? formatRelativeTime(m.createdAt) : "";
|
|
3620
|
+
console.log(` ${source_default.cyan("\u2605")} ${source_default.bold(m.message ?? "(no message)")}`);
|
|
3621
|
+
console.log(` ${source_default.dim("ID:")} ${m.id} ${source_default.dim(age)}`);
|
|
3622
|
+
if (m.affectedFiles.length > 0) {
|
|
3623
|
+
console.log(` ${source_default.dim("Files:")} ${m.affectedFiles.slice(0, 5).join(", ")}${m.affectedFiles.length > 5 ? ` +${m.affectedFiles.length - 5} more` : ""}`);
|
|
3624
|
+
}
|
|
3625
|
+
console.log();
|
|
3626
|
+
}
|
|
3627
|
+
});
|
|
3628
|
+
program2.command("checkpoint").description("Create or list checkpoints").argument("[action]", '"create" or "list" (default: list)').option("-p, --path <path>", "Repository path", ".").action(async (action, opts) => {
|
|
3629
|
+
const rootPath = resolve(opts.path);
|
|
3630
|
+
if (!TrellisVcsEngine.isRepo(rootPath)) {
|
|
3631
|
+
console.error(source_default.red("Not a TrellisVCS repository. Run `trellis init` first."));
|
|
3632
|
+
process.exit(1);
|
|
3633
|
+
}
|
|
3634
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
3635
|
+
engine.open();
|
|
3636
|
+
if (action === "create") {
|
|
3637
|
+
try {
|
|
3638
|
+
const op = await engine.createCheckpoint("manual");
|
|
3639
|
+
console.log(source_default.green(`\u2713 Checkpoint created`));
|
|
3640
|
+
console.log(` ${source_default.dim("Hash:")} ${op.hash.slice(0, 32)}\u2026`);
|
|
3641
|
+
console.log(` ${source_default.dim("Trigger:")} manual`);
|
|
3642
|
+
} catch (err) {
|
|
3643
|
+
console.error(source_default.red(`Failed: ${err.message}`));
|
|
3644
|
+
process.exit(1);
|
|
3645
|
+
}
|
|
3646
|
+
return;
|
|
3647
|
+
}
|
|
3648
|
+
const checkpoints = engine.listCheckpoints();
|
|
3649
|
+
if (checkpoints.length === 0) {
|
|
3650
|
+
console.log(source_default.dim("No checkpoints"));
|
|
3651
|
+
return;
|
|
3652
|
+
}
|
|
3653
|
+
console.log(source_default.bold(`Checkpoints (${checkpoints.length})
|
|
3654
|
+
`));
|
|
3655
|
+
for (const cp of checkpoints) {
|
|
3656
|
+
const age = cp.createdAt ? formatRelativeTime(cp.createdAt) : "";
|
|
3657
|
+
console.log(` ${source_default.dim("\u25CF")} ${cp.id.slice(0, 32)} ${source_default.dim(cp.trigger ?? "")} ${source_default.dim(age)}`);
|
|
3658
|
+
}
|
|
3659
|
+
});
|
|
3660
|
+
program2.command("diff").description("Show file-level diff between two points in history").argument("[from]", "Starting op hash or milestone ID").argument("[to]", "Ending op hash (default: current head)").option("-p, --path <path>", "Repository path", ".").option("--stat", "Show only summary stats").action((from, to, opts) => {
|
|
3661
|
+
const rootPath = resolve(opts.path);
|
|
3662
|
+
if (!TrellisVcsEngine.isRepo(rootPath)) {
|
|
3663
|
+
console.error(source_default.red("Not a TrellisVCS repository. Run `trellis init` first."));
|
|
3664
|
+
process.exit(1);
|
|
3665
|
+
}
|
|
3666
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
3667
|
+
engine.open();
|
|
3668
|
+
let result;
|
|
3669
|
+
if (from && to) {
|
|
3670
|
+
result = engine.diffOps(from, to);
|
|
3671
|
+
} else if (from) {
|
|
3672
|
+
result = engine.diffFromOp(from);
|
|
3673
|
+
} else {
|
|
3674
|
+
const ops = engine.getOps();
|
|
3675
|
+
if (ops.length < 2) {
|
|
3676
|
+
console.log(source_default.dim("Not enough history to diff."));
|
|
3677
|
+
return;
|
|
3678
|
+
}
|
|
3679
|
+
result = engine.diffOps(ops[0].hash, ops[ops.length - 1].hash);
|
|
3680
|
+
}
|
|
3681
|
+
if (result.diffs.length === 0) {
|
|
3682
|
+
console.log(source_default.dim("No differences."));
|
|
3683
|
+
return;
|
|
3684
|
+
}
|
|
3685
|
+
const s = result.stats;
|
|
3686
|
+
console.log(`${source_default.green(`+${s.added} added`)} ${source_default.yellow(`~${s.modified} modified`)} ${source_default.red(`-${s.removed} removed`)}${s.renamed ? ` ${source_default.blue(`\u2192${s.renamed} renamed`)}` : ""}`);
|
|
3687
|
+
console.log();
|
|
3688
|
+
if (opts.stat)
|
|
3689
|
+
return;
|
|
3690
|
+
for (const diff of result.diffs) {
|
|
3691
|
+
switch (diff.kind) {
|
|
3692
|
+
case "fileAdded":
|
|
3693
|
+
console.log(`${source_default.green("+ " + diff.path)}`);
|
|
3694
|
+
break;
|
|
3695
|
+
case "fileDeleted":
|
|
3696
|
+
console.log(`${source_default.red("- " + diff.path)}`);
|
|
3697
|
+
break;
|
|
3698
|
+
case "fileRenamed":
|
|
3699
|
+
console.log(`${source_default.blue(`\u2192 ${diff.oldPath} \u2192 ${diff.path}`)}`);
|
|
3700
|
+
break;
|
|
3701
|
+
case "fileModified":
|
|
3702
|
+
console.log(`${source_default.yellow("~ " + diff.path)}`);
|
|
3703
|
+
if (diff.unifiedDiff) {
|
|
3704
|
+
for (const line of diff.unifiedDiff.split(`
|
|
3705
|
+
`)) {
|
|
3706
|
+
if (line.startsWith("+")) {
|
|
3707
|
+
console.log(source_default.green(line));
|
|
3708
|
+
} else if (line.startsWith("-")) {
|
|
3709
|
+
console.log(source_default.red(line));
|
|
3710
|
+
} else if (line.startsWith("@@")) {
|
|
3711
|
+
console.log(source_default.cyan(line));
|
|
3712
|
+
} else {
|
|
3713
|
+
console.log(source_default.dim(line));
|
|
3714
|
+
}
|
|
3715
|
+
}
|
|
3716
|
+
}
|
|
3717
|
+
break;
|
|
3718
|
+
}
|
|
3719
|
+
console.log();
|
|
3720
|
+
}
|
|
3721
|
+
});
|
|
3722
|
+
program2.command("merge").description("Merge a branch into the current branch").argument("<branch>", "Source branch to merge").option("-p, --path <path>", "Repository path", ".").option("--dry-run", "Preview merge without applying changes").action((branch, opts) => {
|
|
3723
|
+
const rootPath = resolve(opts.path);
|
|
3724
|
+
if (!TrellisVcsEngine.isRepo(rootPath)) {
|
|
3725
|
+
console.error(source_default.red("Not a TrellisVCS repository. Run `trellis init` first."));
|
|
3726
|
+
process.exit(1);
|
|
3727
|
+
}
|
|
3728
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
3729
|
+
engine.open();
|
|
3730
|
+
const result = engine.mergeBranch(branch);
|
|
3731
|
+
if (result.clean) {
|
|
3732
|
+
console.log(source_default.green("\u2713 Merge completed cleanly"));
|
|
3733
|
+
} else {
|
|
3734
|
+
console.log(source_default.yellow(`\u26A0 Merge has ${result.conflicts.length} conflict(s)`));
|
|
3735
|
+
}
|
|
3736
|
+
const s = result.stats;
|
|
3737
|
+
console.log(` ${source_default.dim("Modified:")} ${s.modified}`);
|
|
3738
|
+
console.log(` ${source_default.dim("Deleted:")} ${s.deleted}`);
|
|
3739
|
+
console.log(` ${source_default.dim("Conflicted:")} ${s.conflicted}`);
|
|
3740
|
+
if (result.conflicts.length > 0) {
|
|
3741
|
+
console.log();
|
|
3742
|
+
console.log(source_default.bold("Conflicts:"));
|
|
3743
|
+
for (const c of result.conflicts) {
|
|
3744
|
+
console.log(` ${source_default.red("\u2717")} ${c.path} (${c.kind})`);
|
|
3745
|
+
}
|
|
3746
|
+
}
|
|
3747
|
+
if (opts.dryRun) {
|
|
3748
|
+
console.log();
|
|
3749
|
+
console.log(source_default.dim("(dry run \u2014 no changes applied)"));
|
|
3750
|
+
}
|
|
3751
|
+
});
|
|
3752
|
+
program2.command("parse").description("Parse a file into AST-level semantic entities").argument("<file>", "File to parse").option("-p, --path <path>", "Repository path", ".").action((file, opts) => {
|
|
3753
|
+
const rootPath = resolve(opts.path);
|
|
3754
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
3755
|
+
if (TrellisVcsEngine.isRepo(rootPath))
|
|
3756
|
+
engine.open();
|
|
3757
|
+
const { readFileSync: readFileSync3 } = __require("fs");
|
|
3758
|
+
const filePath = resolve(file);
|
|
3759
|
+
const content = readFileSync3(filePath, "utf-8");
|
|
3760
|
+
const result = engine.parseFile(content, file);
|
|
3761
|
+
if (!result) {
|
|
3762
|
+
console.log(source_default.dim(`No parser available for: ${file}`));
|
|
3763
|
+
return;
|
|
3764
|
+
}
|
|
3765
|
+
console.log(source_default.bold(`Parse: ${file}
|
|
3766
|
+
`));
|
|
3767
|
+
console.log(` ${source_default.dim("Language:")} ${result.language}`);
|
|
3768
|
+
console.log(` ${source_default.dim("Declarations:")} ${result.declarations.length}`);
|
|
3769
|
+
console.log(` ${source_default.dim("Imports:")} ${result.imports.length}`);
|
|
3770
|
+
console.log(` ${source_default.dim("Exports:")} ${result.exports.length}`);
|
|
3771
|
+
if (result.declarations.length > 0) {
|
|
3772
|
+
console.log();
|
|
3773
|
+
console.log(source_default.bold("Declarations:"));
|
|
3774
|
+
for (const d of result.declarations) {
|
|
3775
|
+
console.log(` ${source_default.cyan(d.kind.padEnd(14))} ${source_default.bold(d.name)}${d.children.length ? ` (${d.children.length} members)` : ""}`);
|
|
3776
|
+
for (const child of d.children) {
|
|
3777
|
+
console.log(` ${source_default.dim(child.kind.padEnd(14))} ${child.name}`);
|
|
3778
|
+
}
|
|
3779
|
+
}
|
|
3780
|
+
}
|
|
3781
|
+
if (result.imports.length > 0) {
|
|
3782
|
+
console.log();
|
|
3783
|
+
console.log(source_default.bold("Imports:"));
|
|
3784
|
+
for (const imp of result.imports) {
|
|
3785
|
+
const specs = imp.specifiers.length > 0 ? ` { ${imp.specifiers.join(", ")} }` : "";
|
|
3786
|
+
console.log(` ${source_default.dim("from")} ${source_default.yellow(imp.source)}${specs}`);
|
|
3787
|
+
}
|
|
3788
|
+
}
|
|
3789
|
+
});
|
|
3790
|
+
program2.command("sdiff").description("Show semantic diff between two versions of a file").argument("<fileA>", "Old version of the file").argument("<fileB>", "New version of the file").option("-p, --path <path>", "Repository path", ".").action((fileA, fileB, opts) => {
|
|
3791
|
+
const rootPath = resolve(opts.path);
|
|
3792
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
3793
|
+
if (TrellisVcsEngine.isRepo(rootPath))
|
|
3794
|
+
engine.open();
|
|
3795
|
+
const { readFileSync: readFileSync3 } = __require("fs");
|
|
3796
|
+
const oldContent = readFileSync3(resolve(fileA), "utf-8");
|
|
3797
|
+
const newContent = readFileSync3(resolve(fileB), "utf-8");
|
|
3798
|
+
const patches = engine.semanticDiff(oldContent, newContent, fileA);
|
|
3799
|
+
if (patches.length === 0) {
|
|
3800
|
+
console.log(source_default.dim("No semantic differences."));
|
|
3801
|
+
return;
|
|
3802
|
+
}
|
|
3803
|
+
console.log(source_default.bold(`Semantic diff: ${fileA} \u2192 ${fileB}
|
|
3804
|
+
`));
|
|
3805
|
+
console.log(` ${source_default.dim("Patches:")} ${patches.length}
|
|
3806
|
+
`);
|
|
3807
|
+
for (const patch of patches) {
|
|
3808
|
+
switch (patch.kind) {
|
|
3809
|
+
case "symbolAdd":
|
|
3810
|
+
console.log(` ${source_default.green("+")} ${source_default.green(`${patch.entity.kind}: ${patch.entity.name}`)}`);
|
|
3811
|
+
break;
|
|
3812
|
+
case "symbolRemove":
|
|
3813
|
+
console.log(` ${source_default.red("-")} ${source_default.red(`${patch.entityName}`)} (removed)`);
|
|
3814
|
+
break;
|
|
3815
|
+
case "symbolModify":
|
|
3816
|
+
console.log(` ${source_default.yellow("~")} ${source_default.yellow(patch.entityName)} (modified)`);
|
|
3817
|
+
break;
|
|
3818
|
+
case "symbolRename":
|
|
3819
|
+
console.log(` ${source_default.blue("\u2192")} ${source_default.blue(`${patch.oldName} \u2192 ${patch.newName}`)} (renamed)`);
|
|
3820
|
+
break;
|
|
3821
|
+
case "importAdd":
|
|
3822
|
+
console.log(` ${source_default.green("+")} import from ${source_default.yellow(patch.source)}`);
|
|
3823
|
+
break;
|
|
3824
|
+
case "importRemove":
|
|
3825
|
+
console.log(` ${source_default.red("-")} import from ${source_default.yellow(patch.source)}`);
|
|
3826
|
+
break;
|
|
3827
|
+
case "importModify":
|
|
3828
|
+
console.log(` ${source_default.yellow("~")} import from ${source_default.yellow(patch.source)} (specifiers changed)`);
|
|
3829
|
+
break;
|
|
3830
|
+
case "exportAdd":
|
|
3831
|
+
console.log(` ${source_default.green("+")} export ${source_default.bold(patch.name)}`);
|
|
3832
|
+
break;
|
|
3833
|
+
case "exportRemove":
|
|
3834
|
+
console.log(` ${source_default.red("-")} export ${source_default.bold(patch.name)}`);
|
|
3835
|
+
break;
|
|
3836
|
+
case "symbolMove":
|
|
3837
|
+
console.log(` ${source_default.blue("\u2192")} ${source_default.blue(patch.entityName)} moved ${patch.oldFile} \u2192 ${patch.newFile}`);
|
|
3838
|
+
break;
|
|
3839
|
+
}
|
|
3840
|
+
}
|
|
3841
|
+
});
|
|
3842
|
+
program2.command("sync").description("Sync operations with another TrellisVCS repository").argument("[action]", '"push", "pull", "status", or "reconcile" (default: status)').option("-p, --path <path>", "Local repository path", ".").option("--remote <remote>", "Remote repository path").action((action, opts) => {
|
|
3843
|
+
const rootPath = resolve(opts.path);
|
|
3844
|
+
if (!TrellisVcsEngine.isRepo(rootPath)) {
|
|
3845
|
+
console.error(source_default.red("Not a TrellisVCS repository. Run `trellis init` first."));
|
|
3846
|
+
process.exit(1);
|
|
3847
|
+
}
|
|
3848
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
3849
|
+
engine.open();
|
|
3850
|
+
const ops = engine.getOps();
|
|
3851
|
+
if (action === "status" || !action) {
|
|
3852
|
+
console.log(source_default.bold(`Sync Status
|
|
3853
|
+
`));
|
|
3854
|
+
console.log(` ${source_default.dim("Local ops:")} ${ops.length}`);
|
|
3855
|
+
console.log(` ${source_default.dim("Head:")} ${ops.length > 0 ? ops[ops.length - 1].hash.slice(0, 16) + "\u2026" : "(none)"}`);
|
|
3856
|
+
console.log(` ${source_default.dim("Branch:")} ${engine.getCurrentBranch()}`);
|
|
3857
|
+
return;
|
|
3858
|
+
}
|
|
3859
|
+
if (action === "reconcile" && opts.remote) {
|
|
3860
|
+
const remotePath = resolve(opts.remote);
|
|
3861
|
+
if (!TrellisVcsEngine.isRepo(remotePath)) {
|
|
3862
|
+
console.error(source_default.red(`Not a TrellisVCS repository: ${remotePath}`));
|
|
3863
|
+
process.exit(1);
|
|
3864
|
+
}
|
|
3865
|
+
const remoteEngine = new TrellisVcsEngine({ rootPath: remotePath });
|
|
3866
|
+
remoteEngine.open();
|
|
3867
|
+
const remoteOps = remoteEngine.getOps();
|
|
3868
|
+
const { reconcile: reconcile2 } = (init_reconciler(), __toCommonJS(exports_reconciler));
|
|
3869
|
+
const result = reconcile2(ops, remoteOps);
|
|
3870
|
+
console.log(source_default.bold(`Reconcile Result
|
|
3871
|
+
`));
|
|
3872
|
+
console.log(` ${source_default.dim("Merged ops:")} ${result.merged.length}`);
|
|
3873
|
+
console.log(` ${source_default.dim("Unique local:")} ${result.uniqueToA.length}`);
|
|
3874
|
+
console.log(` ${source_default.dim("Unique remote:")} ${result.uniqueToB.length}`);
|
|
3875
|
+
console.log(` ${source_default.dim("Fork point:")} ${result.forkPoint?.slice(0, 16) ?? "(none)"}`);
|
|
3876
|
+
console.log(` ${source_default.dim("Clean:")} ${result.clean ? source_default.green("yes") : source_default.red("no")}`);
|
|
3877
|
+
if (result.conflicts.length > 0) {
|
|
3878
|
+
console.log();
|
|
3879
|
+
console.log(source_default.bold("Conflicts:"));
|
|
3880
|
+
for (const c of result.conflicts) {
|
|
3881
|
+
console.log(` ${source_default.red("\u2717")} ${c.filePath}: ${c.reason}`);
|
|
3882
|
+
}
|
|
3883
|
+
}
|
|
3884
|
+
return;
|
|
3885
|
+
}
|
|
3886
|
+
console.log(source_default.dim("Use --remote <path> with reconcile to compare repositories."));
|
|
3887
|
+
console.log(source_default.dim("Full peer sync requires a transport layer (coming soon)."));
|
|
3888
|
+
});
|
|
3889
|
+
program2.command("garden").description("Explore the Idea Garden \u2014 abandoned work clusters").argument("[action]", '"list", "show <id>", "search", "revive <id>", or "stats" (default: list)').argument("[id]", "Cluster ID (for show/revive)").option("-p, --path <path>", "Repository path", ".").option("-f, --file <file>", "Filter by file path").option("-k, --keyword <keyword>", "Filter by keyword").option("-s, --status <status>", "Filter by status (abandoned|draft|revived)").option("-n, --limit <n>", "Max results", parseInt).action((action, id, opts) => {
|
|
3890
|
+
const rootPath = resolve(opts.path);
|
|
3891
|
+
if (!TrellisVcsEngine.isRepo(rootPath)) {
|
|
3892
|
+
console.error(source_default.red("Not a TrellisVCS repository. Run `trellis init` first."));
|
|
3893
|
+
process.exit(1);
|
|
3894
|
+
}
|
|
3895
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
3896
|
+
engine.open();
|
|
3897
|
+
const garden = engine.garden();
|
|
3898
|
+
if (action === "stats") {
|
|
3899
|
+
const s = garden.stats();
|
|
3900
|
+
console.log(source_default.bold(`Idea Garden Stats
|
|
3901
|
+
`));
|
|
3902
|
+
console.log(` ${source_default.dim("Total clusters:")} ${s.total}`);
|
|
3903
|
+
console.log(` ${source_default.dim("Abandoned:")} ${s.abandoned}`);
|
|
3904
|
+
console.log(` ${source_default.dim("Draft:")} ${s.draft}`);
|
|
3905
|
+
console.log(` ${source_default.dim("Revived:")} ${s.revived}`);
|
|
3906
|
+
console.log(` ${source_default.dim("Total ops:")} ${s.totalOps}`);
|
|
3907
|
+
console.log(` ${source_default.dim("Total files:")} ${s.totalFiles}`);
|
|
3908
|
+
return;
|
|
3909
|
+
}
|
|
3910
|
+
if (action === "show") {
|
|
3911
|
+
if (!id) {
|
|
3912
|
+
console.error(source_default.red("Usage: trellis garden show <cluster-id>"));
|
|
3913
|
+
process.exit(1);
|
|
3914
|
+
}
|
|
3915
|
+
const cluster = garden.getCluster(id);
|
|
3916
|
+
if (!cluster) {
|
|
3917
|
+
console.error(source_default.red(`Cluster not found: ${id}`));
|
|
3918
|
+
process.exit(1);
|
|
3919
|
+
}
|
|
3920
|
+
console.log(source_default.bold(`Cluster: ${cluster.id}
|
|
3921
|
+
`));
|
|
3922
|
+
console.log(` ${source_default.dim("Status:")} ${formatClusterStatus(cluster.status)}`);
|
|
3923
|
+
console.log(` ${source_default.dim("Detected by:")} ${cluster.detectedBy}`);
|
|
3924
|
+
console.log(` ${source_default.dim("Created:")} ${cluster.createdAt}`);
|
|
3925
|
+
console.log(` ${source_default.dim("Abandoned:")} ${cluster.abandonedAt}`);
|
|
3926
|
+
console.log(` ${source_default.dim("Ops:")} ${cluster.ops.length}`);
|
|
3927
|
+
console.log(` ${source_default.dim("Files:")} ${cluster.affectedFiles.join(", ")}`);
|
|
3928
|
+
if (cluster.estimatedIntent) {
|
|
3929
|
+
console.log(` ${source_default.dim("Intent:")} ${cluster.estimatedIntent}`);
|
|
3930
|
+
}
|
|
3931
|
+
console.log();
|
|
3932
|
+
console.log(source_default.bold("Operations:"));
|
|
3933
|
+
for (const op of cluster.ops.slice(0, 20)) {
|
|
3934
|
+
console.log(` ${formatOpKind(op.kind)} ${source_default.dim(op.hash.slice(0, 12))} ${op.vcs?.filePath ?? ""}`);
|
|
3935
|
+
}
|
|
3936
|
+
if (cluster.ops.length > 20) {
|
|
3937
|
+
console.log(source_default.dim(` ... +${cluster.ops.length - 20} more`));
|
|
3938
|
+
}
|
|
3939
|
+
return;
|
|
3940
|
+
}
|
|
3941
|
+
if (action === "revive") {
|
|
3942
|
+
if (!id) {
|
|
3943
|
+
console.error(source_default.red("Usage: trellis garden revive <cluster-id>"));
|
|
3944
|
+
process.exit(1);
|
|
3945
|
+
}
|
|
3946
|
+
const ops = garden.revive(id);
|
|
3947
|
+
if (!ops) {
|
|
3948
|
+
console.error(source_default.red(`Cluster not found: ${id}`));
|
|
3949
|
+
process.exit(1);
|
|
3950
|
+
}
|
|
3951
|
+
console.log(source_default.green(`\u2713 Cluster revived: ${id}`));
|
|
3952
|
+
console.log(` ${source_default.dim("Ops to replay:")} ${ops.length}`);
|
|
3953
|
+
console.log(` ${source_default.dim("Files:")} ${[...new Set(ops.filter((o) => o.vcs?.filePath).map((o) => o.vcs.filePath))].join(", ")}`);
|
|
3954
|
+
return;
|
|
3955
|
+
}
|
|
3956
|
+
if (action === "search") {
|
|
3957
|
+
const results = garden.search({
|
|
3958
|
+
file: opts.file,
|
|
3959
|
+
keyword: opts.keyword,
|
|
3960
|
+
status: opts.status,
|
|
3961
|
+
limit: opts.limit
|
|
3962
|
+
});
|
|
3963
|
+
if (results.length === 0) {
|
|
3964
|
+
console.log(source_default.dim("No matching clusters found."));
|
|
3965
|
+
return;
|
|
3966
|
+
}
|
|
3967
|
+
console.log(source_default.bold(`Search results (${results.length})
|
|
3968
|
+
`));
|
|
3969
|
+
for (const c of results) {
|
|
3970
|
+
printClusterSummary(c);
|
|
3971
|
+
}
|
|
3972
|
+
return;
|
|
3973
|
+
}
|
|
3974
|
+
const clusters = garden.search({
|
|
3975
|
+
file: opts.file,
|
|
3976
|
+
keyword: opts.keyword,
|
|
3977
|
+
status: opts.status,
|
|
3978
|
+
limit: opts.limit
|
|
3979
|
+
});
|
|
3980
|
+
if (clusters.length === 0) {
|
|
3981
|
+
console.log(source_default.dim("No idea clusters found. The garden is empty."));
|
|
3982
|
+
return;
|
|
3983
|
+
}
|
|
3984
|
+
console.log(source_default.bold(`Idea Garden (${clusters.length} clusters)
|
|
3985
|
+
`));
|
|
3986
|
+
for (const c of clusters) {
|
|
3987
|
+
printClusterSummary(c);
|
|
3988
|
+
}
|
|
3989
|
+
});
|
|
3990
|
+
function formatClusterStatus(status) {
|
|
3991
|
+
switch (status) {
|
|
3992
|
+
case "abandoned":
|
|
3993
|
+
return source_default.yellow("abandoned");
|
|
3994
|
+
case "draft":
|
|
3995
|
+
return source_default.blue("draft");
|
|
3996
|
+
case "revived":
|
|
3997
|
+
return source_default.green("revived");
|
|
3998
|
+
default:
|
|
3999
|
+
return source_default.dim(status);
|
|
4000
|
+
}
|
|
4001
|
+
}
|
|
4002
|
+
function printClusterSummary(c) {
|
|
4003
|
+
console.log(` ${source_default.cyan("\u2740")} ${source_default.bold(c.id)} ${formatClusterStatus(c.status)} ${source_default.dim(c.detectedBy)}`);
|
|
4004
|
+
console.log(` ${source_default.dim("Ops:")} ${c.ops.length} ${source_default.dim("Files:")} ${c.affectedFiles.slice(0, 3).join(", ")}${c.affectedFiles.length > 3 ? ` +${c.affectedFiles.length - 3}` : ""}`);
|
|
4005
|
+
console.log(` ${source_default.dim("Created:")} ${formatRelativeTime(c.createdAt)} ${source_default.dim("Abandoned:")} ${formatRelativeTime(c.abandonedAt)}`);
|
|
4006
|
+
console.log();
|
|
4007
|
+
}
|
|
4008
|
+
function formatIssueStatus(status) {
|
|
4009
|
+
switch (status) {
|
|
4010
|
+
case "backlog":
|
|
4011
|
+
return source_default.gray("backlog");
|
|
4012
|
+
case "queue":
|
|
4013
|
+
return source_default.blue("queue");
|
|
4014
|
+
case "in_progress":
|
|
4015
|
+
return source_default.yellow("in_progress");
|
|
4016
|
+
case "paused":
|
|
4017
|
+
return source_default.magenta("paused");
|
|
4018
|
+
case "closed":
|
|
4019
|
+
return source_default.green("closed");
|
|
4020
|
+
default:
|
|
4021
|
+
return source_default.dim(status ?? "unknown");
|
|
4022
|
+
}
|
|
4023
|
+
}
|
|
4024
|
+
function formatPriority(p) {
|
|
4025
|
+
switch (p) {
|
|
4026
|
+
case "critical":
|
|
4027
|
+
return source_default.red("critical");
|
|
4028
|
+
case "high":
|
|
4029
|
+
return source_default.yellow("high");
|
|
4030
|
+
case "medium":
|
|
4031
|
+
return source_default.cyan("medium");
|
|
4032
|
+
case "low":
|
|
4033
|
+
return source_default.dim("low");
|
|
4034
|
+
default:
|
|
4035
|
+
return source_default.dim(p ?? "");
|
|
4036
|
+
}
|
|
4037
|
+
}
|
|
4038
|
+
function formatCriterionStatus(status) {
|
|
4039
|
+
switch (status) {
|
|
4040
|
+
case "passed":
|
|
4041
|
+
return source_default.green("\u2713 passed");
|
|
4042
|
+
case "failed":
|
|
4043
|
+
return source_default.red("\u2717 failed");
|
|
4044
|
+
case "pending":
|
|
4045
|
+
return source_default.dim("\u25CB pending");
|
|
4046
|
+
default:
|
|
4047
|
+
return source_default.dim(status ?? "pending");
|
|
4048
|
+
}
|
|
4049
|
+
}
|
|
4050
|
+
var issueCmd = program2.command("issue").description("Manage issues (task tracking)");
|
|
4051
|
+
issueCmd.command("create").description("Create a new issue").requiredOption("-t, --title <title>", "Issue title").option("-P, --priority <priority>", "Priority: critical, high, medium, low", "medium").option("-l, --labels <labels>", "Comma-separated labels").option("--assignee <agentId>", "Agent to assign").option("--parent <id>", "Parent issue ID (for sub-tasks)").option("-d, --desc <description>", "Short description").option("-S, --status <status>", "Initial status: backlog (default) or queue", "backlog").option("--ac <criteria...>", 'Acceptance criteria. Prefix with "test:" for test commands').option("-p, --path <path>", "Repository path", ".").action(async (opts) => {
|
|
4052
|
+
const rootPath = resolve(opts.path);
|
|
4053
|
+
requireRepo(rootPath);
|
|
4054
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4055
|
+
engine.open();
|
|
4056
|
+
const labels = opts.labels ? opts.labels.split(",").map((l) => l.trim()) : undefined;
|
|
4057
|
+
const criteria = opts.ac ? opts.ac.map((ac) => {
|
|
4058
|
+
if (ac.startsWith("test:")) {
|
|
4059
|
+
return { description: ac.slice(5), command: ac.slice(5) };
|
|
4060
|
+
}
|
|
4061
|
+
return { description: ac };
|
|
4062
|
+
}) : undefined;
|
|
4063
|
+
const op = await engine.createIssue(opts.title, {
|
|
4064
|
+
priority: opts.priority,
|
|
4065
|
+
labels,
|
|
4066
|
+
assignee: opts.assignee,
|
|
4067
|
+
parentId: opts.parent,
|
|
4068
|
+
description: opts.desc,
|
|
4069
|
+
status: opts.status,
|
|
4070
|
+
criteria
|
|
4071
|
+
});
|
|
4072
|
+
const issueId = op.vcs?.issueId;
|
|
4073
|
+
console.log(source_default.green(`\u2713 Issue created: ${source_default.bold(issueId)}`));
|
|
4074
|
+
console.log(` ${source_default.dim("Title:")} ${opts.title}`);
|
|
4075
|
+
console.log(` ${source_default.dim("Priority:")} ${formatPriority(opts.priority)}`);
|
|
4076
|
+
if (labels) {
|
|
4077
|
+
console.log(` ${source_default.dim("Labels:")} ${labels.join(", ")}`);
|
|
4078
|
+
}
|
|
4079
|
+
if (opts.parent) {
|
|
4080
|
+
console.log(` ${source_default.dim("Parent:")} ${opts.parent}`);
|
|
4081
|
+
}
|
|
4082
|
+
if (criteria) {
|
|
4083
|
+
console.log(` ${source_default.dim("Criteria:")} ${criteria.length} acceptance criteria`);
|
|
4084
|
+
}
|
|
4085
|
+
});
|
|
4086
|
+
issueCmd.command("list").description("List issues").option("--status <status>", "Filter by status: backlog, queue, in_progress, paused, closed").option("--label <label>", "Filter by label").option("--assignee <agentId>", "Filter by assignee").option("--parent <id>", "Filter by parent issue").option("-p, --path <path>", "Repository path", ".").action((opts) => {
|
|
4087
|
+
const rootPath = resolve(opts.path);
|
|
4088
|
+
requireRepo(rootPath);
|
|
4089
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4090
|
+
engine.open();
|
|
4091
|
+
const issues = engine.listIssues({
|
|
4092
|
+
status: opts.status,
|
|
4093
|
+
label: opts.label,
|
|
4094
|
+
assignee: opts.assignee,
|
|
4095
|
+
parentId: opts.parent
|
|
4096
|
+
});
|
|
4097
|
+
if (issues.length === 0) {
|
|
4098
|
+
console.log(source_default.dim("No issues found."));
|
|
4099
|
+
return;
|
|
4100
|
+
}
|
|
4101
|
+
console.log(source_default.bold(`Issues (${issues.length})
|
|
4102
|
+
`));
|
|
4103
|
+
for (const issue of issues) {
|
|
4104
|
+
const labels = issue.labels.length > 0 ? source_default.dim(` [${issue.labels.join(",")}]`) : "";
|
|
4105
|
+
const assignee = issue.assignee ? source_default.dim(` \u2192 ${issue.assignee}`) : "";
|
|
4106
|
+
const parent = issue.parentId ? source_default.dim(` \u2190 ${issue.parentId}`) : "";
|
|
4107
|
+
const blocked = issue.isBlocked ? source_default.yellow(" \uD83D\uDD12 blocked") : "";
|
|
4108
|
+
const criteria = issue.criteria.length > 0 ? source_default.dim(` (${issue.criteria.filter((c) => c.status === "passed").length}/${issue.criteria.length} AC)`) : "";
|
|
4109
|
+
console.log(` ${formatPriority(issue.priority)} ${source_default.bold(issue.id)} ${formatIssueStatus(issue.status)} ${issue.title ?? ""}${labels}${assignee}${parent}${blocked}${criteria}`);
|
|
4110
|
+
}
|
|
4111
|
+
});
|
|
4112
|
+
issueCmd.command("show").description("Show issue details").argument("<id>", "Issue ID (e.g. TRL-1)").option("-p, --path <path>", "Repository path", ".").action((id, opts) => {
|
|
4113
|
+
const rootPath = resolve(opts.path);
|
|
4114
|
+
requireRepo(rootPath);
|
|
4115
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4116
|
+
engine.open();
|
|
4117
|
+
const issue = engine.getIssue(id);
|
|
4118
|
+
if (!issue) {
|
|
4119
|
+
console.error(source_default.red(`Issue not found: ${id}`));
|
|
4120
|
+
process.exit(1);
|
|
4121
|
+
}
|
|
4122
|
+
console.log(source_default.bold(`${issue.id}: ${issue.title ?? "(untitled)"}
|
|
4123
|
+
`));
|
|
4124
|
+
if (issue.description) {
|
|
4125
|
+
console.log(` ${source_default.dim(issue.description)}
|
|
4126
|
+
`);
|
|
4127
|
+
}
|
|
4128
|
+
console.log(` ${source_default.dim("Status:")} ${formatIssueStatus(issue.status)}`);
|
|
4129
|
+
console.log(` ${source_default.dim("Priority:")} ${formatPriority(issue.priority)}`);
|
|
4130
|
+
if (issue.labels.length > 0) {
|
|
4131
|
+
console.log(` ${source_default.dim("Labels:")} ${issue.labels.join(", ")}`);
|
|
4132
|
+
}
|
|
4133
|
+
if (issue.assignee) {
|
|
4134
|
+
console.log(` ${source_default.dim("Assignee:")} ${issue.assignee}`);
|
|
4135
|
+
}
|
|
4136
|
+
if (issue.parentId) {
|
|
4137
|
+
console.log(` ${source_default.dim("Parent:")} ${issue.parentId}`);
|
|
4138
|
+
}
|
|
4139
|
+
if (issue.branchName) {
|
|
4140
|
+
console.log(` ${source_default.dim("Branch:")} ${issue.branchName}`);
|
|
4141
|
+
}
|
|
4142
|
+
if (issue.blockedBy.length > 0) {
|
|
4143
|
+
console.log(` ${source_default.dim("Blocked by:")} ${issue.blockedBy.map((b) => source_default.yellow(b)).join(", ")}`);
|
|
4144
|
+
}
|
|
4145
|
+
if (issue.blocking.length > 0) {
|
|
4146
|
+
console.log(` ${source_default.dim("Blocking:")} ${issue.blocking.map((b) => source_default.cyan(b)).join(", ")}`);
|
|
4147
|
+
}
|
|
4148
|
+
if (issue.createdAt) {
|
|
4149
|
+
console.log(` ${source_default.dim("Created:")} ${formatRelativeTime(issue.createdAt)}`);
|
|
4150
|
+
}
|
|
4151
|
+
if (issue.startedAt) {
|
|
4152
|
+
console.log(` ${source_default.dim("Started:")} ${formatRelativeTime(issue.startedAt)}`);
|
|
4153
|
+
}
|
|
4154
|
+
if (issue.closedAt) {
|
|
4155
|
+
console.log(` ${source_default.dim("Closed:")} ${formatRelativeTime(issue.closedAt)}`);
|
|
4156
|
+
}
|
|
4157
|
+
if (issue.criteria.length > 0) {
|
|
4158
|
+
console.log(`
|
|
4159
|
+
${source_default.bold("Acceptance Criteria:")}`);
|
|
4160
|
+
for (const c of issue.criteria) {
|
|
4161
|
+
const desc = c.description ?? c.id;
|
|
4162
|
+
const cmd = c.command ? source_default.dim(` (${c.command})`) : "";
|
|
4163
|
+
console.log(` ${formatCriterionStatus(c.status)} ${desc}${cmd}`);
|
|
4164
|
+
}
|
|
4165
|
+
}
|
|
4166
|
+
});
|
|
4167
|
+
issueCmd.command("start").description("Start working on an issue (creates branch, auto-assigns)").argument("<id>", "Issue ID").option("-p, --path <path>", "Repository path", ".").action(async (id, opts) => {
|
|
4168
|
+
const rootPath = resolve(opts.path);
|
|
4169
|
+
requireRepo(rootPath);
|
|
4170
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4171
|
+
engine.open();
|
|
4172
|
+
const op = await engine.startIssue(id);
|
|
4173
|
+
const issue = engine.getIssue(id);
|
|
4174
|
+
console.log(source_default.green(`\u2713 Started issue ${source_default.bold(id)}`));
|
|
4175
|
+
if (issue?.branchName) {
|
|
4176
|
+
console.log(` ${source_default.dim("Branch:")} ${issue.branchName}`);
|
|
4177
|
+
}
|
|
4178
|
+
if (issue?.assignee) {
|
|
4179
|
+
console.log(` ${source_default.dim("Assignee:")} ${issue.assignee}`);
|
|
4180
|
+
}
|
|
4181
|
+
});
|
|
4182
|
+
issueCmd.command("pause").description("Pause an in-progress issue (switches to default branch)").argument("<id>", "Issue ID").requiredOption("-n, --note <note>", "Why paused and what must happen before resuming").option("-p, --path <path>", "Repository path", ".").action(async (id, opts) => {
|
|
4183
|
+
const rootPath = resolve(opts.path);
|
|
4184
|
+
requireRepo(rootPath);
|
|
4185
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4186
|
+
engine.open();
|
|
4187
|
+
await engine.pauseIssue(id, opts.note);
|
|
4188
|
+
console.log(source_default.yellow(`\u23F8 Paused issue ${source_default.bold(id)}`));
|
|
4189
|
+
console.log(` ${source_default.dim("Note:")} ${opts.note}`);
|
|
4190
|
+
console.log(` ${source_default.dim("Switched to:")} ${engine.getCurrentBranch()}`);
|
|
4191
|
+
});
|
|
4192
|
+
issueCmd.command("resume").description("Resume a paused issue (switches to issue branch)").argument("<id>", "Issue ID").option("-p, --path <path>", "Repository path", ".").action(async (id, opts) => {
|
|
4193
|
+
const rootPath = resolve(opts.path);
|
|
4194
|
+
requireRepo(rootPath);
|
|
4195
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4196
|
+
engine.open();
|
|
4197
|
+
await engine.resumeIssue(id);
|
|
4198
|
+
const issue = engine.getIssue(id);
|
|
4199
|
+
console.log(source_default.green(`\u25B6 Resumed issue ${source_default.bold(id)}`));
|
|
4200
|
+
if (issue?.branchName) {
|
|
4201
|
+
console.log(` ${source_default.dim("Branch:")} ${issue.branchName}`);
|
|
4202
|
+
}
|
|
4203
|
+
});
|
|
4204
|
+
issueCmd.command("triage").description("Move a backlog issue to queue (ready to start)").argument("<id>", "Issue ID").option("-p, --path <path>", "Repository path", ".").action(async (id, opts) => {
|
|
4205
|
+
const rootPath = resolve(opts.path);
|
|
4206
|
+
requireRepo(rootPath);
|
|
4207
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4208
|
+
engine.open();
|
|
4209
|
+
await engine.triageIssue(id);
|
|
4210
|
+
console.log(source_default.green(`\u2713 Triaged ${source_default.bold(id)} \u2192 queue`));
|
|
4211
|
+
});
|
|
4212
|
+
issueCmd.command("update").description("Update issue metadata").argument("<id>", "Issue ID").option("--title <title>", "New title").option("-d, --desc <description>", "Short description").option("--status <status>", "New status: backlog, queue, in_progress, paused, closed").option("-P, --priority <priority>", "Priority: critical, high, medium, low").option("-l, --labels <labels>", "Comma-separated labels").option("--assignee <agentId>", "Agent to assign").option("-p, --path <path>", "Repository path", ".").action(async (id, opts) => {
|
|
4213
|
+
const rootPath = resolve(opts.path);
|
|
4214
|
+
requireRepo(rootPath);
|
|
4215
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4216
|
+
engine.open();
|
|
4217
|
+
const updates = {};
|
|
4218
|
+
if (opts.title !== undefined)
|
|
4219
|
+
updates.title = opts.title;
|
|
4220
|
+
if (opts.desc !== undefined)
|
|
4221
|
+
updates.description = opts.desc;
|
|
4222
|
+
if (opts.status !== undefined)
|
|
4223
|
+
updates.status = opts.status;
|
|
4224
|
+
if (opts.priority !== undefined)
|
|
4225
|
+
updates.priority = opts.priority;
|
|
4226
|
+
if (opts.labels !== undefined) {
|
|
4227
|
+
updates.labels = opts.labels.split(",").map((l) => l.trim());
|
|
4228
|
+
}
|
|
4229
|
+
if (opts.assignee !== undefined)
|
|
4230
|
+
updates.assignee = opts.assignee;
|
|
4231
|
+
await engine.updateIssue(id, updates);
|
|
4232
|
+
console.log(source_default.green(`\u2713 Updated ${source_default.bold(id)}`));
|
|
4233
|
+
});
|
|
4234
|
+
issueCmd.command("describe").description("Set an issue description").argument("<id>", "Issue ID").argument("<description>", "Short description text").option("-p, --path <path>", "Repository path", ".").action(async (id, description, opts) => {
|
|
4235
|
+
const rootPath = resolve(opts.path);
|
|
4236
|
+
requireRepo(rootPath);
|
|
4237
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4238
|
+
engine.open();
|
|
4239
|
+
await engine.updateIssue(id, { description });
|
|
4240
|
+
console.log(source_default.green(`\u2713 Description set for ${source_default.bold(id)}`));
|
|
4241
|
+
});
|
|
4242
|
+
issueCmd.command("assign").description("Assign an issue to an agent").argument("<id>", "Issue ID").requiredOption("--to <agentId>", "Agent ID to assign").option("-p, --path <path>", "Repository path", ".").action(async (id, opts) => {
|
|
4243
|
+
const rootPath = resolve(opts.path);
|
|
4244
|
+
requireRepo(rootPath);
|
|
4245
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4246
|
+
engine.open();
|
|
4247
|
+
await engine.assignIssue(id, opts.to);
|
|
4248
|
+
console.log(source_default.green(`\u2713 Assigned ${source_default.bold(id)} \u2192 ${opts.to}`));
|
|
4249
|
+
});
|
|
4250
|
+
issueCmd.command("ac").description("Add acceptance criterion to an issue").argument("<id>", "Issue ID").argument("<description>", "Criterion description").option("--test <command>", "Shell command to validate (exit 0 = pass)").option("-p, --path <path>", "Repository path", ".").action(async (id, description, opts) => {
|
|
4251
|
+
const rootPath = resolve(opts.path);
|
|
4252
|
+
requireRepo(rootPath);
|
|
4253
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4254
|
+
engine.open();
|
|
4255
|
+
await engine.addCriterion(id, description, opts.test);
|
|
4256
|
+
const cmdNote = opts.test ? source_default.dim(` (test: ${opts.test})`) : "";
|
|
4257
|
+
console.log(source_default.green(`\u2713 Added criterion to ${source_default.bold(id)}: ${description}${cmdNote}`));
|
|
4258
|
+
});
|
|
4259
|
+
issueCmd.command("ac-pass").description("Manually mark an acceptance criterion as passed").argument("<id>", "Issue ID").argument("<index>", "Criterion number (1-based)").option("-p, --path <path>", "Repository path", ".").action(async (id, index, opts) => {
|
|
4260
|
+
const rootPath = resolve(opts.path);
|
|
4261
|
+
requireRepo(rootPath);
|
|
4262
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4263
|
+
engine.open();
|
|
4264
|
+
await engine.setCriterionStatus(id, parseInt(index, 10), "passed");
|
|
4265
|
+
console.log(source_default.green(`\u2713 Criterion #${index} on ${source_default.bold(id)} marked as passed`));
|
|
4266
|
+
});
|
|
4267
|
+
issueCmd.command("ac-fail").description("Manually mark an acceptance criterion as failed").argument("<id>", "Issue ID").argument("<index>", "Criterion number (1-based)").option("-p, --path <path>", "Repository path", ".").action(async (id, index, opts) => {
|
|
4268
|
+
const rootPath = resolve(opts.path);
|
|
4269
|
+
requireRepo(rootPath);
|
|
4270
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4271
|
+
engine.open();
|
|
4272
|
+
await engine.setCriterionStatus(id, parseInt(index, 10), "failed");
|
|
4273
|
+
console.log(source_default.red(`\u2717 Criterion #${index} on ${source_default.bold(id)} marked as failed`));
|
|
4274
|
+
});
|
|
4275
|
+
issueCmd.command("check").description("Run acceptance criteria for an issue").argument("<id>", "Issue ID").option("-p, --path <path>", "Repository path", ".").action(async (id, opts) => {
|
|
4276
|
+
const rootPath = resolve(opts.path);
|
|
4277
|
+
requireRepo(rootPath);
|
|
4278
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4279
|
+
engine.open();
|
|
4280
|
+
console.log(source_default.bold(`Running criteria for ${id}...
|
|
4281
|
+
`));
|
|
4282
|
+
const results = await engine.runCriteria(id);
|
|
4283
|
+
if (results.length === 0) {
|
|
4284
|
+
console.log(source_default.dim("No acceptance criteria defined."));
|
|
4285
|
+
return;
|
|
4286
|
+
}
|
|
4287
|
+
for (const r of results) {
|
|
4288
|
+
const desc = r.description ?? r.id;
|
|
4289
|
+
const statusStr = r.status === "passed" ? source_default.green("\u2713 PASSED") : r.status === "failed" ? source_default.red("\u2717 FAILED") : source_default.dim("\u25CB SKIPPED");
|
|
4290
|
+
console.log(` ${statusStr} ${desc}`);
|
|
4291
|
+
if (r.command) {
|
|
4292
|
+
console.log(` ${source_default.dim("$")} ${r.command}`);
|
|
4293
|
+
}
|
|
4294
|
+
if (r.output && r.status === "failed") {
|
|
4295
|
+
const lines = r.output.split(`
|
|
4296
|
+
`).slice(0, 5);
|
|
4297
|
+
for (const line of lines) {
|
|
4298
|
+
console.log(` ${source_default.dim(line)}`);
|
|
4299
|
+
}
|
|
4300
|
+
}
|
|
4301
|
+
}
|
|
4302
|
+
const passed = results.filter((r) => r.status === "passed").length;
|
|
4303
|
+
const total = results.length;
|
|
4304
|
+
console.log();
|
|
4305
|
+
if (passed === total) {
|
|
4306
|
+
console.log(source_default.green(`All ${total} criteria passed. Close with: trellis issue close ${id} --confirm`));
|
|
4307
|
+
} else {
|
|
4308
|
+
console.log(source_default.yellow(`${passed}/${total} criteria passing.`));
|
|
4309
|
+
}
|
|
4310
|
+
});
|
|
4311
|
+
issueCmd.command("close").description("Close an issue (requires all criteria pass + --confirm)").argument("<id>", "Issue ID").option("--confirm", "Confirm closure after criteria pass").option("-p, --path <path>", "Repository path", ".").action(async (id, opts) => {
|
|
4312
|
+
const rootPath = resolve(opts.path);
|
|
4313
|
+
requireRepo(rootPath);
|
|
4314
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4315
|
+
engine.open();
|
|
4316
|
+
try {
|
|
4317
|
+
const result = await engine.closeIssue(id, { confirm: opts.confirm });
|
|
4318
|
+
if (!result.op) {
|
|
4319
|
+
console.log(source_default.bold(`Criteria status for ${id}:
|
|
4320
|
+
`));
|
|
4321
|
+
for (const r of result.criteriaResults) {
|
|
4322
|
+
console.log(` ${formatCriterionStatus(r.status)} ${r.description ?? r.id}`);
|
|
4323
|
+
}
|
|
4324
|
+
console.log();
|
|
4325
|
+
console.log(source_default.yellow(`All criteria pass. Re-run with --confirm to close: trellis issue close ${id} --confirm`));
|
|
4326
|
+
return;
|
|
4327
|
+
}
|
|
4328
|
+
console.log(source_default.green(`\u2713 Issue ${source_default.bold(id)} closed`));
|
|
4329
|
+
} catch (err) {
|
|
4330
|
+
console.error(source_default.red(err.message));
|
|
4331
|
+
process.exit(1);
|
|
4332
|
+
}
|
|
4333
|
+
});
|
|
4334
|
+
issueCmd.command("reopen").description("Reopen a closed issue").argument("<id>", "Issue ID").option("-p, --path <path>", "Repository path", ".").action(async (id, opts) => {
|
|
4335
|
+
const rootPath = resolve(opts.path);
|
|
4336
|
+
requireRepo(rootPath);
|
|
4337
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4338
|
+
engine.open();
|
|
4339
|
+
await engine.reopenIssue(id);
|
|
4340
|
+
console.log(source_default.green(`\u2713 Issue ${source_default.bold(id)} reopened`));
|
|
4341
|
+
});
|
|
4342
|
+
issueCmd.command("block").description("Mark an issue as blocked by another issue").argument("<id>", "Issue ID to block").argument("<blockedBy>", "Issue ID that blocks it").option("-p, --path <path>", "Repository path", ".").action(async (id, blockedBy, opts) => {
|
|
4343
|
+
const rootPath = resolve(opts.path);
|
|
4344
|
+
requireRepo(rootPath);
|
|
4345
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4346
|
+
engine.open();
|
|
4347
|
+
await engine.blockIssue(id, blockedBy);
|
|
4348
|
+
console.log(source_default.yellow(`\uD83D\uDD12 ${source_default.bold(id)} is now blocked by ${source_default.bold(blockedBy)}`));
|
|
4349
|
+
});
|
|
4350
|
+
issueCmd.command("unblock").description("Remove a blocking relationship").argument("<id>", "Blocked issue ID").argument("<blockedBy>", "Blocking issue ID to remove").option("-p, --path <path>", "Repository path", ".").action(async (id, blockedBy, opts) => {
|
|
4351
|
+
const rootPath = resolve(opts.path);
|
|
4352
|
+
requireRepo(rootPath);
|
|
4353
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4354
|
+
engine.open();
|
|
4355
|
+
await engine.unblockIssue(id, blockedBy);
|
|
4356
|
+
console.log(source_default.green(`\uD83D\uDD13 ${source_default.bold(id)} is no longer blocked by ${source_default.bold(blockedBy)}`));
|
|
4357
|
+
});
|
|
4358
|
+
issueCmd.command("active").description("Show all active (in-progress) issues").option("-p, --path <path>", "Repository path", ".").action((opts) => {
|
|
4359
|
+
const rootPath = resolve(opts.path);
|
|
4360
|
+
requireRepo(rootPath);
|
|
4361
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4362
|
+
engine.open();
|
|
4363
|
+
const active = engine.getActiveIssues();
|
|
4364
|
+
if (active.length === 0) {
|
|
4365
|
+
console.log(source_default.dim("No active issues."));
|
|
4366
|
+
return;
|
|
4367
|
+
}
|
|
4368
|
+
console.log(source_default.bold(`Active Issues (${active.length})
|
|
4369
|
+
`));
|
|
4370
|
+
for (const issue of active) {
|
|
4371
|
+
const branch = issue.branchName ? source_default.dim(` on ${issue.branchName}`) : "";
|
|
4372
|
+
const assignee = issue.assignee ? source_default.dim(` \u2192 ${issue.assignee}`) : "";
|
|
4373
|
+
console.log(` ${formatPriority(issue.priority)} ${source_default.bold(issue.id)} ${issue.title ?? ""}${branch}${assignee}`);
|
|
4374
|
+
}
|
|
4375
|
+
});
|
|
4376
|
+
issueCmd.command("readiness").description("Check if all issues are complete (no queue, paused, or in-progress)").option("-p, --path <path>", "Repository path", ".").action((opts) => {
|
|
4377
|
+
const rootPath = resolve(opts.path);
|
|
4378
|
+
requireRepo(rootPath);
|
|
4379
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4380
|
+
engine.open();
|
|
4381
|
+
const result = engine.checkCompletionReadiness();
|
|
4382
|
+
console.log(result.summary);
|
|
4383
|
+
if (!result.ready) {
|
|
4384
|
+
process.exit(1);
|
|
4385
|
+
}
|
|
4386
|
+
});
|
|
4387
|
+
var decisionCmd = program2.command("decision").description("Manage decision traces");
|
|
4388
|
+
decisionCmd.command("list").description("List decision traces").option("-p, --path <path>", "Repository path", ".").option("-t, --tool <pattern>", 'Filter by tool name pattern (e.g. "trellis_issue_*")').option("-e, --entity <id>", "Filter by related entity ID").option("-n, --limit <n>", "Max results", "20").action((opts) => {
|
|
4389
|
+
const rootPath = resolve(opts.path);
|
|
4390
|
+
requireRepo(rootPath);
|
|
4391
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4392
|
+
engine.open();
|
|
4393
|
+
const decisions = engine.queryDecisions({
|
|
4394
|
+
toolPattern: opts.tool,
|
|
4395
|
+
entityId: opts.entity,
|
|
4396
|
+
limit: parseInt(opts.limit, 10)
|
|
4397
|
+
});
|
|
4398
|
+
if (decisions.length === 0) {
|
|
4399
|
+
console.log(source_default.dim("No decision traces found."));
|
|
4400
|
+
return;
|
|
4401
|
+
}
|
|
4402
|
+
for (const d of decisions) {
|
|
4403
|
+
const ts = d.createdAt ? source_default.dim(d.createdAt) : "";
|
|
4404
|
+
console.log(`${source_default.cyan(d.id)} ${source_default.white(d.toolName)} ${ts}`);
|
|
4405
|
+
if (d.rationale) {
|
|
4406
|
+
console.log(` ${source_default.dim("\u2192")} ${d.rationale}`);
|
|
4407
|
+
}
|
|
4408
|
+
}
|
|
4409
|
+
});
|
|
4410
|
+
decisionCmd.command("show").description("Show full details of a decision trace").argument("<id>", "Decision ID (e.g. DEC-1)").option("-p, --path <path>", "Repository path", ".").action((id, opts) => {
|
|
4411
|
+
const rootPath = resolve(opts.path);
|
|
4412
|
+
requireRepo(rootPath);
|
|
4413
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4414
|
+
engine.open();
|
|
4415
|
+
const d = engine.getDecision(id);
|
|
4416
|
+
if (!d) {
|
|
4417
|
+
console.error(source_default.red(`Decision ${id} not found.`));
|
|
4418
|
+
process.exit(1);
|
|
4419
|
+
}
|
|
4420
|
+
console.log(`${source_default.bold("ID:")} ${d.id}`);
|
|
4421
|
+
console.log(`${source_default.bold("Tool:")} ${d.toolName}`);
|
|
4422
|
+
console.log(`${source_default.bold("Created:")} ${d.createdAt ?? "unknown"}`);
|
|
4423
|
+
console.log(`${source_default.bold("Agent:")} ${d.createdBy ?? "unknown"}`);
|
|
4424
|
+
if (d.context)
|
|
4425
|
+
console.log(`${source_default.bold("Context:")} ${d.context}`);
|
|
4426
|
+
if (d.rationale)
|
|
4427
|
+
console.log(`${source_default.bold("Rationale:")} ${d.rationale}`);
|
|
4428
|
+
if (d.alternatives && d.alternatives.length > 0) {
|
|
4429
|
+
console.log(`${source_default.bold("Alternatives:")} ${d.alternatives.join(", ")}`);
|
|
4430
|
+
}
|
|
4431
|
+
if (d.outputSummary) {
|
|
4432
|
+
console.log(`${source_default.bold("Output:")} ${d.outputSummary}`);
|
|
4433
|
+
}
|
|
4434
|
+
if (d.relatedEntities.length > 0) {
|
|
4435
|
+
console.log(`${source_default.bold("Related:")} ${d.relatedEntities.join(", ")}`);
|
|
4436
|
+
}
|
|
4437
|
+
});
|
|
4438
|
+
decisionCmd.command("chain").description("Trace all decisions that affected a given entity").argument("<entityId>", 'Entity ID (e.g. "issue:TRL-5", "file:src/engine.ts")').option("-p, --path <path>", "Repository path", ".").action((entityId, opts) => {
|
|
4439
|
+
const rootPath = resolve(opts.path);
|
|
4440
|
+
requireRepo(rootPath);
|
|
4441
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4442
|
+
engine.open();
|
|
4443
|
+
const chain = engine.getDecisionChain(entityId);
|
|
4444
|
+
if (chain.length === 0) {
|
|
4445
|
+
console.log(source_default.dim(`No decision traces found for ${entityId}.`));
|
|
4446
|
+
return;
|
|
4447
|
+
}
|
|
4448
|
+
console.log(source_default.bold(`Decision chain for ${entityId} (${chain.length} decisions):`));
|
|
4449
|
+
for (const d of chain) {
|
|
4450
|
+
const ts = d.createdAt ? source_default.dim(d.createdAt) : "";
|
|
4451
|
+
console.log(` ${source_default.cyan(d.id)} ${source_default.white(d.toolName)} ${ts}`);
|
|
4452
|
+
if (d.rationale) {
|
|
4453
|
+
console.log(` ${source_default.dim("\u2192")} ${d.rationale}`);
|
|
4454
|
+
}
|
|
4455
|
+
}
|
|
4456
|
+
});
|
|
4457
|
+
program2.command("identity").description("Manage local identity (Ed25519 key pair)").argument("[action]", '"init" or "show" (default: show)').option("-p, --path <path>", "Repository path", ".").option("--name <name>", "Display name for new identity").option("--email <email>", "Email for new identity").action((action, opts) => {
|
|
4458
|
+
const rootPath = resolve(opts.path);
|
|
4459
|
+
const trellisDir = join5(rootPath, ".trellis");
|
|
4460
|
+
if (action === "init") {
|
|
4461
|
+
if (hasIdentity(trellisDir)) {
|
|
4462
|
+
console.error(source_default.yellow("Identity already exists. Use `trellis identity` to view it."));
|
|
4463
|
+
process.exit(1);
|
|
4464
|
+
}
|
|
4465
|
+
const name = opts.name ?? "Anonymous";
|
|
4466
|
+
const email = opts.email;
|
|
4467
|
+
const identity2 = createIdentity({ displayName: name, email });
|
|
4468
|
+
saveIdentity(trellisDir, identity2);
|
|
4469
|
+
console.log(source_default.green("\u2713 Identity created"));
|
|
4470
|
+
console.log(` ${source_default.dim("Name:")} ${identity2.displayName}`);
|
|
4471
|
+
if (identity2.email) {
|
|
4472
|
+
console.log(` ${source_default.dim("Email:")} ${identity2.email}`);
|
|
4473
|
+
}
|
|
4474
|
+
console.log(` ${source_default.dim("DID:")} ${identity2.did}`);
|
|
4475
|
+
console.log(` ${source_default.dim("ID:")} ${identity2.entityId}`);
|
|
4476
|
+
return;
|
|
4477
|
+
}
|
|
4478
|
+
const identity = loadIdentity(trellisDir);
|
|
4479
|
+
if (!identity) {
|
|
4480
|
+
console.log(source_default.dim('No identity configured. Run `trellis identity init --name "Your Name"`.'));
|
|
4481
|
+
return;
|
|
4482
|
+
}
|
|
4483
|
+
const pub = toPublicIdentity(identity);
|
|
4484
|
+
console.log(source_default.bold(`Identity
|
|
4485
|
+
`));
|
|
4486
|
+
console.log(` ${source_default.dim("Name:")} ${pub.displayName}`);
|
|
4487
|
+
if (pub.email) {
|
|
4488
|
+
console.log(` ${source_default.dim("Email:")} ${pub.email}`);
|
|
4489
|
+
}
|
|
4490
|
+
console.log(` ${source_default.dim("DID:")} ${pub.did}`);
|
|
4491
|
+
console.log(` ${source_default.dim("Entity ID:")} ${pub.entityId}`);
|
|
4492
|
+
console.log(` ${source_default.dim("Public Key:")} ${pub.publicKey.slice(0, 32)}\u2026`);
|
|
4493
|
+
console.log(` ${source_default.dim("Created:")} ${pub.createdAt}`);
|
|
4494
|
+
});
|
|
4495
|
+
program2.command("refs").description("List wiki-link references in files or find backlinks").argument("[file]", "File to list outgoing refs for").option("-p, --path <path>", "Repository path", ".").option("--backlinks <entity>", "Show all files referencing an entity (e.g. TRL-5)").option("--broken", "List all broken and stale references").option("--stats", "Show reference index statistics").action((file, opts) => {
|
|
4496
|
+
const rootPath = resolve(opts.path);
|
|
4497
|
+
if (!TrellisVcsEngine.isRepo(rootPath)) {
|
|
4498
|
+
console.error(source_default.red("Not a TrellisVCS repository. Run `trellis init` first."));
|
|
4499
|
+
process.exit(1);
|
|
4500
|
+
}
|
|
4501
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4502
|
+
engine.open();
|
|
4503
|
+
const { readFileSync: readFileSync3 } = __require("fs");
|
|
4504
|
+
const {
|
|
4505
|
+
parseFileRefs,
|
|
4506
|
+
buildRefIndex,
|
|
4507
|
+
getOutgoingRefs,
|
|
4508
|
+
getBacklinks,
|
|
4509
|
+
getIndexStats
|
|
4510
|
+
} = (init_links(), __toCommonJS(exports_links));
|
|
4511
|
+
const {
|
|
4512
|
+
resolveRef,
|
|
4513
|
+
resolveRefs,
|
|
4514
|
+
createResolverContext
|
|
4515
|
+
} = (init_links(), __toCommonJS(exports_links));
|
|
4516
|
+
const { StaleRefRegistry, getDiagnostics } = (init_links(), __toCommonJS(exports_links));
|
|
4517
|
+
const resolverCtx = createResolverContext(engine);
|
|
4518
|
+
const trackedFiles = engine.trackedFiles();
|
|
4519
|
+
const fileContents = [];
|
|
4520
|
+
for (const f of trackedFiles) {
|
|
4521
|
+
try {
|
|
4522
|
+
const absPath = join5(rootPath, f.path);
|
|
4523
|
+
const content = readFileSync3(absPath, "utf-8");
|
|
4524
|
+
fileContents.push({ path: f.path, content });
|
|
4525
|
+
} catch {}
|
|
4526
|
+
}
|
|
4527
|
+
const index = buildRefIndex(fileContents, resolverCtx);
|
|
4528
|
+
if (opts.stats) {
|
|
4529
|
+
const stats = getIndexStats(index);
|
|
4530
|
+
console.log(source_default.bold(`Reference Index Stats
|
|
4531
|
+
`));
|
|
4532
|
+
console.log(` ${source_default.dim("Files with refs:")} ${stats.totalFiles}`);
|
|
4533
|
+
console.log(` ${source_default.dim("Total refs:")} ${stats.totalRefs}`);
|
|
4534
|
+
console.log(` ${source_default.dim("Unique entities:")} ${stats.totalEntities}`);
|
|
4535
|
+
return;
|
|
4536
|
+
}
|
|
4537
|
+
if (opts.backlinks) {
|
|
4538
|
+
const entity = opts.backlinks;
|
|
4539
|
+
const candidates = [
|
|
4540
|
+
`issue:${entity}`,
|
|
4541
|
+
`file:${entity}`,
|
|
4542
|
+
`symbol:${entity}`,
|
|
4543
|
+
`identity:${entity}`,
|
|
4544
|
+
`milestone:${entity}`,
|
|
4545
|
+
`decision:${entity}`,
|
|
4546
|
+
entity
|
|
4547
|
+
];
|
|
4548
|
+
let found = false;
|
|
4549
|
+
for (const eid of candidates) {
|
|
4550
|
+
const sources = getBacklinks(index, eid);
|
|
4551
|
+
if (sources.length > 0) {
|
|
4552
|
+
console.log(source_default.bold(`Backlinks for ${source_default.cyan(eid)} (${sources.length})
|
|
4553
|
+
`));
|
|
4554
|
+
for (const s of sources) {
|
|
4555
|
+
console.log(` ${source_default.dim(s.filePath)}:${s.line} ${source_default.dim(`(${s.context})`)}`);
|
|
4556
|
+
}
|
|
4557
|
+
found = true;
|
|
4558
|
+
break;
|
|
4559
|
+
}
|
|
4560
|
+
}
|
|
4561
|
+
if (!found) {
|
|
4562
|
+
console.log(source_default.dim(`No references found for: ${entity}`));
|
|
4563
|
+
}
|
|
4564
|
+
return;
|
|
4565
|
+
}
|
|
4566
|
+
if (opts.broken) {
|
|
4567
|
+
const registry = new StaleRefRegistry;
|
|
4568
|
+
const resolvedIds = new Set;
|
|
4569
|
+
for (const [, refs] of index.outgoing) {
|
|
4570
|
+
for (const ref of refs) {
|
|
4571
|
+
const resolved = resolveRef(ref, resolverCtx);
|
|
4572
|
+
if (resolved.state === "resolved" && resolved.entityId) {
|
|
4573
|
+
resolvedIds.add(resolved.entityId);
|
|
4574
|
+
}
|
|
4575
|
+
}
|
|
4576
|
+
}
|
|
4577
|
+
const diags = getDiagnostics(index, registry, resolvedIds);
|
|
4578
|
+
if (diags.length === 0) {
|
|
4579
|
+
console.log(source_default.green("\u2713 No broken or stale references found."));
|
|
4580
|
+
return;
|
|
4581
|
+
}
|
|
4582
|
+
const stale = diags.filter((d) => d.state === "stale");
|
|
4583
|
+
const broken = diags.filter((d) => d.state === "broken");
|
|
4584
|
+
if (broken.length > 0) {
|
|
4585
|
+
console.log(source_default.bold(source_default.red(`Broken references (${broken.length})
|
|
4586
|
+
`)));
|
|
4587
|
+
for (const d of broken) {
|
|
4588
|
+
console.log(` ${source_default.red("\u2717")} ${d.source.filePath}:${d.source.line} ${d.message}`);
|
|
4589
|
+
}
|
|
4590
|
+
console.log();
|
|
4591
|
+
}
|
|
4592
|
+
if (stale.length > 0) {
|
|
4593
|
+
console.log(source_default.bold(source_default.yellow(`Stale references (${stale.length})
|
|
4594
|
+
`)));
|
|
4595
|
+
for (const d of stale) {
|
|
4596
|
+
console.log(` ${source_default.yellow("\u26A0")} ${d.source.filePath}:${d.source.line} ${d.message}`);
|
|
4597
|
+
}
|
|
4598
|
+
}
|
|
4599
|
+
return;
|
|
4600
|
+
}
|
|
4601
|
+
if (file) {
|
|
4602
|
+
const refs = getOutgoingRefs(index, file);
|
|
4603
|
+
if (refs.length === 0) {
|
|
4604
|
+
console.log(source_default.dim(`No [[...]] references found in: ${file}`));
|
|
4605
|
+
return;
|
|
4606
|
+
}
|
|
4607
|
+
console.log(source_default.bold(`References in ${source_default.cyan(file)} (${refs.length})
|
|
4608
|
+
`));
|
|
4609
|
+
for (const ref of refs) {
|
|
4610
|
+
const resolved = resolveRef(ref, resolverCtx);
|
|
4611
|
+
const stateIcon = resolved.state === "resolved" ? source_default.green("\u2713") : resolved.state === "stale" ? source_default.yellow("\u26A0") : source_default.red("\u2717");
|
|
4612
|
+
const display = ref.alias ?? ref.raw;
|
|
4613
|
+
const entityId = resolved.entityId ?? source_default.dim("unresolved");
|
|
4614
|
+
console.log(` ${stateIcon} [[${display}]] \u2192 ${entityId} ${source_default.dim(`L${ref.source.line}`)}`);
|
|
4615
|
+
}
|
|
4616
|
+
} else {
|
|
4617
|
+
const stats = getIndexStats(index);
|
|
4618
|
+
if (stats.totalRefs === 0) {
|
|
4619
|
+
console.log(source_default.dim("No [[...]] references found in any tracked files."));
|
|
4620
|
+
return;
|
|
4621
|
+
}
|
|
4622
|
+
console.log(source_default.bold(`References (${stats.totalRefs} across ${stats.totalFiles} files)
|
|
4623
|
+
`));
|
|
4624
|
+
for (const [filePath, refs] of index.outgoing) {
|
|
4625
|
+
console.log(` ${source_default.cyan(filePath)} (${refs.length} refs)`);
|
|
4626
|
+
for (const ref of refs) {
|
|
4627
|
+
const resolved = resolveRef(ref, resolverCtx);
|
|
4628
|
+
const stateIcon = resolved.state === "resolved" ? source_default.green("\u2713") : resolved.state === "stale" ? source_default.yellow("\u26A0") : source_default.red("\u2717");
|
|
4629
|
+
console.log(` ${stateIcon} [[${ref.raw}]] ${source_default.dim(`L${ref.source.line}`)}`);
|
|
4630
|
+
}
|
|
4631
|
+
}
|
|
4632
|
+
}
|
|
4633
|
+
});
|
|
4634
|
+
program2.command("search").description("Semantic search across all embedded content").argument("<query>", "Natural language search query").option("-p, --path <path>", "Repository path", ".").option("-l, --limit <n>", "Max results", "10").option("-t, --type <types>", "Filter by chunk type(s), comma-separated (issue_title,issue_desc,milestone_msg,markdown,code_entity,doc_comment,summary_md)").action(async (query, opts) => {
|
|
4635
|
+
const rootPath = resolve(opts.path);
|
|
4636
|
+
if (!TrellisVcsEngine.isRepo(rootPath)) {
|
|
4637
|
+
console.error(source_default.red("Not a TrellisVCS repository. Run `trellis init` first."));
|
|
4638
|
+
process.exit(1);
|
|
4639
|
+
}
|
|
4640
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4641
|
+
engine.open();
|
|
4642
|
+
const { EmbeddingManager } = (init_embeddings(), __toCommonJS(exports_embeddings));
|
|
4643
|
+
const dbPath = join5(rootPath, ".trellis", "embeddings.db");
|
|
4644
|
+
const manager = new EmbeddingManager(dbPath);
|
|
4645
|
+
try {
|
|
4646
|
+
const searchOpts = {
|
|
4647
|
+
limit: parseInt(opts.limit, 10) || 10
|
|
4648
|
+
};
|
|
4649
|
+
if (opts.type) {
|
|
4650
|
+
searchOpts.types = opts.type.split(",").map((t) => t.trim());
|
|
4651
|
+
}
|
|
4652
|
+
const results = await manager.search(query, searchOpts);
|
|
4653
|
+
if (results.length === 0) {
|
|
4654
|
+
console.log(source_default.dim("No results found. Try `trellis reindex` to build the index."));
|
|
4655
|
+
return;
|
|
4656
|
+
}
|
|
4657
|
+
console.log(source_default.bold(`Search results for ${source_default.cyan(`"${query}"`)} (${results.length})
|
|
4658
|
+
`));
|
|
4659
|
+
for (const r of results) {
|
|
4660
|
+
const score = (r.score * 100).toFixed(1);
|
|
4661
|
+
const typeTag = source_default.dim(`[${r.chunk.chunkType}]`);
|
|
4662
|
+
const filePart = r.chunk.filePath ? source_default.dim(` ${r.chunk.filePath}`) : "";
|
|
4663
|
+
const preview = r.chunk.content.slice(0, 120).replace(/\n/g, " ");
|
|
4664
|
+
console.log(` ${source_default.green(`${score}%`)} ${typeTag}${filePart}`);
|
|
4665
|
+
console.log(` ${source_default.dim(preview)}`);
|
|
4666
|
+
console.log();
|
|
4667
|
+
}
|
|
4668
|
+
} finally {
|
|
4669
|
+
manager.close();
|
|
4670
|
+
}
|
|
4671
|
+
});
|
|
4672
|
+
program2.command("reindex").description("Rebuild the semantic embedding index").option("-p, --path <path>", "Repository path", ".").action(async (opts) => {
|
|
4673
|
+
const rootPath = resolve(opts.path);
|
|
4674
|
+
if (!TrellisVcsEngine.isRepo(rootPath)) {
|
|
4675
|
+
console.error(source_default.red("Not a TrellisVCS repository. Run `trellis init` first."));
|
|
4676
|
+
process.exit(1);
|
|
4677
|
+
}
|
|
4678
|
+
const engine = new TrellisVcsEngine({ rootPath });
|
|
4679
|
+
engine.open();
|
|
4680
|
+
const { EmbeddingManager } = (init_embeddings(), __toCommonJS(exports_embeddings));
|
|
4681
|
+
const dbPath = join5(rootPath, ".trellis", "embeddings.db");
|
|
4682
|
+
const manager = new EmbeddingManager(dbPath);
|
|
4683
|
+
try {
|
|
4684
|
+
console.log(source_default.dim("Loading embedding model\u2026"));
|
|
4685
|
+
const result = await manager.reindex(engine);
|
|
4686
|
+
console.log(source_default.green(`\u2713 Indexed ${result.chunks} chunks`));
|
|
4687
|
+
const stats = manager.stats();
|
|
4688
|
+
console.log(source_default.dim(` Types: ${JSON.stringify(stats.byType)}`));
|
|
4689
|
+
} finally {
|
|
4690
|
+
manager.close();
|
|
4691
|
+
}
|
|
4692
|
+
});
|
|
4693
|
+
function formatOpKind(kind) {
|
|
4694
|
+
const kindMap = {
|
|
4695
|
+
"vcs:fileAdd": source_default.green("+add"),
|
|
4696
|
+
"vcs:fileModify": source_default.yellow("~mod"),
|
|
4697
|
+
"vcs:fileDelete": source_default.red("-del"),
|
|
4698
|
+
"vcs:fileRename": source_default.blue("\u2192ren"),
|
|
4699
|
+
"vcs:branchCreate": source_default.magenta("\u2295branch"),
|
|
4700
|
+
"vcs:branchAdvance": source_default.magenta("\u2192branch"),
|
|
4701
|
+
"vcs:milestoneCreate": source_default.cyan("\u2605milestone"),
|
|
4702
|
+
"vcs:checkpointCreate": source_default.dim("\u25CFcheckpoint")
|
|
4703
|
+
};
|
|
4704
|
+
return kindMap[kind] ?? source_default.dim(kind);
|
|
4705
|
+
}
|
|
4706
|
+
function formatRelativeTime(iso) {
|
|
4707
|
+
const now = Date.now();
|
|
4708
|
+
const then = new Date(iso).getTime();
|
|
4709
|
+
const diff = now - then;
|
|
4710
|
+
if (diff < 60000)
|
|
4711
|
+
return "just now";
|
|
4712
|
+
if (diff < 3600000)
|
|
4713
|
+
return `${Math.floor(diff / 60000)}m ago`;
|
|
4714
|
+
if (diff < 86400000)
|
|
4715
|
+
return `${Math.floor(diff / 3600000)}h ago`;
|
|
4716
|
+
return `${Math.floor(diff / 86400000)}d ago`;
|
|
4717
|
+
}
|
|
4718
|
+
program2.parse();
|