@kaynooo/cli-proc 0.2.0 → 0.3.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/README.md +1 -1
- package/dist/pid-cli.js +183 -2
- package/dist/ports-cli.js +183 -2
- package/dist/proc-cli.js +2109 -0
- package/dist/procs-cli.js +183 -2
- package/package.json +5 -3
- package/src/pid-cli.ts +1 -1
- package/src/ports-cli.ts +1 -1
- package/src/proc-cli.ts +19 -0
- package/src/procs-cli.ts +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# proc
|
|
2
2
|
|
|
3
|
-
Three process and port helpers for Linux, reading `/proc` directly instead of shelling out to `ps`, `ss` or `lsof`: `pid` inspects one process, `ports` tells who listens on what, `procs` finds leftover or heavy dev processes.
|
|
3
|
+
Three process and port helpers for Linux (also reachable as `proc pid|ports|procs`), reading `/proc` directly instead of shelling out to `ps`, `ss` or `lsof`: `pid` inspects one process, `ports` tells who listens on what, `procs` finds leftover or heavy dev processes.
|
|
4
4
|
|
|
5
5
|
```sh
|
|
6
6
|
bun install -g @kaynooo/cli-proc
|
package/dist/pid-cli.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
1
|
+
#!/usr/bin/env -S bun --no-env-file
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/pid-cli.ts
|
|
@@ -839,6 +839,79 @@ var LEVEL_OPTIONS = {
|
|
|
839
839
|
help: { type: "boolean", short: "h", description: "show this help" }
|
|
840
840
|
};
|
|
841
841
|
var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
|
|
842
|
+
async function commandRows(all, colors) {
|
|
843
|
+
const commands = shown(all);
|
|
844
|
+
const width = Math.max(0, ...Object.keys(commands).map((name) => name.length));
|
|
845
|
+
const groups = new Map([["Commands:", []]]);
|
|
846
|
+
const summaries = await Promise.all(Object.values(commands).map(async ({ summary }) => resolveSummary(summary)));
|
|
847
|
+
for (const [index, [name, { group }]] of Object.entries(commands).entries()) {
|
|
848
|
+
const heading = group === undefined ? "Commands:" : `${group}:`;
|
|
849
|
+
const rows = groups.get(heading) ?? [];
|
|
850
|
+
rows.push(` ${colors.cyan(name.padEnd(width))} ${colors.dim(summaries[index])}`);
|
|
851
|
+
groups.set(heading, rows);
|
|
852
|
+
}
|
|
853
|
+
return [...groups].filter(([, rows]) => rows.length > 0).flatMap(([heading, rows], index) => [...index === 0 ? [] : [""], colors.bold(heading), ...rows]);
|
|
854
|
+
}
|
|
855
|
+
function shown(commands) {
|
|
856
|
+
return Object.fromEntries(Object.entries(commands).filter(([, { hidden }]) => hidden !== true));
|
|
857
|
+
}
|
|
858
|
+
function takeOutputFlags(argv) {
|
|
859
|
+
let index = 0;
|
|
860
|
+
for (;index < argv.length; index++) {
|
|
861
|
+
const word = argv[index];
|
|
862
|
+
if (word === "--plain")
|
|
863
|
+
applyOutput(undefined, true);
|
|
864
|
+
else if (word.startsWith("--color="))
|
|
865
|
+
applyOutput(word.slice("--color=".length), undefined);
|
|
866
|
+
else if (word === "--color" && argv[index + 1] !== undefined)
|
|
867
|
+
applyOutput(argv[++index], undefined);
|
|
868
|
+
else
|
|
869
|
+
break;
|
|
870
|
+
}
|
|
871
|
+
return argv.slice(index);
|
|
872
|
+
}
|
|
873
|
+
async function resolveSummary(summary) {
|
|
874
|
+
return typeof summary === "string" ? summary : summary();
|
|
875
|
+
}
|
|
876
|
+
function scopeOf(commands, extra, aliases) {
|
|
877
|
+
return { commands: typeof commands === "function" ? {} : commands, lazy: [...typeof commands === "function" ? [commands] : [], ...extra === undefined ? [] : [extra]], aliases };
|
|
878
|
+
}
|
|
879
|
+
async function expand(scope, words) {
|
|
880
|
+
if (scope.lazy.length === 0)
|
|
881
|
+
return scope;
|
|
882
|
+
const commands = { ...scope.commands };
|
|
883
|
+
for (const added of await Promise.all(scope.lazy.map(async (lazy) => lazy(words)))) {
|
|
884
|
+
for (const [name, command] of Object.entries(added))
|
|
885
|
+
commands[name] ??= command;
|
|
886
|
+
}
|
|
887
|
+
return { ...scope, commands, lazy: [] };
|
|
888
|
+
}
|
|
889
|
+
async function help2({ path, header, summary: lazySummary, commands, defaultCommand, examples, replaces }, stream) {
|
|
890
|
+
const colors = paint(stream);
|
|
891
|
+
const prefix = path.join(" ");
|
|
892
|
+
const summary = await resolveSummary(lazySummary);
|
|
893
|
+
if (wantsHelpJson())
|
|
894
|
+
return JSON.stringify(description(`${prefix} <command> [options]: ${summary}`, { examples, replaces }));
|
|
895
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
896
|
+
const options = fallback !== undefined && "run" in fallback ? await optionsOf(fallback.run) : undefined;
|
|
897
|
+
const own = Object.entries(options ?? {}).filter(([name]) => !(name in TOP_OPTIONS));
|
|
898
|
+
return [
|
|
899
|
+
header(colors, summary),
|
|
900
|
+
"",
|
|
901
|
+
usageLine(`${prefix} ${defaultCommand === undefined ? colors.cyan("<command>") : `[${colors.cyan("<command>")}]`} [options]`, colors),
|
|
902
|
+
...replacesRows(replaces, colors),
|
|
903
|
+
"",
|
|
904
|
+
...await commandRows(commands, colors),
|
|
905
|
+
...own.length === 0 ? [] : ["", `${colors.bold(`Options of ${defaultCommand}:`)} ${colors.dim("the command run when none is named")}`, ...optionRows(Object.fromEntries(own), colors)],
|
|
906
|
+
"",
|
|
907
|
+
colors.bold("Options:"),
|
|
908
|
+
...optionRows(path.length === 1 ? TOP_OPTIONS : LEVEL_OPTIONS, colors),
|
|
909
|
+
...exampleRows(examples ?? [], colors),
|
|
910
|
+
"",
|
|
911
|
+
`${colors.dim("Run")} ${prefix} ${colors.cyan("<command>")} --help ${colors.dim("for its options.")}`
|
|
912
|
+
].join(`
|
|
913
|
+
`);
|
|
914
|
+
}
|
|
842
915
|
function printError(prefix, message, hint, cause) {
|
|
843
916
|
const colors = paint(process8.stderr);
|
|
844
917
|
const styled = takesControlCodes(process8.stderr);
|
|
@@ -860,12 +933,68 @@ function reportThrown(prefix, error, redact = (text) => text, json = false) {
|
|
|
860
933
|
const help = error instanceof UsageError ? `run ${prefix} --help for its options` : undefined;
|
|
861
934
|
const named = error instanceof Error && !(error instanceof Exit) && error.name !== "Error" ? `${error.name}: ${error.message}` : undefined;
|
|
862
935
|
const cause = error instanceof Error && error.cause instanceof Error ? redact(error.cause.message) : undefined;
|
|
863
|
-
if (error instanceof
|
|
936
|
+
if (error instanceof Exit && error.code === 2)
|
|
864
937
|
noteUsageError(error.message);
|
|
865
938
|
const report = json ? printJsonError : (message, hint, causeText) => printError(prefix, message, hint, causeText);
|
|
866
939
|
report(redact(named ?? (error instanceof Error ? error.message : String(error))), error instanceof Exit ? error.hint ?? help : undefined, cause);
|
|
867
940
|
return error instanceof Exit ? error.code : 1;
|
|
868
941
|
}
|
|
942
|
+
async function dispatch(level, typed, { version, redact }) {
|
|
943
|
+
const prefix = level.path.join(" ");
|
|
944
|
+
let argv;
|
|
945
|
+
try {
|
|
946
|
+
argv = takeOutputFlags(typed);
|
|
947
|
+
} catch (error) {
|
|
948
|
+
return reportThrown(prefix, error);
|
|
949
|
+
}
|
|
950
|
+
const [name, ...args] = argv;
|
|
951
|
+
const fallback = level.defaultCommand === undefined ? undefined : level.commands[level.defaultCommand];
|
|
952
|
+
if (name === undefined && level.runBare && fallback !== undefined && "run" in fallback) {
|
|
953
|
+
try {
|
|
954
|
+
return await fallback.run([]);
|
|
955
|
+
} catch (error) {
|
|
956
|
+
return reportThrown(`${prefix} ${level.defaultCommand}`, error, redact, false);
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
if (name === undefined && level.path.length > 1) {
|
|
960
|
+
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process8.stderr));
|
|
961
|
+
return 2;
|
|
962
|
+
}
|
|
963
|
+
if (name === undefined || name === "help" || name === "--help" || name === "-h") {
|
|
964
|
+
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process8.stdout));
|
|
965
|
+
return 0;
|
|
966
|
+
}
|
|
967
|
+
if (level.path.length === 1 && (name === "--version" || name === "-v")) {
|
|
968
|
+
console.log(version);
|
|
969
|
+
return 0;
|
|
970
|
+
}
|
|
971
|
+
let command = level.commands[level.aliases?.[name] ?? name];
|
|
972
|
+
let rest = args;
|
|
973
|
+
let ran = name;
|
|
974
|
+
if (command === undefined && level.lazy.length > 0) {
|
|
975
|
+
level = await expand(level, [...level.path.slice(1), ...argv]);
|
|
976
|
+
command = level.commands[level.aliases?.[name] ?? name];
|
|
977
|
+
}
|
|
978
|
+
if (command === undefined && level.defaultCommand !== undefined) {
|
|
979
|
+
command = level.commands[level.defaultCommand];
|
|
980
|
+
rest = argv;
|
|
981
|
+
ran = level.defaultCommand;
|
|
982
|
+
}
|
|
983
|
+
if (command === undefined) {
|
|
984
|
+
const meant = closest(name, Object.keys(shown(level.commands)));
|
|
985
|
+
printError(prefix, `no command ${name}`, meant === undefined ? `run ${prefix} --help for its commands` : `did you mean ${meant}?`);
|
|
986
|
+
return 2;
|
|
987
|
+
}
|
|
988
|
+
if ("commands" in command) {
|
|
989
|
+
const { summary } = command;
|
|
990
|
+
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 });
|
|
991
|
+
}
|
|
992
|
+
try {
|
|
993
|
+
return await command.run(rest);
|
|
994
|
+
} catch (error) {
|
|
995
|
+
return reportThrown(`${prefix} ${ran}`, error, redact, wantsJson(rest));
|
|
996
|
+
}
|
|
997
|
+
}
|
|
869
998
|
var COMPLETE = "__complete";
|
|
870
999
|
var FILES = "__files__";
|
|
871
1000
|
async function completingOf(run) {
|
|
@@ -880,6 +1009,9 @@ async function completingOf(run) {
|
|
|
880
1009
|
}
|
|
881
1010
|
return new Completing({});
|
|
882
1011
|
}
|
|
1012
|
+
async function optionsOf(run) {
|
|
1013
|
+
return (await completingOf(run)).options;
|
|
1014
|
+
}
|
|
883
1015
|
function candidateLine(candidate) {
|
|
884
1016
|
return typeof candidate === "string" ? candidate : candidate.join("\t");
|
|
885
1017
|
}
|
|
@@ -918,6 +1050,28 @@ async function leafCandidates(run, words) {
|
|
|
918
1050
|
const values = name === undefined ? undefined : await completing.candidates[name]?.();
|
|
919
1051
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
920
1052
|
}
|
|
1053
|
+
async function candidates(scope, defaultCommand, words, before = []) {
|
|
1054
|
+
const [word = "", ...rest] = words;
|
|
1055
|
+
const listing = rest.length === 0 && !word.startsWith("-");
|
|
1056
|
+
let { commands, aliases } = scope;
|
|
1057
|
+
let command = commands[aliases?.[word] ?? word];
|
|
1058
|
+
if (listing || command === undefined) {
|
|
1059
|
+
({ commands, aliases } = await expand(scope, [...before, ...words]));
|
|
1060
|
+
command = commands[aliases?.[word] ?? word];
|
|
1061
|
+
}
|
|
1062
|
+
if (listing) {
|
|
1063
|
+
const own = await Promise.all(Object.entries(shown(commands)).map(async ([name, { summary }]) => `${name} ${await resolveSummary(summary)}`));
|
|
1064
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
1065
|
+
const values = fallback !== undefined && "run" in fallback ? await leafCandidates(fallback.run, [word]) : [];
|
|
1066
|
+
const valueOf = (line) => line.slice(0, line.includes("\t") ? line.indexOf("\t") : undefined);
|
|
1067
|
+
return [...own, ...values.filter((line) => line !== FILES && !(valueOf(line) in commands))];
|
|
1068
|
+
}
|
|
1069
|
+
if (command === undefined) {
|
|
1070
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
1071
|
+
return fallback !== undefined && "run" in fallback ? leafCandidates(fallback.run, words) : [];
|
|
1072
|
+
}
|
|
1073
|
+
return "commands" in command ? candidates(scopeOf(command.commands, command.extraCommands, command.aliases), undefined, rest, [...before, word]) : leafCandidates(command.run, rest);
|
|
1074
|
+
}
|
|
921
1075
|
async function printCandidates(found) {
|
|
922
1076
|
for (const line of await found)
|
|
923
1077
|
console.log(line);
|
|
@@ -934,6 +1088,33 @@ function packageVersion(entry = Bun.main) {
|
|
|
934
1088
|
return "0.0.0";
|
|
935
1089
|
}
|
|
936
1090
|
}
|
|
1091
|
+
async function runCli(cli, argv = process8.argv.slice(2)) {
|
|
1092
|
+
const version = cli.version ?? packageVersion();
|
|
1093
|
+
if (argv[0] === COMPLETE)
|
|
1094
|
+
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
1095
|
+
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 });
|
|
1096
|
+
if (cli.usage !== false) {
|
|
1097
|
+
try {
|
|
1098
|
+
recordUsage(usageEntry(cli.name, commandPath(scopeOf(cli.commands, cli.extraCommands, cli.aliases), argv), argv, code));
|
|
1099
|
+
} catch {}
|
|
1100
|
+
}
|
|
1101
|
+
return code;
|
|
1102
|
+
}
|
|
1103
|
+
function commandPath(scope, argv) {
|
|
1104
|
+
const path = [];
|
|
1105
|
+
let level = scope;
|
|
1106
|
+
for (const word of argv.filter((each) => !each.startsWith("-"))) {
|
|
1107
|
+
const name = level.aliases !== undefined && Object.hasOwn(level.aliases, word) ? level.aliases[word] ?? word : word;
|
|
1108
|
+
const command = Object.hasOwn(level.commands, name) ? level.commands[name] : undefined;
|
|
1109
|
+
if (command === undefined)
|
|
1110
|
+
break;
|
|
1111
|
+
path.push(word);
|
|
1112
|
+
if (!("commands" in command))
|
|
1113
|
+
break;
|
|
1114
|
+
level = scopeOf(command.commands, command.extraCommands, command.aliases);
|
|
1115
|
+
}
|
|
1116
|
+
return path.length === 0 ? undefined : path.join(" ");
|
|
1117
|
+
}
|
|
937
1118
|
async function runMain({ name, version, run, redact, usage }, argv = process8.argv.slice(2)) {
|
|
938
1119
|
if (argv[0] === COMPLETE)
|
|
939
1120
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|
package/dist/ports-cli.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
1
|
+
#!/usr/bin/env -S bun --no-env-file
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/ports-cli.ts
|
|
@@ -828,6 +828,79 @@ var LEVEL_OPTIONS = {
|
|
|
828
828
|
help: { type: "boolean", short: "h", description: "show this help" }
|
|
829
829
|
};
|
|
830
830
|
var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
|
|
831
|
+
async function commandRows(all, colors) {
|
|
832
|
+
const commands = shown(all);
|
|
833
|
+
const width = Math.max(0, ...Object.keys(commands).map((name) => name.length));
|
|
834
|
+
const groups = new Map([["Commands:", []]]);
|
|
835
|
+
const summaries = await Promise.all(Object.values(commands).map(async ({ summary }) => resolveSummary(summary)));
|
|
836
|
+
for (const [index, [name, { group }]] of Object.entries(commands).entries()) {
|
|
837
|
+
const heading = group === undefined ? "Commands:" : `${group}:`;
|
|
838
|
+
const rows = groups.get(heading) ?? [];
|
|
839
|
+
rows.push(` ${colors.cyan(name.padEnd(width))} ${colors.dim(summaries[index])}`);
|
|
840
|
+
groups.set(heading, rows);
|
|
841
|
+
}
|
|
842
|
+
return [...groups].filter(([, rows]) => rows.length > 0).flatMap(([heading, rows], index) => [...index === 0 ? [] : [""], colors.bold(heading), ...rows]);
|
|
843
|
+
}
|
|
844
|
+
function shown(commands) {
|
|
845
|
+
return Object.fromEntries(Object.entries(commands).filter(([, { hidden }]) => hidden !== true));
|
|
846
|
+
}
|
|
847
|
+
function takeOutputFlags(argv) {
|
|
848
|
+
let index = 0;
|
|
849
|
+
for (;index < argv.length; index++) {
|
|
850
|
+
const word = argv[index];
|
|
851
|
+
if (word === "--plain")
|
|
852
|
+
applyOutput(undefined, true);
|
|
853
|
+
else if (word.startsWith("--color="))
|
|
854
|
+
applyOutput(word.slice("--color=".length), undefined);
|
|
855
|
+
else if (word === "--color" && argv[index + 1] !== undefined)
|
|
856
|
+
applyOutput(argv[++index], undefined);
|
|
857
|
+
else
|
|
858
|
+
break;
|
|
859
|
+
}
|
|
860
|
+
return argv.slice(index);
|
|
861
|
+
}
|
|
862
|
+
async function resolveSummary(summary) {
|
|
863
|
+
return typeof summary === "string" ? summary : summary();
|
|
864
|
+
}
|
|
865
|
+
function scopeOf(commands, extra, aliases) {
|
|
866
|
+
return { commands: typeof commands === "function" ? {} : commands, lazy: [...typeof commands === "function" ? [commands] : [], ...extra === undefined ? [] : [extra]], aliases };
|
|
867
|
+
}
|
|
868
|
+
async function expand(scope, words) {
|
|
869
|
+
if (scope.lazy.length === 0)
|
|
870
|
+
return scope;
|
|
871
|
+
const commands = { ...scope.commands };
|
|
872
|
+
for (const added of await Promise.all(scope.lazy.map(async (lazy) => lazy(words)))) {
|
|
873
|
+
for (const [name, command] of Object.entries(added))
|
|
874
|
+
commands[name] ??= command;
|
|
875
|
+
}
|
|
876
|
+
return { ...scope, commands, lazy: [] };
|
|
877
|
+
}
|
|
878
|
+
async function help2({ path, header, summary: lazySummary, commands, defaultCommand, examples, replaces }, stream) {
|
|
879
|
+
const colors = paint(stream);
|
|
880
|
+
const prefix = path.join(" ");
|
|
881
|
+
const summary = await resolveSummary(lazySummary);
|
|
882
|
+
if (wantsHelpJson())
|
|
883
|
+
return JSON.stringify(description(`${prefix} <command> [options]: ${summary}`, { examples, replaces }));
|
|
884
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
885
|
+
const options = fallback !== undefined && "run" in fallback ? await optionsOf(fallback.run) : undefined;
|
|
886
|
+
const own = Object.entries(options ?? {}).filter(([name]) => !(name in TOP_OPTIONS));
|
|
887
|
+
return [
|
|
888
|
+
header(colors, summary),
|
|
889
|
+
"",
|
|
890
|
+
usageLine(`${prefix} ${defaultCommand === undefined ? colors.cyan("<command>") : `[${colors.cyan("<command>")}]`} [options]`, colors),
|
|
891
|
+
...replacesRows(replaces, colors),
|
|
892
|
+
"",
|
|
893
|
+
...await commandRows(commands, colors),
|
|
894
|
+
...own.length === 0 ? [] : ["", `${colors.bold(`Options of ${defaultCommand}:`)} ${colors.dim("the command run when none is named")}`, ...optionRows(Object.fromEntries(own), colors)],
|
|
895
|
+
"",
|
|
896
|
+
colors.bold("Options:"),
|
|
897
|
+
...optionRows(path.length === 1 ? TOP_OPTIONS : LEVEL_OPTIONS, colors),
|
|
898
|
+
...exampleRows(examples ?? [], colors),
|
|
899
|
+
"",
|
|
900
|
+
`${colors.dim("Run")} ${prefix} ${colors.cyan("<command>")} --help ${colors.dim("for its options.")}`
|
|
901
|
+
].join(`
|
|
902
|
+
`);
|
|
903
|
+
}
|
|
831
904
|
function printError(prefix, message, hint, cause) {
|
|
832
905
|
const colors = paint(process8.stderr);
|
|
833
906
|
const styled = takesControlCodes(process8.stderr);
|
|
@@ -849,12 +922,68 @@ function reportThrown(prefix, error, redact = (text) => text, json = false) {
|
|
|
849
922
|
const help = error instanceof UsageError ? `run ${prefix} --help for its options` : undefined;
|
|
850
923
|
const named = error instanceof Error && !(error instanceof Exit) && error.name !== "Error" ? `${error.name}: ${error.message}` : undefined;
|
|
851
924
|
const cause = error instanceof Error && error.cause instanceof Error ? redact(error.cause.message) : undefined;
|
|
852
|
-
if (error instanceof
|
|
925
|
+
if (error instanceof Exit && error.code === 2)
|
|
853
926
|
noteUsageError(error.message);
|
|
854
927
|
const report = json ? printJsonError : (message, hint, causeText) => printError(prefix, message, hint, causeText);
|
|
855
928
|
report(redact(named ?? (error instanceof Error ? error.message : String(error))), error instanceof Exit ? error.hint ?? help : undefined, cause);
|
|
856
929
|
return error instanceof Exit ? error.code : 1;
|
|
857
930
|
}
|
|
931
|
+
async function dispatch(level, typed, { version, redact }) {
|
|
932
|
+
const prefix = level.path.join(" ");
|
|
933
|
+
let argv;
|
|
934
|
+
try {
|
|
935
|
+
argv = takeOutputFlags(typed);
|
|
936
|
+
} catch (error) {
|
|
937
|
+
return reportThrown(prefix, error);
|
|
938
|
+
}
|
|
939
|
+
const [name, ...args] = argv;
|
|
940
|
+
const fallback = level.defaultCommand === undefined ? undefined : level.commands[level.defaultCommand];
|
|
941
|
+
if (name === undefined && level.runBare && fallback !== undefined && "run" in fallback) {
|
|
942
|
+
try {
|
|
943
|
+
return await fallback.run([]);
|
|
944
|
+
} catch (error) {
|
|
945
|
+
return reportThrown(`${prefix} ${level.defaultCommand}`, error, redact, false);
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
if (name === undefined && level.path.length > 1) {
|
|
949
|
+
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process8.stderr));
|
|
950
|
+
return 2;
|
|
951
|
+
}
|
|
952
|
+
if (name === undefined || name === "help" || name === "--help" || name === "-h") {
|
|
953
|
+
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process8.stdout));
|
|
954
|
+
return 0;
|
|
955
|
+
}
|
|
956
|
+
if (level.path.length === 1 && (name === "--version" || name === "-v")) {
|
|
957
|
+
console.log(version);
|
|
958
|
+
return 0;
|
|
959
|
+
}
|
|
960
|
+
let command = level.commands[level.aliases?.[name] ?? name];
|
|
961
|
+
let rest = args;
|
|
962
|
+
let ran = name;
|
|
963
|
+
if (command === undefined && level.lazy.length > 0) {
|
|
964
|
+
level = await expand(level, [...level.path.slice(1), ...argv]);
|
|
965
|
+
command = level.commands[level.aliases?.[name] ?? name];
|
|
966
|
+
}
|
|
967
|
+
if (command === undefined && level.defaultCommand !== undefined) {
|
|
968
|
+
command = level.commands[level.defaultCommand];
|
|
969
|
+
rest = argv;
|
|
970
|
+
ran = level.defaultCommand;
|
|
971
|
+
}
|
|
972
|
+
if (command === undefined) {
|
|
973
|
+
const meant = closest(name, Object.keys(shown(level.commands)));
|
|
974
|
+
printError(prefix, `no command ${name}`, meant === undefined ? `run ${prefix} --help for its commands` : `did you mean ${meant}?`);
|
|
975
|
+
return 2;
|
|
976
|
+
}
|
|
977
|
+
if ("commands" in command) {
|
|
978
|
+
const { summary } = command;
|
|
979
|
+
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 });
|
|
980
|
+
}
|
|
981
|
+
try {
|
|
982
|
+
return await command.run(rest);
|
|
983
|
+
} catch (error) {
|
|
984
|
+
return reportThrown(`${prefix} ${ran}`, error, redact, wantsJson(rest));
|
|
985
|
+
}
|
|
986
|
+
}
|
|
858
987
|
var COMPLETE = "__complete";
|
|
859
988
|
var FILES = "__files__";
|
|
860
989
|
async function completingOf(run) {
|
|
@@ -869,6 +998,9 @@ async function completingOf(run) {
|
|
|
869
998
|
}
|
|
870
999
|
return new Completing({});
|
|
871
1000
|
}
|
|
1001
|
+
async function optionsOf(run) {
|
|
1002
|
+
return (await completingOf(run)).options;
|
|
1003
|
+
}
|
|
872
1004
|
function candidateLine(candidate) {
|
|
873
1005
|
return typeof candidate === "string" ? candidate : candidate.join("\t");
|
|
874
1006
|
}
|
|
@@ -907,6 +1039,28 @@ async function leafCandidates(run, words) {
|
|
|
907
1039
|
const values = name === undefined ? undefined : await completing.candidates[name]?.();
|
|
908
1040
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
909
1041
|
}
|
|
1042
|
+
async function candidates(scope, defaultCommand, words, before = []) {
|
|
1043
|
+
const [word = "", ...rest] = words;
|
|
1044
|
+
const listing = rest.length === 0 && !word.startsWith("-");
|
|
1045
|
+
let { commands, aliases } = scope;
|
|
1046
|
+
let command = commands[aliases?.[word] ?? word];
|
|
1047
|
+
if (listing || command === undefined) {
|
|
1048
|
+
({ commands, aliases } = await expand(scope, [...before, ...words]));
|
|
1049
|
+
command = commands[aliases?.[word] ?? word];
|
|
1050
|
+
}
|
|
1051
|
+
if (listing) {
|
|
1052
|
+
const own = await Promise.all(Object.entries(shown(commands)).map(async ([name, { summary }]) => `${name} ${await resolveSummary(summary)}`));
|
|
1053
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
1054
|
+
const values = fallback !== undefined && "run" in fallback ? await leafCandidates(fallback.run, [word]) : [];
|
|
1055
|
+
const valueOf = (line) => line.slice(0, line.includes("\t") ? line.indexOf("\t") : undefined);
|
|
1056
|
+
return [...own, ...values.filter((line) => line !== FILES && !(valueOf(line) in commands))];
|
|
1057
|
+
}
|
|
1058
|
+
if (command === undefined) {
|
|
1059
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
1060
|
+
return fallback !== undefined && "run" in fallback ? leafCandidates(fallback.run, words) : [];
|
|
1061
|
+
}
|
|
1062
|
+
return "commands" in command ? candidates(scopeOf(command.commands, command.extraCommands, command.aliases), undefined, rest, [...before, word]) : leafCandidates(command.run, rest);
|
|
1063
|
+
}
|
|
910
1064
|
async function printCandidates(found) {
|
|
911
1065
|
for (const line of await found)
|
|
912
1066
|
console.log(line);
|
|
@@ -923,6 +1077,33 @@ function packageVersion(entry = Bun.main) {
|
|
|
923
1077
|
return "0.0.0";
|
|
924
1078
|
}
|
|
925
1079
|
}
|
|
1080
|
+
async function runCli(cli, argv = process8.argv.slice(2)) {
|
|
1081
|
+
const version = cli.version ?? packageVersion();
|
|
1082
|
+
if (argv[0] === COMPLETE)
|
|
1083
|
+
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
1084
|
+
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 });
|
|
1085
|
+
if (cli.usage !== false) {
|
|
1086
|
+
try {
|
|
1087
|
+
recordUsage(usageEntry(cli.name, commandPath(scopeOf(cli.commands, cli.extraCommands, cli.aliases), argv), argv, code));
|
|
1088
|
+
} catch {}
|
|
1089
|
+
}
|
|
1090
|
+
return code;
|
|
1091
|
+
}
|
|
1092
|
+
function commandPath(scope, argv) {
|
|
1093
|
+
const path = [];
|
|
1094
|
+
let level = scope;
|
|
1095
|
+
for (const word of argv.filter((each) => !each.startsWith("-"))) {
|
|
1096
|
+
const name = level.aliases !== undefined && Object.hasOwn(level.aliases, word) ? level.aliases[word] ?? word : word;
|
|
1097
|
+
const command = Object.hasOwn(level.commands, name) ? level.commands[name] : undefined;
|
|
1098
|
+
if (command === undefined)
|
|
1099
|
+
break;
|
|
1100
|
+
path.push(word);
|
|
1101
|
+
if (!("commands" in command))
|
|
1102
|
+
break;
|
|
1103
|
+
level = scopeOf(command.commands, command.extraCommands, command.aliases);
|
|
1104
|
+
}
|
|
1105
|
+
return path.length === 0 ? undefined : path.join(" ");
|
|
1106
|
+
}
|
|
926
1107
|
async function runMain({ name, version, run, redact, usage }, argv = process8.argv.slice(2)) {
|
|
927
1108
|
if (argv[0] === COMPLETE)
|
|
928
1109
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|