@kaynooo/cli-wt 0.1.1 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -10
- package/dist/cli-0gbs87r4.js +313 -0
- package/dist/cli-1nyesy1b.js +674 -0
- package/dist/cli-4g5ehhgn.js +251 -0
- package/dist/cli-6ybxw5sc.js +97 -0
- package/dist/cli-dmkfxdbr.js +153 -0
- package/dist/cli-dmzvmpge.js +106 -0
- package/dist/cli-e2h20nyy.js +36 -0
- package/dist/cli-j82fak6j.js +69 -0
- package/dist/cli-k4471y24.js +779 -0
- package/dist/cli-kek450vq.js +519 -0
- package/dist/cli-n5rt07v7.js +105 -0
- package/dist/cli-pkvw2k4x.js +37 -0
- package/dist/cli-w4w36hng.js +67 -0
- package/dist/cli.js +238 -2576
- package/dist/commands-jtnbchtb.js +222 -0
- package/dist/init-4ynjrztr.js +166 -0
- package/dist/list-js6drtvg.js +88 -0
- package/dist/print-q6qaqg22.js +77 -0
- package/dist/prompt-y71tp3f5.js +436 -0
- package/dist/prune-beweag7w.js +17 -0
- package/dist/run-7eayrqwy.js +18 -0
- package/dist/shell-xrgvqrpp.js +55 -0
- package/dist/stack-h3pgyx1e.js +20 -0
- package/dist/tools-4j56r47a.js +13 -0
- package/package.json +6 -6
- package/src/cli.ts +270 -76
- package/src/commands.ts +58 -42
- package/src/compose.ts +37 -14
- package/src/config.ts +8 -1
- package/src/errors.ts +31 -5
- package/src/files.ts +15 -23
- package/src/git.ts +53 -7
- package/src/index.ts +3 -2
- package/src/init.ts +56 -10
- package/src/jobs.ts +60 -19
- package/src/list.ts +26 -13
- package/src/print.ts +76 -0
- package/src/project.ts +134 -23
- package/src/proxy.ts +9 -9
- package/src/prune.ts +149 -0
- package/src/queue.ts +27 -3
- package/src/run.ts +82 -23
- package/src/shell.ts +34 -7
- package/src/stack.ts +41 -17
- package/src/sync.ts +28 -3
- package/src/tools.ts +91 -0
|
@@ -0,0 +1,674 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
outputMode,
|
|
4
|
+
xdgFolder,
|
|
5
|
+
takesControlCodes,
|
|
6
|
+
paint,
|
|
7
|
+
formatDuration,
|
|
8
|
+
usageLine,
|
|
9
|
+
wantsHelpJson,
|
|
10
|
+
description,
|
|
11
|
+
exampleRows,
|
|
12
|
+
optionRows,
|
|
13
|
+
closest,
|
|
14
|
+
Exit,
|
|
15
|
+
fail,
|
|
16
|
+
UsageError,
|
|
17
|
+
HelpShown,
|
|
18
|
+
Completing,
|
|
19
|
+
completion,
|
|
20
|
+
OUTPUT_OPTIONS,
|
|
21
|
+
applyOutput
|
|
22
|
+
} from "./cli-k4471y24.js";
|
|
23
|
+
// ../kit/src/argv.ts
|
|
24
|
+
import { readFileSync, realpathSync } from "fs";
|
|
25
|
+
import { resolve } from "path";
|
|
26
|
+
import process2 from "process";
|
|
27
|
+
function commandLine() {
|
|
28
|
+
try {
|
|
29
|
+
return readFileSync("/proc/self/cmdline", "utf8").split("\x00").slice(0, -1);
|
|
30
|
+
} catch {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function realPath(word) {
|
|
35
|
+
try {
|
|
36
|
+
return realpathSync(resolve(word));
|
|
37
|
+
} catch {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function scriptArgs(argv = process2.argv.slice(2), cmdline = commandLine(), script = process2.argv[1]) {
|
|
42
|
+
const target = script === undefined ? undefined : realPath(script) ?? script;
|
|
43
|
+
const at = cmdline === undefined || target === undefined ? -1 : cmdline.findIndex((word, index) => index > 0 && (resolve(word) === script || realPath(word) === target));
|
|
44
|
+
const after = at === -1 ? [] : cmdline.slice(at + 1);
|
|
45
|
+
return after[0] === "--" && after.length === argv.length + 1 && argv.every((word, index) => word === after[index + 1]) ? after : argv;
|
|
46
|
+
}
|
|
47
|
+
// ../kit/src/process.ts
|
|
48
|
+
import { readFileSync as readFileSync2 } from "fs";
|
|
49
|
+
import { resolve as resolve2 } from "path";
|
|
50
|
+
import process3 from "process";
|
|
51
|
+
var SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
52
|
+
var GROUP_SIGNALS = new Set(["SIGINT", "SIGHUP"]);
|
|
53
|
+
var SCHEME = /^[a-z][\w+.-]+:/i;
|
|
54
|
+
var OPENERS = { darwin: ["open"], win32: ["cmd", "/c", "start", ""] };
|
|
55
|
+
function groupsOf(pid) {
|
|
56
|
+
try {
|
|
57
|
+
const stat = readFileSync2(`/proc/${pid}/stat`, "utf8");
|
|
58
|
+
const fields = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
|
|
59
|
+
return { group: Number(fields[2]), foreground: Number(fields[5]) };
|
|
60
|
+
} catch {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function heardTheTerminal(child) {
|
|
65
|
+
if (child.pid === undefined)
|
|
66
|
+
return false;
|
|
67
|
+
const own = groupsOf("self");
|
|
68
|
+
return own !== undefined && own.group === own.foreground && groupsOf(child.pid)?.group === own.group;
|
|
69
|
+
}
|
|
70
|
+
function holdSignals(cleanup) {
|
|
71
|
+
let received;
|
|
72
|
+
let child;
|
|
73
|
+
const held = {
|
|
74
|
+
get signal() {
|
|
75
|
+
return received;
|
|
76
|
+
},
|
|
77
|
+
forward: (to) => {
|
|
78
|
+
child = to;
|
|
79
|
+
},
|
|
80
|
+
release: () => {
|
|
81
|
+
for (const signal of SIGNALS)
|
|
82
|
+
process3.off(signal, onSignal);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
function onSignal(signal) {
|
|
86
|
+
received = signal;
|
|
87
|
+
if (child !== undefined) {
|
|
88
|
+
if (!GROUP_SIGNALS.has(signal) || !heardTheTerminal(child))
|
|
89
|
+
child.kill(signal);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (cleanup === undefined)
|
|
93
|
+
return;
|
|
94
|
+
held.release();
|
|
95
|
+
cleanup(signal);
|
|
96
|
+
process3.kill(process3.pid, signal);
|
|
97
|
+
}
|
|
98
|
+
for (const signal of SIGNALS)
|
|
99
|
+
process3.on(signal, onSignal);
|
|
100
|
+
return held;
|
|
101
|
+
}
|
|
102
|
+
function openUrl(target, env = process3.env, platform = process3.platform) {
|
|
103
|
+
const opener = env.BROWSER === undefined || env.BROWSER === "" ? OPENERS[platform] ?? ["xdg-open"] : [env.BROWSER];
|
|
104
|
+
try {
|
|
105
|
+
Bun.spawn([...opener, SCHEME.test(target) ? target : resolve2(target)], { env, stdio: ["ignore", "ignore", "ignore"] }).unref();
|
|
106
|
+
return true;
|
|
107
|
+
} catch {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ../kit/src/progress.ts
|
|
113
|
+
var ESC = String.fromCharCode(27);
|
|
114
|
+
var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x7E]|\\][^\\u0007${ESC}]*(?:\\u0007|${ESC}\\\\))`, "y");
|
|
115
|
+
var RESET_STYLE = `${ESC}[0m`;
|
|
116
|
+
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
117
|
+
// ../kit/src/cli.ts
|
|
118
|
+
import { readFileSync as readFileSync3, realpathSync as realpathSync2 } from "fs";
|
|
119
|
+
import { dirname as dirname2, join as join2 } from "path";
|
|
120
|
+
import process7 from "process";
|
|
121
|
+
|
|
122
|
+
// ../kit/src/askpass.ts
|
|
123
|
+
import process4 from "process";
|
|
124
|
+
var SOCKET_VARIABLE = "KAYNOOO_ASKPASS_SOCKET";
|
|
125
|
+
async function askpassClient(argv) {
|
|
126
|
+
const socketPath = process4.env[SOCKET_VARIABLE];
|
|
127
|
+
if (socketPath === undefined)
|
|
128
|
+
return;
|
|
129
|
+
const request = { prompt: argv[0] ?? "", ...process4.env.SSH_ASKPASS_PROMPT === "none" ? { notice: true } : {} };
|
|
130
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
131
|
+
let received = "";
|
|
132
|
+
try {
|
|
133
|
+
await Bun.connect({
|
|
134
|
+
unix: socketPath,
|
|
135
|
+
socket: {
|
|
136
|
+
open: (socket) => void socket.write(`${JSON.stringify(request)}
|
|
137
|
+
`),
|
|
138
|
+
data: (_, chunk) => {
|
|
139
|
+
received += chunk.toString();
|
|
140
|
+
},
|
|
141
|
+
close: () => resolve(received),
|
|
142
|
+
error: () => resolve("")
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
} catch {
|
|
146
|
+
return 1;
|
|
147
|
+
}
|
|
148
|
+
const reply = await promise;
|
|
149
|
+
if (reply === "")
|
|
150
|
+
return 1;
|
|
151
|
+
const { answer } = JSON.parse(reply);
|
|
152
|
+
if (request.notice === true)
|
|
153
|
+
return 0;
|
|
154
|
+
if (answer === undefined)
|
|
155
|
+
return 1;
|
|
156
|
+
process4.stdout.write(`${answer}
|
|
157
|
+
`);
|
|
158
|
+
return 0;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// ../kit/src/completions.ts
|
|
162
|
+
var COMPLETION_SHELLS = ["fish", "bash", "zsh"];
|
|
163
|
+
var FISH_FUNCTION = `functions -q __kaynooo_cli_complete; or function __kaynooo_cli_complete --argument-names bin --description 'what a @kaynooo/cli-kit bin offers for the word being typed'
|
|
164
|
+
set -l tokens (commandline -xpc 2>/dev/null; or commandline -opc)
|
|
165
|
+
set -l current (commandline -ct)
|
|
166
|
+
for line in ($bin __complete $tokens[2..-1] $current 2>/dev/null)
|
|
167
|
+
if test "$line" = __files__
|
|
168
|
+
__fish_complete_path $current
|
|
169
|
+
else
|
|
170
|
+
echo $line
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
end`;
|
|
174
|
+
var BASH_FUNCTION = `__kaynooo_cli_complete() {
|
|
175
|
+
local IFS=$'\\n' cur=\${COMP_WORDS[COMP_CWORD]} line words=
|
|
176
|
+
local -a lines
|
|
177
|
+
COMPREPLY=()
|
|
178
|
+
lines=($("\${COMP_WORDS[0]}" __complete "\${COMP_WORDS[@]:1:COMP_CWORD}" 2>/dev/null))
|
|
179
|
+
for line in "\${lines[@]}"; do
|
|
180
|
+
if [[ $line == __files__ ]]; then
|
|
181
|
+
COMPREPLY+=($(compgen -f -- "$cur"))
|
|
182
|
+
else
|
|
183
|
+
words+="\${line%%$'\\t'*}"$'\\n'
|
|
184
|
+
fi
|
|
185
|
+
done
|
|
186
|
+
COMPREPLY+=($(compgen -W "$words" -- "$cur"))
|
|
187
|
+
}`;
|
|
188
|
+
var ZSH_FUNCTION = `__kaynooo_cli_complete() {
|
|
189
|
+
local line
|
|
190
|
+
local -a described plain
|
|
191
|
+
for line in \${(f)"$("\${words[1]}" __complete "\${(@)words[2,CURRENT]}" 2>/dev/null)"}; do
|
|
192
|
+
if [[ $line == __files__ ]]; then
|
|
193
|
+
_files
|
|
194
|
+
elif [[ $line == *$'\\t'* ]]; then
|
|
195
|
+
described+=("\${\${line%%$'\\t'*}//:/\\\\:}:\${line#*$'\\t'}")
|
|
196
|
+
else
|
|
197
|
+
plain+=("$line")
|
|
198
|
+
fi
|
|
199
|
+
done
|
|
200
|
+
(( \${#described} )) && _describe -V value described
|
|
201
|
+
(( \${#plain} )) && compadd -- "\${plain[@]}"
|
|
202
|
+
return 0
|
|
203
|
+
}`;
|
|
204
|
+
function completionScript(shell, bins, header = `# Generated by \`${bins[0]} --completions ${shell}\``) {
|
|
205
|
+
if (shell === "fish")
|
|
206
|
+
return `${header}
|
|
207
|
+
${FISH_FUNCTION}
|
|
208
|
+
${bins.map((bin) => `complete -c ${bin} -f -a '(__kaynooo_cli_complete ${bin})'
|
|
209
|
+
`).join("")}`;
|
|
210
|
+
if (shell === "bash")
|
|
211
|
+
return `${header}
|
|
212
|
+
${BASH_FUNCTION}
|
|
213
|
+
complete -F __kaynooo_cli_complete ${bins.join(" ")}
|
|
214
|
+
`;
|
|
215
|
+
return `${header}
|
|
216
|
+
${ZSH_FUNCTION}
|
|
217
|
+
compdef __kaynooo_cli_complete ${bins.join(" ")}
|
|
218
|
+
`;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// ../kit/src/notify.ts
|
|
222
|
+
import process5 from "process";
|
|
223
|
+
var NOTIFY_AFTER_MS = 1e4;
|
|
224
|
+
var CANCELLED = 130;
|
|
225
|
+
function base64(text) {
|
|
226
|
+
return Buffer.from(text).toString("base64");
|
|
227
|
+
}
|
|
228
|
+
function notification(title, body, env = process5.env) {
|
|
229
|
+
const id = `kit${process5.pid}`;
|
|
230
|
+
const codes = [`\x1B]99;i=${id}:d=0:o=unfocused:e=1;${base64(title)}\x1B\\`, `\x1B]99;i=${id}:p=body:e=1;${base64(body)}\x1B\\`];
|
|
231
|
+
return codes.map((code) => env.TMUX === undefined || env.TMUX === "" ? code : `\x1BPtmux;${code.replaceAll("\x1B", "\x1B\x1B")}\x1B\\`).join("");
|
|
232
|
+
}
|
|
233
|
+
function notifyEnd(command, code, ms, { stream = process5.stderr, env = process5.env } = {}) {
|
|
234
|
+
if (ms < NOTIFY_AFTER_MS || code === CANCELLED || outputMode(stream, env) !== "terminal")
|
|
235
|
+
return;
|
|
236
|
+
stream.write(notification(command, code === 0 ? `done in ${formatDuration(ms)}` : `failed with exit ${code} after ${formatDuration(ms)}`, env));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// ../kit/src/usage.ts
|
|
240
|
+
import { appendFileSync, mkdirSync } from "fs";
|
|
241
|
+
import { homedir } from "os";
|
|
242
|
+
import { dirname, join } from "path";
|
|
243
|
+
import process6 from "process";
|
|
244
|
+
|
|
245
|
+
// ../kit/src/logs.ts
|
|
246
|
+
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
247
|
+
var CLI_FOLDER = "kaynooo-cli";
|
|
248
|
+
|
|
249
|
+
// ../kit/src/usage.ts
|
|
250
|
+
var UNKNOWN_OPTION = /unknown option '(--[a-z][a-z0-9-]*)[=']/;
|
|
251
|
+
var FLAG_NAME = /^(?:--[a-z][a-z0-9-]*|-[a-zA-Z])$/;
|
|
252
|
+
var LONGEST_NAME = 40;
|
|
253
|
+
var usageError;
|
|
254
|
+
function noteUsageError(message) {
|
|
255
|
+
usageError = message;
|
|
256
|
+
}
|
|
257
|
+
function stateHome(env = process6.env) {
|
|
258
|
+
return xdgFolder(env.XDG_STATE_HOME, join(homedir(), ".local", "state"));
|
|
259
|
+
}
|
|
260
|
+
function usagePath(env = process6.env) {
|
|
261
|
+
return join(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
262
|
+
}
|
|
263
|
+
function usageRecorded(env = process6.env) {
|
|
264
|
+
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
265
|
+
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
266
|
+
}
|
|
267
|
+
function flagNames(argv) {
|
|
268
|
+
const end = argv.indexOf("--");
|
|
269
|
+
const names = (end === -1 ? argv : argv.slice(0, end)).filter((word) => word.startsWith("-") && word !== "-").map((word) => word.includes("=") ? word.slice(0, word.indexOf("=")) : word);
|
|
270
|
+
return [...new Set(names)].filter((name) => name.length <= LONGEST_NAME && FLAG_NAME.test(name));
|
|
271
|
+
}
|
|
272
|
+
function usageEntry(bin, command, argv, code, { ms, now = new Date } = {}) {
|
|
273
|
+
const unknown = code === 2 && usageError !== undefined ? UNKNOWN_OPTION.exec(usageError)?.[1] : undefined;
|
|
274
|
+
usageError = undefined;
|
|
275
|
+
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown }, ...ms === undefined ? {} : { ms: Math.round(ms) } };
|
|
276
|
+
}
|
|
277
|
+
function recordUsage(entry, env = process6.env) {
|
|
278
|
+
if (!usageRecorded(env))
|
|
279
|
+
return;
|
|
280
|
+
try {
|
|
281
|
+
const path = usagePath(env);
|
|
282
|
+
mkdirSync(dirname(path), { recursive: true, mode: 448 });
|
|
283
|
+
appendFileSync(path, `${JSON.stringify(entry)}
|
|
284
|
+
`, { mode: 384 });
|
|
285
|
+
} catch {}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// ../kit/src/cli.ts
|
|
289
|
+
var LEVEL_OPTIONS = {
|
|
290
|
+
...OUTPUT_OPTIONS,
|
|
291
|
+
help: { type: "boolean", short: "h", description: "show this help" }
|
|
292
|
+
};
|
|
293
|
+
var TOP_OPTIONS = {
|
|
294
|
+
...LEVEL_OPTIONS,
|
|
295
|
+
version: { type: "boolean", short: "v", description: "print the version" },
|
|
296
|
+
completions: { type: "string", choices: COMPLETION_SHELLS, description: "print the shell completion script" }
|
|
297
|
+
};
|
|
298
|
+
async function commandRows(all, colors) {
|
|
299
|
+
const commands = shown(all);
|
|
300
|
+
const width = Math.max(0, ...Object.keys(commands).map((name) => name.length));
|
|
301
|
+
const groups = new Map([["Commands:", []]]);
|
|
302
|
+
const summaries = await Promise.all(Object.values(commands).map(async ({ summary }) => resolveSummary(summary)));
|
|
303
|
+
for (const [index, [name, { group }]] of Object.entries(commands).entries()) {
|
|
304
|
+
const heading = group === undefined ? "Commands:" : `${group}:`;
|
|
305
|
+
const rows = groups.get(heading) ?? [];
|
|
306
|
+
rows.push(` ${colors.cyan(name.padEnd(width))} ${colors.dim(summaries[index])}`);
|
|
307
|
+
groups.set(heading, rows);
|
|
308
|
+
}
|
|
309
|
+
return [...groups].filter(([, rows]) => rows.length > 0).flatMap(([heading, rows], index) => [...index === 0 ? [] : [""], colors.bold(heading), ...rows]);
|
|
310
|
+
}
|
|
311
|
+
function shown(commands) {
|
|
312
|
+
return Object.fromEntries(Object.entries(commands).filter(([, { hidden }]) => hidden !== true));
|
|
313
|
+
}
|
|
314
|
+
function takeOutputFlags(argv) {
|
|
315
|
+
let index = 0;
|
|
316
|
+
for (;index < argv.length; index++) {
|
|
317
|
+
const word = argv[index];
|
|
318
|
+
if (word === "--plain")
|
|
319
|
+
applyOutput(undefined, true);
|
|
320
|
+
else if (word.startsWith("--color="))
|
|
321
|
+
applyOutput(word.slice("--color=".length), undefined);
|
|
322
|
+
else if (word === "--color" && argv[index + 1] !== undefined)
|
|
323
|
+
applyOutput(argv[++index], undefined);
|
|
324
|
+
else
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
return argv.slice(index);
|
|
328
|
+
}
|
|
329
|
+
async function resolveSummary(summary) {
|
|
330
|
+
return typeof summary === "string" ? summary : summary();
|
|
331
|
+
}
|
|
332
|
+
function lookup({ commands, aliases }, word) {
|
|
333
|
+
const name = aliases !== undefined && Object.hasOwn(aliases, word) ? aliases[word] ?? word : word;
|
|
334
|
+
return Object.hasOwn(commands, name) ? commands[name] : undefined;
|
|
335
|
+
}
|
|
336
|
+
function scopeOf(commands, extra, aliases) {
|
|
337
|
+
return { commands: typeof commands === "function" ? {} : commands, lazy: [...typeof commands === "function" ? [commands] : [], ...extra === undefined ? [] : [extra]], aliases };
|
|
338
|
+
}
|
|
339
|
+
async function expand(scope, words) {
|
|
340
|
+
if (scope.lazy.length === 0)
|
|
341
|
+
return scope;
|
|
342
|
+
const commands = { ...scope.commands };
|
|
343
|
+
for (const added of await Promise.all(scope.lazy.map(async (lazy) => lazy(words)))) {
|
|
344
|
+
for (const [name, command] of Object.entries(added))
|
|
345
|
+
commands[name] ??= command;
|
|
346
|
+
}
|
|
347
|
+
return { ...scope, commands, lazy: [] };
|
|
348
|
+
}
|
|
349
|
+
async function help({ path, header, summary: lazySummary, commands, defaultCommand, examples, replaces }, stream) {
|
|
350
|
+
const colors = paint(stream);
|
|
351
|
+
const prefix = path.join(" ");
|
|
352
|
+
const summary = await resolveSummary(lazySummary);
|
|
353
|
+
if (wantsHelpJson())
|
|
354
|
+
return JSON.stringify(description(`${prefix} <command> [options]: ${summary}`, { examples, replaces }));
|
|
355
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
356
|
+
const options = fallback !== undefined && "run" in fallback ? await optionsOf(fallback.run) : undefined;
|
|
357
|
+
const own = Object.entries(options ?? {}).filter(([name]) => !(name in TOP_OPTIONS));
|
|
358
|
+
return [
|
|
359
|
+
header(colors, summary),
|
|
360
|
+
"",
|
|
361
|
+
usageLine(`${prefix} ${defaultCommand === undefined ? colors.cyan("<command>") : `[${colors.cyan("<command>")}]`} [options]`, colors),
|
|
362
|
+
"",
|
|
363
|
+
...await commandRows(commands, colors),
|
|
364
|
+
...own.length === 0 ? [] : ["", `${colors.bold(`Options of ${defaultCommand}:`)} ${colors.dim("the command run when none is named")}`, ...optionRows(Object.fromEntries(own), colors)],
|
|
365
|
+
"",
|
|
366
|
+
colors.bold("Options:"),
|
|
367
|
+
...optionRows(path.length === 1 ? TOP_OPTIONS : LEVEL_OPTIONS, colors),
|
|
368
|
+
...exampleRows(examples ?? [], colors),
|
|
369
|
+
"",
|
|
370
|
+
`${colors.dim("Run")} ${prefix} ${colors.cyan("<command>")} --help ${colors.dim("for its options.")}`
|
|
371
|
+
].join(`
|
|
372
|
+
`);
|
|
373
|
+
}
|
|
374
|
+
function printError(prefix, message, hint, cause) {
|
|
375
|
+
const colors = paint(process7.stderr);
|
|
376
|
+
const styled = takesControlCodes(process7.stderr);
|
|
377
|
+
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
378
|
+
const under = (label, text, tint) => text === undefined ? "" : `
|
|
379
|
+
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
380
|
+
console.error(`${head} ${message}${under("cause", cause, colors.dim)}${under("hint", hint, colors.yellow)}`);
|
|
381
|
+
}
|
|
382
|
+
function printJsonError(message, hint, cause) {
|
|
383
|
+
console.error(JSON.stringify({ error: message, ...hint === undefined ? {} : { hint }, ...cause === undefined ? {} : { cause } }));
|
|
384
|
+
}
|
|
385
|
+
function wantsJson(argv) {
|
|
386
|
+
const end = argv.indexOf("--");
|
|
387
|
+
return (end === -1 ? argv : argv.slice(0, end)).includes("--json");
|
|
388
|
+
}
|
|
389
|
+
function reportThrown(prefix, error, redact = (text) => text, json = false) {
|
|
390
|
+
if (error instanceof HelpShown)
|
|
391
|
+
return 0;
|
|
392
|
+
const help = error instanceof UsageError ? `run ${prefix} --help for its options` : undefined;
|
|
393
|
+
const named = error instanceof Error && !(error instanceof Exit) && error.name !== "Error" ? `${error.name}: ${error.message}` : undefined;
|
|
394
|
+
const cause = error instanceof Error && error.cause instanceof Error ? redact(error.cause.message) : undefined;
|
|
395
|
+
if (error instanceof Exit && error.code === 2)
|
|
396
|
+
noteUsageError(error.message);
|
|
397
|
+
const report = json ? printJsonError : (message, hint, causeText) => printError(prefix, message, hint, causeText);
|
|
398
|
+
report(redact(named ?? (error instanceof Error ? error.message : String(error))), error instanceof Exit ? error.hint ?? help : undefined, cause);
|
|
399
|
+
return error instanceof Exit ? error.code : 1;
|
|
400
|
+
}
|
|
401
|
+
async function dispatch(level, typed, { version, redact }) {
|
|
402
|
+
const prefix = level.path.join(" ");
|
|
403
|
+
let argv;
|
|
404
|
+
try {
|
|
405
|
+
argv = takeOutputFlags(typed);
|
|
406
|
+
} catch (error) {
|
|
407
|
+
return reportThrown(prefix, error);
|
|
408
|
+
}
|
|
409
|
+
const [name, ...args] = argv;
|
|
410
|
+
const fallback = level.defaultCommand === undefined ? undefined : level.commands[level.defaultCommand];
|
|
411
|
+
if (name === undefined && level.runBare && fallback !== undefined && "run" in fallback) {
|
|
412
|
+
try {
|
|
413
|
+
return await fallback.run([]);
|
|
414
|
+
} catch (error) {
|
|
415
|
+
return reportThrown(`${prefix} ${level.defaultCommand}`, error, redact, false);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
if (name === undefined && level.path.length > 1) {
|
|
419
|
+
console.error(await help(await expand(level, [...level.path.slice(1), ...argv]), process7.stderr));
|
|
420
|
+
return 2;
|
|
421
|
+
}
|
|
422
|
+
if (name === undefined || name === "help" || name === "--help" || name === "-h") {
|
|
423
|
+
console.log(await help(await expand(level, [...level.path.slice(1), ...argv]), process7.stdout));
|
|
424
|
+
return 0;
|
|
425
|
+
}
|
|
426
|
+
if (level.path.length === 1 && (name === "--version" || name === "-v")) {
|
|
427
|
+
console.log(version);
|
|
428
|
+
return 0;
|
|
429
|
+
}
|
|
430
|
+
let command = lookup(level, name);
|
|
431
|
+
let rest = args;
|
|
432
|
+
let ran = name;
|
|
433
|
+
if (command === undefined && level.lazy.length > 0) {
|
|
434
|
+
level = await expand(level, [...level.path.slice(1), ...argv]);
|
|
435
|
+
command = lookup(level, name);
|
|
436
|
+
}
|
|
437
|
+
if (command === undefined && level.defaultCommand !== undefined) {
|
|
438
|
+
command = level.commands[level.defaultCommand];
|
|
439
|
+
rest = argv;
|
|
440
|
+
ran = level.defaultCommand;
|
|
441
|
+
}
|
|
442
|
+
if (command === undefined) {
|
|
443
|
+
const meant = closest(name, Object.keys(shown(level.commands)));
|
|
444
|
+
printError(prefix, `no command ${name}`, meant === undefined ? `run ${prefix} --help for its commands` : `did you mean ${meant}?`);
|
|
445
|
+
return 2;
|
|
446
|
+
}
|
|
447
|
+
if ("commands" in command) {
|
|
448
|
+
const { summary } = command;
|
|
449
|
+
return dispatch({ path: [...level.path, name], header: (colors, text) => `${colors.bold(`${prefix} ${name}`)} ${text}`, summary, ...scopeOf(command.commands, command.extraCommands, command.aliases), defaultCommand: undefined, runBare: false, examples: command.examples, replaces: undefined }, rest, { version, redact });
|
|
450
|
+
}
|
|
451
|
+
try {
|
|
452
|
+
return await command.run(rest);
|
|
453
|
+
} catch (error) {
|
|
454
|
+
return reportThrown(`${prefix} ${ran}`, error, redact, wantsJson(rest));
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
var COMPLETE = "__complete";
|
|
458
|
+
var FILES = "__files__";
|
|
459
|
+
async function completingOf(run) {
|
|
460
|
+
completion.active = true;
|
|
461
|
+
try {
|
|
462
|
+
await run([]);
|
|
463
|
+
} catch (error) {
|
|
464
|
+
if (error instanceof Completing)
|
|
465
|
+
return error;
|
|
466
|
+
} finally {
|
|
467
|
+
completion.active = false;
|
|
468
|
+
}
|
|
469
|
+
return new Completing({});
|
|
470
|
+
}
|
|
471
|
+
async function optionsOf(run) {
|
|
472
|
+
return (await completingOf(run)).options;
|
|
473
|
+
}
|
|
474
|
+
function candidateLine(candidate) {
|
|
475
|
+
return typeof candidate === "string" ? candidate : candidate.join("\t");
|
|
476
|
+
}
|
|
477
|
+
function positionalBeingTyped(before, { options, positionals }) {
|
|
478
|
+
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
479
|
+
const typed = [];
|
|
480
|
+
let at = 0;
|
|
481
|
+
while (at < before.length) {
|
|
482
|
+
const word = before[at];
|
|
483
|
+
if (word === "--") {
|
|
484
|
+
typed.push(...before.slice(at + 1));
|
|
485
|
+
break;
|
|
486
|
+
}
|
|
487
|
+
if (!word.startsWith("-"))
|
|
488
|
+
typed.push(word);
|
|
489
|
+
else if (takesValue(word))
|
|
490
|
+
at++;
|
|
491
|
+
at++;
|
|
492
|
+
}
|
|
493
|
+
const last = positionals.at(-1);
|
|
494
|
+
if (typed.length < positionals.length)
|
|
495
|
+
return { name: positionals[typed.length], typed };
|
|
496
|
+
return { name: last?.replace("]", "").endsWith("...") === true ? last : undefined, typed };
|
|
497
|
+
}
|
|
498
|
+
async function leafCandidates(run, words) {
|
|
499
|
+
const completing = await completingOf(run);
|
|
500
|
+
if ((words.at(-1) ?? "").startsWith("-"))
|
|
501
|
+
return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
|
|
502
|
+
const flag = words.at(-2);
|
|
503
|
+
const option = flag?.startsWith("-") === true ? Object.entries(completing.options).find(([name, { short }]) => flag === `--${name}` || flag === `-${short}`)?.[1] : undefined;
|
|
504
|
+
const { name, typed } = positionalBeingTyped(words.slice(0, -1), completing);
|
|
505
|
+
if (option?.type === "string") {
|
|
506
|
+
const values = option.choices === undefined ? await option.candidates?.(typed) : option.choices;
|
|
507
|
+
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
508
|
+
}
|
|
509
|
+
const values = name === undefined ? undefined : await completing.candidates[name]?.(typed);
|
|
510
|
+
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
511
|
+
}
|
|
512
|
+
async function candidates(scope, defaultCommand, words, before = []) {
|
|
513
|
+
const [word = "", ...rest] = words;
|
|
514
|
+
const listing = rest.length === 0 && !word.startsWith("-");
|
|
515
|
+
let { commands } = scope;
|
|
516
|
+
let command = lookup(scope, word);
|
|
517
|
+
if (listing || command === undefined) {
|
|
518
|
+
const expanded = await expand(scope, [...before, ...words]);
|
|
519
|
+
commands = expanded.commands;
|
|
520
|
+
command = lookup(expanded, word);
|
|
521
|
+
}
|
|
522
|
+
if (listing) {
|
|
523
|
+
const own = await Promise.all(Object.entries(shown(commands)).map(async ([name, { summary }]) => `${name} ${await resolveSummary(summary)}`));
|
|
524
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
525
|
+
const values = fallback !== undefined && "run" in fallback ? await leafCandidates(fallback.run, [word]) : [];
|
|
526
|
+
const valueOf = (line) => line.slice(0, line.includes("\t") ? line.indexOf("\t") : undefined);
|
|
527
|
+
return [...own, ...values.filter((line) => line !== FILES && !Object.hasOwn(commands, valueOf(line)))];
|
|
528
|
+
}
|
|
529
|
+
if (command === undefined) {
|
|
530
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
531
|
+
return fallback !== undefined && "run" in fallback ? leafCandidates(fallback.run, words) : [];
|
|
532
|
+
}
|
|
533
|
+
return "commands" in command ? candidates(scopeOf(command.commands, command.extraCommands, command.aliases), undefined, rest, [...before, word]) : leafCandidates(command.run, rest);
|
|
534
|
+
}
|
|
535
|
+
var COMPLETIONS = "--completions";
|
|
536
|
+
function flagValue([flag = "", ...rest]) {
|
|
537
|
+
return flag.includes("=") ? [flag.slice(flag.indexOf("=") + 1), ...rest] : rest;
|
|
538
|
+
}
|
|
539
|
+
function printCompletionScript(name, args) {
|
|
540
|
+
const shell = COMPLETION_SHELLS.find((each) => each === args[0]);
|
|
541
|
+
if (shell === undefined || args.length !== 1) {
|
|
542
|
+
printError(name, `${COMPLETIONS} takes one of ${COMPLETION_SHELLS.join(", ")}`, `${name} ${COMPLETIONS} fish`);
|
|
543
|
+
return 2;
|
|
544
|
+
}
|
|
545
|
+
console.log(completionScript(shell, [name]).trimEnd());
|
|
546
|
+
return 0;
|
|
547
|
+
}
|
|
548
|
+
async function printCandidates(found) {
|
|
549
|
+
for (const line of await found)
|
|
550
|
+
console.log(line);
|
|
551
|
+
return 0;
|
|
552
|
+
}
|
|
553
|
+
function packageVersion(entry = Bun.main) {
|
|
554
|
+
let resolved = entry;
|
|
555
|
+
try {
|
|
556
|
+
resolved = realpathSync2(entry);
|
|
557
|
+
} catch {}
|
|
558
|
+
for (let dir = dirname2(resolved);; dir = dirname2(dir)) {
|
|
559
|
+
try {
|
|
560
|
+
const { version } = JSON.parse(readFileSync3(join2(dir, "package.json"), "utf8"));
|
|
561
|
+
if (typeof version === "string")
|
|
562
|
+
return version;
|
|
563
|
+
} catch {}
|
|
564
|
+
if (dirname2(dir) === dir)
|
|
565
|
+
return "0.0.0";
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
async function runCli(cli, argv = scriptArgs()) {
|
|
569
|
+
const version = cli.version ?? packageVersion();
|
|
570
|
+
const answered = await askpassClient(argv);
|
|
571
|
+
if (answered !== undefined)
|
|
572
|
+
return answered;
|
|
573
|
+
if (argv[0] === COMPLETE)
|
|
574
|
+
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
575
|
+
if (argv[0] === COMPLETIONS || argv[0]?.startsWith(`${COMPLETIONS}=`) === true)
|
|
576
|
+
return printCompletionScript(cli.name, flagValue(argv));
|
|
577
|
+
const started = performance.now();
|
|
578
|
+
const code = await dispatch({ path: [cli.name], header: (colors, about) => `${colors.bold(cli.name)} ${colors.dim(version)} ${about}`, summary: cli.about, ...scopeOf(cli.commands, cli.extraCommands, cli.aliases), defaultCommand: cli.defaultCommand, runBare: cli.runBare === true, examples: cli.examples, replaces: cli.replaces }, argv, { version, redact: cli.redact });
|
|
579
|
+
const command = commandPath(scopeOf(cli.commands, cli.extraCommands, cli.aliases), argv);
|
|
580
|
+
notifyEnd(command === undefined ? cli.name : `${cli.name} ${command}`, code, performance.now() - started);
|
|
581
|
+
if (cli.usage !== false) {
|
|
582
|
+
try {
|
|
583
|
+
recordUsage(usageEntry(cli.name, command, argv, code, { ms: performance.now() - started }));
|
|
584
|
+
} catch {}
|
|
585
|
+
}
|
|
586
|
+
return code;
|
|
587
|
+
}
|
|
588
|
+
function commandPath(scope, argv) {
|
|
589
|
+
const path = [];
|
|
590
|
+
let level = scope;
|
|
591
|
+
for (const word of argv.filter((each) => !each.startsWith("-"))) {
|
|
592
|
+
const command = lookup(level, word);
|
|
593
|
+
if (command === undefined)
|
|
594
|
+
break;
|
|
595
|
+
path.push(word);
|
|
596
|
+
if (!("commands" in command))
|
|
597
|
+
break;
|
|
598
|
+
level = scopeOf(command.commands, command.extraCommands, command.aliases);
|
|
599
|
+
}
|
|
600
|
+
return path.length === 0 ? undefined : path.join(" ");
|
|
601
|
+
}
|
|
602
|
+
// ../kit/src/compose.ts
|
|
603
|
+
async function captureOutput(argv, { cwd, env } = {}) {
|
|
604
|
+
const run = Bun.spawn(argv, { ...cwd === undefined ? {} : { cwd }, env: { ...process.env, ...env }, stdin: "ignore", stdout: "pipe", stderr: "pipe" });
|
|
605
|
+
const [stdout, stderr, code] = await Promise.all([new Response(run.stdout).text(), new Response(run.stderr).text(), run.exited]);
|
|
606
|
+
return { code, stdout, stderr };
|
|
607
|
+
}
|
|
608
|
+
async function compose(args, { flags = [], ...options } = {}) {
|
|
609
|
+
return captureOutput(["docker", "compose", ...flags, ...args], options);
|
|
610
|
+
}
|
|
611
|
+
// ../kit/src/port.ts
|
|
612
|
+
function isListening(port) {
|
|
613
|
+
try {
|
|
614
|
+
Bun.listen({ hostname: "0.0.0.0", port, socket: { data: () => {
|
|
615
|
+
return;
|
|
616
|
+
} } }).stop(true);
|
|
617
|
+
return false;
|
|
618
|
+
} catch (error) {
|
|
619
|
+
return error.code === "EADDRINUSE";
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
// ../kit/src/secrets.ts
|
|
623
|
+
var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "session", "jwt", "ssn", "iban", "card", "cvv", "otp"]);
|
|
624
|
+
var NOT_PASSWORDS = new Set(["compass", "encompass", "bypass", "overpass", "underpass", "surpass", "trespass"]);
|
|
625
|
+
// ../kit/src/sessions.ts
|
|
626
|
+
var {Glob } = globalThis.Bun;
|
|
627
|
+
var SESSION_FILES = new Glob("*.json");
|
|
628
|
+
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
629
|
+
// src/errors.ts
|
|
630
|
+
import { readFileSync as readFileSync4 } from "fs";
|
|
631
|
+
var {$ } = globalThis.Bun;
|
|
632
|
+
|
|
633
|
+
class KnownError extends Error {
|
|
634
|
+
hint;
|
|
635
|
+
constructor(message, hint) {
|
|
636
|
+
super(message);
|
|
637
|
+
this.hint = hint;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
var BOOT_TIME_LINE = /^btime (\d+)$/m;
|
|
641
|
+
var bootedAt;
|
|
642
|
+
function bootTime() {
|
|
643
|
+
bootedAt ??= Number(readFileSync4("/proc/stat", "utf8").match(BOOT_TIME_LINE)?.[1] ?? 0) * 1000;
|
|
644
|
+
return bootedAt;
|
|
645
|
+
}
|
|
646
|
+
function alive(pid) {
|
|
647
|
+
try {
|
|
648
|
+
process.kill(pid, 0);
|
|
649
|
+
return true;
|
|
650
|
+
} catch (error) {
|
|
651
|
+
return error.code === "EPERM";
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
function knownMessage(error) {
|
|
655
|
+
if (error instanceof KnownError)
|
|
656
|
+
return error.message;
|
|
657
|
+
return error instanceof $.ShellError ? error.stderr.toString().trim() || error.message : undefined;
|
|
658
|
+
}
|
|
659
|
+
function knownHint(error) {
|
|
660
|
+
return error instanceof KnownError ? error.hint : undefined;
|
|
661
|
+
}
|
|
662
|
+
function failureText(error) {
|
|
663
|
+
const hint = knownHint(error);
|
|
664
|
+
return `${knownMessage(error) ?? (error instanceof Error ? error.message : String(error))}${hint === undefined ? "" : `
|
|
665
|
+
${hint}`}`;
|
|
666
|
+
}
|
|
667
|
+
function failKnown(error) {
|
|
668
|
+
const message = knownMessage(error);
|
|
669
|
+
if (message !== undefined)
|
|
670
|
+
fail(message, 1, knownHint(error));
|
|
671
|
+
throw error;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
export { holdSignals, openUrl, completionScript, printError, runCli, compose, isListening, KnownError, bootTime, alive, knownMessage, knownHint, failureText, failKnown };
|