@justestif/pk 0.1.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/README.md +94 -0
- package/dist/index.js +3109 -0
- package/package.json +35 -0
- package/skill/SKILL.md +68 -0
- package/skill/assets/templates/decision.md +29 -0
- package/skill/assets/templates/index.md +25 -0
- package/skill/assets/templates/note.md +25 -0
- package/skill/assets/templates/question.md +25 -0
- package/skill/assets/templates/source.md +21 -0
- package/skill/references/git-workflow.md +77 -0
- package/skill/references/knowledge-model.md +127 -0
- package/skill/references/source-principles.md +73 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,3109 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// @bun
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
function __accessProp(key) {
|
|
9
|
+
return this[key];
|
|
10
|
+
}
|
|
11
|
+
var __toESMCache_node;
|
|
12
|
+
var __toESMCache_esm;
|
|
13
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
14
|
+
var canCache = mod != null && typeof mod === "object";
|
|
15
|
+
if (canCache) {
|
|
16
|
+
var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
|
|
17
|
+
var cached = cache.get(mod);
|
|
18
|
+
if (cached)
|
|
19
|
+
return cached;
|
|
20
|
+
}
|
|
21
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
22
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
23
|
+
for (let key of __getOwnPropNames(mod))
|
|
24
|
+
if (!__hasOwnProp.call(to, key))
|
|
25
|
+
__defProp(to, key, {
|
|
26
|
+
get: __accessProp.bind(mod, key),
|
|
27
|
+
enumerable: true
|
|
28
|
+
});
|
|
29
|
+
if (canCache)
|
|
30
|
+
cache.set(mod, to);
|
|
31
|
+
return to;
|
|
32
|
+
};
|
|
33
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
34
|
+
var __require = import.meta.require;
|
|
35
|
+
|
|
36
|
+
// node_modules/commander/lib/error.js
|
|
37
|
+
var require_error = __commonJS((exports) => {
|
|
38
|
+
class CommanderError extends Error {
|
|
39
|
+
constructor(exitCode, code, message) {
|
|
40
|
+
super(message);
|
|
41
|
+
Error.captureStackTrace(this, this.constructor);
|
|
42
|
+
this.name = this.constructor.name;
|
|
43
|
+
this.code = code;
|
|
44
|
+
this.exitCode = exitCode;
|
|
45
|
+
this.nestedError = undefined;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
class InvalidArgumentError extends CommanderError {
|
|
50
|
+
constructor(message) {
|
|
51
|
+
super(1, "commander.invalidArgument", message);
|
|
52
|
+
Error.captureStackTrace(this, this.constructor);
|
|
53
|
+
this.name = this.constructor.name;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.CommanderError = CommanderError;
|
|
57
|
+
exports.InvalidArgumentError = InvalidArgumentError;
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// node_modules/commander/lib/argument.js
|
|
61
|
+
var require_argument = __commonJS((exports) => {
|
|
62
|
+
var { InvalidArgumentError } = require_error();
|
|
63
|
+
|
|
64
|
+
class Argument {
|
|
65
|
+
constructor(name, description) {
|
|
66
|
+
this.description = description || "";
|
|
67
|
+
this.variadic = false;
|
|
68
|
+
this.parseArg = undefined;
|
|
69
|
+
this.defaultValue = undefined;
|
|
70
|
+
this.defaultValueDescription = undefined;
|
|
71
|
+
this.argChoices = undefined;
|
|
72
|
+
switch (name[0]) {
|
|
73
|
+
case "<":
|
|
74
|
+
this.required = true;
|
|
75
|
+
this._name = name.slice(1, -1);
|
|
76
|
+
break;
|
|
77
|
+
case "[":
|
|
78
|
+
this.required = false;
|
|
79
|
+
this._name = name.slice(1, -1);
|
|
80
|
+
break;
|
|
81
|
+
default:
|
|
82
|
+
this.required = true;
|
|
83
|
+
this._name = name;
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
if (this._name.endsWith("...")) {
|
|
87
|
+
this.variadic = true;
|
|
88
|
+
this._name = this._name.slice(0, -3);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
name() {
|
|
92
|
+
return this._name;
|
|
93
|
+
}
|
|
94
|
+
_collectValue(value, previous) {
|
|
95
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
96
|
+
return [value];
|
|
97
|
+
}
|
|
98
|
+
previous.push(value);
|
|
99
|
+
return previous;
|
|
100
|
+
}
|
|
101
|
+
default(value, description) {
|
|
102
|
+
this.defaultValue = value;
|
|
103
|
+
this.defaultValueDescription = description;
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
argParser(fn) {
|
|
107
|
+
this.parseArg = fn;
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
choices(values) {
|
|
111
|
+
this.argChoices = values.slice();
|
|
112
|
+
this.parseArg = (arg, previous) => {
|
|
113
|
+
if (!this.argChoices.includes(arg)) {
|
|
114
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
115
|
+
}
|
|
116
|
+
if (this.variadic) {
|
|
117
|
+
return this._collectValue(arg, previous);
|
|
118
|
+
}
|
|
119
|
+
return arg;
|
|
120
|
+
};
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
argRequired() {
|
|
124
|
+
this.required = true;
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
argOptional() {
|
|
128
|
+
this.required = false;
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function humanReadableArgName(arg) {
|
|
133
|
+
const nameOutput = arg.name() + (arg.variadic === true ? "..." : "");
|
|
134
|
+
return arg.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
|
|
135
|
+
}
|
|
136
|
+
exports.Argument = Argument;
|
|
137
|
+
exports.humanReadableArgName = humanReadableArgName;
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// node_modules/commander/lib/help.js
|
|
141
|
+
var require_help = __commonJS((exports) => {
|
|
142
|
+
var { humanReadableArgName } = require_argument();
|
|
143
|
+
|
|
144
|
+
class Help {
|
|
145
|
+
constructor() {
|
|
146
|
+
this.helpWidth = undefined;
|
|
147
|
+
this.minWidthToWrap = 40;
|
|
148
|
+
this.sortSubcommands = false;
|
|
149
|
+
this.sortOptions = false;
|
|
150
|
+
this.showGlobalOptions = false;
|
|
151
|
+
}
|
|
152
|
+
prepareContext(contextOptions) {
|
|
153
|
+
this.helpWidth = this.helpWidth ?? contextOptions.helpWidth ?? 80;
|
|
154
|
+
}
|
|
155
|
+
visibleCommands(cmd) {
|
|
156
|
+
const visibleCommands = cmd.commands.filter((cmd2) => !cmd2._hidden);
|
|
157
|
+
const helpCommand = cmd._getHelpCommand();
|
|
158
|
+
if (helpCommand && !helpCommand._hidden) {
|
|
159
|
+
visibleCommands.push(helpCommand);
|
|
160
|
+
}
|
|
161
|
+
if (this.sortSubcommands) {
|
|
162
|
+
visibleCommands.sort((a, b) => {
|
|
163
|
+
return a.name().localeCompare(b.name());
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
return visibleCommands;
|
|
167
|
+
}
|
|
168
|
+
compareOptions(a, b) {
|
|
169
|
+
const getSortKey = (option) => {
|
|
170
|
+
return option.short ? option.short.replace(/^-/, "") : option.long.replace(/^--/, "");
|
|
171
|
+
};
|
|
172
|
+
return getSortKey(a).localeCompare(getSortKey(b));
|
|
173
|
+
}
|
|
174
|
+
visibleOptions(cmd) {
|
|
175
|
+
const visibleOptions = cmd.options.filter((option) => !option.hidden);
|
|
176
|
+
const helpOption = cmd._getHelpOption();
|
|
177
|
+
if (helpOption && !helpOption.hidden) {
|
|
178
|
+
const removeShort = helpOption.short && cmd._findOption(helpOption.short);
|
|
179
|
+
const removeLong = helpOption.long && cmd._findOption(helpOption.long);
|
|
180
|
+
if (!removeShort && !removeLong) {
|
|
181
|
+
visibleOptions.push(helpOption);
|
|
182
|
+
} else if (helpOption.long && !removeLong) {
|
|
183
|
+
visibleOptions.push(cmd.createOption(helpOption.long, helpOption.description));
|
|
184
|
+
} else if (helpOption.short && !removeShort) {
|
|
185
|
+
visibleOptions.push(cmd.createOption(helpOption.short, helpOption.description));
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (this.sortOptions) {
|
|
189
|
+
visibleOptions.sort(this.compareOptions);
|
|
190
|
+
}
|
|
191
|
+
return visibleOptions;
|
|
192
|
+
}
|
|
193
|
+
visibleGlobalOptions(cmd) {
|
|
194
|
+
if (!this.showGlobalOptions)
|
|
195
|
+
return [];
|
|
196
|
+
const globalOptions = [];
|
|
197
|
+
for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
198
|
+
const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
|
|
199
|
+
globalOptions.push(...visibleOptions);
|
|
200
|
+
}
|
|
201
|
+
if (this.sortOptions) {
|
|
202
|
+
globalOptions.sort(this.compareOptions);
|
|
203
|
+
}
|
|
204
|
+
return globalOptions;
|
|
205
|
+
}
|
|
206
|
+
visibleArguments(cmd) {
|
|
207
|
+
if (cmd._argsDescription) {
|
|
208
|
+
cmd.registeredArguments.forEach((argument) => {
|
|
209
|
+
argument.description = argument.description || cmd._argsDescription[argument.name()] || "";
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
if (cmd.registeredArguments.find((argument) => argument.description)) {
|
|
213
|
+
return cmd.registeredArguments;
|
|
214
|
+
}
|
|
215
|
+
return [];
|
|
216
|
+
}
|
|
217
|
+
subcommandTerm(cmd) {
|
|
218
|
+
const args = cmd.registeredArguments.map((arg) => humanReadableArgName(arg)).join(" ");
|
|
219
|
+
return cmd._name + (cmd._aliases[0] ? "|" + cmd._aliases[0] : "") + (cmd.options.length ? " [options]" : "") + (args ? " " + args : "");
|
|
220
|
+
}
|
|
221
|
+
optionTerm(option) {
|
|
222
|
+
return option.flags;
|
|
223
|
+
}
|
|
224
|
+
argumentTerm(argument) {
|
|
225
|
+
return argument.name();
|
|
226
|
+
}
|
|
227
|
+
longestSubcommandTermLength(cmd, helper) {
|
|
228
|
+
return helper.visibleCommands(cmd).reduce((max, command) => {
|
|
229
|
+
return Math.max(max, this.displayWidth(helper.styleSubcommandTerm(helper.subcommandTerm(command))));
|
|
230
|
+
}, 0);
|
|
231
|
+
}
|
|
232
|
+
longestOptionTermLength(cmd, helper) {
|
|
233
|
+
return helper.visibleOptions(cmd).reduce((max, option) => {
|
|
234
|
+
return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
|
|
235
|
+
}, 0);
|
|
236
|
+
}
|
|
237
|
+
longestGlobalOptionTermLength(cmd, helper) {
|
|
238
|
+
return helper.visibleGlobalOptions(cmd).reduce((max, option) => {
|
|
239
|
+
return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
|
|
240
|
+
}, 0);
|
|
241
|
+
}
|
|
242
|
+
longestArgumentTermLength(cmd, helper) {
|
|
243
|
+
return helper.visibleArguments(cmd).reduce((max, argument) => {
|
|
244
|
+
return Math.max(max, this.displayWidth(helper.styleArgumentTerm(helper.argumentTerm(argument))));
|
|
245
|
+
}, 0);
|
|
246
|
+
}
|
|
247
|
+
commandUsage(cmd) {
|
|
248
|
+
let cmdName = cmd._name;
|
|
249
|
+
if (cmd._aliases[0]) {
|
|
250
|
+
cmdName = cmdName + "|" + cmd._aliases[0];
|
|
251
|
+
}
|
|
252
|
+
let ancestorCmdNames = "";
|
|
253
|
+
for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
254
|
+
ancestorCmdNames = ancestorCmd.name() + " " + ancestorCmdNames;
|
|
255
|
+
}
|
|
256
|
+
return ancestorCmdNames + cmdName + " " + cmd.usage();
|
|
257
|
+
}
|
|
258
|
+
commandDescription(cmd) {
|
|
259
|
+
return cmd.description();
|
|
260
|
+
}
|
|
261
|
+
subcommandDescription(cmd) {
|
|
262
|
+
return cmd.summary() || cmd.description();
|
|
263
|
+
}
|
|
264
|
+
optionDescription(option) {
|
|
265
|
+
const extraInfo = [];
|
|
266
|
+
if (option.argChoices) {
|
|
267
|
+
extraInfo.push(`choices: ${option.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
268
|
+
}
|
|
269
|
+
if (option.defaultValue !== undefined) {
|
|
270
|
+
const showDefault = option.required || option.optional || option.isBoolean() && typeof option.defaultValue === "boolean";
|
|
271
|
+
if (showDefault) {
|
|
272
|
+
extraInfo.push(`default: ${option.defaultValueDescription || JSON.stringify(option.defaultValue)}`);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (option.presetArg !== undefined && option.optional) {
|
|
276
|
+
extraInfo.push(`preset: ${JSON.stringify(option.presetArg)}`);
|
|
277
|
+
}
|
|
278
|
+
if (option.envVar !== undefined) {
|
|
279
|
+
extraInfo.push(`env: ${option.envVar}`);
|
|
280
|
+
}
|
|
281
|
+
if (extraInfo.length > 0) {
|
|
282
|
+
const extraDescription = `(${extraInfo.join(", ")})`;
|
|
283
|
+
if (option.description) {
|
|
284
|
+
return `${option.description} ${extraDescription}`;
|
|
285
|
+
}
|
|
286
|
+
return extraDescription;
|
|
287
|
+
}
|
|
288
|
+
return option.description;
|
|
289
|
+
}
|
|
290
|
+
argumentDescription(argument) {
|
|
291
|
+
const extraInfo = [];
|
|
292
|
+
if (argument.argChoices) {
|
|
293
|
+
extraInfo.push(`choices: ${argument.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
294
|
+
}
|
|
295
|
+
if (argument.defaultValue !== undefined) {
|
|
296
|
+
extraInfo.push(`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`);
|
|
297
|
+
}
|
|
298
|
+
if (extraInfo.length > 0) {
|
|
299
|
+
const extraDescription = `(${extraInfo.join(", ")})`;
|
|
300
|
+
if (argument.description) {
|
|
301
|
+
return `${argument.description} ${extraDescription}`;
|
|
302
|
+
}
|
|
303
|
+
return extraDescription;
|
|
304
|
+
}
|
|
305
|
+
return argument.description;
|
|
306
|
+
}
|
|
307
|
+
formatItemList(heading, items, helper) {
|
|
308
|
+
if (items.length === 0)
|
|
309
|
+
return [];
|
|
310
|
+
return [helper.styleTitle(heading), ...items, ""];
|
|
311
|
+
}
|
|
312
|
+
groupItems(unsortedItems, visibleItems, getGroup) {
|
|
313
|
+
const result = new Map;
|
|
314
|
+
unsortedItems.forEach((item) => {
|
|
315
|
+
const group = getGroup(item);
|
|
316
|
+
if (!result.has(group))
|
|
317
|
+
result.set(group, []);
|
|
318
|
+
});
|
|
319
|
+
visibleItems.forEach((item) => {
|
|
320
|
+
const group = getGroup(item);
|
|
321
|
+
if (!result.has(group)) {
|
|
322
|
+
result.set(group, []);
|
|
323
|
+
}
|
|
324
|
+
result.get(group).push(item);
|
|
325
|
+
});
|
|
326
|
+
return result;
|
|
327
|
+
}
|
|
328
|
+
formatHelp(cmd, helper) {
|
|
329
|
+
const termWidth = helper.padWidth(cmd, helper);
|
|
330
|
+
const helpWidth = helper.helpWidth ?? 80;
|
|
331
|
+
function callFormatItem(term, description) {
|
|
332
|
+
return helper.formatItem(term, termWidth, description, helper);
|
|
333
|
+
}
|
|
334
|
+
let output = [
|
|
335
|
+
`${helper.styleTitle("Usage:")} ${helper.styleUsage(helper.commandUsage(cmd))}`,
|
|
336
|
+
""
|
|
337
|
+
];
|
|
338
|
+
const commandDescription = helper.commandDescription(cmd);
|
|
339
|
+
if (commandDescription.length > 0) {
|
|
340
|
+
output = output.concat([
|
|
341
|
+
helper.boxWrap(helper.styleCommandDescription(commandDescription), helpWidth),
|
|
342
|
+
""
|
|
343
|
+
]);
|
|
344
|
+
}
|
|
345
|
+
const argumentList = helper.visibleArguments(cmd).map((argument) => {
|
|
346
|
+
return callFormatItem(helper.styleArgumentTerm(helper.argumentTerm(argument)), helper.styleArgumentDescription(helper.argumentDescription(argument)));
|
|
347
|
+
});
|
|
348
|
+
output = output.concat(this.formatItemList("Arguments:", argumentList, helper));
|
|
349
|
+
const optionGroups = this.groupItems(cmd.options, helper.visibleOptions(cmd), (option) => option.helpGroupHeading ?? "Options:");
|
|
350
|
+
optionGroups.forEach((options, group) => {
|
|
351
|
+
const optionList = options.map((option) => {
|
|
352
|
+
return callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option)));
|
|
353
|
+
});
|
|
354
|
+
output = output.concat(this.formatItemList(group, optionList, helper));
|
|
355
|
+
});
|
|
356
|
+
if (helper.showGlobalOptions) {
|
|
357
|
+
const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
|
|
358
|
+
return callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option)));
|
|
359
|
+
});
|
|
360
|
+
output = output.concat(this.formatItemList("Global Options:", globalOptionList, helper));
|
|
361
|
+
}
|
|
362
|
+
const commandGroups = this.groupItems(cmd.commands, helper.visibleCommands(cmd), (sub) => sub.helpGroup() || "Commands:");
|
|
363
|
+
commandGroups.forEach((commands, group) => {
|
|
364
|
+
const commandList = commands.map((sub) => {
|
|
365
|
+
return callFormatItem(helper.styleSubcommandTerm(helper.subcommandTerm(sub)), helper.styleSubcommandDescription(helper.subcommandDescription(sub)));
|
|
366
|
+
});
|
|
367
|
+
output = output.concat(this.formatItemList(group, commandList, helper));
|
|
368
|
+
});
|
|
369
|
+
return output.join(`
|
|
370
|
+
`);
|
|
371
|
+
}
|
|
372
|
+
displayWidth(str) {
|
|
373
|
+
return stripColor(str).length;
|
|
374
|
+
}
|
|
375
|
+
styleTitle(str) {
|
|
376
|
+
return str;
|
|
377
|
+
}
|
|
378
|
+
styleUsage(str) {
|
|
379
|
+
return str.split(" ").map((word) => {
|
|
380
|
+
if (word === "[options]")
|
|
381
|
+
return this.styleOptionText(word);
|
|
382
|
+
if (word === "[command]")
|
|
383
|
+
return this.styleSubcommandText(word);
|
|
384
|
+
if (word[0] === "[" || word[0] === "<")
|
|
385
|
+
return this.styleArgumentText(word);
|
|
386
|
+
return this.styleCommandText(word);
|
|
387
|
+
}).join(" ");
|
|
388
|
+
}
|
|
389
|
+
styleCommandDescription(str) {
|
|
390
|
+
return this.styleDescriptionText(str);
|
|
391
|
+
}
|
|
392
|
+
styleOptionDescription(str) {
|
|
393
|
+
return this.styleDescriptionText(str);
|
|
394
|
+
}
|
|
395
|
+
styleSubcommandDescription(str) {
|
|
396
|
+
return this.styleDescriptionText(str);
|
|
397
|
+
}
|
|
398
|
+
styleArgumentDescription(str) {
|
|
399
|
+
return this.styleDescriptionText(str);
|
|
400
|
+
}
|
|
401
|
+
styleDescriptionText(str) {
|
|
402
|
+
return str;
|
|
403
|
+
}
|
|
404
|
+
styleOptionTerm(str) {
|
|
405
|
+
return this.styleOptionText(str);
|
|
406
|
+
}
|
|
407
|
+
styleSubcommandTerm(str) {
|
|
408
|
+
return str.split(" ").map((word) => {
|
|
409
|
+
if (word === "[options]")
|
|
410
|
+
return this.styleOptionText(word);
|
|
411
|
+
if (word[0] === "[" || word[0] === "<")
|
|
412
|
+
return this.styleArgumentText(word);
|
|
413
|
+
return this.styleSubcommandText(word);
|
|
414
|
+
}).join(" ");
|
|
415
|
+
}
|
|
416
|
+
styleArgumentTerm(str) {
|
|
417
|
+
return this.styleArgumentText(str);
|
|
418
|
+
}
|
|
419
|
+
styleOptionText(str) {
|
|
420
|
+
return str;
|
|
421
|
+
}
|
|
422
|
+
styleArgumentText(str) {
|
|
423
|
+
return str;
|
|
424
|
+
}
|
|
425
|
+
styleSubcommandText(str) {
|
|
426
|
+
return str;
|
|
427
|
+
}
|
|
428
|
+
styleCommandText(str) {
|
|
429
|
+
return str;
|
|
430
|
+
}
|
|
431
|
+
padWidth(cmd, helper) {
|
|
432
|
+
return Math.max(helper.longestOptionTermLength(cmd, helper), helper.longestGlobalOptionTermLength(cmd, helper), helper.longestSubcommandTermLength(cmd, helper), helper.longestArgumentTermLength(cmd, helper));
|
|
433
|
+
}
|
|
434
|
+
preformatted(str) {
|
|
435
|
+
return /\n[^\S\r\n]/.test(str);
|
|
436
|
+
}
|
|
437
|
+
formatItem(term, termWidth, description, helper) {
|
|
438
|
+
const itemIndent = 2;
|
|
439
|
+
const itemIndentStr = " ".repeat(itemIndent);
|
|
440
|
+
if (!description)
|
|
441
|
+
return itemIndentStr + term;
|
|
442
|
+
const paddedTerm = term.padEnd(termWidth + term.length - helper.displayWidth(term));
|
|
443
|
+
const spacerWidth = 2;
|
|
444
|
+
const helpWidth = this.helpWidth ?? 80;
|
|
445
|
+
const remainingWidth = helpWidth - termWidth - spacerWidth - itemIndent;
|
|
446
|
+
let formattedDescription;
|
|
447
|
+
if (remainingWidth < this.minWidthToWrap || helper.preformatted(description)) {
|
|
448
|
+
formattedDescription = description;
|
|
449
|
+
} else {
|
|
450
|
+
const wrappedDescription = helper.boxWrap(description, remainingWidth);
|
|
451
|
+
formattedDescription = wrappedDescription.replace(/\n/g, `
|
|
452
|
+
` + " ".repeat(termWidth + spacerWidth));
|
|
453
|
+
}
|
|
454
|
+
return itemIndentStr + paddedTerm + " ".repeat(spacerWidth) + formattedDescription.replace(/\n/g, `
|
|
455
|
+
${itemIndentStr}`);
|
|
456
|
+
}
|
|
457
|
+
boxWrap(str, width) {
|
|
458
|
+
if (width < this.minWidthToWrap)
|
|
459
|
+
return str;
|
|
460
|
+
const rawLines = str.split(/\r\n|\n/);
|
|
461
|
+
const chunkPattern = /[\s]*[^\s]+/g;
|
|
462
|
+
const wrappedLines = [];
|
|
463
|
+
rawLines.forEach((line) => {
|
|
464
|
+
const chunks = line.match(chunkPattern);
|
|
465
|
+
if (chunks === null) {
|
|
466
|
+
wrappedLines.push("");
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
let sumChunks = [chunks.shift()];
|
|
470
|
+
let sumWidth = this.displayWidth(sumChunks[0]);
|
|
471
|
+
chunks.forEach((chunk) => {
|
|
472
|
+
const visibleWidth = this.displayWidth(chunk);
|
|
473
|
+
if (sumWidth + visibleWidth <= width) {
|
|
474
|
+
sumChunks.push(chunk);
|
|
475
|
+
sumWidth += visibleWidth;
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
wrappedLines.push(sumChunks.join(""));
|
|
479
|
+
const nextChunk = chunk.trimStart();
|
|
480
|
+
sumChunks = [nextChunk];
|
|
481
|
+
sumWidth = this.displayWidth(nextChunk);
|
|
482
|
+
});
|
|
483
|
+
wrappedLines.push(sumChunks.join(""));
|
|
484
|
+
});
|
|
485
|
+
return wrappedLines.join(`
|
|
486
|
+
`);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
function stripColor(str) {
|
|
490
|
+
const sgrPattern = /\x1b\[\d*(;\d*)*m/g;
|
|
491
|
+
return str.replace(sgrPattern, "");
|
|
492
|
+
}
|
|
493
|
+
exports.Help = Help;
|
|
494
|
+
exports.stripColor = stripColor;
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
// node_modules/commander/lib/option.js
|
|
498
|
+
var require_option = __commonJS((exports) => {
|
|
499
|
+
var { InvalidArgumentError } = require_error();
|
|
500
|
+
|
|
501
|
+
class Option {
|
|
502
|
+
constructor(flags, description) {
|
|
503
|
+
this.flags = flags;
|
|
504
|
+
this.description = description || "";
|
|
505
|
+
this.required = flags.includes("<");
|
|
506
|
+
this.optional = flags.includes("[");
|
|
507
|
+
this.variadic = /\w\.\.\.[>\]]$/.test(flags);
|
|
508
|
+
this.mandatory = false;
|
|
509
|
+
const optionFlags = splitOptionFlags(flags);
|
|
510
|
+
this.short = optionFlags.shortFlag;
|
|
511
|
+
this.long = optionFlags.longFlag;
|
|
512
|
+
this.negate = false;
|
|
513
|
+
if (this.long) {
|
|
514
|
+
this.negate = this.long.startsWith("--no-");
|
|
515
|
+
}
|
|
516
|
+
this.defaultValue = undefined;
|
|
517
|
+
this.defaultValueDescription = undefined;
|
|
518
|
+
this.presetArg = undefined;
|
|
519
|
+
this.envVar = undefined;
|
|
520
|
+
this.parseArg = undefined;
|
|
521
|
+
this.hidden = false;
|
|
522
|
+
this.argChoices = undefined;
|
|
523
|
+
this.conflictsWith = [];
|
|
524
|
+
this.implied = undefined;
|
|
525
|
+
this.helpGroupHeading = undefined;
|
|
526
|
+
}
|
|
527
|
+
default(value, description) {
|
|
528
|
+
this.defaultValue = value;
|
|
529
|
+
this.defaultValueDescription = description;
|
|
530
|
+
return this;
|
|
531
|
+
}
|
|
532
|
+
preset(arg) {
|
|
533
|
+
this.presetArg = arg;
|
|
534
|
+
return this;
|
|
535
|
+
}
|
|
536
|
+
conflicts(names) {
|
|
537
|
+
this.conflictsWith = this.conflictsWith.concat(names);
|
|
538
|
+
return this;
|
|
539
|
+
}
|
|
540
|
+
implies(impliedOptionValues) {
|
|
541
|
+
let newImplied = impliedOptionValues;
|
|
542
|
+
if (typeof impliedOptionValues === "string") {
|
|
543
|
+
newImplied = { [impliedOptionValues]: true };
|
|
544
|
+
}
|
|
545
|
+
this.implied = Object.assign(this.implied || {}, newImplied);
|
|
546
|
+
return this;
|
|
547
|
+
}
|
|
548
|
+
env(name) {
|
|
549
|
+
this.envVar = name;
|
|
550
|
+
return this;
|
|
551
|
+
}
|
|
552
|
+
argParser(fn) {
|
|
553
|
+
this.parseArg = fn;
|
|
554
|
+
return this;
|
|
555
|
+
}
|
|
556
|
+
makeOptionMandatory(mandatory = true) {
|
|
557
|
+
this.mandatory = !!mandatory;
|
|
558
|
+
return this;
|
|
559
|
+
}
|
|
560
|
+
hideHelp(hide = true) {
|
|
561
|
+
this.hidden = !!hide;
|
|
562
|
+
return this;
|
|
563
|
+
}
|
|
564
|
+
_collectValue(value, previous) {
|
|
565
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
566
|
+
return [value];
|
|
567
|
+
}
|
|
568
|
+
previous.push(value);
|
|
569
|
+
return previous;
|
|
570
|
+
}
|
|
571
|
+
choices(values) {
|
|
572
|
+
this.argChoices = values.slice();
|
|
573
|
+
this.parseArg = (arg, previous) => {
|
|
574
|
+
if (!this.argChoices.includes(arg)) {
|
|
575
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
576
|
+
}
|
|
577
|
+
if (this.variadic) {
|
|
578
|
+
return this._collectValue(arg, previous);
|
|
579
|
+
}
|
|
580
|
+
return arg;
|
|
581
|
+
};
|
|
582
|
+
return this;
|
|
583
|
+
}
|
|
584
|
+
name() {
|
|
585
|
+
if (this.long) {
|
|
586
|
+
return this.long.replace(/^--/, "");
|
|
587
|
+
}
|
|
588
|
+
return this.short.replace(/^-/, "");
|
|
589
|
+
}
|
|
590
|
+
attributeName() {
|
|
591
|
+
if (this.negate) {
|
|
592
|
+
return camelcase(this.name().replace(/^no-/, ""));
|
|
593
|
+
}
|
|
594
|
+
return camelcase(this.name());
|
|
595
|
+
}
|
|
596
|
+
helpGroup(heading) {
|
|
597
|
+
this.helpGroupHeading = heading;
|
|
598
|
+
return this;
|
|
599
|
+
}
|
|
600
|
+
is(arg) {
|
|
601
|
+
return this.short === arg || this.long === arg;
|
|
602
|
+
}
|
|
603
|
+
isBoolean() {
|
|
604
|
+
return !this.required && !this.optional && !this.negate;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
class DualOptions {
|
|
609
|
+
constructor(options) {
|
|
610
|
+
this.positiveOptions = new Map;
|
|
611
|
+
this.negativeOptions = new Map;
|
|
612
|
+
this.dualOptions = new Set;
|
|
613
|
+
options.forEach((option) => {
|
|
614
|
+
if (option.negate) {
|
|
615
|
+
this.negativeOptions.set(option.attributeName(), option);
|
|
616
|
+
} else {
|
|
617
|
+
this.positiveOptions.set(option.attributeName(), option);
|
|
618
|
+
}
|
|
619
|
+
});
|
|
620
|
+
this.negativeOptions.forEach((value, key) => {
|
|
621
|
+
if (this.positiveOptions.has(key)) {
|
|
622
|
+
this.dualOptions.add(key);
|
|
623
|
+
}
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
valueFromOption(value, option) {
|
|
627
|
+
const optionKey = option.attributeName();
|
|
628
|
+
if (!this.dualOptions.has(optionKey))
|
|
629
|
+
return true;
|
|
630
|
+
const preset = this.negativeOptions.get(optionKey).presetArg;
|
|
631
|
+
const negativeValue = preset !== undefined ? preset : false;
|
|
632
|
+
return option.negate === (negativeValue === value);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
function camelcase(str) {
|
|
636
|
+
return str.split("-").reduce((str2, word) => {
|
|
637
|
+
return str2 + word[0].toUpperCase() + word.slice(1);
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
function splitOptionFlags(flags) {
|
|
641
|
+
let shortFlag;
|
|
642
|
+
let longFlag;
|
|
643
|
+
const shortFlagExp = /^-[^-]$/;
|
|
644
|
+
const longFlagExp = /^--[^-]/;
|
|
645
|
+
const flagParts = flags.split(/[ |,]+/).concat("guard");
|
|
646
|
+
if (shortFlagExp.test(flagParts[0]))
|
|
647
|
+
shortFlag = flagParts.shift();
|
|
648
|
+
if (longFlagExp.test(flagParts[0]))
|
|
649
|
+
longFlag = flagParts.shift();
|
|
650
|
+
if (!shortFlag && shortFlagExp.test(flagParts[0]))
|
|
651
|
+
shortFlag = flagParts.shift();
|
|
652
|
+
if (!shortFlag && longFlagExp.test(flagParts[0])) {
|
|
653
|
+
shortFlag = longFlag;
|
|
654
|
+
longFlag = flagParts.shift();
|
|
655
|
+
}
|
|
656
|
+
if (flagParts[0].startsWith("-")) {
|
|
657
|
+
const unsupportedFlag = flagParts[0];
|
|
658
|
+
const baseError = `option creation failed due to '${unsupportedFlag}' in option flags '${flags}'`;
|
|
659
|
+
if (/^-[^-][^-]/.test(unsupportedFlag))
|
|
660
|
+
throw new Error(`${baseError}
|
|
661
|
+
- a short flag is a single dash and a single character
|
|
662
|
+
- either use a single dash and a single character (for a short flag)
|
|
663
|
+
- or use a double dash for a long option (and can have two, like '--ws, --workspace')`);
|
|
664
|
+
if (shortFlagExp.test(unsupportedFlag))
|
|
665
|
+
throw new Error(`${baseError}
|
|
666
|
+
- too many short flags`);
|
|
667
|
+
if (longFlagExp.test(unsupportedFlag))
|
|
668
|
+
throw new Error(`${baseError}
|
|
669
|
+
- too many long flags`);
|
|
670
|
+
throw new Error(`${baseError}
|
|
671
|
+
- unrecognised flag format`);
|
|
672
|
+
}
|
|
673
|
+
if (shortFlag === undefined && longFlag === undefined)
|
|
674
|
+
throw new Error(`option creation failed due to no flags found in '${flags}'.`);
|
|
675
|
+
return { shortFlag, longFlag };
|
|
676
|
+
}
|
|
677
|
+
exports.Option = Option;
|
|
678
|
+
exports.DualOptions = DualOptions;
|
|
679
|
+
});
|
|
680
|
+
|
|
681
|
+
// node_modules/commander/lib/suggestSimilar.js
|
|
682
|
+
var require_suggestSimilar = __commonJS((exports) => {
|
|
683
|
+
var maxDistance = 3;
|
|
684
|
+
function editDistance(a, b) {
|
|
685
|
+
if (Math.abs(a.length - b.length) > maxDistance)
|
|
686
|
+
return Math.max(a.length, b.length);
|
|
687
|
+
const d = [];
|
|
688
|
+
for (let i = 0;i <= a.length; i++) {
|
|
689
|
+
d[i] = [i];
|
|
690
|
+
}
|
|
691
|
+
for (let j = 0;j <= b.length; j++) {
|
|
692
|
+
d[0][j] = j;
|
|
693
|
+
}
|
|
694
|
+
for (let j = 1;j <= b.length; j++) {
|
|
695
|
+
for (let i = 1;i <= a.length; i++) {
|
|
696
|
+
let cost = 1;
|
|
697
|
+
if (a[i - 1] === b[j - 1]) {
|
|
698
|
+
cost = 0;
|
|
699
|
+
} else {
|
|
700
|
+
cost = 1;
|
|
701
|
+
}
|
|
702
|
+
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
|
|
703
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
704
|
+
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
return d[a.length][b.length];
|
|
709
|
+
}
|
|
710
|
+
function suggestSimilar(word, candidates) {
|
|
711
|
+
if (!candidates || candidates.length === 0)
|
|
712
|
+
return "";
|
|
713
|
+
candidates = Array.from(new Set(candidates));
|
|
714
|
+
const searchingOptions = word.startsWith("--");
|
|
715
|
+
if (searchingOptions) {
|
|
716
|
+
word = word.slice(2);
|
|
717
|
+
candidates = candidates.map((candidate) => candidate.slice(2));
|
|
718
|
+
}
|
|
719
|
+
let similar = [];
|
|
720
|
+
let bestDistance = maxDistance;
|
|
721
|
+
const minSimilarity = 0.4;
|
|
722
|
+
candidates.forEach((candidate) => {
|
|
723
|
+
if (candidate.length <= 1)
|
|
724
|
+
return;
|
|
725
|
+
const distance = editDistance(word, candidate);
|
|
726
|
+
const length = Math.max(word.length, candidate.length);
|
|
727
|
+
const similarity = (length - distance) / length;
|
|
728
|
+
if (similarity > minSimilarity) {
|
|
729
|
+
if (distance < bestDistance) {
|
|
730
|
+
bestDistance = distance;
|
|
731
|
+
similar = [candidate];
|
|
732
|
+
} else if (distance === bestDistance) {
|
|
733
|
+
similar.push(candidate);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
});
|
|
737
|
+
similar.sort((a, b) => a.localeCompare(b));
|
|
738
|
+
if (searchingOptions) {
|
|
739
|
+
similar = similar.map((candidate) => `--${candidate}`);
|
|
740
|
+
}
|
|
741
|
+
if (similar.length > 1) {
|
|
742
|
+
return `
|
|
743
|
+
(Did you mean one of ${similar.join(", ")}?)`;
|
|
744
|
+
}
|
|
745
|
+
if (similar.length === 1) {
|
|
746
|
+
return `
|
|
747
|
+
(Did you mean ${similar[0]}?)`;
|
|
748
|
+
}
|
|
749
|
+
return "";
|
|
750
|
+
}
|
|
751
|
+
exports.suggestSimilar = suggestSimilar;
|
|
752
|
+
});
|
|
753
|
+
|
|
754
|
+
// node_modules/commander/lib/command.js
|
|
755
|
+
var require_command = __commonJS((exports) => {
|
|
756
|
+
var EventEmitter = __require("events").EventEmitter;
|
|
757
|
+
var childProcess = __require("child_process");
|
|
758
|
+
var path = __require("path");
|
|
759
|
+
var fs = __require("fs");
|
|
760
|
+
var process2 = __require("process");
|
|
761
|
+
var { Argument, humanReadableArgName } = require_argument();
|
|
762
|
+
var { CommanderError } = require_error();
|
|
763
|
+
var { Help, stripColor } = require_help();
|
|
764
|
+
var { Option, DualOptions } = require_option();
|
|
765
|
+
var { suggestSimilar } = require_suggestSimilar();
|
|
766
|
+
|
|
767
|
+
class Command extends EventEmitter {
|
|
768
|
+
constructor(name) {
|
|
769
|
+
super();
|
|
770
|
+
this.commands = [];
|
|
771
|
+
this.options = [];
|
|
772
|
+
this.parent = null;
|
|
773
|
+
this._allowUnknownOption = false;
|
|
774
|
+
this._allowExcessArguments = false;
|
|
775
|
+
this.registeredArguments = [];
|
|
776
|
+
this._args = this.registeredArguments;
|
|
777
|
+
this.args = [];
|
|
778
|
+
this.rawArgs = [];
|
|
779
|
+
this.processedArgs = [];
|
|
780
|
+
this._scriptPath = null;
|
|
781
|
+
this._name = name || "";
|
|
782
|
+
this._optionValues = {};
|
|
783
|
+
this._optionValueSources = {};
|
|
784
|
+
this._storeOptionsAsProperties = false;
|
|
785
|
+
this._actionHandler = null;
|
|
786
|
+
this._executableHandler = false;
|
|
787
|
+
this._executableFile = null;
|
|
788
|
+
this._executableDir = null;
|
|
789
|
+
this._defaultCommandName = null;
|
|
790
|
+
this._exitCallback = null;
|
|
791
|
+
this._aliases = [];
|
|
792
|
+
this._combineFlagAndOptionalValue = true;
|
|
793
|
+
this._description = "";
|
|
794
|
+
this._summary = "";
|
|
795
|
+
this._argsDescription = undefined;
|
|
796
|
+
this._enablePositionalOptions = false;
|
|
797
|
+
this._passThroughOptions = false;
|
|
798
|
+
this._lifeCycleHooks = {};
|
|
799
|
+
this._showHelpAfterError = false;
|
|
800
|
+
this._showSuggestionAfterError = true;
|
|
801
|
+
this._savedState = null;
|
|
802
|
+
this._outputConfiguration = {
|
|
803
|
+
writeOut: (str) => process2.stdout.write(str),
|
|
804
|
+
writeErr: (str) => process2.stderr.write(str),
|
|
805
|
+
outputError: (str, write) => write(str),
|
|
806
|
+
getOutHelpWidth: () => process2.stdout.isTTY ? process2.stdout.columns : undefined,
|
|
807
|
+
getErrHelpWidth: () => process2.stderr.isTTY ? process2.stderr.columns : undefined,
|
|
808
|
+
getOutHasColors: () => useColor() ?? (process2.stdout.isTTY && process2.stdout.hasColors?.()),
|
|
809
|
+
getErrHasColors: () => useColor() ?? (process2.stderr.isTTY && process2.stderr.hasColors?.()),
|
|
810
|
+
stripColor: (str) => stripColor(str)
|
|
811
|
+
};
|
|
812
|
+
this._hidden = false;
|
|
813
|
+
this._helpOption = undefined;
|
|
814
|
+
this._addImplicitHelpCommand = undefined;
|
|
815
|
+
this._helpCommand = undefined;
|
|
816
|
+
this._helpConfiguration = {};
|
|
817
|
+
this._helpGroupHeading = undefined;
|
|
818
|
+
this._defaultCommandGroup = undefined;
|
|
819
|
+
this._defaultOptionGroup = undefined;
|
|
820
|
+
}
|
|
821
|
+
copyInheritedSettings(sourceCommand) {
|
|
822
|
+
this._outputConfiguration = sourceCommand._outputConfiguration;
|
|
823
|
+
this._helpOption = sourceCommand._helpOption;
|
|
824
|
+
this._helpCommand = sourceCommand._helpCommand;
|
|
825
|
+
this._helpConfiguration = sourceCommand._helpConfiguration;
|
|
826
|
+
this._exitCallback = sourceCommand._exitCallback;
|
|
827
|
+
this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
|
|
828
|
+
this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
|
|
829
|
+
this._allowExcessArguments = sourceCommand._allowExcessArguments;
|
|
830
|
+
this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
|
|
831
|
+
this._showHelpAfterError = sourceCommand._showHelpAfterError;
|
|
832
|
+
this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;
|
|
833
|
+
return this;
|
|
834
|
+
}
|
|
835
|
+
_getCommandAndAncestors() {
|
|
836
|
+
const result = [];
|
|
837
|
+
for (let command = this;command; command = command.parent) {
|
|
838
|
+
result.push(command);
|
|
839
|
+
}
|
|
840
|
+
return result;
|
|
841
|
+
}
|
|
842
|
+
command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
|
|
843
|
+
let desc = actionOptsOrExecDesc;
|
|
844
|
+
let opts = execOpts;
|
|
845
|
+
if (typeof desc === "object" && desc !== null) {
|
|
846
|
+
opts = desc;
|
|
847
|
+
desc = null;
|
|
848
|
+
}
|
|
849
|
+
opts = opts || {};
|
|
850
|
+
const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
|
|
851
|
+
const cmd = this.createCommand(name);
|
|
852
|
+
if (desc) {
|
|
853
|
+
cmd.description(desc);
|
|
854
|
+
cmd._executableHandler = true;
|
|
855
|
+
}
|
|
856
|
+
if (opts.isDefault)
|
|
857
|
+
this._defaultCommandName = cmd._name;
|
|
858
|
+
cmd._hidden = !!(opts.noHelp || opts.hidden);
|
|
859
|
+
cmd._executableFile = opts.executableFile || null;
|
|
860
|
+
if (args)
|
|
861
|
+
cmd.arguments(args);
|
|
862
|
+
this._registerCommand(cmd);
|
|
863
|
+
cmd.parent = this;
|
|
864
|
+
cmd.copyInheritedSettings(this);
|
|
865
|
+
if (desc)
|
|
866
|
+
return this;
|
|
867
|
+
return cmd;
|
|
868
|
+
}
|
|
869
|
+
createCommand(name) {
|
|
870
|
+
return new Command(name);
|
|
871
|
+
}
|
|
872
|
+
createHelp() {
|
|
873
|
+
return Object.assign(new Help, this.configureHelp());
|
|
874
|
+
}
|
|
875
|
+
configureHelp(configuration) {
|
|
876
|
+
if (configuration === undefined)
|
|
877
|
+
return this._helpConfiguration;
|
|
878
|
+
this._helpConfiguration = configuration;
|
|
879
|
+
return this;
|
|
880
|
+
}
|
|
881
|
+
configureOutput(configuration) {
|
|
882
|
+
if (configuration === undefined)
|
|
883
|
+
return this._outputConfiguration;
|
|
884
|
+
this._outputConfiguration = {
|
|
885
|
+
...this._outputConfiguration,
|
|
886
|
+
...configuration
|
|
887
|
+
};
|
|
888
|
+
return this;
|
|
889
|
+
}
|
|
890
|
+
showHelpAfterError(displayHelp = true) {
|
|
891
|
+
if (typeof displayHelp !== "string")
|
|
892
|
+
displayHelp = !!displayHelp;
|
|
893
|
+
this._showHelpAfterError = displayHelp;
|
|
894
|
+
return this;
|
|
895
|
+
}
|
|
896
|
+
showSuggestionAfterError(displaySuggestion = true) {
|
|
897
|
+
this._showSuggestionAfterError = !!displaySuggestion;
|
|
898
|
+
return this;
|
|
899
|
+
}
|
|
900
|
+
addCommand(cmd, opts) {
|
|
901
|
+
if (!cmd._name) {
|
|
902
|
+
throw new Error(`Command passed to .addCommand() must have a name
|
|
903
|
+
- specify the name in Command constructor or using .name()`);
|
|
904
|
+
}
|
|
905
|
+
opts = opts || {};
|
|
906
|
+
if (opts.isDefault)
|
|
907
|
+
this._defaultCommandName = cmd._name;
|
|
908
|
+
if (opts.noHelp || opts.hidden)
|
|
909
|
+
cmd._hidden = true;
|
|
910
|
+
this._registerCommand(cmd);
|
|
911
|
+
cmd.parent = this;
|
|
912
|
+
cmd._checkForBrokenPassThrough();
|
|
913
|
+
return this;
|
|
914
|
+
}
|
|
915
|
+
createArgument(name, description) {
|
|
916
|
+
return new Argument(name, description);
|
|
917
|
+
}
|
|
918
|
+
argument(name, description, parseArg, defaultValue) {
|
|
919
|
+
const argument = this.createArgument(name, description);
|
|
920
|
+
if (typeof parseArg === "function") {
|
|
921
|
+
argument.default(defaultValue).argParser(parseArg);
|
|
922
|
+
} else {
|
|
923
|
+
argument.default(parseArg);
|
|
924
|
+
}
|
|
925
|
+
this.addArgument(argument);
|
|
926
|
+
return this;
|
|
927
|
+
}
|
|
928
|
+
arguments(names) {
|
|
929
|
+
names.trim().split(/ +/).forEach((detail) => {
|
|
930
|
+
this.argument(detail);
|
|
931
|
+
});
|
|
932
|
+
return this;
|
|
933
|
+
}
|
|
934
|
+
addArgument(argument) {
|
|
935
|
+
const previousArgument = this.registeredArguments.slice(-1)[0];
|
|
936
|
+
if (previousArgument?.variadic) {
|
|
937
|
+
throw new Error(`only the last argument can be variadic '${previousArgument.name()}'`);
|
|
938
|
+
}
|
|
939
|
+
if (argument.required && argument.defaultValue !== undefined && argument.parseArg === undefined) {
|
|
940
|
+
throw new Error(`a default value for a required argument is never used: '${argument.name()}'`);
|
|
941
|
+
}
|
|
942
|
+
this.registeredArguments.push(argument);
|
|
943
|
+
return this;
|
|
944
|
+
}
|
|
945
|
+
helpCommand(enableOrNameAndArgs, description) {
|
|
946
|
+
if (typeof enableOrNameAndArgs === "boolean") {
|
|
947
|
+
this._addImplicitHelpCommand = enableOrNameAndArgs;
|
|
948
|
+
if (enableOrNameAndArgs && this._defaultCommandGroup) {
|
|
949
|
+
this._initCommandGroup(this._getHelpCommand());
|
|
950
|
+
}
|
|
951
|
+
return this;
|
|
952
|
+
}
|
|
953
|
+
const nameAndArgs = enableOrNameAndArgs ?? "help [command]";
|
|
954
|
+
const [, helpName, helpArgs] = nameAndArgs.match(/([^ ]+) *(.*)/);
|
|
955
|
+
const helpDescription = description ?? "display help for command";
|
|
956
|
+
const helpCommand = this.createCommand(helpName);
|
|
957
|
+
helpCommand.helpOption(false);
|
|
958
|
+
if (helpArgs)
|
|
959
|
+
helpCommand.arguments(helpArgs);
|
|
960
|
+
if (helpDescription)
|
|
961
|
+
helpCommand.description(helpDescription);
|
|
962
|
+
this._addImplicitHelpCommand = true;
|
|
963
|
+
this._helpCommand = helpCommand;
|
|
964
|
+
if (enableOrNameAndArgs || description)
|
|
965
|
+
this._initCommandGroup(helpCommand);
|
|
966
|
+
return this;
|
|
967
|
+
}
|
|
968
|
+
addHelpCommand(helpCommand, deprecatedDescription) {
|
|
969
|
+
if (typeof helpCommand !== "object") {
|
|
970
|
+
this.helpCommand(helpCommand, deprecatedDescription);
|
|
971
|
+
return this;
|
|
972
|
+
}
|
|
973
|
+
this._addImplicitHelpCommand = true;
|
|
974
|
+
this._helpCommand = helpCommand;
|
|
975
|
+
this._initCommandGroup(helpCommand);
|
|
976
|
+
return this;
|
|
977
|
+
}
|
|
978
|
+
_getHelpCommand() {
|
|
979
|
+
const hasImplicitHelpCommand = this._addImplicitHelpCommand ?? (this.commands.length && !this._actionHandler && !this._findCommand("help"));
|
|
980
|
+
if (hasImplicitHelpCommand) {
|
|
981
|
+
if (this._helpCommand === undefined) {
|
|
982
|
+
this.helpCommand(undefined, undefined);
|
|
983
|
+
}
|
|
984
|
+
return this._helpCommand;
|
|
985
|
+
}
|
|
986
|
+
return null;
|
|
987
|
+
}
|
|
988
|
+
hook(event, listener) {
|
|
989
|
+
const allowedValues = ["preSubcommand", "preAction", "postAction"];
|
|
990
|
+
if (!allowedValues.includes(event)) {
|
|
991
|
+
throw new Error(`Unexpected value for event passed to hook : '${event}'.
|
|
992
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
993
|
+
}
|
|
994
|
+
if (this._lifeCycleHooks[event]) {
|
|
995
|
+
this._lifeCycleHooks[event].push(listener);
|
|
996
|
+
} else {
|
|
997
|
+
this._lifeCycleHooks[event] = [listener];
|
|
998
|
+
}
|
|
999
|
+
return this;
|
|
1000
|
+
}
|
|
1001
|
+
exitOverride(fn) {
|
|
1002
|
+
if (fn) {
|
|
1003
|
+
this._exitCallback = fn;
|
|
1004
|
+
} else {
|
|
1005
|
+
this._exitCallback = (err) => {
|
|
1006
|
+
if (err.code !== "commander.executeSubCommandAsync") {
|
|
1007
|
+
throw err;
|
|
1008
|
+
} else {}
|
|
1009
|
+
};
|
|
1010
|
+
}
|
|
1011
|
+
return this;
|
|
1012
|
+
}
|
|
1013
|
+
_exit(exitCode, code, message) {
|
|
1014
|
+
if (this._exitCallback) {
|
|
1015
|
+
this._exitCallback(new CommanderError(exitCode, code, message));
|
|
1016
|
+
}
|
|
1017
|
+
process2.exit(exitCode);
|
|
1018
|
+
}
|
|
1019
|
+
action(fn) {
|
|
1020
|
+
const listener = (args) => {
|
|
1021
|
+
const expectedArgsCount = this.registeredArguments.length;
|
|
1022
|
+
const actionArgs = args.slice(0, expectedArgsCount);
|
|
1023
|
+
if (this._storeOptionsAsProperties) {
|
|
1024
|
+
actionArgs[expectedArgsCount] = this;
|
|
1025
|
+
} else {
|
|
1026
|
+
actionArgs[expectedArgsCount] = this.opts();
|
|
1027
|
+
}
|
|
1028
|
+
actionArgs.push(this);
|
|
1029
|
+
return fn.apply(this, actionArgs);
|
|
1030
|
+
};
|
|
1031
|
+
this._actionHandler = listener;
|
|
1032
|
+
return this;
|
|
1033
|
+
}
|
|
1034
|
+
createOption(flags, description) {
|
|
1035
|
+
return new Option(flags, description);
|
|
1036
|
+
}
|
|
1037
|
+
_callParseArg(target, value, previous, invalidArgumentMessage) {
|
|
1038
|
+
try {
|
|
1039
|
+
return target.parseArg(value, previous);
|
|
1040
|
+
} catch (err) {
|
|
1041
|
+
if (err.code === "commander.invalidArgument") {
|
|
1042
|
+
const message = `${invalidArgumentMessage} ${err.message}`;
|
|
1043
|
+
this.error(message, { exitCode: err.exitCode, code: err.code });
|
|
1044
|
+
}
|
|
1045
|
+
throw err;
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
_registerOption(option) {
|
|
1049
|
+
const matchingOption = option.short && this._findOption(option.short) || option.long && this._findOption(option.long);
|
|
1050
|
+
if (matchingOption) {
|
|
1051
|
+
const matchingFlag = option.long && this._findOption(option.long) ? option.long : option.short;
|
|
1052
|
+
throw new Error(`Cannot add option '${option.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'
|
|
1053
|
+
- already used by option '${matchingOption.flags}'`);
|
|
1054
|
+
}
|
|
1055
|
+
this._initOptionGroup(option);
|
|
1056
|
+
this.options.push(option);
|
|
1057
|
+
}
|
|
1058
|
+
_registerCommand(command) {
|
|
1059
|
+
const knownBy = (cmd) => {
|
|
1060
|
+
return [cmd.name()].concat(cmd.aliases());
|
|
1061
|
+
};
|
|
1062
|
+
const alreadyUsed = knownBy(command).find((name) => this._findCommand(name));
|
|
1063
|
+
if (alreadyUsed) {
|
|
1064
|
+
const existingCmd = knownBy(this._findCommand(alreadyUsed)).join("|");
|
|
1065
|
+
const newCmd = knownBy(command).join("|");
|
|
1066
|
+
throw new Error(`cannot add command '${newCmd}' as already have command '${existingCmd}'`);
|
|
1067
|
+
}
|
|
1068
|
+
this._initCommandGroup(command);
|
|
1069
|
+
this.commands.push(command);
|
|
1070
|
+
}
|
|
1071
|
+
addOption(option) {
|
|
1072
|
+
this._registerOption(option);
|
|
1073
|
+
const oname = option.name();
|
|
1074
|
+
const name = option.attributeName();
|
|
1075
|
+
if (option.negate) {
|
|
1076
|
+
const positiveLongFlag = option.long.replace(/^--no-/, "--");
|
|
1077
|
+
if (!this._findOption(positiveLongFlag)) {
|
|
1078
|
+
this.setOptionValueWithSource(name, option.defaultValue === undefined ? true : option.defaultValue, "default");
|
|
1079
|
+
}
|
|
1080
|
+
} else if (option.defaultValue !== undefined) {
|
|
1081
|
+
this.setOptionValueWithSource(name, option.defaultValue, "default");
|
|
1082
|
+
}
|
|
1083
|
+
const handleOptionValue = (val, invalidValueMessage, valueSource) => {
|
|
1084
|
+
if (val == null && option.presetArg !== undefined) {
|
|
1085
|
+
val = option.presetArg;
|
|
1086
|
+
}
|
|
1087
|
+
const oldValue = this.getOptionValue(name);
|
|
1088
|
+
if (val !== null && option.parseArg) {
|
|
1089
|
+
val = this._callParseArg(option, val, oldValue, invalidValueMessage);
|
|
1090
|
+
} else if (val !== null && option.variadic) {
|
|
1091
|
+
val = option._collectValue(val, oldValue);
|
|
1092
|
+
}
|
|
1093
|
+
if (val == null) {
|
|
1094
|
+
if (option.negate) {
|
|
1095
|
+
val = false;
|
|
1096
|
+
} else if (option.isBoolean() || option.optional) {
|
|
1097
|
+
val = true;
|
|
1098
|
+
} else {
|
|
1099
|
+
val = "";
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
this.setOptionValueWithSource(name, val, valueSource);
|
|
1103
|
+
};
|
|
1104
|
+
this.on("option:" + oname, (val) => {
|
|
1105
|
+
const invalidValueMessage = `error: option '${option.flags}' argument '${val}' is invalid.`;
|
|
1106
|
+
handleOptionValue(val, invalidValueMessage, "cli");
|
|
1107
|
+
});
|
|
1108
|
+
if (option.envVar) {
|
|
1109
|
+
this.on("optionEnv:" + oname, (val) => {
|
|
1110
|
+
const invalidValueMessage = `error: option '${option.flags}' value '${val}' from env '${option.envVar}' is invalid.`;
|
|
1111
|
+
handleOptionValue(val, invalidValueMessage, "env");
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
return this;
|
|
1115
|
+
}
|
|
1116
|
+
_optionEx(config, flags, description, fn, defaultValue) {
|
|
1117
|
+
if (typeof flags === "object" && flags instanceof Option) {
|
|
1118
|
+
throw new Error("To add an Option object use addOption() instead of option() or requiredOption()");
|
|
1119
|
+
}
|
|
1120
|
+
const option = this.createOption(flags, description);
|
|
1121
|
+
option.makeOptionMandatory(!!config.mandatory);
|
|
1122
|
+
if (typeof fn === "function") {
|
|
1123
|
+
option.default(defaultValue).argParser(fn);
|
|
1124
|
+
} else if (fn instanceof RegExp) {
|
|
1125
|
+
const regex = fn;
|
|
1126
|
+
fn = (val, def) => {
|
|
1127
|
+
const m = regex.exec(val);
|
|
1128
|
+
return m ? m[0] : def;
|
|
1129
|
+
};
|
|
1130
|
+
option.default(defaultValue).argParser(fn);
|
|
1131
|
+
} else {
|
|
1132
|
+
option.default(fn);
|
|
1133
|
+
}
|
|
1134
|
+
return this.addOption(option);
|
|
1135
|
+
}
|
|
1136
|
+
option(flags, description, parseArg, defaultValue) {
|
|
1137
|
+
return this._optionEx({}, flags, description, parseArg, defaultValue);
|
|
1138
|
+
}
|
|
1139
|
+
requiredOption(flags, description, parseArg, defaultValue) {
|
|
1140
|
+
return this._optionEx({ mandatory: true }, flags, description, parseArg, defaultValue);
|
|
1141
|
+
}
|
|
1142
|
+
combineFlagAndOptionalValue(combine = true) {
|
|
1143
|
+
this._combineFlagAndOptionalValue = !!combine;
|
|
1144
|
+
return this;
|
|
1145
|
+
}
|
|
1146
|
+
allowUnknownOption(allowUnknown = true) {
|
|
1147
|
+
this._allowUnknownOption = !!allowUnknown;
|
|
1148
|
+
return this;
|
|
1149
|
+
}
|
|
1150
|
+
allowExcessArguments(allowExcess = true) {
|
|
1151
|
+
this._allowExcessArguments = !!allowExcess;
|
|
1152
|
+
return this;
|
|
1153
|
+
}
|
|
1154
|
+
enablePositionalOptions(positional = true) {
|
|
1155
|
+
this._enablePositionalOptions = !!positional;
|
|
1156
|
+
return this;
|
|
1157
|
+
}
|
|
1158
|
+
passThroughOptions(passThrough = true) {
|
|
1159
|
+
this._passThroughOptions = !!passThrough;
|
|
1160
|
+
this._checkForBrokenPassThrough();
|
|
1161
|
+
return this;
|
|
1162
|
+
}
|
|
1163
|
+
_checkForBrokenPassThrough() {
|
|
1164
|
+
if (this.parent && this._passThroughOptions && !this.parent._enablePositionalOptions) {
|
|
1165
|
+
throw new Error(`passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`);
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
storeOptionsAsProperties(storeAsProperties = true) {
|
|
1169
|
+
if (this.options.length) {
|
|
1170
|
+
throw new Error("call .storeOptionsAsProperties() before adding options");
|
|
1171
|
+
}
|
|
1172
|
+
if (Object.keys(this._optionValues).length) {
|
|
1173
|
+
throw new Error("call .storeOptionsAsProperties() before setting option values");
|
|
1174
|
+
}
|
|
1175
|
+
this._storeOptionsAsProperties = !!storeAsProperties;
|
|
1176
|
+
return this;
|
|
1177
|
+
}
|
|
1178
|
+
getOptionValue(key) {
|
|
1179
|
+
if (this._storeOptionsAsProperties) {
|
|
1180
|
+
return this[key];
|
|
1181
|
+
}
|
|
1182
|
+
return this._optionValues[key];
|
|
1183
|
+
}
|
|
1184
|
+
setOptionValue(key, value) {
|
|
1185
|
+
return this.setOptionValueWithSource(key, value, undefined);
|
|
1186
|
+
}
|
|
1187
|
+
setOptionValueWithSource(key, value, source) {
|
|
1188
|
+
if (this._storeOptionsAsProperties) {
|
|
1189
|
+
this[key] = value;
|
|
1190
|
+
} else {
|
|
1191
|
+
this._optionValues[key] = value;
|
|
1192
|
+
}
|
|
1193
|
+
this._optionValueSources[key] = source;
|
|
1194
|
+
return this;
|
|
1195
|
+
}
|
|
1196
|
+
getOptionValueSource(key) {
|
|
1197
|
+
return this._optionValueSources[key];
|
|
1198
|
+
}
|
|
1199
|
+
getOptionValueSourceWithGlobals(key) {
|
|
1200
|
+
let source;
|
|
1201
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1202
|
+
if (cmd.getOptionValueSource(key) !== undefined) {
|
|
1203
|
+
source = cmd.getOptionValueSource(key);
|
|
1204
|
+
}
|
|
1205
|
+
});
|
|
1206
|
+
return source;
|
|
1207
|
+
}
|
|
1208
|
+
_prepareUserArgs(argv, parseOptions) {
|
|
1209
|
+
if (argv !== undefined && !Array.isArray(argv)) {
|
|
1210
|
+
throw new Error("first parameter to parse must be array or undefined");
|
|
1211
|
+
}
|
|
1212
|
+
parseOptions = parseOptions || {};
|
|
1213
|
+
if (argv === undefined && parseOptions.from === undefined) {
|
|
1214
|
+
if (process2.versions?.electron) {
|
|
1215
|
+
parseOptions.from = "electron";
|
|
1216
|
+
}
|
|
1217
|
+
const execArgv = process2.execArgv ?? [];
|
|
1218
|
+
if (execArgv.includes("-e") || execArgv.includes("--eval") || execArgv.includes("-p") || execArgv.includes("--print")) {
|
|
1219
|
+
parseOptions.from = "eval";
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
if (argv === undefined) {
|
|
1223
|
+
argv = process2.argv;
|
|
1224
|
+
}
|
|
1225
|
+
this.rawArgs = argv.slice();
|
|
1226
|
+
let userArgs;
|
|
1227
|
+
switch (parseOptions.from) {
|
|
1228
|
+
case undefined:
|
|
1229
|
+
case "node":
|
|
1230
|
+
this._scriptPath = argv[1];
|
|
1231
|
+
userArgs = argv.slice(2);
|
|
1232
|
+
break;
|
|
1233
|
+
case "electron":
|
|
1234
|
+
if (process2.defaultApp) {
|
|
1235
|
+
this._scriptPath = argv[1];
|
|
1236
|
+
userArgs = argv.slice(2);
|
|
1237
|
+
} else {
|
|
1238
|
+
userArgs = argv.slice(1);
|
|
1239
|
+
}
|
|
1240
|
+
break;
|
|
1241
|
+
case "user":
|
|
1242
|
+
userArgs = argv.slice(0);
|
|
1243
|
+
break;
|
|
1244
|
+
case "eval":
|
|
1245
|
+
userArgs = argv.slice(1);
|
|
1246
|
+
break;
|
|
1247
|
+
default:
|
|
1248
|
+
throw new Error(`unexpected parse option { from: '${parseOptions.from}' }`);
|
|
1249
|
+
}
|
|
1250
|
+
if (!this._name && this._scriptPath)
|
|
1251
|
+
this.nameFromFilename(this._scriptPath);
|
|
1252
|
+
this._name = this._name || "program";
|
|
1253
|
+
return userArgs;
|
|
1254
|
+
}
|
|
1255
|
+
parse(argv, parseOptions) {
|
|
1256
|
+
this._prepareForParse();
|
|
1257
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1258
|
+
this._parseCommand([], userArgs);
|
|
1259
|
+
return this;
|
|
1260
|
+
}
|
|
1261
|
+
async parseAsync(argv, parseOptions) {
|
|
1262
|
+
this._prepareForParse();
|
|
1263
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1264
|
+
await this._parseCommand([], userArgs);
|
|
1265
|
+
return this;
|
|
1266
|
+
}
|
|
1267
|
+
_prepareForParse() {
|
|
1268
|
+
if (this._savedState === null) {
|
|
1269
|
+
this.saveStateBeforeParse();
|
|
1270
|
+
} else {
|
|
1271
|
+
this.restoreStateBeforeParse();
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
saveStateBeforeParse() {
|
|
1275
|
+
this._savedState = {
|
|
1276
|
+
_name: this._name,
|
|
1277
|
+
_optionValues: { ...this._optionValues },
|
|
1278
|
+
_optionValueSources: { ...this._optionValueSources }
|
|
1279
|
+
};
|
|
1280
|
+
}
|
|
1281
|
+
restoreStateBeforeParse() {
|
|
1282
|
+
if (this._storeOptionsAsProperties)
|
|
1283
|
+
throw new Error(`Can not call parse again when storeOptionsAsProperties is true.
|
|
1284
|
+
- either make a new Command for each call to parse, or stop storing options as properties`);
|
|
1285
|
+
this._name = this._savedState._name;
|
|
1286
|
+
this._scriptPath = null;
|
|
1287
|
+
this.rawArgs = [];
|
|
1288
|
+
this._optionValues = { ...this._savedState._optionValues };
|
|
1289
|
+
this._optionValueSources = { ...this._savedState._optionValueSources };
|
|
1290
|
+
this.args = [];
|
|
1291
|
+
this.processedArgs = [];
|
|
1292
|
+
}
|
|
1293
|
+
_checkForMissingExecutable(executableFile, executableDir, subcommandName) {
|
|
1294
|
+
if (fs.existsSync(executableFile))
|
|
1295
|
+
return;
|
|
1296
|
+
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";
|
|
1297
|
+
const executableMissing = `'${executableFile}' does not exist
|
|
1298
|
+
- if '${subcommandName}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
|
|
1299
|
+
- if the default executable name is not suitable, use the executableFile option to supply a custom name or path
|
|
1300
|
+
- ${executableDirMessage}`;
|
|
1301
|
+
throw new Error(executableMissing);
|
|
1302
|
+
}
|
|
1303
|
+
_executeSubCommand(subcommand, args) {
|
|
1304
|
+
args = args.slice();
|
|
1305
|
+
let launchWithNode = false;
|
|
1306
|
+
const sourceExt = [".js", ".ts", ".tsx", ".mjs", ".cjs"];
|
|
1307
|
+
function findFile(baseDir, baseName) {
|
|
1308
|
+
const localBin = path.resolve(baseDir, baseName);
|
|
1309
|
+
if (fs.existsSync(localBin))
|
|
1310
|
+
return localBin;
|
|
1311
|
+
if (sourceExt.includes(path.extname(baseName)))
|
|
1312
|
+
return;
|
|
1313
|
+
const foundExt = sourceExt.find((ext) => fs.existsSync(`${localBin}${ext}`));
|
|
1314
|
+
if (foundExt)
|
|
1315
|
+
return `${localBin}${foundExt}`;
|
|
1316
|
+
return;
|
|
1317
|
+
}
|
|
1318
|
+
this._checkForMissingMandatoryOptions();
|
|
1319
|
+
this._checkForConflictingOptions();
|
|
1320
|
+
let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;
|
|
1321
|
+
let executableDir = this._executableDir || "";
|
|
1322
|
+
if (this._scriptPath) {
|
|
1323
|
+
let resolvedScriptPath;
|
|
1324
|
+
try {
|
|
1325
|
+
resolvedScriptPath = fs.realpathSync(this._scriptPath);
|
|
1326
|
+
} catch {
|
|
1327
|
+
resolvedScriptPath = this._scriptPath;
|
|
1328
|
+
}
|
|
1329
|
+
executableDir = path.resolve(path.dirname(resolvedScriptPath), executableDir);
|
|
1330
|
+
}
|
|
1331
|
+
if (executableDir) {
|
|
1332
|
+
let localFile = findFile(executableDir, executableFile);
|
|
1333
|
+
if (!localFile && !subcommand._executableFile && this._scriptPath) {
|
|
1334
|
+
const legacyName = path.basename(this._scriptPath, path.extname(this._scriptPath));
|
|
1335
|
+
if (legacyName !== this._name) {
|
|
1336
|
+
localFile = findFile(executableDir, `${legacyName}-${subcommand._name}`);
|
|
1337
|
+
}
|
|
1338
|
+
}
|
|
1339
|
+
executableFile = localFile || executableFile;
|
|
1340
|
+
}
|
|
1341
|
+
launchWithNode = sourceExt.includes(path.extname(executableFile));
|
|
1342
|
+
let proc;
|
|
1343
|
+
if (process2.platform !== "win32") {
|
|
1344
|
+
if (launchWithNode) {
|
|
1345
|
+
args.unshift(executableFile);
|
|
1346
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1347
|
+
proc = childProcess.spawn(process2.argv[0], args, { stdio: "inherit" });
|
|
1348
|
+
} else {
|
|
1349
|
+
proc = childProcess.spawn(executableFile, args, { stdio: "inherit" });
|
|
1350
|
+
}
|
|
1351
|
+
} else {
|
|
1352
|
+
this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
|
|
1353
|
+
args.unshift(executableFile);
|
|
1354
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1355
|
+
proc = childProcess.spawn(process2.execPath, args, { stdio: "inherit" });
|
|
1356
|
+
}
|
|
1357
|
+
if (!proc.killed) {
|
|
1358
|
+
const signals = ["SIGUSR1", "SIGUSR2", "SIGTERM", "SIGINT", "SIGHUP"];
|
|
1359
|
+
signals.forEach((signal) => {
|
|
1360
|
+
process2.on(signal, () => {
|
|
1361
|
+
if (proc.killed === false && proc.exitCode === null) {
|
|
1362
|
+
proc.kill(signal);
|
|
1363
|
+
}
|
|
1364
|
+
});
|
|
1365
|
+
});
|
|
1366
|
+
}
|
|
1367
|
+
const exitCallback = this._exitCallback;
|
|
1368
|
+
proc.on("close", (code) => {
|
|
1369
|
+
code = code ?? 1;
|
|
1370
|
+
if (!exitCallback) {
|
|
1371
|
+
process2.exit(code);
|
|
1372
|
+
} else {
|
|
1373
|
+
exitCallback(new CommanderError(code, "commander.executeSubCommandAsync", "(close)"));
|
|
1374
|
+
}
|
|
1375
|
+
});
|
|
1376
|
+
proc.on("error", (err) => {
|
|
1377
|
+
if (err.code === "ENOENT") {
|
|
1378
|
+
this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
|
|
1379
|
+
} else if (err.code === "EACCES") {
|
|
1380
|
+
throw new Error(`'${executableFile}' not executable`);
|
|
1381
|
+
}
|
|
1382
|
+
if (!exitCallback) {
|
|
1383
|
+
process2.exit(1);
|
|
1384
|
+
} else {
|
|
1385
|
+
const wrappedError = new CommanderError(1, "commander.executeSubCommandAsync", "(error)");
|
|
1386
|
+
wrappedError.nestedError = err;
|
|
1387
|
+
exitCallback(wrappedError);
|
|
1388
|
+
}
|
|
1389
|
+
});
|
|
1390
|
+
this.runningCommand = proc;
|
|
1391
|
+
}
|
|
1392
|
+
_dispatchSubcommand(commandName, operands, unknown) {
|
|
1393
|
+
const subCommand = this._findCommand(commandName);
|
|
1394
|
+
if (!subCommand)
|
|
1395
|
+
this.help({ error: true });
|
|
1396
|
+
subCommand._prepareForParse();
|
|
1397
|
+
let promiseChain;
|
|
1398
|
+
promiseChain = this._chainOrCallSubCommandHook(promiseChain, subCommand, "preSubcommand");
|
|
1399
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
1400
|
+
if (subCommand._executableHandler) {
|
|
1401
|
+
this._executeSubCommand(subCommand, operands.concat(unknown));
|
|
1402
|
+
} else {
|
|
1403
|
+
return subCommand._parseCommand(operands, unknown);
|
|
1404
|
+
}
|
|
1405
|
+
});
|
|
1406
|
+
return promiseChain;
|
|
1407
|
+
}
|
|
1408
|
+
_dispatchHelpCommand(subcommandName) {
|
|
1409
|
+
if (!subcommandName) {
|
|
1410
|
+
this.help();
|
|
1411
|
+
}
|
|
1412
|
+
const subCommand = this._findCommand(subcommandName);
|
|
1413
|
+
if (subCommand && !subCommand._executableHandler) {
|
|
1414
|
+
subCommand.help();
|
|
1415
|
+
}
|
|
1416
|
+
return this._dispatchSubcommand(subcommandName, [], [this._getHelpOption()?.long ?? this._getHelpOption()?.short ?? "--help"]);
|
|
1417
|
+
}
|
|
1418
|
+
_checkNumberOfArguments() {
|
|
1419
|
+
this.registeredArguments.forEach((arg, i) => {
|
|
1420
|
+
if (arg.required && this.args[i] == null) {
|
|
1421
|
+
this.missingArgument(arg.name());
|
|
1422
|
+
}
|
|
1423
|
+
});
|
|
1424
|
+
if (this.registeredArguments.length > 0 && this.registeredArguments[this.registeredArguments.length - 1].variadic) {
|
|
1425
|
+
return;
|
|
1426
|
+
}
|
|
1427
|
+
if (this.args.length > this.registeredArguments.length) {
|
|
1428
|
+
this._excessArguments(this.args);
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1431
|
+
_processArguments() {
|
|
1432
|
+
const myParseArg = (argument, value, previous) => {
|
|
1433
|
+
let parsedValue = value;
|
|
1434
|
+
if (value !== null && argument.parseArg) {
|
|
1435
|
+
const invalidValueMessage = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'.`;
|
|
1436
|
+
parsedValue = this._callParseArg(argument, value, previous, invalidValueMessage);
|
|
1437
|
+
}
|
|
1438
|
+
return parsedValue;
|
|
1439
|
+
};
|
|
1440
|
+
this._checkNumberOfArguments();
|
|
1441
|
+
const processedArgs = [];
|
|
1442
|
+
this.registeredArguments.forEach((declaredArg, index) => {
|
|
1443
|
+
let value = declaredArg.defaultValue;
|
|
1444
|
+
if (declaredArg.variadic) {
|
|
1445
|
+
if (index < this.args.length) {
|
|
1446
|
+
value = this.args.slice(index);
|
|
1447
|
+
if (declaredArg.parseArg) {
|
|
1448
|
+
value = value.reduce((processed, v) => {
|
|
1449
|
+
return myParseArg(declaredArg, v, processed);
|
|
1450
|
+
}, declaredArg.defaultValue);
|
|
1451
|
+
}
|
|
1452
|
+
} else if (value === undefined) {
|
|
1453
|
+
value = [];
|
|
1454
|
+
}
|
|
1455
|
+
} else if (index < this.args.length) {
|
|
1456
|
+
value = this.args[index];
|
|
1457
|
+
if (declaredArg.parseArg) {
|
|
1458
|
+
value = myParseArg(declaredArg, value, declaredArg.defaultValue);
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
1461
|
+
processedArgs[index] = value;
|
|
1462
|
+
});
|
|
1463
|
+
this.processedArgs = processedArgs;
|
|
1464
|
+
}
|
|
1465
|
+
_chainOrCall(promise, fn) {
|
|
1466
|
+
if (promise?.then && typeof promise.then === "function") {
|
|
1467
|
+
return promise.then(() => fn());
|
|
1468
|
+
}
|
|
1469
|
+
return fn();
|
|
1470
|
+
}
|
|
1471
|
+
_chainOrCallHooks(promise, event) {
|
|
1472
|
+
let result = promise;
|
|
1473
|
+
const hooks = [];
|
|
1474
|
+
this._getCommandAndAncestors().reverse().filter((cmd) => cmd._lifeCycleHooks[event] !== undefined).forEach((hookedCommand) => {
|
|
1475
|
+
hookedCommand._lifeCycleHooks[event].forEach((callback) => {
|
|
1476
|
+
hooks.push({ hookedCommand, callback });
|
|
1477
|
+
});
|
|
1478
|
+
});
|
|
1479
|
+
if (event === "postAction") {
|
|
1480
|
+
hooks.reverse();
|
|
1481
|
+
}
|
|
1482
|
+
hooks.forEach((hookDetail) => {
|
|
1483
|
+
result = this._chainOrCall(result, () => {
|
|
1484
|
+
return hookDetail.callback(hookDetail.hookedCommand, this);
|
|
1485
|
+
});
|
|
1486
|
+
});
|
|
1487
|
+
return result;
|
|
1488
|
+
}
|
|
1489
|
+
_chainOrCallSubCommandHook(promise, subCommand, event) {
|
|
1490
|
+
let result = promise;
|
|
1491
|
+
if (this._lifeCycleHooks[event] !== undefined) {
|
|
1492
|
+
this._lifeCycleHooks[event].forEach((hook) => {
|
|
1493
|
+
result = this._chainOrCall(result, () => {
|
|
1494
|
+
return hook(this, subCommand);
|
|
1495
|
+
});
|
|
1496
|
+
});
|
|
1497
|
+
}
|
|
1498
|
+
return result;
|
|
1499
|
+
}
|
|
1500
|
+
_parseCommand(operands, unknown) {
|
|
1501
|
+
const parsed = this.parseOptions(unknown);
|
|
1502
|
+
this._parseOptionsEnv();
|
|
1503
|
+
this._parseOptionsImplied();
|
|
1504
|
+
operands = operands.concat(parsed.operands);
|
|
1505
|
+
unknown = parsed.unknown;
|
|
1506
|
+
this.args = operands.concat(unknown);
|
|
1507
|
+
if (operands && this._findCommand(operands[0])) {
|
|
1508
|
+
return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
|
|
1509
|
+
}
|
|
1510
|
+
if (this._getHelpCommand() && operands[0] === this._getHelpCommand().name()) {
|
|
1511
|
+
return this._dispatchHelpCommand(operands[1]);
|
|
1512
|
+
}
|
|
1513
|
+
if (this._defaultCommandName) {
|
|
1514
|
+
this._outputHelpIfRequested(unknown);
|
|
1515
|
+
return this._dispatchSubcommand(this._defaultCommandName, operands, unknown);
|
|
1516
|
+
}
|
|
1517
|
+
if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) {
|
|
1518
|
+
this.help({ error: true });
|
|
1519
|
+
}
|
|
1520
|
+
this._outputHelpIfRequested(parsed.unknown);
|
|
1521
|
+
this._checkForMissingMandatoryOptions();
|
|
1522
|
+
this._checkForConflictingOptions();
|
|
1523
|
+
const checkForUnknownOptions = () => {
|
|
1524
|
+
if (parsed.unknown.length > 0) {
|
|
1525
|
+
this.unknownOption(parsed.unknown[0]);
|
|
1526
|
+
}
|
|
1527
|
+
};
|
|
1528
|
+
const commandEvent = `command:${this.name()}`;
|
|
1529
|
+
if (this._actionHandler) {
|
|
1530
|
+
checkForUnknownOptions();
|
|
1531
|
+
this._processArguments();
|
|
1532
|
+
let promiseChain;
|
|
1533
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "preAction");
|
|
1534
|
+
promiseChain = this._chainOrCall(promiseChain, () => this._actionHandler(this.processedArgs));
|
|
1535
|
+
if (this.parent) {
|
|
1536
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
1537
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1538
|
+
});
|
|
1539
|
+
}
|
|
1540
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "postAction");
|
|
1541
|
+
return promiseChain;
|
|
1542
|
+
}
|
|
1543
|
+
if (this.parent?.listenerCount(commandEvent)) {
|
|
1544
|
+
checkForUnknownOptions();
|
|
1545
|
+
this._processArguments();
|
|
1546
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1547
|
+
} else if (operands.length) {
|
|
1548
|
+
if (this._findCommand("*")) {
|
|
1549
|
+
return this._dispatchSubcommand("*", operands, unknown);
|
|
1550
|
+
}
|
|
1551
|
+
if (this.listenerCount("command:*")) {
|
|
1552
|
+
this.emit("command:*", operands, unknown);
|
|
1553
|
+
} else if (this.commands.length) {
|
|
1554
|
+
this.unknownCommand();
|
|
1555
|
+
} else {
|
|
1556
|
+
checkForUnknownOptions();
|
|
1557
|
+
this._processArguments();
|
|
1558
|
+
}
|
|
1559
|
+
} else if (this.commands.length) {
|
|
1560
|
+
checkForUnknownOptions();
|
|
1561
|
+
this.help({ error: true });
|
|
1562
|
+
} else {
|
|
1563
|
+
checkForUnknownOptions();
|
|
1564
|
+
this._processArguments();
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
_findCommand(name) {
|
|
1568
|
+
if (!name)
|
|
1569
|
+
return;
|
|
1570
|
+
return this.commands.find((cmd) => cmd._name === name || cmd._aliases.includes(name));
|
|
1571
|
+
}
|
|
1572
|
+
_findOption(arg) {
|
|
1573
|
+
return this.options.find((option) => option.is(arg));
|
|
1574
|
+
}
|
|
1575
|
+
_checkForMissingMandatoryOptions() {
|
|
1576
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1577
|
+
cmd.options.forEach((anOption) => {
|
|
1578
|
+
if (anOption.mandatory && cmd.getOptionValue(anOption.attributeName()) === undefined) {
|
|
1579
|
+
cmd.missingMandatoryOptionValue(anOption);
|
|
1580
|
+
}
|
|
1581
|
+
});
|
|
1582
|
+
});
|
|
1583
|
+
}
|
|
1584
|
+
_checkForConflictingLocalOptions() {
|
|
1585
|
+
const definedNonDefaultOptions = this.options.filter((option) => {
|
|
1586
|
+
const optionKey = option.attributeName();
|
|
1587
|
+
if (this.getOptionValue(optionKey) === undefined) {
|
|
1588
|
+
return false;
|
|
1589
|
+
}
|
|
1590
|
+
return this.getOptionValueSource(optionKey) !== "default";
|
|
1591
|
+
});
|
|
1592
|
+
const optionsWithConflicting = definedNonDefaultOptions.filter((option) => option.conflictsWith.length > 0);
|
|
1593
|
+
optionsWithConflicting.forEach((option) => {
|
|
1594
|
+
const conflictingAndDefined = definedNonDefaultOptions.find((defined) => option.conflictsWith.includes(defined.attributeName()));
|
|
1595
|
+
if (conflictingAndDefined) {
|
|
1596
|
+
this._conflictingOption(option, conflictingAndDefined);
|
|
1597
|
+
}
|
|
1598
|
+
});
|
|
1599
|
+
}
|
|
1600
|
+
_checkForConflictingOptions() {
|
|
1601
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1602
|
+
cmd._checkForConflictingLocalOptions();
|
|
1603
|
+
});
|
|
1604
|
+
}
|
|
1605
|
+
parseOptions(args) {
|
|
1606
|
+
const operands = [];
|
|
1607
|
+
const unknown = [];
|
|
1608
|
+
let dest = operands;
|
|
1609
|
+
function maybeOption(arg) {
|
|
1610
|
+
return arg.length > 1 && arg[0] === "-";
|
|
1611
|
+
}
|
|
1612
|
+
const negativeNumberArg = (arg) => {
|
|
1613
|
+
if (!/^-(\d+|\d*\.\d+)(e[+-]?\d+)?$/.test(arg))
|
|
1614
|
+
return false;
|
|
1615
|
+
return !this._getCommandAndAncestors().some((cmd) => cmd.options.map((opt) => opt.short).some((short) => /^-\d$/.test(short)));
|
|
1616
|
+
};
|
|
1617
|
+
let activeVariadicOption = null;
|
|
1618
|
+
let activeGroup = null;
|
|
1619
|
+
let i = 0;
|
|
1620
|
+
while (i < args.length || activeGroup) {
|
|
1621
|
+
const arg = activeGroup ?? args[i++];
|
|
1622
|
+
activeGroup = null;
|
|
1623
|
+
if (arg === "--") {
|
|
1624
|
+
if (dest === unknown)
|
|
1625
|
+
dest.push(arg);
|
|
1626
|
+
dest.push(...args.slice(i));
|
|
1627
|
+
break;
|
|
1628
|
+
}
|
|
1629
|
+
if (activeVariadicOption && (!maybeOption(arg) || negativeNumberArg(arg))) {
|
|
1630
|
+
this.emit(`option:${activeVariadicOption.name()}`, arg);
|
|
1631
|
+
continue;
|
|
1632
|
+
}
|
|
1633
|
+
activeVariadicOption = null;
|
|
1634
|
+
if (maybeOption(arg)) {
|
|
1635
|
+
const option = this._findOption(arg);
|
|
1636
|
+
if (option) {
|
|
1637
|
+
if (option.required) {
|
|
1638
|
+
const value = args[i++];
|
|
1639
|
+
if (value === undefined)
|
|
1640
|
+
this.optionMissingArgument(option);
|
|
1641
|
+
this.emit(`option:${option.name()}`, value);
|
|
1642
|
+
} else if (option.optional) {
|
|
1643
|
+
let value = null;
|
|
1644
|
+
if (i < args.length && (!maybeOption(args[i]) || negativeNumberArg(args[i]))) {
|
|
1645
|
+
value = args[i++];
|
|
1646
|
+
}
|
|
1647
|
+
this.emit(`option:${option.name()}`, value);
|
|
1648
|
+
} else {
|
|
1649
|
+
this.emit(`option:${option.name()}`);
|
|
1650
|
+
}
|
|
1651
|
+
activeVariadicOption = option.variadic ? option : null;
|
|
1652
|
+
continue;
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
if (arg.length > 2 && arg[0] === "-" && arg[1] !== "-") {
|
|
1656
|
+
const option = this._findOption(`-${arg[1]}`);
|
|
1657
|
+
if (option) {
|
|
1658
|
+
if (option.required || option.optional && this._combineFlagAndOptionalValue) {
|
|
1659
|
+
this.emit(`option:${option.name()}`, arg.slice(2));
|
|
1660
|
+
} else {
|
|
1661
|
+
this.emit(`option:${option.name()}`);
|
|
1662
|
+
activeGroup = `-${arg.slice(2)}`;
|
|
1663
|
+
}
|
|
1664
|
+
continue;
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
if (/^--[^=]+=/.test(arg)) {
|
|
1668
|
+
const index = arg.indexOf("=");
|
|
1669
|
+
const option = this._findOption(arg.slice(0, index));
|
|
1670
|
+
if (option && (option.required || option.optional)) {
|
|
1671
|
+
this.emit(`option:${option.name()}`, arg.slice(index + 1));
|
|
1672
|
+
continue;
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
if (dest === operands && maybeOption(arg) && !(this.commands.length === 0 && negativeNumberArg(arg))) {
|
|
1676
|
+
dest = unknown;
|
|
1677
|
+
}
|
|
1678
|
+
if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
|
|
1679
|
+
if (this._findCommand(arg)) {
|
|
1680
|
+
operands.push(arg);
|
|
1681
|
+
unknown.push(...args.slice(i));
|
|
1682
|
+
break;
|
|
1683
|
+
} else if (this._getHelpCommand() && arg === this._getHelpCommand().name()) {
|
|
1684
|
+
operands.push(arg, ...args.slice(i));
|
|
1685
|
+
break;
|
|
1686
|
+
} else if (this._defaultCommandName) {
|
|
1687
|
+
unknown.push(arg, ...args.slice(i));
|
|
1688
|
+
break;
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
if (this._passThroughOptions) {
|
|
1692
|
+
dest.push(arg, ...args.slice(i));
|
|
1693
|
+
break;
|
|
1694
|
+
}
|
|
1695
|
+
dest.push(arg);
|
|
1696
|
+
}
|
|
1697
|
+
return { operands, unknown };
|
|
1698
|
+
}
|
|
1699
|
+
opts() {
|
|
1700
|
+
if (this._storeOptionsAsProperties) {
|
|
1701
|
+
const result = {};
|
|
1702
|
+
const len = this.options.length;
|
|
1703
|
+
for (let i = 0;i < len; i++) {
|
|
1704
|
+
const key = this.options[i].attributeName();
|
|
1705
|
+
result[key] = key === this._versionOptionName ? this._version : this[key];
|
|
1706
|
+
}
|
|
1707
|
+
return result;
|
|
1708
|
+
}
|
|
1709
|
+
return this._optionValues;
|
|
1710
|
+
}
|
|
1711
|
+
optsWithGlobals() {
|
|
1712
|
+
return this._getCommandAndAncestors().reduce((combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()), {});
|
|
1713
|
+
}
|
|
1714
|
+
error(message, errorOptions) {
|
|
1715
|
+
this._outputConfiguration.outputError(`${message}
|
|
1716
|
+
`, this._outputConfiguration.writeErr);
|
|
1717
|
+
if (typeof this._showHelpAfterError === "string") {
|
|
1718
|
+
this._outputConfiguration.writeErr(`${this._showHelpAfterError}
|
|
1719
|
+
`);
|
|
1720
|
+
} else if (this._showHelpAfterError) {
|
|
1721
|
+
this._outputConfiguration.writeErr(`
|
|
1722
|
+
`);
|
|
1723
|
+
this.outputHelp({ error: true });
|
|
1724
|
+
}
|
|
1725
|
+
const config = errorOptions || {};
|
|
1726
|
+
const exitCode = config.exitCode || 1;
|
|
1727
|
+
const code = config.code || "commander.error";
|
|
1728
|
+
this._exit(exitCode, code, message);
|
|
1729
|
+
}
|
|
1730
|
+
_parseOptionsEnv() {
|
|
1731
|
+
this.options.forEach((option) => {
|
|
1732
|
+
if (option.envVar && option.envVar in process2.env) {
|
|
1733
|
+
const optionKey = option.attributeName();
|
|
1734
|
+
if (this.getOptionValue(optionKey) === undefined || ["default", "config", "env"].includes(this.getOptionValueSource(optionKey))) {
|
|
1735
|
+
if (option.required || option.optional) {
|
|
1736
|
+
this.emit(`optionEnv:${option.name()}`, process2.env[option.envVar]);
|
|
1737
|
+
} else {
|
|
1738
|
+
this.emit(`optionEnv:${option.name()}`);
|
|
1739
|
+
}
|
|
1740
|
+
}
|
|
1741
|
+
}
|
|
1742
|
+
});
|
|
1743
|
+
}
|
|
1744
|
+
_parseOptionsImplied() {
|
|
1745
|
+
const dualHelper = new DualOptions(this.options);
|
|
1746
|
+
const hasCustomOptionValue = (optionKey) => {
|
|
1747
|
+
return this.getOptionValue(optionKey) !== undefined && !["default", "implied"].includes(this.getOptionValueSource(optionKey));
|
|
1748
|
+
};
|
|
1749
|
+
this.options.filter((option) => option.implied !== undefined && hasCustomOptionValue(option.attributeName()) && dualHelper.valueFromOption(this.getOptionValue(option.attributeName()), option)).forEach((option) => {
|
|
1750
|
+
Object.keys(option.implied).filter((impliedKey) => !hasCustomOptionValue(impliedKey)).forEach((impliedKey) => {
|
|
1751
|
+
this.setOptionValueWithSource(impliedKey, option.implied[impliedKey], "implied");
|
|
1752
|
+
});
|
|
1753
|
+
});
|
|
1754
|
+
}
|
|
1755
|
+
missingArgument(name) {
|
|
1756
|
+
const message = `error: missing required argument '${name}'`;
|
|
1757
|
+
this.error(message, { code: "commander.missingArgument" });
|
|
1758
|
+
}
|
|
1759
|
+
optionMissingArgument(option) {
|
|
1760
|
+
const message = `error: option '${option.flags}' argument missing`;
|
|
1761
|
+
this.error(message, { code: "commander.optionMissingArgument" });
|
|
1762
|
+
}
|
|
1763
|
+
missingMandatoryOptionValue(option) {
|
|
1764
|
+
const message = `error: required option '${option.flags}' not specified`;
|
|
1765
|
+
this.error(message, { code: "commander.missingMandatoryOptionValue" });
|
|
1766
|
+
}
|
|
1767
|
+
_conflictingOption(option, conflictingOption) {
|
|
1768
|
+
const findBestOptionFromValue = (option2) => {
|
|
1769
|
+
const optionKey = option2.attributeName();
|
|
1770
|
+
const optionValue = this.getOptionValue(optionKey);
|
|
1771
|
+
const negativeOption = this.options.find((target) => target.negate && optionKey === target.attributeName());
|
|
1772
|
+
const positiveOption = this.options.find((target) => !target.negate && optionKey === target.attributeName());
|
|
1773
|
+
if (negativeOption && (negativeOption.presetArg === undefined && optionValue === false || negativeOption.presetArg !== undefined && optionValue === negativeOption.presetArg)) {
|
|
1774
|
+
return negativeOption;
|
|
1775
|
+
}
|
|
1776
|
+
return positiveOption || option2;
|
|
1777
|
+
};
|
|
1778
|
+
const getErrorMessage = (option2) => {
|
|
1779
|
+
const bestOption = findBestOptionFromValue(option2);
|
|
1780
|
+
const optionKey = bestOption.attributeName();
|
|
1781
|
+
const source = this.getOptionValueSource(optionKey);
|
|
1782
|
+
if (source === "env") {
|
|
1783
|
+
return `environment variable '${bestOption.envVar}'`;
|
|
1784
|
+
}
|
|
1785
|
+
return `option '${bestOption.flags}'`;
|
|
1786
|
+
};
|
|
1787
|
+
const message = `error: ${getErrorMessage(option)} cannot be used with ${getErrorMessage(conflictingOption)}`;
|
|
1788
|
+
this.error(message, { code: "commander.conflictingOption" });
|
|
1789
|
+
}
|
|
1790
|
+
unknownOption(flag) {
|
|
1791
|
+
if (this._allowUnknownOption)
|
|
1792
|
+
return;
|
|
1793
|
+
let suggestion = "";
|
|
1794
|
+
if (flag.startsWith("--") && this._showSuggestionAfterError) {
|
|
1795
|
+
let candidateFlags = [];
|
|
1796
|
+
let command = this;
|
|
1797
|
+
do {
|
|
1798
|
+
const moreFlags = command.createHelp().visibleOptions(command).filter((option) => option.long).map((option) => option.long);
|
|
1799
|
+
candidateFlags = candidateFlags.concat(moreFlags);
|
|
1800
|
+
command = command.parent;
|
|
1801
|
+
} while (command && !command._enablePositionalOptions);
|
|
1802
|
+
suggestion = suggestSimilar(flag, candidateFlags);
|
|
1803
|
+
}
|
|
1804
|
+
const message = `error: unknown option '${flag}'${suggestion}`;
|
|
1805
|
+
this.error(message, { code: "commander.unknownOption" });
|
|
1806
|
+
}
|
|
1807
|
+
_excessArguments(receivedArgs) {
|
|
1808
|
+
if (this._allowExcessArguments)
|
|
1809
|
+
return;
|
|
1810
|
+
const expected = this.registeredArguments.length;
|
|
1811
|
+
const s = expected === 1 ? "" : "s";
|
|
1812
|
+
const forSubcommand = this.parent ? ` for '${this.name()}'` : "";
|
|
1813
|
+
const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;
|
|
1814
|
+
this.error(message, { code: "commander.excessArguments" });
|
|
1815
|
+
}
|
|
1816
|
+
unknownCommand() {
|
|
1817
|
+
const unknownName = this.args[0];
|
|
1818
|
+
let suggestion = "";
|
|
1819
|
+
if (this._showSuggestionAfterError) {
|
|
1820
|
+
const candidateNames = [];
|
|
1821
|
+
this.createHelp().visibleCommands(this).forEach((command) => {
|
|
1822
|
+
candidateNames.push(command.name());
|
|
1823
|
+
if (command.alias())
|
|
1824
|
+
candidateNames.push(command.alias());
|
|
1825
|
+
});
|
|
1826
|
+
suggestion = suggestSimilar(unknownName, candidateNames);
|
|
1827
|
+
}
|
|
1828
|
+
const message = `error: unknown command '${unknownName}'${suggestion}`;
|
|
1829
|
+
this.error(message, { code: "commander.unknownCommand" });
|
|
1830
|
+
}
|
|
1831
|
+
version(str, flags, description) {
|
|
1832
|
+
if (str === undefined)
|
|
1833
|
+
return this._version;
|
|
1834
|
+
this._version = str;
|
|
1835
|
+
flags = flags || "-V, --version";
|
|
1836
|
+
description = description || "output the version number";
|
|
1837
|
+
const versionOption = this.createOption(flags, description);
|
|
1838
|
+
this._versionOptionName = versionOption.attributeName();
|
|
1839
|
+
this._registerOption(versionOption);
|
|
1840
|
+
this.on("option:" + versionOption.name(), () => {
|
|
1841
|
+
this._outputConfiguration.writeOut(`${str}
|
|
1842
|
+
`);
|
|
1843
|
+
this._exit(0, "commander.version", str);
|
|
1844
|
+
});
|
|
1845
|
+
return this;
|
|
1846
|
+
}
|
|
1847
|
+
description(str, argsDescription) {
|
|
1848
|
+
if (str === undefined && argsDescription === undefined)
|
|
1849
|
+
return this._description;
|
|
1850
|
+
this._description = str;
|
|
1851
|
+
if (argsDescription) {
|
|
1852
|
+
this._argsDescription = argsDescription;
|
|
1853
|
+
}
|
|
1854
|
+
return this;
|
|
1855
|
+
}
|
|
1856
|
+
summary(str) {
|
|
1857
|
+
if (str === undefined)
|
|
1858
|
+
return this._summary;
|
|
1859
|
+
this._summary = str;
|
|
1860
|
+
return this;
|
|
1861
|
+
}
|
|
1862
|
+
alias(alias) {
|
|
1863
|
+
if (alias === undefined)
|
|
1864
|
+
return this._aliases[0];
|
|
1865
|
+
let command = this;
|
|
1866
|
+
if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
|
|
1867
|
+
command = this.commands[this.commands.length - 1];
|
|
1868
|
+
}
|
|
1869
|
+
if (alias === command._name)
|
|
1870
|
+
throw new Error("Command alias can't be the same as its name");
|
|
1871
|
+
const matchingCommand = this.parent?._findCommand(alias);
|
|
1872
|
+
if (matchingCommand) {
|
|
1873
|
+
const existingCmd = [matchingCommand.name()].concat(matchingCommand.aliases()).join("|");
|
|
1874
|
+
throw new Error(`cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`);
|
|
1875
|
+
}
|
|
1876
|
+
command._aliases.push(alias);
|
|
1877
|
+
return this;
|
|
1878
|
+
}
|
|
1879
|
+
aliases(aliases) {
|
|
1880
|
+
if (aliases === undefined)
|
|
1881
|
+
return this._aliases;
|
|
1882
|
+
aliases.forEach((alias) => this.alias(alias));
|
|
1883
|
+
return this;
|
|
1884
|
+
}
|
|
1885
|
+
usage(str) {
|
|
1886
|
+
if (str === undefined) {
|
|
1887
|
+
if (this._usage)
|
|
1888
|
+
return this._usage;
|
|
1889
|
+
const args = this.registeredArguments.map((arg) => {
|
|
1890
|
+
return humanReadableArgName(arg);
|
|
1891
|
+
});
|
|
1892
|
+
return [].concat(this.options.length || this._helpOption !== null ? "[options]" : [], this.commands.length ? "[command]" : [], this.registeredArguments.length ? args : []).join(" ");
|
|
1893
|
+
}
|
|
1894
|
+
this._usage = str;
|
|
1895
|
+
return this;
|
|
1896
|
+
}
|
|
1897
|
+
name(str) {
|
|
1898
|
+
if (str === undefined)
|
|
1899
|
+
return this._name;
|
|
1900
|
+
this._name = str;
|
|
1901
|
+
return this;
|
|
1902
|
+
}
|
|
1903
|
+
helpGroup(heading) {
|
|
1904
|
+
if (heading === undefined)
|
|
1905
|
+
return this._helpGroupHeading ?? "";
|
|
1906
|
+
this._helpGroupHeading = heading;
|
|
1907
|
+
return this;
|
|
1908
|
+
}
|
|
1909
|
+
commandsGroup(heading) {
|
|
1910
|
+
if (heading === undefined)
|
|
1911
|
+
return this._defaultCommandGroup ?? "";
|
|
1912
|
+
this._defaultCommandGroup = heading;
|
|
1913
|
+
return this;
|
|
1914
|
+
}
|
|
1915
|
+
optionsGroup(heading) {
|
|
1916
|
+
if (heading === undefined)
|
|
1917
|
+
return this._defaultOptionGroup ?? "";
|
|
1918
|
+
this._defaultOptionGroup = heading;
|
|
1919
|
+
return this;
|
|
1920
|
+
}
|
|
1921
|
+
_initOptionGroup(option) {
|
|
1922
|
+
if (this._defaultOptionGroup && !option.helpGroupHeading)
|
|
1923
|
+
option.helpGroup(this._defaultOptionGroup);
|
|
1924
|
+
}
|
|
1925
|
+
_initCommandGroup(cmd) {
|
|
1926
|
+
if (this._defaultCommandGroup && !cmd.helpGroup())
|
|
1927
|
+
cmd.helpGroup(this._defaultCommandGroup);
|
|
1928
|
+
}
|
|
1929
|
+
nameFromFilename(filename) {
|
|
1930
|
+
this._name = path.basename(filename, path.extname(filename));
|
|
1931
|
+
return this;
|
|
1932
|
+
}
|
|
1933
|
+
executableDir(path2) {
|
|
1934
|
+
if (path2 === undefined)
|
|
1935
|
+
return this._executableDir;
|
|
1936
|
+
this._executableDir = path2;
|
|
1937
|
+
return this;
|
|
1938
|
+
}
|
|
1939
|
+
helpInformation(contextOptions) {
|
|
1940
|
+
const helper = this.createHelp();
|
|
1941
|
+
const context = this._getOutputContext(contextOptions);
|
|
1942
|
+
helper.prepareContext({
|
|
1943
|
+
error: context.error,
|
|
1944
|
+
helpWidth: context.helpWidth,
|
|
1945
|
+
outputHasColors: context.hasColors
|
|
1946
|
+
});
|
|
1947
|
+
const text = helper.formatHelp(this, helper);
|
|
1948
|
+
if (context.hasColors)
|
|
1949
|
+
return text;
|
|
1950
|
+
return this._outputConfiguration.stripColor(text);
|
|
1951
|
+
}
|
|
1952
|
+
_getOutputContext(contextOptions) {
|
|
1953
|
+
contextOptions = contextOptions || {};
|
|
1954
|
+
const error = !!contextOptions.error;
|
|
1955
|
+
let baseWrite;
|
|
1956
|
+
let hasColors;
|
|
1957
|
+
let helpWidth;
|
|
1958
|
+
if (error) {
|
|
1959
|
+
baseWrite = (str) => this._outputConfiguration.writeErr(str);
|
|
1960
|
+
hasColors = this._outputConfiguration.getErrHasColors();
|
|
1961
|
+
helpWidth = this._outputConfiguration.getErrHelpWidth();
|
|
1962
|
+
} else {
|
|
1963
|
+
baseWrite = (str) => this._outputConfiguration.writeOut(str);
|
|
1964
|
+
hasColors = this._outputConfiguration.getOutHasColors();
|
|
1965
|
+
helpWidth = this._outputConfiguration.getOutHelpWidth();
|
|
1966
|
+
}
|
|
1967
|
+
const write = (str) => {
|
|
1968
|
+
if (!hasColors)
|
|
1969
|
+
str = this._outputConfiguration.stripColor(str);
|
|
1970
|
+
return baseWrite(str);
|
|
1971
|
+
};
|
|
1972
|
+
return { error, write, hasColors, helpWidth };
|
|
1973
|
+
}
|
|
1974
|
+
outputHelp(contextOptions) {
|
|
1975
|
+
let deprecatedCallback;
|
|
1976
|
+
if (typeof contextOptions === "function") {
|
|
1977
|
+
deprecatedCallback = contextOptions;
|
|
1978
|
+
contextOptions = undefined;
|
|
1979
|
+
}
|
|
1980
|
+
const outputContext = this._getOutputContext(contextOptions);
|
|
1981
|
+
const eventContext = {
|
|
1982
|
+
error: outputContext.error,
|
|
1983
|
+
write: outputContext.write,
|
|
1984
|
+
command: this
|
|
1985
|
+
};
|
|
1986
|
+
this._getCommandAndAncestors().reverse().forEach((command) => command.emit("beforeAllHelp", eventContext));
|
|
1987
|
+
this.emit("beforeHelp", eventContext);
|
|
1988
|
+
let helpInformation = this.helpInformation({ error: outputContext.error });
|
|
1989
|
+
if (deprecatedCallback) {
|
|
1990
|
+
helpInformation = deprecatedCallback(helpInformation);
|
|
1991
|
+
if (typeof helpInformation !== "string" && !Buffer.isBuffer(helpInformation)) {
|
|
1992
|
+
throw new Error("outputHelp callback must return a string or a Buffer");
|
|
1993
|
+
}
|
|
1994
|
+
}
|
|
1995
|
+
outputContext.write(helpInformation);
|
|
1996
|
+
if (this._getHelpOption()?.long) {
|
|
1997
|
+
this.emit(this._getHelpOption().long);
|
|
1998
|
+
}
|
|
1999
|
+
this.emit("afterHelp", eventContext);
|
|
2000
|
+
this._getCommandAndAncestors().forEach((command) => command.emit("afterAllHelp", eventContext));
|
|
2001
|
+
}
|
|
2002
|
+
helpOption(flags, description) {
|
|
2003
|
+
if (typeof flags === "boolean") {
|
|
2004
|
+
if (flags) {
|
|
2005
|
+
if (this._helpOption === null)
|
|
2006
|
+
this._helpOption = undefined;
|
|
2007
|
+
if (this._defaultOptionGroup) {
|
|
2008
|
+
this._initOptionGroup(this._getHelpOption());
|
|
2009
|
+
}
|
|
2010
|
+
} else {
|
|
2011
|
+
this._helpOption = null;
|
|
2012
|
+
}
|
|
2013
|
+
return this;
|
|
2014
|
+
}
|
|
2015
|
+
this._helpOption = this.createOption(flags ?? "-h, --help", description ?? "display help for command");
|
|
2016
|
+
if (flags || description)
|
|
2017
|
+
this._initOptionGroup(this._helpOption);
|
|
2018
|
+
return this;
|
|
2019
|
+
}
|
|
2020
|
+
_getHelpOption() {
|
|
2021
|
+
if (this._helpOption === undefined) {
|
|
2022
|
+
this.helpOption(undefined, undefined);
|
|
2023
|
+
}
|
|
2024
|
+
return this._helpOption;
|
|
2025
|
+
}
|
|
2026
|
+
addHelpOption(option) {
|
|
2027
|
+
this._helpOption = option;
|
|
2028
|
+
this._initOptionGroup(option);
|
|
2029
|
+
return this;
|
|
2030
|
+
}
|
|
2031
|
+
help(contextOptions) {
|
|
2032
|
+
this.outputHelp(contextOptions);
|
|
2033
|
+
let exitCode = Number(process2.exitCode ?? 0);
|
|
2034
|
+
if (exitCode === 0 && contextOptions && typeof contextOptions !== "function" && contextOptions.error) {
|
|
2035
|
+
exitCode = 1;
|
|
2036
|
+
}
|
|
2037
|
+
this._exit(exitCode, "commander.help", "(outputHelp)");
|
|
2038
|
+
}
|
|
2039
|
+
addHelpText(position, text) {
|
|
2040
|
+
const allowedValues = ["beforeAll", "before", "after", "afterAll"];
|
|
2041
|
+
if (!allowedValues.includes(position)) {
|
|
2042
|
+
throw new Error(`Unexpected value for position to addHelpText.
|
|
2043
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
2044
|
+
}
|
|
2045
|
+
const helpEvent = `${position}Help`;
|
|
2046
|
+
this.on(helpEvent, (context) => {
|
|
2047
|
+
let helpStr;
|
|
2048
|
+
if (typeof text === "function") {
|
|
2049
|
+
helpStr = text({ error: context.error, command: context.command });
|
|
2050
|
+
} else {
|
|
2051
|
+
helpStr = text;
|
|
2052
|
+
}
|
|
2053
|
+
if (helpStr) {
|
|
2054
|
+
context.write(`${helpStr}
|
|
2055
|
+
`);
|
|
2056
|
+
}
|
|
2057
|
+
});
|
|
2058
|
+
return this;
|
|
2059
|
+
}
|
|
2060
|
+
_outputHelpIfRequested(args) {
|
|
2061
|
+
const helpOption = this._getHelpOption();
|
|
2062
|
+
const helpRequested = helpOption && args.find((arg) => helpOption.is(arg));
|
|
2063
|
+
if (helpRequested) {
|
|
2064
|
+
this.outputHelp();
|
|
2065
|
+
this._exit(0, "commander.helpDisplayed", "(outputHelp)");
|
|
2066
|
+
}
|
|
2067
|
+
}
|
|
2068
|
+
}
|
|
2069
|
+
function incrementNodeInspectorPort(args) {
|
|
2070
|
+
return args.map((arg) => {
|
|
2071
|
+
if (!arg.startsWith("--inspect")) {
|
|
2072
|
+
return arg;
|
|
2073
|
+
}
|
|
2074
|
+
let debugOption;
|
|
2075
|
+
let debugHost = "127.0.0.1";
|
|
2076
|
+
let debugPort = "9229";
|
|
2077
|
+
let match;
|
|
2078
|
+
if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
|
|
2079
|
+
debugOption = match[1];
|
|
2080
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
|
|
2081
|
+
debugOption = match[1];
|
|
2082
|
+
if (/^\d+$/.test(match[3])) {
|
|
2083
|
+
debugPort = match[3];
|
|
2084
|
+
} else {
|
|
2085
|
+
debugHost = match[3];
|
|
2086
|
+
}
|
|
2087
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) {
|
|
2088
|
+
debugOption = match[1];
|
|
2089
|
+
debugHost = match[3];
|
|
2090
|
+
debugPort = match[4];
|
|
2091
|
+
}
|
|
2092
|
+
if (debugOption && debugPort !== "0") {
|
|
2093
|
+
return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
|
|
2094
|
+
}
|
|
2095
|
+
return arg;
|
|
2096
|
+
});
|
|
2097
|
+
}
|
|
2098
|
+
function useColor() {
|
|
2099
|
+
if (process2.env.NO_COLOR || process2.env.FORCE_COLOR === "0" || process2.env.FORCE_COLOR === "false")
|
|
2100
|
+
return false;
|
|
2101
|
+
if (process2.env.FORCE_COLOR || process2.env.CLICOLOR_FORCE !== undefined)
|
|
2102
|
+
return true;
|
|
2103
|
+
return;
|
|
2104
|
+
}
|
|
2105
|
+
exports.Command = Command;
|
|
2106
|
+
exports.useColor = useColor;
|
|
2107
|
+
});
|
|
2108
|
+
|
|
2109
|
+
// node_modules/commander/index.js
|
|
2110
|
+
var require_commander = __commonJS((exports) => {
|
|
2111
|
+
var { Argument } = require_argument();
|
|
2112
|
+
var { Command } = require_command();
|
|
2113
|
+
var { CommanderError, InvalidArgumentError } = require_error();
|
|
2114
|
+
var { Help } = require_help();
|
|
2115
|
+
var { Option } = require_option();
|
|
2116
|
+
exports.program = new Command;
|
|
2117
|
+
exports.createCommand = (name) => new Command(name);
|
|
2118
|
+
exports.createOption = (flags, description) => new Option(flags, description);
|
|
2119
|
+
exports.createArgument = (name, description) => new Argument(name, description);
|
|
2120
|
+
exports.Command = Command;
|
|
2121
|
+
exports.Option = Option;
|
|
2122
|
+
exports.Argument = Argument;
|
|
2123
|
+
exports.Help = Help;
|
|
2124
|
+
exports.CommanderError = CommanderError;
|
|
2125
|
+
exports.InvalidArgumentError = InvalidArgumentError;
|
|
2126
|
+
exports.InvalidOptionArgumentError = InvalidArgumentError;
|
|
2127
|
+
});
|
|
2128
|
+
|
|
2129
|
+
// node_modules/commander/esm.mjs
|
|
2130
|
+
var import__ = __toESM(require_commander(), 1);
|
|
2131
|
+
var {
|
|
2132
|
+
program,
|
|
2133
|
+
createCommand,
|
|
2134
|
+
createArgument,
|
|
2135
|
+
createOption,
|
|
2136
|
+
CommanderError,
|
|
2137
|
+
InvalidArgumentError,
|
|
2138
|
+
InvalidOptionArgumentError,
|
|
2139
|
+
Command,
|
|
2140
|
+
Argument,
|
|
2141
|
+
Option,
|
|
2142
|
+
Help
|
|
2143
|
+
} = import__.default;
|
|
2144
|
+
|
|
2145
|
+
// src/commands/new.ts
|
|
2146
|
+
import { existsSync as existsSync3, mkdirSync, writeFileSync } from "fs";
|
|
2147
|
+
import path3 from "path";
|
|
2148
|
+
|
|
2149
|
+
// src/lib/paths.ts
|
|
2150
|
+
import { existsSync } from "fs";
|
|
2151
|
+
import path from "path";
|
|
2152
|
+
var KNOWLEDGE_DIR = "knowledge";
|
|
2153
|
+
function findKnowledgeDir(cwd = process.cwd()) {
|
|
2154
|
+
const envOverride = process.env["PK_KNOWLEDGE_DIR"];
|
|
2155
|
+
if (envOverride)
|
|
2156
|
+
return path.resolve(envOverride);
|
|
2157
|
+
let current = path.resolve(cwd);
|
|
2158
|
+
while (true) {
|
|
2159
|
+
const candidate = path.join(current, KNOWLEDGE_DIR);
|
|
2160
|
+
if (existsSync(candidate))
|
|
2161
|
+
return candidate;
|
|
2162
|
+
const parent = path.dirname(current);
|
|
2163
|
+
if (parent === current)
|
|
2164
|
+
break;
|
|
2165
|
+
current = parent;
|
|
2166
|
+
}
|
|
2167
|
+
return path.join(path.resolve(cwd), KNOWLEDGE_DIR);
|
|
2168
|
+
}
|
|
2169
|
+
|
|
2170
|
+
// src/lib/templates.ts
|
|
2171
|
+
var TEMPLATES = {
|
|
2172
|
+
decision: `---
|
|
2173
|
+
id: decision-{{date}}-{{slug}}
|
|
2174
|
+
type: decision
|
|
2175
|
+
title: {{title}}
|
|
2176
|
+
created: {{date}}
|
|
2177
|
+
updated: {{date}}
|
|
2178
|
+
status: accepted
|
|
2179
|
+
tags: [{{tags}}]
|
|
2180
|
+
---
|
|
2181
|
+
|
|
2182
|
+
## Decision
|
|
2183
|
+
|
|
2184
|
+
What was decided.
|
|
2185
|
+
|
|
2186
|
+
## Context
|
|
2187
|
+
|
|
2188
|
+
Why this decision came up.
|
|
2189
|
+
|
|
2190
|
+
## Rationale
|
|
2191
|
+
|
|
2192
|
+
Why this option won.
|
|
2193
|
+
|
|
2194
|
+
## Consequences
|
|
2195
|
+
|
|
2196
|
+
What this changes or constrains.
|
|
2197
|
+
|
|
2198
|
+
## Related
|
|
2199
|
+
|
|
2200
|
+
Links to source, questions, notes, or superseded decisions.
|
|
2201
|
+
`,
|
|
2202
|
+
note: `---
|
|
2203
|
+
id: note-{{date}}-{{slug}}
|
|
2204
|
+
type: note
|
|
2205
|
+
title: {{title}}
|
|
2206
|
+
created: {{date}}
|
|
2207
|
+
updated: {{date}}
|
|
2208
|
+
status: active
|
|
2209
|
+
tags: [{{tags}}]
|
|
2210
|
+
---
|
|
2211
|
+
|
|
2212
|
+
## Summary
|
|
2213
|
+
|
|
2214
|
+
One short paragraph.
|
|
2215
|
+
|
|
2216
|
+
## Details
|
|
2217
|
+
|
|
2218
|
+
Durable project knowledge.
|
|
2219
|
+
|
|
2220
|
+
## Evidence
|
|
2221
|
+
|
|
2222
|
+
Source links, files, quotes, or observations.
|
|
2223
|
+
|
|
2224
|
+
## Related
|
|
2225
|
+
|
|
2226
|
+
Links to related notes, decisions, or questions.
|
|
2227
|
+
`,
|
|
2228
|
+
question: `---
|
|
2229
|
+
id: question-{{date}}-{{slug}}
|
|
2230
|
+
type: question
|
|
2231
|
+
title: {{title}}
|
|
2232
|
+
created: {{date}}
|
|
2233
|
+
updated: {{date}}
|
|
2234
|
+
status: open
|
|
2235
|
+
tags: [{{tags}}]
|
|
2236
|
+
---
|
|
2237
|
+
|
|
2238
|
+
## Question
|
|
2239
|
+
|
|
2240
|
+
What is being asked.
|
|
2241
|
+
|
|
2242
|
+
## Why It Matters
|
|
2243
|
+
|
|
2244
|
+
What depends on the answer.
|
|
2245
|
+
|
|
2246
|
+
## Current Understanding
|
|
2247
|
+
|
|
2248
|
+
What is known so far.
|
|
2249
|
+
|
|
2250
|
+
## Resolution
|
|
2251
|
+
|
|
2252
|
+
Leave blank until answered.
|
|
2253
|
+
`,
|
|
2254
|
+
source: `---
|
|
2255
|
+
id: source-{{date}}-{{slug}}
|
|
2256
|
+
type: source
|
|
2257
|
+
title: {{title}}
|
|
2258
|
+
created: {{date}}
|
|
2259
|
+
updated: {{date}}
|
|
2260
|
+
status: unprocessed
|
|
2261
|
+
tags: [{{tags}}]
|
|
2262
|
+
---
|
|
2263
|
+
|
|
2264
|
+
## Source
|
|
2265
|
+
|
|
2266
|
+
Origin: paste URL, filename, or description here.
|
|
2267
|
+
|
|
2268
|
+
## Raw Material
|
|
2269
|
+
|
|
2270
|
+
Paste or summarize the raw input here.
|
|
2271
|
+
|
|
2272
|
+
## Extracted Items
|
|
2273
|
+
|
|
2274
|
+
Leave blank until processed \u2014 list notes/decisions/questions extracted from this source.
|
|
2275
|
+
`
|
|
2276
|
+
};
|
|
2277
|
+
function renderTemplate(type, vars) {
|
|
2278
|
+
const tmpl = TEMPLATES[type];
|
|
2279
|
+
if (!tmpl)
|
|
2280
|
+
throw new Error(`Unknown note type: ${type}`);
|
|
2281
|
+
return Object.entries(vars).reduce((t, [k, v]) => t.replaceAll(`{{${k}}}`, v), tmpl);
|
|
2282
|
+
}
|
|
2283
|
+
|
|
2284
|
+
// src/lib/notes.ts
|
|
2285
|
+
import { existsSync as existsSync2, readdirSync, readFileSync, statSync } from "fs";
|
|
2286
|
+
import path2 from "path";
|
|
2287
|
+
var TYPE_DIRS = {
|
|
2288
|
+
decision: "decisions",
|
|
2289
|
+
index: "indexes",
|
|
2290
|
+
note: "notes",
|
|
2291
|
+
question: "questions",
|
|
2292
|
+
source: "sources"
|
|
2293
|
+
};
|
|
2294
|
+
var STATUSES = {
|
|
2295
|
+
decision: ["proposed", "accepted", "superseded"],
|
|
2296
|
+
index: ["active", "archived"],
|
|
2297
|
+
note: ["active", "superseded", "archived"],
|
|
2298
|
+
question: ["open", "answered", "obsolete"],
|
|
2299
|
+
source: ["unprocessed", "processed", "archived"]
|
|
2300
|
+
};
|
|
2301
|
+
var REQUIRED_SECTIONS = {
|
|
2302
|
+
decision: ["Decision", "Context", "Rationale", "Consequences", "Related"],
|
|
2303
|
+
index: ["Purpose", "Key Links", "Open Questions", "Recent Changes"],
|
|
2304
|
+
note: ["Summary", "Details", "Evidence", "Related"],
|
|
2305
|
+
question: ["Question", "Why It Matters", "Current Understanding", "Resolution"],
|
|
2306
|
+
source: ["Source", "Raw Material", "Extracted Items"]
|
|
2307
|
+
};
|
|
2308
|
+
var LENGTH_WARN = {
|
|
2309
|
+
decision: 120,
|
|
2310
|
+
index: 200,
|
|
2311
|
+
note: 150,
|
|
2312
|
+
question: 80,
|
|
2313
|
+
source: 400
|
|
2314
|
+
};
|
|
2315
|
+
var PRIMARY_SECTION = {
|
|
2316
|
+
decision: "Decision",
|
|
2317
|
+
index: "Purpose",
|
|
2318
|
+
note: "Summary",
|
|
2319
|
+
question: "Question",
|
|
2320
|
+
source: "Source"
|
|
2321
|
+
};
|
|
2322
|
+
function parseFrontmatter(text) {
|
|
2323
|
+
if (!text.startsWith(`---
|
|
2324
|
+
`))
|
|
2325
|
+
return { err: "missing opening frontmatter delimiter" };
|
|
2326
|
+
const end = text.indexOf(`
|
|
2327
|
+
---
|
|
2328
|
+
`, 4);
|
|
2329
|
+
if (end === -1)
|
|
2330
|
+
return { err: "missing closing frontmatter delimiter" };
|
|
2331
|
+
const raw = text.slice(4, end);
|
|
2332
|
+
const body = text.slice(end + 5);
|
|
2333
|
+
const meta = {};
|
|
2334
|
+
for (const line of raw.split(`
|
|
2335
|
+
`)) {
|
|
2336
|
+
const trimmed = line.trimEnd();
|
|
2337
|
+
if (!trimmed.trim())
|
|
2338
|
+
continue;
|
|
2339
|
+
const colon = trimmed.indexOf(":");
|
|
2340
|
+
if (colon === -1)
|
|
2341
|
+
return { err: `invalid frontmatter line: ${JSON.stringify(trimmed)}` };
|
|
2342
|
+
const key = trimmed.slice(0, colon).trim();
|
|
2343
|
+
const val = trimmed.slice(colon + 1).trim();
|
|
2344
|
+
if (val.startsWith("[") && val.endsWith("]")) {
|
|
2345
|
+
const inner = val.slice(1, -1).trim();
|
|
2346
|
+
meta[key] = inner ? inner.split(",").map((p) => p.trim().replace(/^['"]|['"]$/g, "")) : [];
|
|
2347
|
+
} else {
|
|
2348
|
+
meta[key] = val.replace(/^['"]|['"]$/g, "");
|
|
2349
|
+
}
|
|
2350
|
+
}
|
|
2351
|
+
return { body, meta };
|
|
2352
|
+
}
|
|
2353
|
+
function walkMd(dir) {
|
|
2354
|
+
if (!existsSync2(dir))
|
|
2355
|
+
return [];
|
|
2356
|
+
const results = [];
|
|
2357
|
+
for (const entry of readdirSync(dir)) {
|
|
2358
|
+
const full = path2.join(dir, entry);
|
|
2359
|
+
if (statSync(full).isDirectory()) {
|
|
2360
|
+
results.push(...walkMd(full));
|
|
2361
|
+
} else if (entry.endsWith(".md")) {
|
|
2362
|
+
results.push(full);
|
|
2363
|
+
}
|
|
2364
|
+
}
|
|
2365
|
+
return results.sort();
|
|
2366
|
+
}
|
|
2367
|
+
function allNotes(knowledgeDir) {
|
|
2368
|
+
return walkMd(knowledgeDir).map((p) => {
|
|
2369
|
+
const text = readFileSync(p, "utf8");
|
|
2370
|
+
const result = parseFrontmatter(text);
|
|
2371
|
+
if ("err" in result)
|
|
2372
|
+
return { body: "", err: result.err, meta: {}, path: p };
|
|
2373
|
+
return { ...result, path: p };
|
|
2374
|
+
});
|
|
2375
|
+
}
|
|
2376
|
+
function validNotes(knowledgeDir, excludeTypes = []) {
|
|
2377
|
+
return allNotes(knowledgeDir).filter((n) => !n.err && !excludeTypes.includes(n.meta.type ?? ""));
|
|
2378
|
+
}
|
|
2379
|
+
function excerpt(note, maxChars = 140) {
|
|
2380
|
+
const section = PRIMARY_SECTION[note.meta.type ?? ""];
|
|
2381
|
+
if (!section)
|
|
2382
|
+
return "";
|
|
2383
|
+
const re = new RegExp(`(?:^|\\n)## ${section}[ \\t]*\\n([\\s\\S]*?)(?=\\n## |$)`);
|
|
2384
|
+
const m = note.body.match(re);
|
|
2385
|
+
if (!m || !m[1])
|
|
2386
|
+
return "";
|
|
2387
|
+
for (const para of m[1].split(`
|
|
2388
|
+
|
|
2389
|
+
`)) {
|
|
2390
|
+
const p = para.trim();
|
|
2391
|
+
if (!p || p.startsWith("#") || p.startsWith("-") || p.startsWith("*"))
|
|
2392
|
+
continue;
|
|
2393
|
+
return p.length > maxChars ? p.slice(0, maxChars - 3) + "..." : p;
|
|
2394
|
+
}
|
|
2395
|
+
return "";
|
|
2396
|
+
}
|
|
2397
|
+
function slugify(text) {
|
|
2398
|
+
return text.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
|
|
2399
|
+
}
|
|
2400
|
+
|
|
2401
|
+
// src/commands/new.ts
|
|
2402
|
+
function registerNew(program2) {
|
|
2403
|
+
program2.command("new <type> <title>").description("Create a new knowledge note").option("--tags <tags>", "Comma-separated tags", "").action((type, title, opts) => {
|
|
2404
|
+
if (!TYPE_DIRS[type]) {
|
|
2405
|
+
console.error(`Unknown type: ${type}. Valid: ${Object.keys(TYPE_DIRS).filter((t) => t !== "index").join(", ")}`);
|
|
2406
|
+
process.exit(1);
|
|
2407
|
+
}
|
|
2408
|
+
const knowledgeDir = findKnowledgeDir();
|
|
2409
|
+
const today = new Date().toISOString().slice(0, 10);
|
|
2410
|
+
const slug = slugify(title);
|
|
2411
|
+
const tags = opts.tags.split(",").map((t) => t.trim()).filter(Boolean).join(", ");
|
|
2412
|
+
const content = renderTemplate(type, { date: today, slug, tags, title });
|
|
2413
|
+
const dir = path3.join(knowledgeDir, TYPE_DIRS[type]);
|
|
2414
|
+
mkdirSync(dir, { recursive: true });
|
|
2415
|
+
const outPath = path3.join(dir, `${today}-${slug}.md`);
|
|
2416
|
+
if (existsSync3(outPath)) {
|
|
2417
|
+
console.error(`Already exists: ${outPath}`);
|
|
2418
|
+
process.exit(1);
|
|
2419
|
+
}
|
|
2420
|
+
writeFileSync(outPath, content);
|
|
2421
|
+
console.log(outPath);
|
|
2422
|
+
});
|
|
2423
|
+
}
|
|
2424
|
+
|
|
2425
|
+
// src/commands/search.ts
|
|
2426
|
+
import { readFileSync as readFileSync2 } from "fs";
|
|
2427
|
+
|
|
2428
|
+
// src/lib/db.ts
|
|
2429
|
+
import { Database } from "bun:sqlite";
|
|
2430
|
+
import { existsSync as existsSync4 } from "fs";
|
|
2431
|
+
import path4 from "path";
|
|
2432
|
+
var DB_NAME = ".index.db";
|
|
2433
|
+
function dbPath(knowledgeDir) {
|
|
2434
|
+
return path4.join(knowledgeDir, DB_NAME);
|
|
2435
|
+
}
|
|
2436
|
+
function openDb(knowledgeDir) {
|
|
2437
|
+
const db = new Database(dbPath(knowledgeDir));
|
|
2438
|
+
db.run(`CREATE VIRTUAL TABLE IF NOT EXISTS notes_fts USING fts5(
|
|
2439
|
+
id,
|
|
2440
|
+
path UNINDEXED,
|
|
2441
|
+
type UNINDEXED,
|
|
2442
|
+
status UNINDEXED,
|
|
2443
|
+
title,
|
|
2444
|
+
tags,
|
|
2445
|
+
body,
|
|
2446
|
+
tokenize = 'porter unicode61'
|
|
2447
|
+
)`);
|
|
2448
|
+
return db;
|
|
2449
|
+
}
|
|
2450
|
+
function rebuild(knowledgeDir) {
|
|
2451
|
+
const db = openDb(knowledgeDir);
|
|
2452
|
+
db.run("DELETE FROM notes_fts");
|
|
2453
|
+
const insert = db.prepare("INSERT INTO notes_fts(id,path,type,status,title,tags,body) VALUES(?,?,?,?,?,?,?)");
|
|
2454
|
+
const notes = validNotes(knowledgeDir, ["index"]);
|
|
2455
|
+
for (const n of notes) {
|
|
2456
|
+
insert.run(n.meta.id ?? "", n.path, n.meta.type ?? "", n.meta.status ?? "", n.meta.title ?? "", (n.meta.tags ?? []).join(" "), n.body);
|
|
2457
|
+
}
|
|
2458
|
+
db.close();
|
|
2459
|
+
return notes.length;
|
|
2460
|
+
}
|
|
2461
|
+
function search(knowledgeDir, query, opts = {}) {
|
|
2462
|
+
const { filterStatus, filterTag, filterType, limit } = opts;
|
|
2463
|
+
if (!existsSync4(dbPath(knowledgeDir))) {
|
|
2464
|
+
throw new Error("Search index not found \u2014 run: pk index");
|
|
2465
|
+
}
|
|
2466
|
+
const db = openDb(knowledgeDir);
|
|
2467
|
+
const args = [ftsQuery(query)];
|
|
2468
|
+
let sql = `SELECT path,id,type,status,title,tags,bm25(notes_fts) as score
|
|
2469
|
+
FROM notes_fts WHERE notes_fts MATCH ?`;
|
|
2470
|
+
if (filterType) {
|
|
2471
|
+
sql += " AND type = ?";
|
|
2472
|
+
args.push(filterType);
|
|
2473
|
+
}
|
|
2474
|
+
if (filterStatus) {
|
|
2475
|
+
sql += " AND status = ?";
|
|
2476
|
+
args.push(filterStatus);
|
|
2477
|
+
}
|
|
2478
|
+
sql += " ORDER BY score";
|
|
2479
|
+
if (limit && limit > 0)
|
|
2480
|
+
sql += ` LIMIT ${limit}`;
|
|
2481
|
+
const rows = db.query(sql).all(...args);
|
|
2482
|
+
db.close();
|
|
2483
|
+
return rows.map((r) => ({ ...r, tags: r.tags ? r.tags.split(" ") : [] })).filter((r) => !filterTag || r.tags.includes(filterTag));
|
|
2484
|
+
}
|
|
2485
|
+
function ftsQuery(q) {
|
|
2486
|
+
const terms = q.trim().toLowerCase().split(/\s+/).filter(Boolean);
|
|
2487
|
+
if (terms.length === 0)
|
|
2488
|
+
return '"*"';
|
|
2489
|
+
return terms.map((t) => `"${t.replace(/"/g, '""')}"`).join(" AND ");
|
|
2490
|
+
}
|
|
2491
|
+
|
|
2492
|
+
// src/commands/search.ts
|
|
2493
|
+
function registerSearch(program2) {
|
|
2494
|
+
program2.command("search <query>").description("Search knowledge notes via FTS5 BM25").option("--type <type>", "Filter by note type").option("--status <status>", "Filter by status").option("--tag <tag>", "Filter by tag").option("--limit <n>", "Max results", "10").option("--context", "Include full note body in output").option("--json", "JSON output").action((query, opts) => {
|
|
2495
|
+
const knowledgeDir = findKnowledgeDir();
|
|
2496
|
+
let results;
|
|
2497
|
+
try {
|
|
2498
|
+
results = search(knowledgeDir, query, {
|
|
2499
|
+
filterStatus: opts.status,
|
|
2500
|
+
filterTag: opts.tag,
|
|
2501
|
+
filterType: opts.type,
|
|
2502
|
+
limit: parseInt(opts.limit, 10)
|
|
2503
|
+
});
|
|
2504
|
+
} catch (err) {
|
|
2505
|
+
console.error(String(err));
|
|
2506
|
+
process.exit(1);
|
|
2507
|
+
}
|
|
2508
|
+
if (opts.json) {
|
|
2509
|
+
console.log(JSON.stringify(results, null, 2));
|
|
2510
|
+
return;
|
|
2511
|
+
}
|
|
2512
|
+
if (results.length === 0) {
|
|
2513
|
+
console.log("No results.");
|
|
2514
|
+
return;
|
|
2515
|
+
}
|
|
2516
|
+
for (const r of results) {
|
|
2517
|
+
const tags = r.tags.join(", ");
|
|
2518
|
+
console.log(`${r.path} | ${r.type} | ${r.status} | ${r.id} | ${r.title}${tags ? " | " + tags : ""}`);
|
|
2519
|
+
if (opts.context) {
|
|
2520
|
+
try {
|
|
2521
|
+
console.log(readFileSync2(r.path, "utf8"));
|
|
2522
|
+
console.log("---");
|
|
2523
|
+
} catch {}
|
|
2524
|
+
}
|
|
2525
|
+
}
|
|
2526
|
+
});
|
|
2527
|
+
}
|
|
2528
|
+
|
|
2529
|
+
// src/commands/synthesize.ts
|
|
2530
|
+
function registerSynthesize(program2) {
|
|
2531
|
+
program2.command("synthesize [query]").description("Produce a ranked context dump of matching notes").option("--all", "Include all notes").option("--type <type>", "Filter by note type").option("--tag <tag>", "Filter by tag").option("--limit <n>", "Max notes", "10").option("--session-start", "Open questions + recent decisions + active notes").action((query, opts) => {
|
|
2532
|
+
const knowledgeDir = findKnowledgeDir();
|
|
2533
|
+
const today = new Date().toISOString().slice(0, 10);
|
|
2534
|
+
let notes;
|
|
2535
|
+
if (opts.sessionStart) {
|
|
2536
|
+
const all = validNotes(knowledgeDir, ["index", "source"]);
|
|
2537
|
+
notes = all.filter((n) => n.meta.status === "open" || n.meta.status === "accepted" || n.meta.status === "active").slice(0, parseInt(opts.limit, 10));
|
|
2538
|
+
} else if (opts.all) {
|
|
2539
|
+
notes = validNotes(knowledgeDir, ["index"]).slice(0, parseInt(opts.limit, 10));
|
|
2540
|
+
} else if (query) {
|
|
2541
|
+
let results;
|
|
2542
|
+
try {
|
|
2543
|
+
results = search(knowledgeDir, query, { filterTag: opts.tag, filterType: opts.type, limit: parseInt(opts.limit, 10) });
|
|
2544
|
+
} catch (err) {
|
|
2545
|
+
console.error(String(err));
|
|
2546
|
+
process.exit(1);
|
|
2547
|
+
}
|
|
2548
|
+
const byPath = new Map(allNotes(knowledgeDir).map((n) => [n.path, n]));
|
|
2549
|
+
notes = results.map((r) => byPath.get(r.path)).filter(Boolean);
|
|
2550
|
+
} else {
|
|
2551
|
+
console.error("Provide a query or --all");
|
|
2552
|
+
process.exit(1);
|
|
2553
|
+
}
|
|
2554
|
+
if (!notes || notes.length === 0) {
|
|
2555
|
+
console.log("No matching notes.");
|
|
2556
|
+
return;
|
|
2557
|
+
}
|
|
2558
|
+
const label = opts.sessionStart ? "session context" : query ?? "all";
|
|
2559
|
+
console.log(`# Knowledge: ${label} (${notes.length} notes \xB7 ${today})`);
|
|
2560
|
+
for (const n of notes) {
|
|
2561
|
+
if (!n)
|
|
2562
|
+
continue;
|
|
2563
|
+
const tags = (n.meta.tags ?? []).join(", ");
|
|
2564
|
+
console.log(`
|
|
2565
|
+
---
|
|
2566
|
+
### [${n.meta.title ?? "(untitled)"}] \xB7 ${n.meta.type} \xB7 ${n.meta.status}
|
|
2567
|
+
\`${n.path}\`${tags ? `
|
|
2568
|
+
**tags:** ` + tags : ""}
|
|
2569
|
+
`);
|
|
2570
|
+
const ex = excerpt(n);
|
|
2571
|
+
if (ex)
|
|
2572
|
+
console.log(ex);
|
|
2573
|
+
}
|
|
2574
|
+
});
|
|
2575
|
+
}
|
|
2576
|
+
|
|
2577
|
+
// src/commands/rebuild.ts
|
|
2578
|
+
import { mkdirSync as mkdirSync2, writeFileSync as writeFileSync2 } from "fs";
|
|
2579
|
+
import path5 from "path";
|
|
2580
|
+
function registerRebuild(program2) {
|
|
2581
|
+
program2.command("index").description("Rebuild FTS5 search index and markdown index files").action(() => {
|
|
2582
|
+
const knowledgeDir = findKnowledgeDir();
|
|
2583
|
+
const count = rebuild(knowledgeDir);
|
|
2584
|
+
console.log(`indexed ${count} notes`);
|
|
2585
|
+
const today = new Date().toISOString().slice(0, 10);
|
|
2586
|
+
const notes = validNotes(knowledgeDir, ["index"]);
|
|
2587
|
+
const indexDir = path5.join(knowledgeDir, TYPE_DIRS["index"] ?? "indexes");
|
|
2588
|
+
mkdirSync2(indexDir, { recursive: true });
|
|
2589
|
+
function header(id, title) {
|
|
2590
|
+
return `---
|
|
2591
|
+
id: ${id}
|
|
2592
|
+
type: index
|
|
2593
|
+
title: ${title}
|
|
2594
|
+
created: ${today}
|
|
2595
|
+
updated: ${today}
|
|
2596
|
+
status: active
|
|
2597
|
+
tags: []
|
|
2598
|
+
---
|
|
2599
|
+
|
|
2600
|
+
## Purpose
|
|
2601
|
+
|
|
2602
|
+
Generated index.
|
|
2603
|
+
|
|
2604
|
+
## Key Links
|
|
2605
|
+
|
|
2606
|
+
`;
|
|
2607
|
+
}
|
|
2608
|
+
function link(n) {
|
|
2609
|
+
const ex = excerpt(n);
|
|
2610
|
+
const rel = path5.relative(indexDir, n.path);
|
|
2611
|
+
const base = `- [${n.meta.title ?? n.path}](${rel}) \u2014 \`${n.meta.type}\` \xB7 ${n.meta.status}`;
|
|
2612
|
+
return ex ? base + `
|
|
2613
|
+
` + ex + `
|
|
2614
|
+
` : base + `
|
|
2615
|
+
`;
|
|
2616
|
+
}
|
|
2617
|
+
const footer = (section) => `
|
|
2618
|
+
## Open Questions
|
|
2619
|
+
|
|
2620
|
+
${section}
|
|
2621
|
+
|
|
2622
|
+
## Recent Changes
|
|
2623
|
+
|
|
2624
|
+
Generated by pk index.
|
|
2625
|
+
`;
|
|
2626
|
+
writeFileSync2(path5.join(indexDir, "all.md"), header("index-all", "All Knowledge") + notes.map(link).join("") + footer("See open-questions.md."));
|
|
2627
|
+
const decisions = notes.filter((n) => n.meta.type === "decision");
|
|
2628
|
+
writeFileSync2(path5.join(indexDir, "decisions.md"), header("index-decisions", "Decisions") + decisions.map(link).join("") + footer("N/A."));
|
|
2629
|
+
const openQ = notes.filter((n) => n.meta.type === "question" && n.meta.status === "open");
|
|
2630
|
+
writeFileSync2(path5.join(indexDir, "open-questions.md"), header("index-open-questions", "Open Questions") + openQ.map(link).join("") + footer(openQ.length ? openQ.map((n) => `- ${n.meta.title ?? n.path}`).join(`
|
|
2631
|
+
`) : "None currently open."));
|
|
2632
|
+
const byTag = new Map;
|
|
2633
|
+
for (const n of notes)
|
|
2634
|
+
for (const tag of n.meta.tags ?? []) {
|
|
2635
|
+
if (!byTag.has(tag))
|
|
2636
|
+
byTag.set(tag, []);
|
|
2637
|
+
byTag.get(tag).push(n);
|
|
2638
|
+
}
|
|
2639
|
+
let tagsBody = header("index-tags", "Tags");
|
|
2640
|
+
for (const [tag, tn] of [...byTag.entries()].sort())
|
|
2641
|
+
tagsBody += `### ${tag}
|
|
2642
|
+
|
|
2643
|
+
` + tn.map(link).join("") + `
|
|
2644
|
+
`;
|
|
2645
|
+
writeFileSync2(path5.join(indexDir, "tags.md"), tagsBody + footer("See open-questions.md."));
|
|
2646
|
+
console.log(`wrote indexes to ${indexDir}`);
|
|
2647
|
+
});
|
|
2648
|
+
}
|
|
2649
|
+
|
|
2650
|
+
// src/commands/lint.ts
|
|
2651
|
+
import { readFileSync as readFileSync3 } from "fs";
|
|
2652
|
+
import path6 from "path";
|
|
2653
|
+
function issue(level, message, p) {
|
|
2654
|
+
return { level, message, path: p };
|
|
2655
|
+
}
|
|
2656
|
+
function checkRequiredFields(meta, p) {
|
|
2657
|
+
return ["id", "type", "title", "created", "updated", "status", "tags"].flatMap((f) => meta[f] === undefined || meta[f] === "" ? [issue("error", `missing frontmatter field: ${f}`, p)] : []);
|
|
2658
|
+
}
|
|
2659
|
+
function checkTypeAndLocation(meta, p, knowledgeDir) {
|
|
2660
|
+
const type = meta.type;
|
|
2661
|
+
const out = [];
|
|
2662
|
+
if (!STATUSES[type]?.includes(meta.status ?? "")) {
|
|
2663
|
+
out.push(issue("error", `invalid status '${meta.status}' for type ${type}`, p));
|
|
2664
|
+
}
|
|
2665
|
+
const expected = path6.resolve(knowledgeDir, TYPE_DIRS[type]);
|
|
2666
|
+
if (path6.resolve(path6.dirname(p)) !== expected) {
|
|
2667
|
+
out.push(issue("error", `${type} must live in ${expected}/`, p));
|
|
2668
|
+
}
|
|
2669
|
+
return out;
|
|
2670
|
+
}
|
|
2671
|
+
function checkRequiredSections(body, type, p) {
|
|
2672
|
+
const sections = new Set([...body.matchAll(/^## (.+?)\s*$/gm)].map((m) => m[1]));
|
|
2673
|
+
return (REQUIRED_SECTIONS[type] ?? []).flatMap((req) => sections.has(req) ? [] : [issue("error", `missing section: ## ${req}`, p)]);
|
|
2674
|
+
}
|
|
2675
|
+
function checkTags(tags, p) {
|
|
2676
|
+
if (!Array.isArray(tags))
|
|
2677
|
+
return [issue("error", "tags must be a flat list", p)];
|
|
2678
|
+
if (tags.length === 0)
|
|
2679
|
+
return [issue("warn", "tags is empty", p)];
|
|
2680
|
+
return [];
|
|
2681
|
+
}
|
|
2682
|
+
function checkLength(p, type) {
|
|
2683
|
+
const out = [];
|
|
2684
|
+
const lineCount = readFileSync3(p, "utf8").split(`
|
|
2685
|
+
`).length;
|
|
2686
|
+
const warnT = LENGTH_WARN[type];
|
|
2687
|
+
if (warnT && lineCount > warnT) {
|
|
2688
|
+
out.push(issue("warn", `${lineCount} lines exceeds ${type} threshold (${warnT})`, p));
|
|
2689
|
+
}
|
|
2690
|
+
if (type !== "source" && lineCount > 400) {
|
|
2691
|
+
out.push(issue("error", "non-source note exceeds 400 lines", p));
|
|
2692
|
+
}
|
|
2693
|
+
return out;
|
|
2694
|
+
}
|
|
2695
|
+
function checkSourceExtracted(body, meta, p) {
|
|
2696
|
+
if (meta.type !== "source" || meta.status !== "processed")
|
|
2697
|
+
return [];
|
|
2698
|
+
const m = body.match(/^## Extracted Items\s*\n([\s\S]*?)(?=^##\s|$)/m);
|
|
2699
|
+
if (m && !m[1]?.trim())
|
|
2700
|
+
return [issue("warn", "processed source has empty Extracted Items", p)];
|
|
2701
|
+
return [];
|
|
2702
|
+
}
|
|
2703
|
+
function lintNotes(knowledgeDir) {
|
|
2704
|
+
const notes = allNotes(knowledgeDir);
|
|
2705
|
+
const issues = [];
|
|
2706
|
+
const ids = new Map;
|
|
2707
|
+
for (const n of notes) {
|
|
2708
|
+
const p = n.path;
|
|
2709
|
+
if (n.err) {
|
|
2710
|
+
issues.push(issue("error", `parse error: ${n.err}`, p));
|
|
2711
|
+
continue;
|
|
2712
|
+
}
|
|
2713
|
+
issues.push(...checkRequiredFields(n.meta, p));
|
|
2714
|
+
const type = n.meta.type ?? "";
|
|
2715
|
+
if (!TYPE_DIRS[type]) {
|
|
2716
|
+
issues.push(issue("error", `invalid type: ${type}`, p));
|
|
2717
|
+
continue;
|
|
2718
|
+
}
|
|
2719
|
+
issues.push(...checkTypeAndLocation(n.meta, p, knowledgeDir));
|
|
2720
|
+
const id = n.meta.id ?? "";
|
|
2721
|
+
if (id) {
|
|
2722
|
+
if (ids.has(id))
|
|
2723
|
+
issues.push(issue("error", `duplicate id ${id} (also in ${ids.get(id)})`, p));
|
|
2724
|
+
else
|
|
2725
|
+
ids.set(id, p);
|
|
2726
|
+
}
|
|
2727
|
+
issues.push(...checkRequiredSections(n.body, type, p), ...checkTags(n.meta.tags, p), ...checkLength(p, type), ...checkSourceExtracted(n.body, n.meta, p));
|
|
2728
|
+
}
|
|
2729
|
+
return { issues, noteCount: notes.length };
|
|
2730
|
+
}
|
|
2731
|
+
function registerLint(program2) {
|
|
2732
|
+
program2.command("lint").description("Validate knowledge notes structure and frontmatter").action(() => {
|
|
2733
|
+
const knowledgeDir = findKnowledgeDir();
|
|
2734
|
+
const { issues, noteCount } = lintNotes(knowledgeDir);
|
|
2735
|
+
let hasError = false;
|
|
2736
|
+
for (const { level, path: p, message } of issues) {
|
|
2737
|
+
const prefix = level === "error" ? "ERROR" : "WARN ";
|
|
2738
|
+
const out = level === "error" ? process.stderr : process.stdout;
|
|
2739
|
+
out.write(`${prefix} ${p}: ${message}
|
|
2740
|
+
`);
|
|
2741
|
+
if (level === "error")
|
|
2742
|
+
hasError = true;
|
|
2743
|
+
}
|
|
2744
|
+
if (!hasError) {
|
|
2745
|
+
console.log(`lint passed (${noteCount} files, ${issues.filter((i) => i.level === "warn").length} warnings)`);
|
|
2746
|
+
} else {
|
|
2747
|
+
process.exit(1);
|
|
2748
|
+
}
|
|
2749
|
+
});
|
|
2750
|
+
}
|
|
2751
|
+
|
|
2752
|
+
// src/lib/config.ts
|
|
2753
|
+
import { existsSync as existsSync5, mkdirSync as mkdirSync3, readFileSync as readFileSync4, writeFileSync as writeFileSync3 } from "fs";
|
|
2754
|
+
import path7 from "path";
|
|
2755
|
+
import os from "os";
|
|
2756
|
+
var DEFAULT = { auto_commit: true, embedding: "" };
|
|
2757
|
+
function configPath() {
|
|
2758
|
+
return path7.join(os.homedir(), ".pk", "config.json");
|
|
2759
|
+
}
|
|
2760
|
+
function loadConfig() {
|
|
2761
|
+
const p = configPath();
|
|
2762
|
+
if (!existsSync5(p))
|
|
2763
|
+
return { ...DEFAULT };
|
|
2764
|
+
try {
|
|
2765
|
+
return { ...DEFAULT, ...JSON.parse(readFileSync4(p, "utf8")) };
|
|
2766
|
+
} catch {
|
|
2767
|
+
return { ...DEFAULT };
|
|
2768
|
+
}
|
|
2769
|
+
}
|
|
2770
|
+
function saveConfig(config) {
|
|
2771
|
+
const p = configPath();
|
|
2772
|
+
mkdirSync3(path7.dirname(p), { recursive: true });
|
|
2773
|
+
writeFileSync3(p, JSON.stringify(config, null, 2) + `
|
|
2774
|
+
`);
|
|
2775
|
+
}
|
|
2776
|
+
|
|
2777
|
+
// src/commands/config-cmd.ts
|
|
2778
|
+
function registerConfig(program2) {
|
|
2779
|
+
program2.command("config").description("Show or update pk configuration (~/.pk/config.json)").option("--auto-commit <bool>", "Auto-commit knowledge operations (true/false)").option("--embedding <model>", "Embedding model (empty to disable)").action((opts) => {
|
|
2780
|
+
const config = loadConfig();
|
|
2781
|
+
if (opts.autoCommit !== undefined)
|
|
2782
|
+
config.auto_commit = opts.autoCommit === "true";
|
|
2783
|
+
if (opts.embedding !== undefined)
|
|
2784
|
+
config.embedding = opts.embedding;
|
|
2785
|
+
saveConfig(config);
|
|
2786
|
+
console.log(JSON.stringify(config, null, 2));
|
|
2787
|
+
});
|
|
2788
|
+
}
|
|
2789
|
+
|
|
2790
|
+
// src/commands/init.ts
|
|
2791
|
+
import { cpSync, existsSync as existsSync6, mkdirSync as mkdirSync4, readFileSync as readFileSync5, writeFileSync as writeFileSync4 } from "fs";
|
|
2792
|
+
import path8 from "path";
|
|
2793
|
+
var KNOWLEDGE_DIR2 = "knowledge";
|
|
2794
|
+
var AGENTS_SKILLS_DIR = ".agents/skills/pk";
|
|
2795
|
+
var HARNESS_DESCRIPTIONS = {
|
|
2796
|
+
claude: "Claude Code \u2014 UserPromptSubmit hook",
|
|
2797
|
+
codex: "Codex CLI \u2014 AGENTS.md injection",
|
|
2798
|
+
cursor: "Cursor \u2014 .cursor/rules/pk.mdc",
|
|
2799
|
+
omp: "Oh My Pi \u2014 .omp/extensions/pk.ts",
|
|
2800
|
+
opencode: "OpenCode \u2014 experimental.chat.system.transform plugin",
|
|
2801
|
+
pi: "Pi \u2014 .pi/extensions/pk.ts"
|
|
2802
|
+
};
|
|
2803
|
+
function registerInit(program2) {
|
|
2804
|
+
program2.command("init").description("Initialize knowledge base and install agent hook").option("--harness <harness>", `Agent harness: ${Object.keys(HARNESS_DESCRIPTIONS).join(", ")} (default: claude)`, "claude").action((opts) => {
|
|
2805
|
+
const harness = opts.harness;
|
|
2806
|
+
if (!HARNESS_DESCRIPTIONS[harness]) {
|
|
2807
|
+
console.error(`Unknown harness: ${harness}. Valid: ${Object.keys(HARNESS_DESCRIPTIONS).join(", ")}`);
|
|
2808
|
+
process.exit(1);
|
|
2809
|
+
}
|
|
2810
|
+
for (const dir of Object.values(TYPE_DIRS)) {
|
|
2811
|
+
mkdirSync4(path8.join(KNOWLEDGE_DIR2, dir), { recursive: true });
|
|
2812
|
+
}
|
|
2813
|
+
console.log("created knowledge/ directories");
|
|
2814
|
+
const gi = path8.join(KNOWLEDGE_DIR2, ".gitignore");
|
|
2815
|
+
if (!existsSync6(gi)) {
|
|
2816
|
+
writeFileSync4(gi, `.index.db
|
|
2817
|
+
`);
|
|
2818
|
+
console.log("created knowledge/.gitignore");
|
|
2819
|
+
}
|
|
2820
|
+
installSkill();
|
|
2821
|
+
const harnessFns = {
|
|
2822
|
+
claude: installClaudeHook,
|
|
2823
|
+
codex: installCodexAgentsMd,
|
|
2824
|
+
cursor: installCursorRule,
|
|
2825
|
+
omp: installOmpExtension,
|
|
2826
|
+
opencode: installOpenCodePlugin,
|
|
2827
|
+
pi: installPiExtension
|
|
2828
|
+
};
|
|
2829
|
+
harnessFns[harness]();
|
|
2830
|
+
console.log(`
|
|
2831
|
+
pk init complete (${HARNESS_DESCRIPTIONS[harness]})
|
|
2832
|
+
|
|
2833
|
+
Next steps:
|
|
2834
|
+
pk index rebuild search index after adding notes
|
|
2835
|
+
pk new note ... create your first note
|
|
2836
|
+
|
|
2837
|
+
Customize note templates at:
|
|
2838
|
+
${AGENTS_SKILLS_DIR}/assets/templates/
|
|
2839
|
+
|
|
2840
|
+
To reset templates to defaults, delete ${AGENTS_SKILLS_DIR} and re-run pk init.
|
|
2841
|
+
`);
|
|
2842
|
+
});
|
|
2843
|
+
}
|
|
2844
|
+
function skillSourceDir() {
|
|
2845
|
+
return path8.resolve(import.meta.dir, "..", "skill");
|
|
2846
|
+
}
|
|
2847
|
+
function installSkill() {
|
|
2848
|
+
if (existsSync6(AGENTS_SKILLS_DIR)) {
|
|
2849
|
+
console.log(`skill already present at ${AGENTS_SKILLS_DIR} \u2014 skipping (delete to reset)`);
|
|
2850
|
+
return;
|
|
2851
|
+
}
|
|
2852
|
+
const src = skillSourceDir();
|
|
2853
|
+
if (!existsSync6(src)) {
|
|
2854
|
+
console.log(`skill source not found at ${src} \u2014 skipping`);
|
|
2855
|
+
return;
|
|
2856
|
+
}
|
|
2857
|
+
cpSync(src, AGENTS_SKILLS_DIR, { recursive: true });
|
|
2858
|
+
console.log(`installed skill to ${AGENTS_SKILLS_DIR}`);
|
|
2859
|
+
}
|
|
2860
|
+
function installClaudeHook() {
|
|
2861
|
+
mkdirSync4(path8.join(".claude", "hooks"), { recursive: true });
|
|
2862
|
+
const hookFile = path8.join(".claude", "hooks", "pk-user-prompt-submit.ts");
|
|
2863
|
+
writeFileSync4(hookFile, CLAUDE_HOOK);
|
|
2864
|
+
console.log(`wrote ${hookFile}`);
|
|
2865
|
+
const settingsFile = path8.join(".claude", "settings.json");
|
|
2866
|
+
let settings = {};
|
|
2867
|
+
if (existsSync6(settingsFile)) {
|
|
2868
|
+
try {
|
|
2869
|
+
settings = JSON.parse(readFileSync5(settingsFile, "utf8"));
|
|
2870
|
+
} catch {}
|
|
2871
|
+
}
|
|
2872
|
+
const hooks = settings["hooks"] ?? {};
|
|
2873
|
+
const existing = hooks["UserPromptSubmit"] ?? [];
|
|
2874
|
+
const cmd = `bun ${hookFile} user-prompt-submit`;
|
|
2875
|
+
if (!existing.includes(cmd))
|
|
2876
|
+
hooks["UserPromptSubmit"] = [...existing, cmd];
|
|
2877
|
+
settings["hooks"] = hooks;
|
|
2878
|
+
writeFileSync4(settingsFile, JSON.stringify(settings, null, 2) + `
|
|
2879
|
+
`);
|
|
2880
|
+
console.log("updated .claude/settings.json");
|
|
2881
|
+
}
|
|
2882
|
+
var CLAUDE_HOOK = `// pk hook \u2014 auto-generated by pk init
|
|
2883
|
+
async function runPk(args: string[]): Promise<string> {
|
|
2884
|
+
const child = Bun.spawn(['pk', ...args], { stdout: 'pipe', stderr: 'pipe' })
|
|
2885
|
+
const [out] = await Promise.all([new Response(child.stdout).text(), child.exited])
|
|
2886
|
+
return out.trim()
|
|
2887
|
+
}
|
|
2888
|
+
|
|
2889
|
+
async function handleUserPromptSubmit() {
|
|
2890
|
+
const input = JSON.parse(await Bun.stdin.text()) as { prompt?: string }
|
|
2891
|
+
const query = input.prompt?.trim() ?? ''
|
|
2892
|
+
let context = ''
|
|
2893
|
+
try {
|
|
2894
|
+
const args = ['synthesize', '--session-start', '--limit', '8']
|
|
2895
|
+
if (query) args.push(query)
|
|
2896
|
+
context = await runPk(args)
|
|
2897
|
+
} catch {}
|
|
2898
|
+
console.log(JSON.stringify({
|
|
2899
|
+
hookSpecificOutput: { hookEventName: 'UserPromptSubmit', additionalContext: context },
|
|
2900
|
+
suppressOutput: true,
|
|
2901
|
+
}))
|
|
2902
|
+
}
|
|
2903
|
+
|
|
2904
|
+
const event = process.argv[2] ?? ''
|
|
2905
|
+
if (event === 'user-prompt-submit') handleUserPromptSubmit().catch(() => process.exit(0))
|
|
2906
|
+
else process.exit(0)
|
|
2907
|
+
`;
|
|
2908
|
+
function installPiExtension() {
|
|
2909
|
+
const dir = path8.join(".pi", "extensions");
|
|
2910
|
+
mkdirSync4(dir, { recursive: true });
|
|
2911
|
+
const file = path8.join(dir, "pk.ts");
|
|
2912
|
+
writeFileSync4(file, PI_EXTENSION);
|
|
2913
|
+
console.log(`wrote ${file}`);
|
|
2914
|
+
}
|
|
2915
|
+
var PI_EXTENSION = `// pk extension \u2014 auto-generated by pk init
|
|
2916
|
+
import type { ExtensionAPI } from '@mariozechner/pi-coding-agent'
|
|
2917
|
+
|
|
2918
|
+
async function runPk(args: string[]): Promise<string> {
|
|
2919
|
+
const child = Bun.spawn(['pk', ...args], { stdout: 'pipe', stderr: 'pipe' })
|
|
2920
|
+
const [out] = await Promise.all([new Response(child.stdout).text(), child.exited])
|
|
2921
|
+
return out.trim()
|
|
2922
|
+
}
|
|
2923
|
+
|
|
2924
|
+
export default function (pi: ExtensionAPI) {
|
|
2925
|
+
pi.on('before_agent_start', async (event) => {
|
|
2926
|
+
let context = ''
|
|
2927
|
+
try {
|
|
2928
|
+
context = await runPk(['synthesize', '--session-start', '--limit', '8'])
|
|
2929
|
+
} catch {}
|
|
2930
|
+
if (!context) return
|
|
2931
|
+
return {
|
|
2932
|
+
systemPrompt: \`\${context}\\n\\n\${event.systemPrompt ?? ''}\`,
|
|
2933
|
+
}
|
|
2934
|
+
})
|
|
2935
|
+
}
|
|
2936
|
+
`;
|
|
2937
|
+
function installOmpExtension() {
|
|
2938
|
+
const dir = path8.join(".omp", "extensions");
|
|
2939
|
+
mkdirSync4(dir, { recursive: true });
|
|
2940
|
+
const file = path8.join(dir, "pk.ts");
|
|
2941
|
+
writeFileSync4(file, OMP_EXTENSION);
|
|
2942
|
+
console.log(`wrote ${file}`);
|
|
2943
|
+
}
|
|
2944
|
+
var OMP_EXTENSION = `// pk hook auto-generated by pk init
|
|
2945
|
+
import type { HookAPI } from '@oh-my-pi/pi-coding-agent/extensibility/hooks'
|
|
2946
|
+
|
|
2947
|
+
async function runPk(args: string[]): Promise<string> {
|
|
2948
|
+
const child = Bun.spawn(['pk', ...args], { stdout: 'pipe', stderr: 'pipe' })
|
|
2949
|
+
const [out] = await Promise.all([new Response(child.stdout).text(), child.exited])
|
|
2950
|
+
return out.trim()
|
|
2951
|
+
}
|
|
2952
|
+
|
|
2953
|
+
export default function (pi: HookAPI) {
|
|
2954
|
+
pi.on('before_agent_start', async (event) => {
|
|
2955
|
+
let context = ''
|
|
2956
|
+
try {
|
|
2957
|
+
context = await runPk(['synthesize', '--session-start', '--limit', '8'])
|
|
2958
|
+
} catch {}
|
|
2959
|
+
if (!context) return
|
|
2960
|
+
return {
|
|
2961
|
+
message: {
|
|
2962
|
+
customType: 'pk-context',
|
|
2963
|
+
content: context,
|
|
2964
|
+
display: 'pk: project context injected',
|
|
2965
|
+
details: context,
|
|
2966
|
+
},
|
|
2967
|
+
}
|
|
2968
|
+
})
|
|
2969
|
+
}
|
|
2970
|
+
`;
|
|
2971
|
+
function installOpenCodePlugin() {
|
|
2972
|
+
const dir = path8.join(".opencode", "plugins");
|
|
2973
|
+
mkdirSync4(dir, { recursive: true });
|
|
2974
|
+
const file = path8.join(dir, "pk.ts");
|
|
2975
|
+
writeFileSync4(file, OPENCODE_PLUGIN);
|
|
2976
|
+
console.log(`wrote ${file}`);
|
|
2977
|
+
console.log(`Register in .opencode/config.json: { "plugins": [".opencode/plugins/pk.ts"] }`);
|
|
2978
|
+
}
|
|
2979
|
+
var OPENCODE_PLUGIN = `// pk plugin \u2014 auto-generated by pk init
|
|
2980
|
+
import type { Plugin } from '@opencode-ai/plugin'
|
|
2981
|
+
|
|
2982
|
+
async function runPk(args: string[]): Promise<string> {
|
|
2983
|
+
const child = Bun.spawn(['pk', ...args], { stdout: 'pipe', stderr: 'pipe' })
|
|
2984
|
+
const [out] = await Promise.all([new Response(child.stdout).text(), child.exited])
|
|
2985
|
+
return out.trim()
|
|
2986
|
+
}
|
|
2987
|
+
|
|
2988
|
+
export const PkPlugin: Plugin = async () => {
|
|
2989
|
+
return {
|
|
2990
|
+
'experimental.chat.system.transform': async (
|
|
2991
|
+
_input: { sessionID?: string },
|
|
2992
|
+
output: { system: string[] },
|
|
2993
|
+
) => {
|
|
2994
|
+
try {
|
|
2995
|
+
const context = await runPk(['synthesize', '--session-start', '--limit', '8'])
|
|
2996
|
+
if (context) output.system.push(\`\\n\${context}\`)
|
|
2997
|
+
} catch {}
|
|
2998
|
+
},
|
|
2999
|
+
}
|
|
3000
|
+
}
|
|
3001
|
+
`;
|
|
3002
|
+
function installCursorRule() {
|
|
3003
|
+
const dir = path8.join(".cursor", "rules");
|
|
3004
|
+
mkdirSync4(dir, { recursive: true });
|
|
3005
|
+
const file = path8.join(dir, "pk.mdc");
|
|
3006
|
+
writeFileSync4(file, CURSOR_RULE);
|
|
3007
|
+
console.log(`wrote ${file}`);
|
|
3008
|
+
}
|
|
3009
|
+
var CURSOR_RULE = `---
|
|
3010
|
+
description: Project knowledge (pk)
|
|
3011
|
+
globs: ["**/*"]
|
|
3012
|
+
alwaysApply: true
|
|
3013
|
+
---
|
|
3014
|
+
|
|
3015
|
+
# Project Knowledge (pk)
|
|
3016
|
+
|
|
3017
|
+
This project uses \`pk\` for structured knowledge management.
|
|
3018
|
+
|
|
3019
|
+
## Before creating any note, search first
|
|
3020
|
+
|
|
3021
|
+
\`\`\`bash
|
|
3022
|
+
pk search <query>
|
|
3023
|
+
\`\`\`
|
|
3024
|
+
|
|
3025
|
+
## Commands
|
|
3026
|
+
|
|
3027
|
+
\`\`\`bash
|
|
3028
|
+
pk new note|decision|question|source "Title" --tags tag1,tag2
|
|
3029
|
+
pk search <query> [--context] [--limit 5]
|
|
3030
|
+
pk synthesize --session-start # current project context
|
|
3031
|
+
pk index # rebuild search after adding notes
|
|
3032
|
+
pk lint # validate note structure
|
|
3033
|
+
\`\`\`
|
|
3034
|
+
|
|
3035
|
+
## Notes live in \`knowledge/\`
|
|
3036
|
+
|
|
3037
|
+
Templates can be customized at \`.agents/skills/pk/assets/templates/\`.
|
|
3038
|
+
`;
|
|
3039
|
+
function installCodexAgentsMd() {
|
|
3040
|
+
const file = "AGENTS.md";
|
|
3041
|
+
const block = `
|
|
3042
|
+
## Project Knowledge (pk)
|
|
3043
|
+
|
|
3044
|
+
This project uses \`pk\` for structured knowledge management.
|
|
3045
|
+
|
|
3046
|
+
At the start of each session run:
|
|
3047
|
+
\`\`\`bash
|
|
3048
|
+
pk synthesize --session-start
|
|
3049
|
+
\`\`\`
|
|
3050
|
+
|
|
3051
|
+
Always run \`pk search <query>\` before creating new notes.
|
|
3052
|
+
|
|
3053
|
+
\`\`\`bash
|
|
3054
|
+
pk new note|decision|question|source "Title" --tags tag1,tag2
|
|
3055
|
+
pk search <query> [--context]
|
|
3056
|
+
pk index # rebuild after adding notes
|
|
3057
|
+
pk lint # validate structure
|
|
3058
|
+
\`\`\`
|
|
3059
|
+
|
|
3060
|
+
Notes live in \`knowledge/\`. Templates at \`.agents/skills/pk/assets/templates/\`.
|
|
3061
|
+
`;
|
|
3062
|
+
const existing = existsSync6(file) ? readFileSync5(file, "utf8") : "";
|
|
3063
|
+
if (existing.includes("pk synthesize --session-start")) {
|
|
3064
|
+
console.log(`${file} already contains pk block \u2014 skipping`);
|
|
3065
|
+
return;
|
|
3066
|
+
}
|
|
3067
|
+
writeFileSync4(file, existing + block);
|
|
3068
|
+
console.log(`appended pk block to ${file}`);
|
|
3069
|
+
}
|
|
3070
|
+
|
|
3071
|
+
// src/commands/instructions.ts
|
|
3072
|
+
var GUIDES = {
|
|
3073
|
+
new: `pk new note|decision|question|source "Title" --tags tag1,tag2
|
|
3074
|
+
Search first: pk search <query>. Creates knowledge/<type>s/YYYY-MM-DD-slug.md.`,
|
|
3075
|
+
search: `pk search <query> [--type] [--status] [--tag] [--context] [--limit]
|
|
3076
|
+
BM25 ranked FTS5. Requires pk index to have been run first.`,
|
|
3077
|
+
synthesize: `pk synthesize [query] [--all] [--session-start] [--limit]
|
|
3078
|
+
Produces ranked context block. --session-start injects open questions + accepted decisions + active notes.`,
|
|
3079
|
+
index: `pk index
|
|
3080
|
+
Rebuilds FTS5 index (knowledge/.index.db) and markdown index files (knowledge/indexes/). Run after any note change.`,
|
|
3081
|
+
lint: `pk lint
|
|
3082
|
+
Validates frontmatter, required sections, file location, ID uniqueness, tag format, length limits.`,
|
|
3083
|
+
init: `pk init [--harness claude]
|
|
3084
|
+
Creates knowledge/ dirs, installs Claude Code hook at .claude/hooks/pk-user-prompt-submit.ts, updates .claude/settings.json.`,
|
|
3085
|
+
config: `pk config [--auto-commit bool] [--embedding model]
|
|
3086
|
+
Shows or updates pk configuration.`
|
|
3087
|
+
};
|
|
3088
|
+
function registerInstructions(program2) {
|
|
3089
|
+
program2.command("instructions <command>").description("Show behavioral guide for a command").action((cmd) => {
|
|
3090
|
+
const guide = GUIDES[cmd];
|
|
3091
|
+
if (!guide) {
|
|
3092
|
+
console.error(`No guide for '${cmd}'. Available: ${Object.keys(GUIDES).join(", ")}`);
|
|
3093
|
+
process.exit(1);
|
|
3094
|
+
}
|
|
3095
|
+
console.log(guide);
|
|
3096
|
+
});
|
|
3097
|
+
}
|
|
3098
|
+
|
|
3099
|
+
// src/index.ts
|
|
3100
|
+
var program2 = new Command().name("pk").description("Project knowledge \u2014 structured intake, search, and recall").version("0.1.0");
|
|
3101
|
+
registerNew(program2);
|
|
3102
|
+
registerSearch(program2);
|
|
3103
|
+
registerSynthesize(program2);
|
|
3104
|
+
registerRebuild(program2);
|
|
3105
|
+
registerLint(program2);
|
|
3106
|
+
registerConfig(program2);
|
|
3107
|
+
registerInit(program2);
|
|
3108
|
+
registerInstructions(program2);
|
|
3109
|
+
program2.parse();
|