@kaynooo/cli-base 0.9.0 → 0.10.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/dist/cli.js CHANGED
@@ -2,7 +2,7 @@
2
2
  // @bun
3
3
 
4
4
  // src/cli.ts
5
- import process17 from "process";
5
+ import process18 from "process";
6
6
  // ../kit/src/args.ts
7
7
  import process9 from "process";
8
8
  import { parseArgs } from "util";
@@ -637,9 +637,33 @@ function nearestOption(message, options, allowNegative) {
637
637
  const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
638
638
  return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
639
639
  }
640
+ // ../kit/src/argv.ts
641
+ import { readFileSync as readFileSync2, realpathSync } from "fs";
642
+ import { resolve as resolve3 } from "path";
643
+ import process10 from "process";
644
+ function commandLine() {
645
+ try {
646
+ return readFileSync2("/proc/self/cmdline", "utf8").split("\x00").slice(0, -1);
647
+ } catch {
648
+ return;
649
+ }
650
+ }
651
+ function realPath(word) {
652
+ try {
653
+ return realpathSync(resolve3(word));
654
+ } catch {
655
+ return;
656
+ }
657
+ }
658
+ function scriptArgs(argv = process10.argv.slice(2), cmdline = commandLine(), script = process10.argv[1]) {
659
+ const target = script === undefined ? undefined : realPath(script) ?? script;
660
+ const at = cmdline === undefined || target === undefined ? -1 : cmdline.findIndex((word, index) => index > 0 && (resolve3(word) === script || realPath(word) === target));
661
+ const after = at === -1 ? [] : cmdline.slice(at + 1);
662
+ return after[0] === "--" && after.length === argv.length + 1 && argv.every((word, index) => word === after[index + 1]) ? after : argv;
663
+ }
640
664
  // ../kit/src/process.ts
641
665
  import { constants } from "os";
642
- import process10 from "process";
666
+ import process11 from "process";
643
667
  var KILL_GRACE_MS = 2000;
644
668
  var SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
645
669
  function exitCodeOf(code, signal) {
@@ -661,7 +685,7 @@ function holdSignals(cleanup) {
661
685
  },
662
686
  release: () => {
663
687
  for (const signal of SIGNALS)
664
- process10.off(signal, onSignal);
688
+ process11.off(signal, onSignal);
665
689
  }
666
690
  };
667
691
  function onSignal(signal) {
@@ -674,10 +698,10 @@ function holdSignals(cleanup) {
674
698
  return;
675
699
  held.release();
676
700
  cleanup(signal);
677
- process10.kill(process10.pid, signal);
701
+ process11.kill(process11.pid, signal);
678
702
  }
679
703
  for (const signal of SIGNALS)
680
- process10.on(signal, onSignal);
704
+ process11.on(signal, onSignal);
681
705
  return held;
682
706
  }
