@kaynooo/cli-code 0.8.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +1888 -0
- package/dist/{hotspots.js → hotspots-cli.js} +192 -6
- package/dist/{peek.js → peek-cli.js} +193 -7
- package/dist/{todo-scan.js → todo-scan-cli.js} +192 -6
- package/package.json +11 -9
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env -S bun --no-env-file
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
|
-
// src/hotspots.ts
|
|
5
|
-
import
|
|
6
|
-
import process10 from "process";
|
|
4
|
+
// src/hotspots-cli.ts
|
|
5
|
+
import process11 from "process";
|
|
7
6
|
// ../kit/src/args.ts
|
|
8
7
|
import process5 from "process";
|
|
9
8
|
import { parseArgs } from "util";
|
|
@@ -835,6 +834,79 @@ var LEVEL_OPTIONS = {
|
|
|
835
834
|
help: { type: "boolean", short: "h", description: "show this help" }
|
|
836
835
|
};
|
|
837
836
|
var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
|
|
837
|
+
async function commandRows(all, colors) {
|
|
838
|
+
const commands = shown(all);
|
|
839
|
+
const width = Math.max(0, ...Object.keys(commands).map((name) => name.length));
|
|
840
|
+
const groups = new Map([["Commands:", []]]);
|
|
841
|
+
const summaries = await Promise.all(Object.values(commands).map(async ({ summary }) => resolveSummary(summary)));
|
|
842
|
+
for (const [index, [name, { group }]] of Object.entries(commands).entries()) {
|
|
843
|
+
const heading = group === undefined ? "Commands:" : `${group}:`;
|
|
844
|
+
const rows = groups.get(heading) ?? [];
|
|
845
|
+
rows.push(` ${colors.cyan(name.padEnd(width))} ${colors.dim(summaries[index])}`);
|
|
846
|
+
groups.set(heading, rows);
|
|
847
|
+
}
|
|
848
|
+
return [...groups].filter(([, rows]) => rows.length > 0).flatMap(([heading, rows], index) => [...index === 0 ? [] : [""], colors.bold(heading), ...rows]);
|
|
849
|
+
}
|
|
850
|
+
function shown(commands) {
|
|
851
|
+
return Object.fromEntries(Object.entries(commands).filter(([, { hidden }]) => hidden !== true));
|
|
852
|
+
}
|
|
853
|
+
function takeOutputFlags(argv) {
|
|
854
|
+
let index = 0;
|
|
855
|
+
for (;index < argv.length; index++) {
|
|
856
|
+
const word = argv[index];
|
|
857
|
+
if (word === "--plain")
|
|
858
|
+
applyOutput(undefined, true);
|
|
859
|
+
else if (word.startsWith("--color="))
|
|
860
|
+
applyOutput(word.slice("--color=".length), undefined);
|
|
861
|
+
else if (word === "--color" && argv[index + 1] !== undefined)
|
|
862
|
+
applyOutput(argv[++index], undefined);
|
|
863
|
+
else
|
|
864
|
+
break;
|
|
865
|
+
}
|
|
866
|
+
return argv.slice(index);
|
|
867
|
+
}
|
|
868
|
+
async function resolveSummary(summary) {
|
|
869
|
+
return typeof summary === "string" ? summary : summary();
|
|
870
|
+
}
|
|
871
|
+
function scopeOf(commands, extra, aliases) {
|
|
872
|
+
return { commands: typeof commands === "function" ? {} : commands, lazy: [...typeof commands === "function" ? [commands] : [], ...extra === undefined ? [] : [extra]], aliases };
|
|
873
|
+
}
|
|
874
|
+
async function expand(scope, words) {
|
|
875
|
+
if (scope.lazy.length === 0)
|
|
876
|
+
return scope;
|
|
877
|
+
const commands = { ...scope.commands };
|
|
878
|
+
for (const added of await Promise.all(scope.lazy.map(async (lazy) => lazy(words)))) {
|
|
879
|
+
for (const [name, command] of Object.entries(added))
|
|
880
|
+
commands[name] ??= command;
|
|
881
|
+
}
|
|
882
|
+
return { ...scope, commands, lazy: [] };
|
|
883
|
+
}
|
|
884
|
+
async function help2({ path, header, summary: lazySummary, commands, defaultCommand, examples, replaces }, stream) {
|
|
885
|
+
const colors = paint(stream);
|
|
886
|
+
const prefix = path.join(" ");
|
|
887
|
+
const summary = await resolveSummary(lazySummary);
|
|
888
|
+
if (wantsHelpJson())
|
|
889
|
+
return JSON.stringify(description(`${prefix} <command> [options]: ${summary}`, { examples, replaces }));
|
|
890
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
891
|
+
const options = fallback !== undefined && "run" in fallback ? await optionsOf(fallback.run) : undefined;
|
|
892
|
+
const own = Object.entries(options ?? {}).filter(([name]) => !(name in TOP_OPTIONS));
|
|
893
|
+
return [
|
|
894
|
+
header(colors, summary),
|
|
895
|
+
"",
|
|
896
|
+
usageLine(`${prefix} ${defaultCommand === undefined ? colors.cyan("<command>") : `[${colors.cyan("<command>")}]`} [options]`, colors),
|
|
897
|
+
...replacesRows(replaces, colors),
|
|
898
|
+
"",
|
|
899
|
+
...await commandRows(commands, colors),
|
|
900
|
+
...own.length === 0 ? [] : ["", `${colors.bold(`Options of ${defaultCommand}:`)} ${colors.dim("the command run when none is named")}`, ...optionRows(Object.fromEntries(own), colors)],
|
|
901
|
+
"",
|
|
902
|
+
colors.bold("Options:"),
|
|
903
|
+
...optionRows(path.length === 1 ? TOP_OPTIONS : LEVEL_OPTIONS, colors),
|
|
904
|
+
...exampleRows(examples ?? [], colors),
|
|
905
|
+
"",
|
|
906
|
+
`${colors.dim("Run")} ${prefix} ${colors.cyan("<command>")} --help ${colors.dim("for its options.")}`
|
|
907
|
+
].join(`
|
|
908
|
+
`);
|
|
909
|
+
}
|
|
838
910
|
function printError(prefix, message, hint, cause) {
|
|
839
911
|
const colors = paint(process8.stderr);
|
|
840
912
|
const styled = takesControlCodes(process8.stderr);
|
|
@@ -856,12 +928,68 @@ function reportThrown(prefix, error, redact = (text) => text, json = false) {
|
|
|
856
928
|
const help = error instanceof UsageError ? `run ${prefix} --help for its options` : undefined;
|
|
857
929
|
const named = error instanceof Error && !(error instanceof Exit) && error.name !== "Error" ? `${error.name}: ${error.message}` : undefined;
|
|
858
930
|
const cause = error instanceof Error && error.cause instanceof Error ? redact(error.cause.message) : undefined;
|
|
859
|
-
if (error instanceof
|
|
931
|
+
if (error instanceof Exit && error.code === 2)
|
|
860
932
|
noteUsageError(error.message);
|
|
861
933
|
const report = json ? printJsonError : (message, hint, causeText) => printError(prefix, message, hint, causeText);
|
|
862
934
|
report(redact(named ?? (error instanceof Error ? error.message : String(error))), error instanceof Exit ? error.hint ?? help : undefined, cause);
|
|
863
935
|
return error instanceof Exit ? error.code : 1;
|
|
864
936
|
}
|
|
937
|
+
async function dispatch(level, typed, { version, redact }) {
|
|
938
|
+
const prefix = level.path.join(" ");
|
|
939
|
+
let argv;
|
|
940
|
+
try {
|
|
941
|
+
argv = takeOutputFlags(typed);
|
|
942
|
+
} catch (error) {
|
|
943
|
+
return reportThrown(prefix, error);
|
|
944
|
+
}
|
|
945
|
+
const [name, ...args] = argv;
|
|
946
|
+
const fallback = level.defaultCommand === undefined ? undefined : level.commands[level.defaultCommand];
|
|
947
|
+
if (name === undefined && level.runBare && fallback !== undefined && "run" in fallback) {
|
|
948
|
+
try {
|
|
949
|
+
return await fallback.run([]);
|
|
950
|
+
} catch (error) {
|
|
951
|
+
return reportThrown(`${prefix} ${level.defaultCommand}`, error, redact, false);
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
if (name === undefined && level.path.length > 1) {
|
|
955
|
+
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process8.stderr));
|
|
956
|
+
return 2;
|
|
957
|
+
}
|
|
958
|
+
if (name === undefined || name === "help" || name === "--help" || name === "-h") {
|
|
959
|
+
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process8.stdout));
|
|
960
|
+
return 0;
|
|
961
|
+
}
|
|
962
|
+
if (level.path.length === 1 && (name === "--version" || name === "-v")) {
|
|
963
|
+
console.log(version);
|
|
964
|
+
return 0;
|
|
965
|
+
}
|
|
966
|
+
let command = level.commands[level.aliases?.[name] ?? name];
|
|
967
|
+
let rest = args;
|
|
968
|
+
let ran = name;
|
|
969
|
+
if (command === undefined && level.lazy.length > 0) {
|
|
970
|
+
level = await expand(level, [...level.path.slice(1), ...argv]);
|
|
971
|
+
command = level.commands[level.aliases?.[name] ?? name];
|
|
972
|
+
}
|
|
973
|
+
if (command === undefined && level.defaultCommand !== undefined) {
|
|
974
|
+
command = level.commands[level.defaultCommand];
|
|
975
|
+
rest = argv;
|
|
976
|
+
ran = level.defaultCommand;
|
|
977
|
+
}
|
|
978
|
+
if (command === undefined) {
|
|
979
|
+
const meant = closest(name, Object.keys(shown(level.commands)));
|
|
980
|
+
printError(prefix, `no command ${name}`, meant === undefined ? `run ${prefix} --help for its commands` : `did you mean ${meant}?`);
|
|
981
|
+
return 2;
|
|
982
|
+
}
|
|
983
|
+
if ("commands" in command) {
|
|
984
|
+
const { summary } = command;
|
|
985
|
+
return dispatch({ path: [...level.path, name], header: (colors, text) => `${colors.bold(`${prefix} ${name}`)} ${text}`, summary, ...scopeOf(command.commands, command.extraCommands, command.aliases), defaultCommand: undefined, runBare: false, examples: command.examples, replaces: undefined }, rest, { version, redact });
|
|
986
|
+
}
|
|
987
|
+
try {
|
|
988
|
+
return await command.run(rest);
|
|
989
|
+
} catch (error) {
|
|
990
|
+
return reportThrown(`${prefix} ${ran}`, error, redact, wantsJson(rest));
|
|
991
|
+
}
|
|
992
|
+
}
|
|
865
993
|
var COMPLETE = "__complete";
|
|
866
994
|
var FILES = "__files__";
|
|
867
995
|
async function completingOf(run) {
|
|
@@ -876,6 +1004,9 @@ async function completingOf(run) {
|
|
|
876
1004
|
}
|
|
877
1005
|
return new Completing({});
|
|
878
1006
|
}
|
|
1007
|
+
async function optionsOf(run) {
|
|
1008
|
+
return (await completingOf(run)).options;
|
|
1009
|
+
}
|
|
879
1010
|
function candidateLine(candidate) {
|
|
880
1011
|
return typeof candidate === "string" ? candidate : candidate.join("\t");
|
|
881
1012
|
}
|
|
@@ -914,6 +1045,28 @@ async function leafCandidates(run, words) {
|
|
|
914
1045
|
const values = name === undefined ? undefined : await completing.candidates[name]?.();
|
|
915
1046
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
916
1047
|
}
|
|
1048
|
+
async function candidates(scope, defaultCommand, words, before = []) {
|
|
1049
|
+
const [word = "", ...rest] = words;
|
|
1050
|
+
const listing = rest.length === 0 && !word.startsWith("-");
|
|
1051
|
+
let { commands, aliases } = scope;
|
|
1052
|
+
let command = commands[aliases?.[word] ?? word];
|
|
1053
|
+
if (listing || command === undefined) {
|
|
1054
|
+
({ commands, aliases } = await expand(scope, [...before, ...words]));
|
|
1055
|
+
command = commands[aliases?.[word] ?? word];
|
|
1056
|
+
}
|
|
1057
|
+
if (listing) {
|
|
1058
|
+
const own = await Promise.all(Object.entries(shown(commands)).map(async ([name, { summary }]) => `${name} ${await resolveSummary(summary)}`));
|
|
1059
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
1060
|
+
const values = fallback !== undefined && "run" in fallback ? await leafCandidates(fallback.run, [word]) : [];
|
|
1061
|
+
const valueOf = (line) => line.slice(0, line.includes("\t") ? line.indexOf("\t") : undefined);
|
|
1062
|
+
return [...own, ...values.filter((line) => line !== FILES && !(valueOf(line) in commands))];
|
|
1063
|
+
}
|
|
1064
|
+
if (command === undefined) {
|
|
1065
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
1066
|
+
return fallback !== undefined && "run" in fallback ? leafCandidates(fallback.run, words) : [];
|
|
1067
|
+
}
|
|
1068
|
+
return "commands" in command ? candidates(scopeOf(command.commands, command.extraCommands, command.aliases), undefined, rest, [...before, word]) : leafCandidates(command.run, rest);
|
|
1069
|
+
}
|
|
917
1070
|
async function printCandidates(found) {
|
|
918
1071
|
for (const line of await found)
|
|
919
1072
|
console.log(line);
|
|
@@ -930,6 +1083,33 @@ function packageVersion(entry = Bun.main) {
|
|
|
930
1083
|
return "0.0.0";
|
|
931
1084
|
}
|
|
932
1085
|
}
|
|
1086
|
+
async function runCli(cli, argv = process8.argv.slice(2)) {
|
|
1087
|
+
const version = cli.version ?? packageVersion();
|
|
1088
|
+
if (argv[0] === COMPLETE)
|
|
1089
|
+
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
1090
|
+
const code = await dispatch({ path: [cli.name], header: (colors, about) => `${colors.bold(cli.name)} ${colors.dim(version)} ${about}`, summary: cli.about, ...scopeOf(cli.commands, cli.extraCommands, cli.aliases), defaultCommand: cli.defaultCommand, runBare: cli.runBare === true, examples: cli.examples, replaces: cli.replaces }, argv, { version, redact: cli.redact });
|
|
1091
|
+
if (cli.usage !== false) {
|
|
1092
|
+
try {
|
|
1093
|
+
recordUsage(usageEntry(cli.name, commandPath(scopeOf(cli.commands, cli.extraCommands, cli.aliases), argv), argv, code));
|
|
1094
|
+
} catch {}
|
|
1095
|
+
}
|
|
1096
|
+
return code;
|
|
1097
|
+
}
|
|
1098
|
+
function commandPath(scope, argv) {
|
|
1099
|
+
const path = [];
|
|
1100
|
+
let level = scope;
|
|
1101
|
+
for (const word of argv.filter((each) => !each.startsWith("-"))) {
|
|
1102
|
+
const name = level.aliases !== undefined && Object.hasOwn(level.aliases, word) ? level.aliases[word] ?? word : word;
|
|
1103
|
+
const command = Object.hasOwn(level.commands, name) ? level.commands[name] : undefined;
|
|
1104
|
+
if (command === undefined)
|
|
1105
|
+
break;
|
|
1106
|
+
path.push(word);
|
|
1107
|
+
if (!("commands" in command))
|
|
1108
|
+
break;
|
|
1109
|
+
level = scopeOf(command.commands, command.extraCommands, command.aliases);
|
|
1110
|
+
}
|
|
1111
|
+
return path.length === 0 ? undefined : path.join(" ");
|
|
1112
|
+
}
|
|
933
1113
|
async function runMain({ name, version, run, redact, usage }, argv = process8.argv.slice(2)) {
|
|
934
1114
|
if (argv[0] === COMPLETE)
|
|
935
1115
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
@@ -977,6 +1157,10 @@ var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "ses
|
|
|
977
1157
|
var {Glob } = globalThis.Bun;
|
|
978
1158
|
var SESSION_FILES = new Glob("*.json");
|
|
979
1159
|
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
1160
|
+
// src/hotspots.ts
|
|
1161
|
+
import { join as join4 } from "path";
|
|
1162
|
+
import process10 from "process";
|
|
1163
|
+
|
|
980
1164
|
// src/churn.ts
|
|
981
1165
|
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)$/;
|
|
982
1166
|
var TESTS = /(?:^|\/)(?:tests?|__tests__|spec|e2e|__mocks__|fixtures)\/|\.(?:test|spec|bench)\.[^/]+$|(?:^|\/)test_[^/]+$|_test\.(?:go|py)$/;
|
|
@@ -1126,7 +1310,7 @@ var OPTIONS = {
|
|
|
1126
1310
|
all: { type: "boolean", short: "a", description: "keep test files and generated files" },
|
|
1127
1311
|
json: jsonOption("[{rank, path, score, churn, recent, complexity, worst, lines}], never cut unless --limit is given")
|
|
1128
1312
|
};
|
|
1129
|
-
async function
|
|
1313
|
+
async function hotspots(args) {
|
|
1130
1314
|
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'] });
|
|
1131
1315
|
if (positionals.length > 1)
|
|
1132
1316
|
badUsage("give one folder");
|
|
@@ -1167,4 +1351,6 @@ async function main(args) {
|
|
|
1167
1351
|
console.log(footer);
|
|
1168
1352
|
return 0;
|
|
1169
1353
|
}
|
|
1170
|
-
|
|
1354
|
+
|
|
1355
|
+
// src/hotspots-cli.ts
|
|
1356
|
+
process11.exitCode = await runMain({ name: "hotspots", run: hotspots });
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env -S bun --no-env-file
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
|
-
// src/peek.ts
|
|
5
|
-
import
|
|
6
|
-
import { isAbsolute as isAbsolute2, join as join4, relative as relative2, resolve, sep } from "path";
|
|
7
|
-
import process10 from "process";
|
|
4
|
+
// src/peek-cli.ts
|
|
5
|
+
import process11 from "process";
|
|
8
6
|
// ../kit/src/args.ts
|
|
9
7
|
import process5 from "process";
|
|
10
8
|
import { parseArgs } from "util";
|
|
@@ -836,6 +834,79 @@ var LEVEL_OPTIONS = {
|
|
|
836
834
|
help: { type: "boolean", short: "h", description: "show this help" }
|
|
837
835
|
};
|
|
838
836
|
var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
|
|
837
|
+
async function commandRows(all, colors) {
|
|
838
|
+
const commands = shown(all);
|
|
839
|
+
const width = Math.max(0, ...Object.keys(commands).map((name) => name.length));
|
|
840
|
+
const groups = new Map([["Commands:", []]]);
|
|
841
|
+
const summaries = await Promise.all(Object.values(commands).map(async ({ summary }) => resolveSummary(summary)));
|
|
842
|
+
for (const [index, [name, { group }]] of Object.entries(commands).entries()) {
|
|
843
|
+
const heading = group === undefined ? "Commands:" : `${group}:`;
|
|
844
|
+
const rows = groups.get(heading) ?? [];
|
|
845
|
+
rows.push(` ${colors.cyan(name.padEnd(width))} ${colors.dim(summaries[index])}`);
|
|
846
|
+
groups.set(heading, rows);
|
|
847
|
+
}
|
|
848
|
+
return [...groups].filter(([, rows]) => rows.length > 0).flatMap(([heading, rows], index) => [...index === 0 ? [] : [""], colors.bold(heading), ...rows]);
|
|
849
|
+
}
|
|
850
|
+
function shown(commands) {
|
|
851
|
+
return Object.fromEntries(Object.entries(commands).filter(([, { hidden }]) => hidden !== true));
|
|
852
|
+
}
|
|
853
|
+
function takeOutputFlags(argv) {
|
|
854
|
+
let index = 0;
|
|
855
|
+
for (;index < argv.length; index++) {
|
|
856
|
+
const word = argv[index];
|
|
857
|
+
if (word === "--plain")
|
|
858
|
+
applyOutput(undefined, true);
|
|
859
|
+
else if (word.startsWith("--color="))
|
|
860
|
+
applyOutput(word.slice("--color=".length), undefined);
|
|
861
|
+
else if (word === "--color" && argv[index + 1] !== undefined)
|
|
862
|
+
applyOutput(argv[++index], undefined);
|
|
863
|
+
else
|
|
864
|
+
break;
|
|
865
|
+
}
|
|
866
|
+
return argv.slice(index);
|
|
867
|
+
}
|
|
868
|
+
async function resolveSummary(summary) {
|
|
869
|
+
return typeof summary === "string" ? summary : summary();
|
|
870
|
+
}
|
|
871
|
+
function scopeOf(commands, extra, aliases) {
|
|
872
|
+
return { commands: typeof commands === "function" ? {} : commands, lazy: [...typeof commands === "function" ? [commands] : [], ...extra === undefined ? [] : [extra]], aliases };
|
|
873
|
+
}
|
|
874
|
+
async function expand(scope, words) {
|
|
875
|
+
if (scope.lazy.length === 0)
|
|
876
|
+
return scope;
|
|
877
|
+
const commands = { ...scope.commands };
|
|
878
|
+
for (const added of await Promise.all(scope.lazy.map(async (lazy) => lazy(words)))) {
|
|
879
|
+
for (const [name, command] of Object.entries(added))
|
|
880
|
+
commands[name] ??= command;
|
|
881
|
+
}
|
|
882
|
+
return { ...scope, commands, lazy: [] };
|
|
883
|
+
}
|
|
884
|
+
async function help2({ path, header, summary: lazySummary, commands, defaultCommand, examples, replaces }, stream) {
|
|
885
|
+
const colors = paint(stream);
|
|
886
|
+
const prefix = path.join(" ");
|
|
887
|
+
const summary = await resolveSummary(lazySummary);
|
|
888
|
+
if (wantsHelpJson())
|
|
889
|
+
return JSON.stringify(description(`${prefix} <command> [options]: ${summary}`, { examples, replaces }));
|
|
890
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
891
|
+
const options = fallback !== undefined && "run" in fallback ? await optionsOf(fallback.run) : undefined;
|
|
892
|
+
const own = Object.entries(options ?? {}).filter(([name]) => !(name in TOP_OPTIONS));
|
|
893
|
+
return [
|
|
894
|
+
header(colors, summary),
|
|
895
|
+
"",
|
|
896
|
+
usageLine(`${prefix} ${defaultCommand === undefined ? colors.cyan("<command>") : `[${colors.cyan("<command>")}]`} [options]`, colors),
|
|
897
|
+
...replacesRows(replaces, colors),
|
|
898
|
+
"",
|
|
899
|
+
...await commandRows(commands, colors),
|
|
900
|
+
...own.length === 0 ? [] : ["", `${colors.bold(`Options of ${defaultCommand}:`)} ${colors.dim("the command run when none is named")}`, ...optionRows(Object.fromEntries(own), colors)],
|
|
901
|
+
"",
|
|
902
|
+
colors.bold("Options:"),
|
|
903
|
+
...optionRows(path.length === 1 ? TOP_OPTIONS : LEVEL_OPTIONS, colors),
|
|
904
|
+
...exampleRows(examples ?? [], colors),
|
|
905
|
+
"",
|
|
906
|
+
`${colors.dim("Run")} ${prefix} ${colors.cyan("<command>")} --help ${colors.dim("for its options.")}`
|
|
907
|
+
].join(`
|
|
908
|
+
`);
|
|
909
|
+
}
|
|
839
910
|
function printError(prefix, message, hint, cause) {
|
|
840
911
|
const colors = paint(process8.stderr);
|
|
841
912
|
const styled = takesControlCodes(process8.stderr);
|
|
@@ -857,12 +928,68 @@ function reportThrown(prefix, error, redact = (text) => text, json = false) {
|
|
|
857
928
|
const help = error instanceof UsageError ? `run ${prefix} --help for its options` : undefined;
|
|
858
929
|
const named = error instanceof Error && !(error instanceof Exit) && error.name !== "Error" ? `${error.name}: ${error.message}` : undefined;
|
|
859
930
|
const cause = error instanceof Error && error.cause instanceof Error ? redact(error.cause.message) : undefined;
|
|
860
|
-
if (error instanceof
|
|
931
|
+
if (error instanceof Exit && error.code === 2)
|
|
861
932
|
noteUsageError(error.message);
|
|
862
933
|
const report = json ? printJsonError : (message, hint, causeText) => printError(prefix, message, hint, causeText);
|
|
863
934
|
report(redact(named ?? (error instanceof Error ? error.message : String(error))), error instanceof Exit ? error.hint ?? help : undefined, cause);
|
|
864
935
|
return error instanceof Exit ? error.code : 1;
|
|
865
936
|
}
|
|
937
|
+
async function dispatch(level, typed, { version, redact }) {
|
|
938
|
+
const prefix = level.path.join(" ");
|
|
939
|
+
let argv;
|
|
940
|
+
try {
|
|
941
|
+
argv = takeOutputFlags(typed);
|
|
942
|
+
} catch (error) {
|
|
943
|
+
return reportThrown(prefix, error);
|
|
944
|
+
}
|
|
945
|
+
const [name, ...args] = argv;
|
|
946
|
+
const fallback = level.defaultCommand === undefined ? undefined : level.commands[level.defaultCommand];
|
|
947
|
+
if (name === undefined && level.runBare && fallback !== undefined && "run" in fallback) {
|
|
948
|
+
try {
|
|
949
|
+
return await fallback.run([]);
|
|
950
|
+
} catch (error) {
|
|
951
|
+
return reportThrown(`${prefix} ${level.defaultCommand}`, error, redact, false);
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
if (name === undefined && level.path.length > 1) {
|
|
955
|
+
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process8.stderr));
|
|
956
|
+
return 2;
|
|
957
|
+
}
|
|
958
|
+
if (name === undefined || name === "help" || name === "--help" || name === "-h") {
|
|
959
|
+
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process8.stdout));
|
|
960
|
+
return 0;
|
|
961
|
+
}
|
|
962
|
+
if (level.path.length === 1 && (name === "--version" || name === "-v")) {
|
|
963
|
+
console.log(version);
|
|
964
|
+
return 0;
|
|
965
|
+
}
|
|
966
|
+
let command = level.commands[level.aliases?.[name] ?? name];
|
|
967
|
+
let rest = args;
|
|
968
|
+
let ran = name;
|
|
969
|
+
if (command === undefined && level.lazy.length > 0) {
|
|
970
|
+
level = await expand(level, [...level.path.slice(1), ...argv]);
|
|
971
|
+
command = level.commands[level.aliases?.[name] ?? name];
|
|
972
|
+
}
|
|
973
|
+
if (command === undefined && level.defaultCommand !== undefined) {
|
|
974
|
+
command = level.commands[level.defaultCommand];
|
|
975
|
+
rest = argv;
|
|
976
|
+
ran = level.defaultCommand;
|
|
977
|
+
}
|
|
978
|
+
if (command === undefined) {
|
|
979
|
+
const meant = closest(name, Object.keys(shown(level.commands)));
|
|
980
|
+
printError(prefix, `no command ${name}`, meant === undefined ? `run ${prefix} --help for its commands` : `did you mean ${meant}?`);
|
|
981
|
+
return 2;
|
|
982
|
+
}
|
|
983
|
+
if ("commands" in command) {
|
|
984
|
+
const { summary } = command;
|
|
985
|
+
return dispatch({ path: [...level.path, name], header: (colors, text) => `${colors.bold(`${prefix} ${name}`)} ${text}`, summary, ...scopeOf(command.commands, command.extraCommands, command.aliases), defaultCommand: undefined, runBare: false, examples: command.examples, replaces: undefined }, rest, { version, redact });
|
|
986
|
+
}
|
|
987
|
+
try {
|
|
988
|
+
return await command.run(rest);
|
|
989
|
+
} catch (error) {
|
|
990
|
+
return reportThrown(`${prefix} ${ran}`, error, redact, wantsJson(rest));
|
|
991
|
+
}
|
|
992
|
+
}
|
|
866
993
|
var COMPLETE = "__complete";
|
|
867
994
|
var FILES = "__files__";
|
|
868
995
|
async function completingOf(run) {
|
|
@@ -877,6 +1004,9 @@ async function completingOf(run) {
|
|
|
877
1004
|
}
|
|
878
1005
|
return new Completing({});
|
|
879
1006
|
}
|
|
1007
|
+
async function optionsOf(run) {
|
|
1008
|
+
return (await completingOf(run)).options;
|
|
1009
|
+
}
|
|
880
1010
|
function candidateLine(candidate) {
|
|
881
1011
|
return typeof candidate === "string" ? candidate : candidate.join("\t");
|
|
882
1012
|
}
|
|
@@ -915,6 +1045,28 @@ async function leafCandidates(run, words) {
|
|
|
915
1045
|
const values = name === undefined ? undefined : await completing.candidates[name]?.();
|
|
916
1046
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
917
1047
|
}
|
|
1048
|
+
async function candidates(scope, defaultCommand, words, before = []) {
|
|
1049
|
+
const [word = "", ...rest] = words;
|
|
1050
|
+
const listing = rest.length === 0 && !word.startsWith("-");
|
|
1051
|
+
let { commands, aliases } = scope;
|
|
1052
|
+
let command = commands[aliases?.[word] ?? word];
|
|
1053
|
+
if (listing || command === undefined) {
|
|
1054
|
+
({ commands, aliases } = await expand(scope, [...before, ...words]));
|
|
1055
|
+
command = commands[aliases?.[word] ?? word];
|
|
1056
|
+
}
|
|
1057
|
+
if (listing) {
|
|
1058
|
+
const own = await Promise.all(Object.entries(shown(commands)).map(async ([name, { summary }]) => `${name} ${await resolveSummary(summary)}`));
|
|
1059
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
1060
|
+
const values = fallback !== undefined && "run" in fallback ? await leafCandidates(fallback.run, [word]) : [];
|
|
1061
|
+
const valueOf = (line) => line.slice(0, line.includes("\t") ? line.indexOf("\t") : undefined);
|
|
1062
|
+
return [...own, ...values.filter((line) => line !== FILES && !(valueOf(line) in commands))];
|
|
1063
|
+
}
|
|
1064
|
+
if (command === undefined) {
|
|
1065
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
1066
|
+
return fallback !== undefined && "run" in fallback ? leafCandidates(fallback.run, words) : [];
|
|
1067
|
+
}
|
|
1068
|
+
return "commands" in command ? candidates(scopeOf(command.commands, command.extraCommands, command.aliases), undefined, rest, [...before, word]) : leafCandidates(command.run, rest);
|
|
1069
|
+
}
|
|
918
1070
|
async function printCandidates(found) {
|
|
919
1071
|
for (const line of await found)
|
|
920
1072
|
console.log(line);
|
|
@@ -931,6 +1083,33 @@ function packageVersion(entry = Bun.main) {
|
|
|
931
1083
|
return "0.0.0";
|
|
932
1084
|
}
|
|
933
1085
|
}
|
|
1086
|
+
async function runCli(cli, argv = process8.argv.slice(2)) {
|
|
1087
|
+
const version = cli.version ?? packageVersion();
|
|
1088
|
+
if (argv[0] === COMPLETE)
|
|
1089
|
+
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
1090
|
+
const code = await dispatch({ path: [cli.name], header: (colors, about) => `${colors.bold(cli.name)} ${colors.dim(version)} ${about}`, summary: cli.about, ...scopeOf(cli.commands, cli.extraCommands, cli.aliases), defaultCommand: cli.defaultCommand, runBare: cli.runBare === true, examples: cli.examples, replaces: cli.replaces }, argv, { version, redact: cli.redact });
|
|
1091
|
+
if (cli.usage !== false) {
|
|
1092
|
+
try {
|
|
1093
|
+
recordUsage(usageEntry(cli.name, commandPath(scopeOf(cli.commands, cli.extraCommands, cli.aliases), argv), argv, code));
|
|
1094
|
+
} catch {}
|
|
1095
|
+
}
|
|
1096
|
+
return code;
|
|
1097
|
+
}
|
|
1098
|
+
function commandPath(scope, argv) {
|
|
1099
|
+
const path = [];
|
|
1100
|
+
let level = scope;
|
|
1101
|
+
for (const word of argv.filter((each) => !each.startsWith("-"))) {
|
|
1102
|
+
const name = level.aliases !== undefined && Object.hasOwn(level.aliases, word) ? level.aliases[word] ?? word : word;
|
|
1103
|
+
const command = Object.hasOwn(level.commands, name) ? level.commands[name] : undefined;
|
|
1104
|
+
if (command === undefined)
|
|
1105
|
+
break;
|
|
1106
|
+
path.push(word);
|
|
1107
|
+
if (!("commands" in command))
|
|
1108
|
+
break;
|
|
1109
|
+
level = scopeOf(command.commands, command.extraCommands, command.aliases);
|
|
1110
|
+
}
|
|
1111
|
+
return path.length === 0 ? undefined : path.join(" ");
|
|
1112
|
+
}
|
|
934
1113
|
async function runMain({ name, version, run, redact, usage }, argv = process8.argv.slice(2)) {
|
|
935
1114
|
if (argv[0] === COMPLETE)
|
|
936
1115
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
@@ -978,6 +1157,11 @@ var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "ses
|
|
|
978
1157
|
var {Glob } = globalThis.Bun;
|
|
979
1158
|
var SESSION_FILES = new Glob("*.json");
|
|
980
1159
|
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
1160
|
+
// src/peek.ts
|
|
1161
|
+
import { realpathSync as realpathSync2 } from "fs";
|
|
1162
|
+
import { isAbsolute as isAbsolute2, join as join4, relative as relative2, resolve, sep } from "path";
|
|
1163
|
+
import process10 from "process";
|
|
1164
|
+
|
|
981
1165
|
// src/files.ts
|
|
982
1166
|
import { existsSync, readdirSync, statSync } from "fs";
|
|
983
1167
|
import { join as join3, relative } from "path";
|
|
@@ -1336,7 +1520,7 @@ async function definitionsOf(symbols, paths) {
|
|
|
1336
1520
|
fail(`no declaration of ${missing.join(", ")} found`, 1);
|
|
1337
1521
|
return [...found.values()].flat();
|
|
1338
1522
|
}
|
|
1339
|
-
async function
|
|
1523
|
+
async function peek(args) {
|
|
1340
1524
|
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"]] });
|
|
1341
1525
|
const modes = [values.def?.length === 0 ? undefined : values.def, values.refs, values.outline, values.around].filter((mode) => mode !== undefined && mode !== false);
|
|
1342
1526
|
const json = values.json === true;
|
|
@@ -1411,4 +1595,6 @@ async function main(args) {
|
|
|
1411
1595
|
await printSpans(await Promise.all(positionals.map(spanOf)), json);
|
|
1412
1596
|
return 0;
|
|
1413
1597
|
}
|
|
1414
|
-
|
|
1598
|
+
|
|
1599
|
+
// src/peek-cli.ts
|
|
1600
|
+
process11.exitCode = await runMain({ name: "peek", run: peek });
|