@kaynooo/cli-proc 0.2.1 → 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 +182 -1
- package/dist/ports-cli.js +182 -1
- package/dist/proc-cli.js +2109 -0
- package/dist/procs-cli.js +182 -1
- package/package.json +5 -3
- package/src/proc-cli.ts +19 -0
package/dist/procs-cli.js
CHANGED
|
@@ -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/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-proc",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"description": "pid, ports, procs: one process inspected, who listens on a port, and leftover or heavy dev processes cleaned, read straight from /proc (Linux).",
|
|
6
6
|
"author": "Kaynooo <contact@kaynooo.fr>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
"keywords": ["process", "port", "proc", "kill", "cli", "bun"],
|
|
16
16
|
"os": ["linux"],
|
|
17
17
|
"bin": {
|
|
18
|
+
"proc": "./dist/proc-cli.js",
|
|
18
19
|
"pid": "./dist/pid-cli.js",
|
|
19
20
|
"ports": "./dist/ports-cli.js",
|
|
20
21
|
"procs": "./dist/procs-cli.js"
|
|
@@ -27,12 +28,13 @@
|
|
|
27
28
|
"registry": "https://git.kaynooo.fr/api/packages/kaynooo/npm/"
|
|
28
29
|
},
|
|
29
30
|
"scripts": {
|
|
30
|
-
"build": "$npm_execpath build src/pid-cli.ts src/ports-cli.ts src/procs-cli.ts --target bun --outdir dist",
|
|
31
|
+
"build": "$npm_execpath build src/proc-cli.ts src/pid-cli.ts src/ports-cli.ts src/procs-cli.ts --target bun --outdir dist",
|
|
32
|
+
"proc": "$npm_execpath src/proc-cli.ts",
|
|
31
33
|
"pid": "$npm_execpath src/pid-cli.ts",
|
|
32
34
|
"ports": "$npm_execpath src/ports-cli.ts",
|
|
33
35
|
"procs": "$npm_execpath src/procs-cli.ts"
|
|
34
36
|
},
|
|
35
37
|
"devDependencies": {
|
|
36
|
-
"@kaynooo/cli-kit": "0.21.
|
|
38
|
+
"@kaynooo/cli-kit": "0.21.1"
|
|
37
39
|
}
|
|
38
40
|
}
|
package/src/proc-cli.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/usr/bin/env -S bun --no-env-file
|
|
2
|
+
|
|
3
|
+
import process from 'node:process'
|
|
4
|
+
import { runCli } from '@kaynooo/cli-kit'
|
|
5
|
+
import { pid } from './pid'
|
|
6
|
+
import { ports } from './ports'
|
|
7
|
+
import { procs } from './procs'
|
|
8
|
+
|
|
9
|
+
process.exitCode = await runCli({
|
|
10
|
+
name: 'proc',
|
|
11
|
+
about: 'what a process is, who listens on a port and which dev processes are worth cleaning, read from /proc',
|
|
12
|
+
replaces: 'cat /proc/<pid>/..., ss -ltnp, lsof -i and pgrep -f one-liners',
|
|
13
|
+
examples: ['proc pid 1234', 'proc ports', ['proc procs', 'the leftover or heavy processes, dry-run first']],
|
|
14
|
+
commands: {
|
|
15
|
+
pid: { summary: 'what a process is: executable, parents, memory, limits and listening ports', run: pid },
|
|
16
|
+
ports: { summary: 'what listens on this machine, with its owner, and a safe --kill', run: ports },
|
|
17
|
+
procs: { summary: 'the leftover or heavy processes of the calling user, grouped by origin', run: procs },
|
|
18
|
+
},
|
|
19
|
+
})
|