@kaynooo/cli-wait 0.0.0-stage → 0.5.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/LICENSE +21 -0
- package/README.md +50 -2
- package/dist/cli.js +1475 -0
- package/dist/slot-cli.js +1340 -0
- package/package.json +40 -4
package/dist/cli.js
ADDED
|
@@ -0,0 +1,1475 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// @bun
|
|
3
|
+
|
|
4
|
+
// src/cli.ts
|
|
5
|
+
import { existsSync as existsSync3 } from "fs";
|
|
6
|
+
import process13 from "process";
|
|
7
|
+
|
|
8
|
+
// ../kit/src/args.ts
|
|
9
|
+
import process4 from "process";
|
|
10
|
+
import { parseArgs } from "util";
|
|
11
|
+
|
|
12
|
+
// ../kit/src/format.ts
|
|
13
|
+
import process2 from "process";
|
|
14
|
+
|
|
15
|
+
// ../kit/src/style.ts
|
|
16
|
+
import process from "process";
|
|
17
|
+
|
|
18
|
+
// ../../node_modules/.bun/@kaynooo+utils@2.31.2+d0769e28dc4dad16/node_modules/@kaynooo/utils/dist/chunk-mtetydvc.js
|
|
19
|
+
function x(s) {
|
|
20
|
+
let r = s.startsWith("#") ? s.slice(1).toLowerCase() : s.toLowerCase();
|
|
21
|
+
if (!/^[a-f0-9]+$/.test(r))
|
|
22
|
+
return [0, 0, 0];
|
|
23
|
+
let t = r.length, n = (c) => Number.parseInt(c, 16);
|
|
24
|
+
if (t === 3 || t === 4) {
|
|
25
|
+
let c = n(r[0] + r[0]), u = n(r[1] + r[1]), h = n(r[2] + r[2]), g = t === 4 ? n(r[3] + r[3]) : undefined;
|
|
26
|
+
return g !== undefined ? [c, u, h, g] : [c, u, h];
|
|
27
|
+
}
|
|
28
|
+
if (t !== 6 && t !== 8)
|
|
29
|
+
throw Error(`Invalid hex color length: ${t}`);
|
|
30
|
+
let a = n(r.slice(0, 2)), b = n(r.slice(2, 4)), o = n(r.slice(4, 6)), y = t === 8 ? n(r.slice(6, 8)) : undefined;
|
|
31
|
+
if ([a, b, o].some(Number.isNaN) || y !== undefined && Number.isNaN(y))
|
|
32
|
+
throw Error(`Invalid hex color value: ${s}`);
|
|
33
|
+
return y !== undefined ? [a, b, o, y] : [a, b, o];
|
|
34
|
+
}
|
|
35
|
+
function A(s) {
|
|
36
|
+
let [r, t, n, a] = s;
|
|
37
|
+
if (a)
|
|
38
|
+
return `#${(r << 24 | t << 16 | n << 8 | Math.round((a ?? 1) * 255)).toString(16).padStart(8, "0").toUpperCase()}`;
|
|
39
|
+
return `#${(r << 16 | t << 8 | n).toString(16).padStart(6, "0").toUpperCase()}`;
|
|
40
|
+
}
|
|
41
|
+
function R(s, r, t) {
|
|
42
|
+
s = (s % 360 + 360) % 360, t /= 100;
|
|
43
|
+
let n = r * Math.min(t, 1 - t) / 100, a = (b) => {
|
|
44
|
+
let o = (b + s / 30) % 12, y = t - n * Math.max(Math.min(o - 3, 9 - o, 1), -1);
|
|
45
|
+
return Math.round(255 * y);
|
|
46
|
+
};
|
|
47
|
+
return [a(0), a(8), a(4)];
|
|
48
|
+
}
|
|
49
|
+
function k(s) {
|
|
50
|
+
let [r, t, n] = s;
|
|
51
|
+
r /= 255, t /= 255, n /= 255;
|
|
52
|
+
let a = Math.max(r, t, n), b = Math.min(r, t, n), o = 0, y = 0, c = (a + b) / 2;
|
|
53
|
+
if (a !== b) {
|
|
54
|
+
let u = a - b;
|
|
55
|
+
if (y = c > 0 && c < 1 ? u / (1 - Math.abs(2 * c - 1)) : 0, a === r)
|
|
56
|
+
o = (t - n) / u + (t < n ? 6 : 0);
|
|
57
|
+
else if (a === t)
|
|
58
|
+
o = (n - r) / u + 2;
|
|
59
|
+
else
|
|
60
|
+
o = (r - t) / u + 4;
|
|
61
|
+
o /= 6;
|
|
62
|
+
}
|
|
63
|
+
return [Math.round(o * 360), Math.round(y * 100), Math.round(c * 100)];
|
|
64
|
+
}
|
|
65
|
+
function H(s, r, t) {
|
|
66
|
+
s = (s % 360 + 360) % 360, r /= 100, t /= 100;
|
|
67
|
+
let n = (a) => {
|
|
68
|
+
let b = (a + s / 60) % 6, o = t - t * r * Math.max(Math.min(b, 4 - b, 1), 0);
|
|
69
|
+
return Math.round(255 * o);
|
|
70
|
+
};
|
|
71
|
+
return [n(5), n(3), n(1)];
|
|
72
|
+
}
|
|
73
|
+
function f(s) {
|
|
74
|
+
let [r, t, n] = s;
|
|
75
|
+
r /= 255, t /= 255, n /= 255;
|
|
76
|
+
let a = Math.max(r, t, n), b = Math.min(r, t, n), o = a - b, y = 0, c = a === 0 ? 0 : o / a, u = a;
|
|
77
|
+
if (o !== 0) {
|
|
78
|
+
if (a === r)
|
|
79
|
+
y = (t - n) / o + (t < n ? 6 : 0);
|
|
80
|
+
else if (a === t)
|
|
81
|
+
y = (n - r) / o + 2;
|
|
82
|
+
else
|
|
83
|
+
y = (r - t) / o + 4;
|
|
84
|
+
y /= 6;
|
|
85
|
+
}
|
|
86
|
+
return [Math.round(y * 360), Math.round(c * 100), Math.round(u * 100)];
|
|
87
|
+
}
|
|
88
|
+
function O(s) {
|
|
89
|
+
let r = s[0] / 255, t = s[1] / 255, n = s[2] / 255, a = 1 - Math.max(r, t, n);
|
|
90
|
+
if (a === 1)
|
|
91
|
+
return [0, 0, 0, 100];
|
|
92
|
+
let b = (1 - r - a) / (1 - a), o = (1 - t - a) / (1 - a), y = (1 - n - a) / (1 - a);
|
|
93
|
+
return [Math.round(b * 100), Math.round(o * 100), Math.round(y * 100), Math.round(a * 100)];
|
|
94
|
+
}
|
|
95
|
+
function v(s, r, t, n) {
|
|
96
|
+
return s /= 100, r /= 100, t /= 100, n /= 100, [Math.round(255 * (1 - s) * (1 - n)), Math.round(255 * (1 - r) * (1 - n)), Math.round(255 * (1 - t) * (1 - n))];
|
|
97
|
+
}
|
|
98
|
+
var m = (s) => s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
|
|
99
|
+
var p = (s) => s <= 0.0031308 ? 12.92 * s : 1.055 * s ** 0.4166666666666667 - 0.055;
|
|
100
|
+
var i = (s, r) => Math.round(s * 10 ** r) / 10 ** r;
|
|
101
|
+
function T(s) {
|
|
102
|
+
let r = m(s[0] / 255), t = m(s[1] / 255), n = m(s[2] / 255), a = Math.cbrt(0.4122214708 * r + 0.5363325363 * t + 0.0514459929 * n), b = Math.cbrt(0.2119034982 * r + 0.6806995451 * t + 0.1073969566 * n), o = Math.cbrt(0.0883024619 * r + 0.2817188376 * t + 0.6299787005 * n);
|
|
103
|
+
return [i(0.2104542553 * a + 0.793617785 * b - 0.0040720468 * o, 4), i(1.9779984951 * a - 2.428592205 * b + 0.4505937099 * o, 4), i(0.0259040371 * a + 0.7827717662 * b - 0.808675766 * o, 4)];
|
|
104
|
+
}
|
|
105
|
+
function M(s, r, t) {
|
|
106
|
+
let n = (s + 0.3963377774 * r + 0.2158037573 * t) ** 3, a = (s - 0.1055613458 * r - 0.0638541728 * t) ** 3, b = (s - 0.0894841775 * r - 1.291485548 * t) ** 3, o = p(4.0767416621 * n - 3.3077115913 * a + 0.2309699292 * b), y = p(-1.2684380046 * n + 2.6097574011 * a - 0.3413193965 * b), c = p(-0.0041960863 * n - 0.7034186147 * a + 1.707614701 * b), u = (h) => Math.min(255, Math.max(0, Math.round(h * 255)));
|
|
107
|
+
return [u(o), u(y), u(c)];
|
|
108
|
+
}
|
|
109
|
+
function d(s) {
|
|
110
|
+
let [r, t, n] = T(s), a = Math.sqrt(t * t + n * n), b = (Math.atan2(n, t) * 180 / Math.PI + 360) % 360;
|
|
111
|
+
return [i(r, 4), i(a, 4), i(b, 2)];
|
|
112
|
+
}
|
|
113
|
+
function C(s, r, t) {
|
|
114
|
+
let n = t * Math.PI / 180;
|
|
115
|
+
return M(s, r * Math.cos(n), r * Math.sin(n));
|
|
116
|
+
}
|
|
117
|
+
function l(s) {
|
|
118
|
+
let r = s / 255;
|
|
119
|
+
return r <= 0.03928 ? r / 12.92 : ((r + 0.055) / 1.055) ** 2.4;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
class e {
|
|
123
|
+
r;
|
|
124
|
+
g;
|
|
125
|
+
b;
|
|
126
|
+
a;
|
|
127
|
+
constructor(s, r, t, n = 1) {
|
|
128
|
+
this.r = s, this.g = r, this.b = t, this.a = n;
|
|
129
|
+
}
|
|
130
|
+
static from(s, r) {
|
|
131
|
+
if (s === "hex") {
|
|
132
|
+
let [t, n, a, b] = x(r);
|
|
133
|
+
return new e(t, n, a, b);
|
|
134
|
+
}
|
|
135
|
+
if (s === "rgb") {
|
|
136
|
+
if (Array.isArray(r)) {
|
|
137
|
+
let [o, y, c, u] = r;
|
|
138
|
+
return new e(o, y, c, u);
|
|
139
|
+
}
|
|
140
|
+
let { r: t, g: n, b: a, a: b } = r;
|
|
141
|
+
return new e(t, n, a, b);
|
|
142
|
+
}
|
|
143
|
+
if (s === "hsl") {
|
|
144
|
+
let t, n, a, b;
|
|
145
|
+
if (Array.isArray(r))
|
|
146
|
+
[t, n, a, b] = r;
|
|
147
|
+
else
|
|
148
|
+
({ h: t, s: n, l: a, a: b } = r);
|
|
149
|
+
let [o, y, c] = R(t, n, a);
|
|
150
|
+
return new e(o, y, c, b);
|
|
151
|
+
}
|
|
152
|
+
if (s === "hsv") {
|
|
153
|
+
let t, n, a, b;
|
|
154
|
+
if (Array.isArray(r))
|
|
155
|
+
[t, n, a, b] = r;
|
|
156
|
+
else
|
|
157
|
+
({ h: t, s: n, v: a, a: b } = r);
|
|
158
|
+
let [o, y, c] = H(t, n, a);
|
|
159
|
+
return new e(o, y, c, b);
|
|
160
|
+
}
|
|
161
|
+
if (s === "cmyk") {
|
|
162
|
+
let t, n, a, b, o;
|
|
163
|
+
if (Array.isArray(r))
|
|
164
|
+
[t, n, a, b, o] = r;
|
|
165
|
+
else
|
|
166
|
+
({ c: t, m: n, y: a, k: b, a: o } = r);
|
|
167
|
+
let [y, c, u] = v(t, n, a, b);
|
|
168
|
+
return new e(y, c, u, o);
|
|
169
|
+
}
|
|
170
|
+
if (s === "oklch") {
|
|
171
|
+
let t, n, a, b;
|
|
172
|
+
if (Array.isArray(r))
|
|
173
|
+
[t, n, a, b] = r;
|
|
174
|
+
else
|
|
175
|
+
({ l: t, c: n, h: a, a: b } = r);
|
|
176
|
+
let [o, y, c] = C(t, n, a);
|
|
177
|
+
return new e(o, y, c, b);
|
|
178
|
+
}
|
|
179
|
+
if (s === "oklab") {
|
|
180
|
+
let t, n, a, b;
|
|
181
|
+
if (Array.isArray(r))
|
|
182
|
+
[t, n, a, b] = r;
|
|
183
|
+
else
|
|
184
|
+
({ l: t, a: n, b: a, alpha: b } = r);
|
|
185
|
+
let [o, y, c] = M(t, n, a);
|
|
186
|
+
return new e(o, y, c, b);
|
|
187
|
+
}
|
|
188
|
+
throw Error("Invalid input");
|
|
189
|
+
}
|
|
190
|
+
to(s) {
|
|
191
|
+
switch (s) {
|
|
192
|
+
case "rgb":
|
|
193
|
+
return [this.r, this.g, this.b];
|
|
194
|
+
case "rgba":
|
|
195
|
+
return [this.r, this.g, this.b, this.a];
|
|
196
|
+
case "hex":
|
|
197
|
+
return A([this.r, this.g, this.b]);
|
|
198
|
+
case "hexa":
|
|
199
|
+
return A([this.r, this.g, this.b, this.a]);
|
|
200
|
+
case "hsl":
|
|
201
|
+
return k([this.r, this.g, this.b]);
|
|
202
|
+
case "hsla":
|
|
203
|
+
return [...k([this.r, this.g, this.b]), this.a];
|
|
204
|
+
case "hsv":
|
|
205
|
+
return f([this.r, this.g, this.b]);
|
|
206
|
+
case "hsva":
|
|
207
|
+
return [...f([this.r, this.g, this.b]), this.a];
|
|
208
|
+
case "cmyk":
|
|
209
|
+
return O([this.r, this.g, this.b]);
|
|
210
|
+
case "cmyka":
|
|
211
|
+
return [...O([this.r, this.g, this.b]), this.a];
|
|
212
|
+
case "oklch":
|
|
213
|
+
return d([this.r, this.g, this.b]);
|
|
214
|
+
case "oklcha":
|
|
215
|
+
return [...d([this.r, this.g, this.b]), this.a];
|
|
216
|
+
case "oklab":
|
|
217
|
+
return T([this.r, this.g, this.b]);
|
|
218
|
+
case "oklaba":
|
|
219
|
+
return [...T([this.r, this.g, this.b]), this.a];
|
|
220
|
+
default:
|
|
221
|
+
return [this.r, this.g, this.b];
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
getLuminance() {
|
|
225
|
+
let s = l(this.r), r = l(this.g), t = l(this.b);
|
|
226
|
+
return 0.2126 * s + 0.7152 * r + 0.0722 * t;
|
|
227
|
+
}
|
|
228
|
+
contrastRatio(s) {
|
|
229
|
+
let r = this.getLuminance(), t = s.getLuminance(), [n, a] = r >= t ? [r, t] : [t, r];
|
|
230
|
+
return (n + 0.05) / (a + 0.05);
|
|
231
|
+
}
|
|
232
|
+
bestContrastForeground() {
|
|
233
|
+
let s = new e(255, 255, 255, 1), r = new e(0, 0, 0, 1), t = this.contrastRatio(s), n = this.contrastRatio(r);
|
|
234
|
+
return t >= n ? s : r;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// ../../node_modules/.bun/@kaynooo+utils@2.31.2+d0769e28dc4dad16/node_modules/@kaynooo/utils/dist/chunk-y1msbssb.js
|
|
239
|
+
var r = 39;
|
|
240
|
+
var o = 49;
|
|
241
|
+
var f2 = "\x1B[";
|
|
242
|
+
var C2 = { reset: [0, 0], bold: [1, 22], dim: [2, 22], italic: [3, 23], underline: [4, 24], inverse: [7, 27], hidden: [8, 28], strikethrough: [9, 29], black: [30, r], red: [31, r], green: [32, r], yellow: [33, r], blue: [34, r], magenta: [35, r], cyan: [36, r], white: [37, r], gray: [90, r], grey: [90, r], blackBright: [90, r], redBright: [91, r], greenBright: [92, r], yellowBright: [93, r], blueBright: [94, r], magentaBright: [95, r], cyanBright: [96, r], whiteBright: [97, r], bgBlack: [40, o], bgRed: [41, o], bgGreen: [42, o], bgYellow: [43, o], bgBlue: [44, o], bgMagenta: [45, o], bgCyan: [46, o], bgWhite: [47, o], bgBlackBright: [100, o], bgRedBright: [101, o], bgGreenBright: [102, o], bgYellowBright: [103, o], bgBlueBright: [104, o], bgMagentaBright: [105, o], bgCyanBright: [106, o], bgWhiteBright: [107, o], rgb: (i, n, a, l = "fg") => [`${l === "fg" ? 38 : 48};2;${i};${n};${a}`, l === "fg" ? r : o], hex: (i, n = "fg") => {
|
|
243
|
+
let [a, l, s] = e.from("hex", i).to("rgb");
|
|
244
|
+
return [`${n === "fg" ? 38 : 48};2;${a};${l};${s}`, n === "fg" ? r : o];
|
|
245
|
+
}, ansi256: (i, n = "fg") => [`${n === "fg" ? 38 : 48};5;${i}`, n === "fg" ? r : o] };
|
|
246
|
+
var b = new Set(["", "1", "2", "3", "true"]);
|
|
247
|
+
function h(i, n = globalThis.process?.env ?? {}) {
|
|
248
|
+
if (n.FORCE_COLOR !== undefined)
|
|
249
|
+
return b.has(n.FORCE_COLOR);
|
|
250
|
+
if (n.NO_COLOR !== undefined || n.NODE_DISABLE_COLORS !== undefined || n.TERM === "dumb")
|
|
251
|
+
return false;
|
|
252
|
+
return i?.isTTY === true;
|
|
253
|
+
}
|
|
254
|
+
function d2(i, n, a) {
|
|
255
|
+
if (!h(a.stream ?? globalThis.process?.stdout, a.env))
|
|
256
|
+
return n;
|
|
257
|
+
let l = "", s = "";
|
|
258
|
+
for (let g = 0, y = i.length;g < y; g++) {
|
|
259
|
+
let [c, p] = i[g];
|
|
260
|
+
l += `${f2}${c}m`, s = `${f2}${p}m${s}`;
|
|
261
|
+
}
|
|
262
|
+
return l + n + s;
|
|
263
|
+
}
|
|
264
|
+
function u(i, n = []) {
|
|
265
|
+
return new Proxy((l) => d2(n, l, i), { get(l, s) {
|
|
266
|
+
let g = C2[s];
|
|
267
|
+
if (Array.isArray(g))
|
|
268
|
+
return u(i, [...n, g]);
|
|
269
|
+
if (typeof g === "function")
|
|
270
|
+
return (...y) => u(i, [...n, g(...y)]);
|
|
271
|
+
throw Error(`Unknown style: ${String(s)}`);
|
|
272
|
+
} });
|
|
273
|
+
}
|
|
274
|
+
function le(i = {}) {
|
|
275
|
+
return u(i);
|
|
276
|
+
}
|
|
277
|
+
var t = le();
|
|
278
|
+
|
|
279
|
+
// ../kit/src/style.ts
|
|
280
|
+
function takesControlCodes(stream, env = process.env) {
|
|
281
|
+
return stream.isTTY === true && env.NO_COLOR === undefined && env.NODE_DISABLE_COLORS === undefined && env.TERM !== "dumb";
|
|
282
|
+
}
|
|
283
|
+
function paint(stream, env = process.env) {
|
|
284
|
+
return le({ stream: { isTTY: takesControlCodes(stream, env) }, env });
|
|
285
|
+
}
|
|
286
|
+
var STATUSES = {
|
|
287
|
+
passed: { symbol: "\u2713", words: ["passed", "success", "succeeded", "ok"], paint: (colors) => colors.green },
|
|
288
|
+
failed: { symbol: "\u2717", words: ["failed", "failure", "error", "errored"], paint: (colors) => colors.red },
|
|
289
|
+
running: { symbol: "\u25CF", words: ["running", "in_progress", "started"], paint: (colors) => colors.cyan },
|
|
290
|
+
queued: { symbol: "\u25CB", words: ["queued", "waiting", "pending", "created", "blocked", "scheduled"], paint: (colors) => colors.yellow },
|
|
291
|
+
skipped: { symbol: "-", words: ["skipped", "cancelled", "canceled", "manual"], paint: (colors) => colors.dim }
|
|
292
|
+
};
|
|
293
|
+
var STATUS_OF = new Map(Object.entries(STATUSES).flatMap(([status, { words }]) => words.map((word) => [word, status])));
|
|
294
|
+
|
|
295
|
+
// ../kit/src/format.ts
|
|
296
|
+
var UNITS = [["d", 86400], ["h", 3600], ["m", 60], ["s", 1]];
|
|
297
|
+
function formatSeconds(total) {
|
|
298
|
+
let rest = Math.max(0, Math.round(total));
|
|
299
|
+
const amounts = UNITS.map(([unit, size]) => {
|
|
300
|
+
const amount = Math.floor(rest / size);
|
|
301
|
+
rest -= amount * size;
|
|
302
|
+
return `${amount}${unit}`;
|
|
303
|
+
});
|
|
304
|
+
const first = amounts.findIndex((amount) => !amount.startsWith("0"));
|
|
305
|
+
if (first === -1)
|
|
306
|
+
return "0s";
|
|
307
|
+
const next = amounts[first + 1];
|
|
308
|
+
return next === undefined || next.startsWith("0") ? amounts[first] ?? "0s" : `${amounts[first]} ${next}`;
|
|
309
|
+
}
|
|
310
|
+
function formatDuration(ms) {
|
|
311
|
+
return ms < 60000 ? `${(Math.max(0, ms) / 1000).toFixed(1)}s` : formatSeconds(ms / 1000);
|
|
312
|
+
}
|
|
313
|
+
function formatElapsed(ms) {
|
|
314
|
+
return formatSeconds(Math.floor(Math.max(0, ms) / 1000));
|
|
315
|
+
}
|
|
316
|
+
function upTo(text, separator) {
|
|
317
|
+
const end = text.indexOf(separator);
|
|
318
|
+
return end === -1 ? text : text.slice(0, end);
|
|
319
|
+
}
|
|
320
|
+
function formatAge(date, now = Date.now()) {
|
|
321
|
+
const at = typeof date === "number" ? date : Date.parse(date);
|
|
322
|
+
if (Number.isNaN(at))
|
|
323
|
+
return "-";
|
|
324
|
+
const seconds = (now - at) / 1000;
|
|
325
|
+
return seconds < 60 ? "just now" : `${upTo(formatSeconds(seconds), " ")} ago`;
|
|
326
|
+
}
|
|
327
|
+
function truncate(text, width) {
|
|
328
|
+
return text.length <= width ? text : `${text.slice(0, Math.max(0, width - 1))}\u2026`;
|
|
329
|
+
}
|
|
330
|
+
var LINE_BREAKS = /\s*\n\s*/g;
|
|
331
|
+
var ESCAPE = String.fromCharCode(27);
|
|
332
|
+
function textWidth(text) {
|
|
333
|
+
return Bun.stringWidth(text);
|
|
334
|
+
}
|
|
335
|
+
function table(rows, { cellWidth, header = false, align = [], fit } = {}) {
|
|
336
|
+
const cells = cellWidth === undefined ? rows : rows.map((row) => row.map((cell) => truncate(cell.replaceAll(LINE_BREAKS, " "), cellWidth)));
|
|
337
|
+
const widths = [];
|
|
338
|
+
for (const row of cells) {
|
|
339
|
+
for (const [index, cell] of row.entries())
|
|
340
|
+
widths[index] = Math.max(widths[index] ?? 0, textWidth(cell));
|
|
341
|
+
}
|
|
342
|
+
const shown = fit === undefined ? cells : narrow(cells, widths, fit);
|
|
343
|
+
const colors = paint(process2.stdout);
|
|
344
|
+
const lines = shown.map((row) => row.map((cell, index) => {
|
|
345
|
+
const width = widths[index] ?? 0;
|
|
346
|
+
const gap = " ".repeat(Math.max(0, width - textWidth(cell)));
|
|
347
|
+
if (align[index] === "right")
|
|
348
|
+
return `${gap}${cell}`;
|
|
349
|
+
return index === row.length - 1 ? cell : `${cell}${gap}`;
|
|
350
|
+
}).join(" ").trimEnd());
|
|
351
|
+
return lines.map((line, index) => header && index === 0 ? colors.bold(line) : line).join(`
|
|
352
|
+
`);
|
|
353
|
+
}
|
|
354
|
+
function narrow(rows, widths, limit) {
|
|
355
|
+
const used = () => widths.reduce((sum, width) => sum + width, 0) + 2 * Math.max(0, widths.length - 1);
|
|
356
|
+
const cuttable = (index) => rows.every((row) => !(row[index] ?? "").includes(ESCAPE));
|
|
357
|
+
const floor = 8;
|
|
358
|
+
while (used() > limit) {
|
|
359
|
+
let widest = -1;
|
|
360
|
+
for (const [index, width] of widths.entries()) {
|
|
361
|
+
if (cuttable(index) && width > floor && width > (widths[widest] ?? 0))
|
|
362
|
+
widest = index;
|
|
363
|
+
}
|
|
364
|
+
if (widest === -1)
|
|
365
|
+
break;
|
|
366
|
+
widths[widest] = Math.max(floor, (widths[widest] ?? 0) - (used() - limit));
|
|
367
|
+
}
|
|
368
|
+
return rows.map((row) => row.map((cell, index) => textWidth(cell) > (widths[index] ?? 0) ? truncate(cell, widths[index] ?? 0) : cell));
|
|
369
|
+
}
|
|
370
|
+
function printJson(value) {
|
|
371
|
+
console.log(JSON.stringify(value, undefined, 2));
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// ../kit/src/help.ts
|
|
375
|
+
import process3 from "process";
|
|
376
|
+
var OPTIONAL = /\[[^\]]+\]/g;
|
|
377
|
+
var USAGE_PREFIX = /^usage:\s*/i;
|
|
378
|
+
var FULL_STOP = /\.$/;
|
|
379
|
+
function usageLine(usage, colors = paint(process3.stdout)) {
|
|
380
|
+
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
381
|
+
}
|
|
382
|
+
function helpText(usage, options, colors = paint(process3.stdout), { examples = [], replaces } = {}) {
|
|
383
|
+
return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exampleRows(examples, colors)].join(`
|
|
384
|
+
`);
|
|
385
|
+
}
|
|
386
|
+
function replacesRows(replaces, colors = paint(process3.stdout)) {
|
|
387
|
+
return replaces === undefined ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
388
|
+
}
|
|
389
|
+
function wantsHelpJson(env = process3.env) {
|
|
390
|
+
return env.KIT_HELP === "json";
|
|
391
|
+
}
|
|
392
|
+
function description(usage, { examples = [], replaces } = {}) {
|
|
393
|
+
const lines = usage.replace(USAGE_PREFIX, "").split(`
|
|
394
|
+
`);
|
|
395
|
+
const first = lines[0];
|
|
396
|
+
const colon = first.indexOf(": ");
|
|
397
|
+
const prose = lines[lines.indexOf("") + 1] ?? "";
|
|
398
|
+
const fallback = lines.includes("") ? `${prose.charAt(0).toLowerCase()}${prose.slice(1)}`.replace(FULL_STOP, "") : "";
|
|
399
|
+
return {
|
|
400
|
+
synopsis: colon === -1 ? first : first.slice(0, colon),
|
|
401
|
+
summary: colon === -1 ? fallback : first.slice(colon + 2),
|
|
402
|
+
...replaces === undefined ? {} : { replaces },
|
|
403
|
+
examples: examples.map((example) => typeof example === "string" ? { line: example } : { line: example[0], note: example[1] })
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
function exampleRows(examples, colors = paint(process3.stdout)) {
|
|
407
|
+
if (examples.length === 0)
|
|
408
|
+
return [];
|
|
409
|
+
return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
|
|
410
|
+
}
|
|
411
|
+
function optionRows(options, colors = paint(process3.stdout)) {
|
|
412
|
+
const rows = Object.entries(options).map(([name, option]) => {
|
|
413
|
+
const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
|
|
414
|
+
const named = option.choices === undefined ? "<value>" : `<${option.choices.join("|")}>`;
|
|
415
|
+
const placeholder = option.optional === undefined ? named : `[${named}]`;
|
|
416
|
+
const value = option.type === "string" ? ` ${colors.dim(placeholder)}` : "";
|
|
417
|
+
return { flag: `${short}${colors.cyan(`--${name}`)}${value}`, width: `-x, --${name}`.length + (option.type === "string" ? placeholder.length + 1 : 0), description: option.description ?? "" };
|
|
418
|
+
});
|
|
419
|
+
const width = Math.max(0, ...rows.map((row) => row.width));
|
|
420
|
+
return rows.map((row) => row.description === "" ? ` ${row.flag}` : ` ${row.flag}${" ".repeat(width - row.width)} ${colors.dim(row.description)}`);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// ../kit/src/text.ts
|
|
424
|
+
var ANSI = new RegExp(`${String.fromCharCode(27)}\\[[\\d;?]*[a-z]`, "gi");
|
|
425
|
+
function distance(a, b) {
|
|
426
|
+
let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
|
|
427
|
+
for (const [i, left] of [...a].entries()) {
|
|
428
|
+
const current = [i + 1];
|
|
429
|
+
for (const [j, right] of [...b].entries())
|
|
430
|
+
current.push(Math.min((previous[j + 1] ?? 0) + 1, (current[j] ?? 0) + 1, (previous[j] ?? 0) + (left === right ? 0 : 1)));
|
|
431
|
+
previous = current;
|
|
432
|
+
}
|
|
433
|
+
return previous[b.length] ?? 0;
|
|
434
|
+
}
|
|
435
|
+
function closest(name, names) {
|
|
436
|
+
const started = names.find((known) => known.startsWith(name));
|
|
437
|
+
if (started !== undefined)
|
|
438
|
+
return started;
|
|
439
|
+
const [best] = names.map((known) => ({ known, far: distance(name, known) })).sort((a, b) => a.far - b.far);
|
|
440
|
+
return best !== undefined && best.far <= (name.length < 4 ? 1 : 2) ? best.known : undefined;
|
|
441
|
+
}
|
|
442
|
+
// ../kit/src/input.ts
|
|
443
|
+
var WHOLE = /^\d+$/;
|
|
444
|
+
function wholeNumber(value, name, fallback, min = 1) {
|
|
445
|
+
if (value === undefined)
|
|
446
|
+
return fallback;
|
|
447
|
+
const number = Number(value);
|
|
448
|
+
if (!WHOLE.test(value) || !Number.isSafeInteger(number) || number < min)
|
|
449
|
+
badUsage(`${name.startsWith("<") ? name : `--${name}`} takes a whole number from ${min}, not ${value}`);
|
|
450
|
+
return number;
|
|
451
|
+
}
|
|
452
|
+
var DURATION = /^(\d+(?:\.\d+)?)(ms|[smhd])?$/;
|
|
453
|
+
var UNIT_MS = { ms: 1, s: 1000, m: 60000, h: 3600000, d: 86400000 };
|
|
454
|
+
function duration(value, name, fallback, unit = "s") {
|
|
455
|
+
if (value === undefined)
|
|
456
|
+
return fallback;
|
|
457
|
+
const match = DURATION.exec(value.trim());
|
|
458
|
+
const ms = match === null ? 0 : Number(match[1]) * UNIT_MS[match[2] ?? unit];
|
|
459
|
+
if (ms < 1)
|
|
460
|
+
badUsage(`--${name} takes a duration such as 30s, 5m, 1h or 7d, not ${value}`);
|
|
461
|
+
return ms;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// ../kit/src/args.ts
|
|
465
|
+
var DASHES = /^-+/;
|
|
466
|
+
function jsonOption(holds) {
|
|
467
|
+
return { type: "boolean", description: holds === undefined ? "print JSON" : `print JSON: ${holds}` };
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
class Exit extends Error {
|
|
471
|
+
code;
|
|
472
|
+
hint;
|
|
473
|
+
constructor(message, code, hint) {
|
|
474
|
+
super(message);
|
|
475
|
+
this.code = code;
|
|
476
|
+
this.hint = hint;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
function fail(message, code = 2, hint) {
|
|
480
|
+
throw new Exit(message, code, hint);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
class UsageError extends Exit {
|
|
484
|
+
constructor(message, hint) {
|
|
485
|
+
super(message, 2, hint);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
function badUsage(message) {
|
|
489
|
+
throw new UsageError(message);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
class HelpShown extends Exit {
|
|
493
|
+
constructor() {
|
|
494
|
+
super("help shown", 0);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
class Completing {
|
|
499
|
+
options;
|
|
500
|
+
positionals;
|
|
501
|
+
candidates;
|
|
502
|
+
constructor(options, positionals = [], candidates = {}) {
|
|
503
|
+
this.options = options;
|
|
504
|
+
this.positionals = positionals;
|
|
505
|
+
this.candidates = candidates;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
var completion = { active: false };
|
|
509
|
+
var OUTPUT_OPTIONS = {
|
|
510
|
+
color: { type: "string", description: "auto (on a terminal alone), always or never" },
|
|
511
|
+
plain: { type: "boolean", description: "plain text, no colour or spinner: --color never" }
|
|
512
|
+
};
|
|
513
|
+
function applyOutput(color, plain, env = process4.env) {
|
|
514
|
+
const when = plain === true ? "never" : color ?? "auto";
|
|
515
|
+
if (when === "auto")
|
|
516
|
+
return;
|
|
517
|
+
if (when !== "always" && when !== "never")
|
|
518
|
+
badUsage(`--color takes auto, always or never, not ${when}`);
|
|
519
|
+
if (when === "always") {
|
|
520
|
+
env.FORCE_COLOR = "1";
|
|
521
|
+
delete env.NO_COLOR;
|
|
522
|
+
} else {
|
|
523
|
+
env.NO_COLOR = "1";
|
|
524
|
+
delete env.FORCE_COLOR;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
function parse(args, usage, options, settings = {}) {
|
|
528
|
+
const output = Object.fromEntries(Object.entries(OUTPUT_OPTIONS).filter(([name]) => !(name in options)));
|
|
529
|
+
const all = { ...options, ...output, help: { type: "boolean", short: "h", description: "show this help" } };
|
|
530
|
+
const listing = listed(all, settings.allowNegative === true);
|
|
531
|
+
if (completion.active)
|
|
532
|
+
throw new Completing(listing, settings.positionals, settings.candidates);
|
|
533
|
+
const { own, rest } = settings.trailing === true ? splitTrailing(args, all, settings.positionals?.length ?? 0) : splitRest(args, settings.rest === true);
|
|
534
|
+
let parsed;
|
|
535
|
+
try {
|
|
536
|
+
parsed = parseArgs({ args: joinDashValues(withBare(own, all), all), options: all, allowPositionals: true, allowNegative: settings.allowNegative === true, strict: true });
|
|
537
|
+
} catch (error) {
|
|
538
|
+
const message = parseError(error);
|
|
539
|
+
throw new UsageError(message, nearestOption(message, all, settings.allowNegative === true));
|
|
540
|
+
}
|
|
541
|
+
const values = parsed.values;
|
|
542
|
+
applyOutput("color" in output ? values.color : undefined, "plain" in output ? values.plain : undefined);
|
|
543
|
+
if (values.help === true) {
|
|
544
|
+
console.log(wantsHelpJson() ? JSON.stringify(description(usage, settings)) : helpText(usage, listing, undefined, settings));
|
|
545
|
+
throw new HelpShown;
|
|
546
|
+
}
|
|
547
|
+
checkChoices(parsed.values, all);
|
|
548
|
+
checkConflicts(parsed.values, all);
|
|
549
|
+
if (settings.positionals !== undefined)
|
|
550
|
+
checkPositionals(parsed.positionals, settings.positionals);
|
|
551
|
+
return { ...parsed, values: parsed.values, positionals: parsed.positionals, rest };
|
|
552
|
+
}
|
|
553
|
+
function splitRest(args, passes) {
|
|
554
|
+
const separator = passes ? args.indexOf("--") : -1;
|
|
555
|
+
return { own: separator === -1 ? args : args.slice(0, separator), rest: separator === -1 ? undefined : args.slice(separator + 1) };
|
|
556
|
+
}
|
|
557
|
+
function splitTrailing(args, options, count) {
|
|
558
|
+
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
559
|
+
let seen = 0;
|
|
560
|
+
if (count === 0)
|
|
561
|
+
return { own: args, rest: undefined };
|
|
562
|
+
for (let at = 0;at < args.length; at++) {
|
|
563
|
+
const word = args[at];
|
|
564
|
+
if (word === "--")
|
|
565
|
+
break;
|
|
566
|
+
if (word.startsWith("-")) {
|
|
567
|
+
if (takesValue(word))
|
|
568
|
+
at++;
|
|
569
|
+
continue;
|
|
570
|
+
}
|
|
571
|
+
seen++;
|
|
572
|
+
if (seen === count)
|
|
573
|
+
return { own: args.slice(0, at + 1), rest: args.slice(at + 1) };
|
|
574
|
+
}
|
|
575
|
+
return { own: args, rest: undefined };
|
|
576
|
+
}
|
|
577
|
+
function listed(options, allowNegative) {
|
|
578
|
+
if (!allowNegative)
|
|
579
|
+
return options;
|
|
580
|
+
return Object.fromEntries(Object.entries(options).flatMap(([name, option]) => {
|
|
581
|
+
if (option.type !== "boolean" || option.negative === undefined)
|
|
582
|
+
return [[name, option]];
|
|
583
|
+
return [...option.description === undefined ? [] : [[name, option]], [`no-${name}`, { type: "boolean", description: option.negative }]];
|
|
584
|
+
}));
|
|
585
|
+
}
|
|
586
|
+
function withBare(args, options) {
|
|
587
|
+
const bare = new Map(Object.entries(options).flatMap(([name, { short, optional }]) => optional === undefined ? [] : [[`--${name}`, optional], ...short === undefined ? [] : [[`-${short}`, optional]]]));
|
|
588
|
+
if (bare.size === 0)
|
|
589
|
+
return args;
|
|
590
|
+
const separator = args.indexOf("--");
|
|
591
|
+
const own = separator === -1 ? args : args.slice(0, separator);
|
|
592
|
+
return [...own.flatMap((arg, index) => {
|
|
593
|
+
const optional = bare.get(arg);
|
|
594
|
+
const next = own[index + 1];
|
|
595
|
+
const value = next !== undefined && (optional?.accepts === undefined ? !next.startsWith("-") : optional.accepts.test(next));
|
|
596
|
+
return optional === undefined || value ? [arg] : [arg, optional.bare];
|
|
597
|
+
}), ...separator === -1 ? [] : args.slice(separator)];
|
|
598
|
+
}
|
|
599
|
+
function checkConflicts(values, options) {
|
|
600
|
+
const given = (name) => values[name] !== undefined && values[name] !== false;
|
|
601
|
+
for (const [name, { conflicts = [] }] of Object.entries(options)) {
|
|
602
|
+
const other = given(name) ? conflicts.find(given) : undefined;
|
|
603
|
+
if (other !== undefined)
|
|
604
|
+
badUsage(`--${name} cannot be used with --${other}`);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
function checkChoices(values, options) {
|
|
608
|
+
for (const [name, option] of Object.entries(options)) {
|
|
609
|
+
if (option.choices === undefined)
|
|
610
|
+
continue;
|
|
611
|
+
const given = values[name];
|
|
612
|
+
for (const value of Array.isArray(given) ? given : [given]) {
|
|
613
|
+
if (typeof value === "string")
|
|
614
|
+
oneOf(value, name, option.choices);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
function oneOf(value, name, choices) {
|
|
619
|
+
if (!choices.includes(value))
|
|
620
|
+
badUsage(`${name.startsWith("<") ? name : `--${name}`} takes one of ${choices.join(", ")}, not ${value}`);
|
|
621
|
+
return value;
|
|
622
|
+
}
|
|
623
|
+
function checkPositionals(given, names) {
|
|
624
|
+
const variadic = names.at(-1)?.replace("]", "").endsWith("...") === true;
|
|
625
|
+
const required = names.filter((name) => !name.startsWith("[")).length;
|
|
626
|
+
if (given.length < required)
|
|
627
|
+
badUsage(`missing ${names[given.length]}`);
|
|
628
|
+
if (!variadic && given.length > names.length)
|
|
629
|
+
badUsage(`unexpected argument ${given[names.length]}`);
|
|
630
|
+
}
|
|
631
|
+
function joinDashValues(args, options) {
|
|
632
|
+
const longOf = new Map(Object.entries(options).flatMap(([name, option]) => option.short === undefined ? [[`--${name}`, name]] : [[`--${name}`, name], [`-${option.short}`, name]]));
|
|
633
|
+
const isOption = (arg) => arg === "--" || longOf.has(arg.includes("=") ? arg.slice(0, arg.indexOf("=")) : arg);
|
|
634
|
+
const joined = [];
|
|
635
|
+
for (let i = 0;i < args.length; i++) {
|
|
636
|
+
const arg = args[i] ?? "";
|
|
637
|
+
if (arg === "--")
|
|
638
|
+
return [...joined, ...args.slice(i)];
|
|
639
|
+
const name = longOf.get(arg);
|
|
640
|
+
const next = args[i + 1];
|
|
641
|
+
if (name !== undefined && options[name]?.type === "string" && next !== undefined && next.startsWith("-") && next !== "-" && !isOption(next)) {
|
|
642
|
+
joined.push(`--${name}=${next}`);
|
|
643
|
+
i++;
|
|
644
|
+
} else {
|
|
645
|
+
joined.push(arg);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
return joined;
|
|
649
|
+
}
|
|
650
|
+
function parseError(error) {
|
|
651
|
+
const message = upTo(error instanceof Error ? error.message : String(error), ". ");
|
|
652
|
+
return `${message.charAt(0).toLowerCase()}${message.slice(1)}`;
|
|
653
|
+
}
|
|
654
|
+
var UNKNOWN_OPTION = /^unknown option '(-[^']+)'/;
|
|
655
|
+
function nearestOption(message, options, allowNegative) {
|
|
656
|
+
const typed = UNKNOWN_OPTION.exec(message)?.[1];
|
|
657
|
+
if (typed === undefined)
|
|
658
|
+
return;
|
|
659
|
+
const word = typed.replace(DASHES, "");
|
|
660
|
+
if (word.length < 2 && !typed.startsWith("--"))
|
|
661
|
+
return;
|
|
662
|
+
const known = Object.entries(options).flatMap(([name, option]) => [
|
|
663
|
+
...option.short === undefined ? [] : [`-${option.short}`],
|
|
664
|
+
`--${name}`,
|
|
665
|
+
...allowNegative && option.type === "boolean" ? [`--no-${name}`] : []
|
|
666
|
+
]);
|
|
667
|
+
const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
|
|
668
|
+
return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
|
|
669
|
+
}
|
|
670
|
+
// ../kit/src/progress.ts
|
|
671
|
+
import process6 from "process";
|
|
672
|
+
|
|
673
|
+
// ../kit/src/process.ts
|
|
674
|
+
import { constants } from "os";
|
|
675
|
+
import process5 from "process";
|
|
676
|
+
var SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
677
|
+
function exitCodeOf(code, signal) {
|
|
678
|
+
if (code !== null)
|
|
679
|
+
return code;
|
|
680
|
+
if (typeof signal === "number")
|
|
681
|
+
return 128 + signal;
|
|
682
|
+
return signal === null ? 129 : 128 + constants.signals[signal];
|
|
683
|
+
}
|
|
684
|
+
function holdSignals(cleanup) {
|
|
685
|
+
let received;
|
|
686
|
+
let child;
|
|
687
|
+
const held = {
|
|
688
|
+
get signal() {
|
|
689
|
+
return received;
|
|
690
|
+
},
|
|
691
|
+
forward: (to) => {
|
|
692
|
+
child = to;
|
|
693
|
+
},
|
|
694
|
+
release: () => {
|
|
695
|
+
for (const signal of SIGNALS)
|
|
696
|
+
process5.off(signal, onSignal);
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
function onSignal(signal) {
|
|
700
|
+
received = signal;
|
|
701
|
+
if (child !== undefined) {
|
|
702
|
+
child.kill(signal);
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
if (cleanup === undefined)
|
|
706
|
+
return;
|
|
707
|
+
held.release();
|
|
708
|
+
cleanup(signal);
|
|
709
|
+
process5.kill(process5.pid, signal);
|
|
710
|
+
}
|
|
711
|
+
for (const signal of SIGNALS)
|
|
712
|
+
process5.on(signal, onSignal);
|
|
713
|
+
return held;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
// ../kit/src/progress.ts
|
|
717
|
+
var FRAMES = ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"];
|
|
718
|
+
var FRAME_MS = 200;
|
|
719
|
+
var CLEAR_LINE = "\r\x1B[2K";
|
|
720
|
+
var LINE_UP = "\x1B[1A\x1B[2K";
|
|
721
|
+
var ESC = String.fromCharCode(27);
|
|
722
|
+
var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x7E]|\\][^\\u0007${ESC}]*(?:\\u0007|${ESC}\\\\))`, "y");
|
|
723
|
+
var RESET_STYLE = `${ESC}[0m`;
|
|
724
|
+
var CTRL_C = 3;
|
|
725
|
+
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
726
|
+
var CONSOLE_METHODS = ["log", "info", "warn", "error", "debug", "dir", "dirxml", "table", "trace", "group", "groupCollapsed", "count", "timeEnd", "timeLog", "assert"];
|
|
727
|
+
function liveRegion(render, stream = process6.stderr, env = process6.env) {
|
|
728
|
+
if (!takesControlCodes(stream, env))
|
|
729
|
+
return { redraw: () => {
|
|
730
|
+
return;
|
|
731
|
+
}, print: (text) => void stream.write(`${text}
|
|
732
|
+
`), stop: () => {
|
|
733
|
+
return;
|
|
734
|
+
} };
|
|
735
|
+
const write = stream.write.bind(stream);
|
|
736
|
+
let frame = 0;
|
|
737
|
+
let drawn = 0;
|
|
738
|
+
const clear = () => {
|
|
739
|
+
if (drawn > 0)
|
|
740
|
+
write(`${CLEAR_LINE}${LINE_UP.repeat(drawn - 1)}`);
|
|
741
|
+
drawn = 0;
|
|
742
|
+
};
|
|
743
|
+
const draw = () => {
|
|
744
|
+
const width = (stream.columns === undefined || stream.columns === 0 ? 80 : stream.columns) - 1;
|
|
745
|
+
const height = stream.rows === undefined || stream.rows === 0 ? 24 : stream.rows;
|
|
746
|
+
const lines = render(FRAMES[frame++ % FRAMES.length]).slice(0, Math.max(1, height - 1)).map((line) => cutToWidth(line, width));
|
|
747
|
+
if (lines.length === 0) {
|
|
748
|
+
clear();
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
751
|
+
write(`${drawn > 0 ? `${CLEAR_LINE}${LINE_UP.repeat(drawn - 1)}` : CLEAR_LINE}${lines.join(`
|
|
752
|
+
`)}`);
|
|
753
|
+
drawn = lines.length;
|
|
754
|
+
};
|
|
755
|
+
const timer = setInterval(draw, FRAME_MS);
|
|
756
|
+
const restore = interceptOutput(clear);
|
|
757
|
+
const release = swallowInput();
|
|
758
|
+
const onExit = () => {
|
|
759
|
+
clear();
|
|
760
|
+
release();
|
|
761
|
+
};
|
|
762
|
+
const signals = holdSignals(stop);
|
|
763
|
+
function stop() {
|
|
764
|
+
clearInterval(timer);
|
|
765
|
+
clear();
|
|
766
|
+
restore();
|
|
767
|
+
release();
|
|
768
|
+
process6.off("exit", onExit);
|
|
769
|
+
signals.release();
|
|
770
|
+
}
|
|
771
|
+
timer.unref();
|
|
772
|
+
process6.on("exit", onExit);
|
|
773
|
+
return {
|
|
774
|
+
redraw: draw,
|
|
775
|
+
print: (text) => {
|
|
776
|
+
clear();
|
|
777
|
+
write(`${text}
|
|
778
|
+
`);
|
|
779
|
+
draw();
|
|
780
|
+
},
|
|
781
|
+
stop
|
|
782
|
+
};
|
|
783
|
+
}
|
|
784
|
+
function cutToWidth(line, width) {
|
|
785
|
+
if (Bun.stringWidth(line) <= width)
|
|
786
|
+
return line;
|
|
787
|
+
let room = width;
|
|
788
|
+
let kept = "";
|
|
789
|
+
let index = 0;
|
|
790
|
+
let linked = false;
|
|
791
|
+
while (index < line.length) {
|
|
792
|
+
ESCAPE_CODE.lastIndex = index;
|
|
793
|
+
const escape = ESCAPE_CODE.exec(line);
|
|
794
|
+
if (escape !== null) {
|
|
795
|
+
kept += escape[0];
|
|
796
|
+
linked ||= escape[0].startsWith(`${ESC}]`);
|
|
797
|
+
index = ESCAPE_CODE.lastIndex;
|
|
798
|
+
continue;
|
|
799
|
+
}
|
|
800
|
+
const char = String.fromCodePoint(line.codePointAt(index));
|
|
801
|
+
const columns = Bun.stringWidth(char);
|
|
802
|
+
if (columns > room)
|
|
803
|
+
break;
|
|
804
|
+
kept += char;
|
|
805
|
+
room -= columns;
|
|
806
|
+
index += char.length;
|
|
807
|
+
}
|
|
808
|
+
return `${kept}${RESET_STYLE}${linked ? CLOSE_LINK : ""}`;
|
|
809
|
+
}
|
|
810
|
+
function swallowInput() {
|
|
811
|
+
const input = process6.stdin;
|
|
812
|
+
if (!input.isTTY)
|
|
813
|
+
return () => {
|
|
814
|
+
return;
|
|
815
|
+
};
|
|
816
|
+
const onData = (data) => {
|
|
817
|
+
if (data.includes(CTRL_C))
|
|
818
|
+
process6.kill(process6.pid, "SIGINT");
|
|
819
|
+
};
|
|
820
|
+
input.setRawMode(true);
|
|
821
|
+
input.on("data", onData);
|
|
822
|
+
input.resume();
|
|
823
|
+
let released = false;
|
|
824
|
+
return () => {
|
|
825
|
+
if (released)
|
|
826
|
+
return;
|
|
827
|
+
released = true;
|
|
828
|
+
input.off("data", onData);
|
|
829
|
+
input.pause();
|
|
830
|
+
input.setRawMode(false);
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
function interceptOutput(clear) {
|
|
834
|
+
const targets = [...CONSOLE_METHODS.map((name) => [console, name]), [process6.stdout, "write"], [process6.stderr, "write"]];
|
|
835
|
+
const wrapped = targets.map(([target, name]) => {
|
|
836
|
+
const original = Reflect.get(target, name);
|
|
837
|
+
const wrapper = (...args) => {
|
|
838
|
+
clear();
|
|
839
|
+
return original.apply(target, args);
|
|
840
|
+
};
|
|
841
|
+
Reflect.set(target, name, wrapper);
|
|
842
|
+
return { target, name, original, wrapper };
|
|
843
|
+
});
|
|
844
|
+
return () => {
|
|
845
|
+
for (const { target, name, original, wrapper } of wrapped) {
|
|
846
|
+
if (Reflect.get(target, name) === wrapper)
|
|
847
|
+
Reflect.set(target, name, original);
|
|
848
|
+
}
|
|
849
|
+
};
|
|
850
|
+
}
|
|
851
|
+
// ../kit/src/cli.ts
|
|
852
|
+
import process9 from "process";
|
|
853
|
+
|
|
854
|
+
// ../kit/src/usage.ts
|
|
855
|
+
import { appendFileSync, mkdirSync } from "fs";
|
|
856
|
+
import { homedir as homedir2 } from "os";
|
|
857
|
+
import { dirname, isAbsolute, join as join2 } from "path";
|
|
858
|
+
import process8 from "process";
|
|
859
|
+
|
|
860
|
+
// ../kit/src/logs.ts
|
|
861
|
+
import { lstatSync, openSync } from "fs";
|
|
862
|
+
import { mkdir, mkdtemp, readdir, rm, stat } from "fs/promises";
|
|
863
|
+
import { homedir, tmpdir } from "os";
|
|
864
|
+
import { join } from "path";
|
|
865
|
+
import process7 from "process";
|
|
866
|
+
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
867
|
+
var CLI_FOLDER = "kaynooo-cli";
|
|
868
|
+
function isPrivateDirectory(path) {
|
|
869
|
+
try {
|
|
870
|
+
const info = lstatSync(path, { throwIfNoEntry: false });
|
|
871
|
+
return info !== undefined && info.isDirectory() && info.uid === (process7.getuid?.() ?? info.uid) && (info.mode & 63) === 0;
|
|
872
|
+
} catch {
|
|
873
|
+
return false;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
async function privateLogDirectory(name, env = process7.env) {
|
|
877
|
+
const runtime = env.XDG_RUNTIME_DIR;
|
|
878
|
+
const base = runtime === undefined || runtime === "" ? join(homedir(), ".cache", CLI_FOLDER) : join(runtime, CLI_FOLDER);
|
|
879
|
+
const path = join(base, name);
|
|
880
|
+
try {
|
|
881
|
+
await mkdir(path, { recursive: true, mode: 448 });
|
|
882
|
+
if (isPrivateDirectory(path))
|
|
883
|
+
return path;
|
|
884
|
+
} catch {}
|
|
885
|
+
return mkdtemp(join(tmpdir(), `${name.replaceAll("/", "-")}-`));
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
// ../kit/src/usage.ts
|
|
889
|
+
var UNKNOWN_OPTION2 = /unknown option '(--[a-z][a-z0-9-]*)[=']/;
|
|
890
|
+
var FLAG_NAME = /^(?:--[a-z][a-z0-9-]*|-[a-zA-Z])$/;
|
|
891
|
+
var LONGEST_NAME = 40;
|
|
892
|
+
var usageError;
|
|
893
|
+
function noteUsageError(message) {
|
|
894
|
+
usageError = message;
|
|
895
|
+
}
|
|
896
|
+
function stateHome(env = process8.env) {
|
|
897
|
+
const dir = env.XDG_STATE_HOME;
|
|
898
|
+
return dir !== undefined && isAbsolute(dir) ? dir : join2(homedir2(), ".local", "state");
|
|
899
|
+
}
|
|
900
|
+
function usagePath(env = process8.env) {
|
|
901
|
+
return join2(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
902
|
+
}
|
|
903
|
+
function usageRecorded(env = process8.env) {
|
|
904
|
+
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
905
|
+
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
906
|
+
}
|
|
907
|
+
function flagNames(argv) {
|
|
908
|
+
const end = argv.indexOf("--");
|
|
909
|
+
const names = (end === -1 ? argv : argv.slice(0, end)).filter((word) => word.startsWith("-") && word !== "-").map((word) => word.includes("=") ? word.slice(0, word.indexOf("=")) : word);
|
|
910
|
+
return [...new Set(names)].filter((name) => name.length <= LONGEST_NAME && FLAG_NAME.test(name));
|
|
911
|
+
}
|
|
912
|
+
function usageEntry(bin, command, argv, code, now = new Date) {
|
|
913
|
+
const unknown = code === 2 && usageError !== undefined ? UNKNOWN_OPTION2.exec(usageError)?.[1] : undefined;
|
|
914
|
+
usageError = undefined;
|
|
915
|
+
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
916
|
+
}
|
|
917
|
+
function recordUsage(entry, env = process8.env) {
|
|
918
|
+
if (!usageRecorded(env))
|
|
919
|
+
return;
|
|
920
|
+
try {
|
|
921
|
+
const path = usagePath(env);
|
|
922
|
+
mkdirSync(dirname(path), { recursive: true, mode: 448 });
|
|
923
|
+
appendFileSync(path, `${JSON.stringify(entry)}
|
|
924
|
+
`, { mode: 384 });
|
|
925
|
+
} catch {}
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
// ../kit/src/cli.ts
|
|
929
|
+
var LEVEL_OPTIONS = {
|
|
930
|
+
...OUTPUT_OPTIONS,
|
|
931
|
+
help: { type: "boolean", short: "h", description: "show this help" }
|
|
932
|
+
};
|
|
933
|
+
var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
|
|
934
|
+
function printError(prefix, message, hint, cause) {
|
|
935
|
+
const colors = paint(process9.stderr);
|
|
936
|
+
const styled = takesControlCodes(process9.stderr);
|
|
937
|
+
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
938
|
+
const under = (label, text, tint) => text === undefined ? "" : `
|
|
939
|
+
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
940
|
+
console.error(`${head} ${message}${under("cause", cause, colors.dim)}${under("hint", hint, colors.yellow)}`);
|
|
941
|
+
}
|
|
942
|
+
function printJsonError(message, hint, cause) {
|
|
943
|
+
console.error(JSON.stringify({ error: message, ...hint === undefined ? {} : { hint }, ...cause === undefined ? {} : { cause } }));
|
|
944
|
+
}
|
|
945
|
+
function wantsJson(argv) {
|
|
946
|
+
const end = argv.indexOf("--");
|
|
947
|
+
return (end === -1 ? argv : argv.slice(0, end)).includes("--json");
|
|
948
|
+
}
|
|
949
|
+
function reportThrown(prefix, error, redact = (text) => text, json = false) {
|
|
950
|
+
if (error instanceof HelpShown)
|
|
951
|
+
return 0;
|
|
952
|
+
const help = error instanceof UsageError ? `run ${prefix} --help for its options` : undefined;
|
|
953
|
+
const named = error instanceof Error && !(error instanceof Exit) && error.name !== "Error" ? `${error.name}: ${error.message}` : undefined;
|
|
954
|
+
const cause = error instanceof Error && error.cause instanceof Error ? redact(error.cause.message) : undefined;
|
|
955
|
+
if (error instanceof UsageError)
|
|
956
|
+
noteUsageError(error.message);
|
|
957
|
+
const report = json ? printJsonError : (message, hint, causeText) => printError(prefix, message, hint, causeText);
|
|
958
|
+
report(redact(named ?? (error instanceof Error ? error.message : String(error))), error instanceof Exit ? error.hint ?? help : undefined, cause);
|
|
959
|
+
return error instanceof Exit ? error.code : 1;
|
|
960
|
+
}
|
|
961
|
+
var COMPLETE = "__complete";
|
|
962
|
+
var FILES = "__files__";
|
|
963
|
+
async function completingOf(run) {
|
|
964
|
+
completion.active = true;
|
|
965
|
+
try {
|
|
966
|
+
await run([]);
|
|
967
|
+
} catch (error) {
|
|
968
|
+
if (error instanceof Completing)
|
|
969
|
+
return error;
|
|
970
|
+
} finally {
|
|
971
|
+
completion.active = false;
|
|
972
|
+
}
|
|
973
|
+
return new Completing({});
|
|
974
|
+
}
|
|
975
|
+
function candidateLine(candidate) {
|
|
976
|
+
return typeof candidate === "string" ? candidate : candidate.join("\t");
|
|
977
|
+
}
|
|
978
|
+
function positionalBeingTyped(before, { options, positionals }) {
|
|
979
|
+
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
980
|
+
let index = 0;
|
|
981
|
+
let at = 0;
|
|
982
|
+
while (at < before.length) {
|
|
983
|
+
const word = before[at];
|
|
984
|
+
if (word === "--") {
|
|
985
|
+
index += before.length - at - 1;
|
|
986
|
+
break;
|
|
987
|
+
}
|
|
988
|
+
if (!word.startsWith("-"))
|
|
989
|
+
index++;
|
|
990
|
+
else if (takesValue(word))
|
|
991
|
+
at++;
|
|
992
|
+
at++;
|
|
993
|
+
}
|
|
994
|
+
const last = positionals.at(-1);
|
|
995
|
+
if (index < positionals.length)
|
|
996
|
+
return positionals[index];
|
|
997
|
+
return last?.replace("]", "").endsWith("...") === true ? last : undefined;
|
|
998
|
+
}
|
|
999
|
+
async function leafCandidates(run, words) {
|
|
1000
|
+
const completing = await completingOf(run);
|
|
1001
|
+
if ((words.at(-1) ?? "").startsWith("-"))
|
|
1002
|
+
return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
|
|
1003
|
+
const flag = words.at(-2);
|
|
1004
|
+
const option = flag?.startsWith("-") === true ? Object.entries(completing.options).find(([name, { short }]) => flag === `--${name}` || flag === `-${short}`)?.[1] : undefined;
|
|
1005
|
+
if (option?.type === "string") {
|
|
1006
|
+
const values = option.choices === undefined ? await option.candidates?.() : option.choices;
|
|
1007
|
+
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
1008
|
+
}
|
|
1009
|
+
const name = positionalBeingTyped(words.slice(0, -1), completing);
|
|
1010
|
+
const values = name === undefined ? undefined : await completing.candidates[name]?.();
|
|
1011
|
+
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
1012
|
+
}
|
|
1013
|
+
async function printCandidates(found) {
|
|
1014
|
+
for (const line of await found)
|
|
1015
|
+
console.log(line);
|
|
1016
|
+
return 0;
|
|
1017
|
+
}
|
|
1018
|
+
async function runMain({ name, version, run, redact, usage }, argv = process9.argv.slice(2)) {
|
|
1019
|
+
if (argv[0] === COMPLETE)
|
|
1020
|
+
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
1021
|
+
if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
|
|
1022
|
+
console.log(version);
|
|
1023
|
+
return 0;
|
|
1024
|
+
}
|
|
1025
|
+
let code;
|
|
1026
|
+
try {
|
|
1027
|
+
code = await run(argv);
|
|
1028
|
+
} catch (error) {
|
|
1029
|
+
code = reportThrown(name, error, redact, wantsJson(argv));
|
|
1030
|
+
}
|
|
1031
|
+
if (usage !== false)
|
|
1032
|
+
recordUsage(usageEntry(name, undefined, argv, code));
|
|
1033
|
+
return code;
|
|
1034
|
+
}
|
|
1035
|
+
// ../kit/src/secrets.ts
|
|
1036
|
+
var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "session", "jwt", "ssn", "iban", "card", "cvv", "otp"]);
|
|
1037
|
+
// ../kit/src/sessions.ts
|
|
1038
|
+
import { homedir as homedir3 } from "os";
|
|
1039
|
+
import { join as join3 } from "path";
|
|
1040
|
+
import process10 from "process";
|
|
1041
|
+
var {Glob } = globalThis.Bun;
|
|
1042
|
+
var SESSION_FILES = new Glob("*.json");
|
|
1043
|
+
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
1044
|
+
async function sessionDirs() {
|
|
1045
|
+
const home = homedir3();
|
|
1046
|
+
const dirs = new Set([join3(home, ".claude", "sessions")]);
|
|
1047
|
+
if (process10.env.CLAUDE_CONFIG_DIR !== undefined)
|
|
1048
|
+
dirs.add(join3(process10.env.CLAUDE_CONFIG_DIR, "sessions"));
|
|
1049
|
+
try {
|
|
1050
|
+
for await (const dir of ACCOUNT_DIRS.scan({ cwd: join3(home, ".claude-accounts"), onlyFiles: false, absolute: true }))
|
|
1051
|
+
dirs.add(dir);
|
|
1052
|
+
} catch {}
|
|
1053
|
+
return [...dirs];
|
|
1054
|
+
}
|
|
1055
|
+
async function procStat(pid) {
|
|
1056
|
+
try {
|
|
1057
|
+
const stat = await Bun.file(`/proc/${pid}/stat`).text();
|
|
1058
|
+
return stat.slice(stat.lastIndexOf(")") + 2).split(" ");
|
|
1059
|
+
} catch {
|
|
1060
|
+
return;
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
async function isAlive(pid, procStart) {
|
|
1064
|
+
const stat = await procStat(pid);
|
|
1065
|
+
if (stat === undefined)
|
|
1066
|
+
return false;
|
|
1067
|
+
return procStart === undefined || stat[19] === procStart;
|
|
1068
|
+
}
|
|
1069
|
+
async function liveSessions() {
|
|
1070
|
+
const sessions = new Map;
|
|
1071
|
+
for (const dir of await sessionDirs()) {
|
|
1072
|
+
let files;
|
|
1073
|
+
try {
|
|
1074
|
+
files = await Array.fromAsync(SESSION_FILES.scan({ cwd: dir, absolute: true }));
|
|
1075
|
+
} catch {
|
|
1076
|
+
continue;
|
|
1077
|
+
}
|
|
1078
|
+
for (const file of files) {
|
|
1079
|
+
let entry;
|
|
1080
|
+
try {
|
|
1081
|
+
entry = await Bun.file(file).json();
|
|
1082
|
+
} catch {
|
|
1083
|
+
continue;
|
|
1084
|
+
}
|
|
1085
|
+
if (entry.name === undefined || entry.pid === undefined || sessions.has(entry.pid) || !await isAlive(entry.pid, entry.procStart))
|
|
1086
|
+
continue;
|
|
1087
|
+
sessions.set(entry.pid, { name: entry.name, pid: entry.pid, status: entry.status ?? "unknown", cwd: entry.cwd ?? "" });
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
return [...sessions.values()];
|
|
1091
|
+
}
|
|
1092
|
+
async function currentSession(sessions) {
|
|
1093
|
+
const byPid = new Map(sessions.map((session) => [session.pid, session]));
|
|
1094
|
+
let pid = process10.ppid;
|
|
1095
|
+
for (let depth = 0;depth < 32 && pid > 1; depth++) {
|
|
1096
|
+
const session = byPid.get(pid);
|
|
1097
|
+
if (session !== undefined)
|
|
1098
|
+
return session;
|
|
1099
|
+
const parent = Number((await procStat(pid))?.[1]);
|
|
1100
|
+
if (!Number.isInteger(parent))
|
|
1101
|
+
return;
|
|
1102
|
+
pid = parent;
|
|
1103
|
+
}
|
|
1104
|
+
return;
|
|
1105
|
+
}
|
|
1106
|
+
// package.json
|
|
1107
|
+
var version = "0.5.1";
|
|
1108
|
+
|
|
1109
|
+
// src/conditions.ts
|
|
1110
|
+
import { existsSync, statSync } from "fs";
|
|
1111
|
+
var TAIL = 20;
|
|
1112
|
+
var AROUND = 5;
|
|
1113
|
+
var CARRIAGE_RETURN = /\r$/;
|
|
1114
|
+
var SLICE_BYTES = 1024 * 1024;
|
|
1115
|
+
var MAX_LINE = 1e4;
|
|
1116
|
+
var PORT_TIMEOUT_MS = 3000;
|
|
1117
|
+
|
|
1118
|
+
class FileWatch {
|
|
1119
|
+
path;
|
|
1120
|
+
until;
|
|
1121
|
+
failOn;
|
|
1122
|
+
offset = 0;
|
|
1123
|
+
decoder = new TextDecoder;
|
|
1124
|
+
partial = "";
|
|
1125
|
+
identity;
|
|
1126
|
+
number = 0;
|
|
1127
|
+
recent = [];
|
|
1128
|
+
failure;
|
|
1129
|
+
matched = false;
|
|
1130
|
+
constructor(path, until, failOn) {
|
|
1131
|
+
this.path = path;
|
|
1132
|
+
this.until = until;
|
|
1133
|
+
this.failOn = failOn;
|
|
1134
|
+
}
|
|
1135
|
+
get done() {
|
|
1136
|
+
return this.until === undefined || this.matched;
|
|
1137
|
+
}
|
|
1138
|
+
get failing() {
|
|
1139
|
+
return this.failure !== undefined;
|
|
1140
|
+
}
|
|
1141
|
+
get exists() {
|
|
1142
|
+
return existsSync(this.path);
|
|
1143
|
+
}
|
|
1144
|
+
async poll() {
|
|
1145
|
+
const events = [];
|
|
1146
|
+
for (let lines = await this.nextLines();lines !== undefined; lines = await this.nextLines()) {
|
|
1147
|
+
for (const line of lines) {
|
|
1148
|
+
if (this.failure !== undefined) {
|
|
1149
|
+
if (this.failure.after.length < AROUND)
|
|
1150
|
+
this.failure.after.push(line);
|
|
1151
|
+
continue;
|
|
1152
|
+
}
|
|
1153
|
+
if (this.failOn?.test(line.text) === true) {
|
|
1154
|
+
this.failure = { before: this.recent.slice(-AROUND), line, after: [], waited: false };
|
|
1155
|
+
continue;
|
|
1156
|
+
}
|
|
1157
|
+
this.recent.push(line);
|
|
1158
|
+
if (this.recent.length > TAIL)
|
|
1159
|
+
this.recent.shift();
|
|
1160
|
+
if (!this.matched && this.until?.test(line.text) === true) {
|
|
1161
|
+
this.matched = true;
|
|
1162
|
+
events.push({ kind: "until", line });
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
if (this.failure !== undefined && !this.failure.waited && this.failure.after.length < AROUND)
|
|
1167
|
+
this.failure.waited = true;
|
|
1168
|
+
else if (this.failure !== undefined)
|
|
1169
|
+
events.push({ kind: "fail", line: this.failure.line, excerpt: [...this.failure.before, this.failure.line, ...this.failure.after] });
|
|
1170
|
+
return events;
|
|
1171
|
+
}
|
|
1172
|
+
excerpt() {
|
|
1173
|
+
return this.recent;
|
|
1174
|
+
}
|
|
1175
|
+
async nextLines() {
|
|
1176
|
+
if (!existsSync(this.path))
|
|
1177
|
+
return;
|
|
1178
|
+
const { size, dev, ino } = statSync(this.path);
|
|
1179
|
+
const replaced = this.identity !== undefined && (this.identity.dev !== dev || this.identity.ino !== ino);
|
|
1180
|
+
this.identity = { dev, ino };
|
|
1181
|
+
if (size < this.offset || replaced) {
|
|
1182
|
+
this.offset = 0;
|
|
1183
|
+
this.decoder = new TextDecoder;
|
|
1184
|
+
this.partial = "";
|
|
1185
|
+
this.number = 0;
|
|
1186
|
+
this.recent = [];
|
|
1187
|
+
}
|
|
1188
|
+
if (size === this.offset)
|
|
1189
|
+
return;
|
|
1190
|
+
const end = Math.min(size, this.offset + SLICE_BYTES);
|
|
1191
|
+
const chunk = this.decoder.decode(await Bun.file(this.path).slice(this.offset, end).bytes(), { stream: true });
|
|
1192
|
+
this.offset = end;
|
|
1193
|
+
const pieces = (this.partial + chunk).split(`
|
|
1194
|
+
`);
|
|
1195
|
+
this.partial = (pieces.pop() ?? "").slice(0, MAX_LINE);
|
|
1196
|
+
return pieces.map((text) => ({ number: ++this.number, text: Bun.stripANSI(text.slice(0, MAX_LINE).replace(CARRIAGE_RETURN, "")) }));
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
async function portProblem(host, port, timeoutMs = PORT_TIMEOUT_MS) {
|
|
1200
|
+
const attempt = Bun.connect({ hostname: host, port, socket: { data: (socket) => void socket.end() } });
|
|
1201
|
+
let timer;
|
|
1202
|
+
try {
|
|
1203
|
+
const socket = await Promise.race([attempt, new Promise((resolve) => {
|
|
1204
|
+
timer = setTimeout(resolve, timeoutMs, "timeout");
|
|
1205
|
+
})]);
|
|
1206
|
+
if (socket === "timeout") {
|
|
1207
|
+
closeWhenConnected(attempt);
|
|
1208
|
+
return "ETIMEDOUT";
|
|
1209
|
+
}
|
|
1210
|
+
socket.end();
|
|
1211
|
+
return;
|
|
1212
|
+
} catch (error) {
|
|
1213
|
+
return error instanceof Error ? error.code ?? error.message : String(error);
|
|
1214
|
+
} finally {
|
|
1215
|
+
clearTimeout(timer);
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
async function closeWhenConnected(attempt) {
|
|
1219
|
+
try {
|
|
1220
|
+
(await attempt).end();
|
|
1221
|
+
} catch {}
|
|
1222
|
+
}
|
|
1223
|
+
async function urlCheck(url, timeout) {
|
|
1224
|
+
try {
|
|
1225
|
+
const response = await fetch(url, { signal: AbortSignal.timeout(timeout) });
|
|
1226
|
+
await response.body?.cancel();
|
|
1227
|
+
return response.ok ? { status: response.status } : { problem: `answered ${response.status}` };
|
|
1228
|
+
} catch (error) {
|
|
1229
|
+
return { problem: error instanceof Error ? error.message : String(error), isTimeout: error instanceof Error && error.name === "TimeoutError" };
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
// src/task.ts
|
|
1234
|
+
import { existsSync as existsSync2, lstatSync as lstatSync2, readFileSync, statSync as statSync2 } from "fs";
|
|
1235
|
+
import { join as join4 } from "path";
|
|
1236
|
+
import process12 from "process";
|
|
1237
|
+
var EXITED = /(?:^|\n)\[exited with code (-?\d+)\]\n*$/;
|
|
1238
|
+
var FAILURE = /\(fail\)|\berror:/i;
|
|
1239
|
+
var TASK_ID = /^[\w-]+$/;
|
|
1240
|
+
var TAIL_BYTES = 256;
|
|
1241
|
+
var LONGEST_FAILURES = 30;
|
|
1242
|
+
var CARRIAGE_RETURN2 = /\r$/;
|
|
1243
|
+
function tasksRoot(env) {
|
|
1244
|
+
return join4(env.CLAUDE_CODE_TMPDIR ?? env.TMPDIR ?? "/tmp", `claude-${process12.getuid?.() ?? 0}`);
|
|
1245
|
+
}
|
|
1246
|
+
function taskFile(task, env = process12.env) {
|
|
1247
|
+
if (task.includes("/") || task.endsWith(".output"))
|
|
1248
|
+
return task;
|
|
1249
|
+
if (!TASK_ID.test(task))
|
|
1250
|
+
return;
|
|
1251
|
+
const root = tasksRoot(env);
|
|
1252
|
+
const found = existsSync2(root) ? [...new Bun.Glob(`*/*/tasks/${task}.output`).scanSync({ cwd: root, onlyFiles: false })] : [];
|
|
1253
|
+
return found.length === 0 ? undefined : join4(root, found[0] ?? "");
|
|
1254
|
+
}
|
|
1255
|
+
function isTranscript(path) {
|
|
1256
|
+
return existsSync2(path) && lstatSync2(path).isSymbolicLink() && path.endsWith(".output");
|
|
1257
|
+
}
|
|
1258
|
+
async function exitCode(path) {
|
|
1259
|
+
if (!existsSync2(path))
|
|
1260
|
+
return;
|
|
1261
|
+
const size = statSync2(path).size;
|
|
1262
|
+
const found = EXITED.exec(await Bun.file(path).slice(Math.max(0, size - TAIL_BYTES), size).text());
|
|
1263
|
+
return found === null ? undefined : Number(found[1]);
|
|
1264
|
+
}
|
|
1265
|
+
async function taskEnd(path, lines) {
|
|
1266
|
+
if (!existsSync2(path))
|
|
1267
|
+
return { failures: [], tail: [] };
|
|
1268
|
+
const text = (await Bun.file(path).text()).replace(EXITED, "");
|
|
1269
|
+
const all = text.split(`
|
|
1270
|
+
`).map((line, i) => ({ number: i + 1, text: Bun.stripANSI(line.replace(CARRIAGE_RETURN2, "")) }));
|
|
1271
|
+
while (all.at(-1)?.text.trim() === "")
|
|
1272
|
+
all.pop();
|
|
1273
|
+
const tail = all.slice(-lines);
|
|
1274
|
+
const first = tail[0]?.number ?? Infinity;
|
|
1275
|
+
return { failures: all.filter((line) => line.number < first && FAILURE.test(line.text)).slice(-LONGEST_FAILURES), tail };
|
|
1276
|
+
}
|
|
1277
|
+
function pidAlive(pid) {
|
|
1278
|
+
try {
|
|
1279
|
+
process12.kill(pid, 0);
|
|
1280
|
+
} catch (error) {
|
|
1281
|
+
return error.code === "EPERM";
|
|
1282
|
+
}
|
|
1283
|
+
return !isZombie(pid);
|
|
1284
|
+
}
|
|
1285
|
+
function isZombie(pid) {
|
|
1286
|
+
try {
|
|
1287
|
+
const stat = readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
1288
|
+
return stat.slice(stat.lastIndexOf(")") + 2, stat.lastIndexOf(")") + 3) === "Z";
|
|
1289
|
+
} catch {
|
|
1290
|
+
return false;
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
// src/cli.ts
|
|
1295
|
+
var USAGE = `waitfor [options]: waits until a file shows a line, a port listens, a URL answers 2xx or a process exits
|
|
1296
|
+
waitfor task <task id | file>: waits for a Claude Code background task to end (waitfor task --help)
|
|
1297
|
+
|
|
1298
|
+
Every condition given must be met. A --fail line in --file ends the wait at once.
|
|
1299
|
+
Exits 0 once met, 1 on a --fail line, 2 on a timeout or a usage error, each with an excerpt of what it saw last.`;
|
|
1300
|
+
var TASK_USAGE = `waitfor task [options] <task id | file>: waits for a Claude Code background task to end, then prints its exit code, its last lines and the (fail) and error: lines above them
|
|
1301
|
+
|
|
1302
|
+
The task is the id a background Bash call returned, or the path of its .output file.
|
|
1303
|
+
Exits 0 when the task exited 0, 1 when it exited with another code, 2 on a timeout or a usage error.
|
|
1304
|
+
--json prints {task, path, code, ms, failures, tail}, each line as {number, text}.`;
|
|
1305
|
+
var TIMEOUT = { type: "string", short: "t", description: "how long before giving up (5m): 30s, 5m, 1h, a bare number in seconds" };
|
|
1306
|
+
var TASK_OPTIONS = {
|
|
1307
|
+
lines: { type: "string", short: "n", description: "how many of its last lines to print (20)" },
|
|
1308
|
+
timeout: TIMEOUT,
|
|
1309
|
+
json: jsonOption()
|
|
1310
|
+
};
|
|
1311
|
+
var OPTIONS = {
|
|
1312
|
+
file: { type: "string", short: "f", description: "a log file to read, from its start, waiting for it to exist" },
|
|
1313
|
+
until: { type: "string", short: "u", description: "a regular expression a line of --file must match" },
|
|
1314
|
+
fail: { type: "string", short: "e", description: "a regular expression whose first matching line of --file is a failure" },
|
|
1315
|
+
port: { type: "string", short: "p", description: "a TCP port that must accept connections" },
|
|
1316
|
+
host: { type: "string", short: "H", description: "the host of --port (127.0.0.1)" },
|
|
1317
|
+
url: { type: "string", description: "a URL that must answer 2xx" },
|
|
1318
|
+
pid: { type: "string", description: "a process id that must exit" },
|
|
1319
|
+
timeout: TIMEOUT
|
|
1320
|
+
};
|
|
1321
|
+
var POLL = 250;
|
|
1322
|
+
var URL_TIMEOUT = 5000;
|
|
1323
|
+
var WIDTH = 400;
|
|
1324
|
+
function regex(source, name) {
|
|
1325
|
+
if (source === undefined)
|
|
1326
|
+
return;
|
|
1327
|
+
try {
|
|
1328
|
+
return new RegExp(source);
|
|
1329
|
+
} catch (error) {
|
|
1330
|
+
fail(`--${name} is no valid regular expression: ${error instanceof Error ? error.message : String(error)}`);
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
function numbered(lines, marked) {
|
|
1334
|
+
const width = String(lines.at(-1)?.number ?? 0).length;
|
|
1335
|
+
return lines.map((line) => `${line === marked ? ">" : " "} ${String(line.number).padStart(width)} ${truncate(line.text, WIDTH)}`).join(`
|
|
1336
|
+
`);
|
|
1337
|
+
}
|
|
1338
|
+
async function waitTask(args) {
|
|
1339
|
+
const { values, positionals: [task] } = parse(args, TASK_USAGE, TASK_OPTIONS, { positionals: ["<task>"] });
|
|
1340
|
+
const path = taskFile(task);
|
|
1341
|
+
if (path === undefined)
|
|
1342
|
+
fail(`no background task ${task}: give the id a background Bash call returned, or the path of its .output file`);
|
|
1343
|
+
if (!existsSync3(path))
|
|
1344
|
+
fail(`no output file ${path}: ${task} is no background task of this machine`);
|
|
1345
|
+
if (isTranscript(path))
|
|
1346
|
+
fail(`${task} is a subagent, whose end its notification reports: waitfor task waits on a background Bash task`);
|
|
1347
|
+
const lines = wholeNumber(values.lines, "lines", 20);
|
|
1348
|
+
const timeout = duration(values.timeout, "timeout", 5 * 60000);
|
|
1349
|
+
const started = Date.now();
|
|
1350
|
+
const { dim } = paint(process13.stderr);
|
|
1351
|
+
const region = liveRegion((spinner) => [`${spinner} task ${task}: still running ${dim(formatElapsed(Date.now() - started))}`]);
|
|
1352
|
+
let code;
|
|
1353
|
+
try {
|
|
1354
|
+
for (;; ) {
|
|
1355
|
+
code = await exitCode(path);
|
|
1356
|
+
if (code !== undefined || Date.now() - started >= timeout)
|
|
1357
|
+
break;
|
|
1358
|
+
await Bun.sleep(POLL);
|
|
1359
|
+
}
|
|
1360
|
+
} finally {
|
|
1361
|
+
region.stop();
|
|
1362
|
+
}
|
|
1363
|
+
const { failures, tail } = await taskEnd(path, lines);
|
|
1364
|
+
const last = tail.length === 0 ? "" : `
|
|
1365
|
+
last lines of ${path}:
|
|
1366
|
+
${numbered(tail)}`;
|
|
1367
|
+
if (code === undefined)
|
|
1368
|
+
fail(`gave up after ${formatDuration(timeout)}: task ${task} is still running${last}`);
|
|
1369
|
+
if (values.json === true) {
|
|
1370
|
+
printJson({ task, path, code, ms: Date.now() - started, failures, tail });
|
|
1371
|
+
return code === 0 ? 0 : 1;
|
|
1372
|
+
}
|
|
1373
|
+
const failed = failures.length === 0 ? "" : `
|
|
1374
|
+
(fail) and error: lines above them:
|
|
1375
|
+
${numbered(failures)}`;
|
|
1376
|
+
console.log(`task ${task} exited with code ${code}, waited ${formatDuration(Date.now() - started)}${failed}${last}`);
|
|
1377
|
+
return code === 0 ? 0 : 1;
|
|
1378
|
+
}
|
|
1379
|
+
async function main(args) {
|
|
1380
|
+
if (args[0] === "task")
|
|
1381
|
+
return waitTask(args.slice(1));
|
|
1382
|
+
const parsed = parse(args, USAGE, OPTIONS, { replaces: "sleep loops, tail -f and until ! pgrep -f", examples: [['waitfor --file dev.log --until "ready in" --fail error', "a dev server up, or its first error"], ["waitfor --port 5432 -t 2m", "Postgres accepting connections"]] });
|
|
1383
|
+
const { values, positionals } = parsed;
|
|
1384
|
+
if (positionals.length > 0)
|
|
1385
|
+
fail(`unexpected argument ${positionals[0]}
|
|
1386
|
+
|
|
1387
|
+
${USAGE}`);
|
|
1388
|
+
if (values.until === undefined && values.port === undefined && values.url === undefined && values.pid === undefined)
|
|
1389
|
+
fail(`give --until with --file, --port, --url or --pid
|
|
1390
|
+
|
|
1391
|
+
${USAGE}`);
|
|
1392
|
+
if ((values.until !== undefined || values.fail !== undefined) !== (values.file !== undefined))
|
|
1393
|
+
fail("--until and --fail read the --file lines, and --file needs one of them");
|
|
1394
|
+
if (values.host !== undefined && values.port === undefined)
|
|
1395
|
+
fail("--host goes with --port");
|
|
1396
|
+
const port = values.port === undefined ? undefined : wholeNumber(values.port, "port", 0);
|
|
1397
|
+
if (port !== undefined && port > 65535)
|
|
1398
|
+
fail(`--port takes a port up to 65535, not ${port}`);
|
|
1399
|
+
if (values.url !== undefined && !URL.canParse(values.url))
|
|
1400
|
+
fail(`--url ${values.url} is no URL`);
|
|
1401
|
+
const pid = values.pid === undefined ? undefined : wholeNumber(values.pid, "pid", 0);
|
|
1402
|
+
const host = values.host ?? "127.0.0.1";
|
|
1403
|
+
const timeout = duration(values.timeout, "timeout", 5 * 60000);
|
|
1404
|
+
const watch = values.file === undefined ? undefined : new FileWatch(values.file, regex(values.until, "until"), regex(values.fail, "fail"));
|
|
1405
|
+
const started = Date.now();
|
|
1406
|
+
let portLeft = port === undefined ? undefined : "not checked yet";
|
|
1407
|
+
let urlLeft = values.url === undefined ? undefined : "not checked yet";
|
|
1408
|
+
let isPidLeft = pid !== undefined;
|
|
1409
|
+
const { dim } = paint(process13.stderr);
|
|
1410
|
+
const region = liveRegion((spinner) => {
|
|
1411
|
+
const waited = dim(formatElapsed(Date.now() - started));
|
|
1412
|
+
const left = [
|
|
1413
|
+
watch === undefined || watch.done ? undefined : `${watch.path}: ${watch.exists ? "no --until line yet" : "does not exist yet"}`,
|
|
1414
|
+
portLeft === undefined ? undefined : `${host}:${port}: ${portLeft}`,
|
|
1415
|
+
urlLeft === undefined ? undefined : `${values.url}: ${urlLeft}`,
|
|
1416
|
+
isPidLeft ? `pid ${pid}: still running` : undefined
|
|
1417
|
+
];
|
|
1418
|
+
return left.filter((line) => line !== undefined).map((line) => `${spinner} ${line} ${waited}`);
|
|
1419
|
+
});
|
|
1420
|
+
try {
|
|
1421
|
+
for (;; ) {
|
|
1422
|
+
if (watch !== undefined) {
|
|
1423
|
+
for (const event of await watch.poll()) {
|
|
1424
|
+
if (event.kind === "until") {
|
|
1425
|
+
console.log(`${watch.path}:${event.line.number}: ${truncate(event.line.text, WIDTH)}`);
|
|
1426
|
+
continue;
|
|
1427
|
+
}
|
|
1428
|
+
console.error(`waitfor: ${watch.path}:${event.line.number} matched --fail after ${formatDuration(Date.now() - started)}
|
|
1429
|
+
${numbered(event.excerpt, event.line)}`);
|
|
1430
|
+
return 1;
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
if (port !== undefined && portLeft !== undefined) {
|
|
1434
|
+
portLeft = await portProblem(host, port);
|
|
1435
|
+
if (portLeft === undefined)
|
|
1436
|
+
console.log(`${host}:${port} listening after ${formatDuration(Date.now() - started)}`);
|
|
1437
|
+
}
|
|
1438
|
+
if (values.url !== undefined && urlLeft !== undefined) {
|
|
1439
|
+
const budget = Math.max(1, Math.min(URL_TIMEOUT, started + timeout - Date.now()));
|
|
1440
|
+
const check = await urlCheck(values.url, budget);
|
|
1441
|
+
const isCutShort = "problem" in check && check.isTimeout === true && budget < URL_TIMEOUT && urlLeft !== "not checked yet";
|
|
1442
|
+
if (!isCutShort)
|
|
1443
|
+
urlLeft = "problem" in check ? check.problem : undefined;
|
|
1444
|
+
if ("status" in check)
|
|
1445
|
+
console.log(`${values.url} answered ${check.status} after ${formatDuration(Date.now() - started)}`);
|
|
1446
|
+
}
|
|
1447
|
+
if (pid !== undefined && isPidLeft) {
|
|
1448
|
+
isPidLeft = pidAlive(pid);
|
|
1449
|
+
if (!isPidLeft)
|
|
1450
|
+
console.log(`pid ${pid} exited after ${formatDuration(Date.now() - started)}`);
|
|
1451
|
+
}
|
|
1452
|
+
if ((watch?.done ?? true) && portLeft === undefined && urlLeft === undefined && !isPidLeft)
|
|
1453
|
+
return 0;
|
|
1454
|
+
if (Date.now() - started >= timeout && !(watch?.failing ?? false))
|
|
1455
|
+
break;
|
|
1456
|
+
await Bun.sleep(POLL);
|
|
1457
|
+
}
|
|
1458
|
+
} finally {
|
|
1459
|
+
region.stop();
|
|
1460
|
+
}
|
|
1461
|
+
const missing = [];
|
|
1462
|
+
if (watch !== undefined && !watch.done)
|
|
1463
|
+
missing.push(watch.exists ? `no line of ${watch.path} matched --until` : `${watch.path} does not exist`);
|
|
1464
|
+
if (portLeft !== undefined)
|
|
1465
|
+
missing.push(`${host}:${port} is not listening (${portLeft})`);
|
|
1466
|
+
if (urlLeft !== undefined)
|
|
1467
|
+
missing.push(`${values.url} ${urlLeft}`);
|
|
1468
|
+
if (isPidLeft)
|
|
1469
|
+
missing.push(`pid ${pid} is still running`);
|
|
1470
|
+
const lines = watch?.excerpt() ?? [];
|
|
1471
|
+
fail(`gave up after ${formatDuration(timeout)}: ${missing.join(", ")}${lines.length === 0 ? "" : `
|
|
1472
|
+
last lines of ${watch?.path}:
|
|
1473
|
+
${numbered(lines)}`}`);
|
|
1474
|
+
}
|
|
1475
|
+
process13.exitCode = await runMain({ name: "waitfor", version, run: main });
|