@pixelfactoryio/theway 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +36 -2
- package/dist/theway.js +2674 -0
- package/package.json +33 -4
- package/plugin/.claude-plugin/plugin.json +9 -0
- package/plugin/commands/archive.md +89 -0
- package/plugin/commands/develop-workflow.md +106 -0
- package/plugin/commands/develop.md +38 -0
- package/plugin/commands/document.md +58 -0
- package/plugin/commands/engineering.md +84 -0
- package/plugin/commands/list.md +24 -0
- package/plugin/commands/setup.md +25 -0
- package/plugin/templates/theway/AGENTS.md +170 -0
- package/plugin/templates/theway/CLAUDE.md +17 -0
- package/plugin/templates/theway/changes/archive/.gitkeep +0 -0
- package/plugin/templates/theway/config.yaml +90 -0
- package/plugin/templates/theway/data/standards/architecture-principles.md +50 -0
- package/plugin/templates/theway/data/standards/develop.md +28 -0
- package/plugin/templates/theway/data/standards/interaction.md +13 -0
- package/plugin/templates/theway/data/standards/tamm-framework.md +82 -0
- package/plugin/templates/theway/project.md +31 -0
- package/plugin/templates/theway/templates/coding-standards-template.md +102 -0
- package/plugin/templates/theway/templates/source-tree-template.md +129 -0
- package/plugin/templates/theway/templates/spec-template.md +114 -0
- package/plugin/templates/theway/templates/tasks-template.md +86 -0
- package/plugin/templates/theway/templates/tech-stack-template.md +159 -0
package/dist/theway.js
ADDED
|
@@ -0,0 +1,2674 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// node_modules/commander/lib/error.js
|
|
4
|
+
class CommanderError extends Error {
|
|
5
|
+
constructor(exitCode, code, message) {
|
|
6
|
+
super(message);
|
|
7
|
+
Error.captureStackTrace(this, this.constructor);
|
|
8
|
+
this.name = this.constructor.name;
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.exitCode = exitCode;
|
|
11
|
+
this.nestedError = undefined;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
class InvalidArgumentError extends CommanderError {
|
|
16
|
+
constructor(message) {
|
|
17
|
+
super(1, "commander.invalidArgument", message);
|
|
18
|
+
Error.captureStackTrace(this, this.constructor);
|
|
19
|
+
this.name = this.constructor.name;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// node_modules/commander/lib/argument.js
|
|
24
|
+
class Argument {
|
|
25
|
+
constructor(name, description) {
|
|
26
|
+
this.description = description || "";
|
|
27
|
+
this.variadic = false;
|
|
28
|
+
this.parseArg = undefined;
|
|
29
|
+
this.defaultValue = undefined;
|
|
30
|
+
this.defaultValueDescription = undefined;
|
|
31
|
+
this.argChoices = undefined;
|
|
32
|
+
switch (name[0]) {
|
|
33
|
+
case "<":
|
|
34
|
+
this.required = true;
|
|
35
|
+
this._name = name.slice(1, -1);
|
|
36
|
+
break;
|
|
37
|
+
case "[":
|
|
38
|
+
this.required = false;
|
|
39
|
+
this._name = name.slice(1, -1);
|
|
40
|
+
break;
|
|
41
|
+
default:
|
|
42
|
+
this.required = true;
|
|
43
|
+
this._name = name;
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
if (this._name.endsWith("...")) {
|
|
47
|
+
this.variadic = true;
|
|
48
|
+
this._name = this._name.slice(0, -3);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
name() {
|
|
52
|
+
return this._name;
|
|
53
|
+
}
|
|
54
|
+
_collectValue(value, previous) {
|
|
55
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
56
|
+
return [value];
|
|
57
|
+
}
|
|
58
|
+
previous.push(value);
|
|
59
|
+
return previous;
|
|
60
|
+
}
|
|
61
|
+
default(value, description) {
|
|
62
|
+
this.defaultValue = value;
|
|
63
|
+
this.defaultValueDescription = description;
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
argParser(fn) {
|
|
67
|
+
this.parseArg = fn;
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
choices(values) {
|
|
71
|
+
this.argChoices = values.slice();
|
|
72
|
+
this.parseArg = (arg, previous) => {
|
|
73
|
+
if (!this.argChoices.includes(arg)) {
|
|
74
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
75
|
+
}
|
|
76
|
+
if (this.variadic) {
|
|
77
|
+
return this._collectValue(arg, previous);
|
|
78
|
+
}
|
|
79
|
+
return arg;
|
|
80
|
+
};
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
argRequired() {
|
|
84
|
+
this.required = true;
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
argOptional() {
|
|
88
|
+
this.required = false;
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function humanReadableArgName(arg) {
|
|
93
|
+
const nameOutput = arg.name() + (arg.variadic === true ? "..." : "");
|
|
94
|
+
return arg.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// node_modules/commander/lib/command.js
|
|
98
|
+
import { EventEmitter } from "node:events";
|
|
99
|
+
import childProcess from "node:child_process";
|
|
100
|
+
import path from "node:path";
|
|
101
|
+
import fs from "node:fs";
|
|
102
|
+
import process2 from "node:process";
|
|
103
|
+
import { stripVTControlCharacters as stripVTControlCharacters2 } from "node:util";
|
|
104
|
+
|
|
105
|
+
// node_modules/commander/lib/help.js
|
|
106
|
+
import { stripVTControlCharacters } from "node:util";
|
|
107
|
+
|
|
108
|
+
class Help {
|
|
109
|
+
constructor() {
|
|
110
|
+
this.helpWidth = undefined;
|
|
111
|
+
this.minWidthToWrap = 40;
|
|
112
|
+
this.sortSubcommands = false;
|
|
113
|
+
this.sortOptions = false;
|
|
114
|
+
this.showGlobalOptions = false;
|
|
115
|
+
}
|
|
116
|
+
prepareContext(contextOptions) {
|
|
117
|
+
this.helpWidth = this.helpWidth ?? contextOptions.helpWidth ?? 80;
|
|
118
|
+
}
|
|
119
|
+
visibleCommands(cmd) {
|
|
120
|
+
const visibleCommands = cmd.commands.filter((cmd2) => !cmd2._hidden);
|
|
121
|
+
const helpCommand = cmd._getHelpCommand();
|
|
122
|
+
if (helpCommand && !helpCommand._hidden) {
|
|
123
|
+
visibleCommands.push(helpCommand);
|
|
124
|
+
}
|
|
125
|
+
if (this.sortSubcommands) {
|
|
126
|
+
visibleCommands.sort((a, b) => {
|
|
127
|
+
return a.name().localeCompare(b.name());
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
return visibleCommands;
|
|
131
|
+
}
|
|
132
|
+
compareOptions(a, b) {
|
|
133
|
+
const getSortKey = (option) => {
|
|
134
|
+
return option.short ? option.short.replace(/^-/, "") : option.long.replace(/^--/, "");
|
|
135
|
+
};
|
|
136
|
+
return getSortKey(a).localeCompare(getSortKey(b));
|
|
137
|
+
}
|
|
138
|
+
visibleOptions(cmd) {
|
|
139
|
+
const visibleOptions = cmd.options.filter((option) => !option.hidden);
|
|
140
|
+
const helpOption = cmd._getHelpOption();
|
|
141
|
+
if (helpOption && !helpOption.hidden) {
|
|
142
|
+
const removeShort = helpOption.short && cmd._findOption(helpOption.short);
|
|
143
|
+
const removeLong = helpOption.long && cmd._findOption(helpOption.long);
|
|
144
|
+
if (!removeShort && !removeLong) {
|
|
145
|
+
visibleOptions.push(helpOption);
|
|
146
|
+
} else if (helpOption.long && !removeLong) {
|
|
147
|
+
visibleOptions.push(cmd.createOption(helpOption.long, helpOption.description));
|
|
148
|
+
} else if (helpOption.short && !removeShort) {
|
|
149
|
+
visibleOptions.push(cmd.createOption(helpOption.short, helpOption.description));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (this.sortOptions) {
|
|
153
|
+
visibleOptions.sort(this.compareOptions);
|
|
154
|
+
}
|
|
155
|
+
return visibleOptions;
|
|
156
|
+
}
|
|
157
|
+
visibleGlobalOptions(cmd) {
|
|
158
|
+
if (!this.showGlobalOptions)
|
|
159
|
+
return [];
|
|
160
|
+
const globalOptions = [];
|
|
161
|
+
for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
162
|
+
const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
|
|
163
|
+
globalOptions.push(...visibleOptions);
|
|
164
|
+
}
|
|
165
|
+
if (this.sortOptions) {
|
|
166
|
+
globalOptions.sort(this.compareOptions);
|
|
167
|
+
}
|
|
168
|
+
return globalOptions;
|
|
169
|
+
}
|
|
170
|
+
visibleArguments(cmd) {
|
|
171
|
+
if (cmd._argsDescription) {
|
|
172
|
+
cmd.registeredArguments.forEach((argument) => {
|
|
173
|
+
argument.description = argument.description || cmd._argsDescription[argument.name()] || "";
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
if (cmd.registeredArguments.find((argument) => argument.description)) {
|
|
177
|
+
return cmd.registeredArguments;
|
|
178
|
+
}
|
|
179
|
+
return [];
|
|
180
|
+
}
|
|
181
|
+
subcommandTerm(cmd) {
|
|
182
|
+
const args = cmd.registeredArguments.map((arg) => humanReadableArgName(arg)).join(" ");
|
|
183
|
+
return cmd._name + (cmd._aliases[0] ? "|" + cmd._aliases[0] : "") + (cmd.options.length ? " [options]" : "") + (args ? " " + args : "");
|
|
184
|
+
}
|
|
185
|
+
optionTerm(option) {
|
|
186
|
+
return option.flags;
|
|
187
|
+
}
|
|
188
|
+
argumentTerm(argument) {
|
|
189
|
+
return argument.name();
|
|
190
|
+
}
|
|
191
|
+
longestSubcommandTermLength(cmd, helper) {
|
|
192
|
+
return helper.visibleCommands(cmd).reduce((max, command) => {
|
|
193
|
+
return Math.max(max, this.displayWidth(helper.styleSubcommandTerm(helper.subcommandTerm(command))));
|
|
194
|
+
}, 0);
|
|
195
|
+
}
|
|
196
|
+
longestOptionTermLength(cmd, helper) {
|
|
197
|
+
return helper.visibleOptions(cmd).reduce((max, option) => {
|
|
198
|
+
return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
|
|
199
|
+
}, 0);
|
|
200
|
+
}
|
|
201
|
+
longestGlobalOptionTermLength(cmd, helper) {
|
|
202
|
+
return helper.visibleGlobalOptions(cmd).reduce((max, option) => {
|
|
203
|
+
return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
|
|
204
|
+
}, 0);
|
|
205
|
+
}
|
|
206
|
+
longestArgumentTermLength(cmd, helper) {
|
|
207
|
+
return helper.visibleArguments(cmd).reduce((max, argument) => {
|
|
208
|
+
return Math.max(max, this.displayWidth(helper.styleArgumentTerm(helper.argumentTerm(argument))));
|
|
209
|
+
}, 0);
|
|
210
|
+
}
|
|
211
|
+
commandUsage(cmd) {
|
|
212
|
+
let cmdName = cmd._name;
|
|
213
|
+
if (cmd._aliases[0]) {
|
|
214
|
+
cmdName = cmdName + "|" + cmd._aliases[0];
|
|
215
|
+
}
|
|
216
|
+
let ancestorCmdNames = "";
|
|
217
|
+
for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
218
|
+
ancestorCmdNames = ancestorCmd.name() + " " + ancestorCmdNames;
|
|
219
|
+
}
|
|
220
|
+
return ancestorCmdNames + cmdName + " " + cmd.usage();
|
|
221
|
+
}
|
|
222
|
+
commandDescription(cmd) {
|
|
223
|
+
return cmd.description();
|
|
224
|
+
}
|
|
225
|
+
subcommandDescription(cmd) {
|
|
226
|
+
return cmd.summary() || cmd.description();
|
|
227
|
+
}
|
|
228
|
+
optionDescription(option) {
|
|
229
|
+
const extraInfo = [];
|
|
230
|
+
if (option.argChoices) {
|
|
231
|
+
extraInfo.push(`choices: ${option.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
232
|
+
}
|
|
233
|
+
if (option.defaultValue !== undefined) {
|
|
234
|
+
const showDefault = option.required || option.optional || option.isBoolean() && typeof option.defaultValue === "boolean";
|
|
235
|
+
if (showDefault) {
|
|
236
|
+
extraInfo.push(`default: ${option.defaultValueDescription || JSON.stringify(option.defaultValue)}`);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (option.presetArg !== undefined && option.optional) {
|
|
240
|
+
extraInfo.push(`preset: ${JSON.stringify(option.presetArg)}`);
|
|
241
|
+
}
|
|
242
|
+
if (option.envVar !== undefined) {
|
|
243
|
+
extraInfo.push(`env: ${option.envVar}`);
|
|
244
|
+
}
|
|
245
|
+
if (extraInfo.length > 0) {
|
|
246
|
+
const extraDescription = `(${extraInfo.join(", ")})`;
|
|
247
|
+
if (option.description) {
|
|
248
|
+
return `${option.description} ${extraDescription}`;
|
|
249
|
+
}
|
|
250
|
+
return extraDescription;
|
|
251
|
+
}
|
|
252
|
+
return option.description;
|
|
253
|
+
}
|
|
254
|
+
argumentDescription(argument) {
|
|
255
|
+
const extraInfo = [];
|
|
256
|
+
if (argument.argChoices) {
|
|
257
|
+
extraInfo.push(`choices: ${argument.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
258
|
+
}
|
|
259
|
+
if (argument.defaultValue !== undefined) {
|
|
260
|
+
extraInfo.push(`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`);
|
|
261
|
+
}
|
|
262
|
+
if (extraInfo.length > 0) {
|
|
263
|
+
const extraDescription = `(${extraInfo.join(", ")})`;
|
|
264
|
+
if (argument.description) {
|
|
265
|
+
return `${argument.description} ${extraDescription}`;
|
|
266
|
+
}
|
|
267
|
+
return extraDescription;
|
|
268
|
+
}
|
|
269
|
+
return argument.description;
|
|
270
|
+
}
|
|
271
|
+
formatItemList(heading, items, helper) {
|
|
272
|
+
if (items.length === 0)
|
|
273
|
+
return [];
|
|
274
|
+
return [helper.styleTitle(heading), ...items, ""];
|
|
275
|
+
}
|
|
276
|
+
groupItems(unsortedItems, visibleItems, getGroup) {
|
|
277
|
+
const result = new Map;
|
|
278
|
+
unsortedItems.forEach((item) => {
|
|
279
|
+
const group = getGroup(item);
|
|
280
|
+
if (!result.has(group))
|
|
281
|
+
result.set(group, []);
|
|
282
|
+
});
|
|
283
|
+
visibleItems.forEach((item) => {
|
|
284
|
+
const group = getGroup(item);
|
|
285
|
+
if (!result.has(group)) {
|
|
286
|
+
result.set(group, []);
|
|
287
|
+
}
|
|
288
|
+
result.get(group).push(item);
|
|
289
|
+
});
|
|
290
|
+
return result;
|
|
291
|
+
}
|
|
292
|
+
formatHelp(cmd, helper) {
|
|
293
|
+
const termWidth = helper.padWidth(cmd, helper);
|
|
294
|
+
const helpWidth = helper.helpWidth ?? 80;
|
|
295
|
+
function callFormatItem(term, description) {
|
|
296
|
+
return helper.formatItem(term, termWidth, description, helper);
|
|
297
|
+
}
|
|
298
|
+
let output = [
|
|
299
|
+
`${helper.styleTitle("Usage:")} ${helper.styleUsage(helper.commandUsage(cmd))}`,
|
|
300
|
+
""
|
|
301
|
+
];
|
|
302
|
+
const commandDescription = helper.commandDescription(cmd);
|
|
303
|
+
if (commandDescription.length > 0) {
|
|
304
|
+
output = output.concat([
|
|
305
|
+
helper.boxWrap(helper.styleCommandDescription(commandDescription), helpWidth),
|
|
306
|
+
""
|
|
307
|
+
]);
|
|
308
|
+
}
|
|
309
|
+
const argumentList = helper.visibleArguments(cmd).map((argument) => {
|
|
310
|
+
return callFormatItem(helper.styleArgumentTerm(helper.argumentTerm(argument)), helper.styleArgumentDescription(helper.argumentDescription(argument)));
|
|
311
|
+
});
|
|
312
|
+
output = output.concat(this.formatItemList("Arguments:", argumentList, helper));
|
|
313
|
+
const optionGroups = this.groupItems(cmd.options, helper.visibleOptions(cmd), (option) => option.helpGroupHeading ?? "Options:");
|
|
314
|
+
optionGroups.forEach((options, group) => {
|
|
315
|
+
const optionList = options.map((option) => {
|
|
316
|
+
return callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option)));
|
|
317
|
+
});
|
|
318
|
+
output = output.concat(this.formatItemList(group, optionList, helper));
|
|
319
|
+
});
|
|
320
|
+
if (helper.showGlobalOptions) {
|
|
321
|
+
const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
|
|
322
|
+
return callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option)));
|
|
323
|
+
});
|
|
324
|
+
output = output.concat(this.formatItemList("Global Options:", globalOptionList, helper));
|
|
325
|
+
}
|
|
326
|
+
const commandGroups = this.groupItems(cmd.commands, helper.visibleCommands(cmd), (sub) => sub.helpGroup() || "Commands:");
|
|
327
|
+
commandGroups.forEach((commands, group) => {
|
|
328
|
+
const commandList = commands.map((sub) => {
|
|
329
|
+
return callFormatItem(helper.styleSubcommandTerm(helper.subcommandTerm(sub)), helper.styleSubcommandDescription(helper.subcommandDescription(sub)));
|
|
330
|
+
});
|
|
331
|
+
output = output.concat(this.formatItemList(group, commandList, helper));
|
|
332
|
+
});
|
|
333
|
+
return output.join(`
|
|
334
|
+
`);
|
|
335
|
+
}
|
|
336
|
+
displayWidth(str) {
|
|
337
|
+
return stripVTControlCharacters(str).length;
|
|
338
|
+
}
|
|
339
|
+
styleTitle(str) {
|
|
340
|
+
return str;
|
|
341
|
+
}
|
|
342
|
+
styleUsage(str) {
|
|
343
|
+
return str.split(" ").map((word) => {
|
|
344
|
+
if (word === "[options]")
|
|
345
|
+
return this.styleOptionText(word);
|
|
346
|
+
if (word === "[command]")
|
|
347
|
+
return this.styleSubcommandText(word);
|
|
348
|
+
if (word[0] === "[" || word[0] === "<")
|
|
349
|
+
return this.styleArgumentText(word);
|
|
350
|
+
return this.styleCommandText(word);
|
|
351
|
+
}).join(" ");
|
|
352
|
+
}
|
|
353
|
+
styleCommandDescription(str) {
|
|
354
|
+
return this.styleDescriptionText(str);
|
|
355
|
+
}
|
|
356
|
+
styleOptionDescription(str) {
|
|
357
|
+
return this.styleDescriptionText(str);
|
|
358
|
+
}
|
|
359
|
+
styleSubcommandDescription(str) {
|
|
360
|
+
return this.styleDescriptionText(str);
|
|
361
|
+
}
|
|
362
|
+
styleArgumentDescription(str) {
|
|
363
|
+
return this.styleDescriptionText(str);
|
|
364
|
+
}
|
|
365
|
+
styleDescriptionText(str) {
|
|
366
|
+
return str;
|
|
367
|
+
}
|
|
368
|
+
styleOptionTerm(str) {
|
|
369
|
+
return this.styleOptionText(str);
|
|
370
|
+
}
|
|
371
|
+
styleSubcommandTerm(str) {
|
|
372
|
+
return str.split(" ").map((word) => {
|
|
373
|
+
if (word === "[options]")
|
|
374
|
+
return this.styleOptionText(word);
|
|
375
|
+
if (word[0] === "[" || word[0] === "<")
|
|
376
|
+
return this.styleArgumentText(word);
|
|
377
|
+
return this.styleSubcommandText(word);
|
|
378
|
+
}).join(" ");
|
|
379
|
+
}
|
|
380
|
+
styleArgumentTerm(str) {
|
|
381
|
+
return this.styleArgumentText(str);
|
|
382
|
+
}
|
|
383
|
+
styleOptionText(str) {
|
|
384
|
+
return str;
|
|
385
|
+
}
|
|
386
|
+
styleArgumentText(str) {
|
|
387
|
+
return str;
|
|
388
|
+
}
|
|
389
|
+
styleSubcommandText(str) {
|
|
390
|
+
return str;
|
|
391
|
+
}
|
|
392
|
+
styleCommandText(str) {
|
|
393
|
+
return str;
|
|
394
|
+
}
|
|
395
|
+
padWidth(cmd, helper) {
|
|
396
|
+
return Math.max(helper.longestOptionTermLength(cmd, helper), helper.longestGlobalOptionTermLength(cmd, helper), helper.longestSubcommandTermLength(cmd, helper), helper.longestArgumentTermLength(cmd, helper));
|
|
397
|
+
}
|
|
398
|
+
preformatted(str) {
|
|
399
|
+
return /\n[^\S\r\n]/.test(str);
|
|
400
|
+
}
|
|
401
|
+
formatItem(term, termWidth, description, helper) {
|
|
402
|
+
const itemIndent = 2;
|
|
403
|
+
const itemIndentStr = " ".repeat(itemIndent);
|
|
404
|
+
if (!description)
|
|
405
|
+
return itemIndentStr + term;
|
|
406
|
+
const paddedTerm = term.padEnd(termWidth + term.length - helper.displayWidth(term));
|
|
407
|
+
const spacerWidth = 2;
|
|
408
|
+
const helpWidth = this.helpWidth ?? 80;
|
|
409
|
+
const remainingWidth = helpWidth - termWidth - spacerWidth - itemIndent;
|
|
410
|
+
let formattedDescription;
|
|
411
|
+
if (remainingWidth < this.minWidthToWrap || helper.preformatted(description)) {
|
|
412
|
+
formattedDescription = description;
|
|
413
|
+
} else {
|
|
414
|
+
const wrappedDescription = helper.boxWrap(description, remainingWidth);
|
|
415
|
+
formattedDescription = wrappedDescription.replace(/\n/g, `
|
|
416
|
+
` + " ".repeat(termWidth + spacerWidth));
|
|
417
|
+
}
|
|
418
|
+
return itemIndentStr + paddedTerm + " ".repeat(spacerWidth) + formattedDescription.replace(/\n/g, `
|
|
419
|
+
${itemIndentStr}`);
|
|
420
|
+
}
|
|
421
|
+
boxWrap(str, width) {
|
|
422
|
+
if (width < this.minWidthToWrap)
|
|
423
|
+
return str;
|
|
424
|
+
const rawLines = str.split(/\r\n|\n/);
|
|
425
|
+
const chunkPattern = /[\s]*[^\s]+/g;
|
|
426
|
+
const wrappedLines = [];
|
|
427
|
+
rawLines.forEach((line) => {
|
|
428
|
+
const chunks = line.match(chunkPattern);
|
|
429
|
+
if (chunks === null) {
|
|
430
|
+
wrappedLines.push("");
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
let sumChunks = [chunks.shift()];
|
|
434
|
+
let sumWidth = this.displayWidth(sumChunks[0]);
|
|
435
|
+
chunks.forEach((chunk) => {
|
|
436
|
+
const visibleWidth = this.displayWidth(chunk);
|
|
437
|
+
if (sumWidth + visibleWidth <= width) {
|
|
438
|
+
sumChunks.push(chunk);
|
|
439
|
+
sumWidth += visibleWidth;
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
wrappedLines.push(sumChunks.join(""));
|
|
443
|
+
const nextChunk = chunk.trimStart();
|
|
444
|
+
sumChunks = [nextChunk];
|
|
445
|
+
sumWidth = this.displayWidth(nextChunk);
|
|
446
|
+
});
|
|
447
|
+
wrappedLines.push(sumChunks.join(""));
|
|
448
|
+
});
|
|
449
|
+
return wrappedLines.join(`
|
|
450
|
+
`);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// node_modules/commander/lib/option.js
|
|
455
|
+
class Option {
|
|
456
|
+
constructor(flags, description) {
|
|
457
|
+
this.flags = flags;
|
|
458
|
+
this.description = description || "";
|
|
459
|
+
this.required = flags.includes("<");
|
|
460
|
+
this.optional = flags.includes("[");
|
|
461
|
+
this.variadic = /\w\.\.\.[>\]]$/.test(flags);
|
|
462
|
+
this.mandatory = false;
|
|
463
|
+
const optionFlags = splitOptionFlags(flags);
|
|
464
|
+
this.short = optionFlags.shortFlag;
|
|
465
|
+
this.long = optionFlags.longFlag;
|
|
466
|
+
this.negate = false;
|
|
467
|
+
if (this.long) {
|
|
468
|
+
this.negate = this.long.startsWith("--no-");
|
|
469
|
+
}
|
|
470
|
+
this.defaultValue = undefined;
|
|
471
|
+
this.defaultValueDescription = undefined;
|
|
472
|
+
this.presetArg = undefined;
|
|
473
|
+
this.envVar = undefined;
|
|
474
|
+
this.parseArg = undefined;
|
|
475
|
+
this.hidden = false;
|
|
476
|
+
this.argChoices = undefined;
|
|
477
|
+
this.conflictsWith = [];
|
|
478
|
+
this.implied = undefined;
|
|
479
|
+
this.helpGroupHeading = undefined;
|
|
480
|
+
}
|
|
481
|
+
default(value, description) {
|
|
482
|
+
this.defaultValue = value;
|
|
483
|
+
this.defaultValueDescription = description;
|
|
484
|
+
return this;
|
|
485
|
+
}
|
|
486
|
+
preset(arg) {
|
|
487
|
+
this.presetArg = arg;
|
|
488
|
+
return this;
|
|
489
|
+
}
|
|
490
|
+
conflicts(names) {
|
|
491
|
+
this.conflictsWith = this.conflictsWith.concat(names);
|
|
492
|
+
return this;
|
|
493
|
+
}
|
|
494
|
+
implies(impliedOptionValues) {
|
|
495
|
+
let newImplied = impliedOptionValues;
|
|
496
|
+
if (typeof impliedOptionValues === "string") {
|
|
497
|
+
newImplied = { [impliedOptionValues]: true };
|
|
498
|
+
}
|
|
499
|
+
this.implied = Object.assign(this.implied || {}, newImplied);
|
|
500
|
+
return this;
|
|
501
|
+
}
|
|
502
|
+
env(name) {
|
|
503
|
+
this.envVar = name;
|
|
504
|
+
return this;
|
|
505
|
+
}
|
|
506
|
+
argParser(fn) {
|
|
507
|
+
this.parseArg = fn;
|
|
508
|
+
return this;
|
|
509
|
+
}
|
|
510
|
+
makeOptionMandatory(mandatory = true) {
|
|
511
|
+
this.mandatory = !!mandatory;
|
|
512
|
+
return this;
|
|
513
|
+
}
|
|
514
|
+
hideHelp(hide = true) {
|
|
515
|
+
this.hidden = !!hide;
|
|
516
|
+
return this;
|
|
517
|
+
}
|
|
518
|
+
_collectValue(value, previous) {
|
|
519
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
520
|
+
return [value];
|
|
521
|
+
}
|
|
522
|
+
previous.push(value);
|
|
523
|
+
return previous;
|
|
524
|
+
}
|
|
525
|
+
choices(values) {
|
|
526
|
+
this.argChoices = values.slice();
|
|
527
|
+
this.parseArg = (arg, previous) => {
|
|
528
|
+
if (!this.argChoices.includes(arg)) {
|
|
529
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
530
|
+
}
|
|
531
|
+
if (this.variadic) {
|
|
532
|
+
return this._collectValue(arg, previous);
|
|
533
|
+
}
|
|
534
|
+
return arg;
|
|
535
|
+
};
|
|
536
|
+
return this;
|
|
537
|
+
}
|
|
538
|
+
name() {
|
|
539
|
+
if (this.long) {
|
|
540
|
+
return this.long.replace(/^--/, "");
|
|
541
|
+
}
|
|
542
|
+
return this.short.replace(/^-/, "");
|
|
543
|
+
}
|
|
544
|
+
attributeName() {
|
|
545
|
+
if (this.negate) {
|
|
546
|
+
return camelcase(this.name().replace(/^no-/, ""));
|
|
547
|
+
}
|
|
548
|
+
return camelcase(this.name());
|
|
549
|
+
}
|
|
550
|
+
helpGroup(heading) {
|
|
551
|
+
this.helpGroupHeading = heading;
|
|
552
|
+
return this;
|
|
553
|
+
}
|
|
554
|
+
is(arg) {
|
|
555
|
+
return this.short === arg || this.long === arg;
|
|
556
|
+
}
|
|
557
|
+
isBoolean() {
|
|
558
|
+
return !this.required && !this.optional && !this.negate;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
class DualOptions {
|
|
563
|
+
constructor(options) {
|
|
564
|
+
this.positiveOptions = new Map;
|
|
565
|
+
this.negativeOptions = new Map;
|
|
566
|
+
this.dualOptions = new Set;
|
|
567
|
+
options.forEach((option) => {
|
|
568
|
+
if (option.negate) {
|
|
569
|
+
this.negativeOptions.set(option.attributeName(), option);
|
|
570
|
+
} else {
|
|
571
|
+
this.positiveOptions.set(option.attributeName(), option);
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
this.negativeOptions.forEach((value, key) => {
|
|
575
|
+
if (this.positiveOptions.has(key)) {
|
|
576
|
+
this.dualOptions.add(key);
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
valueFromOption(value, option) {
|
|
581
|
+
const optionKey = option.attributeName();
|
|
582
|
+
if (!this.dualOptions.has(optionKey))
|
|
583
|
+
return true;
|
|
584
|
+
const preset = this.negativeOptions.get(optionKey).presetArg;
|
|
585
|
+
const negativeValue = preset !== undefined ? preset : false;
|
|
586
|
+
return option.negate === (negativeValue === value);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
function camelcase(str) {
|
|
590
|
+
return str.split("-").reduce((str2, word) => {
|
|
591
|
+
return str2 + word[0].toUpperCase() + word.slice(1);
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
function splitOptionFlags(flags) {
|
|
595
|
+
let shortFlag;
|
|
596
|
+
let longFlag;
|
|
597
|
+
const shortFlagExp = /^-[^-]$/;
|
|
598
|
+
const longFlagExp = /^--[^-]/;
|
|
599
|
+
const flagParts = flags.split(/[ |,]+/).concat("guard");
|
|
600
|
+
if (shortFlagExp.test(flagParts[0]))
|
|
601
|
+
shortFlag = flagParts.shift();
|
|
602
|
+
if (longFlagExp.test(flagParts[0]))
|
|
603
|
+
longFlag = flagParts.shift();
|
|
604
|
+
if (!shortFlag && shortFlagExp.test(flagParts[0]))
|
|
605
|
+
shortFlag = flagParts.shift();
|
|
606
|
+
if (!shortFlag && longFlagExp.test(flagParts[0])) {
|
|
607
|
+
shortFlag = longFlag;
|
|
608
|
+
longFlag = flagParts.shift();
|
|
609
|
+
}
|
|
610
|
+
if (flagParts[0].startsWith("-")) {
|
|
611
|
+
const unsupportedFlag = flagParts[0];
|
|
612
|
+
const baseError = `option creation failed due to '${unsupportedFlag}' in option flags '${flags}'`;
|
|
613
|
+
if (/^-[^-][^-]/.test(unsupportedFlag))
|
|
614
|
+
throw new Error(`${baseError}
|
|
615
|
+
- a short flag is a single dash and a single character
|
|
616
|
+
- either use a single dash and a single character (for a short flag)
|
|
617
|
+
- or use a double dash for a long option (and can have two, like '--ws, --workspace')`);
|
|
618
|
+
if (shortFlagExp.test(unsupportedFlag))
|
|
619
|
+
throw new Error(`${baseError}
|
|
620
|
+
- too many short flags`);
|
|
621
|
+
if (longFlagExp.test(unsupportedFlag))
|
|
622
|
+
throw new Error(`${baseError}
|
|
623
|
+
- too many long flags`);
|
|
624
|
+
throw new Error(`${baseError}
|
|
625
|
+
- unrecognised flag format`);
|
|
626
|
+
}
|
|
627
|
+
if (shortFlag === undefined && longFlag === undefined)
|
|
628
|
+
throw new Error(`option creation failed due to no flags found in '${flags}'.`);
|
|
629
|
+
return { shortFlag, longFlag };
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// node_modules/commander/lib/suggestSimilar.js
|
|
633
|
+
var maxDistance = 3;
|
|
634
|
+
function editDistance(a, b) {
|
|
635
|
+
if (Math.abs(a.length - b.length) > maxDistance)
|
|
636
|
+
return Math.max(a.length, b.length);
|
|
637
|
+
const d = [];
|
|
638
|
+
for (let i = 0;i <= a.length; i++) {
|
|
639
|
+
d[i] = [i];
|
|
640
|
+
}
|
|
641
|
+
for (let j = 0;j <= b.length; j++) {
|
|
642
|
+
d[0][j] = j;
|
|
643
|
+
}
|
|
644
|
+
for (let j = 1;j <= b.length; j++) {
|
|
645
|
+
for (let i = 1;i <= a.length; i++) {
|
|
646
|
+
let cost;
|
|
647
|
+
if (a[i - 1] === b[j - 1]) {
|
|
648
|
+
cost = 0;
|
|
649
|
+
} else {
|
|
650
|
+
cost = 1;
|
|
651
|
+
}
|
|
652
|
+
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
|
|
653
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
654
|
+
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
return d[a.length][b.length];
|
|
659
|
+
}
|
|
660
|
+
function suggestSimilar(word, candidates) {
|
|
661
|
+
if (!candidates || candidates.length === 0)
|
|
662
|
+
return "";
|
|
663
|
+
candidates = Array.from(new Set(candidates));
|
|
664
|
+
const searchingOptions = word.startsWith("--");
|
|
665
|
+
if (searchingOptions) {
|
|
666
|
+
word = word.slice(2);
|
|
667
|
+
candidates = candidates.map((candidate) => candidate.slice(2));
|
|
668
|
+
}
|
|
669
|
+
let similar = [];
|
|
670
|
+
let bestDistance = maxDistance;
|
|
671
|
+
const minSimilarity = 0.4;
|
|
672
|
+
candidates.forEach((candidate) => {
|
|
673
|
+
if (candidate.length <= 1)
|
|
674
|
+
return;
|
|
675
|
+
const distance = editDistance(word, candidate);
|
|
676
|
+
const length = Math.max(word.length, candidate.length);
|
|
677
|
+
const similarity = (length - distance) / length;
|
|
678
|
+
if (similarity > minSimilarity) {
|
|
679
|
+
if (distance < bestDistance) {
|
|
680
|
+
bestDistance = distance;
|
|
681
|
+
similar = [candidate];
|
|
682
|
+
} else if (distance === bestDistance) {
|
|
683
|
+
similar.push(candidate);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
});
|
|
687
|
+
similar.sort((a, b) => a.localeCompare(b));
|
|
688
|
+
if (searchingOptions) {
|
|
689
|
+
similar = similar.map((candidate) => `--${candidate}`);
|
|
690
|
+
}
|
|
691
|
+
if (similar.length > 1) {
|
|
692
|
+
return `
|
|
693
|
+
(Did you mean one of ${similar.join(", ")}?)`;
|
|
694
|
+
}
|
|
695
|
+
if (similar.length === 1) {
|
|
696
|
+
return `
|
|
697
|
+
(Did you mean ${similar[0]}?)`;
|
|
698
|
+
}
|
|
699
|
+
return "";
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
// node_modules/commander/lib/command.js
|
|
703
|
+
class Command extends EventEmitter {
|
|
704
|
+
constructor(name) {
|
|
705
|
+
super();
|
|
706
|
+
this.commands = [];
|
|
707
|
+
this.options = [];
|
|
708
|
+
this.parent = null;
|
|
709
|
+
this._allowUnknownOption = false;
|
|
710
|
+
this._allowExcessArguments = false;
|
|
711
|
+
this.registeredArguments = [];
|
|
712
|
+
this._args = this.registeredArguments;
|
|
713
|
+
this.args = [];
|
|
714
|
+
this.rawArgs = [];
|
|
715
|
+
this.processedArgs = [];
|
|
716
|
+
this._scriptPath = null;
|
|
717
|
+
this._name = name || "";
|
|
718
|
+
this._optionValues = {};
|
|
719
|
+
this._optionValueSources = {};
|
|
720
|
+
this._storeOptionsAsProperties = false;
|
|
721
|
+
this._actionHandler = null;
|
|
722
|
+
this._executableHandler = false;
|
|
723
|
+
this._executableFile = null;
|
|
724
|
+
this._executableDir = null;
|
|
725
|
+
this._defaultCommandName = null;
|
|
726
|
+
this._exitCallback = null;
|
|
727
|
+
this._aliases = [];
|
|
728
|
+
this._combineFlagAndOptionalValue = true;
|
|
729
|
+
this._description = "";
|
|
730
|
+
this._summary = "";
|
|
731
|
+
this._argsDescription = undefined;
|
|
732
|
+
this._enablePositionalOptions = false;
|
|
733
|
+
this._passThroughOptions = false;
|
|
734
|
+
this._lifeCycleHooks = {};
|
|
735
|
+
this._showHelpAfterError = false;
|
|
736
|
+
this._showSuggestionAfterError = true;
|
|
737
|
+
this._savedState = null;
|
|
738
|
+
this._outputConfiguration = {
|
|
739
|
+
writeOut: (str) => process2.stdout.write(str),
|
|
740
|
+
writeErr: (str) => process2.stderr.write(str),
|
|
741
|
+
outputError: (str, write) => write(str),
|
|
742
|
+
getOutHelpWidth: () => process2.stdout.isTTY ? process2.stdout.columns : undefined,
|
|
743
|
+
getErrHelpWidth: () => process2.stderr.isTTY ? process2.stderr.columns : undefined,
|
|
744
|
+
getOutHasColors: () => useColor() ?? (process2.stdout.isTTY && process2.stdout.hasColors?.()),
|
|
745
|
+
getErrHasColors: () => useColor() ?? (process2.stderr.isTTY && process2.stderr.hasColors?.()),
|
|
746
|
+
stripColor: (str) => stripVTControlCharacters2(str)
|
|
747
|
+
};
|
|
748
|
+
this._hidden = false;
|
|
749
|
+
this._helpOption = undefined;
|
|
750
|
+
this._addImplicitHelpCommand = undefined;
|
|
751
|
+
this._helpCommand = undefined;
|
|
752
|
+
this._helpConfiguration = {};
|
|
753
|
+
this._helpGroupHeading = undefined;
|
|
754
|
+
this._defaultCommandGroup = undefined;
|
|
755
|
+
this._defaultOptionGroup = undefined;
|
|
756
|
+
}
|
|
757
|
+
copyInheritedSettings(sourceCommand) {
|
|
758
|
+
this._outputConfiguration = sourceCommand._outputConfiguration;
|
|
759
|
+
this._helpOption = sourceCommand._helpOption;
|
|
760
|
+
this._helpCommand = sourceCommand._helpCommand;
|
|
761
|
+
this._helpConfiguration = sourceCommand._helpConfiguration;
|
|
762
|
+
this._exitCallback = sourceCommand._exitCallback;
|
|
763
|
+
this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
|
|
764
|
+
this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
|
|
765
|
+
this._allowExcessArguments = sourceCommand._allowExcessArguments;
|
|
766
|
+
this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
|
|
767
|
+
this._showHelpAfterError = sourceCommand._showHelpAfterError;
|
|
768
|
+
this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;
|
|
769
|
+
return this;
|
|
770
|
+
}
|
|
771
|
+
_getCommandAndAncestors() {
|
|
772
|
+
const result = [];
|
|
773
|
+
for (let command = this;command; command = command.parent) {
|
|
774
|
+
result.push(command);
|
|
775
|
+
}
|
|
776
|
+
return result;
|
|
777
|
+
}
|
|
778
|
+
command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
|
|
779
|
+
let desc = actionOptsOrExecDesc;
|
|
780
|
+
let opts = execOpts;
|
|
781
|
+
if (typeof desc === "object" && desc !== null) {
|
|
782
|
+
opts = desc;
|
|
783
|
+
desc = null;
|
|
784
|
+
}
|
|
785
|
+
opts = opts || {};
|
|
786
|
+
const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
|
|
787
|
+
const cmd = this.createCommand(name);
|
|
788
|
+
if (desc) {
|
|
789
|
+
cmd.description(desc);
|
|
790
|
+
cmd._executableHandler = true;
|
|
791
|
+
}
|
|
792
|
+
if (opts.isDefault)
|
|
793
|
+
this._defaultCommandName = cmd._name;
|
|
794
|
+
cmd._hidden = !!(opts.noHelp || opts.hidden);
|
|
795
|
+
cmd._executableFile = opts.executableFile || null;
|
|
796
|
+
if (args)
|
|
797
|
+
cmd.arguments(args);
|
|
798
|
+
this._registerCommand(cmd);
|
|
799
|
+
cmd.parent = this;
|
|
800
|
+
cmd.copyInheritedSettings(this);
|
|
801
|
+
if (desc)
|
|
802
|
+
return this;
|
|
803
|
+
return cmd;
|
|
804
|
+
}
|
|
805
|
+
createCommand(name) {
|
|
806
|
+
return new Command(name);
|
|
807
|
+
}
|
|
808
|
+
createHelp() {
|
|
809
|
+
return Object.assign(new Help, this.configureHelp());
|
|
810
|
+
}
|
|
811
|
+
configureHelp(configuration) {
|
|
812
|
+
if (configuration === undefined)
|
|
813
|
+
return this._helpConfiguration;
|
|
814
|
+
this._helpConfiguration = configuration;
|
|
815
|
+
return this;
|
|
816
|
+
}
|
|
817
|
+
configureOutput(configuration) {
|
|
818
|
+
if (configuration === undefined)
|
|
819
|
+
return this._outputConfiguration;
|
|
820
|
+
this._outputConfiguration = {
|
|
821
|
+
...this._outputConfiguration,
|
|
822
|
+
...configuration
|
|
823
|
+
};
|
|
824
|
+
return this;
|
|
825
|
+
}
|
|
826
|
+
showHelpAfterError(displayHelp = true) {
|
|
827
|
+
if (typeof displayHelp !== "string")
|
|
828
|
+
displayHelp = !!displayHelp;
|
|
829
|
+
this._showHelpAfterError = displayHelp;
|
|
830
|
+
return this;
|
|
831
|
+
}
|
|
832
|
+
showSuggestionAfterError(displaySuggestion = true) {
|
|
833
|
+
this._showSuggestionAfterError = !!displaySuggestion;
|
|
834
|
+
return this;
|
|
835
|
+
}
|
|
836
|
+
addCommand(cmd, opts) {
|
|
837
|
+
if (!cmd._name) {
|
|
838
|
+
throw new Error(`Command passed to .addCommand() must have a name
|
|
839
|
+
- specify the name in Command constructor or using .name()`);
|
|
840
|
+
}
|
|
841
|
+
opts = opts || {};
|
|
842
|
+
if (opts.isDefault)
|
|
843
|
+
this._defaultCommandName = cmd._name;
|
|
844
|
+
if (opts.noHelp || opts.hidden)
|
|
845
|
+
cmd._hidden = true;
|
|
846
|
+
this._registerCommand(cmd);
|
|
847
|
+
cmd.parent = this;
|
|
848
|
+
cmd._checkForBrokenPassThrough();
|
|
849
|
+
return this;
|
|
850
|
+
}
|
|
851
|
+
createArgument(name, description) {
|
|
852
|
+
return new Argument(name, description);
|
|
853
|
+
}
|
|
854
|
+
argument(name, description, parseArg, defaultValue) {
|
|
855
|
+
const argument = this.createArgument(name, description);
|
|
856
|
+
if (typeof parseArg === "function") {
|
|
857
|
+
argument.default(defaultValue).argParser(parseArg);
|
|
858
|
+
} else {
|
|
859
|
+
argument.default(parseArg);
|
|
860
|
+
}
|
|
861
|
+
this.addArgument(argument);
|
|
862
|
+
return this;
|
|
863
|
+
}
|
|
864
|
+
arguments(names) {
|
|
865
|
+
names.trim().split(/ +/).forEach((detail) => {
|
|
866
|
+
this.argument(detail);
|
|
867
|
+
});
|
|
868
|
+
return this;
|
|
869
|
+
}
|
|
870
|
+
addArgument(argument) {
|
|
871
|
+
const previousArgument = this.registeredArguments.slice(-1)[0];
|
|
872
|
+
if (previousArgument?.variadic) {
|
|
873
|
+
throw new Error(`only the last argument can be variadic '${previousArgument.name()}'`);
|
|
874
|
+
}
|
|
875
|
+
if (argument.required && argument.defaultValue !== undefined && argument.parseArg === undefined) {
|
|
876
|
+
throw new Error(`a default value for a required argument is never used: '${argument.name()}'`);
|
|
877
|
+
}
|
|
878
|
+
this.registeredArguments.push(argument);
|
|
879
|
+
return this;
|
|
880
|
+
}
|
|
881
|
+
helpCommand(enableOrNameAndArgs, description) {
|
|
882
|
+
if (typeof enableOrNameAndArgs === "boolean") {
|
|
883
|
+
this._addImplicitHelpCommand = enableOrNameAndArgs;
|
|
884
|
+
if (enableOrNameAndArgs && this._defaultCommandGroup) {
|
|
885
|
+
this._initCommandGroup(this._getHelpCommand());
|
|
886
|
+
}
|
|
887
|
+
return this;
|
|
888
|
+
}
|
|
889
|
+
const nameAndArgs = enableOrNameAndArgs ?? "help [command]";
|
|
890
|
+
const [, helpName, helpArgs] = nameAndArgs.match(/([^ ]+) *(.*)/);
|
|
891
|
+
const helpDescription = description ?? "display help for command";
|
|
892
|
+
const helpCommand = this.createCommand(helpName);
|
|
893
|
+
helpCommand.helpOption(false);
|
|
894
|
+
if (helpArgs)
|
|
895
|
+
helpCommand.arguments(helpArgs);
|
|
896
|
+
if (helpDescription)
|
|
897
|
+
helpCommand.description(helpDescription);
|
|
898
|
+
this._addImplicitHelpCommand = true;
|
|
899
|
+
this._helpCommand = helpCommand;
|
|
900
|
+
if (enableOrNameAndArgs || description)
|
|
901
|
+
this._initCommandGroup(helpCommand);
|
|
902
|
+
return this;
|
|
903
|
+
}
|
|
904
|
+
addHelpCommand(helpCommand, deprecatedDescription) {
|
|
905
|
+
if (typeof helpCommand !== "object") {
|
|
906
|
+
this.helpCommand(helpCommand, deprecatedDescription);
|
|
907
|
+
return this;
|
|
908
|
+
}
|
|
909
|
+
this._addImplicitHelpCommand = true;
|
|
910
|
+
this._helpCommand = helpCommand;
|
|
911
|
+
this._initCommandGroup(helpCommand);
|
|
912
|
+
return this;
|
|
913
|
+
}
|
|
914
|
+
_getHelpCommand() {
|
|
915
|
+
const hasImplicitHelpCommand = this._addImplicitHelpCommand ?? (this.commands.length && !this._actionHandler && !this._findCommand("help"));
|
|
916
|
+
if (hasImplicitHelpCommand) {
|
|
917
|
+
if (this._helpCommand === undefined) {
|
|
918
|
+
this.helpCommand(undefined, undefined);
|
|
919
|
+
}
|
|
920
|
+
return this._helpCommand;
|
|
921
|
+
}
|
|
922
|
+
return null;
|
|
923
|
+
}
|
|
924
|
+
hook(event, listener) {
|
|
925
|
+
const allowedValues = ["preSubcommand", "preAction", "postAction"];
|
|
926
|
+
if (!allowedValues.includes(event)) {
|
|
927
|
+
throw new Error(`Unexpected value for event passed to hook : '${event}'.
|
|
928
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
929
|
+
}
|
|
930
|
+
if (this._lifeCycleHooks[event]) {
|
|
931
|
+
this._lifeCycleHooks[event].push(listener);
|
|
932
|
+
} else {
|
|
933
|
+
this._lifeCycleHooks[event] = [listener];
|
|
934
|
+
}
|
|
935
|
+
return this;
|
|
936
|
+
}
|
|
937
|
+
exitOverride(fn) {
|
|
938
|
+
if (fn) {
|
|
939
|
+
this._exitCallback = fn;
|
|
940
|
+
} else {
|
|
941
|
+
this._exitCallback = (err) => {
|
|
942
|
+
if (err.code !== "commander.executeSubCommandAsync") {
|
|
943
|
+
throw err;
|
|
944
|
+
}
|
|
945
|
+
};
|
|
946
|
+
}
|
|
947
|
+
return this;
|
|
948
|
+
}
|
|
949
|
+
_exit(exitCode, code, message) {
|
|
950
|
+
if (this._exitCallback) {
|
|
951
|
+
this._exitCallback(new CommanderError(exitCode, code, message));
|
|
952
|
+
}
|
|
953
|
+
process2.exit(exitCode);
|
|
954
|
+
}
|
|
955
|
+
action(fn) {
|
|
956
|
+
const listener = (args) => {
|
|
957
|
+
const expectedArgsCount = this.registeredArguments.length;
|
|
958
|
+
const actionArgs = args.slice(0, expectedArgsCount);
|
|
959
|
+
if (this._storeOptionsAsProperties) {
|
|
960
|
+
actionArgs[expectedArgsCount] = this;
|
|
961
|
+
} else {
|
|
962
|
+
actionArgs[expectedArgsCount] = this.opts();
|
|
963
|
+
}
|
|
964
|
+
actionArgs.push(this);
|
|
965
|
+
return fn.apply(this, actionArgs);
|
|
966
|
+
};
|
|
967
|
+
this._actionHandler = listener;
|
|
968
|
+
return this;
|
|
969
|
+
}
|
|
970
|
+
createOption(flags, description) {
|
|
971
|
+
return new Option(flags, description);
|
|
972
|
+
}
|
|
973
|
+
_callParseArg(target, value, previous, invalidArgumentMessage) {
|
|
974
|
+
try {
|
|
975
|
+
return target.parseArg(value, previous);
|
|
976
|
+
} catch (err) {
|
|
977
|
+
if (err.code === "commander.invalidArgument") {
|
|
978
|
+
const message = `${invalidArgumentMessage} ${err.message}`;
|
|
979
|
+
this.error(message, { exitCode: err.exitCode, code: err.code });
|
|
980
|
+
}
|
|
981
|
+
throw err;
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
_registerOption(option) {
|
|
985
|
+
const matchingOption = option.short && this._findOption(option.short) || option.long && this._findOption(option.long);
|
|
986
|
+
if (matchingOption) {
|
|
987
|
+
const matchingFlag = option.long && this._findOption(option.long) ? option.long : option.short;
|
|
988
|
+
throw new Error(`Cannot add option '${option.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'
|
|
989
|
+
- already used by option '${matchingOption.flags}'`);
|
|
990
|
+
}
|
|
991
|
+
this._initOptionGroup(option);
|
|
992
|
+
this.options.push(option);
|
|
993
|
+
}
|
|
994
|
+
_registerCommand(command) {
|
|
995
|
+
const knownBy = (cmd) => {
|
|
996
|
+
return [cmd.name()].concat(cmd.aliases());
|
|
997
|
+
};
|
|
998
|
+
const alreadyUsed = knownBy(command).find((name) => this._findCommand(name));
|
|
999
|
+
if (alreadyUsed) {
|
|
1000
|
+
const existingCmd = knownBy(this._findCommand(alreadyUsed)).join("|");
|
|
1001
|
+
const newCmd = knownBy(command).join("|");
|
|
1002
|
+
throw new Error(`cannot add command '${newCmd}' as already have command '${existingCmd}'`);
|
|
1003
|
+
}
|
|
1004
|
+
this._initCommandGroup(command);
|
|
1005
|
+
this.commands.push(command);
|
|
1006
|
+
}
|
|
1007
|
+
addOption(option) {
|
|
1008
|
+
this._registerOption(option);
|
|
1009
|
+
const oname = option.name();
|
|
1010
|
+
const name = option.attributeName();
|
|
1011
|
+
if (option.defaultValue !== undefined) {
|
|
1012
|
+
this.setOptionValueWithSource(name, option.defaultValue, "default");
|
|
1013
|
+
}
|
|
1014
|
+
const handleOptionValue = (val, invalidValueMessage, valueSource) => {
|
|
1015
|
+
if (val == null && option.presetArg !== undefined) {
|
|
1016
|
+
val = option.presetArg;
|
|
1017
|
+
}
|
|
1018
|
+
const oldValue = this.getOptionValue(name);
|
|
1019
|
+
if (val !== null && option.parseArg) {
|
|
1020
|
+
val = this._callParseArg(option, val, oldValue, invalidValueMessage);
|
|
1021
|
+
} else if (val !== null && option.variadic) {
|
|
1022
|
+
val = option._collectValue(val, oldValue);
|
|
1023
|
+
}
|
|
1024
|
+
if (val == null) {
|
|
1025
|
+
if (option.negate) {
|
|
1026
|
+
val = false;
|
|
1027
|
+
} else if (option.isBoolean() || option.optional) {
|
|
1028
|
+
val = true;
|
|
1029
|
+
} else {
|
|
1030
|
+
val = "";
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
this.setOptionValueWithSource(name, val, valueSource);
|
|
1034
|
+
};
|
|
1035
|
+
this.on("option:" + oname, (val) => {
|
|
1036
|
+
const invalidValueMessage = `error: option '${option.flags}' argument '${val}' is invalid.`;
|
|
1037
|
+
handleOptionValue(val, invalidValueMessage, "cli");
|
|
1038
|
+
});
|
|
1039
|
+
if (option.envVar) {
|
|
1040
|
+
this.on("optionEnv:" + oname, (val) => {
|
|
1041
|
+
const invalidValueMessage = `error: option '${option.flags}' value '${val}' from env '${option.envVar}' is invalid.`;
|
|
1042
|
+
handleOptionValue(val, invalidValueMessage, "env");
|
|
1043
|
+
});
|
|
1044
|
+
}
|
|
1045
|
+
return this;
|
|
1046
|
+
}
|
|
1047
|
+
_optionEx(config, flags, description, fn, defaultValue) {
|
|
1048
|
+
if (typeof flags === "object" && flags instanceof Option) {
|
|
1049
|
+
throw new Error("To add an Option object use addOption() instead of option() or requiredOption()");
|
|
1050
|
+
}
|
|
1051
|
+
const option = this.createOption(flags, description);
|
|
1052
|
+
option.makeOptionMandatory(!!config.mandatory);
|
|
1053
|
+
if (typeof fn === "function") {
|
|
1054
|
+
option.default(defaultValue).argParser(fn);
|
|
1055
|
+
} else if (fn instanceof RegExp) {
|
|
1056
|
+
const regex = fn;
|
|
1057
|
+
fn = (val, def) => {
|
|
1058
|
+
const m = regex.exec(val);
|
|
1059
|
+
return m ? m[0] : def;
|
|
1060
|
+
};
|
|
1061
|
+
option.default(defaultValue).argParser(fn);
|
|
1062
|
+
} else {
|
|
1063
|
+
option.default(fn);
|
|
1064
|
+
}
|
|
1065
|
+
return this.addOption(option);
|
|
1066
|
+
}
|
|
1067
|
+
option(flags, description, parseArg, defaultValue) {
|
|
1068
|
+
return this._optionEx({}, flags, description, parseArg, defaultValue);
|
|
1069
|
+
}
|
|
1070
|
+
requiredOption(flags, description, parseArg, defaultValue) {
|
|
1071
|
+
return this._optionEx({ mandatory: true }, flags, description, parseArg, defaultValue);
|
|
1072
|
+
}
|
|
1073
|
+
combineFlagAndOptionalValue(combine = true) {
|
|
1074
|
+
this._combineFlagAndOptionalValue = !!combine;
|
|
1075
|
+
return this;
|
|
1076
|
+
}
|
|
1077
|
+
allowUnknownOption(allowUnknown = true) {
|
|
1078
|
+
this._allowUnknownOption = !!allowUnknown;
|
|
1079
|
+
return this;
|
|
1080
|
+
}
|
|
1081
|
+
allowExcessArguments(allowExcess = true) {
|
|
1082
|
+
this._allowExcessArguments = !!allowExcess;
|
|
1083
|
+
return this;
|
|
1084
|
+
}
|
|
1085
|
+
enablePositionalOptions(positional = true) {
|
|
1086
|
+
this._enablePositionalOptions = !!positional;
|
|
1087
|
+
return this;
|
|
1088
|
+
}
|
|
1089
|
+
passThroughOptions(passThrough = true) {
|
|
1090
|
+
this._passThroughOptions = !!passThrough;
|
|
1091
|
+
this._checkForBrokenPassThrough();
|
|
1092
|
+
return this;
|
|
1093
|
+
}
|
|
1094
|
+
_checkForBrokenPassThrough() {
|
|
1095
|
+
if (this.parent && this._passThroughOptions && !this.parent._enablePositionalOptions) {
|
|
1096
|
+
throw new Error(`passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`);
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
storeOptionsAsProperties(storeAsProperties = true) {
|
|
1100
|
+
if (this.options.length) {
|
|
1101
|
+
throw new Error("call .storeOptionsAsProperties() before adding options");
|
|
1102
|
+
}
|
|
1103
|
+
if (Object.keys(this._optionValues).length) {
|
|
1104
|
+
throw new Error("call .storeOptionsAsProperties() before setting option values");
|
|
1105
|
+
}
|
|
1106
|
+
this._storeOptionsAsProperties = !!storeAsProperties;
|
|
1107
|
+
return this;
|
|
1108
|
+
}
|
|
1109
|
+
getOptionValue(key) {
|
|
1110
|
+
if (this._storeOptionsAsProperties) {
|
|
1111
|
+
return this[key];
|
|
1112
|
+
}
|
|
1113
|
+
return this._optionValues[key];
|
|
1114
|
+
}
|
|
1115
|
+
setOptionValue(key, value) {
|
|
1116
|
+
return this.setOptionValueWithSource(key, value, undefined);
|
|
1117
|
+
}
|
|
1118
|
+
setOptionValueWithSource(key, value, source) {
|
|
1119
|
+
if (this._storeOptionsAsProperties) {
|
|
1120
|
+
this[key] = value;
|
|
1121
|
+
} else {
|
|
1122
|
+
this._optionValues[key] = value;
|
|
1123
|
+
}
|
|
1124
|
+
this._optionValueSources[key] = source;
|
|
1125
|
+
return this;
|
|
1126
|
+
}
|
|
1127
|
+
getOptionValueSource(key) {
|
|
1128
|
+
return this._optionValueSources[key];
|
|
1129
|
+
}
|
|
1130
|
+
getOptionValueSourceWithGlobals(key) {
|
|
1131
|
+
let source;
|
|
1132
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1133
|
+
if (cmd.getOptionValueSource(key) !== undefined) {
|
|
1134
|
+
source = cmd.getOptionValueSource(key);
|
|
1135
|
+
}
|
|
1136
|
+
});
|
|
1137
|
+
return source;
|
|
1138
|
+
}
|
|
1139
|
+
_prepareUserArgs(argv, parseOptions) {
|
|
1140
|
+
if (argv !== undefined && !Array.isArray(argv)) {
|
|
1141
|
+
throw new Error("first parameter to parse must be array or undefined");
|
|
1142
|
+
}
|
|
1143
|
+
parseOptions = parseOptions || {};
|
|
1144
|
+
if (argv === undefined && parseOptions.from === undefined) {
|
|
1145
|
+
if (process2.versions?.electron) {
|
|
1146
|
+
parseOptions.from = "electron";
|
|
1147
|
+
}
|
|
1148
|
+
const execArgv = process2.execArgv ?? [];
|
|
1149
|
+
if (execArgv.includes("-e") || execArgv.includes("--eval") || execArgv.includes("-p") || execArgv.includes("--print")) {
|
|
1150
|
+
parseOptions.from = "eval";
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
if (argv === undefined) {
|
|
1154
|
+
argv = process2.argv;
|
|
1155
|
+
}
|
|
1156
|
+
this.rawArgs = argv.slice();
|
|
1157
|
+
let userArgs;
|
|
1158
|
+
switch (parseOptions.from) {
|
|
1159
|
+
case undefined:
|
|
1160
|
+
case "node":
|
|
1161
|
+
this._scriptPath = argv[1];
|
|
1162
|
+
userArgs = argv.slice(2);
|
|
1163
|
+
break;
|
|
1164
|
+
case "electron":
|
|
1165
|
+
if (process2.defaultApp) {
|
|
1166
|
+
this._scriptPath = argv[1];
|
|
1167
|
+
userArgs = argv.slice(2);
|
|
1168
|
+
} else {
|
|
1169
|
+
userArgs = argv.slice(1);
|
|
1170
|
+
}
|
|
1171
|
+
break;
|
|
1172
|
+
case "user":
|
|
1173
|
+
userArgs = argv.slice(0);
|
|
1174
|
+
break;
|
|
1175
|
+
case "eval":
|
|
1176
|
+
userArgs = argv.slice(1);
|
|
1177
|
+
break;
|
|
1178
|
+
default:
|
|
1179
|
+
throw new Error(`unexpected parse option { from: '${parseOptions.from}' }`);
|
|
1180
|
+
}
|
|
1181
|
+
if (!this._name && this._scriptPath)
|
|
1182
|
+
this.nameFromFilename(this._scriptPath);
|
|
1183
|
+
this._name = this._name || "program";
|
|
1184
|
+
return userArgs;
|
|
1185
|
+
}
|
|
1186
|
+
parse(argv, parseOptions) {
|
|
1187
|
+
this._prepareForParse();
|
|
1188
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1189
|
+
this._parseCommand([], userArgs);
|
|
1190
|
+
return this;
|
|
1191
|
+
}
|
|
1192
|
+
async parseAsync(argv, parseOptions) {
|
|
1193
|
+
this._prepareForParse();
|
|
1194
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1195
|
+
await this._parseCommand([], userArgs);
|
|
1196
|
+
return this;
|
|
1197
|
+
}
|
|
1198
|
+
_prepareForParse() {
|
|
1199
|
+
if (this._savedState === null) {
|
|
1200
|
+
this.options.filter((option) => option.negate && option.defaultValue === undefined && this.getOptionValue(option.attributeName()) === undefined).forEach((option) => {
|
|
1201
|
+
const positiveLongFlag = option.long.replace(/^--no-/, "--");
|
|
1202
|
+
if (!this._findOption(positiveLongFlag)) {
|
|
1203
|
+
this.setOptionValueWithSource(option.attributeName(), true, "default");
|
|
1204
|
+
}
|
|
1205
|
+
});
|
|
1206
|
+
this.saveStateBeforeParse();
|
|
1207
|
+
} else {
|
|
1208
|
+
this.restoreStateBeforeParse();
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
saveStateBeforeParse() {
|
|
1212
|
+
this._savedState = {
|
|
1213
|
+
_name: this._name,
|
|
1214
|
+
_optionValues: { ...this._optionValues },
|
|
1215
|
+
_optionValueSources: { ...this._optionValueSources }
|
|
1216
|
+
};
|
|
1217
|
+
}
|
|
1218
|
+
restoreStateBeforeParse() {
|
|
1219
|
+
if (this._storeOptionsAsProperties)
|
|
1220
|
+
throw new Error(`Can not call parse again when storeOptionsAsProperties is true.
|
|
1221
|
+
- either make a new Command for each call to parse, or stop storing options as properties`);
|
|
1222
|
+
this._name = this._savedState._name;
|
|
1223
|
+
this._scriptPath = null;
|
|
1224
|
+
this.rawArgs = [];
|
|
1225
|
+
this._optionValues = { ...this._savedState._optionValues };
|
|
1226
|
+
this._optionValueSources = { ...this._savedState._optionValueSources };
|
|
1227
|
+
this.args = [];
|
|
1228
|
+
this.processedArgs = [];
|
|
1229
|
+
}
|
|
1230
|
+
_checkForMissingExecutable(executableFile, executableDir, subcommandName) {
|
|
1231
|
+
if (fs.existsSync(executableFile))
|
|
1232
|
+
return;
|
|
1233
|
+
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";
|
|
1234
|
+
const executableMissing = `'${executableFile}' does not exist
|
|
1235
|
+
- if '${subcommandName}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
|
|
1236
|
+
- if the default executable name is not suitable, use the executableFile option to supply a custom name or path
|
|
1237
|
+
- ${executableDirMessage}`;
|
|
1238
|
+
throw new Error(executableMissing);
|
|
1239
|
+
}
|
|
1240
|
+
_executeSubCommand(subcommand, args) {
|
|
1241
|
+
args = args.slice();
|
|
1242
|
+
const sourceExt = [".js", ".ts", ".tsx", ".mjs", ".cjs"];
|
|
1243
|
+
function findFile(baseDir, baseName) {
|
|
1244
|
+
const localBin = path.resolve(baseDir, baseName);
|
|
1245
|
+
if (fs.existsSync(localBin))
|
|
1246
|
+
return localBin;
|
|
1247
|
+
if (sourceExt.includes(path.extname(baseName)))
|
|
1248
|
+
return;
|
|
1249
|
+
const foundExt = sourceExt.find((ext) => fs.existsSync(`${localBin}${ext}`));
|
|
1250
|
+
if (foundExt)
|
|
1251
|
+
return `${localBin}${foundExt}`;
|
|
1252
|
+
return;
|
|
1253
|
+
}
|
|
1254
|
+
this._checkForMissingMandatoryOptions();
|
|
1255
|
+
this._checkForConflictingOptions();
|
|
1256
|
+
let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;
|
|
1257
|
+
let executableDir = this._executableDir || "";
|
|
1258
|
+
if (this._scriptPath) {
|
|
1259
|
+
let resolvedScriptPath;
|
|
1260
|
+
try {
|
|
1261
|
+
resolvedScriptPath = fs.realpathSync(this._scriptPath);
|
|
1262
|
+
} catch {
|
|
1263
|
+
resolvedScriptPath = this._scriptPath;
|
|
1264
|
+
}
|
|
1265
|
+
executableDir = path.resolve(path.dirname(resolvedScriptPath), executableDir);
|
|
1266
|
+
}
|
|
1267
|
+
if (executableDir) {
|
|
1268
|
+
let localFile = findFile(executableDir, executableFile);
|
|
1269
|
+
if (!localFile && !subcommand._executableFile && this._scriptPath) {
|
|
1270
|
+
const legacyName = path.basename(this._scriptPath, path.extname(this._scriptPath));
|
|
1271
|
+
if (legacyName !== this._name) {
|
|
1272
|
+
localFile = findFile(executableDir, `${legacyName}-${subcommand._name}`);
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
executableFile = localFile || executableFile;
|
|
1276
|
+
}
|
|
1277
|
+
const launchWithNode = sourceExt.includes(path.extname(executableFile));
|
|
1278
|
+
let proc;
|
|
1279
|
+
if (process2.platform !== "win32") {
|
|
1280
|
+
if (launchWithNode) {
|
|
1281
|
+
args.unshift(executableFile);
|
|
1282
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1283
|
+
proc = childProcess.spawn(process2.argv[0], args, { stdio: "inherit" });
|
|
1284
|
+
} else {
|
|
1285
|
+
proc = childProcess.spawn(executableFile, args, { stdio: "inherit" });
|
|
1286
|
+
}
|
|
1287
|
+
} else {
|
|
1288
|
+
this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
|
|
1289
|
+
args.unshift(executableFile);
|
|
1290
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1291
|
+
proc = childProcess.spawn(process2.execPath, args, { stdio: "inherit" });
|
|
1292
|
+
}
|
|
1293
|
+
if (!proc.killed) {
|
|
1294
|
+
const signals = ["SIGUSR1", "SIGUSR2", "SIGTERM", "SIGINT", "SIGHUP"];
|
|
1295
|
+
signals.forEach((signal) => {
|
|
1296
|
+
process2.on(signal, () => {
|
|
1297
|
+
if (proc.killed === false && proc.exitCode === null) {
|
|
1298
|
+
proc.kill(signal);
|
|
1299
|
+
}
|
|
1300
|
+
});
|
|
1301
|
+
});
|
|
1302
|
+
}
|
|
1303
|
+
const exitCallback = this._exitCallback;
|
|
1304
|
+
proc.on("close", (code) => {
|
|
1305
|
+
code = code ?? 1;
|
|
1306
|
+
if (!exitCallback) {
|
|
1307
|
+
process2.exit(code);
|
|
1308
|
+
} else {
|
|
1309
|
+
exitCallback(new CommanderError(code, "commander.executeSubCommandAsync", "(close)"));
|
|
1310
|
+
}
|
|
1311
|
+
});
|
|
1312
|
+
proc.on("error", (err) => {
|
|
1313
|
+
if (err.code === "ENOENT") {
|
|
1314
|
+
this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
|
|
1315
|
+
} else if (err.code === "EACCES") {
|
|
1316
|
+
throw new Error(`'${executableFile}' not executable`);
|
|
1317
|
+
}
|
|
1318
|
+
if (!exitCallback) {
|
|
1319
|
+
process2.exit(1);
|
|
1320
|
+
} else {
|
|
1321
|
+
const wrappedError = new CommanderError(1, "commander.executeSubCommandAsync", "(error)");
|
|
1322
|
+
wrappedError.nestedError = err;
|
|
1323
|
+
exitCallback(wrappedError);
|
|
1324
|
+
}
|
|
1325
|
+
});
|
|
1326
|
+
this.runningCommand = proc;
|
|
1327
|
+
}
|
|
1328
|
+
_dispatchSubcommand(commandName, operands, unknown) {
|
|
1329
|
+
const subCommand = this._findCommand(commandName);
|
|
1330
|
+
if (!subCommand)
|
|
1331
|
+
this.help({ error: true });
|
|
1332
|
+
subCommand._prepareForParse();
|
|
1333
|
+
let promiseChain;
|
|
1334
|
+
promiseChain = this._chainOrCallSubCommandHook(promiseChain, subCommand, "preSubcommand");
|
|
1335
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
1336
|
+
if (subCommand._executableHandler) {
|
|
1337
|
+
this._executeSubCommand(subCommand, operands.concat(unknown));
|
|
1338
|
+
} else {
|
|
1339
|
+
return subCommand._parseCommand(operands, unknown);
|
|
1340
|
+
}
|
|
1341
|
+
});
|
|
1342
|
+
return promiseChain;
|
|
1343
|
+
}
|
|
1344
|
+
_dispatchHelpCommand(subcommandName) {
|
|
1345
|
+
if (!subcommandName) {
|
|
1346
|
+
this.help();
|
|
1347
|
+
}
|
|
1348
|
+
const subCommand = this._findCommand(subcommandName);
|
|
1349
|
+
if (subCommand && !subCommand._executableHandler) {
|
|
1350
|
+
subCommand.help();
|
|
1351
|
+
}
|
|
1352
|
+
return this._dispatchSubcommand(subcommandName, [], [this._getHelpOption()?.long ?? this._getHelpOption()?.short ?? "--help"]);
|
|
1353
|
+
}
|
|
1354
|
+
_checkNumberOfArguments() {
|
|
1355
|
+
this.registeredArguments.forEach((arg, i) => {
|
|
1356
|
+
if (arg.required && this.args[i] == null) {
|
|
1357
|
+
this.missingArgument(arg.name());
|
|
1358
|
+
}
|
|
1359
|
+
});
|
|
1360
|
+
if (this.registeredArguments.length > 0 && this.registeredArguments[this.registeredArguments.length - 1].variadic) {
|
|
1361
|
+
return;
|
|
1362
|
+
}
|
|
1363
|
+
if (this.args.length > this.registeredArguments.length) {
|
|
1364
|
+
this._excessArguments(this.args);
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
_processArguments() {
|
|
1368
|
+
const myParseArg = (argument, value, previous) => {
|
|
1369
|
+
let parsedValue = value;
|
|
1370
|
+
if (value !== null && argument.parseArg) {
|
|
1371
|
+
const invalidValueMessage = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'.`;
|
|
1372
|
+
parsedValue = this._callParseArg(argument, value, previous, invalidValueMessage);
|
|
1373
|
+
}
|
|
1374
|
+
return parsedValue;
|
|
1375
|
+
};
|
|
1376
|
+
this._checkNumberOfArguments();
|
|
1377
|
+
const processedArgs = [];
|
|
1378
|
+
this.registeredArguments.forEach((declaredArg, index) => {
|
|
1379
|
+
let value = declaredArg.defaultValue;
|
|
1380
|
+
if (declaredArg.variadic) {
|
|
1381
|
+
if (index < this.args.length) {
|
|
1382
|
+
value = this.args.slice(index);
|
|
1383
|
+
if (declaredArg.parseArg) {
|
|
1384
|
+
value = value.reduce((processed, v) => {
|
|
1385
|
+
return myParseArg(declaredArg, v, processed);
|
|
1386
|
+
}, declaredArg.defaultValue);
|
|
1387
|
+
}
|
|
1388
|
+
} else if (value === undefined) {
|
|
1389
|
+
value = [];
|
|
1390
|
+
}
|
|
1391
|
+
} else if (index < this.args.length) {
|
|
1392
|
+
value = this.args[index];
|
|
1393
|
+
if (declaredArg.parseArg) {
|
|
1394
|
+
value = myParseArg(declaredArg, value, declaredArg.defaultValue);
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
processedArgs[index] = value;
|
|
1398
|
+
});
|
|
1399
|
+
this.processedArgs = processedArgs;
|
|
1400
|
+
}
|
|
1401
|
+
_chainOrCall(promise, fn) {
|
|
1402
|
+
if (promise?.then && typeof promise.then === "function") {
|
|
1403
|
+
return promise.then(() => fn());
|
|
1404
|
+
}
|
|
1405
|
+
return fn();
|
|
1406
|
+
}
|
|
1407
|
+
_chainOrCallHooks(promise, event) {
|
|
1408
|
+
let result = promise;
|
|
1409
|
+
const hooks = [];
|
|
1410
|
+
this._getCommandAndAncestors().reverse().filter((cmd) => cmd._lifeCycleHooks[event] !== undefined).forEach((hookedCommand) => {
|
|
1411
|
+
hookedCommand._lifeCycleHooks[event].forEach((callback) => {
|
|
1412
|
+
hooks.push({ hookedCommand, callback });
|
|
1413
|
+
});
|
|
1414
|
+
});
|
|
1415
|
+
if (event === "postAction") {
|
|
1416
|
+
hooks.reverse();
|
|
1417
|
+
}
|
|
1418
|
+
hooks.forEach((hookDetail) => {
|
|
1419
|
+
result = this._chainOrCall(result, () => {
|
|
1420
|
+
return hookDetail.callback(hookDetail.hookedCommand, this);
|
|
1421
|
+
});
|
|
1422
|
+
});
|
|
1423
|
+
return result;
|
|
1424
|
+
}
|
|
1425
|
+
_chainOrCallSubCommandHook(promise, subCommand, event) {
|
|
1426
|
+
let result = promise;
|
|
1427
|
+
if (this._lifeCycleHooks[event] !== undefined) {
|
|
1428
|
+
this._lifeCycleHooks[event].forEach((hook) => {
|
|
1429
|
+
result = this._chainOrCall(result, () => {
|
|
1430
|
+
return hook(this, subCommand);
|
|
1431
|
+
});
|
|
1432
|
+
});
|
|
1433
|
+
}
|
|
1434
|
+
return result;
|
|
1435
|
+
}
|
|
1436
|
+
_parseCommand(operands, unknown) {
|
|
1437
|
+
const parsed = this.parseOptions(unknown);
|
|
1438
|
+
this._parseOptionsEnv();
|
|
1439
|
+
this._parseOptionsImplied();
|
|
1440
|
+
operands = operands.concat(parsed.operands);
|
|
1441
|
+
unknown = parsed.unknown;
|
|
1442
|
+
this.args = operands.concat(unknown);
|
|
1443
|
+
if (operands && this._findCommand(operands[0])) {
|
|
1444
|
+
return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
|
|
1445
|
+
}
|
|
1446
|
+
if (this._getHelpCommand() && operands[0] === this._getHelpCommand().name()) {
|
|
1447
|
+
return this._dispatchHelpCommand(operands[1]);
|
|
1448
|
+
}
|
|
1449
|
+
if (this._defaultCommandName) {
|
|
1450
|
+
this._outputHelpIfRequested(unknown);
|
|
1451
|
+
return this._dispatchSubcommand(this._defaultCommandName, operands, unknown);
|
|
1452
|
+
}
|
|
1453
|
+
if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) {
|
|
1454
|
+
this.help({ error: true });
|
|
1455
|
+
}
|
|
1456
|
+
this._outputHelpIfRequested(parsed.unknown);
|
|
1457
|
+
this._checkForMissingMandatoryOptions();
|
|
1458
|
+
this._checkForConflictingOptions();
|
|
1459
|
+
const checkForUnknownOptions = () => {
|
|
1460
|
+
if (parsed.unknown.length > 0) {
|
|
1461
|
+
this.unknownOption(parsed.unknown[0]);
|
|
1462
|
+
}
|
|
1463
|
+
};
|
|
1464
|
+
const commandEvent = `command:${this.name()}`;
|
|
1465
|
+
if (this._actionHandler) {
|
|
1466
|
+
checkForUnknownOptions();
|
|
1467
|
+
this._processArguments();
|
|
1468
|
+
let promiseChain;
|
|
1469
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "preAction");
|
|
1470
|
+
promiseChain = this._chainOrCall(promiseChain, () => this._actionHandler(this.processedArgs));
|
|
1471
|
+
if (this.parent) {
|
|
1472
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
1473
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1474
|
+
});
|
|
1475
|
+
}
|
|
1476
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "postAction");
|
|
1477
|
+
return promiseChain;
|
|
1478
|
+
}
|
|
1479
|
+
if (this.parent?.listenerCount(commandEvent)) {
|
|
1480
|
+
checkForUnknownOptions();
|
|
1481
|
+
this._processArguments();
|
|
1482
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1483
|
+
} else if (operands.length) {
|
|
1484
|
+
if (this._findCommand("*")) {
|
|
1485
|
+
return this._dispatchSubcommand("*", operands, unknown);
|
|
1486
|
+
}
|
|
1487
|
+
if (this.listenerCount("command:*")) {
|
|
1488
|
+
this.emit("command:*", operands, unknown);
|
|
1489
|
+
} else if (this.commands.length) {
|
|
1490
|
+
this.unknownCommand();
|
|
1491
|
+
} else {
|
|
1492
|
+
checkForUnknownOptions();
|
|
1493
|
+
this._processArguments();
|
|
1494
|
+
}
|
|
1495
|
+
} else if (this.commands.length) {
|
|
1496
|
+
checkForUnknownOptions();
|
|
1497
|
+
this.help({ error: true });
|
|
1498
|
+
} else {
|
|
1499
|
+
checkForUnknownOptions();
|
|
1500
|
+
this._processArguments();
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
_findCommand(name) {
|
|
1504
|
+
if (!name)
|
|
1505
|
+
return;
|
|
1506
|
+
return this.commands.find((cmd) => cmd._name === name || cmd._aliases.includes(name));
|
|
1507
|
+
}
|
|
1508
|
+
_findOption(arg) {
|
|
1509
|
+
return this.options.find((option) => option.is(arg));
|
|
1510
|
+
}
|
|
1511
|
+
_checkForMissingMandatoryOptions() {
|
|
1512
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1513
|
+
cmd.options.forEach((anOption) => {
|
|
1514
|
+
if (anOption.mandatory && cmd.getOptionValue(anOption.attributeName()) === undefined) {
|
|
1515
|
+
cmd.missingMandatoryOptionValue(anOption);
|
|
1516
|
+
}
|
|
1517
|
+
});
|
|
1518
|
+
});
|
|
1519
|
+
}
|
|
1520
|
+
_checkForConflictingLocalOptions() {
|
|
1521
|
+
const definedNonDefaultOptions = this.options.filter((option) => {
|
|
1522
|
+
const optionKey = option.attributeName();
|
|
1523
|
+
if (this.getOptionValue(optionKey) === undefined) {
|
|
1524
|
+
return false;
|
|
1525
|
+
}
|
|
1526
|
+
return this.getOptionValueSource(optionKey) !== "default";
|
|
1527
|
+
});
|
|
1528
|
+
const optionsWithConflicting = definedNonDefaultOptions.filter((option) => option.conflictsWith.length > 0);
|
|
1529
|
+
optionsWithConflicting.forEach((option) => {
|
|
1530
|
+
const conflictingAndDefined = definedNonDefaultOptions.find((defined) => option.conflictsWith.includes(defined.attributeName()));
|
|
1531
|
+
if (conflictingAndDefined) {
|
|
1532
|
+
this._conflictingOption(option, conflictingAndDefined);
|
|
1533
|
+
}
|
|
1534
|
+
});
|
|
1535
|
+
}
|
|
1536
|
+
_checkForConflictingOptions() {
|
|
1537
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1538
|
+
cmd._checkForConflictingLocalOptions();
|
|
1539
|
+
});
|
|
1540
|
+
}
|
|
1541
|
+
parseOptions(args) {
|
|
1542
|
+
const operands = [];
|
|
1543
|
+
const unknown = [];
|
|
1544
|
+
let dest = operands;
|
|
1545
|
+
function maybeOption(arg) {
|
|
1546
|
+
return arg.length > 1 && arg[0] === "-";
|
|
1547
|
+
}
|
|
1548
|
+
const negativeNumberArg = (arg) => {
|
|
1549
|
+
if (!/^-(\d+|\d*\.\d+)(e[+-]?\d+)?$/.test(arg))
|
|
1550
|
+
return false;
|
|
1551
|
+
return !this._getCommandAndAncestors().some((cmd) => cmd.options.map((opt) => opt.short).some((short) => /^-\d$/.test(short)));
|
|
1552
|
+
};
|
|
1553
|
+
let activeVariadicOption = null;
|
|
1554
|
+
let activeGroup = null;
|
|
1555
|
+
let i = 0;
|
|
1556
|
+
while (i < args.length || activeGroup) {
|
|
1557
|
+
const arg = activeGroup ?? args[i++];
|
|
1558
|
+
activeGroup = null;
|
|
1559
|
+
if (arg === "--") {
|
|
1560
|
+
if (dest === unknown)
|
|
1561
|
+
dest.push(arg);
|
|
1562
|
+
dest.push(...args.slice(i));
|
|
1563
|
+
break;
|
|
1564
|
+
}
|
|
1565
|
+
if (activeVariadicOption && (!maybeOption(arg) || negativeNumberArg(arg))) {
|
|
1566
|
+
this.emit(`option:${activeVariadicOption.name()}`, arg);
|
|
1567
|
+
continue;
|
|
1568
|
+
}
|
|
1569
|
+
activeVariadicOption = null;
|
|
1570
|
+
if (maybeOption(arg)) {
|
|
1571
|
+
const option = this._findOption(arg);
|
|
1572
|
+
if (option) {
|
|
1573
|
+
if (option.required) {
|
|
1574
|
+
const value = args[i++];
|
|
1575
|
+
if (value === undefined)
|
|
1576
|
+
this.optionMissingArgument(option);
|
|
1577
|
+
this.emit(`option:${option.name()}`, value);
|
|
1578
|
+
} else if (option.optional) {
|
|
1579
|
+
let value = null;
|
|
1580
|
+
if (i < args.length && (!maybeOption(args[i]) || negativeNumberArg(args[i]))) {
|
|
1581
|
+
value = args[i++];
|
|
1582
|
+
}
|
|
1583
|
+
this.emit(`option:${option.name()}`, value);
|
|
1584
|
+
} else {
|
|
1585
|
+
this.emit(`option:${option.name()}`);
|
|
1586
|
+
}
|
|
1587
|
+
activeVariadicOption = option.variadic ? option : null;
|
|
1588
|
+
continue;
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
if (arg.length > 2 && arg[0] === "-" && arg[1] !== "-") {
|
|
1592
|
+
const option = this._findOption(`-${arg[1]}`);
|
|
1593
|
+
if (option) {
|
|
1594
|
+
if (option.required || option.optional && this._combineFlagAndOptionalValue) {
|
|
1595
|
+
this.emit(`option:${option.name()}`, arg.slice(2));
|
|
1596
|
+
} else {
|
|
1597
|
+
this.emit(`option:${option.name()}`);
|
|
1598
|
+
activeGroup = `-${arg.slice(2)}`;
|
|
1599
|
+
}
|
|
1600
|
+
continue;
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
if (/^--[^=]+=/.test(arg)) {
|
|
1604
|
+
const index = arg.indexOf("=");
|
|
1605
|
+
const option = this._findOption(arg.slice(0, index));
|
|
1606
|
+
if (option && (option.required || option.optional)) {
|
|
1607
|
+
this.emit(`option:${option.name()}`, arg.slice(index + 1));
|
|
1608
|
+
continue;
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
if (dest === operands && maybeOption(arg) && !(this.commands.length === 0 && negativeNumberArg(arg))) {
|
|
1612
|
+
dest = unknown;
|
|
1613
|
+
}
|
|
1614
|
+
if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
|
|
1615
|
+
if (this._findCommand(arg)) {
|
|
1616
|
+
operands.push(arg);
|
|
1617
|
+
unknown.push(...args.slice(i));
|
|
1618
|
+
break;
|
|
1619
|
+
} else if (this._getHelpCommand() && arg === this._getHelpCommand().name()) {
|
|
1620
|
+
operands.push(arg, ...args.slice(i));
|
|
1621
|
+
break;
|
|
1622
|
+
} else if (this._defaultCommandName) {
|
|
1623
|
+
unknown.push(arg, ...args.slice(i));
|
|
1624
|
+
break;
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
if (this._passThroughOptions) {
|
|
1628
|
+
dest.push(arg, ...args.slice(i));
|
|
1629
|
+
break;
|
|
1630
|
+
}
|
|
1631
|
+
dest.push(arg);
|
|
1632
|
+
}
|
|
1633
|
+
return { operands, unknown };
|
|
1634
|
+
}
|
|
1635
|
+
opts() {
|
|
1636
|
+
if (this._storeOptionsAsProperties) {
|
|
1637
|
+
const result = {};
|
|
1638
|
+
const len = this.options.length;
|
|
1639
|
+
for (let i = 0;i < len; i++) {
|
|
1640
|
+
const key = this.options[i].attributeName();
|
|
1641
|
+
result[key] = key === this._versionOptionName ? this._version : this[key];
|
|
1642
|
+
}
|
|
1643
|
+
return result;
|
|
1644
|
+
}
|
|
1645
|
+
return this._optionValues;
|
|
1646
|
+
}
|
|
1647
|
+
optsWithGlobals() {
|
|
1648
|
+
return this._getCommandAndAncestors().reduce((combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()), {});
|
|
1649
|
+
}
|
|
1650
|
+
error(message, errorOptions) {
|
|
1651
|
+
this._outputConfiguration.outputError(`${message}
|
|
1652
|
+
`, this._outputConfiguration.writeErr);
|
|
1653
|
+
if (typeof this._showHelpAfterError === "string") {
|
|
1654
|
+
this._outputConfiguration.writeErr(`${this._showHelpAfterError}
|
|
1655
|
+
`);
|
|
1656
|
+
} else if (this._showHelpAfterError) {
|
|
1657
|
+
this._outputConfiguration.writeErr(`
|
|
1658
|
+
`);
|
|
1659
|
+
this.outputHelp({ error: true });
|
|
1660
|
+
}
|
|
1661
|
+
const config = errorOptions || {};
|
|
1662
|
+
const exitCode = config.exitCode || 1;
|
|
1663
|
+
const code = config.code || "commander.error";
|
|
1664
|
+
this._exit(exitCode, code, message);
|
|
1665
|
+
}
|
|
1666
|
+
_parseOptionsEnv() {
|
|
1667
|
+
this.options.forEach((option) => {
|
|
1668
|
+
if (option.envVar && option.envVar in process2.env) {
|
|
1669
|
+
const optionKey = option.attributeName();
|
|
1670
|
+
if (this.getOptionValue(optionKey) === undefined || ["default", "config", "env"].includes(this.getOptionValueSource(optionKey))) {
|
|
1671
|
+
if (option.required || option.optional) {
|
|
1672
|
+
this.emit(`optionEnv:${option.name()}`, process2.env[option.envVar]);
|
|
1673
|
+
} else {
|
|
1674
|
+
this.emit(`optionEnv:${option.name()}`);
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
});
|
|
1679
|
+
}
|
|
1680
|
+
_parseOptionsImplied() {
|
|
1681
|
+
const dualHelper = new DualOptions(this.options);
|
|
1682
|
+
const hasCustomOptionValue = (optionKey) => {
|
|
1683
|
+
return this.getOptionValue(optionKey) !== undefined && !["default", "implied"].includes(this.getOptionValueSource(optionKey));
|
|
1684
|
+
};
|
|
1685
|
+
this.options.filter((option) => option.implied !== undefined && hasCustomOptionValue(option.attributeName()) && dualHelper.valueFromOption(this.getOptionValue(option.attributeName()), option)).forEach((option) => {
|
|
1686
|
+
Object.keys(option.implied).filter((impliedKey) => !hasCustomOptionValue(impliedKey)).forEach((impliedKey) => {
|
|
1687
|
+
this.setOptionValueWithSource(impliedKey, option.implied[impliedKey], "implied");
|
|
1688
|
+
});
|
|
1689
|
+
});
|
|
1690
|
+
}
|
|
1691
|
+
missingArgument(name) {
|
|
1692
|
+
const message = `error: missing required argument '${name}'`;
|
|
1693
|
+
this.error(message, { code: "commander.missingArgument" });
|
|
1694
|
+
}
|
|
1695
|
+
optionMissingArgument(option) {
|
|
1696
|
+
const message = `error: option '${option.flags}' argument missing`;
|
|
1697
|
+
this.error(message, { code: "commander.optionMissingArgument" });
|
|
1698
|
+
}
|
|
1699
|
+
missingMandatoryOptionValue(option) {
|
|
1700
|
+
const message = `error: required option '${option.flags}' not specified`;
|
|
1701
|
+
this.error(message, { code: "commander.missingMandatoryOptionValue" });
|
|
1702
|
+
}
|
|
1703
|
+
_conflictingOption(option, conflictingOption) {
|
|
1704
|
+
const findBestOptionFromValue = (option2) => {
|
|
1705
|
+
const optionKey = option2.attributeName();
|
|
1706
|
+
const optionValue = this.getOptionValue(optionKey);
|
|
1707
|
+
const negativeOption = this.options.find((target) => target.negate && optionKey === target.attributeName());
|
|
1708
|
+
const positiveOption = this.options.find((target) => !target.negate && optionKey === target.attributeName());
|
|
1709
|
+
if (negativeOption && (negativeOption.presetArg === undefined && optionValue === false || negativeOption.presetArg !== undefined && optionValue === negativeOption.presetArg)) {
|
|
1710
|
+
return negativeOption;
|
|
1711
|
+
}
|
|
1712
|
+
return positiveOption || option2;
|
|
1713
|
+
};
|
|
1714
|
+
const getErrorMessage = (option2) => {
|
|
1715
|
+
const bestOption = findBestOptionFromValue(option2);
|
|
1716
|
+
const optionKey = bestOption.attributeName();
|
|
1717
|
+
const source = this.getOptionValueSource(optionKey);
|
|
1718
|
+
if (source === "env") {
|
|
1719
|
+
return `environment variable '${bestOption.envVar}'`;
|
|
1720
|
+
}
|
|
1721
|
+
return `option '${bestOption.flags}'`;
|
|
1722
|
+
};
|
|
1723
|
+
const message = `error: ${getErrorMessage(option)} cannot be used with ${getErrorMessage(conflictingOption)}`;
|
|
1724
|
+
this.error(message, { code: "commander.conflictingOption" });
|
|
1725
|
+
}
|
|
1726
|
+
unknownOption(flag) {
|
|
1727
|
+
if (this._allowUnknownOption)
|
|
1728
|
+
return;
|
|
1729
|
+
let suggestion = "";
|
|
1730
|
+
if (flag.startsWith("--") && this._showSuggestionAfterError) {
|
|
1731
|
+
let candidateFlags = [];
|
|
1732
|
+
let command = this;
|
|
1733
|
+
do {
|
|
1734
|
+
const moreFlags = command.createHelp().visibleOptions(command).filter((option) => option.long).map((option) => option.long);
|
|
1735
|
+
candidateFlags = candidateFlags.concat(moreFlags);
|
|
1736
|
+
command = command.parent;
|
|
1737
|
+
} while (command && !command._enablePositionalOptions);
|
|
1738
|
+
suggestion = suggestSimilar(flag, candidateFlags);
|
|
1739
|
+
}
|
|
1740
|
+
const message = `error: unknown option '${flag}'${suggestion}`;
|
|
1741
|
+
this.error(message, { code: "commander.unknownOption" });
|
|
1742
|
+
}
|
|
1743
|
+
_excessArguments(receivedArgs) {
|
|
1744
|
+
if (this._allowExcessArguments)
|
|
1745
|
+
return;
|
|
1746
|
+
const expected = this.registeredArguments.length;
|
|
1747
|
+
const s = expected === 1 ? "" : "s";
|
|
1748
|
+
const received = receivedArgs.length;
|
|
1749
|
+
const forSubcommand = this.parent ? ` for '${this.name()}'` : "";
|
|
1750
|
+
const details = receivedArgs.join(", ");
|
|
1751
|
+
const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${received}: ${details}.`;
|
|
1752
|
+
this.error(message, { code: "commander.excessArguments" });
|
|
1753
|
+
}
|
|
1754
|
+
unknownCommand() {
|
|
1755
|
+
const unknownName = this.args[0];
|
|
1756
|
+
let suggestion = "";
|
|
1757
|
+
if (this._showSuggestionAfterError) {
|
|
1758
|
+
const candidateNames = [];
|
|
1759
|
+
this.createHelp().visibleCommands(this).forEach((command) => {
|
|
1760
|
+
candidateNames.push(command.name());
|
|
1761
|
+
if (command.alias())
|
|
1762
|
+
candidateNames.push(command.alias());
|
|
1763
|
+
});
|
|
1764
|
+
suggestion = suggestSimilar(unknownName, candidateNames);
|
|
1765
|
+
}
|
|
1766
|
+
const message = `error: unknown command '${unknownName}'${suggestion}`;
|
|
1767
|
+
this.error(message, { code: "commander.unknownCommand" });
|
|
1768
|
+
}
|
|
1769
|
+
version(str, flags, description) {
|
|
1770
|
+
if (str === undefined)
|
|
1771
|
+
return this._version;
|
|
1772
|
+
this._version = str;
|
|
1773
|
+
flags = flags || "-V, --version";
|
|
1774
|
+
description = description || "output the version number";
|
|
1775
|
+
const versionOption = this.createOption(flags, description);
|
|
1776
|
+
this._versionOptionName = versionOption.attributeName();
|
|
1777
|
+
this._registerOption(versionOption);
|
|
1778
|
+
this.on("option:" + versionOption.name(), () => {
|
|
1779
|
+
this._outputConfiguration.writeOut(`${str}
|
|
1780
|
+
`);
|
|
1781
|
+
this._exit(0, "commander.version", str);
|
|
1782
|
+
});
|
|
1783
|
+
return this;
|
|
1784
|
+
}
|
|
1785
|
+
description(str, argsDescription) {
|
|
1786
|
+
if (str === undefined && argsDescription === undefined)
|
|
1787
|
+
return this._description;
|
|
1788
|
+
this._description = str;
|
|
1789
|
+
if (argsDescription) {
|
|
1790
|
+
this._argsDescription = argsDescription;
|
|
1791
|
+
}
|
|
1792
|
+
return this;
|
|
1793
|
+
}
|
|
1794
|
+
summary(str) {
|
|
1795
|
+
if (str === undefined)
|
|
1796
|
+
return this._summary;
|
|
1797
|
+
this._summary = str;
|
|
1798
|
+
return this;
|
|
1799
|
+
}
|
|
1800
|
+
alias(alias) {
|
|
1801
|
+
if (alias === undefined)
|
|
1802
|
+
return this._aliases[0];
|
|
1803
|
+
let command = this;
|
|
1804
|
+
if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
|
|
1805
|
+
command = this.commands[this.commands.length - 1];
|
|
1806
|
+
}
|
|
1807
|
+
if (alias === command._name)
|
|
1808
|
+
throw new Error("Command alias can't be the same as its name");
|
|
1809
|
+
const matchingCommand = this.parent?._findCommand(alias);
|
|
1810
|
+
if (matchingCommand) {
|
|
1811
|
+
const existingCmd = [matchingCommand.name()].concat(matchingCommand.aliases()).join("|");
|
|
1812
|
+
throw new Error(`cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`);
|
|
1813
|
+
}
|
|
1814
|
+
command._aliases.push(alias);
|
|
1815
|
+
return this;
|
|
1816
|
+
}
|
|
1817
|
+
aliases(aliases) {
|
|
1818
|
+
if (aliases === undefined)
|
|
1819
|
+
return this._aliases;
|
|
1820
|
+
aliases.forEach((alias) => this.alias(alias));
|
|
1821
|
+
return this;
|
|
1822
|
+
}
|
|
1823
|
+
usage(str) {
|
|
1824
|
+
if (str === undefined) {
|
|
1825
|
+
if (this._usage)
|
|
1826
|
+
return this._usage;
|
|
1827
|
+
const args = this.registeredArguments.map((arg) => {
|
|
1828
|
+
return humanReadableArgName(arg);
|
|
1829
|
+
});
|
|
1830
|
+
return [].concat(this.options.length || this._helpOption !== null ? "[options]" : [], this.commands.length ? "[command]" : [], this.registeredArguments.length ? args : []).join(" ");
|
|
1831
|
+
}
|
|
1832
|
+
this._usage = str;
|
|
1833
|
+
return this;
|
|
1834
|
+
}
|
|
1835
|
+
name(str) {
|
|
1836
|
+
if (str === undefined)
|
|
1837
|
+
return this._name;
|
|
1838
|
+
this._name = str;
|
|
1839
|
+
return this;
|
|
1840
|
+
}
|
|
1841
|
+
helpGroup(heading) {
|
|
1842
|
+
if (heading === undefined)
|
|
1843
|
+
return this._helpGroupHeading ?? "";
|
|
1844
|
+
this._helpGroupHeading = heading;
|
|
1845
|
+
return this;
|
|
1846
|
+
}
|
|
1847
|
+
commandsGroup(heading) {
|
|
1848
|
+
if (heading === undefined)
|
|
1849
|
+
return this._defaultCommandGroup ?? "";
|
|
1850
|
+
this._defaultCommandGroup = heading;
|
|
1851
|
+
return this;
|
|
1852
|
+
}
|
|
1853
|
+
optionsGroup(heading) {
|
|
1854
|
+
if (heading === undefined)
|
|
1855
|
+
return this._defaultOptionGroup ?? "";
|
|
1856
|
+
this._defaultOptionGroup = heading;
|
|
1857
|
+
return this;
|
|
1858
|
+
}
|
|
1859
|
+
_initOptionGroup(option) {
|
|
1860
|
+
if (this._defaultOptionGroup && !option.helpGroupHeading)
|
|
1861
|
+
option.helpGroup(this._defaultOptionGroup);
|
|
1862
|
+
}
|
|
1863
|
+
_initCommandGroup(cmd) {
|
|
1864
|
+
if (this._defaultCommandGroup && !cmd.helpGroup())
|
|
1865
|
+
cmd.helpGroup(this._defaultCommandGroup);
|
|
1866
|
+
}
|
|
1867
|
+
nameFromFilename(filename) {
|
|
1868
|
+
this._name = path.basename(filename, path.extname(filename));
|
|
1869
|
+
return this;
|
|
1870
|
+
}
|
|
1871
|
+
executableDir(path2) {
|
|
1872
|
+
if (path2 === undefined)
|
|
1873
|
+
return this._executableDir;
|
|
1874
|
+
this._executableDir = path2;
|
|
1875
|
+
return this;
|
|
1876
|
+
}
|
|
1877
|
+
helpInformation(contextOptions) {
|
|
1878
|
+
const helper = this.createHelp();
|
|
1879
|
+
const context = this._getOutputContext(contextOptions);
|
|
1880
|
+
helper.prepareContext({
|
|
1881
|
+
error: context.error,
|
|
1882
|
+
helpWidth: context.helpWidth,
|
|
1883
|
+
outputHasColors: context.hasColors
|
|
1884
|
+
});
|
|
1885
|
+
const text = helper.formatHelp(this, helper);
|
|
1886
|
+
if (context.hasColors)
|
|
1887
|
+
return text;
|
|
1888
|
+
return this._outputConfiguration.stripColor(text);
|
|
1889
|
+
}
|
|
1890
|
+
_getOutputContext(contextOptions) {
|
|
1891
|
+
contextOptions = contextOptions || {};
|
|
1892
|
+
const error = !!contextOptions.error;
|
|
1893
|
+
let baseWrite;
|
|
1894
|
+
let hasColors;
|
|
1895
|
+
let helpWidth;
|
|
1896
|
+
if (error) {
|
|
1897
|
+
baseWrite = (str) => this._outputConfiguration.writeErr(str);
|
|
1898
|
+
hasColors = this._outputConfiguration.getErrHasColors();
|
|
1899
|
+
helpWidth = this._outputConfiguration.getErrHelpWidth();
|
|
1900
|
+
} else {
|
|
1901
|
+
baseWrite = (str) => this._outputConfiguration.writeOut(str);
|
|
1902
|
+
hasColors = this._outputConfiguration.getOutHasColors();
|
|
1903
|
+
helpWidth = this._outputConfiguration.getOutHelpWidth();
|
|
1904
|
+
}
|
|
1905
|
+
const write = (str) => {
|
|
1906
|
+
if (!hasColors)
|
|
1907
|
+
str = this._outputConfiguration.stripColor(str);
|
|
1908
|
+
return baseWrite(str);
|
|
1909
|
+
};
|
|
1910
|
+
return { error, write, hasColors, helpWidth };
|
|
1911
|
+
}
|
|
1912
|
+
outputHelp(contextOptions) {
|
|
1913
|
+
let deprecatedCallback;
|
|
1914
|
+
if (typeof contextOptions === "function") {
|
|
1915
|
+
deprecatedCallback = contextOptions;
|
|
1916
|
+
contextOptions = undefined;
|
|
1917
|
+
}
|
|
1918
|
+
const outputContext = this._getOutputContext(contextOptions);
|
|
1919
|
+
const eventContext = {
|
|
1920
|
+
error: outputContext.error,
|
|
1921
|
+
write: outputContext.write,
|
|
1922
|
+
command: this
|
|
1923
|
+
};
|
|
1924
|
+
this._getCommandAndAncestors().reverse().forEach((command) => command.emit("beforeAllHelp", eventContext));
|
|
1925
|
+
this.emit("beforeHelp", eventContext);
|
|
1926
|
+
let helpInformation = this.helpInformation({ error: outputContext.error });
|
|
1927
|
+
if (deprecatedCallback) {
|
|
1928
|
+
helpInformation = deprecatedCallback(helpInformation);
|
|
1929
|
+
if (typeof helpInformation !== "string" && !Buffer.isBuffer(helpInformation)) {
|
|
1930
|
+
throw new Error("outputHelp callback must return a string or a Buffer");
|
|
1931
|
+
}
|
|
1932
|
+
}
|
|
1933
|
+
outputContext.write(helpInformation);
|
|
1934
|
+
if (this._getHelpOption()?.long) {
|
|
1935
|
+
this.emit(this._getHelpOption().long);
|
|
1936
|
+
}
|
|
1937
|
+
this.emit("afterHelp", eventContext);
|
|
1938
|
+
this._getCommandAndAncestors().forEach((command) => command.emit("afterAllHelp", eventContext));
|
|
1939
|
+
}
|
|
1940
|
+
helpOption(flags, description) {
|
|
1941
|
+
if (typeof flags === "boolean") {
|
|
1942
|
+
if (flags) {
|
|
1943
|
+
if (this._helpOption === null)
|
|
1944
|
+
this._helpOption = undefined;
|
|
1945
|
+
if (this._defaultOptionGroup) {
|
|
1946
|
+
this._initOptionGroup(this._getHelpOption());
|
|
1947
|
+
}
|
|
1948
|
+
} else {
|
|
1949
|
+
this._helpOption = null;
|
|
1950
|
+
}
|
|
1951
|
+
return this;
|
|
1952
|
+
}
|
|
1953
|
+
this._helpOption = this.createOption(flags ?? "-h, --help", description ?? "display help for command");
|
|
1954
|
+
if (flags || description)
|
|
1955
|
+
this._initOptionGroup(this._helpOption);
|
|
1956
|
+
return this;
|
|
1957
|
+
}
|
|
1958
|
+
_getHelpOption() {
|
|
1959
|
+
if (this._helpOption === undefined) {
|
|
1960
|
+
this.helpOption(undefined, undefined);
|
|
1961
|
+
}
|
|
1962
|
+
return this._helpOption;
|
|
1963
|
+
}
|
|
1964
|
+
addHelpOption(option) {
|
|
1965
|
+
this._helpOption = option;
|
|
1966
|
+
this._initOptionGroup(option);
|
|
1967
|
+
return this;
|
|
1968
|
+
}
|
|
1969
|
+
help(contextOptions) {
|
|
1970
|
+
this.outputHelp(contextOptions);
|
|
1971
|
+
let exitCode = Number(process2.exitCode ?? 0);
|
|
1972
|
+
if (exitCode === 0 && contextOptions && typeof contextOptions !== "function" && contextOptions.error) {
|
|
1973
|
+
exitCode = 1;
|
|
1974
|
+
}
|
|
1975
|
+
this._exit(exitCode, "commander.help", "(outputHelp)");
|
|
1976
|
+
}
|
|
1977
|
+
addHelpText(position, text) {
|
|
1978
|
+
const allowedValues = ["beforeAll", "before", "after", "afterAll"];
|
|
1979
|
+
if (!allowedValues.includes(position)) {
|
|
1980
|
+
throw new Error(`Unexpected value for position to addHelpText.
|
|
1981
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
1982
|
+
}
|
|
1983
|
+
const helpEvent = `${position}Help`;
|
|
1984
|
+
this.on(helpEvent, (context) => {
|
|
1985
|
+
let helpStr;
|
|
1986
|
+
if (typeof text === "function") {
|
|
1987
|
+
helpStr = text({ error: context.error, command: context.command });
|
|
1988
|
+
} else {
|
|
1989
|
+
helpStr = text;
|
|
1990
|
+
}
|
|
1991
|
+
if (helpStr) {
|
|
1992
|
+
context.write(`${helpStr}
|
|
1993
|
+
`);
|
|
1994
|
+
}
|
|
1995
|
+
});
|
|
1996
|
+
return this;
|
|
1997
|
+
}
|
|
1998
|
+
_outputHelpIfRequested(args) {
|
|
1999
|
+
const helpOption = this._getHelpOption();
|
|
2000
|
+
const helpRequested = helpOption && args.find((arg) => helpOption.is(arg));
|
|
2001
|
+
if (helpRequested) {
|
|
2002
|
+
this.outputHelp();
|
|
2003
|
+
this._exit(0, "commander.helpDisplayed", "(outputHelp)");
|
|
2004
|
+
}
|
|
2005
|
+
}
|
|
2006
|
+
}
|
|
2007
|
+
function incrementNodeInspectorPort(args) {
|
|
2008
|
+
return args.map((arg) => {
|
|
2009
|
+
if (!arg.startsWith("--inspect")) {
|
|
2010
|
+
return arg;
|
|
2011
|
+
}
|
|
2012
|
+
let debugOption;
|
|
2013
|
+
let debugHost = "127.0.0.1";
|
|
2014
|
+
let debugPort = "9229";
|
|
2015
|
+
let match;
|
|
2016
|
+
if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
|
|
2017
|
+
debugOption = match[1];
|
|
2018
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
|
|
2019
|
+
debugOption = match[1];
|
|
2020
|
+
if (/^\d+$/.test(match[3])) {
|
|
2021
|
+
debugPort = match[3];
|
|
2022
|
+
} else {
|
|
2023
|
+
debugHost = match[3];
|
|
2024
|
+
}
|
|
2025
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) {
|
|
2026
|
+
debugOption = match[1];
|
|
2027
|
+
debugHost = match[3];
|
|
2028
|
+
debugPort = match[4];
|
|
2029
|
+
}
|
|
2030
|
+
if (debugOption && debugPort !== "0") {
|
|
2031
|
+
return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
|
|
2032
|
+
}
|
|
2033
|
+
return arg;
|
|
2034
|
+
});
|
|
2035
|
+
}
|
|
2036
|
+
function useColor() {
|
|
2037
|
+
if (process2.env.NO_COLOR || process2.env.FORCE_COLOR === "0" || process2.env.FORCE_COLOR === "false")
|
|
2038
|
+
return false;
|
|
2039
|
+
if (process2.env.FORCE_COLOR || process2.env.CLICOLOR_FORCE !== undefined)
|
|
2040
|
+
return true;
|
|
2041
|
+
return;
|
|
2042
|
+
}
|
|
2043
|
+
|
|
2044
|
+
// node_modules/commander/index.js
|
|
2045
|
+
var program = new Command;
|
|
2046
|
+
|
|
2047
|
+
// src/core/archive.ts
|
|
2048
|
+
import { existsSync, mkdirSync, readFileSync as readFileSync3, renameSync } from "node:fs";
|
|
2049
|
+
|
|
2050
|
+
// src/core/io.ts
|
|
2051
|
+
import { readFileSync, statSync } from "node:fs";
|
|
2052
|
+
import { homedir } from "node:os";
|
|
2053
|
+
import { join, resolve } from "node:path";
|
|
2054
|
+
function defaultHome() {
|
|
2055
|
+
return join(homedir(), ".theway");
|
|
2056
|
+
}
|
|
2057
|
+
function pointerPath() {
|
|
2058
|
+
const xdg = process.env.XDG_CONFIG_HOME ?? "";
|
|
2059
|
+
return join(xdg !== "" ? xdg : join(homedir(), ".config"), "theway", "home");
|
|
2060
|
+
}
|
|
2061
|
+
function readPointer() {
|
|
2062
|
+
try {
|
|
2063
|
+
return readFileSync(pointerPath(), "utf-8").trim();
|
|
2064
|
+
} catch {
|
|
2065
|
+
return "";
|
|
2066
|
+
}
|
|
2067
|
+
}
|
|
2068
|
+
function resolveHome() {
|
|
2069
|
+
const env = process.env.THEWAY_HOME ?? "";
|
|
2070
|
+
if (env !== "")
|
|
2071
|
+
return { home: resolve(env), source: "env" };
|
|
2072
|
+
const pointer = readPointer();
|
|
2073
|
+
if (pointer !== "")
|
|
2074
|
+
return { home: resolve(pointer), source: "pointer" };
|
|
2075
|
+
return { home: defaultHome(), source: "default" };
|
|
2076
|
+
}
|
|
2077
|
+
function thewayHome() {
|
|
2078
|
+
return resolveHome().home;
|
|
2079
|
+
}
|
|
2080
|
+
function isDir(path2) {
|
|
2081
|
+
try {
|
|
2082
|
+
return statSync(path2).isDirectory();
|
|
2083
|
+
} catch {
|
|
2084
|
+
return false;
|
|
2085
|
+
}
|
|
2086
|
+
}
|
|
2087
|
+
function isFile(path2) {
|
|
2088
|
+
try {
|
|
2089
|
+
return statSync(path2).isFile();
|
|
2090
|
+
} catch {
|
|
2091
|
+
return false;
|
|
2092
|
+
}
|
|
2093
|
+
}
|
|
2094
|
+
|
|
2095
|
+
// src/core/list.ts
|
|
2096
|
+
import { readdirSync, readFileSync as readFileSync2 } from "node:fs";
|
|
2097
|
+
var CHECKED = /^\s*-\s*\[x\]\s+[0-9]+[a-z]?\./;
|
|
2098
|
+
var UNCHECKED = /^\s*-\s*\[ \]\s+[0-9]+[a-z]?\./;
|
|
2099
|
+
function visibleNames(dir) {
|
|
2100
|
+
try {
|
|
2101
|
+
return readdirSync(dir).filter((n) => !n.startsWith("."));
|
|
2102
|
+
} catch {
|
|
2103
|
+
return [];
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
function subdirs(dir) {
|
|
2107
|
+
return visibleNames(dir).filter((n) => isDir(`${dir}/${n}`)).sort();
|
|
2108
|
+
}
|
|
2109
|
+
function filesEndingWith(dir, suffix) {
|
|
2110
|
+
return visibleNames(dir).filter((n) => n.endsWith(suffix) && isFile(`${dir}/${n}`));
|
|
2111
|
+
}
|
|
2112
|
+
function activeIds(home, project) {
|
|
2113
|
+
return subdirs(`${home}/${project}/changes`).filter((n) => n !== "archive");
|
|
2114
|
+
}
|
|
2115
|
+
function archiveDirs(home, project) {
|
|
2116
|
+
return subdirs(`${home}/${project}/changes/archive`);
|
|
2117
|
+
}
|
|
2118
|
+
function taskCounts(dir) {
|
|
2119
|
+
let checked = 0;
|
|
2120
|
+
let total = 0;
|
|
2121
|
+
if (!isFile(`${dir}/tasks.md`))
|
|
2122
|
+
return [0, 0];
|
|
2123
|
+
for (const line of readFileSync2(`${dir}/tasks.md`, "utf-8").split(`
|
|
2124
|
+
`)) {
|
|
2125
|
+
if (CHECKED.test(line)) {
|
|
2126
|
+
checked++;
|
|
2127
|
+
total++;
|
|
2128
|
+
} else if (UNCHECKED.test(line)) {
|
|
2129
|
+
total++;
|
|
2130
|
+
}
|
|
2131
|
+
}
|
|
2132
|
+
return [checked, total];
|
|
2133
|
+
}
|
|
2134
|
+
function cmdList(project) {
|
|
2135
|
+
const home = thewayHome();
|
|
2136
|
+
const base = `${home}/${project}/changes`;
|
|
2137
|
+
let text = `theway changes — ${project}
|
|
2138
|
+
|
|
2139
|
+
Active changes
|
|
2140
|
+
|
|
2141
|
+
`;
|
|
2142
|
+
const active = activeIds(home, project);
|
|
2143
|
+
for (const name of active) {
|
|
2144
|
+
const dir = `${base}/${name}`;
|
|
2145
|
+
const files = filesEndingWith(dir, ".md").length;
|
|
2146
|
+
const [checked, total] = taskCounts(dir);
|
|
2147
|
+
text += ` ${name.padEnd(20)} ${files} files ${checked}/${total} tasks done
|
|
2148
|
+
`;
|
|
2149
|
+
}
|
|
2150
|
+
if (active.length === 0)
|
|
2151
|
+
text += ` No active changes.
|
|
2152
|
+
`;
|
|
2153
|
+
text += `
|
|
2154
|
+
`;
|
|
2155
|
+
const archived = archiveDirs(home, project);
|
|
2156
|
+
text += `Archived changes (${archived.length})
|
|
2157
|
+
|
|
2158
|
+
`;
|
|
2159
|
+
if (archived.length > 0) {
|
|
2160
|
+
for (const name of archived)
|
|
2161
|
+
text += ` ${name}
|
|
2162
|
+
`;
|
|
2163
|
+
} else {
|
|
2164
|
+
text += ` No archived changes.
|
|
2165
|
+
`;
|
|
2166
|
+
}
|
|
2167
|
+
text += `
|
|
2168
|
+
Summary: ${active.length} active change(s), ${archived.length} archived.
|
|
2169
|
+
`;
|
|
2170
|
+
process.stdout.write(text);
|
|
2171
|
+
return 0;
|
|
2172
|
+
}
|
|
2173
|
+
|
|
2174
|
+
// src/core/archive.ts
|
|
2175
|
+
function localDate() {
|
|
2176
|
+
const d = new Date;
|
|
2177
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
2178
|
+
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
|
|
2179
|
+
}
|
|
2180
|
+
function uncheckedLines(path2) {
|
|
2181
|
+
return readFileSync3(path2, "utf-8").split(`
|
|
2182
|
+
`).flatMap((line, i) => UNCHECKED.test(line) ? [`${i + 1}:${line}`] : []);
|
|
2183
|
+
}
|
|
2184
|
+
function cmdArchive(project, changeId, force) {
|
|
2185
|
+
const home = thewayHome();
|
|
2186
|
+
const src = `${home}/${project}/changes/${changeId}`;
|
|
2187
|
+
const dst = `${home}/${project}/changes/archive/${localDate()}-${changeId}`;
|
|
2188
|
+
if (!isDir(src)) {
|
|
2189
|
+
process.stderr.write(`Error: ${src} not found
|
|
2190
|
+
`);
|
|
2191
|
+
return 1;
|
|
2192
|
+
}
|
|
2193
|
+
if (existsSync(dst)) {
|
|
2194
|
+
process.stderr.write(`Error: ${dst} already exists — not archiving.
|
|
2195
|
+
`);
|
|
2196
|
+
return 1;
|
|
2197
|
+
}
|
|
2198
|
+
if (isFile(`${src}/tasks.md`)) {
|
|
2199
|
+
const unchecked = uncheckedLines(`${src}/tasks.md`);
|
|
2200
|
+
if (unchecked.length > 0) {
|
|
2201
|
+
const list = `${unchecked.join(`
|
|
2202
|
+
`)}
|
|
2203
|
+
`;
|
|
2204
|
+
if (force) {
|
|
2205
|
+
process.stderr.write(`WARNING: archiving with unchecked tasks (--force):
|
|
2206
|
+
${list}`);
|
|
2207
|
+
} else {
|
|
2208
|
+
process.stderr.write(`Error: ${changeId} has unchecked tasks — not archiving.
|
|
2209
|
+
${list}`);
|
|
2210
|
+
process.stderr.write(`Complete or tick them in tasks.md, or re-run with --force.
|
|
2211
|
+
`);
|
|
2212
|
+
return 1;
|
|
2213
|
+
}
|
|
2214
|
+
}
|
|
2215
|
+
}
|
|
2216
|
+
try {
|
|
2217
|
+
mkdirSync(`${home}/${project}/changes/archive`, { recursive: true });
|
|
2218
|
+
renameSync(src, dst);
|
|
2219
|
+
} catch (e) {
|
|
2220
|
+
process.stderr.write(`mv: ${e.message}
|
|
2221
|
+
`);
|
|
2222
|
+
return 1;
|
|
2223
|
+
}
|
|
2224
|
+
process.stdout.write(`Archived: ${dst}
|
|
2225
|
+
`);
|
|
2226
|
+
return 0;
|
|
2227
|
+
}
|
|
2228
|
+
|
|
2229
|
+
// src/core/path.ts
|
|
2230
|
+
import { join as join2, resolve as resolve2 } from "node:path";
|
|
2231
|
+
function segmentReason(name) {
|
|
2232
|
+
if (name === "")
|
|
2233
|
+
return "must not be empty";
|
|
2234
|
+
if (name.includes("/"))
|
|
2235
|
+
return "must not contain /";
|
|
2236
|
+
if (name === "." || name === "..")
|
|
2237
|
+
return `must not be ${name}`;
|
|
2238
|
+
return "";
|
|
2239
|
+
}
|
|
2240
|
+
function cmdPath(project, changeId, verbose) {
|
|
2241
|
+
const segments = [
|
|
2242
|
+
["project", project],
|
|
2243
|
+
["change id", changeId]
|
|
2244
|
+
];
|
|
2245
|
+
for (const [label, value] of segments) {
|
|
2246
|
+
if (value === undefined)
|
|
2247
|
+
continue;
|
|
2248
|
+
const reason = segmentReason(value);
|
|
2249
|
+
if (reason !== "") {
|
|
2250
|
+
const shown = value.replaceAll(`
|
|
2251
|
+
`, "\\n").replaceAll("\r", "\\r");
|
|
2252
|
+
process.stderr.write(`theway: invalid ${label} '${shown}': ${reason}
|
|
2253
|
+
`);
|
|
2254
|
+
return 1;
|
|
2255
|
+
}
|
|
2256
|
+
}
|
|
2257
|
+
const { home, source } = resolveHome();
|
|
2258
|
+
if (verbose)
|
|
2259
|
+
process.stderr.write(`theway: home ${home} (from ${source})
|
|
2260
|
+
`);
|
|
2261
|
+
if (source === "env") {
|
|
2262
|
+
const pointer = readPointer();
|
|
2263
|
+
if (pointer !== "" && resolve2(pointer) !== home) {
|
|
2264
|
+
process.stderr.write(`theway: warning: THEWAY_HOME is ${home} (from env) but ${pointerPath()} holds ${resolve2(pointer)} (from pointer); using ${home}
|
|
2265
|
+
`);
|
|
2266
|
+
}
|
|
2267
|
+
}
|
|
2268
|
+
let out = home;
|
|
2269
|
+
if (project !== undefined)
|
|
2270
|
+
out = join2(home, project);
|
|
2271
|
+
if (project !== undefined && changeId !== undefined)
|
|
2272
|
+
out = join2(home, project, "changes", changeId);
|
|
2273
|
+
process.stdout.write(`${out}
|
|
2274
|
+
`);
|
|
2275
|
+
return 0;
|
|
2276
|
+
}
|
|
2277
|
+
|
|
2278
|
+
// src/core/validate-id.ts
|
|
2279
|
+
var VERB = /^(add|update|refactor|remove)-/;
|
|
2280
|
+
var KEBAB = /^(add|update|refactor|remove)-[a-z0-9]+(-[a-z0-9]+)*$/;
|
|
2281
|
+
var SERIES = /^(add|update|refactor|remove)-[a-z0-9]+(-[a-z0-9]+)*-[0-9]{2}$/;
|
|
2282
|
+
var last = (id) => id.slice(id.lastIndexOf("-") + 1);
|
|
2283
|
+
function formatReason(id) {
|
|
2284
|
+
if (id === "")
|
|
2285
|
+
return "empty change id";
|
|
2286
|
+
if (/[A-Z]/.test(id))
|
|
2287
|
+
return "uppercase letters are not allowed; use lowercase kebab-case";
|
|
2288
|
+
if (id.includes("_"))
|
|
2289
|
+
return "underscores are not allowed; use kebab-case (-)";
|
|
2290
|
+
if (!VERB.test(id))
|
|
2291
|
+
return "must start with a verb prefix: add-, update-, refactor- or remove-";
|
|
2292
|
+
if (!KEBAB.test(id))
|
|
2293
|
+
return "must be kebab-case: lowercase [a-z0-9] segments joined by single hyphens";
|
|
2294
|
+
return "";
|
|
2295
|
+
}
|
|
2296
|
+
function standaloneReason(id) {
|
|
2297
|
+
const reason = formatReason(id);
|
|
2298
|
+
if (reason !== "")
|
|
2299
|
+
return reason;
|
|
2300
|
+
if (/-[0-9]{2}$/.test(id))
|
|
2301
|
+
return `ends in an ordinal (-${last(id)}) but is not a series member; drop it or pass --series`;
|
|
2302
|
+
if (/^[0-9]{1,2}$/.test(last(id))) {
|
|
2303
|
+
return `ends in a number (-${last(id)}); ordinals are only for series members (--series), as -NN`;
|
|
2304
|
+
}
|
|
2305
|
+
return "";
|
|
2306
|
+
}
|
|
2307
|
+
function seriesReason(id) {
|
|
2308
|
+
const reason = formatReason(id);
|
|
2309
|
+
if (reason !== "")
|
|
2310
|
+
return reason;
|
|
2311
|
+
if (/^[a-z]$/.test(last(id))) {
|
|
2312
|
+
return `letter suffix (-${last(id)}) is not allowed; series members end in a two-digit ordinal (-NN, e.g. -01)`;
|
|
2313
|
+
}
|
|
2314
|
+
if (!SERIES.test(id))
|
|
2315
|
+
return "series member must end in a zero-padded two-digit ordinal (-NN, e.g. -01)";
|
|
2316
|
+
if (last(id) === "00")
|
|
2317
|
+
return "series ordinal must start at 01, not 00";
|
|
2318
|
+
const double = /-([0-9]{2})-[0-9]{2}$/.exec(id);
|
|
2319
|
+
if (double !== null) {
|
|
2320
|
+
return `double ordinal (-${double[1]}-${last(id)}); a series member carries exactly one -NN`;
|
|
2321
|
+
}
|
|
2322
|
+
return "";
|
|
2323
|
+
}
|
|
2324
|
+
function cmdValidateId(id, series) {
|
|
2325
|
+
const reason = series ? seriesReason(id) : standaloneReason(id);
|
|
2326
|
+
if (reason !== "") {
|
|
2327
|
+
const shown = id.replaceAll(`
|
|
2328
|
+
`, "\\n").replaceAll("\r", "\\r");
|
|
2329
|
+
process.stderr.write(`invalid change id '${shown}': ${reason}
|
|
2330
|
+
`);
|
|
2331
|
+
return 1;
|
|
2332
|
+
}
|
|
2333
|
+
return 0;
|
|
2334
|
+
}
|
|
2335
|
+
|
|
2336
|
+
// src/install.ts
|
|
2337
|
+
import {
|
|
2338
|
+
accessSync,
|
|
2339
|
+
chmodSync,
|
|
2340
|
+
constants,
|
|
2341
|
+
copyFileSync as copyFileSync2,
|
|
2342
|
+
cpSync as cpSync2,
|
|
2343
|
+
existsSync as existsSync3,
|
|
2344
|
+
lstatSync,
|
|
2345
|
+
mkdirSync as mkdirSync3,
|
|
2346
|
+
readFileSync as readFileSync4,
|
|
2347
|
+
realpathSync,
|
|
2348
|
+
renameSync as renameSync2,
|
|
2349
|
+
rmdirSync,
|
|
2350
|
+
rmSync,
|
|
2351
|
+
writeFileSync
|
|
2352
|
+
} from "node:fs";
|
|
2353
|
+
import { homedir as homedir2 } from "node:os";
|
|
2354
|
+
import { delimiter, dirname, join as join3, resolve as resolve3 } from "node:path";
|
|
2355
|
+
import { fileURLToPath } from "node:url";
|
|
2356
|
+
|
|
2357
|
+
// src/core/init.ts
|
|
2358
|
+
import { copyFileSync, cpSync, existsSync as existsSync2, mkdirSync as mkdirSync2 } from "node:fs";
|
|
2359
|
+
function init(src, home) {
|
|
2360
|
+
try {
|
|
2361
|
+
mkdirSync2(home, { recursive: true });
|
|
2362
|
+
copyFileSync(`${src}/config.yaml`, `${home}/config.default.yaml`);
|
|
2363
|
+
if (existsSync2(`${home}/config.yaml`)) {
|
|
2364
|
+
process.stdout.write(`Kept existing config.yaml (shipped defaults: config.default.yaml)
|
|
2365
|
+
`);
|
|
2366
|
+
} else {
|
|
2367
|
+
copyFileSync(`${src}/config.yaml`, `${home}/config.yaml`);
|
|
2368
|
+
}
|
|
2369
|
+
cpSync(`${src}/data/standards`, `${home}/data/standards`, { recursive: true });
|
|
2370
|
+
cpSync(`${src}/templates`, `${home}/templates`, { recursive: true });
|
|
2371
|
+
} catch (e) {
|
|
2372
|
+
process.stderr.write(`theway: ${e.message}
|
|
2373
|
+
`);
|
|
2374
|
+
return 1;
|
|
2375
|
+
}
|
|
2376
|
+
process.stdout.write(`Initialized ${home}
|
|
2377
|
+
`);
|
|
2378
|
+
return 0;
|
|
2379
|
+
}
|
|
2380
|
+
|
|
2381
|
+
// src/install.ts
|
|
2382
|
+
var self = fileURLToPath(import.meta.url);
|
|
2383
|
+
var pkgRoot = dirname(dirname(self));
|
|
2384
|
+
var plugin = join3(pkgRoot, "plugin");
|
|
2385
|
+
var PLUGIN_DIRS = [".claude-plugin", "commands"];
|
|
2386
|
+
var BINARY_MARKER = "theway-cli-binary:pixelfactoryio/theway";
|
|
2387
|
+
function claudeHome() {
|
|
2388
|
+
const dir = (process.env.CLAUDE_CONFIG_DIR ?? "").trim();
|
|
2389
|
+
return dir !== "" ? dir : join3(homedir2(), ".claude");
|
|
2390
|
+
}
|
|
2391
|
+
function installDir() {
|
|
2392
|
+
const dir = process.env.THEWAY_INSTALL_DIR ?? "";
|
|
2393
|
+
return dir !== "" ? resolve3(dir) : join3(homedir2(), ".local", "bin");
|
|
2394
|
+
}
|
|
2395
|
+
function shownHome(home) {
|
|
2396
|
+
return resolve3(home) === resolve3(defaultHome()) ? "~/.theway" : resolve3(home);
|
|
2397
|
+
}
|
|
2398
|
+
function homeRules(home) {
|
|
2399
|
+
const at = shownHome(home);
|
|
2400
|
+
const root = at.startsWith("~/") ? at : `/${at}`;
|
|
2401
|
+
return [`Read(${root}/**)`, `Edit(${root}/*/changes/**)`];
|
|
2402
|
+
}
|
|
2403
|
+
function rules(home) {
|
|
2404
|
+
return ["Bash(theway:*)", ...homeRules(home)];
|
|
2405
|
+
}
|
|
2406
|
+
function recordedHome() {
|
|
2407
|
+
const pointer = readPointer();
|
|
2408
|
+
return pointer !== "" ? resolve3(pointer) : defaultHome();
|
|
2409
|
+
}
|
|
2410
|
+
function legacyRules(home) {
|
|
2411
|
+
return [...new Set(["Bash(~/.theway/bin/theway:*)", `Bash(${shownHome(home)}/bin/theway:*)`])];
|
|
2412
|
+
}
|
|
2413
|
+
function fail(line) {
|
|
2414
|
+
process.stderr.write(`${line}
|
|
2415
|
+
`);
|
|
2416
|
+
return 1;
|
|
2417
|
+
}
|
|
2418
|
+
function isObject(v) {
|
|
2419
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
2420
|
+
}
|
|
2421
|
+
function readSettings(path2) {
|
|
2422
|
+
if (!existsSync3(path2))
|
|
2423
|
+
return {};
|
|
2424
|
+
let data;
|
|
2425
|
+
try {
|
|
2426
|
+
data = JSON.parse(readFileSync4(path2, "utf-8"));
|
|
2427
|
+
} catch {
|
|
2428
|
+
return null;
|
|
2429
|
+
}
|
|
2430
|
+
if (!isObject(data))
|
|
2431
|
+
return null;
|
|
2432
|
+
const perms = data.permissions;
|
|
2433
|
+
if (perms !== undefined) {
|
|
2434
|
+
if (!isObject(perms))
|
|
2435
|
+
return null;
|
|
2436
|
+
if (perms.allow !== undefined && !Array.isArray(perms.allow))
|
|
2437
|
+
return null;
|
|
2438
|
+
}
|
|
2439
|
+
return data;
|
|
2440
|
+
}
|
|
2441
|
+
function writeSettings(path2, settings) {
|
|
2442
|
+
const tmp = `${path2}.tmp-${process.pid}`;
|
|
2443
|
+
writeFileSync(tmp, `${JSON.stringify(settings, null, 2)}
|
|
2444
|
+
`);
|
|
2445
|
+
renameSync2(tmp, path2);
|
|
2446
|
+
}
|
|
2447
|
+
function invalidSettings(path2) {
|
|
2448
|
+
return fail(`theway: ${path2} is not valid settings JSON; nothing was changed`);
|
|
2449
|
+
}
|
|
2450
|
+
function copyPlugin(home) {
|
|
2451
|
+
const tmp = join3(home, `.theway-install-${process.pid}`);
|
|
2452
|
+
const old = join3(home, `.theway-old-${process.pid}`);
|
|
2453
|
+
const dest = join3(home, "skills", "theway");
|
|
2454
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
2455
|
+
try {
|
|
2456
|
+
for (const dir of PLUGIN_DIRS) {
|
|
2457
|
+
cpSync2(join3(plugin, dir), join3(tmp, dir), { recursive: true });
|
|
2458
|
+
}
|
|
2459
|
+
mkdirSync3(join3(home, "skills"), { recursive: true });
|
|
2460
|
+
if (existsSync3(dest))
|
|
2461
|
+
renameSync2(dest, old);
|
|
2462
|
+
renameSync2(tmp, dest);
|
|
2463
|
+
} finally {
|
|
2464
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
2465
|
+
}
|
|
2466
|
+
rmSync(old, { recursive: true, force: true });
|
|
2467
|
+
return dest;
|
|
2468
|
+
}
|
|
2469
|
+
function removeIfEmpty(dir) {
|
|
2470
|
+
try {
|
|
2471
|
+
rmdirSync(dir);
|
|
2472
|
+
} catch {}
|
|
2473
|
+
}
|
|
2474
|
+
function removeLegacyBin(home) {
|
|
2475
|
+
for (const h of new Set([defaultHome(), resolve3(home)])) {
|
|
2476
|
+
const bin = join3(h, "bin");
|
|
2477
|
+
for (const f of ["theway", "theway.js"])
|
|
2478
|
+
rmSync(join3(bin, f), { force: true });
|
|
2479
|
+
removeIfEmpty(bin);
|
|
2480
|
+
}
|
|
2481
|
+
}
|
|
2482
|
+
function writeBinary(dir) {
|
|
2483
|
+
const dest = join3(dir, "theway");
|
|
2484
|
+
const tmp = join3(dir, `.theway-${process.pid}`);
|
|
2485
|
+
mkdirSync3(dir, { recursive: true });
|
|
2486
|
+
try {
|
|
2487
|
+
copyFileSync2(self, tmp);
|
|
2488
|
+
chmodSync(tmp, 493);
|
|
2489
|
+
renameSync2(tmp, dest);
|
|
2490
|
+
} finally {
|
|
2491
|
+
rmSync(tmp, { force: true });
|
|
2492
|
+
}
|
|
2493
|
+
return dest;
|
|
2494
|
+
}
|
|
2495
|
+
function occupied(path2) {
|
|
2496
|
+
try {
|
|
2497
|
+
lstatSync(path2);
|
|
2498
|
+
return true;
|
|
2499
|
+
} catch {
|
|
2500
|
+
return false;
|
|
2501
|
+
}
|
|
2502
|
+
}
|
|
2503
|
+
function isThewayBinary(path2) {
|
|
2504
|
+
if (!isFile(path2))
|
|
2505
|
+
return false;
|
|
2506
|
+
try {
|
|
2507
|
+
const text = readFileSync4(path2, "utf-8");
|
|
2508
|
+
return text.startsWith("#!/usr/bin/env node") && text.includes(BINARY_MARKER);
|
|
2509
|
+
} catch {
|
|
2510
|
+
return false;
|
|
2511
|
+
}
|
|
2512
|
+
}
|
|
2513
|
+
function writePointer(home, custom) {
|
|
2514
|
+
const path2 = pointerPath();
|
|
2515
|
+
if (custom) {
|
|
2516
|
+
mkdirSync3(dirname(path2), { recursive: true });
|
|
2517
|
+
writeFileSync(path2, `${home}
|
|
2518
|
+
`);
|
|
2519
|
+
} else {
|
|
2520
|
+
rmSync(path2, { force: true });
|
|
2521
|
+
}
|
|
2522
|
+
}
|
|
2523
|
+
function realpathOr(path2) {
|
|
2524
|
+
try {
|
|
2525
|
+
return realpathSync(path2);
|
|
2526
|
+
} catch {
|
|
2527
|
+
return resolve3(path2);
|
|
2528
|
+
}
|
|
2529
|
+
}
|
|
2530
|
+
function copiesOnPath() {
|
|
2531
|
+
const running = realpathOr(self);
|
|
2532
|
+
const seen = new Set;
|
|
2533
|
+
const copies = [];
|
|
2534
|
+
for (const dir of (process.env.PATH ?? "").split(delimiter)) {
|
|
2535
|
+
if (dir === "")
|
|
2536
|
+
continue;
|
|
2537
|
+
const candidate = join3(dir, "theway");
|
|
2538
|
+
if (!isFile(candidate))
|
|
2539
|
+
continue;
|
|
2540
|
+
try {
|
|
2541
|
+
accessSync(candidate, constants.X_OK);
|
|
2542
|
+
} catch {
|
|
2543
|
+
continue;
|
|
2544
|
+
}
|
|
2545
|
+
const real = realpathOr(candidate);
|
|
2546
|
+
if (real === running || seen.has(real))
|
|
2547
|
+
continue;
|
|
2548
|
+
seen.add(real);
|
|
2549
|
+
copies.push(candidate);
|
|
2550
|
+
}
|
|
2551
|
+
return copies;
|
|
2552
|
+
}
|
|
2553
|
+
function checkPath(dir, binary) {
|
|
2554
|
+
const dirs = (process.env.PATH ?? "").split(delimiter).filter((d) => d !== "");
|
|
2555
|
+
if (!dirs.some((d) => resolve3(d) === dir)) {
|
|
2556
|
+
process.stdout.write(`Add ${dir} to your PATH, e.g.: export PATH="${dir}:$PATH"
|
|
2557
|
+
`);
|
|
2558
|
+
}
|
|
2559
|
+
const copies = copiesOnPath();
|
|
2560
|
+
if (copies.length > 0 && realpathOr(copies[0]) !== realpathOr(binary)) {
|
|
2561
|
+
process.stderr.write(`theway: warning: ${copies[0]} comes before ${binary} on PATH, so the commands run it; theway copies on PATH: ${copies.join(", ")}
|
|
2562
|
+
`);
|
|
2563
|
+
}
|
|
2564
|
+
}
|
|
2565
|
+
function install() {
|
|
2566
|
+
if (!existsSync3(join3(plugin, ".claude-plugin"))) {
|
|
2567
|
+
return fail("theway: run `npx --yes github:pixelfactoryio/theway install` to install or update theway");
|
|
2568
|
+
}
|
|
2569
|
+
const cHome = claudeHome();
|
|
2570
|
+
const tHome = resolveHome().home;
|
|
2571
|
+
const previous = recordedHome();
|
|
2572
|
+
const custom = tHome !== resolve3(defaultHome());
|
|
2573
|
+
const dir = installDir();
|
|
2574
|
+
const settingsPath = join3(cHome, "settings.json");
|
|
2575
|
+
const settings = readSettings(settingsPath);
|
|
2576
|
+
if (settings === null)
|
|
2577
|
+
return invalidSettings(settingsPath);
|
|
2578
|
+
const target = join3(dir, "theway");
|
|
2579
|
+
if (occupied(target) && !isThewayBinary(target)) {
|
|
2580
|
+
return fail(`theway: ${target} exists and is not a theway binary; nothing was changed. Remove it, or set THEWAY_INSTALL_DIR to another folder`);
|
|
2581
|
+
}
|
|
2582
|
+
const dest = copyPlugin(cHome);
|
|
2583
|
+
let binary;
|
|
2584
|
+
try {
|
|
2585
|
+
removeLegacyBin(tHome);
|
|
2586
|
+
binary = writeBinary(dir);
|
|
2587
|
+
} catch (e) {
|
|
2588
|
+
return fail(`theway: ${e.message}`);
|
|
2589
|
+
}
|
|
2590
|
+
const status = init(join3(plugin, "templates", "theway"), tHome);
|
|
2591
|
+
if (status !== 0)
|
|
2592
|
+
return status;
|
|
2593
|
+
try {
|
|
2594
|
+
writePointer(tHome, custom);
|
|
2595
|
+
} catch (e) {
|
|
2596
|
+
return fail(`theway: ${e.message}`);
|
|
2597
|
+
}
|
|
2598
|
+
const perms = settings.permissions ?? {};
|
|
2599
|
+
const allow = perms.allow ?? [];
|
|
2600
|
+
const current = rules(tHome);
|
|
2601
|
+
const stale = [...legacyRules(tHome), ...homeRules(previous).filter((r) => !current.includes(r))];
|
|
2602
|
+
const kept = allow.filter((r) => !(typeof r === "string" && stale.includes(r)));
|
|
2603
|
+
const missing = current.filter((r) => !kept.includes(r));
|
|
2604
|
+
if (missing.length > 0 || kept.length !== allow.length) {
|
|
2605
|
+
settings.permissions = { ...perms, allow: [...kept, ...missing] };
|
|
2606
|
+
mkdirSync3(cHome, { recursive: true });
|
|
2607
|
+
writeSettings(settingsPath, settings);
|
|
2608
|
+
}
|
|
2609
|
+
checkPath(dir, binary);
|
|
2610
|
+
const version = JSON.parse(readFileSync4(join3(pkgRoot, "package.json"), "utf-8")).version;
|
|
2611
|
+
process.stdout.write(`Installed theway ${version} to ${dest} (theway@skills-dir), ${binary} and ${tHome}. Start a new Claude Code session or run /reload-plugins.
|
|
2612
|
+
`);
|
|
2613
|
+
return 0;
|
|
2614
|
+
}
|
|
2615
|
+
function uninstall() {
|
|
2616
|
+
const cHome = claudeHome();
|
|
2617
|
+
const tHome = resolveHome().home;
|
|
2618
|
+
const homes = [...new Set([tHome, recordedHome(), defaultHome()])];
|
|
2619
|
+
const settingsPath = join3(cHome, "settings.json");
|
|
2620
|
+
rmSync(join3(cHome, "skills", "theway"), { recursive: true, force: true });
|
|
2621
|
+
const binary = join3(installDir(), "theway");
|
|
2622
|
+
const removed = isThewayBinary(binary);
|
|
2623
|
+
if (removed)
|
|
2624
|
+
rmSync(binary, { force: true });
|
|
2625
|
+
rmSync(pointerPath(), { force: true });
|
|
2626
|
+
removeIfEmpty(dirname(pointerPath()));
|
|
2627
|
+
if (existsSync3(settingsPath)) {
|
|
2628
|
+
const settings = readSettings(settingsPath);
|
|
2629
|
+
if (settings === null)
|
|
2630
|
+
return invalidSettings(settingsPath);
|
|
2631
|
+
const allow = settings.permissions?.allow;
|
|
2632
|
+
if (Array.isArray(allow)) {
|
|
2633
|
+
const drop = homes.flatMap((h) => [...rules(h), ...legacyRules(h)]);
|
|
2634
|
+
const kept = allow.filter((r) => !(typeof r === "string" && drop.includes(r)));
|
|
2635
|
+
if (kept.length !== allow.length) {
|
|
2636
|
+
settings.permissions = { ...settings.permissions, allow: kept };
|
|
2637
|
+
writeSettings(settingsPath, settings);
|
|
2638
|
+
}
|
|
2639
|
+
}
|
|
2640
|
+
}
|
|
2641
|
+
const bin = removed ? ` and ${binary}` : "";
|
|
2642
|
+
process.stdout.write(`Removed theway from ${cHome}${bin}. ${tHome} was kept.
|
|
2643
|
+
`);
|
|
2644
|
+
return 0;
|
|
2645
|
+
}
|
|
2646
|
+
|
|
2647
|
+
// src/program.ts
|
|
2648
|
+
var nonEmpty = (value) => {
|
|
2649
|
+
if (value === "")
|
|
2650
|
+
throw new InvalidArgumentError("must not be empty");
|
|
2651
|
+
return value;
|
|
2652
|
+
};
|
|
2653
|
+
var exit = (code) => {
|
|
2654
|
+
process.exitCode = code;
|
|
2655
|
+
};
|
|
2656
|
+
function buildProgram() {
|
|
2657
|
+
const program2 = new Command("theway");
|
|
2658
|
+
program2.command("install").action(() => exit(install()));
|
|
2659
|
+
program2.command("uninstall").action(() => exit(uninstall()));
|
|
2660
|
+
program2.command("list").argument("<project>", "", nonEmpty).action((project) => exit(cmdList(project)));
|
|
2661
|
+
program2.command("archive").argument("<project>", "", nonEmpty).argument("<change-id>", "", nonEmpty).option("--force").action((project, change, o) => exit(cmdArchive(project, change, o.force === true)));
|
|
2662
|
+
program2.command("path").argument("[project]").argument("[change-id]").option("--verbose").action((project, change, o) => exit(cmdPath(project, change, o.verbose === true)));
|
|
2663
|
+
program2.command("validate-id").argument("<id>").option("--series").action((id, o) => exit(cmdValidateId(id, o.series === true)));
|
|
2664
|
+
return program2;
|
|
2665
|
+
}
|
|
2666
|
+
|
|
2667
|
+
// src/cli.ts
|
|
2668
|
+
var [major, minor] = process.versions.node.split(".").map(Number);
|
|
2669
|
+
if (major < 22 || major === 22 && minor < 12) {
|
|
2670
|
+
process.stderr.write(`theway requires Node.js 22.12 or later (found v${process.versions.node})
|
|
2671
|
+
`);
|
|
2672
|
+
process.exit(1);
|
|
2673
|
+
}
|
|
2674
|
+
buildProgram().parse();
|