683
707
  async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal, maxBytes = Infinity, onChunk, onStart } = {}) {
@@ -691,7 +715,7 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal,
691
715
  return;
692
716
  try {
693
717
  if (grouped)
694
- process10.kill(-proc.pid, killing);
718
+ process11.kill(-proc.pid, killing);
695
719
  else
696
720
  proc.kill(killing);
697
721
  } catch {}
@@ -765,15 +789,15 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
765
789
  var RESET_STYLE = `${ESC}[0m`;
766
790
  var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
767
791
  // ../kit/src/cli.ts
768
- import { readFileSync as readFileSync2, realpathSync } from "fs";
792
+ import { readFileSync as readFileSync3, realpathSync as realpathSync2 } from "fs";
769
793
  import { dirname as dirname3, join as join5 } from "path";
770
- import process12 from "process";
794
+ import process13 from "process";
771
795
 
772
796
  // ../kit/src/usage.ts
773
797
  import { appendFileSync, mkdirSync } from "fs";
774
798
  import { homedir as homedir3 } from "os";
775
799
  import { dirname as dirname2, isAbsolute, join as join4 } from "path";
776
- import process11 from "process";
800
+ import process12 from "process";
777
801
 
778
802
  // ../kit/src/logs.ts
779
803
  var DAY_MS = 24 * 60 * 60 * 1000;
@@ -787,14 +811,14 @@ var usageError;
787
811
  function noteUsageError(message) {
788
812
  usageError = message;
789
813
  }
790
- function stateHome(env = process11.env) {
814
+ function stateHome(env = process12.env) {
791
815
  const dir = env.XDG_STATE_HOME;
792
816
  return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
793
817
  }
794
- function usagePath(env = process11.env) {
818
+ function usagePath(env = process12.env) {
795
819
  return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
796
820
  }
797
- function usageRecorded(env = process11.env) {
821
+ function usageRecorded(env = process12.env) {
798
822
  const off = env.KAYNOOO_CLI_NO_USAGE;
799
823
  return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
800
824
  }
@@ -808,7 +832,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
808
832
  usageError = undefined;
809
833
  return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
810
834
  }
811
- function recordUsage(entry, env = process11.env) {
835
+ function recordUsage(entry, env = process12.env) {
812
836
  if (!usageRecorded(env))
813
837
  return;
814
838
  try {
@@ -899,8 +923,8 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
899
923
  `);
900
924
  }
901
925
  function printError(prefix, message, hint, cause) {
902
- const colors = paint(process12.stderr);
903
- const styled = takesControlCodes(process12.stderr);
926
+ const colors = paint(process13.stderr);
927
+ const styled = takesControlCodes(process13.stderr);
904
928
  const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
905
929
  const under = (label, text, tint) => text === undefined ? "" : `
906
930
  ${styled ? tint(label) : `${label}:`} ${text}`;
@@ -943,11 +967,11 @@ async function dispatch(level, typed, { version, redact }) {
943
967
  }
944
968
  }
945
969
  if (name === undefined && level.path.length > 1) {
946
- console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process12.stderr));
970
+ console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process13.stderr));
947
971
  return 2;
948
972
  }
949
973
  if (name === undefined || name === "help" || name === "--help" || name === "-h") {
950
- console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process12.stdout));
974
+ console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process13.stdout));
951
975
  return 0;
952
976
  }
953
977
  if (level.path.length === 1 && (name === "--version" || name === "-v")) {
@@ -1003,24 +1027,24 @@ function candidateLine(candidate) {
1003
1027
  }
1004
1028
  function positionalBeingTyped(before, { options, positionals }) {
1005
1029
  const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
1006
- let index = 0;
1030
+ const typed = [];
1007
1031
  let at = 0;
1008
1032
  while (at < before.length) {
1009
1033
  const word = before[at];
1010
1034
  if (word === "--") {
1011
- index += before.length - at - 1;
1035
+ typed.push(...before.slice(at + 1));
1012
1036
  break;
1013
1037
  }
1014
1038
  if (!word.startsWith("-"))
1015
- index++;
1039
+ typed.push(word);
1016
1040
  else if (takesValue(word))
1017
1041
  at++;
1018
1042
  at++;
1019
1043
  }
1020
1044
  const last = positionals.at(-1);
1021
- if (index < positionals.length)
1022
- return positionals[index];
1023
- return last?.replace("]", "").endsWith("...") === true ? last : undefined;
1045
+ if (typed.length < positionals.length)
1046
+ return { name: positionals[typed.length], typed };
1047
+ return { name: last?.replace("]", "").endsWith("...") === true ? last : undefined, typed };
1024
1048
  }
1025
1049
  async function leafCandidates(run, words) {
1026
1050
  const completing = await completingOf(run);
@@ -1028,12 +1052,12 @@ async function leafCandidates(run, words) {
1028
1052
  return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
1029
1053
  const flag = words.at(-2);
1030
1054
  const option = flag?.startsWith("-") === true ? Object.entries(completing.options).find(([name, { short }]) => flag === `--${name}` || flag === `-${short}`)?.[1] : undefined;
1055
+ const { name, typed } = positionalBeingTyped(words.slice(0, -1), completing);
1031
1056
  if (option?.type === "string") {
1032
- const values = option.choices === undefined ? await option.candidates?.() : option.choices;
1057
+ const values = option.choices === undefined ? await option.candidates?.(typed) : option.choices;
1033
1058
  return values === undefined ? [FILES] : values.map(candidateLine);
1034
1059
  }
1035
- const name = positionalBeingTyped(words.slice(0, -1), completing);
1036
- const values = name === undefined ? undefined : await completing.candidates[name]?.();
1060
+ const values = name === undefined ? undefined : await completing.candidates[name]?.(typed);
1037
1061
  return values === undefined ? [FILES] : values.map(candidateLine);
1038
1062
  }
1039
1063
  async function candidates(scope, defaultCommand, words, before = []) {
@@ -1064,9 +1088,13 @@ async function printCandidates(found) {
1064
1088
  return 0;
1065
1089
  }
1066
1090
  function packageVersion(entry = Bun.main) {
1067
- for (let dir = dirname3(realpathSync(entry));; dir = dirname3(dir)) {
1091
+ let resolved = entry;
1092
+ try {
1093
+ resolved = realpathSync2(entry);
1094
+ } catch {}
1095
+ for (let dir = dirname3(resolved);; dir = dirname3(dir)) {
1068
1096
  try {
1069
- const { version } = JSON.parse(readFileSync2(join5(dir, "package.json"), "utf8"));
1097
+ const { version } = JSON.parse(readFileSync3(join5(dir, "package.json"), "utf8"));
1070
1098
  if (typeof version === "string")
1071
1099
  return version;
1072
1100
  } catch {}
@@ -1074,7 +1102,7 @@ function packageVersion(entry = Bun.main) {
1074
1102
  return "0.0.0";
1075
1103
  }
1076
1104
  }
1077
- async function runCli(cli, argv = process12.argv.slice(2)) {
1105
+ async function runCli(cli, argv = scriptArgs()) {
1078
1106
  const version = cli.version ?? packageVersion();
1079
1107
  if (argv[0] === COMPLETE)
1080
1108
  return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
@@ -1101,7 +1129,7 @@ function commandPath(scope, argv) {
1101
1129
  }
1102
1130
  return path.length === 0 ? undefined : path.join(" ");
1103
1131
  }
1104
- async function runMain({ name, version, run, redact, usage }, argv = process12.argv.slice(2)) {
1132
+ async function runMain({ name, version, run, redact, usage }, argv = scriptArgs()) {
1105
1133
  if (argv[0] === COMPLETE)
1106
1134
  return printCandidates(leafCandidates(run, argv.slice(1)));
1107
1135
  if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
@@ -1130,13 +1158,13 @@ var {Glob } = globalThis.Bun;
1130
1158
  var SESSION_FILES = new Glob("*.json");
1131
1159
  var ACCOUNT_DIRS = new Glob("*/sessions");
1132
1160
  // src/base.ts
1133
- import process15 from "process";
1161
+ import process16 from "process";
1134
1162
 
1135
1163
  // src/worktree.ts
1136
- import { cpSync, existsSync as existsSync3, lstatSync, mkdirSync as mkdirSync2, mkdtempSync, readdirSync, realpathSync as realpathSync2, rmSync, symlinkSync, unlinkSync } from "fs";
1164
+ import { cpSync, existsSync as existsSync3, lstatSync, mkdirSync as mkdirSync2, mkdtempSync, readdirSync, realpathSync as realpathSync3, rmSync, symlinkSync, unlinkSync } from "fs";
1137
1165
  import { tmpdir } from "os";
1138
- import { basename, dirname as dirname4, isAbsolute as isAbsolute2, join as join6, relative, resolve as resolve3, sep } from "path";
1139
- import process14 from "process";
1166
+ import { basename, dirname as dirname4, isAbsolute as isAbsolute2, join as join6, relative, resolve as resolve4, sep } from "path";
1167
+ import process15 from "process";
1140
1168
  var WHITESPACE = /\s/;
1141
1169
  var ORIGIN_PREFIX = /^origin\//;
1142
1170
  var ENV_FILE = /^\.env(?:\..+)?$/;
@@ -1182,12 +1210,12 @@ async function defaultProofRef(root) {
1182
1210
  return base.code === 0 && stdoutOf(base) !== stdoutOf(head) ? stdoutOf(base) : "HEAD";
1183
1211
  }
1184
1212
  function keptPaths(root, cwd, paths) {
1185
- const realRoot = realpathSync2(root);
1213
+ const realRoot = realpathSync3(root);
1186
1214
  return paths.map((path) => {
1187
- const absolute = resolve3(cwd, path);
1215
+ const absolute = resolve4(cwd, path);
1188
1216
  if (!existsSync3(dirname4(absolute)))
1189
1217
  fail(`--keep ${path}: no such file`);
1190
- const inRepo = relative(realRoot, join6(realpathSync2(dirname4(absolute)), basename(absolute)));
1218
+ const inRepo = relative(realRoot, join6(realpathSync3(dirname4(absolute)), basename(absolute)));
1191
1219
  if (inRepo === "" || inRepo === ".." || inRepo.startsWith(PARENT) || isAbsolute2(inRepo))
1192
1220
  fail(`--keep ${path} is outside the repository`);
1193
1221
  if (!existsSync3(absolute))
@@ -1260,7 +1288,7 @@ function moduleLinks(root, folder) {
1260
1288
  }
1261
1289
  function workspaceTarget(root, path) {
1262
1290
  try {
1263
- const inRepo = relative(realpathSync2(root), realpathSync2(join6(root, path)));
1291
+ const inRepo = relative(realpathSync3(root), realpathSync3(join6(root, path)));
1264
1292
  return inRepo === "" || inRepo.startsWith(PARENT) || inRepo.split(sep).includes("node_modules") ? undefined : inRepo;
1265
1293
  } catch {
1266
1294
  return;
@@ -1292,7 +1320,7 @@ async function sweepStale(root) {
1292
1320
  continue;
1293
1321
  const path = join6(tmpdir(), name);
1294
1322
  const stat = lstatSync(path, { throwIfNoEntry: false });
1295
- if (stat?.isDirectory() === true && stat.uid === process14.getuid?.() && Date.now() - stat.mtimeMs > STALE_AFTER)
1323
+ if (stat?.isDirectory() === true && stat.uid === process15.getuid?.() && Date.now() - stat.mtimeMs > STALE_AFTER)
1296
1324
  rmSync(path, { recursive: true, force: true });
1297
1325
  }
1298
1326
  await git2(["worktree", "prune"], root);
@@ -1382,20 +1410,20 @@ ${USAGE}`);
1382
1410
  if (runsInContainer(given))
1383
1411
  fail("a container of the compose project mounts the main checkout, not the temporary worktree, so the command would not run at the ref: run the tool on the host, or check the ref out by hand");
1384
1412
  const command = shellCommand(given);
1385
- const cwd = process15.cwd();
1413
+ const cwd = process16.cwd();
1386
1414
  const { root, prefix } = await checkout(cwd);
1387
1415
  const ref = parsed.values.ref ?? "HEAD";
1388
1416
  const sha = await resolveRef(root, ref);
1389
1417
  const kept = keptPaths(root, cwd, parsed.values.keep ?? []);
1390
1418
  const ran = await inWorktree({ root, prefix, sha, ref, kept, link: parsed.values["no-link"] !== true }, async (runDir, signals) => runIn(runDir, command, signals));
1391
1419
  if (!ran.started)
1392
- console.error(`base: ${ran.output}`);
1393
- console.error(`base: ran at ${ref} (${sha.slice(0, 7)}), exit ${ran.code}`);
1420
+ console.error(`${role("problem", process16.stderr)("base:")} ${ran.output}`);
1421
+ console.error(`${role("label", process16.stderr)("base:")} ran at ${ref} (${sha.slice(0, 7)}), exit ${ran.code}`);
1394
1422
  return ran.code;
1395
1423
  }
1396
1424
 
1397
1425
  // src/proof.ts
1398
- import process16 from "process";
1426
+ import process17 from "process";
1399
1427
  var USAGE2 = `proof [--ref <rev>] [--runs <n>] [--keep <path>...] [--no-link] [--json] -- <command> [args...]: runs the command at HEAD or another ref in a temporary worktree, then in the working tree, to show that a test fails before a change and passes after it
1400
1428
 
1401
1429
  The ref defaults to the merge base with the default branch of origin (origin/main unless origin/HEAD names another) when HEAD is on a branch ahead of it, else HEAD. --runs <n> runs the command n times now, for a flaky test, and every run must pass.`;
@@ -1445,7 +1473,7 @@ ${USAGE2}`);
1445
1473
  if (runsInContainer(given))
1446
1474
  fail("a container of the compose project mounts the main checkout, not the temporary worktree, so the command would not run at the ref: run the tool on the host, or check the ref out by hand");
1447
1475
  const command = shellCommand(given);
1448
- const cwd = process16.cwd();
1476
+ const cwd = process17.cwd();
1449
1477
  const { root, prefix } = await checkout(cwd);
1450
1478
  const runs = wholeNumber(parsed.values.runs, "runs", 1, 1);
1451
1479
  const ref = parsed.values.ref ?? await defaultProofRef(root);
@@ -1480,7 +1508,7 @@ ${folded(run.output)}`}`);
1480
1508
  printJson({ outcome, proved, ref, sha, before: { code: before.code, output: before.output }, now: { code: now.code, output: now.output }, runs: nowRuns.map((run) => ({ code: run.code })) });
1481
1509
  return proved ? 0 : 1;
1482
1510
  }
1483
- console.log(role(proved ? "ok" : "problem", process16.stdout)(message));
1511
+ console.log(role(proved ? "ok" : "problem", process17.stdout)(message));
1484
1512
  if (runs > 1)
1485
1513
  console.log(`now: ${codes.map((code, i) => `run ${i + 1} ${code === 0 ? "passed" : `failed (exit ${code})`}`).join(", ")}`);
1486
1514
  const shown = side === "before" ? before : now;
@@ -1491,7 +1519,7 @@ ${folded(shown.output)}`);
1491
1519
  }
1492
1520
 
1493
1521
  // src/cli.ts
1494
- process17.exitCode = await runCli({
1522
+ process18.exitCode = await runCli({
1495
1523
  name: "base",
1496
1524
  about: "runs a command at HEAD or another ref in a temporary worktree, the working tree untouched",
1497
1525
  replaces: "git stash, a test run, then git stash pop",
package/dist/proof-cli.js CHANGED
@@ -2,7 +2,7 @@
2
2
  // @bun
3
3
 
4
4
  // src/proof-cli.ts
5
- import process16 from "process";
5
+ import process17 from "process";
6
6
  // ../kit/src/args.ts
7
7
  import process9 from "process";
8
8
  import { parseArgs } from "util";
@@ -637,9 +637,33 @@ function nearestOption(message, options, allowNegative) {
637
637
  const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
638
638
  return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
639
639
  }
640
+ // ../kit/src/argv.ts
641
+ import { readFileSync as readFileSync2, realpathSync } from "fs";
642
+ import { resolve as resolve3 } from "path";
643
+ import process10 from "process";
644
+ function commandLine() {
645
+ try {
646
+ return readFileSync2("/proc/self/cmdline", "utf8").split("\x00").slice(0, -1);
647
+ } catch {
648
+ return;
649
+ }
650
+ }
651
+ function realPath(word) {
652
+ try {
653
+ return realpathSync(resolve3(word));
654
+ } catch {
655
+ return;
656
+ }
657
+ }
658
+ function scriptArgs(argv = process10.argv.slice(2), cmdline = commandLine(), script = process10.argv[1]) {
659
+ const target = script === undefined ? undefined : realPath(script) ?? script;
660
+ const at = cmdline === undefined || target === undefined ? -1 : cmdline.findIndex((word, index) => index > 0 && (resolve3(word) === script || realPath(word) === target));
661
+ const after = at === -1 ? [] : cmdline.slice(at + 1);
662
+ return after[0] === "--" && after.length === argv.length + 1 && argv.every((word, index) => word === after[index + 1]) ? after : argv;
663
+ }
640
664
  // ../kit/src/process.ts
641
665
  import { constants } from "os";
642
- import process10 from "process";
666
+ import process11 from "process";
643
667
  var KILL_GRACE_MS = 2000;
644
668
  var SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
645
669
  function exitCodeOf(code, signal) {
@@ -661,7 +685,7 @@ function holdSignals(cleanup) {
661
685
  },
662
686
  release: () => {
663
687
  for (const signal of SIGNALS)
664
- process10.off(signal, onSignal);
688
+ process11.off(signal, onSignal);
665
689
  }
666
690
  };
667
691
  function onSignal(signal) {
@@ -674,10 +698,10 @@ function holdSignals(cleanup) {
674
698
  return;
675
699
  held.release();
676
700
  cleanup(signal);
677
- process10.kill(process10.pid, signal);
701
+ process11.kill(process11.pid, signal);
678
702
  }
679
703
  for (const signal of SIGNALS)
680
- process10.on(signal, onSignal);
704
+ process11.on(signal, onSignal);
681
705
  return held;
682
706
  }
683
707
  async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal, maxBytes = Infinity, onChunk, onStart } = {}) {
@@ -691,7 +715,7 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal,
691
715
  return;
692
716
  try {
693
717
  if (grouped)
694
- process10.kill(-proc.pid, killing);
718
+ process11.kill(-proc.pid, killing);
695
719
  else
696
720
  proc.kill(killing);
697
721
  } catch {}
@@ -765,15 +789,15 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
765
789
  var RESET_STYLE = `${ESC}[0m`;
766
790
  var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
767
791
  // ../kit/src/cli.ts
768
- import { readFileSync as readFileSync2, realpathSync } from "fs";
792
+ import { readFileSync as readFileSync3, realpathSync as realpathSync2 } from "fs";
769
793
  import { dirname as dirname3, join as join5 } from "path";
770
- import process12 from "process";
794
+ import process13 from "process";
771
795
 
772
796
  // ../kit/src/usage.ts
773
797
  import { appendFileSync, mkdirSync } from "fs";
774
798
  import { homedir as homedir3 } from "os";
775
799
  import { dirname as dirname2, isAbsolute, join as join4 } from "path";
776
- import process11 from "process";
800
+ import process12 from "process";
777
801
 
778
802
  // ../kit/src/logs.ts
779
803
  var DAY_MS = 24 * 60 * 60 * 1000;
@@ -787,14 +811,14 @@ var usageError;
787
811
  function noteUsageError(message) {
788
812
  usageError = message;
789
813
  }
790
- function stateHome(env = process11.env) {
814
+ function stateHome(env = process12.env) {
791
815
  const dir = env.XDG_STATE_HOME;
792
816
  return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
793
817
  }
794
- function usagePath(env = process11.env) {
818
+ function usagePath(env = process12.env) {
795
819
  return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
796
820
  }
797
- function usageRecorded(env = process11.env) {
821
+ function usageRecorded(env = process12.env) {
798
822
  const off = env.KAYNOOO_CLI_NO_USAGE;
799
823
  return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
800
824
  }
@@ -808,7 +832,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
808
832
  usageError = undefined;
809
833
  return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
810
834
  }
811
- function recordUsage(entry, env = process11.env) {
835
+ function recordUsage(entry, env = process12.env) {
812
836
  if (!usageRecorded(env))
813
837
  return;
814
838
  try {
@@ -899,8 +923,8 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
899
923
  `);
900
924
  }
901
925
  function printError(prefix, message, hint, cause) {
902
- const colors = paint(process12.stderr);
903
- const styled = takesControlCodes(process12.stderr);
926
+ const colors = paint(process13.stderr);
927
+ const styled = takesControlCodes(process13.stderr);
904
928
  const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
905
929
  const under = (label, text, tint) => text === undefined ? "" : `
906
930
  ${styled ? tint(label) : `${label}:`} ${text}`;
@@ -943,11 +967,11 @@ async function dispatch(level, typed, { version, redact }) {
943
967
  }
944
968
  }
945
969
  if (name === undefined && level.path.length > 1) {
946
- console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process12.stderr));
970
+ console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process13.stderr));
947
971
  return 2;
948
972
  }
949
973
  if (name === undefined || name === "help" || name === "--help" || name === "-h") {
950
- console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process12.stdout));
974
+ console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process13.stdout));
951
975
  return 0;
952
976
  }
953
977
  if (level.path.length === 1 && (name === "--version" || name === "-v")) {
@@ -1003,24 +1027,24 @@ function candidateLine(candidate) {
1003
1027
  }
1004
1028
  function positionalBeingTyped(before, { options, positionals }) {
1005
1029
  const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
1006
- let index = 0;
1030
+ const typed = [];
1007
1031
  let at = 0;
1008
1032
  while (at < before.length) {
1009
1033
  const word = before[at];
1010
1034
  if (word === "--") {
1011
- index += before.length - at - 1;
1035
+ typed.push(...before.slice(at + 1));
1012
1036
  break;
1013
1037
  }
1014
1038
  if (!word.startsWith("-"))
1015
- index++;
1039
+ typed.push(word);
1016
1040
  else if (takesValue(word))
1017
1041
  at++;
1018
1042
  at++;
1019
1043
  }
1020
1044
  const last = positionals.at(-1);
1021
- if (index < positionals.length)
1022
- return positionals[index];
1023
- return last?.replace("]", "").endsWith("...") === true ? last : undefined;
1045
+ if (typed.length < positionals.length)
1046
+ return { name: positionals[typed.length], typed };
1047
+ return { name: last?.replace("]", "").endsWith("...") === true ? last : undefined, typed };
1024
1048
  }
1025
1049
  async function leafCandidates(run, words) {
1026
1050
  const completing = await completingOf(run);
@@ -1028,12 +1052,12 @@ async function leafCandidates(run, words) {
1028
1052
  return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
1029
1053
  const flag = words.at(-2);
1030
1054
  const option = flag?.startsWith("-") === true ? Object.entries(completing.options).find(([name, { short }]) => flag === `--${name}` || flag === `-${short}`)?.[1] : undefined;
1055
+ const { name, typed } = positionalBeingTyped(words.slice(0, -1), completing);
1031
1056
  if (option?.type === "string") {
1032
- const values = option.choices === undefined ? await option.candidates?.() : option.choices;
1057
+ const values = option.choices === undefined ? await option.candidates?.(typed) : option.choices;
1033
1058
  return values === undefined ? [FILES] : values.map(candidateLine);
1034
1059
  }
1035
- const name = positionalBeingTyped(words.slice(0, -1), completing);
1036
- const values = name === undefined ? undefined : await completing.candidates[name]?.();
1060
+ const values = name === undefined ? undefined : await completing.candidates[name]?.(typed);
1037
1061
  return values === undefined ? [FILES] : values.map(candidateLine);
1038
1062
  }
1039
1063
  async function candidates(scope, defaultCommand, words, before = []) {
@@ -1064,9 +1088,13 @@ async function printCandidates(found) {
1064
1088
  return 0;
1065
1089
  }
1066
1090
  function packageVersion(entry = Bun.main) {
1067
- for (let dir = dirname3(realpathSync(entry));; dir = dirname3(dir)) {
1091
+ let resolved = entry;
1092
+ try {
1093
+ resolved = realpathSync2(entry);
1094
+ } catch {}
1095
+ for (let dir = dirname3(resolved);; dir = dirname3(dir)) {
1068
1096
  try {
1069
- const { version } = JSON.parse(readFileSync2(join5(dir, "package.json"), "utf8"));
1097
+ const { version } = JSON.parse(readFileSync3(join5(dir, "package.json"), "utf8"));
1070
1098
  if (typeof version === "string")
1071
1099
  return version;
1072
1100
  } catch {}
@@ -1074,7 +1102,7 @@ function packageVersion(entry = Bun.main) {
1074
1102
  return "0.0.0";
1075
1103
  }
1076
1104
  }
1077
- async function runCli(cli, argv = process12.argv.slice(2)) {
1105
+ async function runCli(cli, argv = scriptArgs()) {
1078
1106
  const version = cli.version ?? packageVersion();
1079
1107
  if (argv[0] === COMPLETE)
1080
1108
  return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
@@ -1101,7 +1129,7 @@ function commandPath(scope, argv) {
1101
1129
  }
1102
1130
  return path.length === 0 ? undefined : path.join(" ");
1103
1131
  }
1104
- async function runMain({ name, version, run, redact, usage }, argv = process12.argv.slice(2)) {
1132
+ async function runMain({ name, version, run, redact, usage }, argv = scriptArgs()) {
1105
1133
  if (argv[0] === COMPLETE)
1106
1134
  return printCandidates(leafCandidates(run, argv.slice(1)));
1107
1135
  if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
@@ -1130,13 +1158,13 @@ var {Glob } = globalThis.Bun;
1130
1158
  var SESSION_FILES = new Glob("*.json");
1131
1159
  var ACCOUNT_DIRS = new Glob("*/sessions");
1132
1160
  // src/proof.ts
1133
- import process15 from "process";
1161
+ import process16 from "process";
1134
1162
 
1135
1163
  // src/worktree.ts
1136
- import { cpSync, existsSync as existsSync3, lstatSync, mkdirSync as mkdirSync2, mkdtempSync, readdirSync, realpathSync as realpathSync2, rmSync, symlinkSync, unlinkSync } from "fs";
1164
+ import { cpSync, existsSync as existsSync3, lstatSync, mkdirSync as mkdirSync2, mkdtempSync, readdirSync, realpathSync as realpathSync3, rmSync, symlinkSync, unlinkSync } from "fs";
1137
1165
  import { tmpdir } from "os";
1138
- import { basename, dirname as dirname4, isAbsolute as isAbsolute2, join as join6, relative, resolve as resolve3, sep } from "path";
1139
- import process14 from "process";
1166
+ import { basename, dirname as dirname4, isAbsolute as isAbsolute2, join as join6, relative, resolve as resolve4, sep } from "path";
1167
+ import process15 from "process";
1140
1168
  var WHITESPACE = /\s/;
1141
1169
  var ORIGIN_PREFIX = /^origin\//;
1142
1170
  var ENV_FILE = /^\.env(?:\..+)?$/;
@@ -1182,12 +1210,12 @@ async function defaultProofRef(root) {
1182
1210
  return base.code === 0 && stdoutOf(base) !== stdoutOf(head) ? stdoutOf(base) : "HEAD";
1183
1211
  }
1184
1212
  function keptPaths(root, cwd, paths) {
1185
- const realRoot = realpathSync2(root);
1213
+ const realRoot = realpathSync3(root);
1186
1214
  return paths.map((path) => {
1187
- const absolute = resolve3(cwd, path);
1215
+ const absolute = resolve4(cwd, path);
1188
1216
  if (!existsSync3(dirname4(absolute)))
1189
1217
  fail(`--keep ${path}: no such file`);
1190
- const inRepo = relative(realRoot, join6(realpathSync2(dirname4(absolute)), basename(absolute)));
1218
+ const inRepo = relative(realRoot, join6(realpathSync3(dirname4(absolute)), basename(absolute)));
1191
1219
  if (inRepo === "" || inRepo === ".." || inRepo.startsWith(PARENT) || isAbsolute2(inRepo))
1192
1220
  fail(`--keep ${path} is outside the repository`);
1193
1221
  if (!existsSync3(absolute))
@@ -1260,7 +1288,7 @@ function moduleLinks(root, folder) {
1260
1288
  }
1261
1289
  function workspaceTarget(root, path) {
1262
1290
  try {
1263
- const inRepo = relative(realpathSync2(root), realpathSync2(join6(root, path)));
1291
+ const inRepo = relative(realpathSync3(root), realpathSync3(join6(root, path)));
1264
1292
  return inRepo === "" || inRepo.startsWith(PARENT) || inRepo.split(sep).includes("node_modules") ? undefined : inRepo;
1265
1293
  } catch {
1266
1294
  return;
@@ -1292,7 +1320,7 @@ async function sweepStale(root) {
1292
1320
  continue;
1293
1321
  const path = join6(tmpdir(), name);
1294
1322
  const stat = lstatSync(path, { throwIfNoEntry: false });
1295
- if (stat?.isDirectory() === true && stat.uid === process14.getuid?.() && Date.now() - stat.mtimeMs > STALE_AFTER)
1323
+ if (stat?.isDirectory() === true && stat.uid === process15.getuid?.() && Date.now() - stat.mtimeMs > STALE_AFTER)
1296
1324
  rmSync(path, { recursive: true, force: true });
1297
1325
  }
1298
1326
  await git2(["worktree", "prune"], root);
@@ -1412,7 +1440,7 @@ ${USAGE}`);
1412
1440
  if (runsInContainer(given))
1413
1441
  fail("a container of the compose project mounts the main checkout, not the temporary worktree, so the command would not run at the ref: run the tool on the host, or check the ref out by hand");
1414
1442
  const command = shellCommand(given);
1415
- const cwd = process15.cwd();
1443
+ const cwd = process16.cwd();
1416
1444
  const { root, prefix } = await checkout(cwd);
1417
1445
  const runs = wholeNumber(parsed.values.runs, "runs", 1, 1);
1418
1446
  const ref = parsed.values.ref ?? await defaultProofRef(root);
@@ -1447,7 +1475,7 @@ ${folded(run.output)}`}`);
1447
1475
  printJson({ outcome, proved, ref, sha, before: { code: before.code, output: before.output }, now: { code: now.code, output: now.output }, runs: nowRuns.map((run) => ({ code: run.code })) });
1448
1476
  return proved ? 0 : 1;
1449
1477
  }
1450
- console.log(role(proved ? "ok" : "problem", process15.stdout)(message));
1478
+ console.log(role(proved ? "ok" : "problem", process16.stdout)(message));
1451
1479
  if (runs > 1)
1452
1480
  console.log(`now: ${codes.map((code, i) => `run ${i + 1} ${code === 0 ? "passed" : `failed (exit ${code})`}`).join(", ")}`);
1453
1481
  const shown = side === "before" ? before : now;
@@ -1458,4 +1486,4 @@ ${folded(shown.output)}`);
1458
1486
  }
1459
1487
 
1460
1488
  // src/proof-cli.ts
1461
- process16.exitCode = await runMain({ name: "proof", run: proof });
1489
+ process17.exitCode = await runMain({ name: "proof", run: proof });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kaynooo/cli-base",
3
3
  "type": "module",
4
- "version": "0.9.0",
4
+ "version": "0.10.0",
5
5
  "description": "base: a command run at HEAD or another ref in a temporary worktree, the working tree left untouched.",
6
6
  "author": "Kaynooo <contact@kaynooo.fr>",
7
7
  "license": "MIT",
@@ -38,6 +38,6 @@
38
38
  "proof": "$npm_execpath src/proof-cli.ts"
39
39
  },
40
40
  "devDependencies": {
41
- "@kaynooo/cli-kit": "0.35.0"
41
+ "@kaynooo/cli-kit": "0.38.2"
42
42
  }
43
43
  }