@kaynooo/cli-workspace 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +63 -2
- package/dist/audit-cli.js +1628 -0
- package/dist/cli.js +2414 -0
- package/dist/repos-cli.js +1684 -0
- package/dist/sweep-cli.js +1914 -0
- package/package.json +37 -4
package/dist/cli.js
ADDED
|
@@ -0,0 +1,2414 @@
|
|
|
1
|
+
#!/usr/bin/env -S bun --no-env-file
|
|
2
|
+
// @bun
|
|
3
|
+
|
|
4
|
+
// src/cli.ts
|
|
5
|
+
import process18 from "process";
|
|
6
|
+
|
|
7
|
+
// ../kit/src/advisory.ts
|
|
8
|
+
var SEVERITIES = ["low", "moderate", "high", "critical"];
|
|
9
|
+
function severityRank(severity) {
|
|
10
|
+
return SEVERITIES.indexOf(severity);
|
|
11
|
+
}
|
|
12
|
+
function isIgnored(advisory, ignore) {
|
|
13
|
+
return ignore.includes(advisory.ghsa ?? "") || ignore.includes(String(advisory.id));
|
|
14
|
+
}
|
|
15
|
+
// ../kit/src/args.ts
|
|
16
|
+
import process8 from "process";
|
|
17
|
+
import { parseArgs } from "util";
|
|
18
|
+
|
|
19
|
+
// ../kit/src/format.ts
|
|
20
|
+
import process6 from "process";
|
|
21
|
+
|
|
22
|
+
// ../kit/src/style.ts
|
|
23
|
+
import process5 from "process";
|
|
24
|
+
|
|
25
|
+
// ../../node_modules/.bun/@kaynooo+utils@2.31.2+d0769e28dc4dad16/node_modules/@kaynooo/utils/dist/chunk-mtetydvc.js
|
|
26
|
+
function x(s) {
|
|
27
|
+
let r = s.startsWith("#") ? s.slice(1).toLowerCase() : s.toLowerCase();
|
|
28
|
+
if (!/^[a-f0-9]+$/.test(r))
|
|
29
|
+
return [0, 0, 0];
|
|
30
|
+
let t = r.length, n = (c) => Number.parseInt(c, 16);
|
|
31
|
+
if (t === 3 || t === 4) {
|
|
32
|
+
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;
|
|
33
|
+
return g !== undefined ? [c, u, h, g] : [c, u, h];
|
|
34
|
+
}
|
|
35
|
+
if (t !== 6 && t !== 8)
|
|
36
|
+
throw Error(`Invalid hex color length: ${t}`);
|
|
37
|
+
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;
|
|
38
|
+
if ([a, b, o].some(Number.isNaN) || y !== undefined && Number.isNaN(y))
|
|
39
|
+
throw Error(`Invalid hex color value: ${s}`);
|
|
40
|
+
return y !== undefined ? [a, b, o, y] : [a, b, o];
|
|
41
|
+
}
|
|
42
|
+
function A(s) {
|
|
43
|
+
let [r, t, n, a] = s;
|
|
44
|
+
if (a)
|
|
45
|
+
return `#${(r << 24 | t << 16 | n << 8 | Math.round((a ?? 1) * 255)).toString(16).padStart(8, "0").toUpperCase()}`;
|
|
46
|
+
return `#${(r << 16 | t << 8 | n).toString(16).padStart(6, "0").toUpperCase()}`;
|
|
47
|
+
}
|
|
48
|
+
function R(s, r, t) {
|
|
49
|
+
s = (s % 360 + 360) % 360, t /= 100;
|
|
50
|
+
let n = r * Math.min(t, 1 - t) / 100, a = (b) => {
|
|
51
|
+
let o = (b + s / 30) % 12, y = t - n * Math.max(Math.min(o - 3, 9 - o, 1), -1);
|
|
52
|
+
return Math.round(255 * y);
|
|
53
|
+
};
|
|
54
|
+
return [a(0), a(8), a(4)];
|
|
55
|
+
}
|
|
56
|
+
function k(s) {
|
|
57
|
+
let [r, t, n] = s;
|
|
58
|
+
r /= 255, t /= 255, n /= 255;
|
|
59
|
+
let a = Math.max(r, t, n), b = Math.min(r, t, n), o = 0, y = 0, c = (a + b) / 2;
|
|
60
|
+
if (a !== b) {
|
|
61
|
+
let u = a - b;
|
|
62
|
+
if (y = c > 0 && c < 1 ? u / (1 - Math.abs(2 * c - 1)) : 0, a === r)
|
|
63
|
+
o = (t - n) / u + (t < n ? 6 : 0);
|
|
64
|
+
else if (a === t)
|
|
65
|
+
o = (n - r) / u + 2;
|
|
66
|
+
else
|
|
67
|
+
o = (r - t) / u + 4;
|
|
68
|
+
o /= 6;
|
|
69
|
+
}
|
|
70
|
+
return [Math.round(o * 360), Math.round(y * 100), Math.round(c * 100)];
|
|
71
|
+
}
|
|
72
|
+
function H(s, r, t) {
|
|
73
|
+
s = (s % 360 + 360) % 360, r /= 100, t /= 100;
|
|
74
|
+
let n = (a) => {
|
|
75
|
+
let b = (a + s / 60) % 6, o = t - t * r * Math.max(Math.min(b, 4 - b, 1), 0);
|
|
76
|
+
return Math.round(255 * o);
|
|
77
|
+
};
|
|
78
|
+
return [n(5), n(3), n(1)];
|
|
79
|
+
}
|
|
80
|
+
function f(s) {
|
|
81
|
+
let [r, t, n] = s;
|
|
82
|
+
r /= 255, t /= 255, n /= 255;
|
|
83
|
+
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;
|
|
84
|
+
if (o !== 0) {
|
|
85
|
+
if (a === r)
|
|
86
|
+
y = (t - n) / o + (t < n ? 6 : 0);
|
|
87
|
+
else if (a === t)
|
|
88
|
+
y = (n - r) / o + 2;
|
|
89
|
+
else
|
|
90
|
+
y = (r - t) / o + 4;
|
|
91
|
+
y /= 6;
|
|
92
|
+
}
|
|
93
|
+
return [Math.round(y * 360), Math.round(c * 100), Math.round(u * 100)];
|
|
94
|
+
}
|
|
95
|
+
function O(s) {
|
|
96
|
+
let r = s[0] / 255, t = s[1] / 255, n = s[2] / 255, a = 1 - Math.max(r, t, n);
|
|
97
|
+
if (a === 1)
|
|
98
|
+
return [0, 0, 0, 100];
|
|
99
|
+
let b = (1 - r - a) / (1 - a), o = (1 - t - a) / (1 - a), y = (1 - n - a) / (1 - a);
|
|
100
|
+
return [Math.round(b * 100), Math.round(o * 100), Math.round(y * 100), Math.round(a * 100)];
|
|
101
|
+
}
|
|
102
|
+
function v(s, r, t, n) {
|
|
103
|
+
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))];
|
|
104
|
+
}
|
|
105
|
+
var m = (s) => s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
|
|
106
|
+
var p = (s) => s <= 0.0031308 ? 12.92 * s : 1.055 * s ** 0.4166666666666667 - 0.055;
|
|
107
|
+
var i = (s, r) => Math.round(s * 10 ** r) / 10 ** r;
|
|
108
|
+
function T(s) {
|
|
109
|
+
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);
|
|
110
|
+
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)];
|
|
111
|
+
}
|
|
112
|
+
function M(s, r, t) {
|
|
113
|
+
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)));
|
|
114
|
+
return [u(o), u(y), u(c)];
|
|
115
|
+
}
|
|
116
|
+
function d(s) {
|
|
117
|
+
let [r, t, n] = T(s), a = Math.sqrt(t * t + n * n), b = (Math.atan2(n, t) * 180 / Math.PI + 360) % 360;
|
|
118
|
+
return [i(r, 4), i(a, 4), i(b, 2)];
|
|
119
|
+
}
|
|
120
|
+
function C(s, r, t) {
|
|
121
|
+
let n = t * Math.PI / 180;
|
|
122
|
+
return M(s, r * Math.cos(n), r * Math.sin(n));
|
|
123
|
+
}
|
|
124
|
+
function l(s) {
|
|
125
|
+
let r = s / 255;
|
|
126
|
+
return r <= 0.03928 ? r / 12.92 : ((r + 0.055) / 1.055) ** 2.4;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
class e {
|
|
130
|
+
r;
|
|
131
|
+
g;
|
|
132
|
+
b;
|
|
133
|
+
a;
|
|
134
|
+
constructor(s, r, t, n = 1) {
|
|
135
|
+
this.r = s, this.g = r, this.b = t, this.a = n;
|
|
136
|
+
}
|
|
137
|
+
static from(s, r) {
|
|
138
|
+
if (s === "hex") {
|
|
139
|
+
let [t, n, a, b] = x(r);
|
|
140
|
+
return new e(t, n, a, b);
|
|
141
|
+
}
|
|
142
|
+
if (s === "rgb") {
|
|
143
|
+
if (Array.isArray(r)) {
|
|
144
|
+
let [o, y, c, u] = r;
|
|
145
|
+
return new e(o, y, c, u);
|
|
146
|
+
}
|
|
147
|
+
let { r: t, g: n, b: a, a: b } = r;
|
|
148
|
+
return new e(t, n, a, b);
|
|
149
|
+
}
|
|
150
|
+
if (s === "hsl") {
|
|
151
|
+
let t, n, a, b;
|
|
152
|
+
if (Array.isArray(r))
|
|
153
|
+
[t, n, a, b] = r;
|
|
154
|
+
else
|
|
155
|
+
({ h: t, s: n, l: a, a: b } = r);
|
|
156
|
+
let [o, y, c] = R(t, n, a);
|
|
157
|
+
return new e(o, y, c, b);
|
|
158
|
+
}
|
|
159
|
+
if (s === "hsv") {
|
|
160
|
+
let t, n, a, b;
|
|
161
|
+
if (Array.isArray(r))
|
|
162
|
+
[t, n, a, b] = r;
|
|
163
|
+
else
|
|
164
|
+
({ h: t, s: n, v: a, a: b } = r);
|
|
165
|
+
let [o, y, c] = H(t, n, a);
|
|
166
|
+
return new e(o, y, c, b);
|
|
167
|
+
}
|
|
168
|
+
if (s === "cmyk") {
|
|
169
|
+
let t, n, a, b, o;
|
|
170
|
+
if (Array.isArray(r))
|
|
171
|
+
[t, n, a, b, o] = r;
|
|
172
|
+
else
|
|
173
|
+
({ c: t, m: n, y: a, k: b, a: o } = r);
|
|
174
|
+
let [y, c, u] = v(t, n, a, b);
|
|
175
|
+
return new e(y, c, u, o);
|
|
176
|
+
}
|
|
177
|
+
if (s === "oklch") {
|
|
178
|
+
let t, n, a, b;
|
|
179
|
+
if (Array.isArray(r))
|
|
180
|
+
[t, n, a, b] = r;
|
|
181
|
+
else
|
|
182
|
+
({ l: t, c: n, h: a, a: b } = r);
|
|
183
|
+
let [o, y, c] = C(t, n, a);
|
|
184
|
+
return new e(o, y, c, b);
|
|
185
|
+
}
|
|
186
|
+
if (s === "oklab") {
|
|
187
|
+
let t, n, a, b;
|
|
188
|
+
if (Array.isArray(r))
|
|
189
|
+
[t, n, a, b] = r;
|
|
190
|
+
else
|
|
191
|
+
({ l: t, a: n, b: a, alpha: b } = r);
|
|
192
|
+
let [o, y, c] = M(t, n, a);
|
|
193
|
+
return new e(o, y, c, b);
|
|
194
|
+
}
|
|
195
|
+
throw Error("Invalid input");
|
|
196
|
+
}
|
|
197
|
+
to(s) {
|
|
198
|
+
switch (s) {
|
|
199
|
+
case "rgb":
|
|
200
|
+
return [this.r, this.g, this.b];
|
|
201
|
+
case "rgba":
|
|
202
|
+
return [this.r, this.g, this.b, this.a];
|
|
203
|
+
case "hex":
|
|
204
|
+
return A([this.r, this.g, this.b]);
|
|
205
|
+
case "hexa":
|
|
206
|
+
return A([this.r, this.g, this.b, this.a]);
|
|
207
|
+
case "hsl":
|
|
208
|
+
return k([this.r, this.g, this.b]);
|
|
209
|
+
case "hsla":
|
|
210
|
+
return [...k([this.r, this.g, this.b]), this.a];
|
|
211
|
+
case "hsv":
|
|
212
|
+
return f([this.r, this.g, this.b]);
|
|
213
|
+
case "hsva":
|
|
214
|
+
return [...f([this.r, this.g, this.b]), this.a];
|
|
215
|
+
case "cmyk":
|
|
216
|
+
return O([this.r, this.g, this.b]);
|
|
217
|
+
case "cmyka":
|
|
218
|
+
return [...O([this.r, this.g, this.b]), this.a];
|
|
219
|
+
case "oklch":
|
|
220
|
+
return d([this.r, this.g, this.b]);
|
|
221
|
+
case "oklcha":
|
|
222
|
+
return [...d([this.r, this.g, this.b]), this.a];
|
|
223
|
+
case "oklab":
|
|
224
|
+
return T([this.r, this.g, this.b]);
|
|
225
|
+
case "oklaba":
|
|
226
|
+
return [...T([this.r, this.g, this.b]), this.a];
|
|
227
|
+
default:
|
|
228
|
+
return [this.r, this.g, this.b];
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
getLuminance() {
|
|
232
|
+
let s = l(this.r), r = l(this.g), t = l(this.b);
|
|
233
|
+
return 0.2126 * s + 0.7152 * r + 0.0722 * t;
|
|
234
|
+
}
|
|
235
|
+
contrastRatio(s) {
|
|
236
|
+
let r = this.getLuminance(), t = s.getLuminance(), [n, a] = r >= t ? [r, t] : [t, r];
|
|
237
|
+
return (n + 0.05) / (a + 0.05);
|
|
238
|
+
}
|
|
239
|
+
bestContrastForeground() {
|
|
240
|
+
let s = new e(255, 255, 255, 1), r = new e(0, 0, 0, 1), t = this.contrastRatio(s), n = this.contrastRatio(r);
|
|
241
|
+
return t >= n ? s : r;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// ../../node_modules/.bun/@kaynooo+utils@2.31.2+d0769e28dc4dad16/node_modules/@kaynooo/utils/dist/chunk-y1msbssb.js
|
|
246
|
+
var r = 39;
|
|
247
|
+
var o = 49;
|
|
248
|
+
var f2 = "\x1B[";
|
|
249
|
+
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") => {
|
|
250
|
+
let [a, l, s] = e.from("hex", i).to("rgb");
|
|
251
|
+
return [`${n === "fg" ? 38 : 48};2;${a};${l};${s}`, n === "fg" ? r : o];
|
|
252
|
+
}, ansi256: (i, n = "fg") => [`${n === "fg" ? 38 : 48};5;${i}`, n === "fg" ? r : o] };
|
|
253
|
+
var b = new Set(["", "1", "2", "3", "true"]);
|
|
254
|
+
function h(i, n = globalThis.process?.env ?? {}) {
|
|
255
|
+
if (n.FORCE_COLOR !== undefined)
|
|
256
|
+
return b.has(n.FORCE_COLOR);
|
|
257
|
+
if (n.NO_COLOR !== undefined || n.NODE_DISABLE_COLORS !== undefined || n.TERM === "dumb")
|
|
258
|
+
return false;
|
|
259
|
+
return i?.isTTY === true;
|
|
260
|
+
}
|
|
261
|
+
function d2(i, n, a) {
|
|
262
|
+
if (!h(a.stream ?? globalThis.process?.stdout, a.env))
|
|
263
|
+
return n;
|
|
264
|
+
let l = "", s = "";
|
|
265
|
+
for (let g = 0, y = i.length;g < y; g++) {
|
|
266
|
+
let [c, p] = i[g];
|
|
267
|
+
l += `${f2}${c}m`, s = `${f2}${p}m${s}`;
|
|
268
|
+
}
|
|
269
|
+
return l + n + s;
|
|
270
|
+
}
|
|
271
|
+
function u(i, n = []) {
|
|
272
|
+
return new Proxy((l) => d2(n, l, i), { get(l, s) {
|
|
273
|
+
let g = C2[s];
|
|
274
|
+
if (Array.isArray(g))
|
|
275
|
+
return u(i, [...n, g]);
|
|
276
|
+
if (typeof g === "function")
|
|
277
|
+
return (...y) => u(i, [...n, g(...y)]);
|
|
278
|
+
throw Error(`Unknown style: ${String(s)}`);
|
|
279
|
+
} });
|
|
280
|
+
}
|
|
281
|
+
function le(i = {}) {
|
|
282
|
+
return u(i);
|
|
283
|
+
}
|
|
284
|
+
var t = le();
|
|
285
|
+
|
|
286
|
+
// ../kit/src/theme.ts
|
|
287
|
+
import { existsSync as existsSync3, readFileSync, statSync } from "fs";
|
|
288
|
+
import { homedir as homedir2 } from "os";
|
|
289
|
+
import { join as join3, resolve as resolve2 } from "path";
|
|
290
|
+
import process4 from "process";
|
|
291
|
+
|
|
292
|
+
// ../kit/src/config.ts
|
|
293
|
+
import { existsSync } from "fs";
|
|
294
|
+
import { dirname, join, resolve } from "path";
|
|
295
|
+
import process2 from "process";
|
|
296
|
+
function findUp(names, from = process2.cwd(), stop = () => false) {
|
|
297
|
+
const wanted = typeof names === "string" ? [names] : names;
|
|
298
|
+
for (let dir = resolve(from);; dir = dirname(dir)) {
|
|
299
|
+
const found = wanted.map((name) => join(dir, name)).find((path) => existsSync(path));
|
|
300
|
+
if (found !== undefined)
|
|
301
|
+
return found;
|
|
302
|
+
if (stop(dir) || dirname(dir) === dir)
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// ../kit/src/user-config.ts
|
|
308
|
+
import { existsSync as existsSync2 } from "fs";
|
|
309
|
+
import { homedir } from "os";
|
|
310
|
+
import { join as join2 } from "path";
|
|
311
|
+
import process3 from "process";
|
|
312
|
+
function configBase(where = {}) {
|
|
313
|
+
const xdg = where.xdg ?? (where.home === undefined ? process3.env.XDG_CONFIG_HOME : undefined);
|
|
314
|
+
return xdg !== undefined && xdg !== "" ? xdg : join2(where.home ?? homedir(), ".config");
|
|
315
|
+
}
|
|
316
|
+
function userConfigPath(bin, where = {}) {
|
|
317
|
+
return join2(configBase(where), "kaynooo-cli", `${bin}.json`);
|
|
318
|
+
}
|
|
319
|
+
function userConfigReadPath(bin, legacy = [], where = {}) {
|
|
320
|
+
const current = userConfigPath(bin, where);
|
|
321
|
+
if (existsSync2(current))
|
|
322
|
+
return current;
|
|
323
|
+
const base = configBase(where);
|
|
324
|
+
return legacy.map((parts) => join2(base, ...parts)).find((path) => existsSync2(path)) ?? current;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// ../kit/src/theme.ts
|
|
328
|
+
var DEFAULT = {
|
|
329
|
+
colors: { accent: "cyan", id: "yellow", path: "blue", label: "dim", value: "bold", ok: "green", warn: "yellow", problem: "red", note: "dim", passed: "green", failed: "red", running: "cyan", queued: "yellow", skipped: "dim" },
|
|
330
|
+
glyphs: { ok: "\u2713", warn: "\u25B2", problem: "\u2717", note: "\u203A", passed: "\u2713", failed: "\u2717", running: "\u25CF", queued: "\u25CB", skipped: "-", bar: "\u2501" },
|
|
331
|
+
accents: ["cyan", "magenta", "green", "blue", "yellow", "redBright"],
|
|
332
|
+
bins: {}
|
|
333
|
+
};
|
|
334
|
+
var BRIGHT = { accent: "cyanBright", id: "yellowBright", path: "blueBright", label: "white", value: "bold whiteBright", ok: "greenBright", warn: "yellowBright", problem: "redBright", note: "white", passed: "greenBright", failed: "redBright", running: "cyanBright", queued: "yellowBright", skipped: "white" };
|
|
335
|
+
var MONO = { accent: "bold", id: "bold", path: "underline", label: "dim", value: "bold", ok: "bold", warn: "bold", problem: "bold", note: "dim", passed: "bold", failed: "bold", running: "bold", queued: "dim", skipped: "dim" };
|
|
336
|
+
var ASCII = { ok: "+", warn: "!", problem: "x", note: ">", passed: "+", failed: "x", running: "*", queued: "o", skipped: "-", bar: "#" };
|
|
337
|
+
var PRESETS = {
|
|
338
|
+
default: {},
|
|
339
|
+
"high-contrast": { colors: BRIGHT, accents: ["cyanBright", "magentaBright", "greenBright", "blueBright", "yellowBright", "redBright"] },
|
|
340
|
+
mono: { colors: MONO, accents: ["bold"] },
|
|
341
|
+
ascii: { glyphs: ASCII }
|
|
342
|
+
};
|
|
343
|
+
var STYLES = new Set(["bold", "dim", "italic", "underline", "inverse", "strikethrough", "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "gray", "grey", "blackBright", "redBright", "greenBright", "yellowBright", "blueBright", "magentaBright", "cyanBright", "whiteBright"]);
|
|
344
|
+
var HEX = /^#[0-9a-f]{6}$/i;
|
|
345
|
+
var CONTROL = /\p{Cc}/u;
|
|
346
|
+
var MAX_FILE_BYTES = 64 * 1024;
|
|
347
|
+
var SPACES = /\s+/;
|
|
348
|
+
function isColorSpec(spec) {
|
|
349
|
+
if (typeof spec === "number")
|
|
350
|
+
return Number.isInteger(spec) && spec >= 0 && spec <= 255;
|
|
351
|
+
return typeof spec === "string" && spec.trim() !== "" && spec.trim().split(SPACES).every((token) => STYLES.has(token) || HEX.test(token));
|
|
352
|
+
}
|
|
353
|
+
function tinted(colors, spec) {
|
|
354
|
+
if (typeof spec === "number")
|
|
355
|
+
return colors.ansi256(spec);
|
|
356
|
+
return spec.trim().split(SPACES).reduce((chain, token) => HEX.test(token) ? chain.hex(token) : chain[token] ?? chain, colors);
|
|
357
|
+
}
|
|
358
|
+
function isRecord(value) {
|
|
359
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
360
|
+
}
|
|
361
|
+
function readThemeFile(path, warn) {
|
|
362
|
+
try {
|
|
363
|
+
const stat = statSync(path, { throwIfNoEntry: false });
|
|
364
|
+
if (stat === undefined)
|
|
365
|
+
return;
|
|
366
|
+
if (!stat.isFile() || stat.size > MAX_FILE_BYTES) {
|
|
367
|
+
warn(`${path} is not a regular file under ${MAX_FILE_BYTES / 1024} KiB, theme ignored`);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
const parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
371
|
+
if (isRecord(parsed))
|
|
372
|
+
return parsed;
|
|
373
|
+
warn(`${path} is not a JSON object, theme ignored`);
|
|
374
|
+
} catch (error) {
|
|
375
|
+
warn(`${path} does not parse (${error instanceof Error ? error.message : String(error)}), theme ignored`);
|
|
376
|
+
}
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
function applyPreset(theme, name, warn, where) {
|
|
380
|
+
if (name === undefined)
|
|
381
|
+
return;
|
|
382
|
+
if (typeof name !== "string" || !Object.hasOwn(PRESETS, name)) {
|
|
383
|
+
warn(`${where}: no preset ${String(name)}, one of ${Object.keys(PRESETS).join(", ")}`);
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
const preset = PRESETS[name];
|
|
387
|
+
Object.assign(theme.colors, preset.colors);
|
|
388
|
+
Object.assign(theme.glyphs, preset.glyphs);
|
|
389
|
+
if (preset.accents !== undefined)
|
|
390
|
+
theme.accents = [...preset.accents];
|
|
391
|
+
return name;
|
|
392
|
+
}
|
|
393
|
+
function applyFile(theme, file, where, warn) {
|
|
394
|
+
if (isRecord(file.colors)) {
|
|
395
|
+
for (const [key, value] of Object.entries(file.colors)) {
|
|
396
|
+
if (!Object.hasOwn(DEFAULT.colors, key))
|
|
397
|
+
warn(`${where}: no colour ${key}, one of ${Object.keys(DEFAULT.colors).join(", ")}`);
|
|
398
|
+
else if (!isColorSpec(value))
|
|
399
|
+
warn(`${where}: colour ${key} is not an ANSI name, \`#rrggbb\` or a number up to 255`);
|
|
400
|
+
else
|
|
401
|
+
theme.colors[key] = value;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
if (isRecord(file.glyphs)) {
|
|
405
|
+
for (const [key, value] of Object.entries(file.glyphs)) {
|
|
406
|
+
if (!Object.hasOwn(DEFAULT.glyphs, key))
|
|
407
|
+
warn(`${where}: no glyph ${key}, one of ${Object.keys(DEFAULT.glyphs).join(", ")}`);
|
|
408
|
+
else if (typeof value !== "string" || CONTROL.test(value) || Bun.stringWidth(value) !== 1)
|
|
409
|
+
warn(`${where}: glyph ${key} must be one visible character wide, with no control character`);
|
|
410
|
+
else
|
|
411
|
+
theme.glyphs[key] = value;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
if (Array.isArray(file.accents) && file.accents.length > 0 && file.accents.every(isColorSpec))
|
|
415
|
+
theme.accents = [...file.accents];
|
|
416
|
+
else if (file.accents !== undefined)
|
|
417
|
+
warn(`${where}: accents must be a non-empty list of colours`);
|
|
418
|
+
if (isRecord(file.bins)) {
|
|
419
|
+
for (const [bin, value] of Object.entries(file.bins)) {
|
|
420
|
+
if (isColorSpec(value))
|
|
421
|
+
theme.bins[bin] = value;
|
|
422
|
+
else
|
|
423
|
+
warn(`${where}: bin ${bin} has no valid colour`);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
function endsWalk(home) {
|
|
428
|
+
return (dir) => dir === resolve2(home) || existsSync3(join3(dir, ".git"));
|
|
429
|
+
}
|
|
430
|
+
var warned = new Set;
|
|
431
|
+
function warnOnce(message) {
|
|
432
|
+
if (warned.has(message))
|
|
433
|
+
return;
|
|
434
|
+
warned.add(message);
|
|
435
|
+
console.error(`warning: theme ${message}`);
|
|
436
|
+
}
|
|
437
|
+
function isPath(chosen) {
|
|
438
|
+
return chosen.includes("/") || chosen.endsWith(".json");
|
|
439
|
+
}
|
|
440
|
+
function loadTheme({ env = process4.env, from = process4.cwd(), home = homedir2(), warn = warnOnce } = {}) {
|
|
441
|
+
const theme = { colors: { ...DEFAULT.colors }, glyphs: { ...DEFAULT.glyphs }, accents: [...DEFAULT.accents], bins: Object.create(null) };
|
|
442
|
+
const sources = [];
|
|
443
|
+
const chosen = env.KIT_THEME ?? "";
|
|
444
|
+
const wanted = chosen === "" ? [userConfigPath("theme", { home, xdg: env.XDG_CONFIG_HOME }), findUp(join3(".kaynooo", "theme.json"), from, endsWalk(home))] : [];
|
|
445
|
+
if (isPath(chosen))
|
|
446
|
+
wanted.push(resolve2(from, chosen));
|
|
447
|
+
const files = [];
|
|
448
|
+
for (const path of wanted) {
|
|
449
|
+
const file = path === undefined ? undefined : readThemeFile(path, warn);
|
|
450
|
+
if (path !== undefined && file !== undefined)
|
|
451
|
+
files.push({ path, file });
|
|
452
|
+
}
|
|
453
|
+
let preset = "default";
|
|
454
|
+
if (chosen !== "" && !isPath(chosen)) {
|
|
455
|
+
preset = applyPreset(theme, chosen, warn, "KIT_THEME") ?? preset;
|
|
456
|
+
} else {
|
|
457
|
+
const named = files.findLast(({ file }) => file.preset !== undefined);
|
|
458
|
+
if (named !== undefined)
|
|
459
|
+
preset = applyPreset(theme, named.file.preset, warn, named.path) ?? preset;
|
|
460
|
+
}
|
|
461
|
+
for (const { path, file } of files) {
|
|
462
|
+
applyFile(theme, file, path, warn);
|
|
463
|
+
sources.push(path);
|
|
464
|
+
}
|
|
465
|
+
return { theme, sources, preset };
|
|
466
|
+
}
|
|
467
|
+
var current;
|
|
468
|
+
var currentKey = "";
|
|
469
|
+
function activeTheme() {
|
|
470
|
+
const key = `${process4.cwd()}\x00${process4.env.KIT_THEME ?? ""}\x00${process4.env.XDG_CONFIG_HOME ?? ""}`;
|
|
471
|
+
if (current === undefined || key !== currentKey) {
|
|
472
|
+
current = loadTheme();
|
|
473
|
+
currentKey = key;
|
|
474
|
+
}
|
|
475
|
+
return current;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// ../kit/src/style.ts
|
|
479
|
+
var MARK_WORDS = { ok: "ok:", warn: "warning:", problem: "error:", note: "note:" };
|
|
480
|
+
function takesControlCodes(stream, env = process5.env) {
|
|
481
|
+
return stream.isTTY === true && env.NO_COLOR === undefined && env.NODE_DISABLE_COLORS === undefined && env.TERM !== "dumb";
|
|
482
|
+
}
|
|
483
|
+
function paint(stream, env = process5.env) {
|
|
484
|
+
return le({ stream: { isTTY: takesControlCodes(stream, env) }, env });
|
|
485
|
+
}
|
|
486
|
+
function marked(mark, text, stream, detail, env = process5.env) {
|
|
487
|
+
const colors = paint(stream, env);
|
|
488
|
+
let head = MARK_WORDS[mark];
|
|
489
|
+
if (takesControlCodes(stream, env)) {
|
|
490
|
+
const { theme } = activeTheme();
|
|
491
|
+
head = tinted(colors.bold, theme.colors[mark])(theme.glyphs[mark]);
|
|
492
|
+
}
|
|
493
|
+
return `${head} ${text}${detail === undefined ? "" : ` ${colors.dim(detail)}`}`;
|
|
494
|
+
}
|
|
495
|
+
function ok(text, detail) {
|
|
496
|
+
console.log(marked("ok", text, process5.stdout, detail));
|
|
497
|
+
}
|
|
498
|
+
function note(text, detail) {
|
|
499
|
+
console.log(marked("note", text, process5.stdout, detail));
|
|
500
|
+
}
|
|
501
|
+
function problem(text, detail) {
|
|
502
|
+
console.error(marked("problem", text, process5.stderr, detail));
|
|
503
|
+
}
|
|
504
|
+
function role(name, stream, env = process5.env) {
|
|
505
|
+
const colors = paint(stream, env);
|
|
506
|
+
return takesControlCodes(stream, env) ? tinted(colors, activeTheme().theme.colors[name]) : colors;
|
|
507
|
+
}
|
|
508
|
+
var STATUS_WORDS = {
|
|
509
|
+
passed: ["passed", "success", "succeeded", "ok"],
|
|
510
|
+
failed: ["failed", "failure", "error", "errored"],
|
|
511
|
+
running: ["running", "in_progress", "started"],
|
|
512
|
+
queued: ["queued", "waiting", "pending", "created", "blocked", "scheduled"],
|
|
513
|
+
skipped: ["skipped", "cancelled", "canceled", "manual"]
|
|
514
|
+
};
|
|
515
|
+
var STATUS_OF = new Map(Object.entries(STATUS_WORDS).flatMap(([status, words]) => words.map((word) => [word, status])));
|
|
516
|
+
// ../../node_modules/.bun/@kaynooo+utils@2.31.2+d0769e28dc4dad16/node_modules/@kaynooo/utils/dist/chunk-hrsswv1w.js
|
|
517
|
+
function $e(t, e = false, i = 1) {
|
|
518
|
+
let n = typeof e === "object" ? e : { units: e ? "si" : "iec", dp: i }, { units: s = "iec", dp: u = 1 } = n, r = s === "si" ? 1000 : 1024;
|
|
519
|
+
if (Math.abs(t) < r)
|
|
520
|
+
return `${t} B`;
|
|
521
|
+
let c = s === "iec" ? ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"] : ["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"], o = -1, l = 10 ** u;
|
|
522
|
+
do
|
|
523
|
+
t /= r, ++o;
|
|
524
|
+
while (Math.round(Math.abs(t) * l) / l >= r && o < c.length - 1);
|
|
525
|
+
return `${t.toFixed(u)} ${c[o]}`;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// ../kit/src/format.ts
|
|
529
|
+
var UNITS = [["d", 86400], ["h", 3600], ["m", 60], ["s", 1]];
|
|
530
|
+
function formatSeconds(total) {
|
|
531
|
+
let rest = Math.max(0, Math.round(total));
|
|
532
|
+
const amounts = UNITS.map(([unit, size]) => {
|
|
533
|
+
const amount = Math.floor(rest / size);
|
|
534
|
+
rest -= amount * size;
|
|
535
|
+
return `${amount}${unit}`;
|
|
536
|
+
});
|
|
537
|
+
const first = amounts.findIndex((amount) => !amount.startsWith("0"));
|
|
538
|
+
if (first === -1)
|
|
539
|
+
return "0s";
|
|
540
|
+
const next = amounts[first + 1];
|
|
541
|
+
return next === undefined || next.startsWith("0") ? amounts[first] ?? "0s" : `${amounts[first]} ${next}`;
|
|
542
|
+
}
|
|
543
|
+
function formatDuration(ms) {
|
|
544
|
+
return ms < 60000 ? `${(Math.max(0, ms) / 1000).toFixed(1)}s` : formatSeconds(ms / 1000);
|
|
545
|
+
}
|
|
546
|
+
function upTo(text, separator) {
|
|
547
|
+
const end = text.indexOf(separator);
|
|
548
|
+
return end === -1 ? text : text.slice(0, end);
|
|
549
|
+
}
|
|
550
|
+
function formatAge(date, now = Date.now()) {
|
|
551
|
+
const at = typeof date === "number" ? date : Date.parse(date);
|
|
552
|
+
if (Number.isNaN(at))
|
|
553
|
+
return "-";
|
|
554
|
+
const seconds = (now - at) / 1000;
|
|
555
|
+
return seconds < 60 ? "just now" : `${upTo(formatSeconds(seconds), " ")} ago`;
|
|
556
|
+
}
|
|
557
|
+
function truncate(text, width) {
|
|
558
|
+
return text.length <= width ? text : `${text.slice(0, Math.max(0, width - 1))}\u2026`;
|
|
559
|
+
}
|
|
560
|
+
var LINE_BREAKS = /\s*\n\s*/g;
|
|
561
|
+
var ESCAPE = String.fromCharCode(27);
|
|
562
|
+
function textWidth(text) {
|
|
563
|
+
return Bun.stringWidth(text);
|
|
564
|
+
}
|
|
565
|
+
function table(rows, { cellWidth, header = false, align = [], fit, stream = process6.stdout } = {}) {
|
|
566
|
+
const cells = cellWidth === undefined ? rows : rows.map((row) => row.map((cell) => truncate(cell.replaceAll(LINE_BREAKS, " "), cellWidth)));
|
|
567
|
+
const widths = [];
|
|
568
|
+
for (const row of cells) {
|
|
569
|
+
for (const [index, cell] of row.entries())
|
|
570
|
+
widths[index] = Math.max(widths[index] ?? 0, textWidth(cell));
|
|
571
|
+
}
|
|
572
|
+
const shown = fit === undefined ? cells : narrow(cells, widths, fit);
|
|
573
|
+
const colors = paint(stream);
|
|
574
|
+
const lines = shown.map((row) => row.map((cell, index) => {
|
|
575
|
+
const width = widths[index] ?? 0;
|
|
576
|
+
const gap = " ".repeat(Math.max(0, width - textWidth(cell)));
|
|
577
|
+
if (align[index] === "right")
|
|
578
|
+
return `${gap}${cell}`;
|
|
579
|
+
return index === row.length - 1 ? cell : `${cell}${gap}`;
|
|
580
|
+
}).join(" ").trimEnd());
|
|
581
|
+
const rule = header && takesControlCodes(stream) && lines.length > 1 ? [colors.dim("\u2500".repeat(Math.max(...lines.map(textWidth))))] : [];
|
|
582
|
+
return lines.flatMap((line, index) => header && index === 0 ? [colors.bold(line), ...rule] : [line]).join(`
|
|
583
|
+
`);
|
|
584
|
+
}
|
|
585
|
+
function narrow(rows, widths, limit) {
|
|
586
|
+
const used = () => widths.reduce((sum, width) => sum + width, 0) + 2 * Math.max(0, widths.length - 1);
|
|
587
|
+
const cuttable = (index) => rows.every((row) => !(row[index] ?? "").includes(ESCAPE));
|
|
588
|
+
const floor = 8;
|
|
589
|
+
while (used() > limit) {
|
|
590
|
+
let widest = -1;
|
|
591
|
+
for (const [index, width] of widths.entries()) {
|
|
592
|
+
if (cuttable(index) && width > floor && width > (widths[widest] ?? 0))
|
|
593
|
+
widest = index;
|
|
594
|
+
}
|
|
595
|
+
if (widest === -1)
|
|
596
|
+
break;
|
|
597
|
+
widths[widest] = Math.max(floor, (widths[widest] ?? 0) - (used() - limit));
|
|
598
|
+
}
|
|
599
|
+
return rows.map((row) => row.map((cell, index) => textWidth(cell) > (widths[index] ?? 0) ? truncate(cell, widths[index] ?? 0) : cell));
|
|
600
|
+
}
|
|
601
|
+
function printJson(value) {
|
|
602
|
+
console.log(JSON.stringify(value, undefined, 2));
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
// ../kit/src/help.ts
|
|
606
|
+
import process7 from "process";
|
|
607
|
+
var OPTIONAL = /\[[^\]]+\]/g;
|
|
608
|
+
var USAGE_PREFIX = /^usage:\s*/i;
|
|
609
|
+
var FULL_STOP = /\.$/;
|
|
610
|
+
function usageLine(usage, colors = paint(process7.stdout)) {
|
|
611
|
+
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
612
|
+
}
|
|
613
|
+
function helpText(usage, options, colors = paint(process7.stdout), { examples = [], replaces, exits = [] } = {}) {
|
|
614
|
+
return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
|
|
615
|
+
`);
|
|
616
|
+
}
|
|
617
|
+
function exitRows(exits, colors = paint(process7.stdout)) {
|
|
618
|
+
if (exits.length === 0)
|
|
619
|
+
return [];
|
|
620
|
+
const width = Math.max(...exits.map(([code]) => String(code).length));
|
|
621
|
+
return ["", colors.bold("Exits:"), ...exits.map(([code, meaning]) => ` ${colors.cyan(String(code).padEnd(width))} ${meaning}`)];
|
|
622
|
+
}
|
|
623
|
+
function replacesRows(replaces, colors = paint(process7.stdout)) {
|
|
624
|
+
return replaces === undefined ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
625
|
+
}
|
|
626
|
+
function wantsHelpJson(env = process7.env) {
|
|
627
|
+
return env.KIT_HELP === "json";
|
|
628
|
+
}
|
|
629
|
+
function description(usage, { examples = [], replaces, exits } = {}, options) {
|
|
630
|
+
const lines = usage.replace(USAGE_PREFIX, "").split(`
|
|
631
|
+
`);
|
|
632
|
+
const first = lines[0];
|
|
633
|
+
const colon = first.indexOf(": ");
|
|
634
|
+
const prose = lines[lines.indexOf("") + 1] ?? "";
|
|
635
|
+
const fallback = lines.includes("") ? `${prose.charAt(0).toLowerCase()}${prose.slice(1)}`.replace(FULL_STOP, "") : "";
|
|
636
|
+
return {
|
|
637
|
+
synopsis: colon === -1 ? first : first.slice(0, colon),
|
|
638
|
+
summary: colon === -1 ? fallback : first.slice(colon + 2),
|
|
639
|
+
...replaces === undefined ? {} : { replaces },
|
|
640
|
+
examples: examples.map((example) => typeof example === "string" ? { line: example } : { line: example[0], note: example[1] }),
|
|
641
|
+
...exits === undefined ? {} : { exits: exits.map(([code, meaning]) => ({ code, meaning })) },
|
|
642
|
+
...options === undefined ? {} : { options: Object.entries(options).map(([name, { short, description }]) => ({ name, ...short === undefined ? {} : { short }, ...description === undefined ? {} : { description } })) }
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
function exampleRows(examples, colors = paint(process7.stdout)) {
|
|
646
|
+
if (examples.length === 0)
|
|
647
|
+
return [];
|
|
648
|
+
return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
|
|
649
|
+
}
|
|
650
|
+
function optionRows(options, colors = paint(process7.stdout)) {
|
|
651
|
+
const rows = Object.entries(options).map(([name, option]) => {
|
|
652
|
+
const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
|
|
653
|
+
const named = option.choices === undefined ? "<value>" : `<${option.choices.join("|")}>`;
|
|
654
|
+
const placeholder = option.optional === undefined ? named : `[${named}]`;
|
|
655
|
+
const value = option.type === "string" ? ` ${colors.dim(placeholder)}` : "";
|
|
656
|
+
return { flag: `${short}${colors.cyan(`--${name}`)}${value}`, width: `-x, --${name}`.length + (option.type === "string" ? placeholder.length + 1 : 0), description: option.description ?? "" };
|
|
657
|
+
});
|
|
658
|
+
const width = Math.max(0, ...rows.map((row) => row.width));
|
|
659
|
+
return rows.map((row) => row.description === "" ? ` ${row.flag}` : ` ${row.flag}${" ".repeat(width - row.width)} ${colors.dim(row.description)}`);
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// ../kit/src/text.ts
|
|
663
|
+
var ANSI = new RegExp(`${String.fromCharCode(27)}\\[[\\d;?]*[a-z]`, "gi");
|
|
664
|
+
function distance(a, b) {
|
|
665
|
+
let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
|
|
666
|
+
for (const [i, left] of [...a].entries()) {
|
|
667
|
+
const current = [i + 1];
|
|
668
|
+
for (const [j, right] of [...b].entries())
|
|
669
|
+
current.push(Math.min((previous[j + 1] ?? 0) + 1, (current[j] ?? 0) + 1, (previous[j] ?? 0) + (left === right ? 0 : 1)));
|
|
670
|
+
previous = current;
|
|
671
|
+
}
|
|
672
|
+
return previous[b.length] ?? 0;
|
|
673
|
+
}
|
|
674
|
+
function closest(name, names) {
|
|
675
|
+
const started = names.find((known) => known.startsWith(name));
|
|
676
|
+
if (started !== undefined)
|
|
677
|
+
return started;
|
|
678
|
+
const [best] = names.map((known) => ({ known, far: distance(name, known) })).sort((a, b) => a.far - b.far);
|
|
679
|
+
return best !== undefined && best.far <= (name.length < 4 ? 1 : 2) ? best.known : undefined;
|
|
680
|
+
}
|
|
681
|
+
// ../kit/src/input.ts
|
|
682
|
+
var DURATION = /^(\d+(?:\.\d+)?)(ms|[smhd])?$/;
|
|
683
|
+
var UNIT_MS = { ms: 1, s: 1000, m: 60000, h: 3600000, d: 86400000 };
|
|
684
|
+
function duration(value, name, fallback, unit = "s") {
|
|
685
|
+
if (value === undefined)
|
|
686
|
+
return fallback;
|
|
687
|
+
const match = DURATION.exec(value.trim());
|
|
688
|
+
const ms = match === null ? 0 : Number(match[1]) * UNIT_MS[match[2] ?? unit];
|
|
689
|
+
if (ms < 1)
|
|
690
|
+
badUsage(`--${name} takes a duration such as 30s, 5m, 1h or 7d, not ${value}`);
|
|
691
|
+
return ms;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
// ../kit/src/args.ts
|
|
695
|
+
var DASHES = /^-+/;
|
|
696
|
+
function jsonOption(holds) {
|
|
697
|
+
return { type: "boolean", description: holds === undefined ? "print JSON" : `print JSON: ${holds}` };
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
class Exit extends Error {
|
|
701
|
+
code;
|
|
702
|
+
hint;
|
|
703
|
+
constructor(message, code, hint) {
|
|
704
|
+
super(message);
|
|
705
|
+
this.code = code;
|
|
706
|
+
this.hint = hint;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
function fail(message, code = 2, hint) {
|
|
710
|
+
throw new Exit(message, code, hint);
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
class UsageError extends Exit {
|
|
714
|
+
constructor(message, hint) {
|
|
715
|
+
super(message, 2, hint);
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
function badUsage(message) {
|
|
719
|
+
throw new UsageError(message);
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
class HelpShown extends Exit {
|
|
723
|
+
constructor() {
|
|
724
|
+
super("help shown", 0);
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
class Completing {
|
|
729
|
+
options;
|
|
730
|
+
positionals;
|
|
731
|
+
candidates;
|
|
732
|
+
constructor(options, positionals = [], candidates = {}) {
|
|
733
|
+
this.options = options;
|
|
734
|
+
this.positionals = positionals;
|
|
735
|
+
this.candidates = candidates;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
var completion = { active: false };
|
|
739
|
+
var OUTPUT_OPTIONS = {
|
|
740
|
+
color: { type: "string", description: "auto (on a terminal alone), always or never" },
|
|
741
|
+
plain: { type: "boolean", description: "plain text, no colour or spinner: --color never" }
|
|
742
|
+
};
|
|
743
|
+
function applyOutput(color, plain, env = process8.env) {
|
|
744
|
+
const when = plain === true ? "never" : color ?? "auto";
|
|
745
|
+
if (when === "auto")
|
|
746
|
+
return;
|
|
747
|
+
if (when !== "always" && when !== "never")
|
|
748
|
+
badUsage(`--color takes auto, always or never, not ${when}`);
|
|
749
|
+
if (when === "always") {
|
|
750
|
+
env.FORCE_COLOR = "1";
|
|
751
|
+
delete env.NO_COLOR;
|
|
752
|
+
} else {
|
|
753
|
+
env.NO_COLOR = "1";
|
|
754
|
+
delete env.FORCE_COLOR;
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
function parse(args, usage, options, settings = {}) {
|
|
758
|
+
const output = Object.fromEntries(Object.entries(OUTPUT_OPTIONS).filter(([name]) => !(name in options)));
|
|
759
|
+
const all = { ...options, ...output, help: { type: "boolean", short: "h", description: "show this help" } };
|
|
760
|
+
const listing = listed(all, settings.allowNegative === true);
|
|
761
|
+
if (completion.active)
|
|
762
|
+
throw new Completing(listing, settings.positionals, settings.candidates);
|
|
763
|
+
const { own, rest } = settings.trailing === true ? splitTrailing(args, all, settings.positionals?.length ?? 0) : splitRest(args, settings.rest === true);
|
|
764
|
+
let parsed;
|
|
765
|
+
try {
|
|
766
|
+
parsed = parseArgs({ args: joinDashValues(withBare(withAliases(own, all), all), all), options: all, allowPositionals: true, allowNegative: settings.allowNegative === true, strict: true });
|
|
767
|
+
} catch (error) {
|
|
768
|
+
const message = parseError(error);
|
|
769
|
+
throw new UsageError(message, nearestOption(message, all, settings.allowNegative === true));
|
|
770
|
+
}
|
|
771
|
+
const values = parsed.values;
|
|
772
|
+
applyOutput("color" in output ? values.color : undefined, "plain" in output ? values.plain : undefined);
|
|
773
|
+
if (values.help === true) {
|
|
774
|
+
console.log(wantsHelpJson() ? JSON.stringify(description(usage, settings, listing)) : helpText(usage, listing, undefined, settings));
|
|
775
|
+
throw new HelpShown;
|
|
776
|
+
}
|
|
777
|
+
checkChoices(parsed.values, all);
|
|
778
|
+
checkConflicts(parsed.values, all);
|
|
779
|
+
if (settings.positionals !== undefined)
|
|
780
|
+
checkPositionals(parsed.positionals, settings.positionals);
|
|
781
|
+
return { ...parsed, values: parsed.values, positionals: parsed.positionals, rest };
|
|
782
|
+
}
|
|
783
|
+
function splitRest(args, passes) {
|
|
784
|
+
const separator = passes ? args.indexOf("--") : -1;
|
|
785
|
+
return { own: separator === -1 ? args : args.slice(0, separator), rest: separator === -1 ? undefined : args.slice(separator + 1) };
|
|
786
|
+
}
|
|
787
|
+
function splitTrailing(args, options, count) {
|
|
788
|
+
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
789
|
+
let seen = 0;
|
|
790
|
+
if (count === 0)
|
|
791
|
+
return { own: args, rest: undefined };
|
|
792
|
+
for (let at = 0;at < args.length; at++) {
|
|
793
|
+
const word = args[at];
|
|
794
|
+
if (word === "--")
|
|
795
|
+
break;
|
|
796
|
+
if (word.startsWith("-")) {
|
|
797
|
+
if (takesValue(word))
|
|
798
|
+
at++;
|
|
799
|
+
continue;
|
|
800
|
+
}
|
|
801
|
+
seen++;
|
|
802
|
+
if (seen === count)
|
|
803
|
+
return { own: args.slice(0, at + 1), rest: args.slice(at + 1) };
|
|
804
|
+
}
|
|
805
|
+
return { own: args, rest: undefined };
|
|
806
|
+
}
|
|
807
|
+
function listed(options, allowNegative) {
|
|
808
|
+
if (!allowNegative)
|
|
809
|
+
return options;
|
|
810
|
+
return Object.fromEntries(Object.entries(options).flatMap(([name, option]) => {
|
|
811
|
+
if (option.type !== "boolean" || option.negative === undefined)
|
|
812
|
+
return [[name, option]];
|
|
813
|
+
return [...option.description === undefined ? [] : [[name, option]], [`no-${name}`, { type: "boolean", description: option.negative }]];
|
|
814
|
+
}));
|
|
815
|
+
}
|
|
816
|
+
function withAliases(args, options) {
|
|
817
|
+
const named = new Map(Object.entries(options).flatMap(([name, { aliases = [] }]) => aliases.map((alias) => [`--${alias}`, `--${name}`])));
|
|
818
|
+
if (named.size === 0)
|
|
819
|
+
return args;
|
|
820
|
+
const separator = args.indexOf("--");
|
|
821
|
+
const own = separator === -1 ? args : args.slice(0, separator);
|
|
822
|
+
return [...own.map((arg) => {
|
|
823
|
+
const at = arg.indexOf("=");
|
|
824
|
+
const current = named.get(at === -1 ? arg : arg.slice(0, at));
|
|
825
|
+
return current === undefined ? arg : `${current}${at === -1 ? "" : arg.slice(at)}`;
|
|
826
|
+
}), ...separator === -1 ? [] : args.slice(separator)];
|
|
827
|
+
}
|
|
828
|
+
function withBare(args, options) {
|
|
829
|
+
const bare = new Map(Object.entries(options).flatMap(([name, { short, optional }]) => optional === undefined ? [] : [[`--${name}`, optional], ...short === undefined ? [] : [[`-${short}`, optional]]]));
|
|
830
|
+
if (bare.size === 0)
|
|
831
|
+
return args;
|
|
832
|
+
const separator = args.indexOf("--");
|
|
833
|
+
const own = separator === -1 ? args : args.slice(0, separator);
|
|
834
|
+
return [...own.flatMap((arg, index) => {
|
|
835
|
+
const optional = bare.get(arg);
|
|
836
|
+
const next = own[index + 1];
|
|
837
|
+
const value = next !== undefined && (optional?.accepts === undefined ? !next.startsWith("-") : optional.accepts.test(next));
|
|
838
|
+
return optional === undefined || value ? [arg] : [arg, optional.bare];
|
|
839
|
+
}), ...separator === -1 ? [] : args.slice(separator)];
|
|
840
|
+
}
|
|
841
|
+
function checkConflicts(values, options) {
|
|
842
|
+
const given = (name) => values[name] !== undefined && values[name] !== false;
|
|
843
|
+
for (const [name, { conflicts = [] }] of Object.entries(options)) {
|
|
844
|
+
const other = given(name) ? conflicts.find(given) : undefined;
|
|
845
|
+
if (other !== undefined)
|
|
846
|
+
badUsage(`--${name} cannot be used with --${other}`);
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
function checkChoices(values, options) {
|
|
850
|
+
for (const [name, option] of Object.entries(options)) {
|
|
851
|
+
if (option.choices === undefined)
|
|
852
|
+
continue;
|
|
853
|
+
const given = values[name];
|
|
854
|
+
for (const value of Array.isArray(given) ? given : [given]) {
|
|
855
|
+
if (typeof value === "string")
|
|
856
|
+
oneOf(value, name, option.choices);
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
function oneOf(value, name, choices) {
|
|
861
|
+
if (!choices.includes(value))
|
|
862
|
+
badUsage(`${name.startsWith("<") ? name : `--${name}`} takes one of ${choices.join(", ")}, not ${value}`);
|
|
863
|
+
return value;
|
|
864
|
+
}
|
|
865
|
+
function checkPositionals(given, names) {
|
|
866
|
+
const variadic = names.at(-1)?.replace("]", "").endsWith("...") === true;
|
|
867
|
+
const required = names.filter((name) => !name.startsWith("[")).length;
|
|
868
|
+
if (given.length < required)
|
|
869
|
+
badUsage(`missing ${names[given.length]}`);
|
|
870
|
+
if (!variadic && given.length > names.length)
|
|
871
|
+
badUsage(`unexpected argument ${given[names.length]}`);
|
|
872
|
+
}
|
|
873
|
+
function joinDashValues(args, options) {
|
|
874
|
+
const longOf = new Map(Object.entries(options).flatMap(([name, option]) => option.short === undefined ? [[`--${name}`, name]] : [[`--${name}`, name], [`-${option.short}`, name]]));
|
|
875
|
+
const isOption = (arg) => arg === "--" || longOf.has(arg.includes("=") ? arg.slice(0, arg.indexOf("=")) : arg);
|
|
876
|
+
const joined = [];
|
|
877
|
+
for (let i = 0;i < args.length; i++) {
|
|
878
|
+
const arg = args[i] ?? "";
|
|
879
|
+
if (arg === "--")
|
|
880
|
+
return [...joined, ...args.slice(i)];
|
|
881
|
+
const name = longOf.get(arg);
|
|
882
|
+
const next = args[i + 1];
|
|
883
|
+
if (name !== undefined && options[name]?.type === "string" && next !== undefined && next.startsWith("-") && next !== "-" && !isOption(next)) {
|
|
884
|
+
joined.push(`--${name}=${next}`);
|
|
885
|
+
i++;
|
|
886
|
+
} else {
|
|
887
|
+
joined.push(arg);
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
return joined;
|
|
891
|
+
}
|
|
892
|
+
function parseError(error) {
|
|
893
|
+
const message = upTo(error instanceof Error ? error.message : String(error), ". ");
|
|
894
|
+
return `${message.charAt(0).toLowerCase()}${message.slice(1)}`;
|
|
895
|
+
}
|
|
896
|
+
var UNKNOWN_OPTION = /^unknown option '(-[^']+)'/;
|
|
897
|
+
function nearestOption(message, options, allowNegative) {
|
|
898
|
+
const typed = UNKNOWN_OPTION.exec(message)?.[1];
|
|
899
|
+
if (typed === undefined)
|
|
900
|
+
return;
|
|
901
|
+
const word = typed.replace(DASHES, "");
|
|
902
|
+
if (word.length < 2 && !typed.startsWith("--"))
|
|
903
|
+
return;
|
|
904
|
+
const known = Object.entries(options).flatMap(([name, option]) => [
|
|
905
|
+
...option.short === undefined ? [] : [`-${option.short}`],
|
|
906
|
+
`--${name}`,
|
|
907
|
+
...allowNegative && option.type === "boolean" ? [`--no-${name}`] : []
|
|
908
|
+
]);
|
|
909
|
+
const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
|
|
910
|
+
return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
|
|
911
|
+
}
|
|
912
|
+
// ../kit/src/process.ts
|
|
913
|
+
import { constants } from "os";
|
|
914
|
+
import process9 from "process";
|
|
915
|
+
var KILL_GRACE_MS = 2000;
|
|
916
|
+
function exitCodeOf(code, signal) {
|
|
917
|
+
if (code !== null)
|
|
918
|
+
return code;
|
|
919
|
+
if (typeof signal === "number")
|
|
920
|
+
return 128 + signal;
|
|
921
|
+
return signal === null ? 129 : 128 + constants.signals[signal];
|
|
922
|
+
}
|
|
923
|
+
async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal, maxBytes = Infinity, onChunk, onStart } = {}) {
|
|
924
|
+
const chunks = [];
|
|
925
|
+
const grouped = timeoutMs !== undefined || signal !== undefined || onStart !== undefined || onChunk !== undefined;
|
|
926
|
+
const proc = Bun.spawn(argv, { ...cwd === undefined ? {} : { cwd }, ...env === undefined ? {} : { env }, stdin, stdout: "pipe", stderr: "pipe", ...grouped ? { detached: true } : {} });
|
|
927
|
+
const state = { kept: 0, truncated: false, timedOut: false, failure: undefined, done: false };
|
|
928
|
+
const timers = [];
|
|
929
|
+
const killGroup = (killing) => {
|
|
930
|
+
if (state.done)
|
|
931
|
+
return;
|
|
932
|
+
try {
|
|
933
|
+
if (grouped)
|
|
934
|
+
process9.kill(-proc.pid, killing);
|
|
935
|
+
else
|
|
936
|
+
proc.kill(killing);
|
|
937
|
+
} catch {}
|
|
938
|
+
};
|
|
939
|
+
const stop = () => {
|
|
940
|
+
killGroup("SIGTERM");
|
|
941
|
+
timers.push(setTimeout(killGroup, KILL_GRACE_MS, "SIGKILL"));
|
|
942
|
+
};
|
|
943
|
+
const onAbort = () => stop();
|
|
944
|
+
if (timeoutMs !== undefined) {
|
|
945
|
+
timers.push(setTimeout(() => {
|
|
946
|
+
state.timedOut = true;
|
|
947
|
+
stop();
|
|
948
|
+
}, timeoutMs));
|
|
949
|
+
}
|
|
950
|
+
if (signal?.aborted === true)
|
|
951
|
+
onAbort();
|
|
952
|
+
else
|
|
953
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
954
|
+
onStart?.({ kill: (killing) => killGroup(killing) });
|
|
955
|
+
const drain = async (stream, name) => {
|
|
956
|
+
for await (const data of stream) {
|
|
957
|
+
if (state.failure !== undefined)
|
|
958
|
+
continue;
|
|
959
|
+
try {
|
|
960
|
+
onChunk?.({ stream: name, data });
|
|
961
|
+
} catch (error) {
|
|
962
|
+
state.failure = error;
|
|
963
|
+
stop();
|
|
964
|
+
continue;
|
|
965
|
+
}
|
|
966
|
+
if (state.kept + data.length > maxBytes) {
|
|
967
|
+
state.truncated = true;
|
|
968
|
+
continue;
|
|
969
|
+
}
|
|
970
|
+
state.kept += data.length;
|
|
971
|
+
chunks.push({ stream: name, data });
|
|
972
|
+
}
|
|
973
|
+
};
|
|
974
|
+
try {
|
|
975
|
+
await Promise.all([drain(proc.stdout, "stdout"), drain(proc.stderr, "stderr")]);
|
|
976
|
+
await proc.exited;
|
|
977
|
+
} finally {
|
|
978
|
+
state.done = true;
|
|
979
|
+
for (const timer of timers)
|
|
980
|
+
clearTimeout(timer);
|
|
981
|
+
signal?.removeEventListener("abort", onAbort);
|
|
982
|
+
}
|
|
983
|
+
if (state.failure !== undefined)
|
|
984
|
+
throw state.failure;
|
|
985
|
+
const joined = () => Buffer.concat(chunks.map(({ data }) => data));
|
|
986
|
+
let output;
|
|
987
|
+
let text;
|
|
988
|
+
return {
|
|
989
|
+
code: exitCodeOf(proc.exitCode, proc.signalCode),
|
|
990
|
+
chunks,
|
|
991
|
+
timedOut: state.timedOut,
|
|
992
|
+
truncated: state.truncated,
|
|
993
|
+
get output() {
|
|
994
|
+
return output ??= joined();
|
|
995
|
+
},
|
|
996
|
+
get text() {
|
|
997
|
+
return text ??= (output ??= joined()).toString();
|
|
998
|
+
}
|
|
999
|
+
};
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
// ../kit/src/progress.ts
|
|
1003
|
+
var ESC = String.fromCharCode(27);
|
|
1004
|
+
var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x7E]|\\][^\\u0007${ESC}]*(?:\\u0007|${ESC}\\\\))`, "y");
|
|
1005
|
+
var RESET_STYLE = `${ESC}[0m`;
|
|
1006
|
+
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
1007
|
+
|
|
1008
|
+
// ../kit/src/tool.ts
|
|
1009
|
+
import process10 from "process";
|
|
1010
|
+
function cwdGone() {
|
|
1011
|
+
try {
|
|
1012
|
+
process10.cwd();
|
|
1013
|
+
return false;
|
|
1014
|
+
} catch {
|
|
1015
|
+
return true;
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
class SpawnError extends Error {
|
|
1020
|
+
missing;
|
|
1021
|
+
constructor(message, missing) {
|
|
1022
|
+
super(message);
|
|
1023
|
+
this.missing = missing;
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
function spawnFailure(command, error) {
|
|
1027
|
+
const gone = cwdGone();
|
|
1028
|
+
const missing = !gone && error?.code === "ENOENT";
|
|
1029
|
+
return new SpawnError(`${command}: ${error instanceof Error ? error.message : String(error)}${gone ? " (the working directory was removed)" : ""}`, missing);
|
|
1030
|
+
}
|
|
1031
|
+
async function runTool(label, argv, { timeoutMs, cwd, onStart } = {}) {
|
|
1032
|
+
let ran;
|
|
1033
|
+
try {
|
|
1034
|
+
ran = await runCommand(argv, { env: process10.env, ...timeoutMs === undefined ? {} : { timeoutMs }, ...cwd === undefined ? {} : { cwd }, ...onStart === undefined ? {} : { onStart } });
|
|
1035
|
+
} catch (error) {
|
|
1036
|
+
throw spawnFailure(label, error);
|
|
1037
|
+
}
|
|
1038
|
+
if (ran.timedOut)
|
|
1039
|
+
throw new Error(`${label}: no answer in ${formatDuration(timeoutMs ?? 0)}`);
|
|
1040
|
+
const decoders = { stdout: new TextDecoder, stderr: new TextDecoder };
|
|
1041
|
+
const streams = { stdout: "", stderr: "" };
|
|
1042
|
+
for (const { stream, data } of ran.chunks)
|
|
1043
|
+
streams[stream] += decoders[stream].decode(data, { stream: true });
|
|
1044
|
+
streams.stdout += decoders.stdout.decode();
|
|
1045
|
+
streams.stderr += decoders.stderr.decode();
|
|
1046
|
+
return { code: ran.code, ...streams };
|
|
1047
|
+
}
|
|
1048
|
+
// ../kit/src/cli.ts
|
|
1049
|
+
import { readFileSync as readFileSync2, realpathSync } from "fs";
|
|
1050
|
+
import { dirname as dirname3, join as join5 } from "path";
|
|
1051
|
+
import process12 from "process";
|
|
1052
|
+
|
|
1053
|
+
// ../kit/src/usage.ts
|
|
1054
|
+
import { appendFileSync, mkdirSync } from "fs";
|
|
1055
|
+
import { homedir as homedir3 } from "os";
|
|
1056
|
+
import { dirname as dirname2, isAbsolute, join as join4 } from "path";
|
|
1057
|
+
import process11 from "process";
|
|
1058
|
+
|
|
1059
|
+
// ../kit/src/logs.ts
|
|
1060
|
+
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
1061
|
+
var CLI_FOLDER = "kaynooo-cli";
|
|
1062
|
+
|
|
1063
|
+
// ../kit/src/usage.ts
|
|
1064
|
+
var UNKNOWN_OPTION2 = /unknown option '(--[a-z][a-z0-9-]*)[=']/;
|
|
1065
|
+
var FLAG_NAME = /^(?:--[a-z][a-z0-9-]*|-[a-zA-Z])$/;
|
|
1066
|
+
var LONGEST_NAME = 40;
|
|
1067
|
+
var usageError;
|
|
1068
|
+
function noteUsageError(message) {
|
|
1069
|
+
usageError = message;
|
|
1070
|
+
}
|
|
1071
|
+
function stateHome(env = process11.env) {
|
|
1072
|
+
const dir = env.XDG_STATE_HOME;
|
|
1073
|
+
return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
|
|
1074
|
+
}
|
|
1075
|
+
function usagePath(env = process11.env) {
|
|
1076
|
+
return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
1077
|
+
}
|
|
1078
|
+
function usageRecorded(env = process11.env) {
|
|
1079
|
+
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
1080
|
+
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
1081
|
+
}
|
|
1082
|
+
function flagNames(argv) {
|
|
1083
|
+
const end = argv.indexOf("--");
|
|
1084
|
+
const names = (end === -1 ? argv : argv.slice(0, end)).filter((word) => word.startsWith("-") && word !== "-").map((word) => word.includes("=") ? word.slice(0, word.indexOf("=")) : word);
|
|
1085
|
+
return [...new Set(names)].filter((name) => name.length <= LONGEST_NAME && FLAG_NAME.test(name));
|
|
1086
|
+
}
|
|
1087
|
+
function usageEntry(bin, command, argv, code, now = new Date) {
|
|
1088
|
+
const unknown = code === 2 && usageError !== undefined ? UNKNOWN_OPTION2.exec(usageError)?.[1] : undefined;
|
|
1089
|
+
usageError = undefined;
|
|
1090
|
+
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
1091
|
+
}
|
|
1092
|
+
function recordUsage(entry, env = process11.env) {
|
|
1093
|
+
if (!usageRecorded(env))
|
|
1094
|
+
return;
|
|
1095
|
+
try {
|
|
1096
|
+
const path = usagePath(env);
|
|
1097
|
+
mkdirSync(dirname2(path), { recursive: true, mode: 448 });
|
|
1098
|
+
appendFileSync(path, `${JSON.stringify(entry)}
|
|
1099
|
+
`, { mode: 384 });
|
|
1100
|
+
} catch {}
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
// ../kit/src/cli.ts
|
|
1104
|
+
var LEVEL_OPTIONS = {
|
|
1105
|
+
...OUTPUT_OPTIONS,
|
|
1106
|
+
help: { type: "boolean", short: "h", description: "show this help" }
|
|
1107
|
+
};
|
|
1108
|
+
var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
|
|
1109
|
+
async function commandRows(all, colors) {
|
|
1110
|
+
const commands = shown(all);
|
|
1111
|
+
const width = Math.max(0, ...Object.keys(commands).map((name) => name.length));
|
|
1112
|
+
const groups = new Map([["Commands:", []]]);
|
|
1113
|
+
const summaries = await Promise.all(Object.values(commands).map(async ({ summary }) => resolveSummary(summary)));
|
|
1114
|
+
for (const [index, [name, { group }]] of Object.entries(commands).entries()) {
|
|
1115
|
+
const heading = group === undefined ? "Commands:" : `${group}:`;
|
|
1116
|
+
const rows = groups.get(heading) ?? [];
|
|
1117
|
+
rows.push(` ${colors.cyan(name.padEnd(width))} ${colors.dim(summaries[index])}`);
|
|
1118
|
+
groups.set(heading, rows);
|
|
1119
|
+
}
|
|
1120
|
+
return [...groups].filter(([, rows]) => rows.length > 0).flatMap(([heading, rows], index) => [...index === 0 ? [] : [""], colors.bold(heading), ...rows]);
|
|
1121
|
+
}
|
|
1122
|
+
function shown(commands) {
|
|
1123
|
+
return Object.fromEntries(Object.entries(commands).filter(([, { hidden }]) => hidden !== true));
|
|
1124
|
+
}
|
|
1125
|
+
function takeOutputFlags(argv) {
|
|
1126
|
+
let index = 0;
|
|
1127
|
+
for (;index < argv.length; index++) {
|
|
1128
|
+
const word = argv[index];
|
|
1129
|
+
if (word === "--plain")
|
|
1130
|
+
applyOutput(undefined, true);
|
|
1131
|
+
else if (word.startsWith("--color="))
|
|
1132
|
+
applyOutput(word.slice("--color=".length), undefined);
|
|
1133
|
+
else if (word === "--color" && argv[index + 1] !== undefined)
|
|
1134
|
+
applyOutput(argv[++index], undefined);
|
|
1135
|
+
else
|
|
1136
|
+
break;
|
|
1137
|
+
}
|
|
1138
|
+
return argv.slice(index);
|
|
1139
|
+
}
|
|
1140
|
+
async function resolveSummary(summary) {
|
|
1141
|
+
return typeof summary === "string" ? summary : summary();
|
|
1142
|
+
}
|
|
1143
|
+
function scopeOf(commands, extra, aliases) {
|
|
1144
|
+
return { commands: typeof commands === "function" ? {} : commands, lazy: [...typeof commands === "function" ? [commands] : [], ...extra === undefined ? [] : [extra]], aliases };
|
|
1145
|
+
}
|
|
1146
|
+
async function expand(scope, words) {
|
|
1147
|
+
if (scope.lazy.length === 0)
|
|
1148
|
+
return scope;
|
|
1149
|
+
const commands = { ...scope.commands };
|
|
1150
|
+
for (const added of await Promise.all(scope.lazy.map(async (lazy) => lazy(words)))) {
|
|
1151
|
+
for (const [name, command] of Object.entries(added))
|
|
1152
|
+
commands[name] ??= command;
|
|
1153
|
+
}
|
|
1154
|
+
return { ...scope, commands, lazy: [] };
|
|
1155
|
+
}
|
|
1156
|
+
async function help2({ path, header, summary: lazySummary, commands, defaultCommand, examples, replaces }, stream) {
|
|
1157
|
+
const colors = paint(stream);
|
|
1158
|
+
const prefix = path.join(" ");
|
|
1159
|
+
const summary = await resolveSummary(lazySummary);
|
|
1160
|
+
if (wantsHelpJson())
|
|
1161
|
+
return JSON.stringify(description(`${prefix} <command> [options]: ${summary}`, { examples, replaces }));
|
|
1162
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
1163
|
+
const options = fallback !== undefined && "run" in fallback ? await optionsOf(fallback.run) : undefined;
|
|
1164
|
+
const own = Object.entries(options ?? {}).filter(([name]) => !(name in TOP_OPTIONS));
|
|
1165
|
+
return [
|
|
1166
|
+
header(colors, summary),
|
|
1167
|
+
"",
|
|
1168
|
+
usageLine(`${prefix} ${defaultCommand === undefined ? colors.cyan("<command>") : `[${colors.cyan("<command>")}]`} [options]`, colors),
|
|
1169
|
+
...replacesRows(replaces, colors),
|
|
1170
|
+
"",
|
|
1171
|
+
...await commandRows(commands, colors),
|
|
1172
|
+
...own.length === 0 ? [] : ["", `${colors.bold(`Options of ${defaultCommand}:`)} ${colors.dim("the command run when none is named")}`, ...optionRows(Object.fromEntries(own), colors)],
|
|
1173
|
+
"",
|
|
1174
|
+
colors.bold("Options:"),
|
|
1175
|
+
...optionRows(path.length === 1 ? TOP_OPTIONS : LEVEL_OPTIONS, colors),
|
|
1176
|
+
...exampleRows(examples ?? [], colors),
|
|
1177
|
+
"",
|
|
1178
|
+
`${colors.dim("Run")} ${prefix} ${colors.cyan("<command>")} --help ${colors.dim("for its options.")}`
|
|
1179
|
+
].join(`
|
|
1180
|
+
`);
|
|
1181
|
+
}
|
|
1182
|
+
function printError(prefix, message, hint, cause) {
|
|
1183
|
+
const colors = paint(process12.stderr);
|
|
1184
|
+
const styled = takesControlCodes(process12.stderr);
|
|
1185
|
+
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
1186
|
+
const under = (label, text, tint) => text === undefined ? "" : `
|
|
1187
|
+
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
1188
|
+
console.error(`${head} ${message}${under("cause", cause, colors.dim)}${under("hint", hint, colors.yellow)}`);
|
|
1189
|
+
}
|
|
1190
|
+
function printJsonError(message, hint, cause) {
|
|
1191
|
+
console.error(JSON.stringify({ error: message, ...hint === undefined ? {} : { hint }, ...cause === undefined ? {} : { cause } }));
|
|
1192
|
+
}
|
|
1193
|
+
function wantsJson(argv) {
|
|
1194
|
+
const end = argv.indexOf("--");
|
|
1195
|
+
return (end === -1 ? argv : argv.slice(0, end)).includes("--json");
|
|
1196
|
+
}
|
|
1197
|
+
function reportThrown(prefix, error, redact = (text) => text, json = false) {
|
|
1198
|
+
if (error instanceof HelpShown)
|
|
1199
|
+
return 0;
|
|
1200
|
+
const help = error instanceof UsageError ? `run ${prefix} --help for its options` : undefined;
|
|
1201
|
+
const named = error instanceof Error && !(error instanceof Exit) && error.name !== "Error" ? `${error.name}: ${error.message}` : undefined;
|
|
1202
|
+
const cause = error instanceof Error && error.cause instanceof Error ? redact(error.cause.message) : undefined;
|
|
1203
|
+
if (error instanceof Exit && error.code === 2)
|
|
1204
|
+
noteUsageError(error.message);
|
|
1205
|
+
const report = json ? printJsonError : (message, hint, causeText) => printError(prefix, message, hint, causeText);
|
|
1206
|
+
report(redact(named ?? (error instanceof Error ? error.message : String(error))), error instanceof Exit ? error.hint ?? help : undefined, cause);
|
|
1207
|
+
return error instanceof Exit ? error.code : 1;
|
|
1208
|
+
}
|
|
1209
|
+
async function dispatch(level, typed, { version, redact }) {
|
|
1210
|
+
const prefix = level.path.join(" ");
|
|
1211
|
+
let argv;
|
|
1212
|
+
try {
|
|
1213
|
+
argv = takeOutputFlags(typed);
|
|
1214
|
+
} catch (error) {
|
|
1215
|
+
return reportThrown(prefix, error);
|
|
1216
|
+
}
|
|
1217
|
+
const [name, ...args] = argv;
|
|
1218
|
+
const fallback = level.defaultCommand === undefined ? undefined : level.commands[level.defaultCommand];
|
|
1219
|
+
if (name === undefined && level.runBare && fallback !== undefined && "run" in fallback) {
|
|
1220
|
+
try {
|
|
1221
|
+
return await fallback.run([]);
|
|
1222
|
+
} catch (error) {
|
|
1223
|
+
return reportThrown(`${prefix} ${level.defaultCommand}`, error, redact, false);
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
if (name === undefined && level.path.length > 1) {
|
|
1227
|
+
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process12.stderr));
|
|
1228
|
+
return 2;
|
|
1229
|
+
}
|
|
1230
|
+
if (name === undefined || name === "help" || name === "--help" || name === "-h") {
|
|
1231
|
+
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process12.stdout));
|
|
1232
|
+
return 0;
|
|
1233
|
+
}
|
|
1234
|
+
if (level.path.length === 1 && (name === "--version" || name === "-v")) {
|
|
1235
|
+
console.log(version);
|
|
1236
|
+
return 0;
|
|
1237
|
+
}
|
|
1238
|
+
let command = level.commands[level.aliases?.[name] ?? name];
|
|
1239
|
+
let rest = args;
|
|
1240
|
+
let ran = name;
|
|
1241
|
+
if (command === undefined && level.lazy.length > 0) {
|
|
1242
|
+
level = await expand(level, [...level.path.slice(1), ...argv]);
|
|
1243
|
+
command = level.commands[level.aliases?.[name] ?? name];
|
|
1244
|
+
}
|
|
1245
|
+
if (command === undefined && level.defaultCommand !== undefined) {
|
|
1246
|
+
command = level.commands[level.defaultCommand];
|
|
1247
|
+
rest = argv;
|
|
1248
|
+
ran = level.defaultCommand;
|
|
1249
|
+
}
|
|
1250
|
+
if (command === undefined) {
|
|
1251
|
+
const meant = closest(name, Object.keys(shown(level.commands)));
|
|
1252
|
+
printError(prefix, `no command ${name}`, meant === undefined ? `run ${prefix} --help for its commands` : `did you mean ${meant}?`);
|
|
1253
|
+
return 2;
|
|
1254
|
+
}
|
|
1255
|
+
if ("commands" in command) {
|
|
1256
|
+
const { summary } = command;
|
|
1257
|
+
return dispatch({ path: [...level.path, name], header: (colors, text) => `${colors.bold(`${prefix} ${name}`)} ${text}`, summary, ...scopeOf(command.commands, command.extraCommands, command.aliases), defaultCommand: undefined, runBare: false, examples: command.examples, replaces: undefined }, rest, { version, redact });
|
|
1258
|
+
}
|
|
1259
|
+
try {
|
|
1260
|
+
return await command.run(rest);
|
|
1261
|
+
} catch (error) {
|
|
1262
|
+
return reportThrown(`${prefix} ${ran}`, error, redact, wantsJson(rest));
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
var COMPLETE = "__complete";
|
|
1266
|
+
var FILES = "__files__";
|
|
1267
|
+
async function completingOf(run) {
|
|
1268
|
+
completion.active = true;
|
|
1269
|
+
try {
|
|
1270
|
+
await run([]);
|
|
1271
|
+
} catch (error) {
|
|
1272
|
+
if (error instanceof Completing)
|
|
1273
|
+
return error;
|
|
1274
|
+
} finally {
|
|
1275
|
+
completion.active = false;
|
|
1276
|
+
}
|
|
1277
|
+
return new Completing({});
|
|
1278
|
+
}
|
|
1279
|
+
async function optionsOf(run) {
|
|
1280
|
+
return (await completingOf(run)).options;
|
|
1281
|
+
}
|
|
1282
|
+
function candidateLine(candidate) {
|
|
1283
|
+
return typeof candidate === "string" ? candidate : candidate.join("\t");
|
|
1284
|
+
}
|
|
1285
|
+
function positionalBeingTyped(before, { options, positionals }) {
|
|
1286
|
+
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
1287
|
+
let index = 0;
|
|
1288
|
+
let at = 0;
|
|
1289
|
+
while (at < before.length) {
|
|
1290
|
+
const word = before[at];
|
|
1291
|
+
if (word === "--") {
|
|
1292
|
+
index += before.length - at - 1;
|
|
1293
|
+
break;
|
|
1294
|
+
}
|
|
1295
|
+
if (!word.startsWith("-"))
|
|
1296
|
+
index++;
|
|
1297
|
+
else if (takesValue(word))
|
|
1298
|
+
at++;
|
|
1299
|
+
at++;
|
|
1300
|
+
}
|
|
1301
|
+
const last = positionals.at(-1);
|
|
1302
|
+
if (index < positionals.length)
|
|
1303
|
+
return positionals[index];
|
|
1304
|
+
return last?.replace("]", "").endsWith("...") === true ? last : undefined;
|
|
1305
|
+
}
|
|
1306
|
+
async function leafCandidates(run, words) {
|
|
1307
|
+
const completing = await completingOf(run);
|
|
1308
|
+
if ((words.at(-1) ?? "").startsWith("-"))
|
|
1309
|
+
return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
|
|
1310
|
+
const flag = words.at(-2);
|
|
1311
|
+
const option = flag?.startsWith("-") === true ? Object.entries(completing.options).find(([name, { short }]) => flag === `--${name}` || flag === `-${short}`)?.[1] : undefined;
|
|
1312
|
+
if (option?.type === "string") {
|
|
1313
|
+
const values = option.choices === undefined ? await option.candidates?.() : option.choices;
|
|
1314
|
+
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
1315
|
+
}
|
|
1316
|
+
const name = positionalBeingTyped(words.slice(0, -1), completing);
|
|
1317
|
+
const values = name === undefined ? undefined : await completing.candidates[name]?.();
|
|
1318
|
+
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
1319
|
+
}
|
|
1320
|
+
async function candidates(scope, defaultCommand, words, before = []) {
|
|
1321
|
+
const [word = "", ...rest] = words;
|
|
1322
|
+
const listing = rest.length === 0 && !word.startsWith("-");
|
|
1323
|
+
let { commands, aliases } = scope;
|
|
1324
|
+
let command = commands[aliases?.[word] ?? word];
|
|
1325
|
+
if (listing || command === undefined) {
|
|
1326
|
+
({ commands, aliases } = await expand(scope, [...before, ...words]));
|
|
1327
|
+
command = commands[aliases?.[word] ?? word];
|
|
1328
|
+
}
|
|
1329
|
+
if (listing) {
|
|
1330
|
+
const own = await Promise.all(Object.entries(shown(commands)).map(async ([name, { summary }]) => `${name} ${await resolveSummary(summary)}`));
|
|
1331
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
1332
|
+
const values = fallback !== undefined && "run" in fallback ? await leafCandidates(fallback.run, [word]) : [];
|
|
1333
|
+
const valueOf = (line) => line.slice(0, line.includes("\t") ? line.indexOf("\t") : undefined);
|
|
1334
|
+
return [...own, ...values.filter((line) => line !== FILES && !(valueOf(line) in commands))];
|
|
1335
|
+
}
|
|
1336
|
+
if (command === undefined) {
|
|
1337
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
1338
|
+
return fallback !== undefined && "run" in fallback ? leafCandidates(fallback.run, words) : [];
|
|
1339
|
+
}
|
|
1340
|
+
return "commands" in command ? candidates(scopeOf(command.commands, command.extraCommands, command.aliases), undefined, rest, [...before, word]) : leafCandidates(command.run, rest);
|
|
1341
|
+
}
|
|
1342
|
+
async function printCandidates(found) {
|
|
1343
|
+
for (const line of await found)
|
|
1344
|
+
console.log(line);
|
|
1345
|
+
return 0;
|
|
1346
|
+
}
|
|
1347
|
+
function packageVersion(entry = Bun.main) {
|
|
1348
|
+
for (let dir = dirname3(realpathSync(entry));; dir = dirname3(dir)) {
|
|
1349
|
+
try {
|
|
1350
|
+
const { version } = JSON.parse(readFileSync2(join5(dir, "package.json"), "utf8"));
|
|
1351
|
+
if (typeof version === "string")
|
|
1352
|
+
return version;
|
|
1353
|
+
} catch {}
|
|
1354
|
+
if (dirname3(dir) === dir)
|
|
1355
|
+
return "0.0.0";
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
async function runCli(cli, argv = process12.argv.slice(2)) {
|
|
1359
|
+
const version = cli.version ?? packageVersion();
|
|
1360
|
+
if (argv[0] === COMPLETE)
|
|
1361
|
+
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
1362
|
+
const code = await dispatch({ path: [cli.name], header: (colors, about) => `${colors.bold(cli.name)} ${colors.dim(version)} ${about}`, summary: cli.about, ...scopeOf(cli.commands, cli.extraCommands, cli.aliases), defaultCommand: cli.defaultCommand, runBare: cli.runBare === true, examples: cli.examples, replaces: cli.replaces }, argv, { version, redact: cli.redact });
|
|
1363
|
+
if (cli.usage !== false) {
|
|
1364
|
+
try {
|
|
1365
|
+
recordUsage(usageEntry(cli.name, commandPath(scopeOf(cli.commands, cli.extraCommands, cli.aliases), argv), argv, code));
|
|
1366
|
+
} catch {}
|
|
1367
|
+
}
|
|
1368
|
+
return code;
|
|
1369
|
+
}
|
|
1370
|
+
function commandPath(scope, argv) {
|
|
1371
|
+
const path = [];
|
|
1372
|
+
let level = scope;
|
|
1373
|
+
for (const word of argv.filter((each) => !each.startsWith("-"))) {
|
|
1374
|
+
const name = level.aliases !== undefined && Object.hasOwn(level.aliases, word) ? level.aliases[word] ?? word : word;
|
|
1375
|
+
const command = Object.hasOwn(level.commands, name) ? level.commands[name] : undefined;
|
|
1376
|
+
if (command === undefined)
|
|
1377
|
+
break;
|
|
1378
|
+
path.push(word);
|
|
1379
|
+
if (!("commands" in command))
|
|
1380
|
+
break;
|
|
1381
|
+
level = scopeOf(command.commands, command.extraCommands, command.aliases);
|
|
1382
|
+
}
|
|
1383
|
+
return path.length === 0 ? undefined : path.join(" ");
|
|
1384
|
+
}
|
|
1385
|
+
async function runMain({ name, version, run, redact, usage }, argv = process12.argv.slice(2)) {
|
|
1386
|
+
if (argv[0] === COMPLETE)
|
|
1387
|
+
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
1388
|
+
if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
|
|
1389
|
+
console.log(version ?? packageVersion());
|
|
1390
|
+
return 0;
|
|
1391
|
+
}
|
|
1392
|
+
let code;
|
|
1393
|
+
try {
|
|
1394
|
+
code = await run(argv);
|
|
1395
|
+
} catch (error) {
|
|
1396
|
+
code = reportThrown(name, error, redact, wantsJson(argv));
|
|
1397
|
+
}
|
|
1398
|
+
if (usage !== false) {
|
|
1399
|
+
try {
|
|
1400
|
+
recordUsage(usageEntry(name, undefined, argv, code));
|
|
1401
|
+
} catch {}
|
|
1402
|
+
}
|
|
1403
|
+
return code;
|
|
1404
|
+
}
|
|
1405
|
+
// ../kit/src/secrets.ts
|
|
1406
|
+
var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "session", "jwt", "ssn", "iban", "card", "cvv", "otp"]);
|
|
1407
|
+
// ../kit/src/sessions.ts
|
|
1408
|
+
var {Glob } = globalThis.Bun;
|
|
1409
|
+
var SESSION_FILES = new Glob("*.json");
|
|
1410
|
+
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
1411
|
+
// ../kit/src/size.ts
|
|
1412
|
+
var SIZE = /^(\d+(?:\.\d+)?)\s*([kmgt]?)i?b?$/i;
|
|
1413
|
+
var POWERS = { "": 0, k: 1, m: 2, g: 3, t: 4 };
|
|
1414
|
+
function parseSize(text, bareUnit = 1) {
|
|
1415
|
+
const match = SIZE.exec(text.trim());
|
|
1416
|
+
if (match === null)
|
|
1417
|
+
return;
|
|
1418
|
+
const suffix = match[2].toLowerCase();
|
|
1419
|
+
return Math.round(Number(match[1]) * (suffix === "" ? bareUnit : 1024 ** POWERS[suffix]));
|
|
1420
|
+
}
|
|
1421
|
+
// src/audit.ts
|
|
1422
|
+
import { existsSync as existsSync5, readdirSync, statSync as statSync2 } from "fs";
|
|
1423
|
+
import { readdir } from "fs/promises";
|
|
1424
|
+
import { join as join7, relative, resolve as resolve3 } from "path";
|
|
1425
|
+
import process14 from "process";
|
|
1426
|
+
|
|
1427
|
+
// src/report.ts
|
|
1428
|
+
import { existsSync as existsSync4, readFileSync as readFileSync3 } from "fs";
|
|
1429
|
+
import { join as join6 } from "path";
|
|
1430
|
+
var UPPER_BOUND = /<\s*(\d[\w.+-]*)/;
|
|
1431
|
+
var DEPENDENCY_KINDS = ["dependencies", "devDependencies", "optionalDependencies", "peerDependencies"];
|
|
1432
|
+
var GHSA = /GHSA-[\w-]+$/;
|
|
1433
|
+
function parseAudit(text) {
|
|
1434
|
+
try {
|
|
1435
|
+
const parsed = JSON.parse(text);
|
|
1436
|
+
return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed : undefined;
|
|
1437
|
+
} catch {
|
|
1438
|
+
return;
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
function fixedAt(range) {
|
|
1442
|
+
const bounds = range.split("||").map((part) => UPPER_BOUND.exec(part)?.[1]);
|
|
1443
|
+
if (bounds.length === 0 || bounds.includes(undefined))
|
|
1444
|
+
return;
|
|
1445
|
+
return bounds.reduce((highest, bound) => Bun.semver.order(bound, highest) > 0 ? bound : highest);
|
|
1446
|
+
}
|
|
1447
|
+
function fixFor(advisories) {
|
|
1448
|
+
const fixes = advisories.map((advisory) => fixedAt(advisory.vulnerable_versions));
|
|
1449
|
+
if (fixes.length === 0 || fixes.includes(undefined))
|
|
1450
|
+
return;
|
|
1451
|
+
return fixes.reduce((highest, fix) => Bun.semver.order(fix, highest) > 0 ? fix : highest);
|
|
1452
|
+
}
|
|
1453
|
+
function findingsOf(audit, reaching, direct, level, ignore = []) {
|
|
1454
|
+
const found = [];
|
|
1455
|
+
for (const [name, all] of Object.entries(audit)) {
|
|
1456
|
+
const kept = all.filter((advisory) => severityRank(advisory.severity) >= severityRank(level) && !isIgnored({ id: advisory.id, ghsa: GHSA.exec(advisory.url)?.[0] }, ignore));
|
|
1457
|
+
if (kept.length === 0)
|
|
1458
|
+
continue;
|
|
1459
|
+
const advisories = kept.map((advisory) => ({ id: advisory.id, ghsa: GHSA.exec(advisory.url)?.[0], url: advisory.url, title: advisory.title, severity: advisory.severity, fixed: fixedAt(advisory.vulnerable_versions) }));
|
|
1460
|
+
found.push({
|
|
1461
|
+
package: name,
|
|
1462
|
+
severity: advisories.reduce((worst, advisory) => severityRank(advisory.severity) > severityRank(worst) ? advisory.severity : worst, advisories[0].severity),
|
|
1463
|
+
advisories,
|
|
1464
|
+
scope: reaching.has(name) ? "prod" : "dev",
|
|
1465
|
+
direct: direct.has(name),
|
|
1466
|
+
fix: fixFor(kept)
|
|
1467
|
+
});
|
|
1468
|
+
}
|
|
1469
|
+
return found.toSorted((a, b) => severityRank(b.severity) - severityRank(a.severity) || Number(b.scope === "prod") - Number(a.scope === "prod") || a.package.localeCompare(b.package));
|
|
1470
|
+
}
|
|
1471
|
+
function readManifest(path) {
|
|
1472
|
+
try {
|
|
1473
|
+
return existsSync4(path) ? JSON.parse(readFileSync3(path, "utf8")) : {};
|
|
1474
|
+
} catch {
|
|
1475
|
+
return {};
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
function namesIn(manifest) {
|
|
1479
|
+
return DEPENDENCY_KINDS.flatMap((kind) => {
|
|
1480
|
+
const section = manifest[kind];
|
|
1481
|
+
return typeof section === "object" && section !== null ? Object.keys(section) : [];
|
|
1482
|
+
});
|
|
1483
|
+
}
|
|
1484
|
+
function directDependencies(root, workspaces) {
|
|
1485
|
+
const names = new Set(namesIn(readManifest(join6(root, "package.json"))));
|
|
1486
|
+
for (const workspace of workspaces) {
|
|
1487
|
+
for (const name of namesIn(readManifest(join6(root, workspace, "package.json"))))
|
|
1488
|
+
names.add(name);
|
|
1489
|
+
}
|
|
1490
|
+
return names;
|
|
1491
|
+
}
|
|
1492
|
+
function workspacePatterns(field) {
|
|
1493
|
+
if (Array.isArray(field))
|
|
1494
|
+
return field;
|
|
1495
|
+
const packages = typeof field === "object" && field !== null ? field.packages : undefined;
|
|
1496
|
+
return Array.isArray(packages) ? packages : [];
|
|
1497
|
+
}
|
|
1498
|
+
function workspaceFolders(root, children) {
|
|
1499
|
+
const patterns = workspacePatterns(readManifest(join6(root, "package.json")).workspaces);
|
|
1500
|
+
return patterns.filter((pattern) => typeof pattern === "string").flatMap((pattern) => pattern.endsWith("/*") ? children(pattern.slice(0, -2)).map((name) => join6(pattern.slice(0, -2), name)) : [pattern]);
|
|
1501
|
+
}
|
|
1502
|
+
|
|
1503
|
+
// src/audit.ts
|
|
1504
|
+
var USAGE = `audit [options] [root]: the known vulnerabilities of the Bun repositories under root (the current folder by default), one table per repository
|
|
1505
|
+
|
|
1506
|
+
Each package with an advisory is one row: its worst severity, how many advisories, whether a production dependency needs it (prod) or only a development one (dev), whether the repository asks for it itself (direct) or a dependency does (the row then says via), and the version that fixes every advisory at once, "-" when an advisory names none. The worst and the production ones come first.
|
|
1507
|
+
It runs \`bun audit --json\` in each repository holding a bun.lock, which reaches the registry; the walk goes three levels deep, stops at a bun.lock and skips node_modules and hidden folders.
|
|
1508
|
+
|
|
1509
|
+
Reach for it to sweep many repositories at once; \`pkg audit\` vets the one repository in a folder (package age, install scripts, licences) as well as its advisories, and shares --level, --prod and --ignore.`;
|
|
1510
|
+
var EXITS = [[0, "no repository has an advisory at the level asked"], [1, "one has"], [2, "a repository's audit failed, no bun.lock sits under root, a usage error or a root that is no folder"]];
|
|
1511
|
+
var OPTIONS = {
|
|
1512
|
+
level: { type: "string", choices: SEVERITIES, default: "moderate", description: "only the advisories at least this severe: low, moderate, high or critical" },
|
|
1513
|
+
prod: { type: "boolean", description: "only the packages a production dependency needs" },
|
|
1514
|
+
ignore: { type: "string", multiple: true, description: "an advisory skipped by GHSA or numeric id, repeatable" },
|
|
1515
|
+
json: jsonOption("[{repo, path, findings: [{package, severity, scope, direct, fix, advisories: [{id, ghsa, url, title, severity, fixed}]}], error}]")
|
|
1516
|
+
};
|
|
1517
|
+
var MAX_DEPTH = 3;
|
|
1518
|
+
var CONCURRENCY = 4;
|
|
1519
|
+
var AUDIT_MS = 60000;
|
|
1520
|
+
function holdsLock(dir) {
|
|
1521
|
+
return existsSync5(join7(dir, "bun.lock")) || existsSync5(join7(dir, "bun.lockb"));
|
|
1522
|
+
}
|
|
1523
|
+
async function findRepos(root) {
|
|
1524
|
+
if (holdsLock(root))
|
|
1525
|
+
return [root];
|
|
1526
|
+
const found = [];
|
|
1527
|
+
async function walk(dir, depth) {
|
|
1528
|
+
let entries;
|
|
1529
|
+
try {
|
|
1530
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
1531
|
+
} catch {
|
|
1532
|
+
return;
|
|
1533
|
+
}
|
|
1534
|
+
for (const entry of entries.toSorted((a, b) => a.name.localeCompare(b.name))) {
|
|
1535
|
+
if (!entry.isDirectory() || entry.name === "node_modules" || entry.name.startsWith("."))
|
|
1536
|
+
continue;
|
|
1537
|
+
const path = join7(dir, entry.name);
|
|
1538
|
+
if (holdsLock(path))
|
|
1539
|
+
found.push(path);
|
|
1540
|
+
else if (depth < MAX_DEPTH)
|
|
1541
|
+
await walk(path, depth + 1);
|
|
1542
|
+
}
|
|
1543
|
+
}
|
|
1544
|
+
await walk(root, 1);
|
|
1545
|
+
return found;
|
|
1546
|
+
}
|
|
1547
|
+
async function pool(items, limit, task) {
|
|
1548
|
+
const results = Array.from({ length: items.length });
|
|
1549
|
+
let next = 0;
|
|
1550
|
+
async function worker() {
|
|
1551
|
+
while (next < items.length) {
|
|
1552
|
+
const index = next++;
|
|
1553
|
+
results[index] = await task(items[index]);
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
|
|
1557
|
+
return results;
|
|
1558
|
+
}
|
|
1559
|
+
function children(root) {
|
|
1560
|
+
return (folder) => {
|
|
1561
|
+
try {
|
|
1562
|
+
return readdirSync(join7(root, folder), { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => entry.name).sort();
|
|
1563
|
+
} catch {
|
|
1564
|
+
return [];
|
|
1565
|
+
}
|
|
1566
|
+
};
|
|
1567
|
+
}
|
|
1568
|
+
async function audited(path, shown, level, ignore) {
|
|
1569
|
+
const [all, production] = await Promise.all([
|
|
1570
|
+
runCommand(["bun", "audit", "--json"], { cwd: path, timeoutMs: AUDIT_MS }),
|
|
1571
|
+
runCommand(["bun", "audit", "--json", "--prod"], { cwd: path, timeoutMs: AUDIT_MS })
|
|
1572
|
+
]);
|
|
1573
|
+
const audit = parseAudit(stdoutOf(all));
|
|
1574
|
+
const reached = parseAudit(stdoutOf(production));
|
|
1575
|
+
if (audit === undefined || reached === undefined)
|
|
1576
|
+
return { repo: shown, path, findings: [], error: failure(audit === undefined ? all : production) };
|
|
1577
|
+
const reaching = new Set(Object.keys(reached));
|
|
1578
|
+
const direct = directDependencies(path, workspaceFolders(path, children(path)));
|
|
1579
|
+
return { repo: shown, path, findings: findingsOf(audit, reaching, direct, level, ignore), error: undefined };
|
|
1580
|
+
}
|
|
1581
|
+
function stdoutOf(ran) {
|
|
1582
|
+
return Buffer.concat(ran.chunks.filter((chunk) => chunk.stream === "stdout").map((chunk) => chunk.data)).toString();
|
|
1583
|
+
}
|
|
1584
|
+
function failure(ran) {
|
|
1585
|
+
return ran.timedOut ? `timed out after ${AUDIT_MS / 1000}s` : ran.text.trim().split(`
|
|
1586
|
+
`).at(-1) ?? `exit ${ran.code}`;
|
|
1587
|
+
}
|
|
1588
|
+
function tint(severity) {
|
|
1589
|
+
const colors = paint(process14.stdout);
|
|
1590
|
+
if (severity === "critical" || severity === "high")
|
|
1591
|
+
return colors.red;
|
|
1592
|
+
return severity === "moderate" ? colors.yellow : colors.dim;
|
|
1593
|
+
}
|
|
1594
|
+
function rows(findings) {
|
|
1595
|
+
return findings.map((found) => [
|
|
1596
|
+
found.package,
|
|
1597
|
+
tint(found.severity)(found.severity),
|
|
1598
|
+
String(found.advisories.length),
|
|
1599
|
+
found.scope,
|
|
1600
|
+
found.direct ? "direct" : "via",
|
|
1601
|
+
found.fix ?? "-"
|
|
1602
|
+
]);
|
|
1603
|
+
}
|
|
1604
|
+
async function audit(args) {
|
|
1605
|
+
const { values, positionals } = parse(args, USAGE, OPTIONS, { positionals: ["[root]"], exits: EXITS, replaces: "a bun audit run in each repository and a read of every advisory to find what is worth fixing", examples: ["audit", "audit ~/Workspace --level high --prod"] });
|
|
1606
|
+
const root = resolve3(positionals[0] ?? ".");
|
|
1607
|
+
if (!existsSync5(root) || !statSync2(root).isDirectory())
|
|
1608
|
+
fail(`no folder ${root}`);
|
|
1609
|
+
const level = oneOf(values.level, "level", SEVERITIES);
|
|
1610
|
+
const paths = await findRepos(root);
|
|
1611
|
+
if (paths.length === 0)
|
|
1612
|
+
fail(`no bun.lock under ${root}`);
|
|
1613
|
+
const audits = await pool(paths, CONCURRENCY, async (path) => audited(path, relative(root, path) || ".", level, values.ignore ?? []));
|
|
1614
|
+
const kept = audits.map((each) => ({ ...each, findings: values.prod === true ? each.findings.filter((found) => found.scope === "prod") : each.findings }));
|
|
1615
|
+
if (values.json === true)
|
|
1616
|
+
printJson(kept);
|
|
1617
|
+
else
|
|
1618
|
+
show(kept, audits.length);
|
|
1619
|
+
if (kept.some((each) => each.error !== undefined))
|
|
1620
|
+
return 2;
|
|
1621
|
+
return kept.some((each) => each.findings.length > 0) ? 1 : 0;
|
|
1622
|
+
}
|
|
1623
|
+
function show(audits, total) {
|
|
1624
|
+
const label = role("label", process14.stdout);
|
|
1625
|
+
const path = role("path", process14.stdout);
|
|
1626
|
+
const flagged = audits.filter((each) => each.error !== undefined || each.findings.length > 0);
|
|
1627
|
+
if (flagged.length === 0) {
|
|
1628
|
+
console.log(label(`no advisory in ${total} ${total === 1 ? "repository" : "repositories"}`));
|
|
1629
|
+
return;
|
|
1630
|
+
}
|
|
1631
|
+
for (const each of flagged) {
|
|
1632
|
+
console.log(`${path(each.repo)} ${label(each.error === undefined ? `${each.findings.length} ${each.findings.length === 1 ? "package" : "packages"}` : `audit failed: ${each.error}`)}`);
|
|
1633
|
+
if (each.findings.length > 0)
|
|
1634
|
+
console.log(table([["package", "severity", "advisories", "scope", "origin", "fix"], ...rows(each.findings)], { header: true, align: ["left", "left", "right", "left", "left", "left"] }));
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
|
|
1638
|
+
// src/repos.ts
|
|
1639
|
+
import { existsSync as existsSync7, statSync as statSync3 } from "fs";
|
|
1640
|
+
import { homedir as homedir4 } from "os";
|
|
1641
|
+
import { join as join9, relative as relative2, resolve as resolve4 } from "path";
|
|
1642
|
+
import process16 from "process";
|
|
1643
|
+
|
|
1644
|
+
// src/repos-scan.ts
|
|
1645
|
+
import { existsSync as existsSync6 } from "fs";
|
|
1646
|
+
import { readdir as readdir2 } from "fs/promises";
|
|
1647
|
+
import { basename, join as join8 } from "path";
|
|
1648
|
+
import process15 from "process";
|
|
1649
|
+
var MAX_DEPTH2 = 3;
|
|
1650
|
+
var CONCURRENCY2 = 8;
|
|
1651
|
+
var GIT_MS = 1e4;
|
|
1652
|
+
var FETCH_MS = 30000;
|
|
1653
|
+
var AHEAD_BEHIND = /\+(\d+) -(\d+)/;
|
|
1654
|
+
var PACKAGE_TAG = /^(.+)-v\d/;
|
|
1655
|
+
var REASONS = ["dirty", "unpushed", "unreleased", "behind"];
|
|
1656
|
+
function reasonsOf(repo) {
|
|
1657
|
+
const reasons = [];
|
|
1658
|
+
if (repo.dirty > 0)
|
|
1659
|
+
reasons.push("dirty");
|
|
1660
|
+
if ((repo.ahead ?? 0) > 0)
|
|
1661
|
+
reasons.push("unpushed");
|
|
1662
|
+
if ((repo.behind ?? 0) > 0)
|
|
1663
|
+
reasons.push("behind");
|
|
1664
|
+
if ((repo.unreleased ?? 0) > 0)
|
|
1665
|
+
reasons.push("unreleased");
|
|
1666
|
+
return reasons;
|
|
1667
|
+
}
|
|
1668
|
+
async function findRepos2(root, skip) {
|
|
1669
|
+
if (existsSync6(join8(root, ".git")))
|
|
1670
|
+
return [root];
|
|
1671
|
+
const found = [];
|
|
1672
|
+
async function walk(dir, depth) {
|
|
1673
|
+
let entries;
|
|
1674
|
+
try {
|
|
1675
|
+
entries = await readdir2(dir, { withFileTypes: true });
|
|
1676
|
+
} catch {
|
|
1677
|
+
return;
|
|
1678
|
+
}
|
|
1679
|
+
for (const entry of entries.toSorted((a, b) => a.name.localeCompare(b.name))) {
|
|
1680
|
+
if (!entry.isDirectory() || entry.name === "node_modules" || entry.name.startsWith(".") || skip.has(entry.name))
|
|
1681
|
+
continue;
|
|
1682
|
+
const path = join8(dir, entry.name);
|
|
1683
|
+
if (existsSync6(join8(path, ".git")))
|
|
1684
|
+
found.push(path);
|
|
1685
|
+
else if (depth < MAX_DEPTH2)
|
|
1686
|
+
await walk(path, depth + 1);
|
|
1687
|
+
}
|
|
1688
|
+
}
|
|
1689
|
+
await walk(root, 1);
|
|
1690
|
+
return found;
|
|
1691
|
+
}
|
|
1692
|
+
async function read(path, args, timeoutMs = GIT_MS) {
|
|
1693
|
+
try {
|
|
1694
|
+
const ran = await runCommand(["git", "--no-optional-locks", ...args], { cwd: path, env: { ...process15.env, GIT_TERMINAL_PROMPT: "0" }, timeoutMs });
|
|
1695
|
+
return ran.code === 0 && !ran.timedOut ? ran.text.trim() : undefined;
|
|
1696
|
+
} catch {
|
|
1697
|
+
return;
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1700
|
+
async function commitsSince(path, tag, folder) {
|
|
1701
|
+
const count = await read(path, ["rev-list", "--count", `${tag}..HEAD`, ...folder === undefined ? [] : ["--", folder]]);
|
|
1702
|
+
return Number(count ?? 0);
|
|
1703
|
+
}
|
|
1704
|
+
async function packageReleases(path) {
|
|
1705
|
+
const names = new Set;
|
|
1706
|
+
for (const tag of (await read(path, ["tag", "--list"]) ?? "").split(`
|
|
1707
|
+
`)) {
|
|
1708
|
+
const name = PACKAGE_TAG.exec(tag)?.[1];
|
|
1709
|
+
if (name !== undefined)
|
|
1710
|
+
names.add(name);
|
|
1711
|
+
}
|
|
1712
|
+
const releases = [];
|
|
1713
|
+
for (const name of [...names].toSorted()) {
|
|
1714
|
+
const folder = [join8("packages", name), name].find((candidate) => existsSync6(join8(path, candidate)));
|
|
1715
|
+
const tag = folder === undefined ? undefined : await read(path, ["describe", "--tags", "--abbrev=0", "--match", `${name}-v*`]);
|
|
1716
|
+
if (folder !== undefined && tag !== undefined && tag !== "")
|
|
1717
|
+
releases.push({ name, tag, unreleased: await commitsSince(path, tag, folder) });
|
|
1718
|
+
}
|
|
1719
|
+
return releases;
|
|
1720
|
+
}
|
|
1721
|
+
async function inspect(path, fetch) {
|
|
1722
|
+
const fetched = fetch ? await read(path, ["fetch", "--quiet"], FETCH_MS) : "";
|
|
1723
|
+
const [status, worktrees, packages] = await Promise.all([
|
|
1724
|
+
read(path, ["status", "--porcelain=v2", "--branch"]),
|
|
1725
|
+
read(path, ["worktree", "list", "--porcelain"]),
|
|
1726
|
+
packageReleases(path)
|
|
1727
|
+
]);
|
|
1728
|
+
const state = { name: basename(path), path, branch: "?", upstream: null, dirty: 0, ahead: null, behind: null, worktrees: 0, tag: null, unreleased: null, packages, fetchFailed: fetched === undefined, unreadable: status === undefined };
|
|
1729
|
+
for (const line of (status ?? "").split(`
|
|
1730
|
+
`)) {
|
|
1731
|
+
if (line.startsWith("# branch.head "))
|
|
1732
|
+
state.branch = line.slice("# branch.head ".length);
|
|
1733
|
+
else if (line.startsWith("# branch.upstream "))
|
|
1734
|
+
state.upstream = line.slice("# branch.upstream ".length);
|
|
1735
|
+
else if (line.startsWith("# branch.ab "))
|
|
1736
|
+
[state.ahead, state.behind] = AHEAD_BEHIND.exec(line)?.slice(1).map(Number);
|
|
1737
|
+
else if (line !== "" && !line.startsWith("#"))
|
|
1738
|
+
state.dirty++;
|
|
1739
|
+
}
|
|
1740
|
+
state.worktrees = Math.max(0, (worktrees ?? "").split(`
|
|
1741
|
+
`).filter((line) => line.startsWith("worktree ")).length - 1);
|
|
1742
|
+
if (packages.length > 0) {
|
|
1743
|
+
state.unreleased = packages.reduce((sum, release) => sum + release.unreleased, 0);
|
|
1744
|
+
} else {
|
|
1745
|
+
const tag = await read(path, ["describe", "--tags", "--abbrev=0"]);
|
|
1746
|
+
if (tag !== undefined && tag !== "") {
|
|
1747
|
+
state.tag = tag;
|
|
1748
|
+
state.unreleased = await commitsSince(path, tag);
|
|
1749
|
+
}
|
|
1750
|
+
}
|
|
1751
|
+
return state;
|
|
1752
|
+
}
|
|
1753
|
+
function pullBlocker(repo) {
|
|
1754
|
+
if (repo.unreadable)
|
|
1755
|
+
return "unreadable";
|
|
1756
|
+
if (repo.fetchFailed)
|
|
1757
|
+
return "fetch failed";
|
|
1758
|
+
if (repo.branch === "(detached)")
|
|
1759
|
+
return "detached HEAD";
|
|
1760
|
+
if (repo.upstream === null)
|
|
1761
|
+
return "no upstream";
|
|
1762
|
+
if (repo.dirty > 0)
|
|
1763
|
+
return `${repo.dirty} dirty ${repo.dirty === 1 ? "file" : "files"}`;
|
|
1764
|
+
return (repo.ahead ?? 0) > 0 ? `diverged, ${repo.ahead} ahead` : undefined;
|
|
1765
|
+
}
|
|
1766
|
+
function firstLine(text) {
|
|
1767
|
+
const end = text.indexOf(`
|
|
1768
|
+
`);
|
|
1769
|
+
return end === -1 ? text : text.slice(0, end);
|
|
1770
|
+
}
|
|
1771
|
+
async function fastForward(path) {
|
|
1772
|
+
try {
|
|
1773
|
+
const ran = await runCommand(["git", "--no-optional-locks", "merge", "--ff-only", "--quiet", "@{upstream}"], { cwd: path, env: { ...process15.env, GIT_TERMINAL_PROMPT: "0" }, timeoutMs: GIT_MS });
|
|
1774
|
+
if (ran.timedOut)
|
|
1775
|
+
return "timed out";
|
|
1776
|
+
return ran.code === 0 ? undefined : firstLine(ran.text.trim()) || `exit ${ran.code}`;
|
|
1777
|
+
} catch (error) {
|
|
1778
|
+
return error instanceof Error ? error.message : String(error);
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
async function pool2(items, limit, task) {
|
|
1782
|
+
const results = new Array(items.length);
|
|
1783
|
+
let next = 0;
|
|
1784
|
+
async function worker() {
|
|
1785
|
+
while (next < items.length) {
|
|
1786
|
+
const index = next++;
|
|
1787
|
+
results[index] = await task(items[index]);
|
|
1788
|
+
}
|
|
1789
|
+
}
|
|
1790
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
|
|
1791
|
+
return results;
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
// src/repos.ts
|
|
1795
|
+
var USAGE2 = `repos [options] [root]: the git repositories under root (~/Workspace by default) that need attention, with their branch, dirty files, ahead/behind their upstream, linked worktrees and commits since their latest tag
|
|
1796
|
+
|
|
1797
|
+
A repository with nothing to report is left out unless --all. A monorepo tagging its packages <folder>-v<version> counts the unreleased commits per package folder. The walk goes ${MAX_DEPTH2} levels deep, stops at a .git and skips node_modules, hidden folders and the names in "skip" of ~/.config/kaynooo-cli/repos.json.
|
|
1798
|
+
|
|
1799
|
+
Ahead and behind are those of the last fetch unless --fetch refreshes the upstreams first, which reaches the remote.
|
|
1800
|
+
--pull fetches, then fast-forwards the repositories listed that are clean, on a branch and only behind their upstream, and touches nothing else: a dirty, ahead, diverged or detached one is reported as skipped. A repository pulled is listed again as it now stands.
|
|
1801
|
+
--path prints only the paths of the repositories listed, one per line, for xargs -d '\\n'.`;
|
|
1802
|
+
var EXITS2 = [[0, "it listed the repositories, including when none needs attention"], [1, "a fast-forward --pull attempted failed"], [2, "a usage error, an unreadable config or a root that is no folder"]];
|
|
1803
|
+
var OPTIONS2 = {
|
|
1804
|
+
all: { type: "boolean", short: "a", description: "list the repositories with nothing to report too" },
|
|
1805
|
+
only: { type: "string", choices: REASONS, description: "only the repositories with this reason: dirty, unpushed, unreleased or behind" },
|
|
1806
|
+
fetch: { type: "boolean", description: "refresh the upstreams first (reaches the remote)" },
|
|
1807
|
+
pull: { type: "boolean", description: "fetch, then fast-forward the clean repositories listed that are only behind their upstream (reaches the remote)" },
|
|
1808
|
+
path: { type: "boolean", conflicts: ["json"], description: "print only the paths of the repositories listed, one per line" },
|
|
1809
|
+
json: jsonOption("[{name, path, branch, upstream, dirty, ahead, behind, worktrees, tag, unreleased, packages, reasons, fetchFailed, unreadable}]")
|
|
1810
|
+
};
|
|
1811
|
+
async function skipped() {
|
|
1812
|
+
const file = userConfigReadPath("repos");
|
|
1813
|
+
if (!existsSync7(file))
|
|
1814
|
+
return new Set;
|
|
1815
|
+
try {
|
|
1816
|
+
const config = await Bun.file(file).json();
|
|
1817
|
+
return new Set(Array.isArray(config.skip) ? config.skip.filter((name) => typeof name === "string") : []);
|
|
1818
|
+
} catch (error) {
|
|
1819
|
+
fail(`${file} does not parse: ${error instanceof Error ? error.message : String(error)}`);
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
function releasedCell(repo) {
|
|
1823
|
+
if (repo.packages.length > 0)
|
|
1824
|
+
return repo.packages.filter((release) => release.unreleased > 0).map((release) => `${release.name} ${release.unreleased}`).join(", ") || "-";
|
|
1825
|
+
return repo.tag === null || repo.unreleased === 0 ? "-" : `${repo.unreleased} since ${repo.tag}`;
|
|
1826
|
+
}
|
|
1827
|
+
function syncCell(repo, stream = process16.stdout) {
|
|
1828
|
+
const colors = paint(stream);
|
|
1829
|
+
const ahead = repo.ahead ?? 0;
|
|
1830
|
+
const behind = repo.behind ?? 0;
|
|
1831
|
+
if (repo.upstream === null)
|
|
1832
|
+
return colors.dim("no upstream");
|
|
1833
|
+
const parts = [ahead > 0 ? colors.green(`+${ahead}`) : "", behind > 0 ? colors.red(`-${behind}`) : ""].filter(Boolean);
|
|
1834
|
+
const diverged = takesControlCodes(stream) && ahead > 0 && behind > 0;
|
|
1835
|
+
return parts.length === 0 ? colors.dim("-") : `${parts.join(" ")}${diverged ? colors.yellow(" diverged") : ""}`;
|
|
1836
|
+
}
|
|
1837
|
+
function branchCell(repo) {
|
|
1838
|
+
const colors = paint(process16.stdout);
|
|
1839
|
+
if (repo.unreadable)
|
|
1840
|
+
return colors.red("? (unreadable)");
|
|
1841
|
+
return repo.branch === "(detached)" ? colors.yellow(repo.branch) : repo.branch;
|
|
1842
|
+
}
|
|
1843
|
+
function row(repo, shown) {
|
|
1844
|
+
const colors = paint(process16.stdout);
|
|
1845
|
+
const tint = role("path", process16.stdout);
|
|
1846
|
+
const unreleased = releasedCell(repo);
|
|
1847
|
+
return [
|
|
1848
|
+
tint(shown),
|
|
1849
|
+
branchCell(repo),
|
|
1850
|
+
repo.dirty === 0 ? colors.dim("-") : colors.yellow(String(repo.dirty)),
|
|
1851
|
+
syncCell(repo),
|
|
1852
|
+
repo.worktrees === 0 ? colors.dim("-") : String(repo.worktrees),
|
|
1853
|
+
unreleased === "-" ? colors.dim("-") : colors.cyan(unreleased)
|
|
1854
|
+
];
|
|
1855
|
+
}
|
|
1856
|
+
async function repos(args) {
|
|
1857
|
+
const { values, positionals } = parse(args, USAGE2, OPTIONS2, { positionals: ["[root]"], exits: EXITS2, replaces: "a cd, git status, git log and git tag loop over each repository of the workspace", examples: ["repos", "repos ~/Workspace --only unreleased --fetch", ["repos --only behind --pull", "fast-forward what is behind and clean"], ["repos --only dirty --path", "the paths, for xargs"]] });
|
|
1858
|
+
const root = resolve4(positionals[0] ?? join9(homedir4(), "Workspace"));
|
|
1859
|
+
if (!existsSync7(root) || !statSync3(root).isDirectory())
|
|
1860
|
+
fail(`no folder ${root}`);
|
|
1861
|
+
const only = values.only === undefined ? undefined : oneOf(values.only, "only", REASONS);
|
|
1862
|
+
const paths = await findRepos2(root, await skipped());
|
|
1863
|
+
const states = await pool2(paths, CONCURRENCY2, async (path) => inspect(path, values.fetch === true || values.pull === true));
|
|
1864
|
+
const keeps = (repo) => {
|
|
1865
|
+
const reasons = reasonsOf(repo);
|
|
1866
|
+
return only === undefined ? values.all === true || reasons.length > 0 || repo.unreadable : reasons.includes(only);
|
|
1867
|
+
};
|
|
1868
|
+
let failedPulls = 0;
|
|
1869
|
+
if (values.pull === true) {
|
|
1870
|
+
const log = values.json === true || values.path === true ? console.error : console.log;
|
|
1871
|
+
for (const [index, repo] of states.entries()) {
|
|
1872
|
+
if (!keeps(repo) || (repo.behind ?? 0) === 0)
|
|
1873
|
+
continue;
|
|
1874
|
+
const blocker = pullBlocker(repo);
|
|
1875
|
+
const refused = blocker === undefined ? await fastForward(repo.path) : undefined;
|
|
1876
|
+
if (blocker !== undefined) {
|
|
1877
|
+
log(marked("note", `${repo.name} skipped`, process16.stdout, blocker));
|
|
1878
|
+
} else if (refused !== undefined) {
|
|
1879
|
+
failedPulls++;
|
|
1880
|
+
log(marked("problem", `${repo.name} not pulled`, process16.stdout, refused));
|
|
1881
|
+
} else {
|
|
1882
|
+
log(marked("ok", `${repo.name} pulled`, process16.stdout, `${repo.behind} ${repo.behind === 1 ? "commit" : "commits"}`));
|
|
1883
|
+
states[index] = await inspect(repo.path, false);
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1887
|
+
const kept = states.filter(keeps);
|
|
1888
|
+
if (values.path === true) {
|
|
1889
|
+
for (const repo of kept)
|
|
1890
|
+
console.log(repo.path);
|
|
1891
|
+
return failedPulls > 0 ? 1 : 0;
|
|
1892
|
+
}
|
|
1893
|
+
if (values.json === true) {
|
|
1894
|
+
printJson(kept.map((repo) => ({ ...repo, reasons: reasonsOf(repo) })));
|
|
1895
|
+
return failedPulls > 0 ? 1 : 0;
|
|
1896
|
+
}
|
|
1897
|
+
const label = role("label", process16.stdout);
|
|
1898
|
+
if (kept.length === 0) {
|
|
1899
|
+
console.log(label(`nothing needs attention in ${states.length} repositories`));
|
|
1900
|
+
return failedPulls > 0 ? 1 : 0;
|
|
1901
|
+
}
|
|
1902
|
+
console.log(table([["repo", "branch", "dirty", "ahead/behind", "worktrees", "unreleased"], ...kept.map((repo) => row(repo, relative2(root, repo.path) || repo.name))], { header: true, align: ["left", "left", "right", "left", "right", "left"] }));
|
|
1903
|
+
const failed = kept.filter((repo) => repo.fetchFailed).map((repo) => repo.name);
|
|
1904
|
+
if (failed.length > 0)
|
|
1905
|
+
console.log(label(`fetch failed for ${failed.join(", ")}`));
|
|
1906
|
+
if (values.fetch !== true)
|
|
1907
|
+
console.log(label("ahead/behind as of the last fetch, --fetch refreshes them"));
|
|
1908
|
+
return failedPulls > 0 ? 1 : 0;
|
|
1909
|
+
}
|
|
1910
|
+
|
|
1911
|
+
// src/sweep.ts
|
|
1912
|
+
import { existsSync as existsSync10, statSync as statSync4 } from "fs";
|
|
1913
|
+
import { homedir as homedir6 } from "os";
|
|
1914
|
+
import { join as join13, relative as relative3, resolve as resolve5 } from "path";
|
|
1915
|
+
|
|
1916
|
+
// ../kit/src/prompt.ts
|
|
1917
|
+
import process17 from "process";
|
|
1918
|
+
var KEY_SEQUENCES = /\x1B\[[0-9;]*[A-Z~]|\x1BO[A-Z]|[\s\S]/giu;
|
|
1919
|
+
var CONTROL2 = /^[\x00-\x1F\x7F]/;
|
|
1920
|
+
var KEYS = { "\x1B[A": "up", "\x1BOA": "up", "\x1B[B": "down", "\x1BOB": "down", "\r": "enter", "\n": "enter", "\x03": "cancel", "\x1B": "cancel", "\x7F": "erase", "\b": "erase" };
|
|
1921
|
+
function readKeys(input) {
|
|
1922
|
+
return (input.match(KEY_SEQUENCES) ?? []).flatMap((sequence) => KEYS[sequence] ?? (CONTROL2.test(sequence) ? [] : [{ text: sequence }]));
|
|
1923
|
+
}
|
|
1924
|
+
async function confirm(question, { default: fallback, ...settings } = {}) {
|
|
1925
|
+
let answered = fallback;
|
|
1926
|
+
const choices = { true: "Y/n", false: "y/N", undefined: "y/n" }[String(fallback)];
|
|
1927
|
+
const accepted = await ask(question, settings, (colors, state) => [state === "asking" ? asking(colors, question, "", choices) : answer(colors, question, state, answered ? "yes" : "no")], (key) => {
|
|
1928
|
+
if (key === "cancel")
|
|
1929
|
+
return "cancel";
|
|
1930
|
+
if (key === "enter")
|
|
1931
|
+
return answered === undefined ? undefined : "enter";
|
|
1932
|
+
const letter = typeof key === "object" ? key.text.toLowerCase() : "";
|
|
1933
|
+
if (letter !== "y" && letter !== "n")
|
|
1934
|
+
return;
|
|
1935
|
+
answered = letter === "y";
|
|
1936
|
+
return "enter";
|
|
1937
|
+
});
|
|
1938
|
+
return accepted ? answered : undefined;
|
|
1939
|
+
}
|
|
1940
|
+
function asking(colors, question, typed, keys) {
|
|
1941
|
+
return `${colors.cyan("?")} ${question} ${typed === "" ? colors.dim(`(${keys})`) : typed}`;
|
|
1942
|
+
}
|
|
1943
|
+
function answer(colors, question, state, picked) {
|
|
1944
|
+
return state === "enter" ? `${colors.green("\u2713")} ${question} ${colors.cyan(picked)}` : `${colors.red("\u2717")} ${question} ${colors.dim("cancelled")}`;
|
|
1945
|
+
}
|
|
1946
|
+
async function ask(question, { hint, terminal = process17 }, render, press) {
|
|
1947
|
+
const { stdin, stderr } = terminal;
|
|
1948
|
+
if (stdin.isTTY !== true || stderr.isTTY !== true)
|
|
1949
|
+
fail(`cannot ask "${question}" off a terminal`, 2, hint);
|
|
1950
|
+
const colors = paint(stderr);
|
|
1951
|
+
let drawn = 0;
|
|
1952
|
+
let outcome;
|
|
1953
|
+
const draw = (state) => {
|
|
1954
|
+
const frame = render(colors, state, { columns: stderr.columns || 80, rows: stderr.rows || 24 });
|
|
1955
|
+
stderr.write(`${drawn > 1 ? `\x1B[${drawn - 1}A` : ""}\r\x1B[0J${frame.join(`
|
|
1956
|
+
`)}`);
|
|
1957
|
+
drawn = frame.length;
|
|
1958
|
+
};
|
|
1959
|
+
try {
|
|
1960
|
+
stdin.setRawMode(true);
|
|
1961
|
+
stdin.resume();
|
|
1962
|
+
stderr.write("\x1B[?25l");
|
|
1963
|
+
draw("asking");
|
|
1964
|
+
return await new Promise((resolve) => {
|
|
1965
|
+
function onData(data) {
|
|
1966
|
+
for (const key of readKeys(data.toString())) {
|
|
1967
|
+
const pressed = press(key);
|
|
1968
|
+
if (pressed !== undefined) {
|
|
1969
|
+
settle(pressed);
|
|
1970
|
+
return;
|
|
1971
|
+
}
|
|
1972
|
+
}
|
|
1973
|
+
draw("asking");
|
|
1974
|
+
}
|
|
1975
|
+
function onEnd() {
|
|
1976
|
+
settle("cancel");
|
|
1977
|
+
}
|
|
1978
|
+
function settle(settled) {
|
|
1979
|
+
outcome = settled;
|
|
1980
|
+
stdin.off("data", onData);
|
|
1981
|
+
stdin.off("end", onEnd);
|
|
1982
|
+
resolve(settled === "enter");
|
|
1983
|
+
}
|
|
1984
|
+
stdin.on("data", onData);
|
|
1985
|
+
stdin.on("end", onEnd);
|
|
1986
|
+
});
|
|
1987
|
+
} finally {
|
|
1988
|
+
draw(outcome ?? "cancel");
|
|
1989
|
+
stderr.write(`
|
|
1990
|
+
\x1B[?25h`);
|
|
1991
|
+
stdin.setRawMode(false);
|
|
1992
|
+
stdin.pause();
|
|
1993
|
+
}
|
|
1994
|
+
}
|
|
1995
|
+
|
|
1996
|
+
// src/apply.ts
|
|
1997
|
+
import { existsSync as existsSync9 } from "fs";
|
|
1998
|
+
import { lstat as lstat3, rm } from "fs/promises";
|
|
1999
|
+
import { basename as basename3, join as join12 } from "path";
|
|
2000
|
+
|
|
2001
|
+
// src/sweep-scan.ts
|
|
2002
|
+
import { existsSync as existsSync8 } from "fs";
|
|
2003
|
+
import { lstat as lstat2, readdir as readdir4, realpath } from "fs/promises";
|
|
2004
|
+
import { homedir as homedir5 } from "os";
|
|
2005
|
+
import { basename as basename2, dirname as dirname4, join as join11 } from "path";
|
|
2006
|
+
|
|
2007
|
+
// src/size.ts
|
|
2008
|
+
import { lstat, readdir as readdir3 } from "fs/promises";
|
|
2009
|
+
import { join as join10 } from "path";
|
|
2010
|
+
async function pooled(items, limit, work) {
|
|
2011
|
+
const results = Array.from({ length: items.length });
|
|
2012
|
+
let next = 0;
|
|
2013
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, async () => {
|
|
2014
|
+
while (next < items.length) {
|
|
2015
|
+
const index = next++;
|
|
2016
|
+
results[index] = await work(items[index]);
|
|
2017
|
+
}
|
|
2018
|
+
}));
|
|
2019
|
+
return results;
|
|
2020
|
+
}
|
|
2021
|
+
var FS_LIMIT = 32;
|
|
2022
|
+
var waiting = [];
|
|
2023
|
+
var running = 0;
|
|
2024
|
+
async function gated(call) {
|
|
2025
|
+
if (running >= FS_LIMIT)
|
|
2026
|
+
await new Promise((resolve) => waiting.push(resolve));
|
|
2027
|
+
running++;
|
|
2028
|
+
try {
|
|
2029
|
+
return await call();
|
|
2030
|
+
} finally {
|
|
2031
|
+
running--;
|
|
2032
|
+
waiting.shift()?.();
|
|
2033
|
+
}
|
|
2034
|
+
}
|
|
2035
|
+
async function sizeOf(folder) {
|
|
2036
|
+
const seen = new Set;
|
|
2037
|
+
let total = 0;
|
|
2038
|
+
const visit = async (dir) => {
|
|
2039
|
+
let entries;
|
|
2040
|
+
try {
|
|
2041
|
+
entries = await gated(async () => readdir3(dir, { withFileTypes: true }));
|
|
2042
|
+
} catch {
|
|
2043
|
+
return;
|
|
2044
|
+
}
|
|
2045
|
+
await Promise.all(entries.map(async (entry) => {
|
|
2046
|
+
const path = join10(dir, entry.name);
|
|
2047
|
+
if (entry.isDirectory())
|
|
2048
|
+
return visit(path);
|
|
2049
|
+
try {
|
|
2050
|
+
const stat = await gated(async () => lstat(path));
|
|
2051
|
+
if (stat.nlink > 1) {
|
|
2052
|
+
const key = `${stat.dev}:${stat.ino}`;
|
|
2053
|
+
if (seen.has(key))
|
|
2054
|
+
return;
|
|
2055
|
+
seen.add(key);
|
|
2056
|
+
}
|
|
2057
|
+
total += stat.size;
|
|
2058
|
+
} catch {}
|
|
2059
|
+
}));
|
|
2060
|
+
};
|
|
2061
|
+
await visit(folder);
|
|
2062
|
+
return total;
|
|
2063
|
+
}
|
|
2064
|
+
|
|
2065
|
+
// src/sweep-scan.ts
|
|
2066
|
+
var FOLDER_NAMES = new Set(["node_modules", ".nuxt", ".next", ".output", "dist", "build", ".turbo", ".svelte-kit", "target"]);
|
|
2067
|
+
var MAX_DEPTH3 = 3;
|
|
2068
|
+
var PROJECT_CACHE_NAMES = new Set([".turbo", ".nuxt", ".next", ".svelte-kit"]);
|
|
2069
|
+
var GLOBAL_CACHES = [".bun/install/cache", ".npm/_cacache", ".cache/pip", ".cache/uv", ".cache/ms-playwright", ".cache/puppeteer", ".cache/go-build", ".cache/typescript", ".cargo/registry"];
|
|
2070
|
+
function isCachePath(path, home = homedir5()) {
|
|
2071
|
+
return GLOBAL_CACHES.some((cache) => join11(home, cache) === path) || basename2(path) === ".cache" && basename2(dirname4(path)) === "node_modules";
|
|
2072
|
+
}
|
|
2073
|
+
async function listing(dir) {
|
|
2074
|
+
try {
|
|
2075
|
+
return await readdir4(dir, { withFileTypes: true });
|
|
2076
|
+
} catch {
|
|
2077
|
+
return [];
|
|
2078
|
+
}
|
|
2079
|
+
}
|
|
2080
|
+
async function addGlobalCaches(found, home) {
|
|
2081
|
+
for (const cache of GLOBAL_CACHES) {
|
|
2082
|
+
const path = join11(home, cache);
|
|
2083
|
+
try {
|
|
2084
|
+
const stat = await lstat2(path);
|
|
2085
|
+
if (stat.isSymbolicLink())
|
|
2086
|
+
found.skipped.push({ path, reason: "symlink" });
|
|
2087
|
+
else if (stat.isDirectory())
|
|
2088
|
+
found.candidates.push({ path, kind: "cache", global: true });
|
|
2089
|
+
} catch {}
|
|
2090
|
+
}
|
|
2091
|
+
}
|
|
2092
|
+
async function findCandidates(root, home = homedir5(), withGlobal = true) {
|
|
2093
|
+
const found = { candidates: [], skipped: [] };
|
|
2094
|
+
const visit = async (dir, depth) => {
|
|
2095
|
+
const entries = await listing(dir);
|
|
2096
|
+
for (const entry of entries) {
|
|
2097
|
+
const path = join11(dir, entry.name);
|
|
2098
|
+
if (entry.name === ".git")
|
|
2099
|
+
continue;
|
|
2100
|
+
if (entry.name === ".worktrees" || entry.name === "worktrees" && basename2(dir) === ".claude") {
|
|
2101
|
+
if (!entry.isDirectory()) {
|
|
2102
|
+
found.skipped.push({ path, reason: "symlink" });
|
|
2103
|
+
continue;
|
|
2104
|
+
}
|
|
2105
|
+
for (const worktree of await listing(path)) {
|
|
2106
|
+
if (worktree.isDirectory())
|
|
2107
|
+
found.candidates.push({ path: join11(path, worktree.name), kind: "worktree" });
|
|
2108
|
+
else if (worktree.isSymbolicLink())
|
|
2109
|
+
found.skipped.push({ path: join11(path, worktree.name), reason: "symlink" });
|
|
2110
|
+
}
|
|
2111
|
+
continue;
|
|
2112
|
+
}
|
|
2113
|
+
if (FOLDER_NAMES.has(entry.name) && (entry.name !== "target" || existsSync8(join11(dir, "Cargo.toml")))) {
|
|
2114
|
+
if (entry.isSymbolicLink()) {
|
|
2115
|
+
found.skipped.push({ path, reason: "symlink" });
|
|
2116
|
+
} else if (entry.isDirectory()) {
|
|
2117
|
+
found.candidates.push({ path, kind: PROJECT_CACHE_NAMES.has(entry.name) ? "cache" : "folder" });
|
|
2118
|
+
if (entry.name === "node_modules") {
|
|
2119
|
+
const cache = (await listing(path)).find((inner) => inner.name === ".cache");
|
|
2120
|
+
if (cache?.isSymbolicLink())
|
|
2121
|
+
found.skipped.push({ path: join11(path, ".cache"), reason: "symlink" });
|
|
2122
|
+
else if (cache?.isDirectory())
|
|
2123
|
+
found.candidates.push({ path: join11(path, ".cache"), kind: "cache", inside: path });
|
|
2124
|
+
}
|
|
2125
|
+
}
|
|
2126
|
+
continue;
|
|
2127
|
+
}
|
|
2128
|
+
if (entry.isDirectory() && depth < MAX_DEPTH3)
|
|
2129
|
+
await visit(path, depth + 1);
|
|
2130
|
+
}
|
|
2131
|
+
};
|
|
2132
|
+
await visit(root, 0);
|
|
2133
|
+
if (withGlobal)
|
|
2134
|
+
await addGlobalCaches(found, home);
|
|
2135
|
+
return found;
|
|
2136
|
+
}
|
|
2137
|
+
async function realOf(path) {
|
|
2138
|
+
try {
|
|
2139
|
+
return await realpath(path);
|
|
2140
|
+
} catch {
|
|
2141
|
+
return;
|
|
2142
|
+
}
|
|
2143
|
+
}
|
|
2144
|
+
var HEADS_PREFIX = /^refs\/heads\//;
|
|
2145
|
+
var TRAILING_SLASH = /\/$/;
|
|
2146
|
+
async function gitIn(cwd, args) {
|
|
2147
|
+
try {
|
|
2148
|
+
const { code, stdout, stderr } = await runTool(`git ${args.join(" ")}`, ["git", "-C", cwd, ...args], { timeoutMs: 30000 });
|
|
2149
|
+
return { code, stdout: stdout.trim(), stderr };
|
|
2150
|
+
} catch (error) {
|
|
2151
|
+
return { code: -1, stdout: "", stderr: String(error) };
|
|
2152
|
+
}
|
|
2153
|
+
}
|
|
2154
|
+
function memo(make) {
|
|
2155
|
+
const cache = new Map;
|
|
2156
|
+
return async (key) => {
|
|
2157
|
+
let value = cache.get(key);
|
|
2158
|
+
if (value === undefined) {
|
|
2159
|
+
value = make(key);
|
|
2160
|
+
cache.set(key, value);
|
|
2161
|
+
}
|
|
2162
|
+
return value;
|
|
2163
|
+
};
|
|
2164
|
+
}
|
|
2165
|
+
var NOT_A_REPO = /not a git repository/i;
|
|
2166
|
+
var toplevelOf = memo(async (dir) => {
|
|
2167
|
+
const { code, stdout, stderr } = await gitIn(dir, ["rev-parse", "--show-toplevel"]);
|
|
2168
|
+
if (code === 0 && stdout !== "")
|
|
2169
|
+
return { top: stdout };
|
|
2170
|
+
return NOT_A_REPO.test(stderr) ? "none" : "failed";
|
|
2171
|
+
});
|
|
2172
|
+
var lastCommitOf = memo(async (top) => {
|
|
2173
|
+
const { code, stdout } = await gitIn(top, ["log", "-1", "--format=%ct"]);
|
|
2174
|
+
return code === 0 && stdout !== "" ? Number(stdout) * 1000 : undefined;
|
|
2175
|
+
});
|
|
2176
|
+
var worktreesOf = memo(async (repo) => {
|
|
2177
|
+
const { code, stdout } = await gitIn(repo, ["worktree", "list", "--porcelain"]);
|
|
2178
|
+
const branches = new Map;
|
|
2179
|
+
if (code !== 0)
|
|
2180
|
+
return branches;
|
|
2181
|
+
for (const block of stdout.split(`
|
|
2182
|
+
|
|
2183
|
+
`)) {
|
|
2184
|
+
const lines = block.split(`
|
|
2185
|
+
`);
|
|
2186
|
+
const path = lines.find((line) => line.startsWith("worktree "))?.slice("worktree ".length);
|
|
2187
|
+
const branch = lines.find((line) => line.startsWith("branch "))?.slice("branch ".length);
|
|
2188
|
+
if (path !== undefined)
|
|
2189
|
+
branches.set(await realOf(path) ?? path, branch);
|
|
2190
|
+
}
|
|
2191
|
+
return branches;
|
|
2192
|
+
});
|
|
2193
|
+
async function judgeFolder(path, kind, inside) {
|
|
2194
|
+
const parent = dirname4(path);
|
|
2195
|
+
const name = basename2(path);
|
|
2196
|
+
const located = await toplevelOf(parent);
|
|
2197
|
+
if (located === "failed")
|
|
2198
|
+
return { skip: "git failed" };
|
|
2199
|
+
if (existsSync8(join11(path, ".git")))
|
|
2200
|
+
return { skip: "holds a git repository" };
|
|
2201
|
+
if (located === "none")
|
|
2202
|
+
return name === "node_modules" || inside !== undefined ? { entry: { path, kind, committedAt: undefined, action: "remove", inside } } : { skip: "not in a git repository" };
|
|
2203
|
+
const tracked = await gitIn(parent, ["ls-files", "--", name]);
|
|
2204
|
+
if (tracked.code !== 0 || tracked.stdout !== "")
|
|
2205
|
+
return { skip: "tracked by git" };
|
|
2206
|
+
if (name !== "node_modules" && inside === undefined && (await gitIn(parent, ["check-ignore", "-q", "--", name])).code !== 0)
|
|
2207
|
+
return { skip: "not ignored by git" };
|
|
2208
|
+
return { entry: { path, kind, committedAt: await lastCommitOf(located.top), action: "remove", inside } };
|
|
2209
|
+
}
|
|
2210
|
+
async function judgeWorktree(path) {
|
|
2211
|
+
const located = await toplevelOf(dirname4(path));
|
|
2212
|
+
if (located === "failed")
|
|
2213
|
+
return { skip: "git failed" };
|
|
2214
|
+
if (located === "none")
|
|
2215
|
+
return { skip: "not in a git repository" };
|
|
2216
|
+
const { top } = located;
|
|
2217
|
+
const real = await realOf(path);
|
|
2218
|
+
const branches = await worktreesOf(top);
|
|
2219
|
+
if (real === undefined || !branches.has(real))
|
|
2220
|
+
return { skip: "not a worktree of this repo" };
|
|
2221
|
+
const committedAt = await lastCommitOf(top);
|
|
2222
|
+
const keep = (reason) => ({ entry: { path, kind: "worktree", committedAt, action: "keep", reason, repo: top } });
|
|
2223
|
+
const branch = branches.get(real);
|
|
2224
|
+
if (branch === undefined)
|
|
2225
|
+
return keep("detached HEAD");
|
|
2226
|
+
const status = await gitIn(path, ["status", "--porcelain", "-z", "--ignored=matching"]);
|
|
2227
|
+
if (status.code !== 0)
|
|
2228
|
+
return keep("status failed");
|
|
2229
|
+
const changes = status.stdout.split("\x00").filter((line) => line !== "");
|
|
2230
|
+
if (changes.some((line) => !line.startsWith("!! ")))
|
|
2231
|
+
return keep("uncommitted changes");
|
|
2232
|
+
if (changes.some((line) => !line.endsWith("/") || !FOLDER_NAMES.has(basename2(line.slice(3).replace(TRAILING_SLASH, "")))))
|
|
2233
|
+
return keep("ignored files (.env, local data) that removing would delete");
|
|
2234
|
+
const unique = await gitIn(path, ["log", "--format=%h", branch, "--not", "--remotes", `--exclude=${branch.replace(HEADS_PREFIX, "")}`, "--branches"]);
|
|
2235
|
+
if (unique.code !== 0)
|
|
2236
|
+
return keep("log failed");
|
|
2237
|
+
if (unique.stdout !== "")
|
|
2238
|
+
return keep(`${unique.stdout.split(`
|
|
2239
|
+
`).length} commits on no other branch or remote`);
|
|
2240
|
+
return { entry: { path, kind: "worktree", committedAt, action: "remove", repo: top } };
|
|
2241
|
+
}
|
|
2242
|
+
async function judge(candidate) {
|
|
2243
|
+
if (candidate.global === true)
|
|
2244
|
+
return { entry: { path: candidate.path, kind: candidate.kind, committedAt: undefined, action: "remove" } };
|
|
2245
|
+
return candidate.kind === "worktree" ? judgeWorktree(candidate.path) : judgeFolder(candidate.path, candidate.kind, candidate.inside);
|
|
2246
|
+
}
|
|
2247
|
+
async function inspect2({ candidates, skipped }) {
|
|
2248
|
+
const entries = [];
|
|
2249
|
+
const rejected = [...skipped];
|
|
2250
|
+
await pooled(candidates, 8, async (candidate) => {
|
|
2251
|
+
const verdict = await judge(candidate);
|
|
2252
|
+
if ("skip" in verdict)
|
|
2253
|
+
rejected.push({ path: candidate.path, reason: verdict.skip });
|
|
2254
|
+
else
|
|
2255
|
+
entries.push({ ...verdict.entry, bytes: await sizeOf(candidate.path) });
|
|
2256
|
+
});
|
|
2257
|
+
const listed = new Set(entries.filter((entry) => entry.action === "remove").map((entry) => entry.path));
|
|
2258
|
+
return { entries: entries.filter((entry) => entry.inside === undefined || !listed.has(entry.inside)), skipped: rejected };
|
|
2259
|
+
}
|
|
2260
|
+
|
|
2261
|
+
// src/apply.ts
|
|
2262
|
+
async function removeFolder(entry) {
|
|
2263
|
+
try {
|
|
2264
|
+
const stat = await lstat3(entry.path);
|
|
2265
|
+
if (stat.isSymbolicLink() || !stat.isDirectory() || !(FOLDER_NAMES.has(basename3(entry.path)) || entry.kind === "cache" && isCachePath(entry.path)))
|
|
2266
|
+
return { path: entry.path, removed: false, error: "no longer a plain folder" };
|
|
2267
|
+
if (existsSync9(join12(entry.path, ".git")))
|
|
2268
|
+
return { path: entry.path, removed: false, error: "holds a git repository" };
|
|
2269
|
+
await rm(entry.path, { recursive: true, force: true });
|
|
2270
|
+
return { path: entry.path, removed: true };
|
|
2271
|
+
} catch (error) {
|
|
2272
|
+
return { path: entry.path, removed: false, error: error instanceof Error ? error.message : String(error) };
|
|
2273
|
+
}
|
|
2274
|
+
}
|
|
2275
|
+
async function removeWorktree(entry) {
|
|
2276
|
+
if (entry.repo === undefined)
|
|
2277
|
+
return { path: entry.path, removed: false, error: "no repository known" };
|
|
2278
|
+
try {
|
|
2279
|
+
const ran = await runTool("git worktree remove", ["git", "-C", entry.repo, "worktree", "remove", entry.path]);
|
|
2280
|
+
return ran.code === 0 ? { path: entry.path, removed: true } : { path: entry.path, removed: false, error: ran.stderr.trim() };
|
|
2281
|
+
} catch (error) {
|
|
2282
|
+
return { path: entry.path, removed: false, error: String(error) };
|
|
2283
|
+
}
|
|
2284
|
+
}
|
|
2285
|
+
async function applyAll(entries, done) {
|
|
2286
|
+
const outcomes = [];
|
|
2287
|
+
for (const entry of entries) {
|
|
2288
|
+
if (entry.action !== "remove")
|
|
2289
|
+
continue;
|
|
2290
|
+
const outcome = await (entry.kind === "worktree" ? removeWorktree(entry) : removeFolder(entry));
|
|
2291
|
+
outcomes.push(outcome);
|
|
2292
|
+
done(outcome);
|
|
2293
|
+
}
|
|
2294
|
+
return outcomes;
|
|
2295
|
+
}
|
|
2296
|
+
|
|
2297
|
+
// src/sweep.ts
|
|
2298
|
+
var LARGE = 100 * 1024 ** 2;
|
|
2299
|
+
var BIG = 1024 ** 3;
|
|
2300
|
+
var USAGE3 = `sweep [options] [root]: lists the regenerable folders (node_modules, .output, dist, build, target beside a Cargo.toml), the caches (.nuxt, .next, .turbo, .svelte-kit and node_modules/.cache per project; ~/.bun/install/cache, ~/.npm/_cacache, ~/.cache/{pip,uv,ms-playwright,puppeteer,go-build,typescript} and ~/.cargo/registry, exact paths only, listed only when no root is given or with --only caches) and the .worktrees/* and .claude/worktrees/* entries under root (~/Workspace by default, ${MAX_DEPTH3} levels down), largest first with the age of the repo's last commit; --only worktrees, folders or caches keeps one kind
|
|
2301
|
+
|
|
2302
|
+
Dry unless --apply, which deletes after a confirm showing the total (read from the terminal; off one, pass --yes). A folder is deleted for good, not trashed. Never touched: a symlink, a folder git tracks files in, a build folder git does not ignore. A worktree is removed with git worktree remove, never forced, and only when it is clean and every commit of its branch is on another branch or a remote; the others stay listed as kept and count for nothing in the total. With --docker the Docker usage comes after the list, left to dclean.
|
|
2303
|
+
|
|
2304
|
+
Exits 0 when it listed, or removed every entry it was allowed to, 1 when a removal failed or the confirm was declined, 2 on a usage error, a missing root, or --apply off a terminal without --yes.`;
|
|
2305
|
+
var ONLY_KINDS = { worktrees: "worktree", folders: "folder", caches: "cache" };
|
|
2306
|
+
var OPTIONS3 = {
|
|
2307
|
+
apply: { type: "boolean", description: "delete what is listed as removable, after the confirm" },
|
|
2308
|
+
yes: { type: "boolean", short: "y", description: "with --apply, skip the confirm" },
|
|
2309
|
+
older: { type: "string", description: "only the entries whose repo last committed longer ago than this (30d, 12h)" },
|
|
2310
|
+
min: { type: "string", description: "only the entries of at least this size (500K, 10M, 1G)" },
|
|
2311
|
+
only: { type: "string", description: "worktrees, folders or caches: leave the other kinds out (.nuxt, .next, .turbo and .svelte-kit are caches; the global caches are listed with no root or with caches)" },
|
|
2312
|
+
docker: { type: "boolean", description: "add the docker system df output, which alone takes about ten seconds" },
|
|
2313
|
+
json: jsonOption("{root, entries: [{path, kind (folder, worktree or cache), bytes, committedAt, action (remove or keep), reason}], skipped: [{path, reason}], reclaimable, docker, removed?}")
|
|
2314
|
+
};
|
|
2315
|
+
async function dockerUsage() {
|
|
2316
|
+
if (Bun.which("docker") === null)
|
|
2317
|
+
return;
|
|
2318
|
+
try {
|
|
2319
|
+
const { code, stdout } = await runTool("docker system df", ["docker", "system", "df"], { timeoutMs: 1e4 });
|
|
2320
|
+
return code === 0 ? stdout.trimEnd() : undefined;
|
|
2321
|
+
} catch {
|
|
2322
|
+
return;
|
|
2323
|
+
}
|
|
2324
|
+
}
|
|
2325
|
+
async function sweep(args) {
|
|
2326
|
+
const { values, positionals } = parse(args, USAGE3, OPTIONS3, { positionals: ["[root]"], replaces: "find ~/Workspace -name node_modules, du -sh and rm -rf by hand", examples: ["sweep", "sweep ~/Workspace --older 30d --min 100M --apply", "sweep --only worktrees --apply"] });
|
|
2327
|
+
if (positionals.length > 1)
|
|
2328
|
+
badUsage("give one root");
|
|
2329
|
+
const root = resolve5(positionals[0] ?? join13(homedir6(), "Workspace"));
|
|
2330
|
+
if (!existsSync10(root) || !statSync4(root).isDirectory())
|
|
2331
|
+
fail(`${root} is not a folder`);
|
|
2332
|
+
if (values.only !== undefined && !Object.hasOwn(ONLY_KINDS, values.only))
|
|
2333
|
+
badUsage(`--only takes worktrees, folders or caches, not ${values.only}`);
|
|
2334
|
+
const olderMs = values.older === undefined ? undefined : duration(values.older, "older", 0);
|
|
2335
|
+
const minBytes = values.min === undefined ? 0 : parseSize(values.min);
|
|
2336
|
+
if (minBytes === undefined)
|
|
2337
|
+
badUsage(`--min takes a size such as 500K, 10M or 1G, not ${values.min}`);
|
|
2338
|
+
const now = Date.now();
|
|
2339
|
+
const found = await findCandidates(root, homedir6(), positionals.length === 0 || values.only === "caches");
|
|
2340
|
+
const wanted = values.only === undefined ? found.candidates : found.candidates.filter((candidate) => candidate.kind === ONLY_KINDS[values.only]);
|
|
2341
|
+
const { entries: all, skipped } = await inspect2({ candidates: wanted, skipped: found.skipped });
|
|
2342
|
+
const entries = all.filter((entry) => entry.bytes >= minBytes && (olderMs === undefined || entry.committedAt !== undefined && now - entry.committedAt >= olderMs)).sort((a, b) => b.bytes - a.bytes || a.path.localeCompare(b.path));
|
|
2343
|
+
const removable = entries.filter((entry) => entry.action === "remove");
|
|
2344
|
+
const reclaimable = removable.reduce((sum, entry) => sum + entry.bytes, 0);
|
|
2345
|
+
const docker = values.docker === true ? await dockerUsage() : undefined;
|
|
2346
|
+
const json = values.json === true;
|
|
2347
|
+
if (values.apply === true && removable.length > 0 && values.yes !== true) {
|
|
2348
|
+
if (!json)
|
|
2349
|
+
print(root, entries, skipped.length, reclaimable, now);
|
|
2350
|
+
const agreed = await confirm(`Delete ${removable.length} ${removable.length === 1 ? "entry" : "entries"} (${$e(reclaimable)})?`, { default: false, hint: "pass --yes to skip the confirm" });
|
|
2351
|
+
if (agreed !== true) {
|
|
2352
|
+
if (json)
|
|
2353
|
+
fail("nothing deleted: the confirm was declined", 1, "pass --yes to skip the confirm");
|
|
2354
|
+
note("nothing deleted");
|
|
2355
|
+
return 1;
|
|
2356
|
+
}
|
|
2357
|
+
}
|
|
2358
|
+
const outcomes = values.apply === true ? await applyAll(removable, (outcome) => report(root, outcome, json)) : undefined;
|
|
2359
|
+
const failed = outcomes?.filter((outcome) => !outcome.removed).length ?? 0;
|
|
2360
|
+
if (json) {
|
|
2361
|
+
printJson({ root, entries: entries.map((entry) => ({ path: entry.path, kind: entry.kind, bytes: entry.bytes, committedAt: entry.committedAt === undefined ? null : new Date(entry.committedAt).toISOString(), action: entry.action, reason: entry.reason ?? null })), skipped, reclaimable, docker: docker ?? null, ...outcomes === undefined ? {} : { removed: outcomes } });
|
|
2362
|
+
return failed === 0 ? 0 : 1;
|
|
2363
|
+
}
|
|
2364
|
+
if (outcomes === undefined) {
|
|
2365
|
+
print(root, entries, skipped.length, reclaimable, now);
|
|
2366
|
+
if (removable.length > 0)
|
|
2367
|
+
console.log("dry run, pass --apply to delete");
|
|
2368
|
+
} else {
|
|
2369
|
+
const bytes = new Map(removable.map((entry) => [entry.path, entry.bytes]));
|
|
2370
|
+
note(`freed ${$e(outcomes.reduce((sum, outcome) => sum + (outcome.removed ? bytes.get(outcome.path) ?? 0 : 0), 0))}`);
|
|
2371
|
+
}
|
|
2372
|
+
if (docker !== undefined)
|
|
2373
|
+
console.log(`
|
|
2374
|
+
docker (left to dclean):
|
|
2375
|
+
${docker}`);
|
|
2376
|
+
return failed === 0 ? 0 : 1;
|
|
2377
|
+
}
|
|
2378
|
+
function report(root, outcome, json) {
|
|
2379
|
+
if (json)
|
|
2380
|
+
return;
|
|
2381
|
+
if (outcome.removed)
|
|
2382
|
+
ok(`removed ${relative3(root, outcome.path)}`);
|
|
2383
|
+
else
|
|
2384
|
+
problem(`kept ${relative3(root, outcome.path)}`, outcome.error);
|
|
2385
|
+
}
|
|
2386
|
+
function print(root, entries, skipped, reclaimable, now) {
|
|
2387
|
+
if (entries.length === 0) {
|
|
2388
|
+
console.log("nothing to sweep");
|
|
2389
|
+
return;
|
|
2390
|
+
}
|
|
2391
|
+
const colors = paint(process.stdout);
|
|
2392
|
+
const path = role("path", process.stdout);
|
|
2393
|
+
const sized = (bytes) => {
|
|
2394
|
+
if (bytes >= BIG)
|
|
2395
|
+
return colors.red($e(bytes));
|
|
2396
|
+
return bytes >= LARGE ? colors.yellow($e(bytes)) : $e(bytes);
|
|
2397
|
+
};
|
|
2398
|
+
console.log(table([["size", "age", "what", "path", "note"], ...entries.map((entry) => [sized(entry.bytes), entry.committedAt === undefined ? colors.dim("-") : colors.dim(formatAge(entry.committedAt, now)), entry.kind, path(relative3(root, entry.path)), entry.action === "keep" ? colors.dim(`kept: ${entry.reason}`) : ""])], { header: true, align: ["right", "right", "left", "left", "left"] }));
|
|
2399
|
+
console.log(`
|
|
2400
|
+
${$e(reclaimable)} reclaimable${skipped === 0 ? "" : `, ${skipped} skipped (symlinks, tracked or not ignored by git)`}`);
|
|
2401
|
+
}
|
|
2402
|
+
|
|
2403
|
+
// src/cli.ts
|
|
2404
|
+
process18.exitCode = await runCli({
|
|
2405
|
+
name: "ws",
|
|
2406
|
+
about: "the repositories under a root: the ones needing attention, the folders worth reclaiming and their known vulnerabilities",
|
|
2407
|
+
replaces: "a cd, git status and du loop over each repository of the workspace, and a bun audit run in each",
|
|
2408
|
+
examples: ["ws repos", ["ws sweep --older 30d", "what a month of idle repos weighs"], "ws audit"],
|
|
2409
|
+
commands: {
|
|
2410
|
+
repos: { summary: "the git repositories that need attention: dirty, ahead or behind, worktrees, unreleased commits", run: repos },
|
|
2411
|
+
sweep: { summary: "the regenerable folders, caches and clean worktrees, largest first, deleted only on --apply", run: sweep },
|
|
2412
|
+
audit: { summary: "the known vulnerabilities of the Bun repositories, one table per repository", run: audit }
|
|
2413
|
+
}
|
|
2414
|
+
});
|