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