@kaynooo/cli-code 0.14.2 → 0.14.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/dist/cli.js +85 -65
- package/dist/hotspots-cli.js +67 -45
- package/dist/peek-cli.js +72 -50
- package/dist/todo-scan-cli.js +64 -42
- package/package.json +2 -2
package/dist/cli.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/cli.ts
|
|
5
|
-
import
|
|
5
|
+
import process18 from "process";
|
|
6
6
|
// ../kit/src/args.ts
|
|
7
7
|
import process9 from "process";
|
|
8
8
|
import { parseArgs } from "util";
|
|
@@ -488,18 +488,14 @@ var FULL_STOP = /\.$/;
|
|
|
488
488
|
function usageLine(usage, colors = paint(process8.stdout)) {
|
|
489
489
|
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
490
490
|
}
|
|
491
|
-
function helpText(usage, options, colors = paint(process8.stdout), { examples = [],
|
|
492
|
-
return [usageLine(usage, colors),
|
|
491
|
+
function helpText(usage, options, colors = paint(process8.stdout), { examples = [], exits = [] } = {}) {
|
|
492
|
+
return [usageLine(usage, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
|
|
493
493
|
`);
|
|
494
494
|
}
|
|
495
495
|
function exitRows(exits, colors = paint(process8.stdout)) {
|
|
496
496
|
if (exits.length === 0)
|
|
497
497
|
return [];
|
|
498
|
-
|
|
499
|
-
return ["", colors.bold("Exits:"), ...exits.map(([code, meaning]) => ` ${colors.cyan(String(code).padEnd(width))} ${meaning}`)];
|
|
500
|
-
}
|
|
501
|
-
function replacesRows(replaces, colors = paint(process8.stdout)) {
|
|
502
|
-
return replaces === undefined || outputMode() === "agent" ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
498
|
+
return ["", `${colors.bold("Exit codes:")} ${exits.map(([code, meaning]) => `${colors.cyan(String(code))} ${meaning}`).join(", ")}`];
|
|
503
499
|
}
|
|
504
500
|
function wantsHelpJson(env = process8.env) {
|
|
505
501
|
return env.KIT_HELP === "json";
|
|
@@ -800,9 +796,33 @@ function nearestOption(message, options, allowNegative) {
|
|
|
800
796
|
const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
|
|
801
797
|
return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
|
|
802
798
|
}
|
|
799
|
+
// ../kit/src/argv.ts
|
|
800
|
+
import { readFileSync as readFileSync3, realpathSync } from "fs";
|
|
801
|
+
import { resolve as resolve4 } from "path";
|
|
802
|
+
import process10 from "process";
|
|
803
|
+
function commandLine() {
|
|
804
|
+
try {
|
|
805
|
+
return readFileSync3("/proc/self/cmdline", "utf8").split("\x00").slice(0, -1);
|
|
806
|
+
} catch {
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
function realPath(word) {
|
|
811
|
+
try {
|
|
812
|
+
return realpathSync(resolve4(word));
|
|
813
|
+
} catch {
|
|
814
|
+
return;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
function scriptArgs(argv = process10.argv.slice(2), cmdline = commandLine(), script = process10.argv[1]) {
|
|
818
|
+
const target = script === undefined ? undefined : realPath(script) ?? script;
|
|
819
|
+
const at = cmdline === undefined || target === undefined ? -1 : cmdline.findIndex((word, index) => index > 0 && (resolve4(word) === script || realPath(word) === target));
|
|
820
|
+
const after = at === -1 ? [] : cmdline.slice(at + 1);
|
|
821
|
+
return after[0] === "--" && after.length === argv.length + 1 && argv.every((word, index) => word === after[index + 1]) ? after : argv;
|
|
822
|
+
}
|
|
803
823
|
// ../kit/src/process.ts
|
|
804
824
|
import { constants } from "os";
|
|
805
|
-
import
|
|
825
|
+
import process11 from "process";
|
|
806
826
|
var KILL_GRACE_MS = 2000;
|
|
807
827
|
function exitCodeOf(code, signal) {
|
|
808
828
|
if (code !== null)
|
|
@@ -822,7 +842,7 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal,
|
|
|
822
842
|
return;
|
|
823
843
|
try {
|
|
824
844
|
if (grouped)
|
|
825
|
-
|
|
845
|
+
process11.kill(-proc.pid, killing);
|
|
826
846
|
else
|
|
827
847
|
proc.kill(killing);
|
|
828
848
|
} catch {}
|
|
@@ -896,15 +916,15 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
|
|
|
896
916
|
var RESET_STYLE = `${ESC}[0m`;
|
|
897
917
|
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
898
918
|
// ../kit/src/cli.ts
|
|
899
|
-
import { readFileSync as
|
|
919
|
+
import { readFileSync as readFileSync4, realpathSync as realpathSync2 } from "fs";
|
|
900
920
|
import { dirname as dirname3, join as join5 } from "path";
|
|
901
|
-
import
|
|
921
|
+
import process13 from "process";
|
|
902
922
|
|
|
903
923
|
// ../kit/src/usage.ts
|
|
904
924
|
import { appendFileSync, mkdirSync } from "fs";
|
|
905
925
|
import { homedir as homedir3 } from "os";
|
|
906
926
|
import { dirname as dirname2, isAbsolute, join as join4 } from "path";
|
|
907
|
-
import
|
|
927
|
+
import process12 from "process";
|
|
908
928
|
|
|
909
929
|
// ../kit/src/logs.ts
|
|
910
930
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -918,14 +938,14 @@ var usageError;
|
|
|
918
938
|
function noteUsageError(message) {
|
|
919
939
|
usageError = message;
|
|
920
940
|
}
|
|
921
|
-
function stateHome(env =
|
|
941
|
+
function stateHome(env = process12.env) {
|
|
922
942
|
const dir = env.XDG_STATE_HOME;
|
|
923
943
|
return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
|
|
924
944
|
}
|
|
925
|
-
function usagePath(env =
|
|
945
|
+
function usagePath(env = process12.env) {
|
|
926
946
|
return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
927
947
|
}
|
|
928
|
-
function usageRecorded(env =
|
|
948
|
+
function usageRecorded(env = process12.env) {
|
|
929
949
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
930
950
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
931
951
|
}
|
|
@@ -939,7 +959,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
939
959
|
usageError = undefined;
|
|
940
960
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
941
961
|
}
|
|
942
|
-
function recordUsage(entry, env =
|
|
962
|
+
function recordUsage(entry, env = process12.env) {
|
|
943
963
|
if (!usageRecorded(env))
|
|
944
964
|
return;
|
|
945
965
|
try {
|
|
@@ -1016,7 +1036,6 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
|
|
|
1016
1036
|
header(colors, summary),
|
|
1017
1037
|
"",
|
|
1018
1038
|
usageLine(`${prefix} ${defaultCommand === undefined ? colors.cyan("<command>") : `[${colors.cyan("<command>")}]`} [options]`, colors),
|
|
1019
|
-
...replacesRows(replaces, colors),
|
|
1020
1039
|
"",
|
|
1021
1040
|
...await commandRows(commands, colors),
|
|
1022
1041
|
...own.length === 0 ? [] : ["", `${colors.bold(`Options of ${defaultCommand}:`)} ${colors.dim("the command run when none is named")}`, ...optionRows(Object.fromEntries(own), colors)],
|
|
@@ -1030,8 +1049,8 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
|
|
|
1030
1049
|
`);
|
|
1031
1050
|
}
|
|
1032
1051
|
function printError(prefix, message, hint, cause) {
|
|
1033
|
-
const colors = paint(
|
|
1034
|
-
const styled = takesControlCodes(
|
|
1052
|
+
const colors = paint(process13.stderr);
|
|
1053
|
+
const styled = takesControlCodes(process13.stderr);
|
|
1035
1054
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
1036
1055
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
1037
1056
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -1074,11 +1093,11 @@ async function dispatch(level, typed, { version, redact }) {
|
|
|
1074
1093
|
}
|
|
1075
1094
|
}
|
|
1076
1095
|
if (name === undefined && level.path.length > 1) {
|
|
1077
|
-
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
1096
|
+
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process13.stderr));
|
|
1078
1097
|
return 2;
|
|
1079
1098
|
}
|
|
1080
1099
|
if (name === undefined || name === "help" || name === "--help" || name === "-h") {
|
|
1081
|
-
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
1100
|
+
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process13.stdout));
|
|
1082
1101
|
return 0;
|
|
1083
1102
|
}
|
|
1084
1103
|
if (level.path.length === 1 && (name === "--version" || name === "-v")) {
|
|
@@ -1134,24 +1153,24 @@ function candidateLine(candidate) {
|
|
|
1134
1153
|
}
|
|
1135
1154
|
function positionalBeingTyped(before, { options, positionals }) {
|
|
1136
1155
|
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
1137
|
-
|
|
1156
|
+
const typed = [];
|
|
1138
1157
|
let at = 0;
|
|
1139
1158
|
while (at < before.length) {
|
|
1140
1159
|
const word = before[at];
|
|
1141
1160
|
if (word === "--") {
|
|
1142
|
-
|
|
1161
|
+
typed.push(...before.slice(at + 1));
|
|
1143
1162
|
break;
|
|
1144
1163
|
}
|
|
1145
1164
|
if (!word.startsWith("-"))
|
|
1146
|
-
|
|
1165
|
+
typed.push(word);
|
|
1147
1166
|
else if (takesValue(word))
|
|
1148
1167
|
at++;
|
|
1149
1168
|
at++;
|
|
1150
1169
|
}
|
|
1151
1170
|
const last = positionals.at(-1);
|
|
1152
|
-
if (
|
|
1153
|
-
return positionals[
|
|
1154
|
-
return last?.replace("]", "").endsWith("...") === true ? last : undefined;
|
|
1171
|
+
if (typed.length < positionals.length)
|
|
1172
|
+
return { name: positionals[typed.length], typed };
|
|
1173
|
+
return { name: last?.replace("]", "").endsWith("...") === true ? last : undefined, typed };
|
|
1155
1174
|
}
|
|
1156
1175
|
async function leafCandidates(run, words) {
|
|
1157
1176
|
const completing = await completingOf(run);
|
|
@@ -1159,12 +1178,12 @@ async function leafCandidates(run, words) {
|
|
|
1159
1178
|
return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
|
|
1160
1179
|
const flag = words.at(-2);
|
|
1161
1180
|
const option = flag?.startsWith("-") === true ? Object.entries(completing.options).find(([name, { short }]) => flag === `--${name}` || flag === `-${short}`)?.[1] : undefined;
|
|
1181
|
+
const { name, typed } = positionalBeingTyped(words.slice(0, -1), completing);
|
|
1162
1182
|
if (option?.type === "string") {
|
|
1163
|
-
const values = option.choices === undefined ? await option.candidates?.() : option.choices;
|
|
1183
|
+
const values = option.choices === undefined ? await option.candidates?.(typed) : option.choices;
|
|
1164
1184
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
1165
1185
|
}
|
|
1166
|
-
const
|
|
1167
|
-
const values = name === undefined ? undefined : await completing.candidates[name]?.();
|
|
1186
|
+
const values = name === undefined ? undefined : await completing.candidates[name]?.(typed);
|
|
1168
1187
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
1169
1188
|
}
|
|
1170
1189
|
async function candidates(scope, defaultCommand, words, before = []) {
|
|
@@ -1195,9 +1214,13 @@ async function printCandidates(found) {
|
|
|
1195
1214
|
return 0;
|
|
1196
1215
|
}
|
|
1197
1216
|
function packageVersion(entry = Bun.main) {
|
|
1198
|
-
|
|
1217
|
+
let resolved = entry;
|
|
1218
|
+
try {
|
|
1219
|
+
resolved = realpathSync2(entry);
|
|
1220
|
+
} catch {}
|
|
1221
|
+
for (let dir = dirname3(resolved);; dir = dirname3(dir)) {
|
|
1199
1222
|
try {
|
|
1200
|
-
const { version } = JSON.parse(
|
|
1223
|
+
const { version } = JSON.parse(readFileSync4(join5(dir, "package.json"), "utf8"));
|
|
1201
1224
|
if (typeof version === "string")
|
|
1202
1225
|
return version;
|
|
1203
1226
|
} catch {}
|
|
@@ -1205,7 +1228,7 @@ function packageVersion(entry = Bun.main) {
|
|
|
1205
1228
|
return "0.0.0";
|
|
1206
1229
|
}
|
|
1207
1230
|
}
|
|
1208
|
-
async function runCli(cli, argv =
|
|
1231
|
+
async function runCli(cli, argv = scriptArgs()) {
|
|
1209
1232
|
const version = cli.version ?? packageVersion();
|
|
1210
1233
|
if (argv[0] === COMPLETE)
|
|
1211
1234
|
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
@@ -1232,7 +1255,7 @@ function commandPath(scope, argv) {
|
|
|
1232
1255
|
}
|
|
1233
1256
|
return path.length === 0 ? undefined : path.join(" ");
|
|
1234
1257
|
}
|
|
1235
|
-
async function runMain({ name, version, run, redact, usage }, argv =
|
|
1258
|
+
async function runMain({ name, version, run, redact, usage }, argv = scriptArgs()) {
|
|
1236
1259
|
if (argv[0] === COMPLETE)
|
|
1237
1260
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
1238
1261
|
if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
|
|
@@ -1288,7 +1311,7 @@ var SESSION_FILES = new Glob("*.json");
|
|
|
1288
1311
|
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
1289
1312
|
// src/hotspots.ts
|
|
1290
1313
|
import { basename, join as join7 } from "path";
|
|
1291
|
-
import
|
|
1314
|
+
import process15 from "process";
|
|
1292
1315
|
|
|
1293
1316
|
// src/churn.ts
|
|
1294
1317
|
var SOURCE = /\.(?:[cm]?[jt]sx?|vue|svelte|astro|kay|py|go|rs|rb|php|java|kt|swift|cs|c|cc|cpp|h|hpp|scala|dart|lua|sh|zsh|fish)$/;
|
|
@@ -1429,9 +1452,8 @@ var USAGE = `hotspots [options] [dir]: ranks the source files under dir (here by
|
|
|
1429
1452
|
|
|
1430
1453
|
Complexity is the count of decision points in the file (branches, loops, cases, catches, boolean operators, ternaries) plus one, churn the commits touching it since --since (${DEFAULT_SINCE}), score their product. A commit counts for less the older it is (half as much every --half-life, 0 for all alike) and a commit touching more than --bulk files, a format or rename sweep, is left out. Each file also shows its most complex declaration, found by indentation. Test files and generated or built files are left out unless --all.
|
|
1431
1454
|
|
|
1432
|
-
Lists at most ${DEFAULT_COUNT} files, the count of the rest after them (--limit <n>, --full for all); --json is never cut unless --limit is given
|
|
1433
|
-
|
|
1434
|
-
Exits 0 when it ranked the files, 1 when no file has commits in the window, 2 on a usage error or outside a git repository.`;
|
|
1455
|
+
Lists at most ${DEFAULT_COUNT} files, the count of the rest after them (--limit <n>, --full for all); --json is never cut unless --limit is given.`;
|
|
1456
|
+
var EXITS = [[0, "it ranked the files"], [1, "no file has commits in the window"], [2, "a usage error or outside a git repository"]];
|
|
1435
1457
|
var OPTIONS = {
|
|
1436
1458
|
since: { type: "string", short: "s", description: `only count the commits since this date, as git reads it (${DEFAULT_SINCE})` },
|
|
1437
1459
|
"half-life": { type: "string", description: `how long until a commit counts half as much (${DEFAULT_HALF_LIFE}), 0 for no decay` },
|
|
@@ -1443,11 +1465,11 @@ var OPTIONS = {
|
|
|
1443
1465
|
var HOT = 0.66;
|
|
1444
1466
|
var WARM = 0.33;
|
|
1445
1467
|
async function hotspots(args) {
|
|
1446
|
-
const { values, positionals } = parse(args, USAGE, OPTIONS, { positionals: ["[dir]"], replaces: "guessing which files are worth splitting first", examples: ["hotspots", 'hotspots src --since "3 months ago" --limit 10'] });
|
|
1468
|
+
const { values, positionals } = parse(args, USAGE, OPTIONS, { exits: EXITS, positionals: ["[dir]"], replaces: "guessing which files are worth splitting first", examples: ["hotspots", 'hotspots src --since "3 months ago" --limit 10'] });
|
|
1447
1469
|
if (positionals.length > 1)
|
|
1448
1470
|
badUsage("give one folder");
|
|
1449
1471
|
const count = limitOf(values, DEFAULT_COUNT);
|
|
1450
|
-
const cwd =
|
|
1472
|
+
const cwd = process15.cwd();
|
|
1451
1473
|
const bulk = Number(values.bulk ?? DEFAULT_BULK);
|
|
1452
1474
|
if (!Number.isInteger(bulk) || bulk < 1)
|
|
1453
1475
|
badUsage(`--bulk takes a number of files, not ${values.bulk}`);
|
|
@@ -1478,8 +1500,8 @@ async function hotspots(args) {
|
|
|
1478
1500
|
printJson(shown);
|
|
1479
1501
|
return 0;
|
|
1480
1502
|
}
|
|
1481
|
-
const colors = paint(
|
|
1482
|
-
const path = role("path",
|
|
1503
|
+
const colors = paint(process15.stdout);
|
|
1504
|
+
const path = role("path", process15.stdout);
|
|
1483
1505
|
const top = shown.reduce((highest, row) => Math.max(highest, row.score), 0);
|
|
1484
1506
|
const heat = (score, text) => {
|
|
1485
1507
|
if (top > 0 && score >= top * HOT)
|
|
@@ -1487,16 +1509,16 @@ async function hotspots(args) {
|
|
|
1487
1509
|
return top > 0 && score >= top * WARM ? colors.yellow(text) : text;
|
|
1488
1510
|
};
|
|
1489
1511
|
printHeader("hotspots", `${basename(cwd)} \xB7 since ${values.since ?? DEFAULT_SINCE}`);
|
|
1490
|
-
console.log(table([["rank", "score", "churn", "recent", "complexity", "lines", "file", "most complex declaration"], ...shown.map((row) => [colors.dim(String(row.rank)), heat(row.score, String(row.score)), ...[row.churn, row.recent, row.complexity, row.lines].map(String), fileLink(path(row.path), row.path,
|
|
1512
|
+
console.log(table([["rank", "score", "churn", "recent", "complexity", "lines", "file", "most complex declaration"], ...shown.map((row) => [colors.dim(String(row.rank)), heat(row.score, String(row.score)), ...[row.churn, row.recent, row.complexity, row.lines].map(String), fileLink(path(row.path), row.path, process15.stdout, cwd), row.worst === undefined ? "" : `${row.worst.name} (${row.worst.complexity}, line ${row.worst.line})`])], { header: true, align: ["right", "right", "right", "right", "right", "right", "left", "left"] }));
|
|
1491
1513
|
if (footer !== undefined)
|
|
1492
1514
|
console.log(footer);
|
|
1493
1515
|
return 0;
|
|
1494
1516
|
}
|
|
1495
1517
|
|
|
1496
1518
|
// src/peek.ts
|
|
1497
|
-
import { realpathSync as
|
|
1498
|
-
import { isAbsolute as isAbsolute2, join as join8, relative as relative2, resolve as
|
|
1499
|
-
import
|
|
1519
|
+
import { realpathSync as realpathSync3 } from "fs";
|
|
1520
|
+
import { isAbsolute as isAbsolute2, join as join8, relative as relative2, resolve as resolve5, sep } from "path";
|
|
1521
|
+
import process16 from "process";
|
|
1500
1522
|
|
|
1501
1523
|
// src/source.ts
|
|
1502
1524
|
var LOCATION = /^(?:\.\/)?([^\s:()]+)(?::(\d+)(?::\d+)?|\((\d+),\d+\))(?=[:\s)]|$)/;
|
|
@@ -1681,9 +1703,8 @@ peek --around <regex> [-C 3] [path...] the lines around each match, overlapping
|
|
|
1681
1703
|
<output> | peek [-C 3] the lines around each path:line the output opens a line with (errs, rg -n, grep -n, tsc, linters)
|
|
1682
1704
|
The paths default to the files git keeps here (.gitignore holds).
|
|
1683
1705
|
--refs lists at most 100 lines and --around prints at most 20 spans, the count of the rest after them (--limit <n>, --full for all).
|
|
1684
|
-
--json prints spans as [{path, from, to, lines}], --refs as [{path, line, text}] ({definitions, references} with --defs-first), --outline as [{path, line, depth, text}], the lines never cut and the rows never counted off unless --limit is given
|
|
1685
|
-
|
|
1686
|
-
Exits 0 when it printed something, 1 when nothing is found, a file cannot be read or a span starts past the end, 2 on a usage error or a path outside the repository.`;
|
|
1706
|
+
--json prints spans as [{path, from, to, lines}], --refs as [{path, line, text}] ({definitions, references} with --defs-first), --outline as [{path, line, depth, text}], the lines never cut and the rows never counted off unless --limit is given.`;
|
|
1707
|
+
var EXITS2 = [[0, "it printed something"], [1, "nothing is found, a file cannot be read or a span starts past the end"], [2, "a usage error or a path outside the repository"]];
|
|
1687
1708
|
var REFS_LIMIT = 100;
|
|
1688
1709
|
var AROUND_LIMIT = 20;
|
|
1689
1710
|
var PARENT = `..${sep}`;
|
|
@@ -1704,13 +1725,13 @@ function cut(text) {
|
|
|
1704
1725
|
return text.length > LONGEST_LINE ? `${text.slice(0, LONGEST_LINE - 1)}\u2026` : text;
|
|
1705
1726
|
}
|
|
1706
1727
|
async function readableRoot() {
|
|
1707
|
-
const ran = await runCommand(["git", "rev-parse", "--show-toplevel"], { cwd:
|
|
1708
|
-
return
|
|
1728
|
+
const ran = await runCommand(["git", "rev-parse", "--show-toplevel"], { cwd: process16.cwd() });
|
|
1729
|
+
return realpathSync3(ran.code === 0 ? ran.text.trim() : process16.cwd());
|
|
1709
1730
|
}
|
|
1710
1731
|
async function isOutside(path) {
|
|
1711
1732
|
let real;
|
|
1712
1733
|
try {
|
|
1713
|
-
real =
|
|
1734
|
+
real = realpathSync3(resolve5(process16.cwd(), path));
|
|
1714
1735
|
} catch {
|
|
1715
1736
|
return false;
|
|
1716
1737
|
}
|
|
@@ -1724,7 +1745,7 @@ async function checkInside(path) {
|
|
|
1724
1745
|
async function pipedSpans(text, context) {
|
|
1725
1746
|
const spans = [];
|
|
1726
1747
|
for (const [path, numbers] of locations(text)) {
|
|
1727
|
-
const source = await isOutside(path) ? undefined : await readText(
|
|
1748
|
+
const source = await isOutside(path) ? undefined : await readText(resolve5(process16.cwd(), path));
|
|
1728
1749
|
if (source !== undefined)
|
|
1729
1750
|
spans.push(...spansAround(path, source.replace(TRAILING_NEWLINE2, "").split(`
|
|
1730
1751
|
`).length, numbers, context));
|
|
@@ -1733,7 +1754,7 @@ async function pipedSpans(text, context) {
|
|
|
1733
1754
|
}
|
|
1734
1755
|
async function textOf(path) {
|
|
1735
1756
|
await checkInside(path);
|
|
1736
|
-
const text = await readText(
|
|
1757
|
+
const text = await readText(resolve5(process16.cwd(), path));
|
|
1737
1758
|
if (text === undefined)
|
|
1738
1759
|
fail(`cannot read ${path} (missing, binary or too large)`, 1);
|
|
1739
1760
|
return text;
|
|
@@ -1791,8 +1812,8 @@ async function spanOf(target) {
|
|
|
1791
1812
|
async function* sources(paths) {
|
|
1792
1813
|
for (const path of paths)
|
|
1793
1814
|
await checkInside(path);
|
|
1794
|
-
for (const path of await listFiles(paths,
|
|
1795
|
-
const text = await readText(join8(
|
|
1815
|
+
for (const path of await listFiles(paths, process16.cwd())) {
|
|
1816
|
+
const text = await readText(join8(process16.cwd(), path));
|
|
1796
1817
|
if (text !== undefined)
|
|
1797
1818
|
yield { path, text };
|
|
1798
1819
|
}
|
|
@@ -1816,7 +1837,7 @@ async function definitionsOf(symbols, paths) {
|
|
|
1816
1837
|
return [...found.values()].flat();
|
|
1817
1838
|
}
|
|
1818
1839
|
async function peek(args) {
|
|
1819
|
-
const { values, positionals } = parse(args, USAGE2, OPTIONS2, { positionals: ["[target...]"], replaces: "sed -n N,Mp, grep -n chains and whole-file reads", examples: [["peek src/a.ts:120-140 src/b.ts:1-40", "two spans in one call"], ["peek --def parseArgs src", "a whole declaration"]] });
|
|
1840
|
+
const { values, positionals } = parse(args, USAGE2, OPTIONS2, { exits: EXITS2, positionals: ["[target...]"], replaces: "sed -n N,Mp, grep -n chains and whole-file reads", examples: [["peek src/a.ts:120-140 src/b.ts:1-40", "two spans in one call"], ["peek --def parseArgs src", "a whole declaration"]] });
|
|
1820
1841
|
const modes = [values.def?.length === 0 ? undefined : values.def, values.refs, values.outline, values.around].filter((mode) => mode !== undefined && mode !== false);
|
|
1821
1842
|
const json = values.json === true;
|
|
1822
1843
|
if (values["defs-first"] === true && values.refs === undefined)
|
|
@@ -1893,7 +1914,7 @@ async function peek(args) {
|
|
|
1893
1914
|
|
|
1894
1915
|
// src/todo-scan.ts
|
|
1895
1916
|
import { join as join9 } from "path";
|
|
1896
|
-
import
|
|
1917
|
+
import process17 from "process";
|
|
1897
1918
|
|
|
1898
1919
|
// src/todo.ts
|
|
1899
1920
|
var COMMENT2 = /(?<![:\w/])\/\/|\/\*|<!--|(?:^|\s)(?:#|--)(?=\s)|^\s*\*(?=\s|$)/;
|
|
@@ -1968,9 +1989,8 @@ var USAGE3 = `todo-scan [options] [path...]: lists the TODO, FIXME and owner/rep
|
|
|
1968
1989
|
|
|
1969
1990
|
--stale keeps only the comments naming a closed issue, each issue's state looked up with ft, or gh for a GitHub repository, and exits 1 when it finds one.
|
|
1970
1991
|
|
|
1971
|
-
Lists at most 100 comments, the count of the rest after them (--limit <n>, --full for all); --json is never cut unless --limit is given
|
|
1972
|
-
|
|
1973
|
-
Exits 0 when it listed the comments or found no stale one, 1 when --stale finds one, 2 on a usage error or when --stale could not look a repository up (ft and gh both failed), so a clean exit never hides an unchecked issue.`;
|
|
1992
|
+
Lists at most 100 comments, the count of the rest after them (--limit <n>, --full for all); --json is never cut unless --limit is given.`;
|
|
1993
|
+
var EXITS3 = [[0, "it listed the comments or found no stale one"], [1, "--stale finds one"], [2, "a usage error or when --stale could not look a repository up (ft and gh both failed)"]];
|
|
1974
1994
|
var LIST_LIMIT = 100;
|
|
1975
1995
|
var OPTIONS3 = {
|
|
1976
1996
|
stale: { type: "boolean", description: "only the comments naming a closed issue, looked up with ft or gh" },
|
|
@@ -1984,8 +2004,8 @@ function printLines(lines, footer) {
|
|
|
1984
2004
|
`));
|
|
1985
2005
|
}
|
|
1986
2006
|
async function todoScan(args) {
|
|
1987
|
-
const { values, positionals } = parse(args, USAGE3, OPTIONS3, { positionals: ["[path...]"], replaces: "grep -rn TODO, then opening each issue it names", examples: ["todo-scan src", ["todo-scan --stale", "only the comments naming a closed issue"]] });
|
|
1988
|
-
const cwd =
|
|
2007
|
+
const { values, positionals } = parse(args, USAGE3, OPTIONS3, { exits: EXITS3, positionals: ["[path...]"], replaces: "grep -rn TODO, then opening each issue it names", examples: ["todo-scan src", ["todo-scan --stale", "only the comments naming a closed issue"]] });
|
|
2008
|
+
const cwd = process17.cwd();
|
|
1989
2009
|
const limit = limitOf(values, LIST_LIMIT);
|
|
1990
2010
|
const marks = [];
|
|
1991
2011
|
for (const path of await listFiles(positionals, cwd)) {
|
|
@@ -2016,7 +2036,7 @@ async function todoScan(args) {
|
|
|
2016
2036
|
}
|
|
2017
2037
|
|
|
2018
2038
|
// src/cli.ts
|
|
2019
|
-
|
|
2039
|
+
process18.exitCode = await runCli({
|
|
2020
2040
|
name: "src",
|
|
2021
2041
|
about: "reads and ranks the source of a folder: line spans, TODO comments and the files worth splitting",
|
|
2022
2042
|
replaces: "sed -n N,Mp, grep -n chains and grep -rn TODO",
|
package/dist/hotspots-cli.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/hotspots-cli.ts
|
|
5
|
-
import
|
|
5
|
+
import process16 from "process";
|
|
6
6
|
// ../kit/src/args.ts
|
|
7
7
|
import process9 from "process";
|
|
8
8
|
import { parseArgs } from "util";
|
|
@@ -488,18 +488,14 @@ var FULL_STOP = /\.$/;
|
|
|
488
488
|
function usageLine(usage, colors = paint(process8.stdout)) {
|
|
489
489
|
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
490
490
|
}
|
|
491
|
-
function helpText(usage, options, colors = paint(process8.stdout), { examples = [],
|
|
492
|
-
return [usageLine(usage, colors),
|
|
491
|
+
function helpText(usage, options, colors = paint(process8.stdout), { examples = [], exits = [] } = {}) {
|
|
492
|
+
return [usageLine(usage, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
|
|
493
493
|
`);
|
|
494
494
|
}
|
|
495
495
|
function exitRows(exits, colors = paint(process8.stdout)) {
|
|
496
496
|
if (exits.length === 0)
|
|
497
497
|
return [];
|
|
498
|
-
|
|
499
|
-
return ["", colors.bold("Exits:"), ...exits.map(([code, meaning]) => ` ${colors.cyan(String(code).padEnd(width))} ${meaning}`)];
|
|
500
|
-
}
|
|
501
|
-
function replacesRows(replaces, colors = paint(process8.stdout)) {
|
|
502
|
-
return replaces === undefined || outputMode() === "agent" ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
498
|
+
return ["", `${colors.bold("Exit codes:")} ${exits.map(([code, meaning]) => `${colors.cyan(String(code))} ${meaning}`).join(", ")}`];
|
|
503
499
|
}
|
|
504
500
|
function wantsHelpJson(env = process8.env) {
|
|
505
501
|
return env.KIT_HELP === "json";
|
|
@@ -800,9 +796,33 @@ function nearestOption(message, options, allowNegative) {
|
|
|
800
796
|
const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
|
|
801
797
|
return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
|
|
802
798
|
}
|
|
799
|
+
// ../kit/src/argv.ts
|
|
800
|
+
import { readFileSync as readFileSync3, realpathSync } from "fs";
|
|
801
|
+
import { resolve as resolve4 } from "path";
|
|
802
|
+
import process10 from "process";
|
|
803
|
+
function commandLine() {
|
|
804
|
+
try {
|
|
805
|
+
return readFileSync3("/proc/self/cmdline", "utf8").split("\x00").slice(0, -1);
|
|
806
|
+
} catch {
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
function realPath(word) {
|
|
811
|
+
try {
|
|
812
|
+
return realpathSync(resolve4(word));
|
|
813
|
+
} catch {
|
|
814
|
+
return;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
function scriptArgs(argv = process10.argv.slice(2), cmdline = commandLine(), script = process10.argv[1]) {
|
|
818
|
+
const target = script === undefined ? undefined : realPath(script) ?? script;
|
|
819
|
+
const at = cmdline === undefined || target === undefined ? -1 : cmdline.findIndex((word, index) => index > 0 && (resolve4(word) === script || realPath(word) === target));
|
|
820
|
+
const after = at === -1 ? [] : cmdline.slice(at + 1);
|
|
821
|
+
return after[0] === "--" && after.length === argv.length + 1 && argv.every((word, index) => word === after[index + 1]) ? after : argv;
|
|
822
|
+
}
|
|
803
823
|
// ../kit/src/process.ts
|
|
804
824
|
import { constants } from "os";
|
|
805
|
-
import
|
|
825
|
+
import process11 from "process";
|
|
806
826
|
var KILL_GRACE_MS = 2000;
|
|
807
827
|
function exitCodeOf(code, signal) {
|
|
808
828
|
if (code !== null)
|
|
@@ -822,7 +842,7 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal,
|
|
|
822
842
|
return;
|
|
823
843
|
try {
|
|
824
844
|
if (grouped)
|
|
825
|
-
|
|
845
|
+
process11.kill(-proc.pid, killing);
|
|
826
846
|
else
|
|
827
847
|
proc.kill(killing);
|
|
828
848
|
} catch {}
|
|
@@ -896,15 +916,15 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
|
|
|
896
916
|
var RESET_STYLE = `${ESC}[0m`;
|
|
897
917
|
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
898
918
|
// ../kit/src/cli.ts
|
|
899
|
-
import { readFileSync as
|
|
919
|
+
import { readFileSync as readFileSync4, realpathSync as realpathSync2 } from "fs";
|
|
900
920
|
import { dirname as dirname3, join as join5 } from "path";
|
|
901
|
-
import
|
|
921
|
+
import process13 from "process";
|
|
902
922
|
|
|
903
923
|
// ../kit/src/usage.ts
|
|
904
924
|
import { appendFileSync, mkdirSync } from "fs";
|
|
905
925
|
import { homedir as homedir3 } from "os";
|
|
906
926
|
import { dirname as dirname2, isAbsolute, join as join4 } from "path";
|
|
907
|
-
import
|
|
927
|
+
import process12 from "process";
|
|
908
928
|
|
|
909
929
|
// ../kit/src/logs.ts
|
|
910
930
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -918,14 +938,14 @@ var usageError;
|
|
|
918
938
|
function noteUsageError(message) {
|
|
919
939
|
usageError = message;
|
|
920
940
|
}
|
|
921
|
-
function stateHome(env =
|
|
941
|
+
function stateHome(env = process12.env) {
|
|
922
942
|
const dir = env.XDG_STATE_HOME;
|
|
923
943
|
return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
|
|
924
944
|
}
|
|
925
|
-
function usagePath(env =
|
|
945
|
+
function usagePath(env = process12.env) {
|
|
926
946
|
return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
927
947
|
}
|
|
928
|
-
function usageRecorded(env =
|
|
948
|
+
function usageRecorded(env = process12.env) {
|
|
929
949
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
930
950
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
931
951
|
}
|
|
@@ -939,7 +959,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
939
959
|
usageError = undefined;
|
|
940
960
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
941
961
|
}
|
|
942
|
-
function recordUsage(entry, env =
|
|
962
|
+
function recordUsage(entry, env = process12.env) {
|
|
943
963
|
if (!usageRecorded(env))
|
|
944
964
|
return;
|
|
945
965
|
try {
|
|
@@ -1016,7 +1036,6 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
|
|
|
1016
1036
|
header(colors, summary),
|
|
1017
1037
|
"",
|
|
1018
1038
|
usageLine(`${prefix} ${defaultCommand === undefined ? colors.cyan("<command>") : `[${colors.cyan("<command>")}]`} [options]`, colors),
|
|
1019
|
-
...replacesRows(replaces, colors),
|
|
1020
1039
|
"",
|
|
1021
1040
|
...await commandRows(commands, colors),
|
|
1022
1041
|
...own.length === 0 ? [] : ["", `${colors.bold(`Options of ${defaultCommand}:`)} ${colors.dim("the command run when none is named")}`, ...optionRows(Object.fromEntries(own), colors)],
|
|
@@ -1030,8 +1049,8 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
|
|
|
1030
1049
|
`);
|
|
1031
1050
|
}
|
|
1032
1051
|
function printError(prefix, message, hint, cause) {
|
|
1033
|
-
const colors = paint(
|
|
1034
|
-
const styled = takesControlCodes(
|
|
1052
|
+
const colors = paint(process13.stderr);
|
|
1053
|
+
const styled = takesControlCodes(process13.stderr);
|
|
1035
1054
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
1036
1055
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
1037
1056
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -1074,11 +1093,11 @@ async function dispatch(level, typed, { version, redact }) {
|
|
|
1074
1093
|
}
|
|
1075
1094
|
}
|
|
1076
1095
|
if (name === undefined && level.path.length > 1) {
|
|
1077
|
-
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
1096
|
+
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process13.stderr));
|
|
1078
1097
|
return 2;
|
|
1079
1098
|
}
|
|
1080
1099
|
if (name === undefined || name === "help" || name === "--help" || name === "-h") {
|
|
1081
|
-
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
1100
|
+
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process13.stdout));
|
|
1082
1101
|
return 0;
|
|
1083
1102
|
}
|
|
1084
1103
|
if (level.path.length === 1 && (name === "--version" || name === "-v")) {
|
|
@@ -1134,24 +1153,24 @@ function candidateLine(candidate) {
|
|
|
1134
1153
|
}
|
|
1135
1154
|
function positionalBeingTyped(before, { options, positionals }) {
|
|
1136
1155
|
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
1137
|
-
|
|
1156
|
+
const typed = [];
|
|
1138
1157
|
let at = 0;
|
|
1139
1158
|
while (at < before.length) {
|
|
1140
1159
|
const word = before[at];
|
|
1141
1160
|
if (word === "--") {
|
|
1142
|
-
|
|
1161
|
+
typed.push(...before.slice(at + 1));
|
|
1143
1162
|
break;
|
|
1144
1163
|
}
|
|
1145
1164
|
if (!word.startsWith("-"))
|
|
1146
|
-
|
|
1165
|
+
typed.push(word);
|
|
1147
1166
|
else if (takesValue(word))
|
|
1148
1167
|
at++;
|
|
1149
1168
|
at++;
|
|
1150
1169
|
}
|
|
1151
1170
|
const last = positionals.at(-1);
|
|
1152
|
-
if (
|
|
1153
|
-
return positionals[
|
|
1154
|
-
return last?.replace("]", "").endsWith("...") === true ? last : undefined;
|
|
1171
|
+
if (typed.length < positionals.length)
|
|
1172
|
+
return { name: positionals[typed.length], typed };
|
|
1173
|
+
return { name: last?.replace("]", "").endsWith("...") === true ? last : undefined, typed };
|
|
1155
1174
|
}
|
|
1156
1175
|
async function leafCandidates(run, words) {
|
|
1157
1176
|
const completing = await completingOf(run);
|
|
@@ -1159,12 +1178,12 @@ async function leafCandidates(run, words) {
|
|
|
1159
1178
|
return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
|
|
1160
1179
|
const flag = words.at(-2);
|
|
1161
1180
|
const option = flag?.startsWith("-") === true ? Object.entries(completing.options).find(([name, { short }]) => flag === `--${name}` || flag === `-${short}`)?.[1] : undefined;
|
|
1181
|
+
const { name, typed } = positionalBeingTyped(words.slice(0, -1), completing);
|
|
1162
1182
|
if (option?.type === "string") {
|
|
1163
|
-
const values = option.choices === undefined ? await option.candidates?.() : option.choices;
|
|
1183
|
+
const values = option.choices === undefined ? await option.candidates?.(typed) : option.choices;
|
|
1164
1184
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
1165
1185
|
}
|
|
1166
|
-
const
|
|
1167
|
-
const values = name === undefined ? undefined : await completing.candidates[name]?.();
|
|
1186
|
+
const values = name === undefined ? undefined : await completing.candidates[name]?.(typed);
|
|
1168
1187
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
1169
1188
|
}
|
|
1170
1189
|
async function candidates(scope, defaultCommand, words, before = []) {
|
|
@@ -1195,9 +1214,13 @@ async function printCandidates(found) {
|
|
|
1195
1214
|
return 0;
|
|
1196
1215
|
}
|
|
1197
1216
|
function packageVersion(entry = Bun.main) {
|
|
1198
|
-
|
|
1217
|
+
let resolved = entry;
|
|
1218
|
+
try {
|
|
1219
|
+
resolved = realpathSync2(entry);
|
|
1220
|
+
} catch {}
|
|
1221
|
+
for (let dir = dirname3(resolved);; dir = dirname3(dir)) {
|
|
1199
1222
|
try {
|
|
1200
|
-
const { version } = JSON.parse(
|
|
1223
|
+
const { version } = JSON.parse(readFileSync4(join5(dir, "package.json"), "utf8"));
|
|
1201
1224
|
if (typeof version === "string")
|
|
1202
1225
|
return version;
|
|
1203
1226
|
} catch {}
|
|
@@ -1205,7 +1228,7 @@ function packageVersion(entry = Bun.main) {
|
|
|
1205
1228
|
return "0.0.0";
|
|
1206
1229
|
}
|
|
1207
1230
|
}
|
|
1208
|
-
async function runCli(cli, argv =
|
|
1231
|
+
async function runCli(cli, argv = scriptArgs()) {
|
|
1209
1232
|
const version = cli.version ?? packageVersion();
|
|
1210
1233
|
if (argv[0] === COMPLETE)
|
|
1211
1234
|
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
@@ -1232,7 +1255,7 @@ function commandPath(scope, argv) {
|
|
|
1232
1255
|
}
|
|
1233
1256
|
return path.length === 0 ? undefined : path.join(" ");
|
|
1234
1257
|
}
|
|
1235
|
-
async function runMain({ name, version, run, redact, usage }, argv =
|
|
1258
|
+
async function runMain({ name, version, run, redact, usage }, argv = scriptArgs()) {
|
|
1236
1259
|
if (argv[0] === COMPLETE)
|
|
1237
1260
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
1238
1261
|
if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
|
|
@@ -1288,7 +1311,7 @@ var SESSION_FILES = new Glob("*.json");
|
|
|
1288
1311
|
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
1289
1312
|
// src/hotspots.ts
|
|
1290
1313
|
import { basename, join as join7 } from "path";
|
|
1291
|
-
import
|
|
1314
|
+
import process15 from "process";
|
|
1292
1315
|
|
|
1293
1316
|
// src/churn.ts
|
|
1294
1317
|
var SOURCE = /\.(?:[cm]?[jt]sx?|vue|svelte|astro|kay|py|go|rs|rb|php|java|kt|swift|cs|c|cc|cpp|h|hpp|scala|dart|lua|sh|zsh|fish)$/;
|
|
@@ -1429,9 +1452,8 @@ var USAGE = `hotspots [options] [dir]: ranks the source files under dir (here by
|
|
|
1429
1452
|
|
|
1430
1453
|
Complexity is the count of decision points in the file (branches, loops, cases, catches, boolean operators, ternaries) plus one, churn the commits touching it since --since (${DEFAULT_SINCE}), score their product. A commit counts for less the older it is (half as much every --half-life, 0 for all alike) and a commit touching more than --bulk files, a format or rename sweep, is left out. Each file also shows its most complex declaration, found by indentation. Test files and generated or built files are left out unless --all.
|
|
1431
1454
|
|
|
1432
|
-
Lists at most ${DEFAULT_COUNT} files, the count of the rest after them (--limit <n>, --full for all); --json is never cut unless --limit is given
|
|
1433
|
-
|
|
1434
|
-
Exits 0 when it ranked the files, 1 when no file has commits in the window, 2 on a usage error or outside a git repository.`;
|
|
1455
|
+
Lists at most ${DEFAULT_COUNT} files, the count of the rest after them (--limit <n>, --full for all); --json is never cut unless --limit is given.`;
|
|
1456
|
+
var EXITS = [[0, "it ranked the files"], [1, "no file has commits in the window"], [2, "a usage error or outside a git repository"]];
|
|
1435
1457
|
var OPTIONS = {
|
|
1436
1458
|
since: { type: "string", short: "s", description: `only count the commits since this date, as git reads it (${DEFAULT_SINCE})` },
|
|
1437
1459
|
"half-life": { type: "string", description: `how long until a commit counts half as much (${DEFAULT_HALF_LIFE}), 0 for no decay` },
|
|
@@ -1443,11 +1465,11 @@ var OPTIONS = {
|
|
|
1443
1465
|
var HOT = 0.66;
|
|
1444
1466
|
var WARM = 0.33;
|
|
1445
1467
|
async function hotspots(args) {
|
|
1446
|
-
const { values, positionals } = parse(args, USAGE, OPTIONS, { positionals: ["[dir]"], replaces: "guessing which files are worth splitting first", examples: ["hotspots", 'hotspots src --since "3 months ago" --limit 10'] });
|
|
1468
|
+
const { values, positionals } = parse(args, USAGE, OPTIONS, { exits: EXITS, positionals: ["[dir]"], replaces: "guessing which files are worth splitting first", examples: ["hotspots", 'hotspots src --since "3 months ago" --limit 10'] });
|
|
1447
1469
|
if (positionals.length > 1)
|
|
1448
1470
|
badUsage("give one folder");
|
|
1449
1471
|
const count = limitOf(values, DEFAULT_COUNT);
|
|
1450
|
-
const cwd =
|
|
1472
|
+
const cwd = process15.cwd();
|
|
1451
1473
|
const bulk = Number(values.bulk ?? DEFAULT_BULK);
|
|
1452
1474
|
if (!Number.isInteger(bulk) || bulk < 1)
|
|
1453
1475
|
badUsage(`--bulk takes a number of files, not ${values.bulk}`);
|
|
@@ -1478,8 +1500,8 @@ async function hotspots(args) {
|
|
|
1478
1500
|
printJson(shown);
|
|
1479
1501
|
return 0;
|
|
1480
1502
|
}
|
|
1481
|
-
const colors = paint(
|
|
1482
|
-
const path = role("path",
|
|
1503
|
+
const colors = paint(process15.stdout);
|
|
1504
|
+
const path = role("path", process15.stdout);
|
|
1483
1505
|
const top = shown.reduce((highest, row) => Math.max(highest, row.score), 0);
|
|
1484
1506
|
const heat = (score, text) => {
|
|
1485
1507
|
if (top > 0 && score >= top * HOT)
|
|
@@ -1487,11 +1509,11 @@ async function hotspots(args) {
|
|
|
1487
1509
|
return top > 0 && score >= top * WARM ? colors.yellow(text) : text;
|
|
1488
1510
|
};
|
|
1489
1511
|
printHeader("hotspots", `${basename(cwd)} \xB7 since ${values.since ?? DEFAULT_SINCE}`);
|
|
1490
|
-
console.log(table([["rank", "score", "churn", "recent", "complexity", "lines", "file", "most complex declaration"], ...shown.map((row) => [colors.dim(String(row.rank)), heat(row.score, String(row.score)), ...[row.churn, row.recent, row.complexity, row.lines].map(String), fileLink(path(row.path), row.path,
|
|
1512
|
+
console.log(table([["rank", "score", "churn", "recent", "complexity", "lines", "file", "most complex declaration"], ...shown.map((row) => [colors.dim(String(row.rank)), heat(row.score, String(row.score)), ...[row.churn, row.recent, row.complexity, row.lines].map(String), fileLink(path(row.path), row.path, process15.stdout, cwd), row.worst === undefined ? "" : `${row.worst.name} (${row.worst.complexity}, line ${row.worst.line})`])], { header: true, align: ["right", "right", "right", "right", "right", "right", "left", "left"] }));
|
|
1491
1513
|
if (footer !== undefined)
|
|
1492
1514
|
console.log(footer);
|
|
1493
1515
|
return 0;
|
|
1494
1516
|
}
|
|
1495
1517
|
|
|
1496
1518
|
// src/hotspots-cli.ts
|
|
1497
|
-
|
|
1519
|
+
process16.exitCode = await runMain({ name: "hotspots", run: hotspots });
|
package/dist/peek-cli.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/peek-cli.ts
|
|
5
|
-
import
|
|
5
|
+
import process16 from "process";
|
|
6
6
|
// ../kit/src/args.ts
|
|
7
7
|
import process9 from "process";
|
|
8
8
|
import { parseArgs } from "util";
|
|
@@ -488,18 +488,14 @@ var FULL_STOP = /\.$/;
|
|
|
488
488
|
function usageLine(usage, colors = paint(process8.stdout)) {
|
|
489
489
|
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
490
490
|
}
|
|
491
|
-
function helpText(usage, options, colors = paint(process8.stdout), { examples = [],
|
|
492
|
-
return [usageLine(usage, colors),
|
|
491
|
+
function helpText(usage, options, colors = paint(process8.stdout), { examples = [], exits = [] } = {}) {
|
|
492
|
+
return [usageLine(usage, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
|
|
493
493
|
`);
|
|
494
494
|
}
|
|
495
495
|
function exitRows(exits, colors = paint(process8.stdout)) {
|
|
496
496
|
if (exits.length === 0)
|
|
497
497
|
return [];
|
|
498
|
-
|
|
499
|
-
return ["", colors.bold("Exits:"), ...exits.map(([code, meaning]) => ` ${colors.cyan(String(code).padEnd(width))} ${meaning}`)];
|
|
500
|
-
}
|
|
501
|
-
function replacesRows(replaces, colors = paint(process8.stdout)) {
|
|
502
|
-
return replaces === undefined || outputMode() === "agent" ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
498
|
+
return ["", `${colors.bold("Exit codes:")} ${exits.map(([code, meaning]) => `${colors.cyan(String(code))} ${meaning}`).join(", ")}`];
|
|
503
499
|
}
|
|
504
500
|
function wantsHelpJson(env = process8.env) {
|
|
505
501
|
return env.KIT_HELP === "json";
|
|
@@ -800,9 +796,33 @@ function nearestOption(message, options, allowNegative) {
|
|
|
800
796
|
const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
|
|
801
797
|
return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
|
|
802
798
|
}
|
|
799
|
+
// ../kit/src/argv.ts
|
|
800
|
+
import { readFileSync as readFileSync3, realpathSync } from "fs";
|
|
801
|
+
import { resolve as resolve4 } from "path";
|
|
802
|
+
import process10 from "process";
|
|
803
|
+
function commandLine() {
|
|
804
|
+
try {
|
|
805
|
+
return readFileSync3("/proc/self/cmdline", "utf8").split("\x00").slice(0, -1);
|
|
806
|
+
} catch {
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
function realPath(word) {
|
|
811
|
+
try {
|
|
812
|
+
return realpathSync(resolve4(word));
|
|
813
|
+
} catch {
|
|
814
|
+
return;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
function scriptArgs(argv = process10.argv.slice(2), cmdline = commandLine(), script = process10.argv[1]) {
|
|
818
|
+
const target = script === undefined ? undefined : realPath(script) ?? script;
|
|
819
|
+
const at = cmdline === undefined || target === undefined ? -1 : cmdline.findIndex((word, index) => index > 0 && (resolve4(word) === script || realPath(word) === target));
|
|
820
|
+
const after = at === -1 ? [] : cmdline.slice(at + 1);
|
|
821
|
+
return after[0] === "--" && after.length === argv.length + 1 && argv.every((word, index) => word === after[index + 1]) ? after : argv;
|
|
822
|
+
}
|
|
803
823
|
// ../kit/src/process.ts
|
|
804
824
|
import { constants } from "os";
|
|
805
|
-
import
|
|
825
|
+
import process11 from "process";
|
|
806
826
|
var KILL_GRACE_MS = 2000;
|
|
807
827
|
function exitCodeOf(code, signal) {
|
|
808
828
|
if (code !== null)
|
|
@@ -822,7 +842,7 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal,
|
|
|
822
842
|
return;
|
|
823
843
|
try {
|
|
824
844
|
if (grouped)
|
|
825
|
-
|
|
845
|
+
process11.kill(-proc.pid, killing);
|
|
826
846
|
else
|
|
827
847
|
proc.kill(killing);
|
|
828
848
|
} catch {}
|
|
@@ -896,15 +916,15 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
|
|
|
896
916
|
var RESET_STYLE = `${ESC}[0m`;
|
|
897
917
|
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
898
918
|
// ../kit/src/cli.ts
|
|
899
|
-
import { readFileSync as
|
|
919
|
+
import { readFileSync as readFileSync4, realpathSync as realpathSync2 } from "fs";
|
|
900
920
|
import { dirname as dirname3, join as join5 } from "path";
|
|
901
|
-
import
|
|
921
|
+
import process13 from "process";
|
|
902
922
|
|
|
903
923
|
// ../kit/src/usage.ts
|
|
904
924
|
import { appendFileSync, mkdirSync } from "fs";
|
|
905
925
|
import { homedir as homedir3 } from "os";
|
|
906
926
|
import { dirname as dirname2, isAbsolute, join as join4 } from "path";
|
|
907
|
-
import
|
|
927
|
+
import process12 from "process";
|
|
908
928
|
|
|
909
929
|
// ../kit/src/logs.ts
|
|
910
930
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -918,14 +938,14 @@ var usageError;
|
|
|
918
938
|
function noteUsageError(message) {
|
|
919
939
|
usageError = message;
|
|
920
940
|
}
|
|
921
|
-
function stateHome(env =
|
|
941
|
+
function stateHome(env = process12.env) {
|
|
922
942
|
const dir = env.XDG_STATE_HOME;
|
|
923
943
|
return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
|
|
924
944
|
}
|
|
925
|
-
function usagePath(env =
|
|
945
|
+
function usagePath(env = process12.env) {
|
|
926
946
|
return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
927
947
|
}
|
|
928
|
-
function usageRecorded(env =
|
|
948
|
+
function usageRecorded(env = process12.env) {
|
|
929
949
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
930
950
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
931
951
|
}
|
|
@@ -939,7 +959,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
939
959
|
usageError = undefined;
|
|
940
960
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
941
961
|
}
|
|
942
|
-
function recordUsage(entry, env =
|
|
962
|
+
function recordUsage(entry, env = process12.env) {
|
|
943
963
|
if (!usageRecorded(env))
|
|
944
964
|
return;
|
|
945
965
|
try {
|
|
@@ -1016,7 +1036,6 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
|
|
|
1016
1036
|
header(colors, summary),
|
|
1017
1037
|
"",
|
|
1018
1038
|
usageLine(`${prefix} ${defaultCommand === undefined ? colors.cyan("<command>") : `[${colors.cyan("<command>")}]`} [options]`, colors),
|
|
1019
|
-
...replacesRows(replaces, colors),
|
|
1020
1039
|
"",
|
|
1021
1040
|
...await commandRows(commands, colors),
|
|
1022
1041
|
...own.length === 0 ? [] : ["", `${colors.bold(`Options of ${defaultCommand}:`)} ${colors.dim("the command run when none is named")}`, ...optionRows(Object.fromEntries(own), colors)],
|
|
@@ -1030,8 +1049,8 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
|
|
|
1030
1049
|
`);
|
|
1031
1050
|
}
|
|
1032
1051
|
function printError(prefix, message, hint, cause) {
|
|
1033
|
-
const colors = paint(
|
|
1034
|
-
const styled = takesControlCodes(
|
|
1052
|
+
const colors = paint(process13.stderr);
|
|
1053
|
+
const styled = takesControlCodes(process13.stderr);
|
|
1035
1054
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
1036
1055
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
1037
1056
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -1074,11 +1093,11 @@ async function dispatch(level, typed, { version, redact }) {
|
|
|
1074
1093
|
}
|
|
1075
1094
|
}
|
|
1076
1095
|
if (name === undefined && level.path.length > 1) {
|
|
1077
|
-
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
1096
|
+
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process13.stderr));
|
|
1078
1097
|
return 2;
|
|
1079
1098
|
}
|
|
1080
1099
|
if (name === undefined || name === "help" || name === "--help" || name === "-h") {
|
|
1081
|
-
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
1100
|
+
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process13.stdout));
|
|
1082
1101
|
return 0;
|
|
1083
1102
|
}
|
|
1084
1103
|
if (level.path.length === 1 && (name === "--version" || name === "-v")) {
|
|
@@ -1134,24 +1153,24 @@ function candidateLine(candidate) {
|
|
|
1134
1153
|
}
|
|
1135
1154
|
function positionalBeingTyped(before, { options, positionals }) {
|
|
1136
1155
|
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
1137
|
-
|
|
1156
|
+
const typed = [];
|
|
1138
1157
|
let at = 0;
|
|
1139
1158
|
while (at < before.length) {
|
|
1140
1159
|
const word = before[at];
|
|
1141
1160
|
if (word === "--") {
|
|
1142
|
-
|
|
1161
|
+
typed.push(...before.slice(at + 1));
|
|
1143
1162
|
break;
|
|
1144
1163
|
}
|
|
1145
1164
|
if (!word.startsWith("-"))
|
|
1146
|
-
|
|
1165
|
+
typed.push(word);
|
|
1147
1166
|
else if (takesValue(word))
|
|
1148
1167
|
at++;
|
|
1149
1168
|
at++;
|
|
1150
1169
|
}
|
|
1151
1170
|
const last = positionals.at(-1);
|
|
1152
|
-
if (
|
|
1153
|
-
return positionals[
|
|
1154
|
-
return last?.replace("]", "").endsWith("...") === true ? last : undefined;
|
|
1171
|
+
if (typed.length < positionals.length)
|
|
1172
|
+
return { name: positionals[typed.length], typed };
|
|
1173
|
+
return { name: last?.replace("]", "").endsWith("...") === true ? last : undefined, typed };
|
|
1155
1174
|
}
|
|
1156
1175
|
async function leafCandidates(run, words) {
|
|
1157
1176
|
const completing = await completingOf(run);
|
|
@@ -1159,12 +1178,12 @@ async function leafCandidates(run, words) {
|
|
|
1159
1178
|
return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
|
|
1160
1179
|
const flag = words.at(-2);
|
|
1161
1180
|
const option = flag?.startsWith("-") === true ? Object.entries(completing.options).find(([name, { short }]) => flag === `--${name}` || flag === `-${short}`)?.[1] : undefined;
|
|
1181
|
+
const { name, typed } = positionalBeingTyped(words.slice(0, -1), completing);
|
|
1162
1182
|
if (option?.type === "string") {
|
|
1163
|
-
const values = option.choices === undefined ? await option.candidates?.() : option.choices;
|
|
1183
|
+
const values = option.choices === undefined ? await option.candidates?.(typed) : option.choices;
|
|
1164
1184
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
1165
1185
|
}
|
|
1166
|
-
const
|
|
1167
|
-
const values = name === undefined ? undefined : await completing.candidates[name]?.();
|
|
1186
|
+
const values = name === undefined ? undefined : await completing.candidates[name]?.(typed);
|
|
1168
1187
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
1169
1188
|
}
|
|
1170
1189
|
async function candidates(scope, defaultCommand, words, before = []) {
|
|
@@ -1195,9 +1214,13 @@ async function printCandidates(found) {
|
|
|
1195
1214
|
return 0;
|
|
1196
1215
|
}
|
|
1197
1216
|
function packageVersion(entry = Bun.main) {
|
|
1198
|
-
|
|
1217
|
+
let resolved = entry;
|
|
1218
|
+
try {
|
|
1219
|
+
resolved = realpathSync2(entry);
|
|
1220
|
+
} catch {}
|
|
1221
|
+
for (let dir = dirname3(resolved);; dir = dirname3(dir)) {
|
|
1199
1222
|
try {
|
|
1200
|
-
const { version } = JSON.parse(
|
|
1223
|
+
const { version } = JSON.parse(readFileSync4(join5(dir, "package.json"), "utf8"));
|
|
1201
1224
|
if (typeof version === "string")
|
|
1202
1225
|
return version;
|
|
1203
1226
|
} catch {}
|
|
@@ -1205,7 +1228,7 @@ function packageVersion(entry = Bun.main) {
|
|
|
1205
1228
|
return "0.0.0";
|
|
1206
1229
|
}
|
|
1207
1230
|
}
|
|
1208
|
-
async function runCli(cli, argv =
|
|
1231
|
+
async function runCli(cli, argv = scriptArgs()) {
|
|
1209
1232
|
const version = cli.version ?? packageVersion();
|
|
1210
1233
|
if (argv[0] === COMPLETE)
|
|
1211
1234
|
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
@@ -1232,7 +1255,7 @@ function commandPath(scope, argv) {
|
|
|
1232
1255
|
}
|
|
1233
1256
|
return path.length === 0 ? undefined : path.join(" ");
|
|
1234
1257
|
}
|
|
1235
|
-
async function runMain({ name, version, run, redact, usage }, argv =
|
|
1258
|
+
async function runMain({ name, version, run, redact, usage }, argv = scriptArgs()) {
|
|
1236
1259
|
if (argv[0] === COMPLETE)
|
|
1237
1260
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
1238
1261
|
if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
|
|
@@ -1287,9 +1310,9 @@ var {Glob } = globalThis.Bun;
|
|
|
1287
1310
|
var SESSION_FILES = new Glob("*.json");
|
|
1288
1311
|
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
1289
1312
|
// src/peek.ts
|
|
1290
|
-
import { realpathSync as
|
|
1291
|
-
import { isAbsolute as isAbsolute2, join as join7, relative as relative2, resolve as
|
|
1292
|
-
import
|
|
1313
|
+
import { realpathSync as realpathSync3 } from "fs";
|
|
1314
|
+
import { isAbsolute as isAbsolute2, join as join7, relative as relative2, resolve as resolve5, sep } from "path";
|
|
1315
|
+
import process15 from "process";
|
|
1293
1316
|
|
|
1294
1317
|
// src/files.ts
|
|
1295
1318
|
import { existsSync as existsSync4, readdirSync, statSync as statSync2 } from "fs";
|
|
@@ -1515,9 +1538,8 @@ peek --around <regex> [-C 3] [path...] the lines around each match, overlapping
|
|
|
1515
1538
|
<output> | peek [-C 3] the lines around each path:line the output opens a line with (errs, rg -n, grep -n, tsc, linters)
|
|
1516
1539
|
The paths default to the files git keeps here (.gitignore holds).
|
|
1517
1540
|
--refs lists at most 100 lines and --around prints at most 20 spans, the count of the rest after them (--limit <n>, --full for all).
|
|
1518
|
-
--json prints spans as [{path, from, to, lines}], --refs as [{path, line, text}] ({definitions, references} with --defs-first), --outline as [{path, line, depth, text}], the lines never cut and the rows never counted off unless --limit is given
|
|
1519
|
-
|
|
1520
|
-
Exits 0 when it printed something, 1 when nothing is found, a file cannot be read or a span starts past the end, 2 on a usage error or a path outside the repository.`;
|
|
1541
|
+
--json prints spans as [{path, from, to, lines}], --refs as [{path, line, text}] ({definitions, references} with --defs-first), --outline as [{path, line, depth, text}], the lines never cut and the rows never counted off unless --limit is given.`;
|
|
1542
|
+
var EXITS = [[0, "it printed something"], [1, "nothing is found, a file cannot be read or a span starts past the end"], [2, "a usage error or a path outside the repository"]];
|
|
1521
1543
|
var REFS_LIMIT = 100;
|
|
1522
1544
|
var AROUND_LIMIT = 20;
|
|
1523
1545
|
var PARENT = `..${sep}`;
|
|
@@ -1538,13 +1560,13 @@ function cut(text) {
|
|
|
1538
1560
|
return text.length > LONGEST_LINE ? `${text.slice(0, LONGEST_LINE - 1)}\u2026` : text;
|
|
1539
1561
|
}
|
|
1540
1562
|
async function readableRoot() {
|
|
1541
|
-
const ran = await runCommand(["git", "rev-parse", "--show-toplevel"], { cwd:
|
|
1542
|
-
return
|
|
1563
|
+
const ran = await runCommand(["git", "rev-parse", "--show-toplevel"], { cwd: process15.cwd() });
|
|
1564
|
+
return realpathSync3(ran.code === 0 ? ran.text.trim() : process15.cwd());
|
|
1543
1565
|
}
|
|
1544
1566
|
async function isOutside(path) {
|
|
1545
1567
|
let real;
|
|
1546
1568
|
try {
|
|
1547
|
-
real =
|
|
1569
|
+
real = realpathSync3(resolve5(process15.cwd(), path));
|
|
1548
1570
|
} catch {
|
|
1549
1571
|
return false;
|
|
1550
1572
|
}
|
|
@@ -1558,7 +1580,7 @@ async function checkInside(path) {
|
|
|
1558
1580
|
async function pipedSpans(text, context) {
|
|
1559
1581
|
const spans = [];
|
|
1560
1582
|
for (const [path, numbers] of locations(text)) {
|
|
1561
|
-
const source = await isOutside(path) ? undefined : await readText(
|
|
1583
|
+
const source = await isOutside(path) ? undefined : await readText(resolve5(process15.cwd(), path));
|
|
1562
1584
|
if (source !== undefined)
|
|
1563
1585
|
spans.push(...spansAround(path, source.replace(TRAILING_NEWLINE2, "").split(`
|
|
1564
1586
|
`).length, numbers, context));
|
|
@@ -1567,7 +1589,7 @@ async function pipedSpans(text, context) {
|
|
|
1567
1589
|
}
|
|
1568
1590
|
async function textOf(path) {
|
|
1569
1591
|
await checkInside(path);
|
|
1570
|
-
const text = await readText(
|
|
1592
|
+
const text = await readText(resolve5(process15.cwd(), path));
|
|
1571
1593
|
if (text === undefined)
|
|
1572
1594
|
fail(`cannot read ${path} (missing, binary or too large)`, 1);
|
|
1573
1595
|
return text;
|
|
@@ -1625,8 +1647,8 @@ async function spanOf(target) {
|
|
|
1625
1647
|
async function* sources(paths) {
|
|
1626
1648
|
for (const path of paths)
|
|
1627
1649
|
await checkInside(path);
|
|
1628
|
-
for (const path of await listFiles(paths,
|
|
1629
|
-
const text = await readText(join7(
|
|
1650
|
+
for (const path of await listFiles(paths, process15.cwd())) {
|
|
1651
|
+
const text = await readText(join7(process15.cwd(), path));
|
|
1630
1652
|
if (text !== undefined)
|
|
1631
1653
|
yield { path, text };
|
|
1632
1654
|
}
|
|
@@ -1650,7 +1672,7 @@ async function definitionsOf(symbols, paths) {
|
|
|
1650
1672
|
return [...found.values()].flat();
|
|
1651
1673
|
}
|
|
1652
1674
|
async function peek(args) {
|
|
1653
|
-
const { values, positionals } = parse(args, USAGE, OPTIONS, { positionals: ["[target...]"], replaces: "sed -n N,Mp, grep -n chains and whole-file reads", examples: [["peek src/a.ts:120-140 src/b.ts:1-40", "two spans in one call"], ["peek --def parseArgs src", "a whole declaration"]] });
|
|
1675
|
+
const { values, positionals } = parse(args, USAGE, OPTIONS, { exits: EXITS, positionals: ["[target...]"], replaces: "sed -n N,Mp, grep -n chains and whole-file reads", examples: [["peek src/a.ts:120-140 src/b.ts:1-40", "two spans in one call"], ["peek --def parseArgs src", "a whole declaration"]] });
|
|
1654
1676
|
const modes = [values.def?.length === 0 ? undefined : values.def, values.refs, values.outline, values.around].filter((mode) => mode !== undefined && mode !== false);
|
|
1655
1677
|
const json = values.json === true;
|
|
1656
1678
|
if (values["defs-first"] === true && values.refs === undefined)
|
|
@@ -1726,4 +1748,4 @@ async function peek(args) {
|
|
|
1726
1748
|
}
|
|
1727
1749
|
|
|
1728
1750
|
// src/peek-cli.ts
|
|
1729
|
-
|
|
1751
|
+
process16.exitCode = await runMain({ name: "peek", run: peek });
|
package/dist/todo-scan-cli.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/todo-scan-cli.ts
|
|
5
|
-
import
|
|
5
|
+
import process16 from "process";
|
|
6
6
|
// ../kit/src/args.ts
|
|
7
7
|
import process9 from "process";
|
|
8
8
|
import { parseArgs } from "util";
|
|
@@ -488,18 +488,14 @@ var FULL_STOP = /\.$/;
|
|
|
488
488
|
function usageLine(usage, colors = paint(process8.stdout)) {
|
|
489
489
|
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
490
490
|
}
|
|
491
|
-
function helpText(usage, options, colors = paint(process8.stdout), { examples = [],
|
|
492
|
-
return [usageLine(usage, colors),
|
|
491
|
+
function helpText(usage, options, colors = paint(process8.stdout), { examples = [], exits = [] } = {}) {
|
|
492
|
+
return [usageLine(usage, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
|
|
493
493
|
`);
|
|
494
494
|
}
|
|
495
495
|
function exitRows(exits, colors = paint(process8.stdout)) {
|
|
496
496
|
if (exits.length === 0)
|
|
497
497
|
return [];
|
|
498
|
-
|
|
499
|
-
return ["", colors.bold("Exits:"), ...exits.map(([code, meaning]) => ` ${colors.cyan(String(code).padEnd(width))} ${meaning}`)];
|
|
500
|
-
}
|
|
501
|
-
function replacesRows(replaces, colors = paint(process8.stdout)) {
|
|
502
|
-
return replaces === undefined || outputMode() === "agent" ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
498
|
+
return ["", `${colors.bold("Exit codes:")} ${exits.map(([code, meaning]) => `${colors.cyan(String(code))} ${meaning}`).join(", ")}`];
|
|
503
499
|
}
|
|
504
500
|
function wantsHelpJson(env = process8.env) {
|
|
505
501
|
return env.KIT_HELP === "json";
|
|
@@ -800,9 +796,33 @@ function nearestOption(message, options, allowNegative) {
|
|
|
800
796
|
const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
|
|
801
797
|
return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
|
|
802
798
|
}
|
|
799
|
+
// ../kit/src/argv.ts
|
|
800
|
+
import { readFileSync as readFileSync3, realpathSync } from "fs";
|
|
801
|
+
import { resolve as resolve4 } from "path";
|
|
802
|
+
import process10 from "process";
|
|
803
|
+
function commandLine() {
|
|
804
|
+
try {
|
|
805
|
+
return readFileSync3("/proc/self/cmdline", "utf8").split("\x00").slice(0, -1);
|
|
806
|
+
} catch {
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
function realPath(word) {
|
|
811
|
+
try {
|
|
812
|
+
return realpathSync(resolve4(word));
|
|
813
|
+
} catch {
|
|
814
|
+
return;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
function scriptArgs(argv = process10.argv.slice(2), cmdline = commandLine(), script = process10.argv[1]) {
|
|
818
|
+
const target = script === undefined ? undefined : realPath(script) ?? script;
|
|
819
|
+
const at = cmdline === undefined || target === undefined ? -1 : cmdline.findIndex((word, index) => index > 0 && (resolve4(word) === script || realPath(word) === target));
|
|
820
|
+
const after = at === -1 ? [] : cmdline.slice(at + 1);
|
|
821
|
+
return after[0] === "--" && after.length === argv.length + 1 && argv.every((word, index) => word === after[index + 1]) ? after : argv;
|
|
822
|
+
}
|
|
803
823
|
// ../kit/src/process.ts
|
|
804
824
|
import { constants } from "os";
|
|
805
|
-
import
|
|
825
|
+
import process11 from "process";
|
|
806
826
|
var KILL_GRACE_MS = 2000;
|
|
807
827
|
function exitCodeOf(code, signal) {
|
|
808
828
|
if (code !== null)
|
|
@@ -822,7 +842,7 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal,
|
|
|
822
842
|
return;
|
|
823
843
|
try {
|
|
824
844
|
if (grouped)
|
|
825
|
-
|
|
845
|
+
process11.kill(-proc.pid, killing);
|
|
826
846
|
else
|
|
827
847
|
proc.kill(killing);
|
|
828
848
|
} catch {}
|
|
@@ -896,15 +916,15 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
|
|
|
896
916
|
var RESET_STYLE = `${ESC}[0m`;
|
|
897
917
|
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
898
918
|
// ../kit/src/cli.ts
|
|
899
|
-
import { readFileSync as
|
|
919
|
+
import { readFileSync as readFileSync4, realpathSync as realpathSync2 } from "fs";
|
|
900
920
|
import { dirname as dirname3, join as join5 } from "path";
|
|
901
|
-
import
|
|
921
|
+
import process13 from "process";
|
|
902
922
|
|
|
903
923
|
// ../kit/src/usage.ts
|
|
904
924
|
import { appendFileSync, mkdirSync } from "fs";
|
|
905
925
|
import { homedir as homedir3 } from "os";
|
|
906
926
|
import { dirname as dirname2, isAbsolute, join as join4 } from "path";
|
|
907
|
-
import
|
|
927
|
+
import process12 from "process";
|
|
908
928
|
|
|
909
929
|
// ../kit/src/logs.ts
|
|
910
930
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -918,14 +938,14 @@ var usageError;
|
|
|
918
938
|
function noteUsageError(message) {
|
|
919
939
|
usageError = message;
|
|
920
940
|
}
|
|
921
|
-
function stateHome(env =
|
|
941
|
+
function stateHome(env = process12.env) {
|
|
922
942
|
const dir = env.XDG_STATE_HOME;
|
|
923
943
|
return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
|
|
924
944
|
}
|
|
925
|
-
function usagePath(env =
|
|
945
|
+
function usagePath(env = process12.env) {
|
|
926
946
|
return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
927
947
|
}
|
|
928
|
-
function usageRecorded(env =
|
|
948
|
+
function usageRecorded(env = process12.env) {
|
|
929
949
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
930
950
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
931
951
|
}
|
|
@@ -939,7 +959,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
939
959
|
usageError = undefined;
|
|
940
960
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
941
961
|
}
|
|
942
|
-
function recordUsage(entry, env =
|
|
962
|
+
function recordUsage(entry, env = process12.env) {
|
|
943
963
|
if (!usageRecorded(env))
|
|
944
964
|
return;
|
|
945
965
|
try {
|
|
@@ -1016,7 +1036,6 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
|
|
|
1016
1036
|
header(colors, summary),
|
|
1017
1037
|
"",
|
|
1018
1038
|
usageLine(`${prefix} ${defaultCommand === undefined ? colors.cyan("<command>") : `[${colors.cyan("<command>")}]`} [options]`, colors),
|
|
1019
|
-
...replacesRows(replaces, colors),
|
|
1020
1039
|
"",
|
|
1021
1040
|
...await commandRows(commands, colors),
|
|
1022
1041
|
...own.length === 0 ? [] : ["", `${colors.bold(`Options of ${defaultCommand}:`)} ${colors.dim("the command run when none is named")}`, ...optionRows(Object.fromEntries(own), colors)],
|
|
@@ -1030,8 +1049,8 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
|
|
|
1030
1049
|
`);
|
|
1031
1050
|
}
|
|
1032
1051
|
function printError(prefix, message, hint, cause) {
|
|
1033
|
-
const colors = paint(
|
|
1034
|
-
const styled = takesControlCodes(
|
|
1052
|
+
const colors = paint(process13.stderr);
|
|
1053
|
+
const styled = takesControlCodes(process13.stderr);
|
|
1035
1054
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
1036
1055
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
1037
1056
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -1074,11 +1093,11 @@ async function dispatch(level, typed, { version, redact }) {
|
|
|
1074
1093
|
}
|
|
1075
1094
|
}
|
|
1076
1095
|
if (name === undefined && level.path.length > 1) {
|
|
1077
|
-
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
1096
|
+
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process13.stderr));
|
|
1078
1097
|
return 2;
|
|
1079
1098
|
}
|
|
1080
1099
|
if (name === undefined || name === "help" || name === "--help" || name === "-h") {
|
|
1081
|
-
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
1100
|
+
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process13.stdout));
|
|
1082
1101
|
return 0;
|
|
1083
1102
|
}
|
|
1084
1103
|
if (level.path.length === 1 && (name === "--version" || name === "-v")) {
|
|
@@ -1134,24 +1153,24 @@ function candidateLine(candidate) {
|
|
|
1134
1153
|
}
|
|
1135
1154
|
function positionalBeingTyped(before, { options, positionals }) {
|
|
1136
1155
|
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
1137
|
-
|
|
1156
|
+
const typed = [];
|
|
1138
1157
|
let at = 0;
|
|
1139
1158
|
while (at < before.length) {
|
|
1140
1159
|
const word = before[at];
|
|
1141
1160
|
if (word === "--") {
|
|
1142
|
-
|
|
1161
|
+
typed.push(...before.slice(at + 1));
|
|
1143
1162
|
break;
|
|
1144
1163
|
}
|
|
1145
1164
|
if (!word.startsWith("-"))
|
|
1146
|
-
|
|
1165
|
+
typed.push(word);
|
|
1147
1166
|
else if (takesValue(word))
|
|
1148
1167
|
at++;
|
|
1149
1168
|
at++;
|
|
1150
1169
|
}
|
|
1151
1170
|
const last = positionals.at(-1);
|
|
1152
|
-
if (
|
|
1153
|
-
return positionals[
|
|
1154
|
-
return last?.replace("]", "").endsWith("...") === true ? last : undefined;
|
|
1171
|
+
if (typed.length < positionals.length)
|
|
1172
|
+
return { name: positionals[typed.length], typed };
|
|
1173
|
+
return { name: last?.replace("]", "").endsWith("...") === true ? last : undefined, typed };
|
|
1155
1174
|
}
|
|
1156
1175
|
async function leafCandidates(run, words) {
|
|
1157
1176
|
const completing = await completingOf(run);
|
|
@@ -1159,12 +1178,12 @@ async function leafCandidates(run, words) {
|
|
|
1159
1178
|
return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
|
|
1160
1179
|
const flag = words.at(-2);
|
|
1161
1180
|
const option = flag?.startsWith("-") === true ? Object.entries(completing.options).find(([name, { short }]) => flag === `--${name}` || flag === `-${short}`)?.[1] : undefined;
|
|
1181
|
+
const { name, typed } = positionalBeingTyped(words.slice(0, -1), completing);
|
|
1162
1182
|
if (option?.type === "string") {
|
|
1163
|
-
const values = option.choices === undefined ? await option.candidates?.() : option.choices;
|
|
1183
|
+
const values = option.choices === undefined ? await option.candidates?.(typed) : option.choices;
|
|
1164
1184
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
1165
1185
|
}
|
|
1166
|
-
const
|
|
1167
|
-
const values = name === undefined ? undefined : await completing.candidates[name]?.();
|
|
1186
|
+
const values = name === undefined ? undefined : await completing.candidates[name]?.(typed);
|
|
1168
1187
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
1169
1188
|
}
|
|
1170
1189
|
async function candidates(scope, defaultCommand, words, before = []) {
|
|
@@ -1195,9 +1214,13 @@ async function printCandidates(found) {
|
|
|
1195
1214
|
return 0;
|
|
1196
1215
|
}
|
|
1197
1216
|
function packageVersion(entry = Bun.main) {
|
|
1198
|
-
|
|
1217
|
+
let resolved = entry;
|
|
1218
|
+
try {
|
|
1219
|
+
resolved = realpathSync2(entry);
|
|
1220
|
+
} catch {}
|
|
1221
|
+
for (let dir = dirname3(resolved);; dir = dirname3(dir)) {
|
|
1199
1222
|
try {
|
|
1200
|
-
const { version } = JSON.parse(
|
|
1223
|
+
const { version } = JSON.parse(readFileSync4(join5(dir, "package.json"), "utf8"));
|
|
1201
1224
|
if (typeof version === "string")
|
|
1202
1225
|
return version;
|
|
1203
1226
|
} catch {}
|
|
@@ -1205,7 +1228,7 @@ function packageVersion(entry = Bun.main) {
|
|
|
1205
1228
|
return "0.0.0";
|
|
1206
1229
|
}
|
|
1207
1230
|
}
|
|
1208
|
-
async function runCli(cli, argv =
|
|
1231
|
+
async function runCli(cli, argv = scriptArgs()) {
|
|
1209
1232
|
const version = cli.version ?? packageVersion();
|
|
1210
1233
|
if (argv[0] === COMPLETE)
|
|
1211
1234
|
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
@@ -1232,7 +1255,7 @@ function commandPath(scope, argv) {
|
|
|
1232
1255
|
}
|
|
1233
1256
|
return path.length === 0 ? undefined : path.join(" ");
|
|
1234
1257
|
}
|
|
1235
|
-
async function runMain({ name, version, run, redact, usage }, argv =
|
|
1258
|
+
async function runMain({ name, version, run, redact, usage }, argv = scriptArgs()) {
|
|
1236
1259
|
if (argv[0] === COMPLETE)
|
|
1237
1260
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
1238
1261
|
if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
|
|
@@ -1288,7 +1311,7 @@ var SESSION_FILES = new Glob("*.json");
|
|
|
1288
1311
|
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
1289
1312
|
// src/todo-scan.ts
|
|
1290
1313
|
import { join as join7 } from "path";
|
|
1291
|
-
import
|
|
1314
|
+
import process15 from "process";
|
|
1292
1315
|
|
|
1293
1316
|
// src/files.ts
|
|
1294
1317
|
import { existsSync as existsSync4, readdirSync, statSync as statSync2 } from "fs";
|
|
@@ -1404,9 +1427,8 @@ var USAGE = `todo-scan [options] [path...]: lists the TODO, FIXME and owner/repo
|
|
|
1404
1427
|
|
|
1405
1428
|
--stale keeps only the comments naming a closed issue, each issue's state looked up with ft, or gh for a GitHub repository, and exits 1 when it finds one.
|
|
1406
1429
|
|
|
1407
|
-
Lists at most 100 comments, the count of the rest after them (--limit <n>, --full for all); --json is never cut unless --limit is given
|
|
1408
|
-
|
|
1409
|
-
Exits 0 when it listed the comments or found no stale one, 1 when --stale finds one, 2 on a usage error or when --stale could not look a repository up (ft and gh both failed), so a clean exit never hides an unchecked issue.`;
|
|
1430
|
+
Lists at most 100 comments, the count of the rest after them (--limit <n>, --full for all); --json is never cut unless --limit is given.`;
|
|
1431
|
+
var EXITS = [[0, "it listed the comments or found no stale one"], [1, "--stale finds one"], [2, "a usage error or when --stale could not look a repository up (ft and gh both failed)"]];
|
|
1410
1432
|
var LIST_LIMIT = 100;
|
|
1411
1433
|
var OPTIONS = {
|
|
1412
1434
|
stale: { type: "boolean", description: "only the comments naming a closed issue, looked up with ft or gh" },
|
|
@@ -1420,8 +1442,8 @@ function printLines(lines, footer) {
|
|
|
1420
1442
|
`));
|
|
1421
1443
|
}
|
|
1422
1444
|
async function todoScan(args) {
|
|
1423
|
-
const { values, positionals } = parse(args, USAGE, OPTIONS, { positionals: ["[path...]"], replaces: "grep -rn TODO, then opening each issue it names", examples: ["todo-scan src", ["todo-scan --stale", "only the comments naming a closed issue"]] });
|
|
1424
|
-
const cwd =
|
|
1445
|
+
const { values, positionals } = parse(args, USAGE, OPTIONS, { exits: EXITS, positionals: ["[path...]"], replaces: "grep -rn TODO, then opening each issue it names", examples: ["todo-scan src", ["todo-scan --stale", "only the comments naming a closed issue"]] });
|
|
1446
|
+
const cwd = process15.cwd();
|
|
1425
1447
|
const limit = limitOf(values, LIST_LIMIT);
|
|
1426
1448
|
const marks = [];
|
|
1427
1449
|
for (const path of await listFiles(positionals, cwd)) {
|
|
@@ -1452,4 +1474,4 @@ async function todoScan(args) {
|
|
|
1452
1474
|
}
|
|
1453
1475
|
|
|
1454
1476
|
// src/todo-scan-cli.ts
|
|
1455
|
-
|
|
1477
|
+
process16.exitCode = await runMain({ name: "todo-scan", run: todoScan });
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-code",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.14.
|
|
4
|
+
"version": "0.14.3",
|
|
5
5
|
"description": "peek, todo-scan and hotspots: source read by spans, symbols and outlines in one call, the TODO and issue-reference comments of a repository with stale ones flagged, and the files ranked by complexity times git churn.",
|
|
6
6
|
"author": "Kaynooo <contact@kaynooo.fr>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -46,6 +46,6 @@
|
|
|
46
46
|
"@kaynooo/utils": "3.0.0"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@kaynooo/cli-kit": "0.
|
|
49
|
+
"@kaynooo/cli-kit": "0.38.2"
|
|
50
50
|
}
|
|
51
51
|
}
|