@kaynooo/cli-check 0.13.0 → 0.15.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/README.md +2 -2
- package/dist/cli.js +398 -139
- package/dist/errs-cli.js +355 -108
- package/dist/fix-cli.js +373 -126
- package/dist/j-cli.js +1032 -208
- package/package.json +2 -2
package/dist/j-cli.js
CHANGED
|
@@ -2,16 +2,37 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/j-cli.ts
|
|
5
|
-
import
|
|
5
|
+
import process22 from "process";
|
|
6
6
|
// ../kit/src/args.ts
|
|
7
|
-
import
|
|
7
|
+
import process10 from "process";
|
|
8
8
|
import { parseArgs } from "util";
|
|
9
9
|
|
|
10
10
|
// ../kit/src/format.ts
|
|
11
|
-
import
|
|
11
|
+
import process7 from "process";
|
|
12
12
|
|
|
13
|
-
// ../kit/src/
|
|
13
|
+
// ../kit/src/mode.ts
|
|
14
14
|
import process2 from "process";
|
|
15
|
+
var MODES = new Set(["terminal", "ci", "agent", "pipe"]);
|
|
16
|
+
var AGENT_VARIABLES = ["CLAUDECODE", "AI_AGENT"];
|
|
17
|
+
function isSet(value) {
|
|
18
|
+
return value !== undefined && value !== "" && value !== "0" && value.toLowerCase() !== "false";
|
|
19
|
+
}
|
|
20
|
+
function outputMode(stream = process2.stdout, env = process2.env) {
|
|
21
|
+
const chosen = env.KIT_MODE;
|
|
22
|
+
if (chosen !== undefined && MODES.has(chosen))
|
|
23
|
+
return chosen;
|
|
24
|
+
if (AGENT_VARIABLES.some((name) => isSet(env[name])))
|
|
25
|
+
return "agent";
|
|
26
|
+
if (stream.isTTY === true)
|
|
27
|
+
return "terminal";
|
|
28
|
+
return isSet(env.CI) ? "ci" : "pipe";
|
|
29
|
+
}
|
|
30
|
+
function isInteractive(streams = process2) {
|
|
31
|
+
return streams.stdin?.isTTY === true && streams.stderr?.isTTY === true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// ../kit/src/style.ts
|
|
35
|
+
import process6 from "process";
|
|
15
36
|
|
|
16
37
|
// ../../node_modules/.bun/@kaynooo+utils@2.31.2+d0769e28dc4dad16/node_modules/@kaynooo/utils/dist/chunk-mtetydvc.js
|
|
17
38
|
function x(s) {
|
|
@@ -274,36 +295,252 @@ function le(i = {}) {
|
|
|
274
295
|
}
|
|
275
296
|
var t = le();
|
|
276
297
|
|
|
277
|
-
// ../kit/src/
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
298
|
+
// ../kit/src/theme.ts
|
|
299
|
+
import { existsSync as existsSync2, readFileSync, statSync } from "fs";
|
|
300
|
+
import { homedir as homedir2 } from "os";
|
|
301
|
+
import { join as join3, resolve as resolve2 } from "path";
|
|
302
|
+
import process5 from "process";
|
|
303
|
+
|
|
304
|
+
// ../kit/src/config.ts
|
|
305
|
+
import { existsSync } from "fs";
|
|
306
|
+
import { dirname, join, resolve } from "path";
|
|
307
|
+
import process3 from "process";
|
|
308
|
+
function findUp(names, from = process3.cwd(), stop = () => false) {
|
|
309
|
+
const wanted = typeof names === "string" ? [names] : names;
|
|
310
|
+
for (let dir = resolve(from);; dir = dirname(dir)) {
|
|
311
|
+
const found = wanted.map((name) => join(dir, name)).find((path) => existsSync(path));
|
|
312
|
+
if (found !== undefined)
|
|
313
|
+
return found;
|
|
314
|
+
if (stop(dir) || dirname(dir) === dir)
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// ../kit/src/user-config.ts
|
|
320
|
+
import { homedir } from "os";
|
|
321
|
+
import { join as join2 } from "path";
|
|
322
|
+
import process4 from "process";
|
|
323
|
+
function configBase(where = {}) {
|
|
324
|
+
const xdg = where.xdg ?? (where.home === undefined ? process4.env.XDG_CONFIG_HOME : undefined);
|
|
325
|
+
return xdg !== undefined && xdg !== "" ? xdg : join2(where.home ?? homedir(), ".config");
|
|
326
|
+
}
|
|
327
|
+
function userConfigPath(bin, where = {}) {
|
|
328
|
+
return join2(configBase(where), "kaynooo-cli", `${bin}.json`);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// ../kit/src/theme.ts
|
|
332
|
+
var DEFAULT = {
|
|
333
|
+
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" },
|
|
334
|
+
glyphs: { ok: "\u2713", warn: "\u25B2", problem: "\u2717", note: "\u203A", passed: "\u2713", failed: "\u2717", running: "\u25CF", queued: "\u25CB", skipped: "-", bar: "\u2501" },
|
|
335
|
+
accents: ["cyan", "magenta", "green", "blue", "yellow", "redBright"],
|
|
336
|
+
bins: {}
|
|
337
|
+
};
|
|
338
|
+
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" };
|
|
339
|
+
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" };
|
|
340
|
+
var ASCII = { ok: "+", warn: "!", problem: "x", note: ">", passed: "+", failed: "x", running: "*", queued: "o", skipped: "-", bar: "#" };
|
|
341
|
+
var PRESETS = {
|
|
342
|
+
default: {},
|
|
343
|
+
"high-contrast": { colors: BRIGHT, accents: ["cyanBright", "magentaBright", "greenBright", "blueBright", "yellowBright", "redBright"] },
|
|
344
|
+
mono: { colors: MONO, accents: ["bold"] },
|
|
345
|
+
ascii: { glyphs: ASCII }
|
|
283
346
|
};
|
|
284
|
-
|
|
285
|
-
|
|
347
|
+
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"]);
|
|
348
|
+
var HEX = /^#[0-9a-f]{6}$/i;
|
|
349
|
+
var CONTROL = /\p{Cc}/u;
|
|
350
|
+
var MAX_FILE_BYTES = 64 * 1024;
|
|
351
|
+
var SPACES = /\s+/;
|
|
352
|
+
function isColorSpec(spec) {
|
|
353
|
+
if (typeof spec === "number")
|
|
354
|
+
return Number.isInteger(spec) && spec >= 0 && spec <= 255;
|
|
355
|
+
return typeof spec === "string" && spec.trim() !== "" && spec.trim().split(SPACES).every((token) => STYLES.has(token) || HEX.test(token));
|
|
356
|
+
}
|
|
357
|
+
var CUBE = [0, 95, 135, 175, 215, 255];
|
|
358
|
+
function cubeLevel(channel) {
|
|
359
|
+
return CUBE.reduce((best, value, level) => Math.abs(value - channel) < Math.abs(CUBE[best] - channel) ? level : best, 0);
|
|
360
|
+
}
|
|
361
|
+
function nearest256(hex) {
|
|
362
|
+
const rgb = [1, 3, 5].map((at) => Number.parseInt(hex.slice(at, at + 2), 16));
|
|
363
|
+
const levels = rgb.map(cubeLevel);
|
|
364
|
+
const cube = levels.map((level) => CUBE[level]);
|
|
365
|
+
const grey = Math.min(23, Math.max(0, Math.round((rgb.reduce((sum, channel) => sum + channel, 0) / 3 - 8) / 10)));
|
|
366
|
+
const greyValue = 8 + grey * 10;
|
|
367
|
+
const distance = (colour) => colour.reduce((sum, channel, at) => sum + (channel - rgb[at]) ** 2, 0);
|
|
368
|
+
return distance([greyValue, greyValue, greyValue]) < distance(cube) ? 232 + grey : 16 + 36 * levels[0] + 6 * levels[1] + levels[2];
|
|
369
|
+
}
|
|
370
|
+
function hasTrueColor(env) {
|
|
371
|
+
return env.COLORTERM === "truecolor" || env.COLORTERM === "24bit";
|
|
372
|
+
}
|
|
373
|
+
function tinted(colors, spec, env = process5.env) {
|
|
374
|
+
if (typeof spec === "number")
|
|
375
|
+
return colors.ansi256(spec);
|
|
376
|
+
return spec.trim().split(SPACES).reduce((chain, token) => {
|
|
377
|
+
if (HEX.test(token))
|
|
378
|
+
return hasTrueColor(env) ? chain.hex(token) : chain.ansi256(nearest256(token));
|
|
379
|
+
return chain[token] ?? chain;
|
|
380
|
+
}, colors);
|
|
381
|
+
}
|
|
382
|
+
function isRecord(value) {
|
|
383
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
384
|
+
}
|
|
385
|
+
function readThemeFile(path, warn) {
|
|
386
|
+
try {
|
|
387
|
+
const stat = statSync(path, { throwIfNoEntry: false });
|
|
388
|
+
if (stat === undefined)
|
|
389
|
+
return;
|
|
390
|
+
if (!stat.isFile() || stat.size > MAX_FILE_BYTES) {
|
|
391
|
+
warn(`${path} is not a regular file under ${MAX_FILE_BYTES / 1024} KiB, theme ignored`);
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
const parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
395
|
+
if (isRecord(parsed))
|
|
396
|
+
return parsed;
|
|
397
|
+
warn(`${path} is not a JSON object, theme ignored`);
|
|
398
|
+
} catch (error) {
|
|
399
|
+
warn(`${path} does not parse (${error instanceof Error ? error.message : String(error)}), theme ignored`);
|
|
400
|
+
}
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
function applyPreset(theme, name, warn, where) {
|
|
404
|
+
if (name === undefined)
|
|
405
|
+
return;
|
|
406
|
+
if (typeof name !== "string" || !Object.hasOwn(PRESETS, name)) {
|
|
407
|
+
warn(`${where}: no preset ${String(name)}, one of ${Object.keys(PRESETS).join(", ")}`);
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
const preset = PRESETS[name];
|
|
411
|
+
Object.assign(theme.colors, preset.colors);
|
|
412
|
+
Object.assign(theme.glyphs, preset.glyphs);
|
|
413
|
+
if (preset.accents !== undefined)
|
|
414
|
+
theme.accents = [...preset.accents];
|
|
415
|
+
return name;
|
|
416
|
+
}
|
|
417
|
+
function applyFile(theme, file, where, warn) {
|
|
418
|
+
if (isRecord(file.colors)) {
|
|
419
|
+
for (const [key, value] of Object.entries(file.colors)) {
|
|
420
|
+
if (!Object.hasOwn(DEFAULT.colors, key))
|
|
421
|
+
warn(`${where}: no colour ${key}, one of ${Object.keys(DEFAULT.colors).join(", ")}`);
|
|
422
|
+
else if (!isColorSpec(value))
|
|
423
|
+
warn(`${where}: colour ${key} is not an ANSI name, \`#rrggbb\` or a number up to 255`);
|
|
424
|
+
else
|
|
425
|
+
theme.colors[key] = value;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
if (isRecord(file.glyphs)) {
|
|
429
|
+
for (const [key, value] of Object.entries(file.glyphs)) {
|
|
430
|
+
if (!Object.hasOwn(DEFAULT.glyphs, key))
|
|
431
|
+
warn(`${where}: no glyph ${key}, one of ${Object.keys(DEFAULT.glyphs).join(", ")}`);
|
|
432
|
+
else if (typeof value !== "string" || CONTROL.test(value) || Bun.stringWidth(value) !== 1)
|
|
433
|
+
warn(`${where}: glyph ${key} must be one visible character wide, with no control character`);
|
|
434
|
+
else
|
|
435
|
+
theme.glyphs[key] = value;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
if (Array.isArray(file.accents) && file.accents.length > 0 && file.accents.every(isColorSpec))
|
|
439
|
+
theme.accents = [...file.accents];
|
|
440
|
+
else if (file.accents !== undefined)
|
|
441
|
+
warn(`${where}: accents must be a non-empty list of colours`);
|
|
442
|
+
if (isRecord(file.bins)) {
|
|
443
|
+
for (const [bin, value] of Object.entries(file.bins)) {
|
|
444
|
+
if (isColorSpec(value))
|
|
445
|
+
theme.bins[bin] = value;
|
|
446
|
+
else
|
|
447
|
+
warn(`${where}: bin ${bin} has no valid colour`);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
function endsWalk(home) {
|
|
452
|
+
return (dir) => dir === resolve2(home) || existsSync2(join3(dir, ".git"));
|
|
286
453
|
}
|
|
287
|
-
|
|
454
|
+
var warned = new Set;
|
|
455
|
+
function warnOnce(message) {
|
|
456
|
+
if (warned.has(message))
|
|
457
|
+
return;
|
|
458
|
+
warned.add(message);
|
|
459
|
+
console.error(`warning: theme ${message}`);
|
|
460
|
+
}
|
|
461
|
+
function isPath(chosen) {
|
|
462
|
+
return chosen.includes("/") || chosen.endsWith(".json");
|
|
463
|
+
}
|
|
464
|
+
function loadTheme({ env = process5.env, from = process5.cwd(), home = homedir2(), warn = warnOnce } = {}) {
|
|
465
|
+
const theme = { colors: { ...DEFAULT.colors }, glyphs: { ...DEFAULT.glyphs }, accents: [...DEFAULT.accents], bins: Object.create(null) };
|
|
466
|
+
const sources = [];
|
|
467
|
+
const chosen = env.KIT_THEME ?? "";
|
|
468
|
+
const wanted = chosen === "" ? [userConfigPath("theme", { home, xdg: env.XDG_CONFIG_HOME }), findUp(join3(".kaynooo", "theme.json"), from, endsWalk(home))] : [];
|
|
469
|
+
if (isPath(chosen))
|
|
470
|
+
wanted.push(resolve2(from, chosen));
|
|
471
|
+
const files = [];
|
|
472
|
+
for (const path of wanted) {
|
|
473
|
+
const file = path === undefined ? undefined : readThemeFile(path, warn);
|
|
474
|
+
if (path !== undefined && file !== undefined)
|
|
475
|
+
files.push({ path, file });
|
|
476
|
+
}
|
|
477
|
+
let preset = "default";
|
|
478
|
+
if (chosen !== "" && !isPath(chosen)) {
|
|
479
|
+
preset = applyPreset(theme, chosen, warn, "KIT_THEME") ?? preset;
|
|
480
|
+
} else {
|
|
481
|
+
const named = files.findLast(({ file }) => file.preset !== undefined);
|
|
482
|
+
if (named !== undefined)
|
|
483
|
+
preset = applyPreset(theme, named.file.preset, warn, named.path) ?? preset;
|
|
484
|
+
}
|
|
485
|
+
for (const { path, file } of files) {
|
|
486
|
+
applyFile(theme, file, path, warn);
|
|
487
|
+
sources.push(path);
|
|
488
|
+
}
|
|
489
|
+
return { theme, sources, preset };
|
|
490
|
+
}
|
|
491
|
+
var current;
|
|
492
|
+
var currentKey = "";
|
|
493
|
+
function activeTheme() {
|
|
494
|
+
const key = `${process5.cwd()}\x00${process5.env.KIT_THEME ?? ""}\x00${process5.env.XDG_CONFIG_HOME ?? ""}`;
|
|
495
|
+
if (current === undefined || key !== currentKey) {
|
|
496
|
+
current = loadTheme();
|
|
497
|
+
currentKey = key;
|
|
498
|
+
}
|
|
499
|
+
return current;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// ../kit/src/style.ts
|
|
503
|
+
var MARK_WORDS = { ok: "ok:", warn: "warning:", problem: "error:", note: "note:" };
|
|
504
|
+
function takesControlCodes(stream, env = process6.env) {
|
|
505
|
+
return stream.isTTY === true && env.NO_COLOR === undefined && env.NODE_DISABLE_COLORS === undefined && env.TERM !== "dumb" && outputMode(stream, env) === "terminal";
|
|
506
|
+
}
|
|
507
|
+
function paint(stream, env = process6.env) {
|
|
288
508
|
return le({ stream: { isTTY: takesControlCodes(stream, env) }, env });
|
|
289
509
|
}
|
|
290
|
-
function marked(mark, text, stream, detail, env =
|
|
510
|
+
function marked(mark, text, stream, detail, env = process6.env) {
|
|
291
511
|
const colors = paint(stream, env);
|
|
292
|
-
|
|
293
|
-
|
|
512
|
+
let head = MARK_WORDS[mark];
|
|
513
|
+
if (takesControlCodes(stream, env)) {
|
|
514
|
+
const { theme } = activeTheme();
|
|
515
|
+
head = tinted(colors.bold, theme.colors[mark], env)(theme.glyphs[mark]);
|
|
516
|
+
}
|
|
294
517
|
return `${head} ${text}${detail === undefined ? "" : ` ${colors.dim(detail)}`}`;
|
|
295
518
|
}
|
|
296
519
|
function warn(text, detail) {
|
|
297
|
-
console.error(marked("warn", text,
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
520
|
+
console.error(marked("warn", text, process6.stderr, detail));
|
|
521
|
+
}
|
|
522
|
+
function role(name, stream, env = process6.env) {
|
|
523
|
+
const colors = paint(stream, env);
|
|
524
|
+
return takesControlCodes(stream, env) ? tinted(colors, activeTheme().theme.colors[name], env) : colors;
|
|
525
|
+
}
|
|
526
|
+
var STATUS_WORDS = {
|
|
527
|
+
passed: ["passed", "success", "succeeded", "ok"],
|
|
528
|
+
failed: ["failed", "failure", "error", "errored"],
|
|
529
|
+
running: ["running", "in_progress", "started", "holding"],
|
|
530
|
+
queued: ["queued", "waiting", "pending", "created", "blocked", "scheduled"],
|
|
531
|
+
skipped: ["skipped", "cancelled", "canceled", "manual"]
|
|
305
532
|
};
|
|
306
|
-
var STATUS_OF = new Map(Object.entries(
|
|
533
|
+
var STATUS_OF = new Map(Object.entries(STATUS_WORDS).flatMap(([status, words]) => words.map((word) => [word, status])));
|
|
534
|
+
function header(bin, context, stream, env = process6.env) {
|
|
535
|
+
if (!takesControlCodes(stream, env))
|
|
536
|
+
return "";
|
|
537
|
+
const colors = paint(stream, env);
|
|
538
|
+
const hash = [...bin].reduce((sum, char) => sum + char.charCodeAt(0), 0);
|
|
539
|
+
const { theme } = activeTheme();
|
|
540
|
+
const spec = theme.bins[bin] ?? theme.accents[hash % theme.accents.length];
|
|
541
|
+
const accent = tinted(typeof spec === "string" && spec.split(" ").includes("bold") ? colors : colors.bold, spec, env);
|
|
542
|
+
return `${accent(bin)}${context === undefined ? "" : colors.dim(` \u203A ${context}`)}`;
|
|
543
|
+
}
|
|
307
544
|
|
|
308
545
|
// ../kit/src/format.ts
|
|
309
546
|
var UNITS = [["d", 86400], ["h", 3600], ["m", 60], ["s", 1]];
|
|
@@ -335,15 +572,18 @@ var ESCAPE = String.fromCharCode(27);
|
|
|
335
572
|
function textWidth(text) {
|
|
336
573
|
return Bun.stringWidth(text);
|
|
337
574
|
}
|
|
338
|
-
function table(rows, { cellWidth, header = false, align = [], fit } = {}) {
|
|
575
|
+
function table(rows, { cellWidth, header = false, align = [], fit, stream = process7.stdout, env = process7.env } = {}) {
|
|
339
576
|
const cells = cellWidth === undefined ? rows : rows.map((row) => row.map((cell) => truncate(cell.replaceAll(LINE_BREAKS, " "), cellWidth)));
|
|
577
|
+
if (outputMode(stream, env) === "agent")
|
|
578
|
+
return cells.map((row) => row.map((cell) => Bun.stripANSI(cell).replaceAll(/[\t\r\n]+/g, " ")).join("\t")).join(`
|
|
579
|
+
`);
|
|
340
580
|
const widths = [];
|
|
341
581
|
for (const row of cells) {
|
|
342
582
|
for (const [index, cell] of row.entries())
|
|
343
583
|
widths[index] = Math.max(widths[index] ?? 0, textWidth(cell));
|
|
344
584
|
}
|
|
345
585
|
const shown = fit === undefined ? cells : narrow(cells, widths, fit);
|
|
346
|
-
const colors = paint(
|
|
586
|
+
const colors = paint(stream);
|
|
347
587
|
const lines = shown.map((row) => row.map((cell, index) => {
|
|
348
588
|
const width = widths[index] ?? 0;
|
|
349
589
|
const gap = " ".repeat(Math.max(0, width - textWidth(cell)));
|
|
@@ -351,7 +591,8 @@ function table(rows, { cellWidth, header = false, align = [], fit } = {}) {
|
|
|
351
591
|
return `${gap}${cell}`;
|
|
352
592
|
return index === row.length - 1 ? cell : `${cell}${gap}`;
|
|
353
593
|
}).join(" ").trimEnd());
|
|
354
|
-
|
|
594
|
+
const rule = header && takesControlCodes(stream) && lines.length > 1 ? [colors.dim("\u2500".repeat(Math.max(...lines.map(textWidth))))] : [];
|
|
595
|
+
return lines.flatMap((line, index) => header && index === 0 ? [colors.bold(line), ...rule] : [line]).join(`
|
|
355
596
|
`);
|
|
356
597
|
}
|
|
357
598
|
function narrow(rows, widths, limit) {
|
|
@@ -370,32 +611,35 @@ function narrow(rows, widths, limit) {
|
|
|
370
611
|
}
|
|
371
612
|
return rows.map((row) => row.map((cell, index) => textWidth(cell) > (widths[index] ?? 0) ? truncate(cell, widths[index] ?? 0) : cell));
|
|
372
613
|
}
|
|
614
|
+
function jsonText(value, replacer, { stream = process7.stdout, env = process7.env } = {}) {
|
|
615
|
+
return JSON.stringify(value, replacer, outputMode(stream, env) === "agent" ? undefined : 2);
|
|
616
|
+
}
|
|
373
617
|
function printJson(value) {
|
|
374
|
-
console.log(
|
|
618
|
+
console.log(jsonText(value));
|
|
375
619
|
}
|
|
376
620
|
|
|
377
621
|
// ../kit/src/help.ts
|
|
378
|
-
import
|
|
622
|
+
import process8 from "process";
|
|
379
623
|
var OPTIONAL = /\[[^\]]+\]/g;
|
|
380
624
|
var USAGE_PREFIX = /^usage:\s*/i;
|
|
381
625
|
var FULL_STOP = /\.$/;
|
|
382
|
-
function usageLine(usage, colors = paint(
|
|
626
|
+
function usageLine(usage, colors = paint(process8.stdout)) {
|
|
383
627
|
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
384
628
|
}
|
|
385
|
-
function helpText(usage, options, colors = paint(
|
|
629
|
+
function helpText(usage, options, colors = paint(process8.stdout), { examples = [], replaces, exits = [] } = {}) {
|
|
386
630
|
return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
|
|
387
631
|
`);
|
|
388
632
|
}
|
|
389
|
-
function exitRows(exits, colors = paint(
|
|
633
|
+
function exitRows(exits, colors = paint(process8.stdout)) {
|
|
390
634
|
if (exits.length === 0)
|
|
391
635
|
return [];
|
|
392
636
|
const width = Math.max(...exits.map(([code]) => String(code).length));
|
|
393
637
|
return ["", colors.bold("Exits:"), ...exits.map(([code, meaning]) => ` ${colors.cyan(String(code).padEnd(width))} ${meaning}`)];
|
|
394
638
|
}
|
|
395
|
-
function replacesRows(replaces, colors = paint(
|
|
396
|
-
return replaces === undefined ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
639
|
+
function replacesRows(replaces, colors = paint(process8.stdout)) {
|
|
640
|
+
return replaces === undefined || outputMode() === "agent" ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
397
641
|
}
|
|
398
|
-
function wantsHelpJson(env =
|
|
642
|
+
function wantsHelpJson(env = process8.env) {
|
|
399
643
|
return env.KIT_HELP === "json";
|
|
400
644
|
}
|
|
401
645
|
function description(usage, { examples = [], replaces, exits } = {}, options) {
|
|
@@ -414,12 +658,12 @@ function description(usage, { examples = [], replaces, exits } = {}, options) {
|
|
|
414
658
|
...options === undefined ? {} : { options: Object.entries(options).map(([name, { short, description }]) => ({ name, ...short === undefined ? {} : { short }, ...description === undefined ? {} : { description } })) }
|
|
415
659
|
};
|
|
416
660
|
}
|
|
417
|
-
function exampleRows(examples, colors = paint(
|
|
418
|
-
if (examples.length === 0)
|
|
661
|
+
function exampleRows(examples, colors = paint(process8.stdout)) {
|
|
662
|
+
if (examples.length === 0 || outputMode() === "agent")
|
|
419
663
|
return [];
|
|
420
664
|
return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
|
|
421
665
|
}
|
|
422
|
-
function optionRows(options, colors = paint(
|
|
666
|
+
function optionRows(options, colors = paint(process8.stdout)) {
|
|
423
667
|
const rows = Object.entries(options).map(([name, option]) => {
|
|
424
668
|
const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
|
|
425
669
|
const named = option.choices === undefined ? "<value>" : `<${option.choices.join("|")}>`;
|
|
@@ -454,7 +698,7 @@ function closest(name, names) {
|
|
|
454
698
|
return best !== undefined && best.far <= (name.length < 4 ? 1 : 2) ? best.known : undefined;
|
|
455
699
|
}
|
|
456
700
|
// ../kit/src/input.ts
|
|
457
|
-
import
|
|
701
|
+
import process9 from "process";
|
|
458
702
|
var DURATION = /^(\d+(?:\.\d+)?)(ms|[smhd])?$/;
|
|
459
703
|
var UNIT_MS = { ms: 1, s: 1000, m: 60000, h: 3600000, d: 86400000 };
|
|
460
704
|
function duration(value, name, fallback, unit = "s") {
|
|
@@ -468,7 +712,7 @@ function duration(value, name, fallback, unit = "s") {
|
|
|
468
712
|
}
|
|
469
713
|
async function readInput(path, { noStdin = "nothing is piped on stdin" } = {}) {
|
|
470
714
|
if (path === "-") {
|
|
471
|
-
if (
|
|
715
|
+
if (process9.stdin.isTTY)
|
|
472
716
|
fail(noStdin);
|
|
473
717
|
return Bun.stdin.text();
|
|
474
718
|
}
|
|
@@ -527,7 +771,7 @@ var OUTPUT_OPTIONS = {
|
|
|
527
771
|
color: { type: "string", description: "auto (on a terminal alone), always or never" },
|
|
528
772
|
plain: { type: "boolean", description: "plain text, no colour or spinner: --color never" }
|
|
529
773
|
};
|
|
530
|
-
function applyOutput(color, plain, env =
|
|
774
|
+
function applyOutput(color, plain, env = process10.env) {
|
|
531
775
|
const when = plain === true ? "never" : color ?? "auto";
|
|
532
776
|
if (when === "auto")
|
|
533
777
|
return;
|
|
@@ -698,7 +942,7 @@ function nearestOption(message, options, allowNegative) {
|
|
|
698
942
|
}
|
|
699
943
|
// ../kit/src/process.ts
|
|
700
944
|
import { constants } from "os";
|
|
701
|
-
import
|
|
945
|
+
import process11 from "process";
|
|
702
946
|
var KILL_GRACE_MS = 2000;
|
|
703
947
|
var SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
704
948
|
function exitCodeOf(code, signal) {
|
|
@@ -720,7 +964,7 @@ function holdSignals(cleanup) {
|
|
|
720
964
|
},
|
|
721
965
|
release: () => {
|
|
722
966
|
for (const signal of SIGNALS)
|
|
723
|
-
|
|
967
|
+
process11.off(signal, onSignal);
|
|
724
968
|
}
|
|
725
969
|
};
|
|
726
970
|
function onSignal(signal) {
|
|
@@ -733,10 +977,10 @@ function holdSignals(cleanup) {
|
|
|
733
977
|
return;
|
|
734
978
|
held.release();
|
|
735
979
|
cleanup(signal);
|
|
736
|
-
|
|
980
|
+
process11.kill(process11.pid, signal);
|
|
737
981
|
}
|
|
738
982
|
for (const signal of SIGNALS)
|
|
739
|
-
|
|
983
|
+
process11.on(signal, onSignal);
|
|
740
984
|
return held;
|
|
741
985
|
}
|
|
742
986
|
async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal, maxBytes = Infinity, onChunk, onStart } = {}) {
|
|
@@ -750,7 +994,7 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal,
|
|
|
750
994
|
return;
|
|
751
995
|
try {
|
|
752
996
|
if (grouped)
|
|
753
|
-
|
|
997
|
+
process11.kill(-proc.pid, killing);
|
|
754
998
|
else
|
|
755
999
|
proc.kill(killing);
|
|
756
1000
|
} catch {}
|
|
@@ -824,42 +1068,42 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
|
|
|
824
1068
|
var RESET_STYLE = `${ESC}[0m`;
|
|
825
1069
|
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
826
1070
|
// ../kit/src/cli.ts
|
|
827
|
-
import { readFileSync, realpathSync } from "fs";
|
|
828
|
-
import { dirname as
|
|
829
|
-
import
|
|
1071
|
+
import { readFileSync as readFileSync2, realpathSync } from "fs";
|
|
1072
|
+
import { dirname as dirname3, join as join6 } from "path";
|
|
1073
|
+
import process14 from "process";
|
|
830
1074
|
|
|
831
1075
|
// ../kit/src/usage.ts
|
|
832
1076
|
import { appendFileSync, mkdirSync } from "fs";
|
|
833
|
-
import { homedir as
|
|
834
|
-
import { dirname, isAbsolute, join as
|
|
835
|
-
import
|
|
1077
|
+
import { homedir as homedir4 } from "os";
|
|
1078
|
+
import { dirname as dirname2, isAbsolute, join as join5 } from "path";
|
|
1079
|
+
import process13 from "process";
|
|
836
1080
|
|
|
837
1081
|
// ../kit/src/logs.ts
|
|
838
1082
|
import { lstatSync, openSync } from "fs";
|
|
839
1083
|
import { mkdir, mkdtemp, readdir, rm, stat } from "fs/promises";
|
|
840
|
-
import { homedir, tmpdir } from "os";
|
|
841
|
-
import { join } from "path";
|
|
842
|
-
import
|
|
1084
|
+
import { homedir as homedir3, tmpdir } from "os";
|
|
1085
|
+
import { join as join4 } from "path";
|
|
1086
|
+
import process12 from "process";
|
|
843
1087
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
844
1088
|
var CLI_FOLDER = "kaynooo-cli";
|
|
845
1089
|
function isPrivateDirectory(path) {
|
|
846
1090
|
try {
|
|
847
1091
|
const info = lstatSync(path, { throwIfNoEntry: false });
|
|
848
|
-
return info !== undefined && info.isDirectory() && info.uid === (
|
|
1092
|
+
return info !== undefined && info.isDirectory() && info.uid === (process12.getuid?.() ?? info.uid) && (info.mode & 63) === 0;
|
|
849
1093
|
} catch {
|
|
850
1094
|
return false;
|
|
851
1095
|
}
|
|
852
1096
|
}
|
|
853
|
-
async function privateLogDirectory(name, env =
|
|
1097
|
+
async function privateLogDirectory(name, env = process12.env) {
|
|
854
1098
|
const runtime = env.XDG_RUNTIME_DIR;
|
|
855
|
-
const base = runtime === undefined || runtime === "" ?
|
|
856
|
-
const path =
|
|
1099
|
+
const base = runtime === undefined || runtime === "" ? join4(homedir3(), ".cache", CLI_FOLDER) : join4(runtime, CLI_FOLDER);
|
|
1100
|
+
const path = join4(base, name);
|
|
857
1101
|
try {
|
|
858
1102
|
await mkdir(path, { recursive: true, mode: 448 });
|
|
859
1103
|
if (isPrivateDirectory(path))
|
|
860
1104
|
return path;
|
|
861
1105
|
} catch {}
|
|
862
|
-
return mkdtemp(
|
|
1106
|
+
return mkdtemp(join4(tmpdir(), `${name.replaceAll("/", "-")}-`));
|
|
863
1107
|
}
|
|
864
1108
|
async function sweepLogs(dir, maxAgeMs = DAY_MS, now = Date.now()) {
|
|
865
1109
|
let names;
|
|
@@ -869,7 +1113,7 @@ async function sweepLogs(dir, maxAgeMs = DAY_MS, now = Date.now()) {
|
|
|
869
1113
|
return;
|
|
870
1114
|
}
|
|
871
1115
|
for (const name of names) {
|
|
872
|
-
const path =
|
|
1116
|
+
const path = join4(dir, name);
|
|
873
1117
|
try {
|
|
874
1118
|
const info = await stat(path);
|
|
875
1119
|
if (info.isDirectory())
|
|
@@ -890,14 +1134,14 @@ var usageError;
|
|
|
890
1134
|
function noteUsageError(message) {
|
|
891
1135
|
usageError = message;
|
|
892
1136
|
}
|
|
893
|
-
function stateHome(env =
|
|
1137
|
+
function stateHome(env = process13.env) {
|
|
894
1138
|
const dir = env.XDG_STATE_HOME;
|
|
895
|
-
return dir !== undefined && isAbsolute(dir) ? dir :
|
|
1139
|
+
return dir !== undefined && isAbsolute(dir) ? dir : join5(homedir4(), ".local", "state");
|
|
896
1140
|
}
|
|
897
|
-
function usagePath(env =
|
|
898
|
-
return
|
|
1141
|
+
function usagePath(env = process13.env) {
|
|
1142
|
+
return join5(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
899
1143
|
}
|
|
900
|
-
function usageRecorded(env =
|
|
1144
|
+
function usageRecorded(env = process13.env) {
|
|
901
1145
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
902
1146
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
903
1147
|
}
|
|
@@ -911,12 +1155,12 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
911
1155
|
usageError = undefined;
|
|
912
1156
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
913
1157
|
}
|
|
914
|
-
function recordUsage(entry, env =
|
|
1158
|
+
function recordUsage(entry, env = process13.env) {
|
|
915
1159
|
if (!usageRecorded(env))
|
|
916
1160
|
return;
|
|
917
1161
|
try {
|
|
918
1162
|
const path = usagePath(env);
|
|
919
|
-
mkdirSync(
|
|
1163
|
+
mkdirSync(dirname2(path), { recursive: true, mode: 448 });
|
|
920
1164
|
appendFileSync(path, `${JSON.stringify(entry)}
|
|
921
1165
|
`, { mode: 384 });
|
|
922
1166
|
} catch {}
|
|
@@ -929,8 +1173,8 @@ var LEVEL_OPTIONS = {
|
|
|
929
1173
|
};
|
|
930
1174
|
var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
|
|
931
1175
|
function printError(prefix, message, hint, cause) {
|
|
932
|
-
const colors = paint(
|
|
933
|
-
const styled = takesControlCodes(
|
|
1176
|
+
const colors = paint(process14.stderr);
|
|
1177
|
+
const styled = takesControlCodes(process14.stderr);
|
|
934
1178
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
935
1179
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
936
1180
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -1013,17 +1257,17 @@ async function printCandidates(found) {
|
|
|
1013
1257
|
return 0;
|
|
1014
1258
|
}
|
|
1015
1259
|
function packageVersion(entry = Bun.main) {
|
|
1016
|
-
for (let dir =
|
|
1260
|
+
for (let dir = dirname3(realpathSync(entry));; dir = dirname3(dir)) {
|
|
1017
1261
|
try {
|
|
1018
|
-
const { version } = JSON.parse(
|
|
1262
|
+
const { version } = JSON.parse(readFileSync2(join6(dir, "package.json"), "utf8"));
|
|
1019
1263
|
if (typeof version === "string")
|
|
1020
1264
|
return version;
|
|
1021
1265
|
} catch {}
|
|
1022
|
-
if (
|
|
1266
|
+
if (dirname3(dir) === dir)
|
|
1023
1267
|
return "0.0.0";
|
|
1024
1268
|
}
|
|
1025
1269
|
}
|
|
1026
|
-
async function runMain({ name, version, run, redact, usage }, argv =
|
|
1270
|
+
async function runMain({ name, version, run, redact, usage }, argv = process14.argv.slice(2)) {
|
|
1027
1271
|
if (argv[0] === COMPLETE)
|
|
1028
1272
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
1029
1273
|
if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
|
|
@@ -1050,88 +1294,670 @@ var {Glob } = globalThis.Bun;
|
|
|
1050
1294
|
var SESSION_FILES = new Glob("*.json");
|
|
1051
1295
|
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
1052
1296
|
// package.json
|
|
1053
|
-
var version = "0.
|
|
1297
|
+
var version = "0.15.0";
|
|
1054
1298
|
|
|
1055
1299
|
// src/j.ts
|
|
1056
|
-
import
|
|
1300
|
+
import process21 from "process";
|
|
1057
1301
|
|
|
1058
|
-
// ../kit/src/
|
|
1059
|
-
import { existsSync, mkdirSync as mkdirSync2, readFileSync as
|
|
1060
|
-
import { homedir as
|
|
1061
|
-
import { dirname as
|
|
1062
|
-
import
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1302
|
+
// ../kit/src/picker.ts
|
|
1303
|
+
import { existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync } from "fs";
|
|
1304
|
+
import { homedir as homedir5 } from "os";
|
|
1305
|
+
import { dirname as dirname4, join as join7 } from "path";
|
|
1306
|
+
import process18 from "process";
|
|
1307
|
+
|
|
1308
|
+
// ../kit/src/picker/score.ts
|
|
1309
|
+
var MATCH = 16;
|
|
1310
|
+
var CONSECUTIVE = 12;
|
|
1311
|
+
var BOUNDARY = 10;
|
|
1312
|
+
var PREFIX = 8;
|
|
1313
|
+
var GAP = 1;
|
|
1314
|
+
var BOUNDARY_AFTER = new Set([" ", "/", "-", "_", ".", ":", "\\", "(", "[", ",", "\t"]);
|
|
1315
|
+
var UPPER = /\p{Lu}/u;
|
|
1316
|
+
var LOWER = /\p{Ll}/u;
|
|
1317
|
+
var NONE = Number.NEGATIVE_INFINITY;
|
|
1318
|
+
function startsWord(text, index) {
|
|
1319
|
+
if (index === 0)
|
|
1320
|
+
return true;
|
|
1321
|
+
const before = text[index - 1];
|
|
1322
|
+
return BOUNDARY_AFTER.has(before) || LOWER.test(before) && UPPER.test(text[index]);
|
|
1323
|
+
}
|
|
1324
|
+
function fuzzy(query, text) {
|
|
1325
|
+
if (query === "")
|
|
1326
|
+
return { score: 0, positions: [] };
|
|
1327
|
+
const exact = query !== query.toLowerCase();
|
|
1328
|
+
const needle = exact ? query : query.toLowerCase();
|
|
1329
|
+
const haystack = exact ? text : text.toLowerCase();
|
|
1330
|
+
if (haystack.length !== text.length || needle.length > haystack.length)
|
|
1331
|
+
return;
|
|
1332
|
+
const width = haystack.length;
|
|
1333
|
+
const best = Array.from({ length: needle.length }, () => new Array(width).fill(NONE));
|
|
1334
|
+
const from = Array.from({ length: needle.length }, () => new Array(width).fill(-1));
|
|
1335
|
+
for (let j = 0;j < needle.length; j++) {
|
|
1336
|
+
let run = NONE;
|
|
1337
|
+
let runAt = -1;
|
|
1338
|
+
for (let i = j;i < width; i++) {
|
|
1339
|
+
if (j > 0 && i - 2 >= j - 1) {
|
|
1340
|
+
const candidate = best[j - 1][i - 2] + (i - 2) * GAP;
|
|
1341
|
+
if (candidate > run) {
|
|
1342
|
+
run = candidate;
|
|
1343
|
+
runAt = i - 2;
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
if (haystack[i] !== needle[j])
|
|
1347
|
+
continue;
|
|
1348
|
+
const here = MATCH + (startsWord(text, i) ? BOUNDARY : 0) + (i === 0 ? PREFIX : 0);
|
|
1349
|
+
if (j === 0) {
|
|
1350
|
+
best[0][i] = here - i * GAP;
|
|
1351
|
+
continue;
|
|
1352
|
+
}
|
|
1353
|
+
const joined = best[j - 1][i - 1] + CONSECUTIVE;
|
|
1354
|
+
const skipped = run - (i - 1) * GAP;
|
|
1355
|
+
if (joined > NONE && joined >= skipped) {
|
|
1356
|
+
best[j][i] = joined + here;
|
|
1357
|
+
from[j][i] = i - 1;
|
|
1358
|
+
} else if (skipped > NONE) {
|
|
1359
|
+
best[j][i] = skipped + here;
|
|
1360
|
+
from[j][i] = runAt;
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
const last = best[needle.length - 1];
|
|
1365
|
+
let end = -1;
|
|
1366
|
+
for (let i = 0;i < width; i++) {
|
|
1367
|
+
if (last[i] > NONE && (end === -1 || last[i] > last[end]))
|
|
1368
|
+
end = i;
|
|
1369
|
+
}
|
|
1370
|
+
if (end === -1)
|
|
1371
|
+
return;
|
|
1372
|
+
const positions = [];
|
|
1373
|
+
for (let j = needle.length - 1, i = end;j >= 0; j--) {
|
|
1374
|
+
positions.unshift(i);
|
|
1375
|
+
i = from[j][i];
|
|
1376
|
+
}
|
|
1377
|
+
return { score: last[end], positions };
|
|
1378
|
+
}
|
|
1379
|
+
function rank(query, lines) {
|
|
1380
|
+
if (query === "")
|
|
1381
|
+
return lines.map((_, index) => ({ index, positions: [] }));
|
|
1382
|
+
const found = [];
|
|
1383
|
+
for (const [index, line] of lines.entries()) {
|
|
1384
|
+
const match = fuzzy(query, line);
|
|
1385
|
+
if (match !== undefined)
|
|
1386
|
+
found.push({ index, positions: match.positions, score: match.score });
|
|
1387
|
+
}
|
|
1388
|
+
return found.sort((a, b) => b.score - a.score || lines[a.index].length - lines[b.index].length || a.index - b.index);
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1391
|
+
// ../kit/src/picker/state.ts
|
|
1392
|
+
var ANSI2 = /\x1B(?:\[[\x30-\x3F]*[\x20-\x2F]*[\x40-\x7E]|\][^\x07\x1B]*(?:\x07|\x1B\\)|[\x40-\x5A\x5C-\x5F])/g;
|
|
1393
|
+
var CONTROL2 = /[\x00-\x1F\x7F-\x9F]/g;
|
|
1394
|
+
var IS_CONTROL = /[\x00-\x1F\x7F-\x9F]/;
|
|
1395
|
+
var SGR = /^\x1B\[[0-9;]*m$/;
|
|
1396
|
+
var RESET = /^\x1B\[0?m$/;
|
|
1397
|
+
var COLUMN_GAP = 2;
|
|
1398
|
+
var PASTE_START = "\x1B[200~";
|
|
1399
|
+
var PASTE_END = "\x1B[201~";
|
|
1400
|
+
var LAST_WORD = /\S*\s*$/;
|
|
1401
|
+
var KEY_SEQUENCES = /\x1B\[[0-9;]*[A-Z~]|\x1BO[A-Z]|\x1B[\s\S]|[\s\S]/gu;
|
|
1402
|
+
var KEYS = {
|
|
1403
|
+
"\x1B[A": "up",
|
|
1404
|
+
"\x1BOA": "up",
|
|
1405
|
+
"\x10": "up",
|
|
1406
|
+
"\x1B[B": "down",
|
|
1407
|
+
"\x1BOB": "down",
|
|
1408
|
+
"\x0E": "down",
|
|
1409
|
+
"\r": "enter",
|
|
1410
|
+
"\n": "enter",
|
|
1411
|
+
"\x03": "cancel",
|
|
1412
|
+
"\x1B": "cancel",
|
|
1413
|
+
"\x7F": "erase",
|
|
1414
|
+
"\b": "erase",
|
|
1415
|
+
"\x15": "clear",
|
|
1416
|
+
"\x17": "word",
|
|
1417
|
+
"\t": "tab"
|
|
1418
|
+
};
|
|
1419
|
+
var CTRL_KEY = /^ctrl-[abd-gkln-z]$/;
|
|
1420
|
+
function repeat(value, count) {
|
|
1421
|
+
return Array.from({ length: count }).fill(value);
|
|
1422
|
+
}
|
|
1423
|
+
function styled(line) {
|
|
1424
|
+
let text = "";
|
|
1425
|
+
const styles = [];
|
|
1426
|
+
let active = "";
|
|
1427
|
+
let colored = false;
|
|
1428
|
+
let last = 0;
|
|
1429
|
+
function add(chunk) {
|
|
1430
|
+
const flat = chunk.replace(CONTROL2, " ");
|
|
1431
|
+
text += flat;
|
|
1432
|
+
colored ||= active !== "" && flat !== "";
|
|
1433
|
+
styles.push(...repeat(active, flat.length));
|
|
1434
|
+
}
|
|
1435
|
+
for (const found of line.matchAll(ANSI2)) {
|
|
1436
|
+
add(line.slice(last, found.index));
|
|
1437
|
+
last = found.index + found[0].length;
|
|
1438
|
+
if (SGR.test(found[0]))
|
|
1439
|
+
active = RESET.test(found[0]) ? "" : active + found[0];
|
|
1440
|
+
}
|
|
1441
|
+
add(line.slice(last));
|
|
1442
|
+
return { text, styles: colored ? styles : undefined };
|
|
1443
|
+
}
|
|
1444
|
+
function lay(rows, searched) {
|
|
1445
|
+
const parsed = rows.map((row) => row.cells.map(styled));
|
|
1446
|
+
const widths = (parsed[0] ?? []).map((_, column) => Math.max(...parsed.map((cells) => Bun.stringWidth(cells[column]?.text ?? ""))));
|
|
1447
|
+
return parsed.map((cells) => {
|
|
1448
|
+
let text = "";
|
|
1449
|
+
const styles = [];
|
|
1450
|
+
let colored = false;
|
|
1451
|
+
let searchEnd = 0;
|
|
1452
|
+
for (const [column, cell] of cells.entries()) {
|
|
1453
|
+
const last = column === cells.length - 1;
|
|
1454
|
+
const shown = cells.length > 1 && last ? cell.text.trimEnd() : cell.text;
|
|
1455
|
+
text += shown;
|
|
1456
|
+
colored ||= cell.styles !== undefined;
|
|
1457
|
+
styles.push(...cell.styles?.slice(0, shown.length) ?? repeat("", shown.length));
|
|
1458
|
+
if (column < searched)
|
|
1459
|
+
searchEnd = cells.length > 1 ? text.trimEnd().length : text.length;
|
|
1460
|
+
if (!last) {
|
|
1461
|
+
const pad = widths[column] - Bun.stringWidth(cell.text) + COLUMN_GAP;
|
|
1462
|
+
text += " ".repeat(pad);
|
|
1463
|
+
styles.push(...repeat("", pad));
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
return { text, styles: colored ? styles : undefined, searchable: text.slice(0, searchEnd) };
|
|
1101
1467
|
});
|
|
1102
|
-
return picked(proc.stdout.toString(), hidden, options);
|
|
1103
1468
|
}
|
|
1104
|
-
function
|
|
1469
|
+
function parseKeys(input, paste = { active: false }, ctrls = []) {
|
|
1470
|
+
const codes = new Map(ctrls.map((name) => [String.fromCharCode(name.codePointAt(name.length - 1) - 96), name]));
|
|
1471
|
+
return (input.match(KEY_SEQUENCES) ?? []).flatMap((sequence) => {
|
|
1472
|
+
const named = codes.get(sequence);
|
|
1473
|
+
const key = named === undefined ? KEYS[sequence] : { ctrl: named };
|
|
1474
|
+
if (sequence === PASTE_START || sequence === PASTE_END) {
|
|
1475
|
+
paste.active = sequence === PASTE_START;
|
|
1476
|
+
return [];
|
|
1477
|
+
}
|
|
1478
|
+
if (paste.active && (sequence === "\r" || sequence === `
|
|
1479
|
+
`))
|
|
1480
|
+
return [];
|
|
1481
|
+
if (key !== undefined)
|
|
1482
|
+
return [key];
|
|
1483
|
+
return IS_CONTROL.test(sequence) || sequence.startsWith("\x1B") ? [] : [{ text: sequence }];
|
|
1484
|
+
});
|
|
1485
|
+
}
|
|
1486
|
+
function createState(rows, { multi = false, prompt = "> ", query = "", header = "", expect = [], searched = Number.POSITIVE_INFINITY } = {}) {
|
|
1487
|
+
const state = { rows: [], items: [], styles: [], searchable: [], searched, header, expect, key: "", status: "", query, matches: [], cursor: 0, marked: new Set, multi, prompt, done: undefined };
|
|
1488
|
+
return withRows(state, rows);
|
|
1489
|
+
}
|
|
1490
|
+
function withRows(state, rows, header = state.header) {
|
|
1491
|
+
const cells = rows.map((row) => typeof row === "string" ? { hidden: [], cells: [row] } : row);
|
|
1492
|
+
const laid = lay(cells, state.searched);
|
|
1493
|
+
const searchable = laid.map((row) => row.searchable);
|
|
1494
|
+
const matches = rank(state.query, searchable);
|
|
1495
|
+
return { ...state, rows: cells, items: laid.map((row) => row.text), styles: laid.map((row) => row.styles), searchable, header, matches, cursor: Math.max(0, Math.min(matches.length - 1, state.cursor)), marked: new Set };
|
|
1496
|
+
}
|
|
1497
|
+
function withQuery(state, query) {
|
|
1498
|
+
return { ...state, query, matches: rank(query, state.searchable), cursor: 0 };
|
|
1499
|
+
}
|
|
1500
|
+
function move(state, by) {
|
|
1501
|
+
return { ...state, cursor: Math.max(0, Math.min(state.matches.length - 1, state.cursor + by)) };
|
|
1502
|
+
}
|
|
1503
|
+
function highlighted(state) {
|
|
1504
|
+
return state.matches[state.cursor]?.index;
|
|
1505
|
+
}
|
|
1506
|
+
function press(state, key) {
|
|
1507
|
+
if (state.done !== undefined)
|
|
1508
|
+
return state;
|
|
1509
|
+
if (typeof key === "object") {
|
|
1510
|
+
if ("text" in key)
|
|
1511
|
+
return withQuery(state, state.query + key.text);
|
|
1512
|
+
return state.expect.includes(key.ctrl) ? { ...state, done: "accept", key: key.ctrl } : state;
|
|
1513
|
+
}
|
|
1514
|
+
switch (key) {
|
|
1515
|
+
case "up":
|
|
1516
|
+
return move(state, -1);
|
|
1517
|
+
case "down":
|
|
1518
|
+
return move(state, 1);
|
|
1519
|
+
case "cancel":
|
|
1520
|
+
return { ...state, done: "cancel" };
|
|
1521
|
+
case "erase":
|
|
1522
|
+
return withQuery(state, [...state.query].slice(0, -1).join(""));
|
|
1523
|
+
case "clear":
|
|
1524
|
+
return withQuery(state, "");
|
|
1525
|
+
case "word":
|
|
1526
|
+
return withQuery(state, state.query.replace(LAST_WORD, ""));
|
|
1527
|
+
case "enter":
|
|
1528
|
+
return state.matches.length === 0 && state.marked.size === 0 ? state : { ...state, done: "accept" };
|
|
1529
|
+
case "tab": {
|
|
1530
|
+
const at = highlighted(state);
|
|
1531
|
+
if (!state.multi || at === undefined)
|
|
1532
|
+
return state;
|
|
1533
|
+
const marked = new Set(state.marked);
|
|
1534
|
+
if (!marked.delete(at))
|
|
1535
|
+
marked.add(at);
|
|
1536
|
+
return move({ ...state, marked }, 1);
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
function chosen(state) {
|
|
1541
|
+
if (state.marked.size > 0)
|
|
1542
|
+
return state.rows.filter((_, index) => state.marked.has(index));
|
|
1543
|
+
const at = highlighted(state);
|
|
1544
|
+
return at === undefined ? [] : [state.rows[at]];
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1547
|
+
// ../kit/src/picker/terminal.ts
|
|
1548
|
+
import { openSync as openSync2 } from "fs";
|
|
1549
|
+
import process17 from "process";
|
|
1550
|
+
import tty from "tty";
|
|
1551
|
+
|
|
1552
|
+
// ../kit/src/picker/preview.ts
|
|
1553
|
+
import process16 from "process";
|
|
1554
|
+
|
|
1555
|
+
// ../kit/src/shell.ts
|
|
1556
|
+
var SEPARATORS = new Set([`
|
|
1557
|
+
`, ";", "|"]);
|
|
1558
|
+
var PLAIN_BREAKERS = new Set(["<", ">", "(", ")", "&", "|"]);
|
|
1559
|
+
var GLOB_CHARS = new Set(["*", "?", "["]);
|
|
1560
|
+
var COMMENT_PREFIXES = new Set([";", "&", "|", "(", ")"]);
|
|
1561
|
+
var SAFE_WORD = /^[\w@%+=:,./-]+$/;
|
|
1562
|
+
var DOUBLE_QUOTED_ESCAPES = new Set(["$", "`", '"', "\\"]);
|
|
1563
|
+
var KEYWORDS = new Set(["then", "do", "else", "if", "elif", "while", "until", "!", "{", "}"]);
|
|
1564
|
+
var SHELLS = new Set(["sh", "bash", "zsh", "dash", "ash", "ksh", "fish", "csh", "tcsh", "nu", "xonsh", "elvish", "pwsh", "powershell"]);
|
|
1565
|
+
var SCANNED_PROGRAMS = new Set(["su", "runuser", "flock"]);
|
|
1566
|
+
var COMMAND_STRING_PROGRAMS = new Set([...SHELLS, ...SCANNED_PROGRAMS]);
|
|
1567
|
+
var POWERSHELL = new Set(["pwsh", "powershell"]);
|
|
1568
|
+
var SHELL_VALUE_OPTIONS = new Set(["-o", "-O", "--rcfile", "--init-file"]);
|
|
1569
|
+
var FISH_VALUE_OPTIONS = new Set(["-C", "--init-command"]);
|
|
1570
|
+
var PRINTERS = new Set(["echo", "printf"]);
|
|
1571
|
+
var FIND_EXEC = new Set(["-exec", "-execdir", "-ok", "-okdir"]);
|
|
1572
|
+
function wrapper(short = "", long = [], operands = 0, runsString = false) {
|
|
1573
|
+
return { short, long, operands, runsString };
|
|
1574
|
+
}
|
|
1575
|
+
var WRAPPERS = new Map([
|
|
1576
|
+
["command", wrapper()],
|
|
1577
|
+
["builtin", wrapper()],
|
|
1578
|
+
["nohup", wrapper()],
|
|
1579
|
+
["setsid", wrapper()],
|
|
1580
|
+
["busybox", wrapper()],
|
|
1581
|
+
["exec", wrapper("a")],
|
|
1582
|
+
["time", wrapper("fo", ["--format", "--output"])],
|
|
1583
|
+
["env", wrapper("uCS", ["--unset", "--chdir", "--split-string", "--argv0"])],
|
|
1584
|
+
["sudo", wrapper("ughpCTrtUDR", ["--user", "--group", "--host", "--prompt", "--close-from", "--command-timeout", "--role", "--type", "--other-user", "--chdir", "--chroot"])],
|
|
1585
|
+
["doas", wrapper("uC")],
|
|
1586
|
+
["timeout", wrapper("sk", ["--signal", "--kill-after"], 1)],
|
|
1587
|
+
["nice", wrapper("n", ["--adjustment"])],
|
|
1588
|
+
["ionice", wrapper("cnpPu", ["--class", "--classdata", "--pid", "--pgid", "--uid"])],
|
|
1589
|
+
["stdbuf", wrapper("ioe", ["--input", "--output", "--error"])],
|
|
1590
|
+
["chroot", wrapper("", ["--userspec", "--groups"], 1)],
|
|
1591
|
+
["watch", wrapper("n", ["--interval"], 0, true)],
|
|
1592
|
+
["flock", wrapper("wE", ["--timeout", "--conflict-exit-code"], 1)],
|
|
1593
|
+
["parallel", wrapper("jnNSaIPlLdTsC", ["--jobs", "--max-args", "--max-replace-args", "--sshlogin", "--arg-file", "--delimiter", "--colsep"], 0, true)],
|
|
1594
|
+
["unbuffer", wrapper()],
|
|
1595
|
+
["systemd-run", wrapper("puEMH", ["--property", "--unit", "--description", "--slice", "--setenv", "--machine", "--host", "--uid", "--gid", "--working-directory", "--nice", "--service-type"])],
|
|
1596
|
+
["xargs", wrapper("IndsEaPL", ["--max-args", "--max-procs", "--max-chars", "--delimiter", "--arg-file", "--eof", "--max-lines", "--process-slot-var"])]
|
|
1597
|
+
]);
|
|
1598
|
+
function shellQuote(word) {
|
|
1599
|
+
return SAFE_WORD.test(word) && !word.startsWith("=") ? word : `'${word.replaceAll("'", "'\\''")}'`;
|
|
1600
|
+
}
|
|
1601
|
+
|
|
1602
|
+
// ../kit/src/picker/preview.ts
|
|
1603
|
+
var PREVIEW_BYTES = 64 * 1024;
|
|
1604
|
+
var PREVIEW_LINES = 200;
|
|
1605
|
+
var PREVIEW_TIMEOUT_MS = 3000;
|
|
1606
|
+
var PLACEHOLDER = /\{([1-9]\d*)?\}/g;
|
|
1607
|
+
function previewCommand(template, line, hidden = []) {
|
|
1608
|
+
return template.replace(PLACEHOLDER, (_, field) => shellQuote(field === undefined ? line : hidden[Number(field) - 1] ?? ""));
|
|
1609
|
+
}
|
|
1610
|
+
function previewLines(output) {
|
|
1105
1611
|
const lines = output.split(`
|
|
1106
1612
|
`);
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1613
|
+
if (lines.at(-1) === "")
|
|
1614
|
+
lines.pop();
|
|
1615
|
+
return lines.slice(0, PREVIEW_LINES);
|
|
1616
|
+
}
|
|
1617
|
+
async function runPreview(template, line, signal, hidden = []) {
|
|
1618
|
+
const proc = Bun.spawn(["sh", "-c", `exec 2>&1
|
|
1619
|
+
${previewCommand(template, line, hidden)}`], { stdin: "ignore", stdout: "pipe", stderr: "ignore", detached: true });
|
|
1620
|
+
const stop = () => {
|
|
1621
|
+
try {
|
|
1622
|
+
process16.kill(-proc.pid, "SIGKILL");
|
|
1623
|
+
} catch {
|
|
1624
|
+
proc.kill();
|
|
1625
|
+
}
|
|
1626
|
+
};
|
|
1627
|
+
const timer = setTimeout(stop, PREVIEW_TIMEOUT_MS);
|
|
1628
|
+
const chunks = [];
|
|
1629
|
+
let size = 0;
|
|
1630
|
+
signal.addEventListener("abort", stop);
|
|
1631
|
+
try {
|
|
1632
|
+
for await (const chunk of proc.stdout) {
|
|
1633
|
+
chunks.push(chunk);
|
|
1634
|
+
size += chunk.length;
|
|
1635
|
+
if (size >= PREVIEW_BYTES) {
|
|
1636
|
+
stop();
|
|
1637
|
+
break;
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
} finally {
|
|
1641
|
+
clearTimeout(timer);
|
|
1642
|
+
signal.removeEventListener("abort", stop);
|
|
1643
|
+
}
|
|
1644
|
+
return signal.aborted ? [] : previewLines(Buffer.concat(chunks).subarray(0, PREVIEW_BYTES).toString());
|
|
1645
|
+
}
|
|
1646
|
+
|
|
1647
|
+
// ../kit/src/picker/view.ts
|
|
1648
|
+
var MIN_HEIGHT = 4;
|
|
1649
|
+
var PREVIEW_MIN_COLUMNS = 80;
|
|
1650
|
+
var PREVIEW_MIN_ROWS = 10;
|
|
1651
|
+
var PREVIEW_SHARE = 0.4;
|
|
1652
|
+
var RESET2 = "\x1B[0m";
|
|
1653
|
+
var SEPARATOR = " \u2502 ";
|
|
1654
|
+
var HEIGHT = /^(\d+)(%?)$/;
|
|
1655
|
+
function parseHeight(text) {
|
|
1656
|
+
const found = HEIGHT.exec(text);
|
|
1657
|
+
if (found === null || Number(found[1]) < 1)
|
|
1658
|
+
return;
|
|
1659
|
+
return found[2] === "%" ? { percent: Math.min(100, Number(found[1])) } : { lines: Number(found[1]) };
|
|
1660
|
+
}
|
|
1661
|
+
function layoutFor(size, height, previewing, position = "right") {
|
|
1662
|
+
const wanted = "lines" in height ? height.lines : Math.round(size.rows * height.percent / 100);
|
|
1663
|
+
const rows = Math.min(size.rows, Math.max(MIN_HEIGHT, wanted));
|
|
1664
|
+
if (position === "down") {
|
|
1665
|
+
const previewHeight = previewing && rows >= PREVIEW_MIN_ROWS ? Math.round(rows * PREVIEW_SHARE) : 0;
|
|
1666
|
+
return { height: rows, listHeight: rows - previewHeight, listWidth: size.columns, previewWidth: 0, previewHeight };
|
|
1667
|
+
}
|
|
1668
|
+
const split = previewing && size.columns >= PREVIEW_MIN_COLUMNS;
|
|
1669
|
+
const listWidth = split ? Math.floor(size.columns / 2) : size.columns;
|
|
1670
|
+
return { height: rows, listHeight: rows, listWidth, previewWidth: split ? size.columns - listWidth - SEPARATOR.length : 0, previewHeight: 0 };
|
|
1671
|
+
}
|
|
1672
|
+
function fit(text, width, plain, mark = plain, marked = new Set, styles) {
|
|
1673
|
+
const room = Bun.stringWidth(text) > width ? width - 1 : width;
|
|
1674
|
+
let out = "";
|
|
1675
|
+
let used = 0;
|
|
1676
|
+
let at = 0;
|
|
1677
|
+
for (const char of text) {
|
|
1678
|
+
const size = Bun.stringWidth(char);
|
|
1679
|
+
if (used + size > room)
|
|
1680
|
+
break;
|
|
1681
|
+
const style = marked.has(at) ? "" : styles?.[at] ?? "";
|
|
1682
|
+
out += style === "" ? (marked.has(at) ? mark : plain)(char) : `${style}${char}${RESET2}`;
|
|
1683
|
+
used += size;
|
|
1684
|
+
at += char.length;
|
|
1685
|
+
}
|
|
1686
|
+
if (room < width) {
|
|
1687
|
+
out += plain("\u2026");
|
|
1688
|
+
used += 1;
|
|
1689
|
+
}
|
|
1690
|
+
return { text: out, width: used };
|
|
1691
|
+
}
|
|
1692
|
+
var identity = (text) => text;
|
|
1693
|
+
function fitStyled(line, width, plain = identity) {
|
|
1694
|
+
const { text, styles } = styled(line);
|
|
1695
|
+
return fit(text, width, plain, plain, new Set, styles);
|
|
1696
|
+
}
|
|
1697
|
+
function caretColumn(state, layout) {
|
|
1698
|
+
return Math.min(layout.listWidth, Bun.stringWidth(state.prompt) + Bun.stringWidth(state.query) + 1);
|
|
1699
|
+
}
|
|
1700
|
+
function listLines(state, layout, theme) {
|
|
1701
|
+
const header = state.header === "" ? [] : state.header.split(`
|
|
1702
|
+
`).map((line) => fitStyled(line, layout.listWidth, theme.label));
|
|
1703
|
+
const rows = Math.max(1, layout.listHeight - 2 - header.length);
|
|
1704
|
+
const prefixWidth = state.multi ? 4 : 2;
|
|
1705
|
+
const top = Math.max(0, Math.min(state.cursor - Math.floor(rows / 2), state.matches.length - rows));
|
|
1706
|
+
const marks = (state.marked.size > 0 ? ` (${state.marked.size} marked)` : "") + (state.status === "" ? "" : ` ${state.status}`);
|
|
1707
|
+
const promptWidth = Bun.stringWidth(state.prompt);
|
|
1708
|
+
const query = fit(state.query, Math.max(1, layout.listWidth - promptWidth), identity);
|
|
1709
|
+
const count = fit(` ${state.matches.length}/${state.items.length}${marks}`, layout.listWidth, theme.label);
|
|
1710
|
+
const lines = [{ text: theme.accent(state.prompt) + query.text, width: promptWidth + query.width }, count, ...header];
|
|
1711
|
+
if (state.matches.length === 0)
|
|
1712
|
+
lines.push(fit(" no match", layout.listWidth, theme.label));
|
|
1713
|
+
for (const [offset, match] of state.matches.slice(top, top + rows).entries()) {
|
|
1714
|
+
const current = top + offset === state.cursor;
|
|
1715
|
+
const pointer = current ? theme.accent("\u276F") : " ";
|
|
1716
|
+
const mark = state.multi ? `${state.marked.has(match.index) ? theme.id("\u25CF") : " "} ` : "";
|
|
1717
|
+
const text = fit(state.items[match.index], layout.listWidth - prefixWidth, current ? theme.value : identity, theme.accent, new Set(match.positions), state.styles[match.index]);
|
|
1718
|
+
lines.push({ text: `${pointer} ${mark}${text.text}`, width: prefixWidth + text.width });
|
|
1719
|
+
}
|
|
1720
|
+
return lines;
|
|
1721
|
+
}
|
|
1722
|
+
function render(state, layout, theme, preview = []) {
|
|
1723
|
+
const left = listLines(state, layout, theme);
|
|
1724
|
+
return Array.from({ length: layout.height }, (_, row) => {
|
|
1725
|
+
if (row >= layout.listHeight)
|
|
1726
|
+
return row === layout.listHeight ? theme.label("\u2500".repeat(layout.listWidth)) : fitStyled(preview[row - layout.listHeight - 1] ?? "", layout.listWidth).text;
|
|
1727
|
+
const list = left[row] ?? { text: "", width: 0 };
|
|
1728
|
+
if (layout.previewWidth === 0)
|
|
1729
|
+
return list.text;
|
|
1730
|
+
return `${list.text}${" ".repeat(Math.max(0, layout.listWidth - list.width))}${theme.label(SEPARATOR)}${fitStyled(preview[row] ?? "", layout.previewWidth).text}`;
|
|
1731
|
+
});
|
|
1732
|
+
}
|
|
1733
|
+
|
|
1734
|
+
// ../kit/src/picker/terminal.ts
|
|
1735
|
+
var PREVIEW_DELAY_MS = 120;
|
|
1736
|
+
var OPEN = { ctrl: "open" };
|
|
1737
|
+
var HIDE_CURSOR = "\x1B[?25l";
|
|
1738
|
+
var SHOW_CURSOR = "\x1B[?25h";
|
|
1739
|
+
var PASTE_ON = "\x1B[?2004h";
|
|
1740
|
+
var PASTE_OFF = "\x1B[?2004l";
|
|
1741
|
+
var CLEAR_BELOW = "\r\x1B[J";
|
|
1742
|
+
var SIGNALS2 = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
1743
|
+
function up(lines) {
|
|
1744
|
+
return lines > 0 ? `\x1B[${lines}A` : "";
|
|
1745
|
+
}
|
|
1746
|
+
function openTerminal() {
|
|
1747
|
+
let input;
|
|
1748
|
+
try {
|
|
1749
|
+
input = new tty.ReadStream(openSync2("/dev/tty", "r"));
|
|
1750
|
+
} catch {
|
|
1751
|
+
return fail("no terminal to read keys from (/dev/tty)");
|
|
1752
|
+
}
|
|
1753
|
+
const out = process17.stderr;
|
|
1754
|
+
let resize;
|
|
1755
|
+
return {
|
|
1756
|
+
get columns() {
|
|
1757
|
+
return out.columns || 80;
|
|
1758
|
+
},
|
|
1759
|
+
get rows() {
|
|
1760
|
+
return out.rows || 24;
|
|
1761
|
+
},
|
|
1762
|
+
theme: { accent: role("accent", out), label: role("label", out), value: role("value", out), id: role("id", out) },
|
|
1763
|
+
write: (text) => void out.write(text),
|
|
1764
|
+
listen(onChunk, onResize) {
|
|
1765
|
+
resize = onResize;
|
|
1766
|
+
input.setRawMode(true);
|
|
1767
|
+
out.on("resize", onResize);
|
|
1768
|
+
input.on("data", (chunk) => onChunk(chunk.toString()));
|
|
1769
|
+
},
|
|
1770
|
+
close() {
|
|
1771
|
+
if (resize !== undefined)
|
|
1772
|
+
out.off("resize", resize);
|
|
1773
|
+
try {
|
|
1774
|
+
input.setRawMode(false);
|
|
1775
|
+
} finally {
|
|
1776
|
+
input.destroy();
|
|
1777
|
+
}
|
|
1778
|
+
}
|
|
1779
|
+
};
|
|
1780
|
+
}
|
|
1781
|
+
async function select(rows, settings, terminal = openTerminal()) {
|
|
1782
|
+
const { theme } = terminal;
|
|
1783
|
+
let state = createState(rows, settings);
|
|
1784
|
+
let preview = [];
|
|
1785
|
+
let reserved = 0;
|
|
1786
|
+
let previewTimer;
|
|
1787
|
+
let previewing = new AbortController;
|
|
1788
|
+
let shownLine;
|
|
1789
|
+
let finish;
|
|
1790
|
+
let restored = false;
|
|
1791
|
+
function draw(clear = false) {
|
|
1792
|
+
if (restored)
|
|
1793
|
+
return;
|
|
1794
|
+
const layout = layoutFor({ columns: terminal.columns, rows: terminal.rows }, settings.height, settings.preview !== undefined, settings.previewPosition);
|
|
1795
|
+
let frame = HIDE_CURSOR;
|
|
1796
|
+
if (reserved !== layout.height || clear) {
|
|
1797
|
+
frame += CLEAR_BELOW + `
|
|
1798
|
+
`.repeat(layout.height - 1) + up(layout.height - 1);
|
|
1799
|
+
reserved = layout.height;
|
|
1800
|
+
}
|
|
1801
|
+
frame += `\r${render(state, layout, theme, preview).map((line) => `\x1B[2K${line}`).join(`
|
|
1802
|
+
`)}${up(layout.height - 1)}\r\x1B[${caretColumn(state, layout)}G${SHOW_CURSOR}`;
|
|
1803
|
+
terminal.write(frame);
|
|
1804
|
+
}
|
|
1805
|
+
function refreshPreview() {
|
|
1806
|
+
const at = highlighted(state);
|
|
1807
|
+
if (settings.preview === undefined || at === shownLine)
|
|
1808
|
+
return;
|
|
1809
|
+
shownLine = at;
|
|
1810
|
+
clearTimeout(previewTimer);
|
|
1811
|
+
previewing.abort();
|
|
1812
|
+
previewing = new AbortController;
|
|
1813
|
+
if (at === undefined) {
|
|
1814
|
+
preview = [];
|
|
1815
|
+
return;
|
|
1816
|
+
}
|
|
1817
|
+
const { signal } = previewing;
|
|
1818
|
+
const template = settings.preview;
|
|
1819
|
+
const row = state.rows[at];
|
|
1820
|
+
async function show() {
|
|
1821
|
+
const shown = typeof template === "string" ? await runPreview(template, row.cells.join(" "), signal, row.hidden) : await previewOf(template, row.hidden, signal);
|
|
1822
|
+
if (!signal.aborted) {
|
|
1823
|
+
preview = shown;
|
|
1824
|
+
draw();
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
previewTimer = setTimeout(() => void show(), PREVIEW_DELAY_MS);
|
|
1828
|
+
}
|
|
1829
|
+
function restore() {
|
|
1830
|
+
if (restored)
|
|
1831
|
+
return;
|
|
1832
|
+
restored = true;
|
|
1833
|
+
clearTimeout(previewTimer);
|
|
1834
|
+
previewing.abort();
|
|
1835
|
+
terminal.write(`${CLEAR_BELOW}${PASTE_OFF}${SHOW_CURSOR}`);
|
|
1836
|
+
terminal.close();
|
|
1837
|
+
}
|
|
1838
|
+
const stop = () => {
|
|
1839
|
+
state = { ...state, done: "cancel" };
|
|
1840
|
+
finish?.();
|
|
1841
|
+
};
|
|
1842
|
+
const paste = { active: false };
|
|
1843
|
+
const { reload } = settings;
|
|
1844
|
+
const pending = [];
|
|
1845
|
+
let busy = false;
|
|
1846
|
+
let draining = false;
|
|
1847
|
+
async function reloadRows() {
|
|
1848
|
+
if (reload === undefined)
|
|
1849
|
+
return;
|
|
1850
|
+
busy = true;
|
|
1851
|
+
state = { ...state, status: "reloading" };
|
|
1852
|
+
draw();
|
|
1853
|
+
try {
|
|
1854
|
+
const next = await reload.run();
|
|
1855
|
+
state = { ...withRows(state, next.rows, next.header), status: "" };
|
|
1856
|
+
shownLine = undefined;
|
|
1857
|
+
} catch (error) {
|
|
1858
|
+
state = { ...state, status: `reload failed: ${error instanceof Error ? error.message : String(error)}` };
|
|
1859
|
+
} finally {
|
|
1860
|
+
busy = false;
|
|
1861
|
+
}
|
|
1862
|
+
}
|
|
1863
|
+
async function drain() {
|
|
1864
|
+
if (draining)
|
|
1865
|
+
return;
|
|
1866
|
+
draining = true;
|
|
1867
|
+
try {
|
|
1868
|
+
for (let key = pending.shift();key !== undefined && state.done === undefined; key = pending.shift()) {
|
|
1869
|
+
if (key === OPEN || typeof key === "object" && "ctrl" in key && key.ctrl === reload?.key)
|
|
1870
|
+
await reloadRows();
|
|
1871
|
+
else
|
|
1872
|
+
state = press(state, key);
|
|
1873
|
+
}
|
|
1874
|
+
} finally {
|
|
1875
|
+
draining = false;
|
|
1876
|
+
}
|
|
1877
|
+
if (state.done !== undefined) {
|
|
1878
|
+
finish?.();
|
|
1879
|
+
return;
|
|
1880
|
+
}
|
|
1881
|
+
refreshPreview();
|
|
1882
|
+
draw();
|
|
1883
|
+
}
|
|
1884
|
+
function onChunk(chunk) {
|
|
1885
|
+
const keys = parseKeys(chunk, paste, [...settings.expect ?? [], ...reload?.key === undefined ? [] : [reload.key]]);
|
|
1886
|
+
if (busy && keys.includes("cancel")) {
|
|
1887
|
+
stop();
|
|
1888
|
+
return;
|
|
1889
|
+
}
|
|
1890
|
+
pending.push(...keys);
|
|
1891
|
+
drain();
|
|
1892
|
+
}
|
|
1893
|
+
const ended = new Promise((resolve) => {
|
|
1894
|
+
finish = resolve;
|
|
1895
|
+
});
|
|
1896
|
+
process17.once("exit", restore);
|
|
1897
|
+
for (const signal of SIGNALS2)
|
|
1898
|
+
process17.once(signal, stop);
|
|
1899
|
+
try {
|
|
1900
|
+
terminal.write(PASTE_ON);
|
|
1901
|
+
terminal.listen(onChunk, () => draw(true));
|
|
1902
|
+
refreshPreview();
|
|
1903
|
+
draw(true);
|
|
1904
|
+
if (reload?.onOpen === true) {
|
|
1905
|
+
pending.push(OPEN);
|
|
1906
|
+
drain();
|
|
1907
|
+
}
|
|
1908
|
+
await ended;
|
|
1909
|
+
} finally {
|
|
1910
|
+
for (const signal of SIGNALS2)
|
|
1911
|
+
process17.off(signal, stop);
|
|
1912
|
+
process17.off("exit", restore);
|
|
1913
|
+
restore();
|
|
1914
|
+
}
|
|
1915
|
+
return state.done === "accept" ? { rows: chosen(state), key: state.key, query: state.query } : undefined;
|
|
1916
|
+
}
|
|
1917
|
+
async function previewOf(preview, hidden, signal) {
|
|
1918
|
+
try {
|
|
1919
|
+
return previewLines(await preview(hidden, signal));
|
|
1920
|
+
} catch (error) {
|
|
1921
|
+
return [error instanceof Error ? error.message : String(error)];
|
|
1922
|
+
}
|
|
1923
|
+
}
|
|
1924
|
+
|
|
1925
|
+
// ../kit/src/picker.ts
|
|
1926
|
+
var REMEMBERED = 20;
|
|
1927
|
+
async function pick(rows, options) {
|
|
1928
|
+
const height = parseHeight(options.height ?? "100%");
|
|
1929
|
+
const keys = [...options.expect ?? [], ...options.reload?.key === undefined ? [] : [options.reload.key]];
|
|
1930
|
+
if (height === undefined)
|
|
1931
|
+
throw new Error(`pick: the height is lines or a percentage, not ${options.height}`);
|
|
1932
|
+
for (const key of keys) {
|
|
1933
|
+
if (!CTRL_KEY.test(key))
|
|
1934
|
+
throw new Error(`pick: ${key} is not a key it can report, one of ctrl-a to ctrl-z but ctrl-c, h, i, j and m`);
|
|
1935
|
+
}
|
|
1936
|
+
const found = await select(rows, { multi: false, prompt: options.prompt, query: "", height, preview: options.preview, previewPosition: "down", header: options.header, expect: options.expect, searched: options.searched, reload: options.reload });
|
|
1937
|
+
return found === undefined ? undefined : { key: found.key, hidden: found.rows[0]?.hidden ?? [], query: found.query };
|
|
1112
1938
|
}
|
|
1113
1939
|
function heightFor(count) {
|
|
1114
1940
|
return String(Math.max(15, Math.ceil((count + 4) / 0.6)));
|
|
1115
1941
|
}
|
|
1116
1942
|
function historyFile(name) {
|
|
1117
|
-
return
|
|
1943
|
+
return join7(process18.env.XDG_CACHE_HOME ?? join7(homedir5(), ".cache"), name, "history.json");
|
|
1118
1944
|
}
|
|
1119
1945
|
function history(name) {
|
|
1120
1946
|
try {
|
|
1121
1947
|
const file = historyFile(name);
|
|
1122
|
-
return
|
|
1948
|
+
return existsSync3(file) ? JSON.parse(readFileSync3(file, "utf-8")) : {};
|
|
1123
1949
|
} catch {
|
|
1124
1950
|
return {};
|
|
1125
1951
|
}
|
|
1126
1952
|
}
|
|
1127
1953
|
function runs(name) {
|
|
1128
|
-
return history(name)[
|
|
1954
|
+
return history(name)[process18.cwd()] ?? [];
|
|
1129
1955
|
}
|
|
1130
1956
|
function record(name, run) {
|
|
1131
1957
|
const all = history(name);
|
|
1132
1958
|
const file = historyFile(name);
|
|
1133
|
-
all[
|
|
1134
|
-
mkdirSync2(
|
|
1959
|
+
all[process18.cwd()] = [run, ...(all[process18.cwd()] ?? []).filter((earlier) => earlier.id !== run.id)].slice(0, REMEMBERED);
|
|
1960
|
+
mkdirSync2(dirname4(file), { recursive: true });
|
|
1135
1961
|
writeFileSync(file, JSON.stringify(all));
|
|
1136
1962
|
}
|
|
1137
1963
|
function byRecency(items, previous, idOf) {
|
|
@@ -1154,12 +1980,12 @@ function list(rows) {
|
|
|
1154
1980
|
}
|
|
1155
1981
|
|
|
1156
1982
|
// ../kit/src/prompt.ts
|
|
1157
|
-
import
|
|
1158
|
-
var
|
|
1159
|
-
var
|
|
1160
|
-
var
|
|
1983
|
+
import process19 from "process";
|
|
1984
|
+
var KEY_SEQUENCES2 = /\x1B\[[0-9;]*[A-Z~]|\x1BO[A-Z]|[\s\S]/giu;
|
|
1985
|
+
var CONTROL3 = /^[\x00-\x1F\x7F]/;
|
|
1986
|
+
var KEYS2 = { "\x1B[A": "up", "\x1BOA": "up", "\x1B[B": "down", "\x1BOB": "down", "\r": "enter", "\n": "enter", "\x03": "cancel", "\x1B": "cancel", "\x7F": "erase", "\b": "erase" };
|
|
1161
1987
|
function readKeys(input) {
|
|
1162
|
-
return (input.match(
|
|
1988
|
+
return (input.match(KEY_SEQUENCES2) ?? []).flatMap((sequence) => KEYS2[sequence] ?? (CONTROL3.test(sequence) ? [] : [{ text: sequence }]));
|
|
1163
1989
|
}
|
|
1164
1990
|
async function input2(question, { default: fallback, mask = false, validate, ...settings } = {}) {
|
|
1165
1991
|
let value = "";
|
|
@@ -1193,9 +2019,9 @@ async function input2(question, { default: fallback, mask = false, validate, ...
|
|
|
1193
2019
|
function answer(colors, question, state, picked) {
|
|
1194
2020
|
return state === "enter" ? `${colors.green("\u2713")} ${question} ${colors.cyan(picked)}` : `${colors.red("\u2717")} ${question} ${colors.dim("cancelled")}`;
|
|
1195
2021
|
}
|
|
1196
|
-
async function ask(question, { hint, terminal =
|
|
2022
|
+
async function ask(question, { hint, terminal = process19 }, render, press) {
|
|
1197
2023
|
const { stdin, stderr } = terminal;
|
|
1198
|
-
if (
|
|
2024
|
+
if (!isInteractive(terminal))
|
|
1199
2025
|
fail(`cannot ask "${question}" off a terminal`, 2, hint);
|
|
1200
2026
|
const colors = paint(stderr);
|
|
1201
2027
|
let drawn = 0;
|
|
@@ -1244,15 +2070,15 @@ async function ask(question, { hint, terminal = process13 }, render, press) {
|
|
|
1244
2070
|
}
|
|
1245
2071
|
|
|
1246
2072
|
// src/job-picker.ts
|
|
1247
|
-
import
|
|
2073
|
+
import process20 from "process";
|
|
1248
2074
|
|
|
1249
2075
|
// src/jobs.ts
|
|
1250
|
-
import { existsSync as
|
|
1251
|
-
import { dirname as
|
|
2076
|
+
import { existsSync as existsSync4, readdirSync, readFileSync as readFileSync4 } from "fs";
|
|
2077
|
+
import { dirname as dirname5, join as join9, resolve as resolvePath } from "path";
|
|
1252
2078
|
|
|
1253
2079
|
// src/steps.ts
|
|
1254
2080
|
import { readdir as readdir2 } from "fs/promises";
|
|
1255
|
-
import { join as
|
|
2081
|
+
import { join as join8 } from "path";
|
|
1256
2082
|
var STEP_NAMES = ["check", "typecheck", "lint", "test"];
|
|
1257
2083
|
var TARGET = /^@?([\w-]+)(?:[ \t][^:=\n]*)?:(?!=)/gm;
|
|
1258
2084
|
var JUSTFILES = ["justfile", "Justfile", ".justfile"];
|
|
@@ -1264,7 +2090,7 @@ var PHPUNIT_CONFIGS = ["phpunit.xml", "phpunit.xml.dist", "phpunit.dist.xml"];
|
|
|
1264
2090
|
var SKIPPED = new Set(["node_modules", "vendor", "dist", "build", "var", "tmp", "coverage"]);
|
|
1265
2091
|
async function readFirst(dir, files) {
|
|
1266
2092
|
for (const file of files) {
|
|
1267
|
-
const handle = Bun.file(
|
|
2093
|
+
const handle = Bun.file(join8(dir, file));
|
|
1268
2094
|
if (await handle.exists())
|
|
1269
2095
|
return { file, text: await handle.text() };
|
|
1270
2096
|
}
|
|
@@ -1272,7 +2098,7 @@ async function readFirst(dir, files) {
|
|
|
1272
2098
|
}
|
|
1273
2099
|
async function read(dir, ...files) {
|
|
1274
2100
|
for (const file of files) {
|
|
1275
|
-
const handle = Bun.file(
|
|
2101
|
+
const handle = Bun.file(join8(dir, file));
|
|
1276
2102
|
if (await handle.exists())
|
|
1277
2103
|
return handle.text();
|
|
1278
2104
|
}
|
|
@@ -1332,7 +2158,7 @@ async function candidates(dir, bin) {
|
|
|
1332
2158
|
}
|
|
1333
2159
|
async function firstFile(dir, names) {
|
|
1334
2160
|
for (const name of names) {
|
|
1335
|
-
if (await Bun.file(
|
|
2161
|
+
if (await Bun.file(join8(dir, name)).exists())
|
|
1336
2162
|
return name;
|
|
1337
2163
|
}
|
|
1338
2164
|
return;
|
|
@@ -1359,9 +2185,9 @@ async function discoverProjects(dir) {
|
|
|
1359
2185
|
for (const entry of entries.toSorted((a, b) => a.name.localeCompare(b.name))) {
|
|
1360
2186
|
if (!entry.isDirectory() || entry.name.startsWith(".") || SKIPPED.has(entry.name))
|
|
1361
2187
|
continue;
|
|
1362
|
-
const steps = await discover(
|
|
2188
|
+
const steps = await discover(join8(dir, entry.name));
|
|
1363
2189
|
if (steps.length > 0)
|
|
1364
|
-
projects.push({ name: entry.name, dir:
|
|
2190
|
+
projects.push({ name: entry.name, dir: join8(dir, entry.name), steps });
|
|
1365
2191
|
}
|
|
1366
2192
|
return projects;
|
|
1367
2193
|
}
|
|
@@ -1404,12 +2230,12 @@ function list2(dir) {
|
|
|
1404
2230
|
}
|
|
1405
2231
|
}
|
|
1406
2232
|
function locate(dir, here, names) {
|
|
1407
|
-
for (let current = dir;; current =
|
|
1408
|
-
const found = names.find((name) =>
|
|
2233
|
+
for (let current = dir;; current = join9(current, "..")) {
|
|
2234
|
+
const found = names.find((name) => existsSync4(join9(current, name)));
|
|
1409
2235
|
if (found !== undefined)
|
|
1410
|
-
return
|
|
2236
|
+
return join9(current, found);
|
|
1411
2237
|
const absolute = resolvePath(current);
|
|
1412
|
-
if (!here ||
|
|
2238
|
+
if (!here || existsSync4(join9(current, ".git")) || dirname5(absolute) === absolute)
|
|
1413
2239
|
return;
|
|
1414
2240
|
}
|
|
1415
2241
|
}
|
|
@@ -1438,7 +2264,7 @@ function justFlags(dir, here) {
|
|
|
1438
2264
|
if (here)
|
|
1439
2265
|
return [];
|
|
1440
2266
|
const file = list2(dir).find((name) => JUSTFILE.test(name));
|
|
1441
|
-
return file === undefined ? undefined : ["--justfile",
|
|
2267
|
+
return file === undefined ? undefined : ["--justfile", join9(dir, file)];
|
|
1442
2268
|
}
|
|
1443
2269
|
function just(dir, here) {
|
|
1444
2270
|
const flags = justFlags(dir, here);
|
|
@@ -1498,10 +2324,10 @@ function show(source, target) {
|
|
|
1498
2324
|
`).trimEnd();
|
|
1499
2325
|
}
|
|
1500
2326
|
function make(dir) {
|
|
1501
|
-
const file = MAKEFILES.find((name) =>
|
|
2327
|
+
const file = MAKEFILES.find((name) => existsSync4(join9(dir, name)));
|
|
1502
2328
|
if (file === undefined)
|
|
1503
2329
|
return [];
|
|
1504
|
-
const source =
|
|
2330
|
+
const source = readFileSync4(join9(dir, file), "utf-8");
|
|
1505
2331
|
return build({ kind: "make", tag: "make", runner: ["make"], separated: false, dir }, targets2(source).map((target) => ({ ...target, body: show(source, target.name) })));
|
|
1506
2332
|
}
|
|
1507
2333
|
function manager(dir, manifest) {
|
|
@@ -1509,53 +2335,53 @@ function manager(dir, manifest) {
|
|
|
1509
2335
|
const known = LOCKFILES.find(([, name]) => name === declared);
|
|
1510
2336
|
if (known !== undefined)
|
|
1511
2337
|
return known[1];
|
|
1512
|
-
for (let current = resolvePath(dir);; current =
|
|
1513
|
-
const found = LOCKFILES.find(([file]) =>
|
|
2338
|
+
for (let current = resolvePath(dir);; current = dirname5(current)) {
|
|
2339
|
+
const found = LOCKFILES.find(([file]) => existsSync4(join9(current, file)));
|
|
1514
2340
|
if (found !== undefined)
|
|
1515
2341
|
return found[1];
|
|
1516
|
-
if (
|
|
2342
|
+
if (dirname5(current) === current)
|
|
1517
2343
|
return "bun";
|
|
1518
2344
|
}
|
|
1519
2345
|
}
|
|
1520
|
-
function
|
|
2346
|
+
function table2(value) {
|
|
1521
2347
|
return value !== null && typeof value === "object" ? value : {};
|
|
1522
2348
|
}
|
|
1523
2349
|
function packageScripts(dir, here) {
|
|
1524
2350
|
const file = locate(dir, here, ["package.json"]);
|
|
1525
2351
|
if (file === undefined)
|
|
1526
2352
|
return [];
|
|
1527
|
-
const manifest = JSON.parse(
|
|
1528
|
-
const tool = manager(
|
|
1529
|
-
const items = Object.entries(
|
|
1530
|
-
return build({ kind: "package", tag: tool, runner: [tool, "run"], separated: tool === "npm", dir:
|
|
2353
|
+
const manifest = JSON.parse(readFileSync4(file, "utf-8"));
|
|
2354
|
+
const tool = manager(dirname5(file), manifest);
|
|
2355
|
+
const items = Object.entries(table2(manifest.scripts)).map(([name, command]) => ({ name, body: text2(command) }));
|
|
2356
|
+
return build({ kind: "package", tag: tool, runner: [tool, "run"], separated: tool === "npm", dir: dirname5(file) }, items);
|
|
1531
2357
|
}
|
|
1532
2358
|
function composer(dir, here) {
|
|
1533
2359
|
const file = locate(dir, here, ["composer.json"]);
|
|
1534
2360
|
if (file === undefined)
|
|
1535
2361
|
return [];
|
|
1536
|
-
const manifest = JSON.parse(
|
|
1537
|
-
const descriptions =
|
|
1538
|
-
const items = Object.entries(
|
|
1539
|
-
return build({ kind: "composer", tag: "composer", runner: ["composer", "run"], separated: true, dir:
|
|
2362
|
+
const manifest = JSON.parse(readFileSync4(file, "utf-8"));
|
|
2363
|
+
const descriptions = table2(manifest["scripts-descriptions"]);
|
|
2364
|
+
const items = Object.entries(table2(manifest.scripts)).map(([name, command]) => ({ name, doc: field(descriptions, name), body: text2(command) }));
|
|
2365
|
+
return build({ kind: "composer", tag: "composer", runner: ["composer", "run"], separated: true, dir: dirname5(file) }, items);
|
|
1540
2366
|
}
|
|
1541
2367
|
function deno(dir, here) {
|
|
1542
2368
|
const file = locate(dir, here, ["deno.json", "deno.jsonc"]);
|
|
1543
2369
|
if (file === undefined)
|
|
1544
2370
|
return [];
|
|
1545
|
-
const manifest = Bun.JSONC.parse(
|
|
1546
|
-
const items = Object.entries(
|
|
1547
|
-
return build({ kind: "deno", tag: "deno", runner: ["deno", "task"], separated: false, dir:
|
|
2371
|
+
const manifest = Bun.JSONC.parse(readFileSync4(file, "utf-8"));
|
|
2372
|
+
const items = Object.entries(table2(manifest.tasks)).map(([name, task]) => ({ name, doc: field(task, "description"), body: field(task, "command") ?? text2(task) }));
|
|
2373
|
+
return build({ kind: "deno", tag: "deno", runner: ["deno", "task"], separated: false, dir: dirname5(file) }, items);
|
|
1548
2374
|
}
|
|
1549
2375
|
function taskfile(dir, here) {
|
|
1550
2376
|
const file = locate(dir, here, TASKFILES);
|
|
1551
2377
|
if (file === undefined)
|
|
1552
2378
|
return [];
|
|
1553
|
-
const tasks =
|
|
2379
|
+
const tasks = table2(Bun.YAML.parse(readFileSync4(file, "utf-8"))?.tasks);
|
|
1554
2380
|
const items = Object.entries(tasks).filter(([, task]) => !(task !== null && typeof task === "object" && task.internal === true)).map(([name, task]) => ({ name, doc: field(task, "desc") ?? field(task, "summary"), body: text2(task) }));
|
|
1555
|
-
return build({ kind: "task", tag: "task", runner: ["task"], separated: true, dir:
|
|
2381
|
+
return build({ kind: "task", tag: "task", runner: ["task"], separated: true, dir: dirname5(file) }, items);
|
|
1556
2382
|
}
|
|
1557
2383
|
function mise(dir, here) {
|
|
1558
|
-
if (!has("mise") || !here && !MISE_FILES.some((name) =>
|
|
2384
|
+
if (!has("mise") || !here && !MISE_FILES.some((name) => existsSync4(join9(dir, name))))
|
|
1559
2385
|
return [];
|
|
1560
2386
|
const proc = Bun.spawnSync(["mise", "tasks", "ls", "--json"], { cwd: dir, stderr: "pipe" });
|
|
1561
2387
|
if (!proc.success)
|
|
@@ -1569,15 +2395,15 @@ function pyproject(dir, here) {
|
|
|
1569
2395
|
const file = locate(dir, here, ["pyproject.toml"]);
|
|
1570
2396
|
if (file === undefined)
|
|
1571
2397
|
return [];
|
|
1572
|
-
const manifest = Bun.TOML.parse(
|
|
1573
|
-
const tool =
|
|
1574
|
-
const project =
|
|
1575
|
-
const uv = locate(
|
|
1576
|
-
const poe = Object.entries(
|
|
1577
|
-
const entryPoints = Object.entries(
|
|
2398
|
+
const manifest = Bun.TOML.parse(readFileSync4(file, "utf-8"));
|
|
2399
|
+
const tool = table2(manifest.tool);
|
|
2400
|
+
const project = table2(manifest.project);
|
|
2401
|
+
const uv = locate(dirname5(file), true, ["uv.lock"]) !== undefined;
|
|
2402
|
+
const poe = Object.entries(table2(table2(tool.poe).tasks)).filter(([name]) => !name.startsWith("_")).map(([name, task]) => ({ name, doc: field(task, "help"), body: field(task, "cmd") ?? field(task, "shell") ?? text2(task) }));
|
|
2403
|
+
const entryPoints = Object.entries(table2(project.scripts)).map(([name, target]) => ({ name, body: text2(target) }));
|
|
1578
2404
|
return [
|
|
1579
|
-
...build({ kind: "poe", tag: "poe", runner: uv ? ["uv", "run", "poe"] : ["poe"], separated: false, dir:
|
|
1580
|
-
...build({ kind: "uv", tag: "uv", runner: ["uv", "run"], separated: false, dir:
|
|
2405
|
+
...build({ kind: "poe", tag: "poe", runner: uv ? ["uv", "run", "poe"] : ["poe"], separated: false, dir: dirname5(file) }, poe),
|
|
2406
|
+
...build({ kind: "uv", tag: "uv", runner: ["uv", "run"], separated: false, dir: dirname5(file) }, entryPoints)
|
|
1581
2407
|
];
|
|
1582
2408
|
}
|
|
1583
2409
|
var READERS = [just, make, packageScripts, composer, deno, taskfile, mise, pyproject];
|
|
@@ -1601,12 +2427,12 @@ function readOr(read, fallback) {
|
|
|
1601
2427
|
}
|
|
1602
2428
|
}
|
|
1603
2429
|
function workspaces() {
|
|
1604
|
-
const manifest = readOr(() => JSON.parse(
|
|
1605
|
-
const declared = Array.isArray(manifest.workspaces) ? manifest.workspaces :
|
|
1606
|
-
const pnpm = readOr(() =>
|
|
2430
|
+
const manifest = readOr(() => JSON.parse(readFileSync4("package.json", "utf-8")), {});
|
|
2431
|
+
const declared = Array.isArray(manifest.workspaces) ? manifest.workspaces : table2(manifest.workspaces).packages;
|
|
2432
|
+
const pnpm = readOr(() => table2(Bun.YAML.parse(readFileSync4("pnpm-workspace.yaml", "utf-8"))).packages, undefined);
|
|
1607
2433
|
const patterns = [declared, pnpm].flatMap((value) => Array.isArray(value) ? value.filter((pattern) => typeof pattern === "string") : []);
|
|
1608
2434
|
const excluded = patterns.filter((pattern) => pattern.startsWith("!")).map((pattern) => new Bun.Glob(pattern.slice(1)));
|
|
1609
|
-
return patterns.filter((pattern) => !pattern.startsWith("!")).flatMap((pattern) => [...new Bun.Glob(`${pattern.replace(TRAILING_SLASHES, "")}/package.json`).scanSync({ cwd: "." })].map((file) =>
|
|
2435
|
+
return patterns.filter((pattern) => !pattern.startsWith("!")).flatMap((pattern) => [...new Bun.Glob(`${pattern.replace(TRAILING_SLASHES, "")}/package.json`).scanSync({ cwd: "." })].map((file) => dirname5(file))).filter((dir) => !dir.split("/").includes("node_modules") && !excluded.some((glob) => glob.match(dir)));
|
|
1610
2436
|
}
|
|
1611
2437
|
function folders() {
|
|
1612
2438
|
return [...new Set([...children(), ...workspaces()])].filter((dir) => dir !== ".");
|
|
@@ -1616,7 +2442,7 @@ function guarded(reader, dir, here, isWarned = false) {
|
|
|
1616
2442
|
return reader(dir, here);
|
|
1617
2443
|
} catch (error) {
|
|
1618
2444
|
if (isWarned)
|
|
1619
|
-
console.error(`j: skipped ${
|
|
2445
|
+
console.error(`j: skipped ${join9(dir, MANIFEST_OF.get(reader) ?? "a manifest")}, it does not parse (${error instanceof Error ? error.message : String(error)})`);
|
|
1620
2446
|
return [];
|
|
1621
2447
|
}
|
|
1622
2448
|
}
|
|
@@ -1640,7 +2466,7 @@ function key(entry) {
|
|
|
1640
2466
|
function id(entry) {
|
|
1641
2467
|
return `${entry.tag}:${key(entry)}`;
|
|
1642
2468
|
}
|
|
1643
|
-
function
|
|
2469
|
+
function resolve3(entries, named) {
|
|
1644
2470
|
const qualified = QUALIFIED.exec(named);
|
|
1645
2471
|
const tagged = qualified && entries.some((entry) => entry.tag === qualified[1]) ? qualified : undefined;
|
|
1646
2472
|
const pool = tagged ? entries.filter((entry) => entry.tag === tagged[1]) : entries;
|
|
@@ -1649,7 +2475,7 @@ function resolve(entries, named) {
|
|
|
1649
2475
|
return exact.length > 0 ? exact : pool.filter((entry) => entry.name === rest);
|
|
1650
2476
|
}
|
|
1651
2477
|
function shortest(entries, entry) {
|
|
1652
|
-
const found =
|
|
2478
|
+
const found = resolve3(entries, key(entry));
|
|
1653
2479
|
return found.length === 1 && found[0] === entry ? key(entry) : id(entry);
|
|
1654
2480
|
}
|
|
1655
2481
|
function signature(entry) {
|
|
@@ -1662,21 +2488,21 @@ function labelled(entries) {
|
|
|
1662
2488
|
return new Set(entries.map((entry) => entry.tag)).size > 1;
|
|
1663
2489
|
}
|
|
1664
2490
|
function row(entry, labels, signed) {
|
|
1665
|
-
const colors = paint(
|
|
2491
|
+
const colors = paint(process20.stderr);
|
|
1666
2492
|
const keyed = entry.dir === "." ? entry.name : `${colors.dim(`${entry.dir}/`)}${entry.name}`;
|
|
1667
2493
|
return {
|
|
1668
2494
|
hidden: [entry.kind, entry.dir, entry.name, id(entry)],
|
|
1669
2495
|
cells: [...labels ? [colors[TINTS[entry.kind]](entry.tag)] : [], keyed, ...signed ? [colors.cyan(signature(entry))] : [], colors.dim(entry.doc ?? "")]
|
|
1670
2496
|
};
|
|
1671
2497
|
}
|
|
1672
|
-
function pickJob(entries, narrowed = false) {
|
|
2498
|
+
async function pickJob(entries, narrowed = false) {
|
|
1673
2499
|
const labels = labelled(entries);
|
|
1674
2500
|
const signed = entries.some((entry) => signature(entry) !== "");
|
|
1675
|
-
const picked =
|
|
2501
|
+
const picked = await pick(entries.map((entry) => row(entry, labels, signed)), {
|
|
1676
2502
|
prompt: `${[...new Set(entries.map((entry) => entry.tag))].join("/")}> `,
|
|
1677
2503
|
searched: labels ? 2 : 1,
|
|
1678
2504
|
...narrowed ? { height: heightFor(entries.length) } : {},
|
|
1679
|
-
preview: `${
|
|
2505
|
+
preview: `${process20.execPath} ${process20.argv[1]} --show {1} {2} {3}`
|
|
1680
2506
|
});
|
|
1681
2507
|
return entries.find((candidate) => id(candidate) === picked?.hidden[3]);
|
|
1682
2508
|
}
|
|
@@ -1685,7 +2511,7 @@ function pickJob(entries, narrowed = false) {
|
|
|
1685
2511
|
var usage2 = `j [name] [args...]
|
|
1686
2512
|
j -
|
|
1687
2513
|
|
|
1688
|
-
Without a name, picks a recipe, target, script or task of this folder, its parents and its child folders
|
|
2514
|
+
Without a name, picks a recipe, target, script or task of this folder, its parents and its child folders when a terminal is attached, and runs it. The ones run last come first, per folder. Without a terminal it lists them instead.
|
|
1689
2515
|
\`j -\` runs the last one again with the same arguments, \`j <name> [args...]\` runs without the picker, the words after the name filling the parameters in order and the required ones left being asked for.
|
|
1690
2516
|
The sources are a Justfile, a Makefile, the scripts of a package.json (through its package manager), composer.json scripts, deno tasks, a Taskfile, mise tasks and a pyproject (poe tasks and entry points).
|
|
1691
2517
|
A name is the key behind the folder it runs from, \`api/test\`, bare for this folder. A name several entries share offers them, unless it comes qualified by its runner: \`just:check\`, \`bun:api/test\`.
|
|
@@ -1698,7 +2524,7 @@ function preview(kind, dir, name) {
|
|
|
1698
2524
|
const entry = readKind(kind, dir).find((candidate) => candidate.name === name);
|
|
1699
2525
|
if (entry === undefined)
|
|
1700
2526
|
return;
|
|
1701
|
-
const colors = paint(
|
|
2527
|
+
const colors = paint(process21.stdout);
|
|
1702
2528
|
console.log(`${colors.dim(`$ ${dir === "." ? "" : `env -C ${dir} `}`)}${colors.bold([...entry.runner, entry.name].join(" "))}
|
|
1703
2529
|
`);
|
|
1704
2530
|
if (entry.body !== undefined)
|
|
@@ -1709,7 +2535,7 @@ function preview(kind, dir, name) {
|
|
|
1709
2535
|
function names(found) {
|
|
1710
2536
|
const described = (entry) => entry.doc === null || entry.doc === "" ? entry.tag : entry.doc;
|
|
1711
2537
|
return [...new Set(found.map(key))].flatMap((name) => {
|
|
1712
|
-
const same =
|
|
2538
|
+
const same = resolve3(found, name);
|
|
1713
2539
|
return same.length === 1 ? [[name, described(same[0])]] : [[name, same.map((entry) => entry.tag).join(", ")], ...same.map((entry) => [id(entry), described(entry)])];
|
|
1714
2540
|
});
|
|
1715
2541
|
}
|
|
@@ -1723,8 +2549,8 @@ function listed2(found) {
|
|
|
1723
2549
|
editsTyped: true
|
|
1724
2550
|
}));
|
|
1725
2551
|
}
|
|
1726
|
-
function
|
|
1727
|
-
const colors = paint(
|
|
2552
|
+
function table3(found) {
|
|
2553
|
+
const colors = paint(process21.stdout);
|
|
1728
2554
|
const labels = labelled(found);
|
|
1729
2555
|
const rows = found.map((entry) => [...labels ? [entry.tag] : [], key(entry), signature(entry)]);
|
|
1730
2556
|
const widths = rows[0]?.map((_, column) => Math.max(...rows.map((cells) => cells[column].length))) ?? [];
|
|
@@ -1775,7 +2601,7 @@ async function j(argv) {
|
|
|
1775
2601
|
if (found.length === 0)
|
|
1776
2602
|
fail("nothing to run here: no recipe, target, script or task", 1);
|
|
1777
2603
|
const previous = runs("j");
|
|
1778
|
-
const interactive =
|
|
2604
|
+
const interactive = isInteractive() && parsed.values.json !== true;
|
|
1779
2605
|
let entry;
|
|
1780
2606
|
let words = given;
|
|
1781
2607
|
if (named === "-") {
|
|
@@ -1785,27 +2611,25 @@ async function j(argv) {
|
|
|
1785
2611
|
fail(earlier ? `${earlier.id} is gone` : "nothing run here yet", 1);
|
|
1786
2612
|
words = [...earlier.args, ...given];
|
|
1787
2613
|
} else if (named === undefined) {
|
|
1788
|
-
if (!interactive
|
|
2614
|
+
if (!interactive) {
|
|
1789
2615
|
if (emitting)
|
|
1790
|
-
fail("the picker needs a terminal
|
|
1791
|
-
if (interactive)
|
|
1792
|
-
console.error("fzf is not installed, listing what can run instead");
|
|
2616
|
+
fail("the picker needs a terminal", 1);
|
|
1793
2617
|
if (parsed.values.json === true)
|
|
1794
2618
|
printJson(found.map((each) => ({ id: id(each), key: key(each), runner: each.tag, dir: each.dir, name: each.name, doc: each.doc, parameters: each.parameters })));
|
|
1795
2619
|
else
|
|
1796
|
-
console.log(
|
|
2620
|
+
console.log(table3(found));
|
|
1797
2621
|
return 0;
|
|
1798
2622
|
}
|
|
1799
|
-
entry = pickJob(byRecency(found, previous, id));
|
|
2623
|
+
entry = await pickJob(byRecency(found, previous, id));
|
|
1800
2624
|
} else {
|
|
1801
|
-
const matching =
|
|
2625
|
+
const matching = resolve3(found, named);
|
|
1802
2626
|
if (matching.length === 0) {
|
|
1803
2627
|
const nearest = closest(named, found.map(key));
|
|
1804
2628
|
fail(`nothing named ${named}${nearest === undefined ? "" : `, did you mean ${nearest}?`}`, 1);
|
|
1805
2629
|
}
|
|
1806
|
-
if (matching.length > 1 &&
|
|
2630
|
+
if (matching.length > 1 && !interactive)
|
|
1807
2631
|
fail(`${named} is several: ${matching.map(id).join(", ")}`, 1, "qualify it with its runner");
|
|
1808
|
-
entry = matching.length === 1 ? matching[0] : pickJob(matching, true);
|
|
2632
|
+
entry = matching.length === 1 ? matching[0] : await pickJob(matching, true);
|
|
1809
2633
|
}
|
|
1810
2634
|
if (entry === undefined)
|
|
1811
2635
|
return 1;
|
|
@@ -1828,4 +2652,4 @@ async function j(argv) {
|
|
|
1828
2652
|
}
|
|
1829
2653
|
|
|
1830
2654
|
// src/j-cli.ts
|
|
1831
|
-
|
|
2655
|
+
process22.exitCode = await runMain({ name: "j", version, run: j });
|