@kaynooo/cli-wait 0.10.1 → 0.12.1
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 +3 -1
- package/dist/cli.js +336 -110
- package/dist/slot-cli.js +206 -72
- package/package.json +2 -2
package/dist/slot-cli.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/slot-cli.ts
|
|
5
|
-
import
|
|
5
|
+
import process21 from "process";
|
|
6
6
|
// ../kit/src/args.ts
|
|
7
7
|
import process9 from "process";
|
|
8
8
|
import { parseArgs } from "util";
|
|
@@ -468,24 +468,20 @@ function printJson(value) {
|
|
|
468
468
|
|
|
469
469
|
// ../kit/src/help.ts
|
|
470
470
|
import process8 from "process";
|
|
471
|
-
var OPTIONAL =
|
|
471
|
+
var OPTIONAL = /(?<!\x1B)\[[^\]]+\]/g;
|
|
472
472
|
var USAGE_PREFIX = /^usage:\s*/i;
|
|
473
473
|
var FULL_STOP = /\.$/;
|
|
474
474
|
function usageLine(usage, colors = paint(process8.stdout)) {
|
|
475
475
|
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
476
476
|
}
|
|
477
|
-
function helpText(usage, options, colors = paint(process8.stdout), { examples = [],
|
|
478
|
-
return [usageLine(usage, colors),
|
|
477
|
+
function helpText(usage, options, colors = paint(process8.stdout), { examples = [], exits = [] } = {}) {
|
|
478
|
+
return [usageLine(usage, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
|
|
479
479
|
`);
|
|
480
480
|
}
|
|
481
481
|
function exitRows(exits, colors = paint(process8.stdout)) {
|
|
482
482
|
if (exits.length === 0)
|
|
483
483
|
return [];
|
|
484
|
-
|
|
485
|
-
return ["", colors.bold("Exits:"), ...exits.map(([code, meaning]) => ` ${colors.cyan(String(code).padEnd(width))} ${meaning}`)];
|
|
486
|
-
}
|
|
487
|
-
function replacesRows(replaces, colors = paint(process8.stdout)) {
|
|
488
|
-
return replaces === undefined || outputMode() === "agent" ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
484
|
+
return ["", `${colors.bold("Exit codes:")} ${exits.map(([code, meaning]) => `${colors.cyan(String(code))} ${meaning}`).join(", ")}`];
|
|
489
485
|
}
|
|
490
486
|
function wantsHelpJson(env = process8.env) {
|
|
491
487
|
return env.KIT_HELP === "json";
|
|
@@ -511,29 +507,40 @@ function exampleRows(examples, colors = paint(process8.stdout)) {
|
|
|
511
507
|
return [];
|
|
512
508
|
return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
|
|
513
509
|
}
|
|
510
|
+
var MAX_INLINE_CHOICES = 24;
|
|
514
511
|
function optionRows(options, colors = paint(process8.stdout)) {
|
|
515
512
|
const rows = Object.entries(options).map(([name, option]) => {
|
|
516
513
|
const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
|
|
517
|
-
const
|
|
514
|
+
const choices = option.choices?.join("|");
|
|
515
|
+
const inline = choices !== undefined && choices.length <= MAX_INLINE_CHOICES;
|
|
516
|
+
const named = inline ? `<${choices}>` : "<value>";
|
|
518
517
|
const placeholder = option.optional === undefined ? named : `[${named}]`;
|
|
519
518
|
const value = option.type === "string" ? ` ${colors.dim(placeholder)}` : "";
|
|
520
|
-
|
|
519
|
+
const described = choices === undefined || inline ? option.description ?? "" : [option.description, `one of ${option.choices.join(", ")}`].filter(Boolean).join("; ");
|
|
520
|
+
return { flag: `${short}${colors.cyan(`--${name}`)}${value}`, width: `-x, --${name}`.length + (option.type === "string" ? placeholder.length + 1 : 0), description: described };
|
|
521
521
|
});
|
|
522
522
|
const width = Math.max(0, ...rows.map((row) => row.width));
|
|
523
523
|
return rows.map((row) => row.description === "" ? ` ${row.flag}` : ` ${row.flag}${" ".repeat(width - row.width)} ${colors.dim(row.description)}`);
|
|
524
524
|
}
|
|
525
525
|
|
|
526
526
|
// ../kit/src/text.ts
|
|
527
|
-
var ANSI = new RegExp(`${String.fromCharCode(27)}\\[[\\d;?]*[a-z]`, "gi");
|
|
528
527
|
function distance(a, b) {
|
|
529
|
-
|
|
530
|
-
|
|
528
|
+
const left = [...a];
|
|
529
|
+
const right = [...b];
|
|
530
|
+
let before = [];
|
|
531
|
+
let previous = Array.from({ length: right.length + 1 }, (_, index) => index);
|
|
532
|
+
for (const [i, x] of left.entries()) {
|
|
531
533
|
const current = [i + 1];
|
|
532
|
-
for (const [j,
|
|
533
|
-
|
|
534
|
+
for (const [j, y] of right.entries()) {
|
|
535
|
+
let cost = Math.min((previous[j + 1] ?? 0) + 1, (current[j] ?? 0) + 1, (previous[j] ?? 0) + (x === y ? 0 : 1));
|
|
536
|
+
if (i > 0 && j > 0 && x === right[j - 1] && left[i - 1] === y)
|
|
537
|
+
cost = Math.min(cost, (before[j - 1] ?? 0) + 1);
|
|
538
|
+
current.push(cost);
|
|
539
|
+
}
|
|
540
|
+
before = previous;
|
|
534
541
|
previous = current;
|
|
535
542
|
}
|
|
536
|
-
return previous[
|
|
543
|
+
return previous[right.length] ?? 0;
|
|
537
544
|
}
|
|
538
545
|
function closest(name, names) {
|
|
539
546
|
const started = names.find((known) => known.startsWith(name));
|
|
@@ -609,7 +616,7 @@ class Completing {
|
|
|
609
616
|
}
|
|
610
617
|
var completion = { active: false };
|
|
611
618
|
var OUTPUT_OPTIONS = {
|
|
612
|
-
color: { type: "string", description: "auto (on a terminal alone
|
|
619
|
+
color: { type: "string", choices: ["auto", "always", "never"], description: "auto (the default) colours on a terminal alone" },
|
|
613
620
|
plain: { type: "boolean", description: "plain text, no colour or spinner: --color never" }
|
|
614
621
|
};
|
|
615
622
|
function applyOutput(color, plain, env = process9.env) {
|
|
@@ -781,12 +788,36 @@ function nearestOption(message, options, allowNegative) {
|
|
|
781
788
|
const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
|
|
782
789
|
return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
|
|
783
790
|
}
|
|
791
|
+
// ../kit/src/argv.ts
|
|
792
|
+
import { readFileSync as readFileSync3, realpathSync } from "fs";
|
|
793
|
+
import { resolve as resolve3 } from "path";
|
|
794
|
+
import process10 from "process";
|
|
795
|
+
function commandLine() {
|
|
796
|
+
try {
|
|
797
|
+
return readFileSync3("/proc/self/cmdline", "utf8").split("\x00").slice(0, -1);
|
|
798
|
+
} catch {
|
|
799
|
+
return;
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
function realPath(word) {
|
|
803
|
+
try {
|
|
804
|
+
return realpathSync(resolve3(word));
|
|
805
|
+
} catch {
|
|
806
|
+
return;
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
function scriptArgs(argv = process10.argv.slice(2), cmdline = commandLine(), script = process10.argv[1]) {
|
|
810
|
+
const target = script === undefined ? undefined : realPath(script) ?? script;
|
|
811
|
+
const at = cmdline === undefined || target === undefined ? -1 : cmdline.findIndex((word, index) => index > 0 && (resolve3(word) === script || realPath(word) === target));
|
|
812
|
+
const after = at === -1 ? [] : cmdline.slice(at + 1);
|
|
813
|
+
return after[0] === "--" && after.length === argv.length + 1 && argv.every((word, index) => word === after[index + 1]) ? after : argv;
|
|
814
|
+
}
|
|
784
815
|
// ../kit/src/progress.ts
|
|
785
|
-
import
|
|
816
|
+
import process12 from "process";
|
|
786
817
|
|
|
787
818
|
// ../kit/src/process.ts
|
|
788
819
|
import { constants } from "os";
|
|
789
|
-
import
|
|
820
|
+
import process11 from "process";
|
|
790
821
|
var KILL_GRACE_MS = 2000;
|
|
791
822
|
var SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
792
823
|
function exitCodeOf(code, signal) {
|
|
@@ -808,7 +839,7 @@ function holdSignals(cleanup) {
|
|
|
808
839
|
},
|
|
809
840
|
release: () => {
|
|
810
841
|
for (const signal of SIGNALS)
|
|
811
|
-
|
|
842
|
+
process11.off(signal, onSignal);
|
|
812
843
|
}
|
|
813
844
|
};
|
|
814
845
|
function onSignal(signal) {
|
|
@@ -821,10 +852,10 @@ function holdSignals(cleanup) {
|
|
|
821
852
|
return;
|
|
822
853
|
held.release();
|
|
823
854
|
cleanup(signal);
|
|
824
|
-
|
|
855
|
+
process11.kill(process11.pid, signal);
|
|
825
856
|
}
|
|
826
857
|
for (const signal of SIGNALS)
|
|
827
|
-
|
|
858
|
+
process11.on(signal, onSignal);
|
|
828
859
|
return held;
|
|
829
860
|
}
|
|
830
861
|
async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal, maxBytes = Infinity, onChunk, onStart } = {}) {
|
|
@@ -838,7 +869,7 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal,
|
|
|
838
869
|
return;
|
|
839
870
|
try {
|
|
840
871
|
if (grouped)
|
|
841
|
-
|
|
872
|
+
process11.kill(-proc.pid, killing);
|
|
842
873
|
else
|
|
843
874
|
proc.kill(killing);
|
|
844
875
|
} catch {}
|
|
@@ -916,7 +947,7 @@ var RESET_STYLE = `${ESC}[0m`;
|
|
|
916
947
|
var CTRL_C = 3;
|
|
917
948
|
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
918
949
|
var CONSOLE_METHODS = ["log", "info", "warn", "error", "debug", "dir", "dirxml", "table", "trace", "group", "groupCollapsed", "count", "timeEnd", "timeLog", "assert"];
|
|
919
|
-
function liveRegion(render, stream =
|
|
950
|
+
function liveRegion(render, stream = process12.stderr, env = process12.env) {
|
|
920
951
|
if (!takesControlCodes(stream, env))
|
|
921
952
|
return { redraw: () => {
|
|
922
953
|
return;
|
|
@@ -963,11 +994,11 @@ function liveRegion(render, stream = process11.stderr, env = process11.env) {
|
|
|
963
994
|
clear();
|
|
964
995
|
restore();
|
|
965
996
|
release();
|
|
966
|
-
|
|
997
|
+
process12.off("exit", onExit);
|
|
967
998
|
signals.release();
|
|
968
999
|
}
|
|
969
1000
|
timer.unref();
|
|
970
|
-
|
|
1001
|
+
process12.on("exit", onExit);
|
|
971
1002
|
return {
|
|
972
1003
|
redraw: draw,
|
|
973
1004
|
print: (text) => {
|
|
@@ -1006,14 +1037,14 @@ function cutToWidth(line, width) {
|
|
|
1006
1037
|
return `${kept}${RESET_STYLE}${linked ? CLOSE_LINK : ""}`;
|
|
1007
1038
|
}
|
|
1008
1039
|
function swallowInput() {
|
|
1009
|
-
const input =
|
|
1040
|
+
const input = process12.stdin;
|
|
1010
1041
|
if (!input.isTTY)
|
|
1011
1042
|
return () => {
|
|
1012
1043
|
return;
|
|
1013
1044
|
};
|
|
1014
1045
|
const onData = (data) => {
|
|
1015
1046
|
if (data.includes(CTRL_C))
|
|
1016
|
-
|
|
1047
|
+
process12.kill(process12.pid, "SIGINT");
|
|
1017
1048
|
};
|
|
1018
1049
|
input.setRawMode(true);
|
|
1019
1050
|
input.on("data", onData);
|
|
@@ -1029,7 +1060,7 @@ function swallowInput() {
|
|
|
1029
1060
|
};
|
|
1030
1061
|
}
|
|
1031
1062
|
function interceptOutput(clear) {
|
|
1032
|
-
const targets = [...CONSOLE_METHODS.map((name) => [console, name]), [
|
|
1063
|
+
const targets = [...CONSOLE_METHODS.map((name) => [console, name]), [process12.stdout, "write"], [process12.stderr, "write"]];
|
|
1033
1064
|
const wrapped = targets.map(([target, name]) => {
|
|
1034
1065
|
const original = Reflect.get(target, name);
|
|
1035
1066
|
const wrapper = (...args) => {
|
|
@@ -1047,33 +1078,111 @@ function interceptOutput(clear) {
|
|
|
1047
1078
|
};
|
|
1048
1079
|
}
|
|
1049
1080
|
// ../kit/src/cli.ts
|
|
1050
|
-
import { readFileSync as
|
|
1081
|
+
import { readFileSync as readFileSync4, realpathSync as realpathSync2 } from "fs";
|
|
1051
1082
|
import { dirname as dirname3, join as join6 } from "path";
|
|
1052
|
-
import
|
|
1083
|
+
import process16 from "process";
|
|
1084
|
+
|
|
1085
|
+
// ../kit/src/completions.ts
|
|
1086
|
+
var COMPLETION_SHELLS = ["fish", "bash", "zsh"];
|
|
1087
|
+
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'
|
|
1088
|
+
set -l tokens (commandline -xpc 2>/dev/null; or commandline -opc)
|
|
1089
|
+
set -l current (commandline -ct)
|
|
1090
|
+
for line in ($bin __complete $tokens[2..-1] $current 2>/dev/null)
|
|
1091
|
+
if test "$line" = __files__
|
|
1092
|
+
__fish_complete_path $current
|
|
1093
|
+
else
|
|
1094
|
+
echo $line
|
|
1095
|
+
end
|
|
1096
|
+
end
|
|
1097
|
+
end`;
|
|
1098
|
+
var BASH_FUNCTION = `__kaynooo_cli_complete() {
|
|
1099
|
+
local IFS=$'\\n' cur=\${COMP_WORDS[COMP_CWORD]} line words=
|
|
1100
|
+
local -a lines
|
|
1101
|
+
COMPREPLY=()
|
|
1102
|
+
lines=($("\${COMP_WORDS[0]}" __complete "\${COMP_WORDS[@]:1:COMP_CWORD}" 2>/dev/null))
|
|
1103
|
+
for line in "\${lines[@]}"; do
|
|
1104
|
+
if [[ $line == __files__ ]]; then
|
|
1105
|
+
COMPREPLY+=($(compgen -f -- "$cur"))
|
|
1106
|
+
else
|
|
1107
|
+
words+="\${line%%$'\\t'*}"$'\\n'
|
|
1108
|
+
fi
|
|
1109
|
+
done
|
|
1110
|
+
COMPREPLY+=($(compgen -W "$words" -- "$cur"))
|
|
1111
|
+
}`;
|
|
1112
|
+
var ZSH_FUNCTION = `__kaynooo_cli_complete() {
|
|
1113
|
+
local line
|
|
1114
|
+
local -a described plain
|
|
1115
|
+
for line in \${(f)"$("\${words[1]}" __complete "\${(@)words[2,CURRENT]}" 2>/dev/null)"}; do
|
|
1116
|
+
if [[ $line == __files__ ]]; then
|
|
1117
|
+
_files
|
|
1118
|
+
elif [[ $line == *$'\\t'* ]]; then
|
|
1119
|
+
described+=("\${\${line%%$'\\t'*}//:/\\\\:}:\${line#*$'\\t'}")
|
|
1120
|
+
else
|
|
1121
|
+
plain+=("$line")
|
|
1122
|
+
fi
|
|
1123
|
+
done
|
|
1124
|
+
(( \${#described} )) && _describe -V value described
|
|
1125
|
+
(( \${#plain} )) && compadd -- "\${plain[@]}"
|
|
1126
|
+
return 0
|
|
1127
|
+
}`;
|
|
1128
|
+
function completionScript(shell, bins, header = `# Generated by \`${bins[0]} --completions ${shell}\``) {
|
|
1129
|
+
if (shell === "fish")
|
|
1130
|
+
return `${header}
|
|
1131
|
+
${FISH_FUNCTION}
|
|
1132
|
+
${bins.map((bin) => `complete -c ${bin} -f -a '(__kaynooo_cli_complete ${bin})'
|
|
1133
|
+
`).join("")}`;
|
|
1134
|
+
if (shell === "bash")
|
|
1135
|
+
return `${header}
|
|
1136
|
+
${BASH_FUNCTION}
|
|
1137
|
+
complete -F __kaynooo_cli_complete ${bins.join(" ")}
|
|
1138
|
+
`;
|
|
1139
|
+
return `${header}
|
|
1140
|
+
${ZSH_FUNCTION}
|
|
1141
|
+
compdef __kaynooo_cli_complete ${bins.join(" ")}
|
|
1142
|
+
`;
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
// ../kit/src/notify.ts
|
|
1146
|
+
import process13 from "process";
|
|
1147
|
+
var NOTIFY_AFTER_MS = 1e4;
|
|
1148
|
+
var CANCELLED = 130;
|
|
1149
|
+
function base64(text) {
|
|
1150
|
+
return Buffer.from(text).toString("base64");
|
|
1151
|
+
}
|
|
1152
|
+
function notification(title, body, env = process13.env) {
|
|
1153
|
+
const id = `kit${process13.pid}`;
|
|
1154
|
+
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\\`];
|
|
1155
|
+
return codes.map((code) => env.TMUX === undefined || env.TMUX === "" ? code : `\x1BPtmux;${code.replaceAll("\x1B", "\x1B\x1B")}\x1B\\`).join("");
|
|
1156
|
+
}
|
|
1157
|
+
function notifyEnd(command, code, ms, { stream = process13.stderr, env = process13.env } = {}) {
|
|
1158
|
+
if (ms < NOTIFY_AFTER_MS || code === CANCELLED || outputMode(stream, env) !== "terminal")
|
|
1159
|
+
return;
|
|
1160
|
+
stream.write(notification(command, code === 0 ? `done in ${formatDuration(ms)}` : `failed with exit ${code} after ${formatDuration(ms)}`, env));
|
|
1161
|
+
}
|
|
1053
1162
|
|
|
1054
1163
|
// ../kit/src/usage.ts
|
|
1055
1164
|
import { appendFileSync, mkdirSync } from "fs";
|
|
1056
1165
|
import { homedir as homedir4 } from "os";
|
|
1057
1166
|
import { dirname as dirname2, isAbsolute, join as join5 } from "path";
|
|
1058
|
-
import
|
|
1167
|
+
import process15 from "process";
|
|
1059
1168
|
|
|
1060
1169
|
// ../kit/src/logs.ts
|
|
1061
1170
|
import { lstatSync, openSync } from "fs";
|
|
1062
1171
|
import { mkdir, mkdtemp, readdir, rm, stat } from "fs/promises";
|
|
1063
1172
|
import { homedir as homedir3, tmpdir } from "os";
|
|
1064
1173
|
import { join as join4 } from "path";
|
|
1065
|
-
import
|
|
1174
|
+
import process14 from "process";
|
|
1066
1175
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
1067
1176
|
var CLI_FOLDER = "kaynooo-cli";
|
|
1068
1177
|
function isPrivateDirectory(path) {
|
|
1069
1178
|
try {
|
|
1070
1179
|
const info = lstatSync(path, { throwIfNoEntry: false });
|
|
1071
|
-
return info !== undefined && info.isDirectory() && info.uid === (
|
|
1180
|
+
return info !== undefined && info.isDirectory() && info.uid === (process14.getuid?.() ?? info.uid) && (info.mode & 63) === 0;
|
|
1072
1181
|
} catch {
|
|
1073
1182
|
return false;
|
|
1074
1183
|
}
|
|
1075
1184
|
}
|
|
1076
|
-
async function privateLogDirectory(name, env =
|
|
1185
|
+
async function privateLogDirectory(name, env = process14.env) {
|
|
1077
1186
|
const runtime = env.XDG_RUNTIME_DIR;
|
|
1078
1187
|
const base = runtime === undefined || runtime === "" ? join4(homedir3(), ".cache", CLI_FOLDER) : join4(runtime, CLI_FOLDER);
|
|
1079
1188
|
const path = join4(base, name);
|
|
@@ -1093,14 +1202,14 @@ var usageError;
|
|
|
1093
1202
|
function noteUsageError(message) {
|
|
1094
1203
|
usageError = message;
|
|
1095
1204
|
}
|
|
1096
|
-
function stateHome(env =
|
|
1205
|
+
function stateHome(env = process15.env) {
|
|
1097
1206
|
const dir = env.XDG_STATE_HOME;
|
|
1098
1207
|
return dir !== undefined && isAbsolute(dir) ? dir : join5(homedir4(), ".local", "state");
|
|
1099
1208
|
}
|
|
1100
|
-
function usagePath(env =
|
|
1209
|
+
function usagePath(env = process15.env) {
|
|
1101
1210
|
return join5(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
1102
1211
|
}
|
|
1103
|
-
function usageRecorded(env =
|
|
1212
|
+
function usageRecorded(env = process15.env) {
|
|
1104
1213
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
1105
1214
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
1106
1215
|
}
|
|
@@ -1114,7 +1223,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
1114
1223
|
usageError = undefined;
|
|
1115
1224
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
1116
1225
|
}
|
|
1117
|
-
function recordUsage(entry, env =
|
|
1226
|
+
function recordUsage(entry, env = process15.env) {
|
|
1118
1227
|
if (!usageRecorded(env))
|
|
1119
1228
|
return;
|
|
1120
1229
|
try {
|
|
@@ -1130,10 +1239,14 @@ var LEVEL_OPTIONS = {
|
|
|
1130
1239
|
...OUTPUT_OPTIONS,
|
|
1131
1240
|
help: { type: "boolean", short: "h", description: "show this help" }
|
|
1132
1241
|
};
|
|
1133
|
-
var TOP_OPTIONS = {
|
|
1242
|
+
var TOP_OPTIONS = {
|
|
1243
|
+
...LEVEL_OPTIONS,
|
|
1244
|
+
version: { type: "boolean", short: "v", description: "print the version" },
|
|
1245
|
+
completions: { type: "string", choices: COMPLETION_SHELLS, description: "print the shell completion script" }
|
|
1246
|
+
};
|
|
1134
1247
|
function printError(prefix, message, hint, cause) {
|
|
1135
|
-
const colors = paint(
|
|
1136
|
-
const styled = takesControlCodes(
|
|
1248
|
+
const colors = paint(process16.stderr);
|
|
1249
|
+
const styled = takesControlCodes(process16.stderr);
|
|
1137
1250
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
1138
1251
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
1139
1252
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -1177,24 +1290,24 @@ function candidateLine(candidate) {
|
|
|
1177
1290
|
}
|
|
1178
1291
|
function positionalBeingTyped(before, { options, positionals }) {
|
|
1179
1292
|
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
1180
|
-
|
|
1293
|
+
const typed = [];
|
|
1181
1294
|
let at = 0;
|
|
1182
1295
|
while (at < before.length) {
|
|
1183
1296
|
const word = before[at];
|
|
1184
1297
|
if (word === "--") {
|
|
1185
|
-
|
|
1298
|
+
typed.push(...before.slice(at + 1));
|
|
1186
1299
|
break;
|
|
1187
1300
|
}
|
|
1188
1301
|
if (!word.startsWith("-"))
|
|
1189
|
-
|
|
1302
|
+
typed.push(word);
|
|
1190
1303
|
else if (takesValue(word))
|
|
1191
1304
|
at++;
|
|
1192
1305
|
at++;
|
|
1193
1306
|
}
|
|
1194
1307
|
const last = positionals.at(-1);
|
|
1195
|
-
if (
|
|
1196
|
-
return positionals[
|
|
1197
|
-
return last?.replace("]", "").endsWith("...") === true ? last : undefined;
|
|
1308
|
+
if (typed.length < positionals.length)
|
|
1309
|
+
return { name: positionals[typed.length], typed };
|
|
1310
|
+
return { name: last?.replace("]", "").endsWith("...") === true ? last : undefined, typed };
|
|
1198
1311
|
}
|
|
1199
1312
|
async function leafCandidates(run, words) {
|
|
1200
1313
|
const completing = await completingOf(run);
|
|
@@ -1202,23 +1315,40 @@ async function leafCandidates(run, words) {
|
|
|
1202
1315
|
return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
|
|
1203
1316
|
const flag = words.at(-2);
|
|
1204
1317
|
const option = flag?.startsWith("-") === true ? Object.entries(completing.options).find(([name, { short }]) => flag === `--${name}` || flag === `-${short}`)?.[1] : undefined;
|
|
1318
|
+
const { name, typed } = positionalBeingTyped(words.slice(0, -1), completing);
|
|
1205
1319
|
if (option?.type === "string") {
|
|
1206
|
-
const values = option.choices === undefined ? await option.candidates?.() : option.choices;
|
|
1320
|
+
const values = option.choices === undefined ? await option.candidates?.(typed) : option.choices;
|
|
1207
1321
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
1208
1322
|
}
|
|
1209
|
-
const
|
|
1210
|
-
const values = name === undefined ? undefined : await completing.candidates[name]?.();
|
|
1323
|
+
const values = name === undefined ? undefined : await completing.candidates[name]?.(typed);
|
|
1211
1324
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
1212
1325
|
}
|
|
1326
|
+
var COMPLETIONS = "--completions";
|
|
1327
|
+
function flagValue([flag = "", ...rest]) {
|
|
1328
|
+
return flag.includes("=") ? [flag.slice(flag.indexOf("=") + 1), ...rest] : rest;
|
|
1329
|
+
}
|
|
1330
|
+
function printCompletionScript(name, args) {
|
|
1331
|
+
const shell = COMPLETION_SHELLS.find((each) => each === args[0]);
|
|
1332
|
+
if (shell === undefined || args.length !== 1) {
|
|
1333
|
+
printError(name, `${COMPLETIONS} takes one of ${COMPLETION_SHELLS.join(", ")}`, `${name} ${COMPLETIONS} fish`);
|
|
1334
|
+
return 2;
|
|
1335
|
+
}
|
|
1336
|
+
console.log(completionScript(shell, [name]).trimEnd());
|
|
1337
|
+
return 0;
|
|
1338
|
+
}
|
|
1213
1339
|
async function printCandidates(found) {
|
|
1214
1340
|
for (const line of await found)
|
|
1215
1341
|
console.log(line);
|
|
1216
1342
|
return 0;
|
|
1217
1343
|
}
|
|
1218
1344
|
function packageVersion(entry = Bun.main) {
|
|
1219
|
-
|
|
1345
|
+
let resolved = entry;
|
|
1346
|
+
try {
|
|
1347
|
+
resolved = realpathSync2(entry);
|
|
1348
|
+
} catch {}
|
|
1349
|
+
for (let dir = dirname3(resolved);; dir = dirname3(dir)) {
|
|
1220
1350
|
try {
|
|
1221
|
-
const { version } = JSON.parse(
|
|
1351
|
+
const { version } = JSON.parse(readFileSync4(join6(dir, "package.json"), "utf8"));
|
|
1222
1352
|
if (typeof version === "string")
|
|
1223
1353
|
return version;
|
|
1224
1354
|
} catch {}
|
|
@@ -1226,19 +1356,23 @@ function packageVersion(entry = Bun.main) {
|
|
|
1226
1356
|
return "0.0.0";
|
|
1227
1357
|
}
|
|
1228
1358
|
}
|
|
1229
|
-
async function runMain({ name, version, run, redact, usage }, argv =
|
|
1359
|
+
async function runMain({ name, version, run, redact, usage }, argv = scriptArgs()) {
|
|
1230
1360
|
if (argv[0] === COMPLETE)
|
|
1231
1361
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
1362
|
+
if (argv[0] === COMPLETIONS || argv[0]?.startsWith(`${COMPLETIONS}=`) === true)
|
|
1363
|
+
return printCompletionScript(name, flagValue(argv));
|
|
1232
1364
|
if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
|
|
1233
1365
|
console.log(version ?? packageVersion());
|
|
1234
1366
|
return 0;
|
|
1235
1367
|
}
|
|
1236
1368
|
let code;
|
|
1369
|
+
const started = performance.now();
|
|
1237
1370
|
try {
|
|
1238
1371
|
code = await run(argv);
|
|
1239
1372
|
} catch (error) {
|
|
1240
1373
|
code = reportThrown(name, error, redact, wantsJson(argv));
|
|
1241
1374
|
}
|
|
1375
|
+
notifyEnd(name, code, performance.now() - started);
|
|
1242
1376
|
if (usage !== false) {
|
|
1243
1377
|
try {
|
|
1244
1378
|
recordUsage(usageEntry(name, undefined, argv, code));
|
|
@@ -1253,15 +1387,15 @@ var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "ses
|
|
|
1253
1387
|
// ../kit/src/sessions.ts
|
|
1254
1388
|
import { homedir as homedir5 } from "os";
|
|
1255
1389
|
import { join as join7 } from "path";
|
|
1256
|
-
import
|
|
1390
|
+
import process17 from "process";
|
|
1257
1391
|
var {Glob } = globalThis.Bun;
|
|
1258
1392
|
var SESSION_FILES = new Glob("*.json");
|
|
1259
1393
|
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
1260
1394
|
async function sessionDirs() {
|
|
1261
1395
|
const home = homedir5();
|
|
1262
1396
|
const dirs = new Set([join7(home, ".claude", "sessions")]);
|
|
1263
|
-
if (
|
|
1264
|
-
dirs.add(join7(
|
|
1397
|
+
if (process17.env.CLAUDE_CONFIG_DIR !== undefined)
|
|
1398
|
+
dirs.add(join7(process17.env.CLAUDE_CONFIG_DIR, "sessions"));
|
|
1265
1399
|
try {
|
|
1266
1400
|
for await (const dir of ACCOUNT_DIRS.scan({ cwd: join7(home, ".claude-accounts"), onlyFiles: false, absolute: true }))
|
|
1267
1401
|
dirs.add(dir);
|
|
@@ -1307,7 +1441,7 @@ async function liveSessions() {
|
|
|
1307
1441
|
}
|
|
1308
1442
|
async function currentSession(sessions) {
|
|
1309
1443
|
const byPid = new Map(sessions.map((session) => [session.pid, session]));
|
|
1310
|
-
let pid =
|
|
1444
|
+
let pid = process17.ppid;
|
|
1311
1445
|
for (let depth = 0;depth < 32 && pid > 1; depth++) {
|
|
1312
1446
|
const session = byPid.get(pid);
|
|
1313
1447
|
if (session !== undefined)
|
|
@@ -1320,20 +1454,20 @@ async function currentSession(sessions) {
|
|
|
1320
1454
|
return;
|
|
1321
1455
|
}
|
|
1322
1456
|
// src/slot.ts
|
|
1323
|
-
import { existsSync as existsSync4, mkdirSync as mkdirSync2, readdirSync, readFileSync as
|
|
1457
|
+
import { existsSync as existsSync4, mkdirSync as mkdirSync2, readdirSync, readFileSync as readFileSync5, rmSync } from "fs";
|
|
1324
1458
|
import { availableParallelism } from "os";
|
|
1325
1459
|
import { join as join8 } from "path";
|
|
1326
|
-
import
|
|
1460
|
+
import process20 from "process";
|
|
1327
1461
|
|
|
1328
1462
|
// src/flock.ts
|
|
1329
1463
|
import { closeSync, constants as constants2, ftruncateSync, openSync as openSync2, writeSync } from "fs";
|
|
1330
|
-
import
|
|
1464
|
+
import process19 from "process";
|
|
1331
1465
|
import { dlopen, FFIType } from "bun:ffi";
|
|
1332
1466
|
var LOCK_EX = 2;
|
|
1333
1467
|
var LOCK_NB = 4;
|
|
1334
1468
|
var flock;
|
|
1335
1469
|
function native() {
|
|
1336
|
-
flock ??= dlopen(
|
|
1470
|
+
flock ??= dlopen(process19.platform === "darwin" ? "libSystem.B.dylib" : "libc.so.6", { flock: { args: [FFIType.i32, FFIType.i32], returns: FFIType.i32 } }).symbols.flock;
|
|
1337
1471
|
return flock;
|
|
1338
1472
|
}
|
|
1339
1473
|
function tryLock(path) {
|
|
@@ -1377,14 +1511,14 @@ function defaultSlots() {
|
|
|
1377
1511
|
return Math.max(1, Math.floor(availableParallelism() / 2));
|
|
1378
1512
|
}
|
|
1379
1513
|
async function stateDir() {
|
|
1380
|
-
const dir =
|
|
1514
|
+
const dir = process20.env.SLOT_DIR ?? await privateLogDirectory("slot");
|
|
1381
1515
|
for (const sub of ["slots", "locks", "waiters"])
|
|
1382
1516
|
mkdirSync2(join8(dir, sub), { recursive: true, mode: 448 });
|
|
1383
1517
|
return dir;
|
|
1384
1518
|
}
|
|
1385
1519
|
function readHolder(path) {
|
|
1386
1520
|
try {
|
|
1387
|
-
const holder = JSON.parse(
|
|
1521
|
+
const holder = JSON.parse(readFileSync5(path, "utf8"));
|
|
1388
1522
|
return typeof holder.pid === "number" && typeof holder.command === "string" ? { pid: holder.pid, command: holder.command, cwd: holder.cwd ?? "", since: holder.since ?? 0, ...holder.session === undefined ? {} : { session: holder.session }, ...holder.resource === undefined ? {} : { resource: holder.resource } } : undefined;
|
|
1389
1523
|
} catch {
|
|
1390
1524
|
return;
|
|
@@ -1419,7 +1553,7 @@ function waitingLine(dir, wanted) {
|
|
|
1419
1553
|
}
|
|
1420
1554
|
async function holderOf(command) {
|
|
1421
1555
|
const session = await currentSession(await liveSessions());
|
|
1422
|
-
return { pid:
|
|
1556
|
+
return { pid: process20.pid, command: command.join(" "), cwd: process20.cwd(), since: Date.now(), ...session === undefined ? {} : { session: session.name } };
|
|
1423
1557
|
}
|
|
1424
1558
|
var waiterCount = 0;
|
|
1425
1559
|
async function acquire(dir, resources, holder, deadline, timeoutMs) {
|
|
@@ -1445,7 +1579,7 @@ async function acquire(dir, resources, holder, deadline, timeoutMs) {
|
|
|
1445
1579
|
held.release();
|
|
1446
1580
|
const resource = busy.kind === "lock" ? `lock ${busy.name}` : "slot";
|
|
1447
1581
|
if (waiter === undefined) {
|
|
1448
|
-
const path = join8(dir, "waiters", `${
|
|
1582
|
+
const path = join8(dir, "waiters", `${process20.pid}-${waiterCount++}`);
|
|
1449
1583
|
const held = tryLock(path);
|
|
1450
1584
|
if (held !== undefined) {
|
|
1451
1585
|
writeHeld(held, JSON.stringify({ ...holder, resource }));
|
|
@@ -1486,7 +1620,7 @@ ${RUN_USAGE}`);
|
|
|
1486
1620
|
const count = wholeNumber(values.slots, "slots", defaultSlots());
|
|
1487
1621
|
const timeoutMs = duration(values.timeout, "timeout", Number.POSITIVE_INFINITY);
|
|
1488
1622
|
const deadline = Date.now() + timeoutMs;
|
|
1489
|
-
const holding = new Set((
|
|
1623
|
+
const holding = new Set((process20.env[HOLDING] ?? "").split(",").filter((token) => token !== ""));
|
|
1490
1624
|
const wanted = [...locks.filter((name) => !holding.has(`lock:${name}`)).map((name) => ({ kind: "lock", name, count })), ...values["no-slot"] === true || holding.has("slot") ? [] : [{ kind: "slot", name: "", count }]];
|
|
1491
1625
|
const signals = holdSignals(() => {
|
|
1492
1626
|
return;
|
|
@@ -1501,7 +1635,7 @@ ${RUN_USAGE}`);
|
|
|
1501
1635
|
const tokens = new Set([...holding, ...locks.map((name) => `lock:${name}`), ...wanted.some((resource) => resource.kind === "slot") ? ["slot"] : []]);
|
|
1502
1636
|
let child;
|
|
1503
1637
|
try {
|
|
1504
|
-
child = Bun.spawn(rest, { stdio: ["inherit", "inherit", "inherit"], env: { ...
|
|
1638
|
+
child = Bun.spawn(rest, { stdio: ["inherit", "inherit", "inherit"], env: { ...process20.env, [HOLDING]: [...tokens].join(",") } });
|
|
1505
1639
|
} catch (error) {
|
|
1506
1640
|
fail(`cannot run ${rest[0]}: ${error instanceof Error ? error.message : String(error)}`, 127);
|
|
1507
1641
|
}
|
|
@@ -1544,11 +1678,11 @@ async function listSlots(args) {
|
|
|
1544
1678
|
printJson({ slots: count, busy, entries });
|
|
1545
1679
|
return 0;
|
|
1546
1680
|
}
|
|
1547
|
-
console.log(paint(
|
|
1681
|
+
console.log(paint(process20.stdout).bold(`slots: ${busy} of ${count} busy`));
|
|
1548
1682
|
if (entries.length > 0)
|
|
1549
|
-
console.log(table(entries.map((entry) => [badge(entry.state,
|
|
1683
|
+
console.log(table(entries.map((entry) => [badge(entry.state, process20.stdout), entry.resource, entry.session ?? `pid ${entry.pid}`, truncate(entry.command, COMMAND_WIDTH), entry.since === 0 ? "?" : formatAge(entry.since)])));
|
|
1550
1684
|
return 0;
|
|
1551
1685
|
}
|
|
1552
1686
|
|
|
1553
1687
|
// src/slot-cli.ts
|
|
1554
|
-
|
|
1688
|
+
process21.exitCode = await runMain({ name: "slot", run: async (args) => args[0] === "ls" ? listSlots(args.slice(1)) : runSlotted(args) });
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-wait",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.12.1",
|
|
5
5
|
"description": "waitfor and slot: waits for a log line, a listening port or a URL answering, and queues heavy commands behind machine-wide slots and named locks, for Claude Code, instead of sleep and polling loops.",
|
|
6
6
|
"author": "Kaynooo <contact@kaynooo.fr>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -38,6 +38,6 @@
|
|
|
38
38
|
"waitfor": "$npm_execpath src/cli.ts"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@kaynooo/cli-kit": "0.
|
|
41
|
+
"@kaynooo/cli-kit": "0.41.0"
|
|
42
42
|
}
|
|
43
43
|
}
|