@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/todo-scan.ts
|
|
5
|
-
import
|
|
6
|
-
import process10 from "process";
|
|
4
|
+
// src/todo-scan-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/todo-scan.ts
|
|
1161
|
+
import { join as join4 } from "path";
|
|
1162
|
+
import process10 from "process";
|
|
1163
|
+
|
|
980
1164
|
// src/files.ts
|
|
981
1165
|
import { existsSync, readdirSync, statSync } from "fs";
|
|
982
1166
|
import { join as join3, relative } from "path";
|
|
@@ -1106,7 +1290,7 @@ function printLines(lines, footer) {
|
|
|
1106
1290
|
console.log(all.join(`
|
|
1107
1291
|
`));
|
|
1108
1292
|
}
|
|
1109
|
-
async function
|
|
1293
|
+
async function todoScan(args) {
|
|
1110
1294
|
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"]] });
|
|
1111
1295
|
const cwd = process10.cwd();
|
|
1112
1296
|
const limit = limitOf(values, LIST_LIMIT);
|
|
@@ -1137,4 +1321,6 @@ async function main(args) {
|
|
|
1137
1321
|
return 1;
|
|
1138
1322
|
return failed.size > 0 ? 2 : 0;
|
|
1139
1323
|
}
|
|
1140
|
-
|
|
1324
|
+
|
|
1325
|
+
// src/todo-scan-cli.ts
|
|
1326
|
+
process11.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.
|
|
4
|
+
"version": "0.9.0",
|
|
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",
|
|
@@ -21,9 +21,10 @@
|
|
|
21
21
|
"hotspots"
|
|
22
22
|
],
|
|
23
23
|
"bin": {
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
24
|
+
"src": "./dist/cli.js",
|
|
25
|
+
"peek": "./dist/peek-cli.js",
|
|
26
|
+
"todo-scan": "./dist/todo-scan-cli.js",
|
|
27
|
+
"hotspots": "./dist/hotspots-cli.js"
|
|
27
28
|
},
|
|
28
29
|
"files": [
|
|
29
30
|
"dist"
|
|
@@ -35,12 +36,13 @@
|
|
|
35
36
|
"registry": "https://git.kaynooo.fr/api/packages/kaynooo/npm/"
|
|
36
37
|
},
|
|
37
38
|
"scripts": {
|
|
38
|
-
"build": "$npm_execpath build src/peek.ts src/todo-scan.ts src/hotspots.ts --target bun --outdir dist",
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
39
|
+
"build": "$npm_execpath build src/cli.ts src/peek-cli.ts src/todo-scan-cli.ts src/hotspots-cli.ts --target bun --outdir dist",
|
|
40
|
+
"src": "$npm_execpath src/cli.ts",
|
|
41
|
+
"peek": "$npm_execpath src/peek-cli.ts",
|
|
42
|
+
"todo-scan": "$npm_execpath src/todo-scan-cli.ts",
|
|
43
|
+
"hotspots": "$npm_execpath src/hotspots-cli.ts"
|
|
42
44
|
},
|
|
43
45
|
"devDependencies": {
|
|
44
|
-
"@kaynooo/cli-kit": "0.21.
|
|
46
|
+
"@kaynooo/cli-kit": "0.21.1"
|
|
45
47
|
}
|
|
46
48
|
}
|