@kaynooo/cli-proc 0.7.1 → 0.7.2
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/pid-cli.js +69 -46
- package/dist/ports-cli.js +75 -52
- package/dist/proc-cli.js +88 -65
- package/dist/procs-cli.js +72 -49
- package/package.json +2 -2
- package/src/pid.ts +4 -3
- package/src/ports.ts +4 -3
- package/src/procs.ts +4 -3
package/dist/procs-cli.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/procs-cli.ts
|
|
5
|
-
import
|
|
5
|
+
import process17 from "process";
|
|
6
6
|
// ../kit/src/args.ts
|
|
7
7
|
import process9 from "process";
|
|
8
8
|
import { parseArgs } from "util";
|
|
@@ -456,18 +456,14 @@ var FULL_STOP = /\.$/;
|
|
|
456
456
|
function usageLine(usage, colors = paint(process8.stdout)) {
|
|
457
457
|
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
458
458
|
}
|
|
459
|
-
function helpText(usage, options, colors = paint(process8.stdout), { examples = [],
|
|
460
|
-
return [usageLine(usage, colors),
|
|
459
|
+
function helpText(usage, options, colors = paint(process8.stdout), { examples = [], exits = [] } = {}) {
|
|
460
|
+
return [usageLine(usage, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
|
|
461
461
|
`);
|
|
462
462
|
}
|
|
463
463
|
function exitRows(exits, colors = paint(process8.stdout)) {
|
|
464
464
|
if (exits.length === 0)
|
|
465
465
|
return [];
|
|
466
|
-
|
|
467
|
-
return ["", colors.bold("Exits:"), ...exits.map(([code, meaning]) => ` ${colors.cyan(String(code).padEnd(width))} ${meaning}`)];
|
|
468
|
-
}
|
|
469
|
-
function replacesRows(replaces, colors = paint(process8.stdout)) {
|
|
470
|
-
return replaces === undefined || outputMode() === "agent" ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
466
|
+
return ["", `${colors.bold("Exit codes:")} ${exits.map(([code, meaning]) => `${colors.cyan(String(code))} ${meaning}`).join(", ")}`];
|
|
471
467
|
}
|
|
472
468
|
function wantsHelpJson(env = process8.env) {
|
|
473
469
|
return env.KIT_HELP === "json";
|
|
@@ -763,9 +759,33 @@ function nearestOption(message, options, allowNegative) {
|
|
|
763
759
|
const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
|
|
764
760
|
return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
|
|
765
761
|
}
|
|
762
|
+
// ../kit/src/argv.ts
|
|
763
|
+
import { readFileSync as readFileSync2, realpathSync } from "fs";
|
|
764
|
+
import { resolve as resolve3 } from "path";
|
|
765
|
+
import process10 from "process";
|
|
766
|
+
function commandLine() {
|
|
767
|
+
try {
|
|
768
|
+
return readFileSync2("/proc/self/cmdline", "utf8").split("\x00").slice(0, -1);
|
|
769
|
+
} catch {
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
function realPath(word) {
|
|
774
|
+
try {
|
|
775
|
+
return realpathSync(resolve3(word));
|
|
776
|
+
} catch {
|
|
777
|
+
return;
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
function scriptArgs(argv = process10.argv.slice(2), cmdline = commandLine(), script = process10.argv[1]) {
|
|
781
|
+
const target = script === undefined ? undefined : realPath(script) ?? script;
|
|
782
|
+
const at = cmdline === undefined || target === undefined ? -1 : cmdline.findIndex((word, index) => index > 0 && (resolve3(word) === script || realPath(word) === target));
|
|
783
|
+
const after = at === -1 ? [] : cmdline.slice(at + 1);
|
|
784
|
+
return after[0] === "--" && after.length === argv.length + 1 && argv.every((word, index) => word === after[index + 1]) ? after : argv;
|
|
785
|
+
}
|
|
766
786
|
// ../kit/src/process.ts
|
|
767
787
|
import { constants } from "os";
|
|
768
|
-
import
|
|
788
|
+
import process11 from "process";
|
|
769
789
|
var KILL_GRACE_MS = 2000;
|
|
770
790
|
function exitCodeOf(code, signal) {
|
|
771
791
|
if (code !== null)
|
|
@@ -785,7 +805,7 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal,
|
|
|
785
805
|
return;
|
|
786
806
|
try {
|
|
787
807
|
if (grouped)
|
|
788
|
-
|
|
808
|
+
process11.kill(-proc.pid, killing);
|
|
789
809
|
else
|
|
790
810
|
proc.kill(killing);
|
|
791
811
|
} catch {}
|
|
@@ -859,15 +879,15 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
|
|
|
859
879
|
var RESET_STYLE = `${ESC}[0m`;
|
|
860
880
|
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
861
881
|
// ../kit/src/cli.ts
|
|
862
|
-
import { readFileSync as
|
|
882
|
+
import { readFileSync as readFileSync3, realpathSync as realpathSync2 } from "fs";
|
|
863
883
|
import { dirname as dirname3, join as join5 } from "path";
|
|
864
|
-
import
|
|
884
|
+
import process13 from "process";
|
|
865
885
|
|
|
866
886
|
// ../kit/src/usage.ts
|
|
867
887
|
import { appendFileSync, mkdirSync } from "fs";
|
|
868
888
|
import { homedir as homedir3 } from "os";
|
|
869
889
|
import { dirname as dirname2, isAbsolute, join as join4 } from "path";
|
|
870
|
-
import
|
|
890
|
+
import process12 from "process";
|
|
871
891
|
|
|
872
892
|
// ../kit/src/logs.ts
|
|
873
893
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -881,14 +901,14 @@ var usageError;
|
|
|
881
901
|
function noteUsageError(message) {
|
|
882
902
|
usageError = message;
|
|
883
903
|
}
|
|
884
|
-
function stateHome(env =
|
|
904
|
+
function stateHome(env = process12.env) {
|
|
885
905
|
const dir = env.XDG_STATE_HOME;
|
|
886
906
|
return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
|
|
887
907
|
}
|
|
888
|
-
function usagePath(env =
|
|
908
|
+
function usagePath(env = process12.env) {
|
|
889
909
|
return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
890
910
|
}
|
|
891
|
-
function usageRecorded(env =
|
|
911
|
+
function usageRecorded(env = process12.env) {
|
|
892
912
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
893
913
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
894
914
|
}
|
|
@@ -902,7 +922,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
902
922
|
usageError = undefined;
|
|
903
923
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
904
924
|
}
|
|
905
|
-
function recordUsage(entry, env =
|
|
925
|
+
function recordUsage(entry, env = process12.env) {
|
|
906
926
|
if (!usageRecorded(env))
|
|
907
927
|
return;
|
|
908
928
|
try {
|
|
@@ -979,7 +999,6 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
|
|
|
979
999
|
header(colors, summary),
|
|
980
1000
|
"",
|
|
981
1001
|
usageLine(`${prefix} ${defaultCommand === undefined ? colors.cyan("<command>") : `[${colors.cyan("<command>")}]`} [options]`, colors),
|
|
982
|
-
...replacesRows(replaces, colors),
|
|
983
1002
|
"",
|
|
984
1003
|
...await commandRows(commands, colors),
|
|
985
1004
|
...own.length === 0 ? [] : ["", `${colors.bold(`Options of ${defaultCommand}:`)} ${colors.dim("the command run when none is named")}`, ...optionRows(Object.fromEntries(own), colors)],
|
|
@@ -993,8 +1012,8 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
|
|
|
993
1012
|
`);
|
|
994
1013
|
}
|
|
995
1014
|
function printError(prefix, message, hint, cause) {
|
|
996
|
-
const colors = paint(
|
|
997
|
-
const styled = takesControlCodes(
|
|
1015
|
+
const colors = paint(process13.stderr);
|
|
1016
|
+
const styled = takesControlCodes(process13.stderr);
|
|
998
1017
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
999
1018
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
1000
1019
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -1037,11 +1056,11 @@ async function dispatch(level, typed, { version, redact }) {
|
|
|
1037
1056
|
}
|
|
1038
1057
|
}
|
|
1039
1058
|
if (name === undefined && level.path.length > 1) {
|
|
1040
|
-
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
1059
|
+
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process13.stderr));
|
|
1041
1060
|
return 2;
|
|
1042
1061
|
}
|
|
1043
1062
|
if (name === undefined || name === "help" || name === "--help" || name === "-h") {
|
|
1044
|
-
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
1063
|
+
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process13.stdout));
|
|
1045
1064
|
return 0;
|
|
1046
1065
|
}
|
|
1047
1066
|
if (level.path.length === 1 && (name === "--version" || name === "-v")) {
|
|
@@ -1097,24 +1116,24 @@ function candidateLine(candidate) {
|
|
|
1097
1116
|
}
|
|
1098
1117
|
function positionalBeingTyped(before, { options, positionals }) {
|
|
1099
1118
|
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
1100
|
-
|
|
1119
|
+
const typed = [];
|
|
1101
1120
|
let at = 0;
|
|
1102
1121
|
while (at < before.length) {
|
|
1103
1122
|
const word = before[at];
|
|
1104
1123
|
if (word === "--") {
|
|
1105
|
-
|
|
1124
|
+
typed.push(...before.slice(at + 1));
|
|
1106
1125
|
break;
|
|
1107
1126
|
}
|
|
1108
1127
|
if (!word.startsWith("-"))
|
|
1109
|
-
|
|
1128
|
+
typed.push(word);
|
|
1110
1129
|
else if (takesValue(word))
|
|
1111
1130
|
at++;
|
|
1112
1131
|
at++;
|
|
1113
1132
|
}
|
|
1114
1133
|
const last = positionals.at(-1);
|
|
1115
|
-
if (
|
|
1116
|
-
return positionals[
|
|
1117
|
-
return last?.replace("]", "").endsWith("...") === true ? last : undefined;
|
|
1134
|
+
if (typed.length < positionals.length)
|
|
1135
|
+
return { name: positionals[typed.length], typed };
|
|
1136
|
+
return { name: last?.replace("]", "").endsWith("...") === true ? last : undefined, typed };
|
|
1118
1137
|
}
|
|
1119
1138
|
async function leafCandidates(run, words) {
|
|
1120
1139
|
const completing = await completingOf(run);
|
|
@@ -1122,12 +1141,12 @@ async function leafCandidates(run, words) {
|
|
|
1122
1141
|
return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
|
|
1123
1142
|
const flag = words.at(-2);
|
|
1124
1143
|
const option = flag?.startsWith("-") === true ? Object.entries(completing.options).find(([name, { short }]) => flag === `--${name}` || flag === `-${short}`)?.[1] : undefined;
|
|
1144
|
+
const { name, typed } = positionalBeingTyped(words.slice(0, -1), completing);
|
|
1125
1145
|
if (option?.type === "string") {
|
|
1126
|
-
const values = option.choices === undefined ? await option.candidates?.() : option.choices;
|
|
1146
|
+
const values = option.choices === undefined ? await option.candidates?.(typed) : option.choices;
|
|
1127
1147
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
1128
1148
|
}
|
|
1129
|
-
const
|
|
1130
|
-
const values = name === undefined ? undefined : await completing.candidates[name]?.();
|
|
1149
|
+
const values = name === undefined ? undefined : await completing.candidates[name]?.(typed);
|
|
1131
1150
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
1132
1151
|
}
|
|
1133
1152
|
async function candidates(scope, defaultCommand, words, before = []) {
|
|
@@ -1158,9 +1177,13 @@ async function printCandidates(found) {
|
|
|
1158
1177
|
return 0;
|
|
1159
1178
|
}
|
|
1160
1179
|
function packageVersion(entry = Bun.main) {
|
|
1161
|
-
|
|
1180
|
+
let resolved = entry;
|
|
1181
|
+
try {
|
|
1182
|
+
resolved = realpathSync2(entry);
|
|
1183
|
+
} catch {}
|
|
1184
|
+
for (let dir = dirname3(resolved);; dir = dirname3(dir)) {
|
|
1162
1185
|
try {
|
|
1163
|
-
const { version } = JSON.parse(
|
|
1186
|
+
const { version } = JSON.parse(readFileSync3(join5(dir, "package.json"), "utf8"));
|
|
1164
1187
|
if (typeof version === "string")
|
|
1165
1188
|
return version;
|
|
1166
1189
|
} catch {}
|
|
@@ -1168,7 +1191,7 @@ function packageVersion(entry = Bun.main) {
|
|
|
1168
1191
|
return "0.0.0";
|
|
1169
1192
|
}
|
|
1170
1193
|
}
|
|
1171
|
-
async function runCli(cli, argv =
|
|
1194
|
+
async function runCli(cli, argv = scriptArgs()) {
|
|
1172
1195
|
const version = cli.version ?? packageVersion();
|
|
1173
1196
|
if (argv[0] === COMPLETE)
|
|
1174
1197
|
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
@@ -1195,7 +1218,7 @@ function commandPath(scope, argv) {
|
|
|
1195
1218
|
}
|
|
1196
1219
|
return path.length === 0 ? undefined : path.join(" ");
|
|
1197
1220
|
}
|
|
1198
|
-
async function runMain({ name, version, run, redact, usage }, argv =
|
|
1221
|
+
async function runMain({ name, version, run, redact, usage }, argv = scriptArgs()) {
|
|
1199
1222
|
if (argv[0] === COMPLETE)
|
|
1200
1223
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
1201
1224
|
if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
|
|
@@ -1239,7 +1262,7 @@ function parseSize(text, bareUnit = 1) {
|
|
|
1239
1262
|
return Math.round(Number(match[1]) * (suffix === "" ? bareUnit : 1024 ** POWERS[suffix]));
|
|
1240
1263
|
}
|
|
1241
1264
|
// ../kit/src/watch.ts
|
|
1242
|
-
import
|
|
1265
|
+
import process14 from "process";
|
|
1243
1266
|
import { format } from "util";
|
|
1244
1267
|
var CLEAR_SCREEN = "\x1B[H\x1B[2J";
|
|
1245
1268
|
var DEFAULT_SECONDS = 2;
|
|
@@ -1261,7 +1284,7 @@ function withoutWatch(args) {
|
|
|
1261
1284
|
}
|
|
1262
1285
|
return rest;
|
|
1263
1286
|
}
|
|
1264
|
-
async function watching(value, step, stream =
|
|
1287
|
+
async function watching(value, step, stream = process14.stdout, sleep = Bun.sleep, rounds = Number.POSITIVE_INFINITY) {
|
|
1265
1288
|
const interval = value === "" ? DEFAULT_SECONDS * 1000 : duration(value, "watch", DEFAULT_SECONDS * 1000);
|
|
1266
1289
|
if (!takesControlCodes(stream))
|
|
1267
1290
|
badUsage("--watch redraws the screen, so it needs a terminal");
|
|
@@ -1269,12 +1292,12 @@ async function watching(value, step, stream = process13.stdout, sleep = Bun.slee
|
|
|
1269
1292
|
for (let round = 0;round < rounds; round++) {
|
|
1270
1293
|
const held = [];
|
|
1271
1294
|
const { log, info } = console;
|
|
1272
|
-
const write =
|
|
1295
|
+
const write = process14.stdout.write.bind(process14.stdout);
|
|
1273
1296
|
const hold = (...parts) => void held.push(`${format(...parts)}
|
|
1274
1297
|
`);
|
|
1275
1298
|
console.log = hold;
|
|
1276
1299
|
console.info = hold;
|
|
1277
|
-
|
|
1300
|
+
process14.stdout.write = (text) => {
|
|
1278
1301
|
held.push(typeof text === "string" ? text : Buffer.from(text).toString());
|
|
1279
1302
|
return true;
|
|
1280
1303
|
};
|
|
@@ -1283,7 +1306,7 @@ async function watching(value, step, stream = process13.stdout, sleep = Bun.slee
|
|
|
1283
1306
|
} finally {
|
|
1284
1307
|
console.log = log;
|
|
1285
1308
|
console.info = info;
|
|
1286
|
-
|
|
1309
|
+
process14.stdout.write = write;
|
|
1287
1310
|
}
|
|
1288
1311
|
stream.write(`${CLEAR_SCREEN}${held.join("")}`);
|
|
1289
1312
|
await sleep(interval);
|
|
@@ -1293,7 +1316,7 @@ async function watching(value, step, stream = process13.stdout, sleep = Bun.slee
|
|
|
1293
1316
|
// src/procs.ts
|
|
1294
1317
|
import { existsSync as existsSync3 } from "fs";
|
|
1295
1318
|
import { homedir as homedir4 } from "os";
|
|
1296
|
-
import
|
|
1319
|
+
import process16 from "process";
|
|
1297
1320
|
|
|
1298
1321
|
// src/read.ts
|
|
1299
1322
|
import { readdir, readFile, readlink } from "fs/promises";
|
|
@@ -1788,9 +1811,8 @@ var usage2 = `procs
|
|
|
1788
1811
|
|
|
1789
1812
|
Lists the leftover or heavy processes of the calling user that are worth cleaning, grouped by origin: the project folder their working directory is in, a browser profile for a chromium and its helpers, else their command. Each group shows its process count, total resident memory and oldest age.
|
|
1790
1813
|
A process is listed when it is a dev tool (bun, node, vite, nuxt, next, chromium and chrome, playwright and webview browsers and their helpers, language servers, test runners) and either an orphan, reparented to init or to the user's systemd, or heavy (500M resident by default). The processes below a listed one, and those sharing a browser profile with it, are listed too, so a chromium whose main process was killed groups with its leftover helpers.
|
|
1791
|
-
\`--kill\` is a dry run that names what it would stop unless --yes is given; it sends SIGTERM, then SIGKILL after --grace to what is still there. It never signals pid 1, shells, terminals, this command and its ancestors, or processes of other users
|
|
1792
|
-
|
|
1793
|
-
Exits 0 when it listed or stopped everything, 1 when a process survived SIGKILL, 2 on a usage error.`;
|
|
1814
|
+
\`--kill\` is a dry run that names what it would stop unless --yes is given; it sends SIGTERM, then SIGKILL after --grace to what is still there. It never signals pid 1, shells, terminals, this command and its ancestors, or processes of other users.`;
|
|
1815
|
+
var EXITS = [[0, "it listed or stopped everything"], [1, "a process survived SIGKILL"], [2, "a usage error"]];
|
|
1794
1816
|
var options = {
|
|
1795
1817
|
json: jsonOption("{ groups: [{ origin, kind, count, rss, oldestSeconds, tools, orphans, pids, processes }], skipped }"),
|
|
1796
1818
|
kill: { type: "boolean", conflicts: ["watch"], description: "stop the listed processes: a dry run without --yes" },
|
|
@@ -1818,7 +1840,7 @@ function summary(group) {
|
|
|
1818
1840
|
};
|
|
1819
1841
|
}
|
|
1820
1842
|
function render(groups) {
|
|
1821
|
-
const colors = paint(
|
|
1843
|
+
const colors = paint(process16.stdout);
|
|
1822
1844
|
const rows = groups.map((group) => [
|
|
1823
1845
|
group.origin,
|
|
1824
1846
|
String(group.members.length),
|
|
@@ -1831,6 +1853,7 @@ function render(groups) {
|
|
|
1831
1853
|
}
|
|
1832
1854
|
async function procs(args) {
|
|
1833
1855
|
const { values } = parse(args, usage2, options, {
|
|
1856
|
+
exits: EXITS,
|
|
1834
1857
|
replaces: "pkill -f and pgrep -f one-liners (which match their own shell), ps aux | grep for leftover chromium, bun or vite processes",
|
|
1835
1858
|
examples: [
|
|
1836
1859
|
["procs", "leftover and heavy dev processes, by origin"],
|
|
@@ -1845,9 +1868,9 @@ async function procs(args) {
|
|
|
1845
1868
|
if (values.watch !== undefined)
|
|
1846
1869
|
return watching(values.watch, async () => procs(withoutWatch(args)));
|
|
1847
1870
|
const olderMs = values.older === undefined ? undefined : duration(values.older, "older", 0, "m");
|
|
1848
|
-
const uid =
|
|
1871
|
+
const uid = process16.getuid?.() ?? -1;
|
|
1849
1872
|
const all = await snapshot("/proc", (owner) => owner === uid);
|
|
1850
|
-
const mine = new Set(ancestry(
|
|
1873
|
+
const mine = new Set(ancestry(process16.pid, new Map(all.map((proc) => [proc.pid, proc]))));
|
|
1851
1874
|
const { groups, protectedCount } = selectGroups(all, { guard: { uid, mine }, now: Date.now(), home: homedir4(), exists: existsSync3, minMem, olderMs });
|
|
1852
1875
|
const pids = groups.flatMap((group) => group.members.map(({ proc }) => proc.pid));
|
|
1853
1876
|
const show = (extra = {}) => {
|
|
@@ -1860,7 +1883,7 @@ async function procs(args) {
|
|
|
1860
1883
|
if (values.kill !== true || pids.length === 0 || values.yes !== true) {
|
|
1861
1884
|
show();
|
|
1862
1885
|
if (values.json !== true && protectedCount > 0)
|
|
1863
|
-
console.error(paint(
|
|
1886
|
+
console.error(paint(process16.stderr).dim(`${protectedCount} more left out: shells, terminals and this command's ancestors are never touched`));
|
|
1864
1887
|
if (values.kill === true && pids.length > 0)
|
|
1865
1888
|
console.error(`dry run: would stop ${pids.length} ${pids.length === 1 ? "process" : "processes"} (${pids.join(" ")}), pass --yes to do it`);
|
|
1866
1889
|
return 0;
|
|
@@ -1876,4 +1899,4 @@ async function procs(args) {
|
|
|
1876
1899
|
}
|
|
1877
1900
|
|
|
1878
1901
|
// src/procs-cli.ts
|
|
1879
|
-
|
|
1902
|
+
process17.exitCode = await runMain({ name: "procs", run: procs });
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-proc",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.7.
|
|
4
|
+
"version": "0.7.2",
|
|
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",
|
|
@@ -35,6 +35,6 @@
|
|
|
35
35
|
"procs": "$npm_execpath src/procs-cli.ts"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@kaynooo/cli-kit": "0.
|
|
38
|
+
"@kaynooo/cli-kit": "0.38.3"
|
|
39
39
|
}
|
|
40
40
|
}
|
package/src/pid.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ExitRow } from '@kaynooo/cli-kit'
|
|
1
2
|
import type { Limit, Socket } from './parse'
|
|
2
3
|
import type { ProcInfo } from './read'
|
|
3
4
|
import process from 'node:process'
|
|
@@ -9,9 +10,8 @@ import { bootMs, fdCount, readProc, readSockets, readText, socketsOf, userName }
|
|
|
9
10
|
const usage = `pid <pid>
|
|
10
11
|
|
|
11
12
|
Shows what a process is: its executable, working directory, command line, parent chain up to init, user, start time and age, memory, open files and limits, the TCP and UDP ports it listens on and its cgroup. Everything is read from /proc; what the calling user cannot read of another user's process is left out and named at the end.
|
|
12
|
-
The environment is not shown unless --env is given, with every value that looks like a secret masked
|
|
13
|
-
|
|
14
|
-
Exits 0 when it showed the process, 1 when no process has this pid, 2 on a usage error.`
|
|
13
|
+
The environment is not shown unless --env is given, with every value that looks like a secret masked.`
|
|
14
|
+
const EXITS: ExitRow[] = [[0, 'it showed the process'], [1, 'no process has this pid'], [2, 'a usage error']]
|
|
15
15
|
|
|
16
16
|
const DIGITS = /^\d+$/
|
|
17
17
|
|
|
@@ -166,6 +166,7 @@ function render(info: Report, showEnv: boolean): string {
|
|
|
166
166
|
|
|
167
167
|
export async function pid(args: string[]): Promise<number> {
|
|
168
168
|
const { values, positionals } = parse(args, usage, options, {
|
|
169
|
+
exits: EXITS,
|
|
169
170
|
positionals: ['<pid>'],
|
|
170
171
|
replaces: 'cat /proc/<pid>/{cmdline,environ,limits}, readlink /proc/<pid>/{exe,cwd}, the fish pid function',
|
|
171
172
|
examples: [
|
package/src/ports.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ExitRow } from '@kaynooo/cli-kit'
|
|
1
2
|
import type { Container, Protocol, Socket } from './parse'
|
|
2
3
|
import type { ProcInfo } from './read'
|
|
3
4
|
import { basename } from 'node:path'
|
|
@@ -13,9 +14,8 @@ const usage = `ports [port]
|
|
|
13
14
|
|
|
14
15
|
Lists what listens on this machine, TCP and UDP over IPv4 and IPv6, one row per port with its bound address, the owning process, its command and its working directory. A port Docker publishes is held by docker-proxy, and shows as its compose project and service when docker is installed; the owner of a socket of another user cannot be read and shows as -.
|
|
15
16
|
\`ports <port>\` keeps that port alone and exits 1 when nothing listens on it.
|
|
16
|
-
\`ports <port> --kill\` stops its owner with SIGTERM, and with --force SIGKILL once --grace has passed, after asking on a terminal unless --yes is given. It refuses processes of other users, pid 1, this command and its ancestors, and docker-proxy, for which it names the container to stop instead
|
|
17
|
-
|
|
18
|
-
Exits 0 when it listed or stopped, 1 when nothing listens on the given port or the owner could not be stopped, 2 on a usage error.`
|
|
17
|
+
\`ports <port> --kill\` stops its owner with SIGTERM, and with --force SIGKILL once --grace has passed, after asking on a terminal unless --yes is given. It refuses processes of other users, pid 1, this command and its ancestors, and docker-proxy, for which it names the container to stop instead.`
|
|
18
|
+
const EXITS: ExitRow[] = [[0, 'it listed or stopped'], [1, 'nothing listens on the given port or the owner could not be stopped'], [2, 'a usage error']]
|
|
19
19
|
|
|
20
20
|
const options = {
|
|
21
21
|
json: jsonOption('[{ port, protocol, addresses, pid, command, cwd, container }]'),
|
|
@@ -143,6 +143,7 @@ async function stop(port: number, rows: PortRow[], procs: Map<number, ProcInfo>,
|
|
|
143
143
|
|
|
144
144
|
export async function ports(args: string[]): Promise<number> {
|
|
145
145
|
const { values, positionals } = parse(args, usage, options, {
|
|
146
|
+
exits: EXITS,
|
|
146
147
|
positionals: ['[port]'],
|
|
147
148
|
replaces: 'ss -ltnp | grep, lsof -i, fuser -k and lookups of docker-proxy by hand',
|
|
148
149
|
examples: [
|
package/src/procs.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ExitRow } from '@kaynooo/cli-kit'
|
|
1
2
|
import type { Group } from './select'
|
|
2
3
|
import { existsSync } from 'node:fs'
|
|
3
4
|
import { homedir } from 'node:os'
|
|
@@ -12,9 +13,8 @@ const usage = `procs
|
|
|
12
13
|
|
|
13
14
|
Lists the leftover or heavy processes of the calling user that are worth cleaning, grouped by origin: the project folder their working directory is in, a browser profile for a chromium and its helpers, else their command. Each group shows its process count, total resident memory and oldest age.
|
|
14
15
|
A process is listed when it is a dev tool (bun, node, vite, nuxt, next, chromium and chrome, playwright and webview browsers and their helpers, language servers, test runners) and either an orphan, reparented to init or to the user's systemd, or heavy (500M resident by default). The processes below a listed one, and those sharing a browser profile with it, are listed too, so a chromium whose main process was killed groups with its leftover helpers.
|
|
15
|
-
\`--kill\` is a dry run that names what it would stop unless --yes is given; it sends SIGTERM, then SIGKILL after --grace to what is still there. It never signals pid 1, shells, terminals, this command and its ancestors, or processes of other users
|
|
16
|
-
|
|
17
|
-
Exits 0 when it listed or stopped everything, 1 when a process survived SIGKILL, 2 on a usage error.`
|
|
16
|
+
\`--kill\` is a dry run that names what it would stop unless --yes is given; it sends SIGTERM, then SIGKILL after --grace to what is still there. It never signals pid 1, shells, terminals, this command and its ancestors, or processes of other users.`
|
|
17
|
+
const EXITS: ExitRow[] = [[0, 'it listed or stopped everything'], [1, 'a process survived SIGKILL'], [2, 'a usage error']]
|
|
18
18
|
|
|
19
19
|
const options = {
|
|
20
20
|
'json': jsonOption('{ groups: [{ origin, kind, count, rss, oldestSeconds, tools, orphans, pids, processes }], skipped }'),
|
|
@@ -62,6 +62,7 @@ function render(groups: Group[]): string {
|
|
|
62
62
|
|
|
63
63
|
export async function procs(args: string[]): Promise<number> {
|
|
64
64
|
const { values } = parse(args, usage, options, {
|
|
65
|
+
exits: EXITS,
|
|
65
66
|
replaces: 'pkill -f and pgrep -f one-liners (which match their own shell), ps aux | grep for leftover chromium, bun or vite processes',
|
|
66
67
|
examples: [
|
|
67
68
|
['procs', 'leftover and heavy dev processes, by origin'],
|