@kaynooo/cli-base 0.12.1 → 0.12.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/cli.js +113 -45
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# base
|
|
2
2
|
|
|
3
|
-
Runs a command at HEAD or another ref for Claude Code, where a session would otherwise reach for `git stash` to see whether a test fails without its change: `base` checks the ref out in a temporary worktree, runs the command there and removes it, the working tree
|
|
3
|
+
Runs a command at HEAD or another ref for Claude Code, where a session would otherwise reach for `git stash` to see whether a test fails without its change: `base` checks the ref out in a temporary worktree, runs the command there and removes it, the working tree's changes left out of the run.
|
|
4
4
|
|
|
5
5
|
```sh
|
|
6
6
|
bun install -g @kaynooo/cli-base
|
|
@@ -11,7 +11,7 @@ base --keep tests/NewTest.php -- vendor/bin/phpunit tests/NewTest.php
|
|
|
11
11
|
|
|
12
12
|
- The ref is HEAD unless `--ref <rev>` names another commit; the command runs in the caller's folder of the worktree, `api/` in `api/`.
|
|
13
13
|
- `--keep <path>` (repeatable) copies a working-tree file or folder over, such as the new test of a red-then-green check; a path missing or outside the repository is refused before anything is created.
|
|
14
|
-
- The env files (`.env.local`, `.env.test.local`), `node_modules`, `vendor` and `.venv` the checkout has but git ignores are symlinked into the worktree, at any depth, so the run finds its dependencies and settings
|
|
14
|
+
- The env files (`.env.local`, `.env.test.local`), `node_modules`, `vendor` and `.venv` the checkout has but git ignores are symlinked into the worktree, at any depth, so the run finds its dependencies and settings: they are the working tree's own, and a write to them lands there. `--no-link` leaves them out for a fully separate run. Other ignored files (build output, caches) are not, so the ref's own are rebuilt; `--keep` copies one a run needs.
|
|
15
15
|
- The worktree is removed afterwards, on failure and on SIGINT, SIGTERM or SIGHUP too (passed on to the command), its links unlinked first so nothing is deleted through them.
|
|
16
16
|
- One argument holding spaces runs through `sh -c`: `base -- 'bun test a.test.ts | tail -5'`.
|
|
17
17
|
- A command run through `docker compose exec|run` or `docker exec` is refused: the container mounts the main checkout, not the temporary worktree, so it would not run at the ref.
|
|
@@ -20,7 +20,7 @@ base --keep tests/NewTest.php -- vendor/bin/phpunit tests/NewTest.php
|
|
|
20
20
|
|
|
21
21
|
## base proof
|
|
22
22
|
|
|
23
|
-
`base proof` shows that a test fails before a change and passes after it, the check `base` makes by hand: it runs the command at HEAD (or `--ref <rev>`) in the same temporary worktree as `base`, then in the working tree, both captured.
|
|
23
|
+
`base proof` shows that a test fails before a change and passes after it, the check `base` makes by hand: it runs the command at the merge base with origin's default branch when HEAD is on a branch ahead of it, else at HEAD (or `--ref <rev>`), in the same temporary worktree as `base`, then in the working tree, both captured.
|
|
24
24
|
|
|
25
25
|
```sh
|
|
26
26
|
base proof --keep tests/NewTest.php -- vendor/bin/phpunit tests/NewTest.php
|
package/dist/cli.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
#!/
|
|
2
|
-
// @bun
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
// @bun 2>/dev/null; exec bun --no-env-file --config=/dev/null "$0" "$@"
|
|
3
3
|
var __esm = (fn, res, err) => () => {
|
|
4
4
|
if (fn)
|
|
5
5
|
try {
|
|
@@ -116,11 +116,14 @@ var init_config = __esm(() => {
|
|
|
116
116
|
|
|
117
117
|
// ../kit/src/user-config.ts
|
|
118
118
|
import { homedir } from "os";
|
|
119
|
-
import { join as join2 } from "path";
|
|
119
|
+
import { isAbsolute, join as join2 } from "path";
|
|
120
120
|
import process4 from "process";
|
|
121
|
+
function xdgFolder(value, fallback) {
|
|
122
|
+
return value !== undefined && isAbsolute(value) ? value : fallback;
|
|
123
|
+
}
|
|
121
124
|
function configBase(where = {}) {
|
|
122
125
|
const xdg = where.xdg ?? (where.home === undefined ? process4.env.XDG_CONFIG_HOME : undefined);
|
|
123
|
-
return xdg
|
|
126
|
+
return xdgFolder(xdg, join2(where.home ?? homedir(), ".config"));
|
|
124
127
|
}
|
|
125
128
|
function userConfigPath(bin, where = {}) {
|
|
126
129
|
return join2(configBase(where), "kaynooo-cli", `${bin}.json`);
|
|
@@ -497,21 +500,32 @@ function wholeNumber(value, name, fallback, min = 1) {
|
|
|
497
500
|
badUsage(`${name.startsWith("<") ? name : `--${name}`} takes a whole number from ${min}, not ${value}`);
|
|
498
501
|
return number;
|
|
499
502
|
}
|
|
503
|
+
function durationMs(text, unit) {
|
|
504
|
+
if (BARE_DURATION.test(text))
|
|
505
|
+
return Number(text) * UNIT_MS[unit];
|
|
506
|
+
if (!DURATION.test(text))
|
|
507
|
+
return 0;
|
|
508
|
+
let ms = 0;
|
|
509
|
+
for (const [, amount, part] of text.matchAll(DURATION_PART))
|
|
510
|
+
ms += Number(amount) * UNIT_MS[part];
|
|
511
|
+
return ms;
|
|
512
|
+
}
|
|
500
513
|
function duration(value, name, fallback, unit = "s") {
|
|
501
514
|
if (value === undefined)
|
|
502
515
|
return fallback;
|
|
503
|
-
const
|
|
504
|
-
const ms = match === null ? 0 : Number(match[1]) * UNIT_MS[match[2] ?? unit];
|
|
516
|
+
const ms = durationMs(value.trim(), unit);
|
|
505
517
|
if (ms < 1)
|
|
506
518
|
badUsage(`--${name} takes a duration such as 30s, 5m, 1h or 7d, not ${value}`);
|
|
507
519
|
return ms;
|
|
508
520
|
}
|
|
509
|
-
var WHOLE, DURATION, UNIT_MS;
|
|
521
|
+
var WHOLE, BARE_DURATION, DURATION, DURATION_PART, UNIT_MS;
|
|
510
522
|
var init_input = __esm(() => {
|
|
511
523
|
init_args();
|
|
512
524
|
WHOLE = /^\d+$/;
|
|
513
|
-
|
|
514
|
-
|
|
525
|
+
BARE_DURATION = /^\d+(?:\.\d+)?$/;
|
|
526
|
+
DURATION = /^(?:\d+(?:\.\d+)?(?:ms|min|[smhd]))+$/;
|
|
527
|
+
DURATION_PART = /(\d+(?:\.\d+)?)(ms|min|[smhd])/g;
|
|
528
|
+
UNIT_MS = { ms: 1, s: 1000, m: 60000, min: 60000, h: 3600000, d: 86400000 };
|
|
515
529
|
});
|
|
516
530
|
|
|
517
531
|
// ../kit/src/args.ts
|
|
@@ -797,10 +811,12 @@ init_format();
|
|
|
797
811
|
init_format();
|
|
798
812
|
|
|
799
813
|
// ../kit/src/process.ts
|
|
814
|
+
import { readFileSync as readFileSync3 } from "fs";
|
|
800
815
|
import { constants } from "os";
|
|
801
816
|
import process11 from "process";
|
|
802
817
|
var KILL_GRACE_MS = 2000;
|
|
803
818
|
var SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
819
|
+
var GROUP_SIGNALS = new Set(["SIGINT", "SIGHUP"]);
|
|
804
820
|
function exitCodeOf(code, signal) {
|
|
805
821
|
if (code !== null)
|
|
806
822
|
return code;
|
|
@@ -808,6 +824,21 @@ function exitCodeOf(code, signal) {
|
|
|
808
824
|
return 128 + signal;
|
|
809
825
|
return signal === null ? 129 : 128 + constants.signals[signal];
|
|
810
826
|
}
|
|
827
|
+
function groupsOf(pid) {
|
|
828
|
+
try {
|
|
829
|
+
const stat = readFileSync3(`/proc/${pid}/stat`, "utf8");
|
|
830
|
+
const fields = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
|
|
831
|
+
return { group: Number(fields[2]), foreground: Number(fields[5]) };
|
|
832
|
+
} catch {
|
|
833
|
+
return;
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
function heardTheTerminal(child) {
|
|
837
|
+
if (child.pid === undefined)
|
|
838
|
+
return false;
|
|
839
|
+
const own = groupsOf("self");
|
|
840
|
+
return own !== undefined && own.group === own.foreground && groupsOf(child.pid)?.group === own.group;
|
|
841
|
+
}
|
|
811
842
|
function holdSignals(cleanup) {
|
|
812
843
|
let received;
|
|
813
844
|
let child;
|
|
@@ -826,7 +857,8 @@ function holdSignals(cleanup) {
|
|
|
826
857
|
function onSignal(signal) {
|
|
827
858
|
received = signal;
|
|
828
859
|
if (child !== undefined) {
|
|
829
|
-
|
|
860
|
+
if (!GROUP_SIGNALS.has(signal) || !heardTheTerminal(child))
|
|
861
|
+
child.kill(signal);
|
|
830
862
|
return;
|
|
831
863
|
}
|
|
832
864
|
if (cleanup === undefined)
|
|
@@ -882,12 +914,19 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", input, timeoutMs,
|
|
|
882
914
|
stop();
|
|
883
915
|
continue;
|
|
884
916
|
}
|
|
917
|
+
if (state.truncated)
|
|
918
|
+
continue;
|
|
919
|
+
let kept = data;
|
|
885
920
|
if (state.kept + data.length > maxBytes) {
|
|
921
|
+
let cut = maxBytes - state.kept;
|
|
922
|
+
while (cut > 0 && ((data[cut] ?? 0) & 192) === 128)
|
|
923
|
+
cut--;
|
|
924
|
+
kept = data.subarray(0, cut);
|
|
886
925
|
state.truncated = true;
|
|
887
|
-
continue;
|
|
888
926
|
}
|
|
889
|
-
state.kept +=
|
|
890
|
-
|
|
927
|
+
state.kept += kept.length;
|
|
928
|
+
if (kept.length > 0)
|
|
929
|
+
chunks.push({ stream: name, data: kept });
|
|
891
930
|
}
|
|
892
931
|
};
|
|
893
932
|
try {
|
|
@@ -904,6 +943,7 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", input, timeoutMs,
|
|
|
904
943
|
const joined = () => Buffer.concat(chunks.map(({ data }) => data));
|
|
905
944
|
let output;
|
|
906
945
|
let text;
|
|
946
|
+
let stdout;
|
|
907
947
|
return {
|
|
908
948
|
code: exitCodeOf(proc.exitCode, proc.signalCode),
|
|
909
949
|
chunks,
|
|
@@ -914,6 +954,9 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", input, timeoutMs,
|
|
|
914
954
|
},
|
|
915
955
|
get text() {
|
|
916
956
|
return text ??= (output ??= joined()).toString();
|
|
957
|
+
},
|
|
958
|
+
get stdout() {
|
|
959
|
+
return stdout ??= Buffer.concat(chunks.filter((chunk) => chunk.stream === "stdout").map((chunk) => chunk.data)).toString();
|
|
917
960
|
}
|
|
918
961
|
};
|
|
919
962
|
}
|
|
@@ -933,7 +976,7 @@ init_style();
|
|
|
933
976
|
init_format();
|
|
934
977
|
// ../kit/src/cli.ts
|
|
935
978
|
init_args();
|
|
936
|
-
import { readFileSync as
|
|
979
|
+
import { readFileSync as readFileSync4, realpathSync as realpathSync2 } from "fs";
|
|
937
980
|
import { dirname as dirname3, join as join5 } from "path";
|
|
938
981
|
import process15 from "process";
|
|
939
982
|
|
|
@@ -1063,14 +1106,16 @@ init_text();
|
|
|
1063
1106
|
// ../kit/src/usage.ts
|
|
1064
1107
|
import { appendFileSync, mkdirSync } from "fs";
|
|
1065
1108
|
import { homedir as homedir3 } from "os";
|
|
1066
|
-
import { dirname as dirname2,
|
|
1109
|
+
import { dirname as dirname2, join as join4 } from "path";
|
|
1067
1110
|
import process14 from "process";
|
|
1068
1111
|
|
|
1069
1112
|
// ../kit/src/logs.ts
|
|
1113
|
+
init_user_config();
|
|
1070
1114
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
1071
1115
|
var CLI_FOLDER = "kaynooo-cli";
|
|
1072
1116
|
|
|
1073
1117
|
// ../kit/src/usage.ts
|
|
1118
|
+
init_user_config();
|
|
1074
1119
|
var UNKNOWN_OPTION2 = /unknown option '(--[a-z][a-z0-9-]*)[=']/;
|
|
1075
1120
|
var FLAG_NAME = /^(?:--[a-z][a-z0-9-]*|-[a-zA-Z])$/;
|
|
1076
1121
|
var LONGEST_NAME = 40;
|
|
@@ -1079,8 +1124,7 @@ function noteUsageError(message) {
|
|
|
1079
1124
|
usageError = message;
|
|
1080
1125
|
}
|
|
1081
1126
|
function stateHome(env = process14.env) {
|
|
1082
|
-
|
|
1083
|
-
return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
|
|
1127
|
+
return xdgFolder(env.XDG_STATE_HOME, join4(homedir3(), ".local", "state"));
|
|
1084
1128
|
}
|
|
1085
1129
|
function usagePath(env = process14.env) {
|
|
1086
1130
|
return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
@@ -1154,6 +1198,10 @@ function takeOutputFlags(argv) {
|
|
|
1154
1198
|
async function resolveSummary(summary) {
|
|
1155
1199
|
return typeof summary === "string" ? summary : summary();
|
|
1156
1200
|
}
|
|
1201
|
+
function lookup({ commands, aliases }, word) {
|
|
1202
|
+
const name = aliases !== undefined && Object.hasOwn(aliases, word) ? aliases[word] ?? word : word;
|
|
1203
|
+
return Object.hasOwn(commands, name) ? commands[name] : undefined;
|
|
1204
|
+
}
|
|
1157
1205
|
function scopeOf(commands, extra, aliases) {
|
|
1158
1206
|
return { commands: typeof commands === "function" ? {} : commands, lazy: [...typeof commands === "function" ? [commands] : [], ...extra === undefined ? [] : [extra]], aliases };
|
|
1159
1207
|
}
|
|
@@ -1248,12 +1296,12 @@ async function dispatch(level, typed, { version, redact }) {
|
|
|
1248
1296
|
console.log(version);
|
|
1249
1297
|
return 0;
|
|
1250
1298
|
}
|
|
1251
|
-
let command = level
|
|
1299
|
+
let command = lookup(level, name);
|
|
1252
1300
|
let rest = args;
|
|
1253
1301
|
let ran = name;
|
|
1254
1302
|
if (command === undefined && level.lazy.length > 0) {
|
|
1255
1303
|
level = await expand(level, [...level.path.slice(1), ...argv]);
|
|
1256
|
-
command = level
|
|
1304
|
+
command = lookup(level, name);
|
|
1257
1305
|
}
|
|
1258
1306
|
if (command === undefined && level.defaultCommand !== undefined) {
|
|
1259
1307
|
command = level.commands[level.defaultCommand];
|
|
@@ -1333,18 +1381,19 @@ async function leafCandidates(run, words) {
|
|
|
1333
1381
|
async function candidates(scope, defaultCommand, words, before = []) {
|
|
1334
1382
|
const [word = "", ...rest] = words;
|
|
1335
1383
|
const listing = rest.length === 0 && !word.startsWith("-");
|
|
1336
|
-
let { commands
|
|
1337
|
-
let command =
|
|
1384
|
+
let { commands } = scope;
|
|
1385
|
+
let command = lookup(scope, word);
|
|
1338
1386
|
if (listing || command === undefined) {
|
|
1339
|
-
|
|
1340
|
-
|
|
1387
|
+
const expanded = await expand(scope, [...before, ...words]);
|
|
1388
|
+
commands = expanded.commands;
|
|
1389
|
+
command = lookup(expanded, word);
|
|
1341
1390
|
}
|
|
1342
1391
|
if (listing) {
|
|
1343
1392
|
const own = await Promise.all(Object.entries(shown(commands)).map(async ([name, { summary }]) => `${name} ${await resolveSummary(summary)}`));
|
|
1344
1393
|
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
1345
1394
|
const values = fallback !== undefined && "run" in fallback ? await leafCandidates(fallback.run, [word]) : [];
|
|
1346
1395
|
const valueOf = (line) => line.slice(0, line.includes("\t") ? line.indexOf("\t") : undefined);
|
|
1347
|
-
return [...own, ...values.filter((line) => line !== FILES && !(valueOf(line)
|
|
1396
|
+
return [...own, ...values.filter((line) => line !== FILES && !Object.hasOwn(commands, valueOf(line)))];
|
|
1348
1397
|
}
|
|
1349
1398
|
if (command === undefined) {
|
|
1350
1399
|
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
@@ -1377,7 +1426,7 @@ function packageVersion(entry = Bun.main) {
|
|
|
1377
1426
|
} catch {}
|
|
1378
1427
|
for (let dir = dirname3(resolved);; dir = dirname3(dir)) {
|
|
1379
1428
|
try {
|
|
1380
|
-
const { version } = JSON.parse(
|
|
1429
|
+
const { version } = JSON.parse(readFileSync4(join5(dir, "package.json"), "utf8"));
|
|
1381
1430
|
if (typeof version === "string")
|
|
1382
1431
|
return version;
|
|
1383
1432
|
} catch {}
|
|
@@ -1409,8 +1458,7 @@ function commandPath(scope, argv) {
|
|
|
1409
1458
|
const path = [];
|
|
1410
1459
|
let level = scope;
|
|
1411
1460
|
for (const word of argv.filter((each) => !each.startsWith("-"))) {
|
|
1412
|
-
const
|
|
1413
|
-
const command = Object.hasOwn(level.commands, name) ? level.commands[name] : undefined;
|
|
1461
|
+
const command = lookup(level, word);
|
|
1414
1462
|
if (command === undefined)
|
|
1415
1463
|
break;
|
|
1416
1464
|
path.push(word);
|
|
@@ -1444,6 +1492,7 @@ init_style();
|
|
|
1444
1492
|
init_text();
|
|
1445
1493
|
// ../kit/src/secrets.ts
|
|
1446
1494
|
var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "session", "jwt", "ssn", "iban", "card", "cvv", "otp"]);
|
|
1495
|
+
var NOT_PASSWORDS = new Set(["compass", "encompass", "bypass", "overpass", "underpass", "surpass", "trespass"]);
|
|
1447
1496
|
// ../kit/src/sessions.ts
|
|
1448
1497
|
var {Glob } = globalThis.Bun;
|
|
1449
1498
|
var SESSION_FILES = new Glob("*.json");
|
|
@@ -1465,7 +1514,7 @@ init_user_config();
|
|
|
1465
1514
|
import process18 from "process";
|
|
1466
1515
|
|
|
1467
1516
|
// src/worktree.ts
|
|
1468
|
-
import { cpSync, existsSync as existsSync3, lstatSync, mkdirSync as mkdirSync2, mkdtempSync, readdirSync, realpathSync as realpathSync3, rmSync, symlinkSync, unlinkSync } from "fs";
|
|
1517
|
+
import { chmodSync, cpSync, existsSync as existsSync3, lstatSync, mkdirSync as mkdirSync2, mkdtempSync, readdirSync, realpathSync as realpathSync3, rmSync, symlinkSync, unlinkSync } from "fs";
|
|
1469
1518
|
import { tmpdir } from "os";
|
|
1470
1519
|
import { basename, dirname as dirname4, isAbsolute as isAbsolute2, join as join6, relative, resolve as resolve4, sep } from "path";
|
|
1471
1520
|
import process17 from "process";
|
|
@@ -1477,9 +1526,6 @@ var TRAILING_SLASH = /\/$/;
|
|
|
1477
1526
|
var PARENT = `..${sep}`;
|
|
1478
1527
|
var BASE_DIR = /^base-\w{6}$/;
|
|
1479
1528
|
var CONTAINER = /(?:^|[\s;&|(])(?:docker\s+compose|docker-compose)\s+(?:-\S+(?:\s+[^-\s]\S*)?\s+)*(?:exec|run)\b|(?:^|[\s;&|(])docker\s+exec\b/;
|
|
1480
|
-
function stdoutOf(ran) {
|
|
1481
|
-
return Buffer.concat(ran.chunks.filter((chunk) => chunk.stream === "stdout").map((chunk) => chunk.data)).toString().trim();
|
|
1482
|
-
}
|
|
1483
1529
|
async function git2(args, cwd) {
|
|
1484
1530
|
return runCommand(["git", ...args], { cwd });
|
|
1485
1531
|
}
|
|
@@ -1490,7 +1536,7 @@ async function checkout(cwd) {
|
|
|
1490
1536
|
const ran = await git2(["rev-parse", "--show-toplevel", "--show-prefix"], cwd);
|
|
1491
1537
|
if (ran.code !== 0)
|
|
1492
1538
|
fail("not inside a git repository");
|
|
1493
|
-
const [root = "", prefix = ""] =
|
|
1539
|
+
const [root = "", prefix = ""] = ran.stdout.trim().split(`
|
|
1494
1540
|
`);
|
|
1495
1541
|
return { root, prefix };
|
|
1496
1542
|
}
|
|
@@ -1498,20 +1544,20 @@ async function resolveRef(root, ref) {
|
|
|
1498
1544
|
const ran = await git2(["rev-parse", "--verify", "--quiet", "--end-of-options", `${ref}^{commit}`], root);
|
|
1499
1545
|
if (ran.code !== 0)
|
|
1500
1546
|
fail(`no commit ${ref} in this repository`);
|
|
1501
|
-
return
|
|
1547
|
+
return ran.stdout.trim();
|
|
1502
1548
|
}
|
|
1503
1549
|
async function defaultRemoteRef(root) {
|
|
1504
1550
|
const head = await git2(["symbolic-ref", "--quiet", "--short", "refs/remotes/origin/HEAD"], root);
|
|
1505
|
-
return head.code === 0 &&
|
|
1551
|
+
return head.code === 0 && head.stdout.trim() !== "" ? head.stdout.trim() : "origin/main";
|
|
1506
1552
|
}
|
|
1507
1553
|
async function defaultProofRef(root) {
|
|
1508
1554
|
const branch = await git2(["symbolic-ref", "--quiet", "--short", "HEAD"], root);
|
|
1509
1555
|
const remote = await defaultRemoteRef(root);
|
|
1510
|
-
if (branch.code !== 0 ||
|
|
1556
|
+
if (branch.code !== 0 || branch.stdout.trim() === remote.replace(ORIGIN_PREFIX, ""))
|
|
1511
1557
|
return "HEAD";
|
|
1512
1558
|
const base = await git2(["merge-base", "HEAD", remote], root);
|
|
1513
1559
|
const head = await git2(["rev-parse", "HEAD"], root);
|
|
1514
|
-
return base.code === 0 &&
|
|
1560
|
+
return base.code === 0 && base.stdout.trim() !== head.stdout.trim() ? base.stdout.trim() : "HEAD";
|
|
1515
1561
|
}
|
|
1516
1562
|
function keptPaths(root, cwd, paths) {
|
|
1517
1563
|
const realRoot = realpathSync3(root);
|
|
@@ -1539,7 +1585,7 @@ async function linkedEntries(root) {
|
|
|
1539
1585
|
const ran = await git2(["ls-files", "--others", "--ignored", "--exclude-standard", "--directory", "-z"], root);
|
|
1540
1586
|
if (ran.code !== 0)
|
|
1541
1587
|
return [];
|
|
1542
|
-
return
|
|
1588
|
+
return ran.stdout.trim().split("\x00").filter((entry) => {
|
|
1543
1589
|
const folder = entry.endsWith("/");
|
|
1544
1590
|
const name = basename(entry);
|
|
1545
1591
|
return folder ? DEPENDENCIES.has(name) : ENV_FILE.test(name);
|
|
@@ -1618,6 +1664,26 @@ function linkModules(root, dir, folder) {
|
|
|
1618
1664
|
return links;
|
|
1619
1665
|
}
|
|
1620
1666
|
var STALE_AFTER = 24 * 60 * 60 * 1000;
|
|
1667
|
+
function makeWritable(path) {
|
|
1668
|
+
const stat = lstatSync(path, { throwIfNoEntry: false });
|
|
1669
|
+
if (stat?.isDirectory() !== true)
|
|
1670
|
+
return;
|
|
1671
|
+
chmodSync(path, stat.mode | 448);
|
|
1672
|
+
for (const entry of readdirSync(path))
|
|
1673
|
+
makeWritable(join6(path, entry));
|
|
1674
|
+
}
|
|
1675
|
+
function removeTree(path) {
|
|
1676
|
+
try {
|
|
1677
|
+
rmSync(path, { recursive: true, force: true });
|
|
1678
|
+
} catch {
|
|
1679
|
+
try {
|
|
1680
|
+
makeWritable(path);
|
|
1681
|
+
rmSync(path, { recursive: true, force: true });
|
|
1682
|
+
} catch (error) {
|
|
1683
|
+
console.error(`${role("warn", process17.stderr)("base:")} could not remove ${path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
1684
|
+
}
|
|
1685
|
+
}
|
|
1686
|
+
}
|
|
1621
1687
|
async function sweepStale(root) {
|
|
1622
1688
|
for (const name of readdirSync(tmpdir())) {
|
|
1623
1689
|
if (!BASE_DIR.test(name))
|
|
@@ -1625,7 +1691,7 @@ async function sweepStale(root) {
|
|
|
1625
1691
|
const path = join6(tmpdir(), name);
|
|
1626
1692
|
const stat = lstatSync(path, { throwIfNoEntry: false });
|
|
1627
1693
|
if (stat?.isDirectory() === true && stat.uid === process17.getuid?.() && Date.now() - stat.mtimeMs > STALE_AFTER)
|
|
1628
|
-
|
|
1694
|
+
removeTree(path);
|
|
1629
1695
|
}
|
|
1630
1696
|
await git2(["worktree", "prune"], root);
|
|
1631
1697
|
}
|
|
@@ -1635,7 +1701,7 @@ async function removeWorktree(root, dir, links) {
|
|
|
1635
1701
|
unlinkSync(link);
|
|
1636
1702
|
}
|
|
1637
1703
|
await git2(["worktree", "remove", "--force", dir], root);
|
|
1638
|
-
|
|
1704
|
+
removeTree(dirname4(dir));
|
|
1639
1705
|
await git2(["worktree", "prune"], root);
|
|
1640
1706
|
}
|
|
1641
1707
|
function shellCommand(given) {
|
|
@@ -1702,7 +1768,9 @@ async function inWorktree(options, body) {
|
|
|
1702
1768
|
}
|
|
1703
1769
|
|
|
1704
1770
|
// src/base.ts
|
|
1705
|
-
var USAGE = `base [--ref <rev>] [--keep <path>...] [--no-link] -- <command> [args...]: runs the command at HEAD or another ref in a temporary worktree,
|
|
1771
|
+
var USAGE = `base [--ref <rev>] [--keep <path>...] [--no-link] -- <command> [args...]: runs the command at HEAD or another ref in a temporary worktree, exiting with its code
|
|
1772
|
+
|
|
1773
|
+
The changes of the working tree stay out of the run, but its ignored env files, node_modules, vendor and .venv are linked in, so a write to them lands in the working tree; --no-link runs fully apart.
|
|
1706
1774
|
|
|
1707
1775
|
Exits with the code of the command, 127 when it cannot be started, 2 on a usage error, an unknown ref or a refused command.`;
|
|
1708
1776
|
var OPTIONS = {
|
|
@@ -1735,9 +1803,9 @@ ${USAGE}`);
|
|
|
1735
1803
|
|
|
1736
1804
|
// src/proof.ts
|
|
1737
1805
|
import process19 from "process";
|
|
1738
|
-
var USAGE2 = `base proof [--ref <rev>] [--runs <n>] [--keep <path>...] [--timeout <duration>] [--no-link] [--json] -- <command> [args...]: runs the command at
|
|
1806
|
+
var USAGE2 = `base proof [--ref <rev>] [--runs <n>] [--keep <path>...] [--timeout <duration>] [--no-link] [--json] -- <command> [args...]: runs the command at the ref below in a temporary worktree, then in the working tree, to show that a test fails before a change and passes after it
|
|
1739
1807
|
|
|
1740
|
-
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. --timeout kills a run that lasts longer (a hung test), which says nothing about the change and ends with exit 2.`;
|
|
1808
|
+
The ignored env files, node_modules, vendor and .venv of the working tree are linked into the temporary worktree, writes included; --no-link runs fully apart. 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. --timeout kills a run that lasts longer (a hung test), which says nothing about the change and ends with exit 2.`;
|
|
1741
1809
|
var EXITS = [[0, "the command fails before and passes in every run now"], [1, "it passes before too, fails in any run now, or passes before and fails now"], [2, "a usage error, an unknown ref, a command that cannot start, ends with exit 126, 127, 125 or a signal, or runs past --timeout"], [130, "interrupted (128 plus the signal)"]];
|
|
1742
1810
|
var OPTIONS2 = {
|
|
1743
1811
|
ref: { type: "string", short: "r", description: "the commit to run before at (default: the merge base with the default branch of origin on a branch ahead of it, else HEAD)" },
|
|
@@ -1838,12 +1906,12 @@ ${folded(shown.output)}`);
|
|
|
1838
1906
|
// src/cli.ts
|
|
1839
1907
|
process20.exitCode = await runCli({
|
|
1840
1908
|
name: "base",
|
|
1841
|
-
about: "runs a command at HEAD or another ref in a temporary worktree, the working tree
|
|
1909
|
+
about: "runs a command at HEAD or another ref in a temporary worktree, sharing node_modules and env files with the working tree unless --no-link",
|
|
1842
1910
|
replaces: "git stash, a test run, then git stash pop",
|
|
1843
|
-
examples: [["base -- bun test tests/api.test.ts", "the test at HEAD, the working tree changes left out"], ["base proof -- bun test tests/new.test.ts", "it fails
|
|
1911
|
+
examples: [["base -- bun test tests/api.test.ts", "the test at HEAD, the working tree changes left out"], ["base proof -- bun test tests/new.test.ts", "it fails before the change and passes in the working tree"]],
|
|
1844
1912
|
commands: {
|
|
1845
1913
|
run: { summary: "the command at HEAD or --ref, exiting with its code (the command when none is named)", run: base },
|
|
1846
|
-
proof: { summary: "the command at HEAD or --ref, then in the working tree: proves a test fails before a change and passes after it", run: proof }
|
|
1914
|
+
proof: { summary: "the command at the merge base with origin's default branch on a branch ahead of it (else HEAD, or --ref), then in the working tree: proves a test fails before a change and passes after it", run: proof }
|
|
1847
1915
|
},
|
|
1848
1916
|
defaultCommand: "run"
|
|
1849
1917
|
});
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-base",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.12.
|
|
5
|
-
"description": "base: a command run at HEAD or another ref in a temporary worktree, the working tree
|
|
4
|
+
"version": "0.12.3",
|
|
5
|
+
"description": "base: a command run at HEAD or another ref in a temporary worktree, sharing node_modules and env files with the working tree.",
|
|
6
6
|
"author": "Kaynooo <contact@kaynooo.fr>",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"homepage": "https://github.com/Kayno0o/cli/tree/main/packages/base#readme",
|
|
@@ -32,10 +32,10 @@
|
|
|
32
32
|
"registry": "https://git.kaynooo.fr/api/packages/kaynooo/npm/"
|
|
33
33
|
},
|
|
34
34
|
"scripts": {
|
|
35
|
-
"build": "$npm_execpath build src/cli.ts --target bun --outdir dist",
|
|
35
|
+
"build": "$npm_execpath build src/cli.ts --target bun --outdir dist && $npm_execpath ../../scripts/shell-shebang.ts",
|
|
36
36
|
"base": "$npm_execpath src/cli.ts"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@kaynooo/cli-kit": "0.
|
|
39
|
+
"@kaynooo/cli-kit": "0.46.4"
|
|
40
40
|
}
|
|
41
41
|
}
|