@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/proc-cli.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/proc-cli.ts
|
|
5
|
-
import
|
|
5
|
+
import process20 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);
|
|
@@ -1291,7 +1314,7 @@ async function watching(value, step, stream = process13.stdout, sleep = Bun.slee
|
|
|
1291
1314
|
return code;
|
|
1292
1315
|
}
|
|
1293
1316
|
// src/pid.ts
|
|
1294
|
-
import
|
|
1317
|
+
import process16 from "process";
|
|
1295
1318
|
|
|
1296
1319
|
// src/mask.ts
|
|
1297
1320
|
import { basename } from "path";
|
|
@@ -1608,9 +1631,8 @@ async function userName(uid) {
|
|
|
1608
1631
|
var usage2 = `pid <pid>
|
|
1609
1632
|
|
|
1610
1633
|
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.
|
|
1611
|
-
The environment is not shown unless --env is given, with every value that looks like a secret masked
|
|
1612
|
-
|
|
1613
|
-
Exits 0 when it showed the process, 1 when no process has this pid, 2 on a usage error.`;
|
|
1634
|
+
The environment is not shown unless --env is given, with every value that looks like a secret masked.`;
|
|
1635
|
+
var EXITS = [[0, "it showed the process"], [1, "no process has this pid"], [2, "a usage error"]];
|
|
1614
1636
|
var DIGITS2 = /^\d+$/;
|
|
1615
1637
|
var options = {
|
|
1616
1638
|
env: { type: "boolean", description: "show the environment, secrets masked" },
|
|
@@ -1682,7 +1704,7 @@ function limitLine(limits, name) {
|
|
|
1682
1704
|
return limit === undefined ? "-" : `soft ${limit.soft}, hard ${limit.hard}`;
|
|
1683
1705
|
}
|
|
1684
1706
|
function render(info, showEnv) {
|
|
1685
|
-
const colors = paint(
|
|
1707
|
+
const colors = paint(process16.stdout);
|
|
1686
1708
|
const memory = info.memory;
|
|
1687
1709
|
const size = (bytes) => bytes === undefined ? "-" : tt(bytes);
|
|
1688
1710
|
const rows = [
|
|
@@ -1712,6 +1734,7 @@ function render(info, showEnv) {
|
|
|
1712
1734
|
}
|
|
1713
1735
|
async function pid(args) {
|
|
1714
1736
|
const { values, positionals } = parse(args, usage2, options, {
|
|
1737
|
+
exits: EXITS,
|
|
1715
1738
|
positionals: ["<pid>"],
|
|
1716
1739
|
replaces: "cat /proc/<pid>/{cmdline,environ,limits}, readlink /proc/<pid>/{exe,cwd}, the fish pid function",
|
|
1717
1740
|
examples: [
|
|
@@ -1735,10 +1758,10 @@ async function pid(args) {
|
|
|
1735
1758
|
|
|
1736
1759
|
// src/ports.ts
|
|
1737
1760
|
import { basename as basename2 } from "path";
|
|
1738
|
-
import
|
|
1761
|
+
import process18 from "process";
|
|
1739
1762
|
|
|
1740
1763
|
// ../kit/src/prompt.ts
|
|
1741
|
-
import
|
|
1764
|
+
import process17 from "process";
|
|
1742
1765
|
|
|
1743
1766
|
// ../kit/src/picker/score.ts
|
|
1744
1767
|
var BOUNDARY_AFTER = new Set([" ", "/", "-", "_", ".", ":", "\\", "(", "[", ",", "\t"]);
|
|
@@ -1830,7 +1853,7 @@ function asking(colors, question, typed, keys) {
|
|
|
1830
1853
|
function answer(colors, question, state, picked) {
|
|
1831
1854
|
return state === "enter" ? `${colors.green("\u2713")} ${question} ${colors.cyan(picked)}` : `${colors.red("\u2717")} ${question} ${colors.dim("cancelled")}`;
|
|
1832
1855
|
}
|
|
1833
|
-
async function ask(question, { hint, terminal =
|
|
1856
|
+
async function ask(question, { hint, terminal = process17 }, render, press) {
|
|
1834
1857
|
const { stdin, stderr } = terminal;
|
|
1835
1858
|
if (!isInteractive(terminal))
|
|
1836
1859
|
fail(`cannot ask "${question}" off a terminal`, 2, hint);
|
|
@@ -1965,9 +1988,8 @@ var usage3 = `ports [port]
|
|
|
1965
1988
|
|
|
1966
1989
|
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 -.
|
|
1967
1990
|
\`ports <port>\` keeps that port alone and exits 1 when nothing listens on it.
|
|
1968
|
-
\`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
|
|
1969
|
-
|
|
1970
|
-
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.`;
|
|
1991
|
+
\`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.`;
|
|
1992
|
+
var EXITS2 = [[0, "it listed or stopped"], [1, "nothing listens on the given port or the owner could not be stopped"], [2, "a usage error"]];
|
|
1971
1993
|
var options2 = {
|
|
1972
1994
|
json: jsonOption("[{ port, protocol, addresses, pid, command, cwd, container }]"),
|
|
1973
1995
|
kill: { type: "boolean", conflicts: ["watch"], description: "stop the owner of the given port" },
|
|
@@ -2016,7 +2038,7 @@ function refusal(row, proc, guard) {
|
|
|
2016
2038
|
return reason === undefined ? undefined : `pid ${row.pid} is ${reason}`;
|
|
2017
2039
|
}
|
|
2018
2040
|
function render2(rows) {
|
|
2019
|
-
const colors = paint(
|
|
2041
|
+
const colors = paint(process18.stdout);
|
|
2020
2042
|
const cells = rows.map((row) => [
|
|
2021
2043
|
colors.cyan(String(row.port)),
|
|
2022
2044
|
row.protocol,
|
|
@@ -2028,7 +2050,7 @@ function render2(rows) {
|
|
|
2028
2050
|
return table([["PORT", "PROTO", "ADDRESS", "PID", "COMMAND", "CWD"], ...cells], { header: true });
|
|
2029
2051
|
}
|
|
2030
2052
|
async function stop(port, rows, procs, values) {
|
|
2031
|
-
const guard = { uid:
|
|
2053
|
+
const guard = { uid: process18.getuid?.() ?? -1, mine: new Set(ancestry(process18.pid, procs)) };
|
|
2032
2054
|
const targets = new Map;
|
|
2033
2055
|
for (const row of rows) {
|
|
2034
2056
|
const reason = refusal(row, row.pid === undefined ? undefined : procs.get(row.pid), guard);
|
|
@@ -2041,7 +2063,7 @@ async function stop(port, rows, procs, values) {
|
|
|
2041
2063
|
return 1;
|
|
2042
2064
|
const names = [...targets].map(([pid, row]) => `${pid} (${truncate(row.command ?? "", 40)})`).join(", ");
|
|
2043
2065
|
if (values.yes !== true) {
|
|
2044
|
-
if (!
|
|
2066
|
+
if (!process18.stdin.isTTY)
|
|
2045
2067
|
fail(`stopping ${names} needs a terminal to ask on: pass --yes`, 2);
|
|
2046
2068
|
if (await confirm(`Stop ${names}?`, { default: false }) !== true)
|
|
2047
2069
|
return 1;
|
|
@@ -2055,6 +2077,7 @@ async function stop(port, rows, procs, values) {
|
|
|
2055
2077
|
}
|
|
2056
2078
|
async function ports(args) {
|
|
2057
2079
|
const { values, positionals } = parse(args, usage3, options2, {
|
|
2080
|
+
exits: EXITS2,
|
|
2058
2081
|
positionals: ["[port]"],
|
|
2059
2082
|
replaces: "ss -ltnp | grep, lsof -i, fuser -k and lookups of docker-proxy by hand",
|
|
2060
2083
|
examples: [
|
|
@@ -2072,7 +2095,7 @@ async function ports(args) {
|
|
|
2072
2095
|
badUsage(`<port> takes a whole number from 1 to 65535, not ${given}`);
|
|
2073
2096
|
if (values.kill === true && port === undefined)
|
|
2074
2097
|
badUsage("--kill takes the port to free: ports <port> --kill");
|
|
2075
|
-
const [sockets, owners, procs] = await Promise.all([readSockets(), socketOwners(), snapshot("/proc", (uid) => uid ===
|
|
2098
|
+
const [sockets, owners, procs] = await Promise.all([readSockets(), socketOwners(), snapshot("/proc", (uid) => uid === process18.getuid?.())]);
|
|
2076
2099
|
const byPid = new Map(procs.map((proc) => [proc.pid, proc]));
|
|
2077
2100
|
const wanted = sockets.filter((socket) => port === undefined || socket.port === port);
|
|
2078
2101
|
const needsDocker = wanted.some((socket) => {
|
|
@@ -2093,14 +2116,14 @@ async function ports(args) {
|
|
|
2093
2116
|
console.log(render2(rows));
|
|
2094
2117
|
const unknown = rows.filter((row) => row.pid === undefined && row.container === undefined).length;
|
|
2095
2118
|
if (unknown > 0)
|
|
2096
|
-
console.error(paint(
|
|
2119
|
+
console.error(paint(process18.stderr).dim(`${unknown} ${unknown === 1 ? "port has" : "ports have"} an owner this user cannot read, shown as -`));
|
|
2097
2120
|
return 0;
|
|
2098
2121
|
}
|
|
2099
2122
|
|
|
2100
2123
|
// src/procs.ts
|
|
2101
2124
|
import { existsSync as existsSync3 } from "fs";
|
|
2102
2125
|
import { homedir as homedir4 } from "os";
|
|
2103
|
-
import
|
|
2126
|
+
import process19 from "process";
|
|
2104
2127
|
|
|
2105
2128
|
// src/select.ts
|
|
2106
2129
|
import { basename as basename3, join as join6, relative } from "path";
|
|
@@ -2220,9 +2243,8 @@ var usage4 = `procs
|
|
|
2220
2243
|
|
|
2221
2244
|
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.
|
|
2222
2245
|
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.
|
|
2223
|
-
\`--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
|
|
2224
|
-
|
|
2225
|
-
Exits 0 when it listed or stopped everything, 1 when a process survived SIGKILL, 2 on a usage error.`;
|
|
2246
|
+
\`--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.`;
|
|
2247
|
+
var EXITS3 = [[0, "it listed or stopped everything"], [1, "a process survived SIGKILL"], [2, "a usage error"]];
|
|
2226
2248
|
var options3 = {
|
|
2227
2249
|
json: jsonOption("{ groups: [{ origin, kind, count, rss, oldestSeconds, tools, orphans, pids, processes }], skipped }"),
|
|
2228
2250
|
kill: { type: "boolean", conflicts: ["watch"], description: "stop the listed processes: a dry run without --yes" },
|
|
@@ -2250,7 +2272,7 @@ function summary(group) {
|
|
|
2250
2272
|
};
|
|
2251
2273
|
}
|
|
2252
2274
|
function render3(groups) {
|
|
2253
|
-
const colors = paint(
|
|
2275
|
+
const colors = paint(process19.stdout);
|
|
2254
2276
|
const rows = groups.map((group) => [
|
|
2255
2277
|
group.origin,
|
|
2256
2278
|
String(group.members.length),
|
|
@@ -2263,6 +2285,7 @@ function render3(groups) {
|
|
|
2263
2285
|
}
|
|
2264
2286
|
async function procs(args) {
|
|
2265
2287
|
const { values } = parse(args, usage4, options3, {
|
|
2288
|
+
exits: EXITS3,
|
|
2266
2289
|
replaces: "pkill -f and pgrep -f one-liners (which match their own shell), ps aux | grep for leftover chromium, bun or vite processes",
|
|
2267
2290
|
examples: [
|
|
2268
2291
|
["procs", "leftover and heavy dev processes, by origin"],
|
|
@@ -2277,9 +2300,9 @@ async function procs(args) {
|
|
|
2277
2300
|
if (values.watch !== undefined)
|
|
2278
2301
|
return watching(values.watch, async () => procs(withoutWatch(args)));
|
|
2279
2302
|
const olderMs = values.older === undefined ? undefined : duration(values.older, "older", 0, "m");
|
|
2280
|
-
const uid =
|
|
2303
|
+
const uid = process19.getuid?.() ?? -1;
|
|
2281
2304
|
const all = await snapshot("/proc", (owner) => owner === uid);
|
|
2282
|
-
const mine = new Set(ancestry(
|
|
2305
|
+
const mine = new Set(ancestry(process19.pid, new Map(all.map((proc) => [proc.pid, proc]))));
|
|
2283
2306
|
const { groups, protectedCount } = selectGroups(all, { guard: { uid, mine }, now: Date.now(), home: homedir4(), exists: existsSync3, minMem, olderMs });
|
|
2284
2307
|
const pids = groups.flatMap((group) => group.members.map(({ proc }) => proc.pid));
|
|
2285
2308
|
const show = (extra = {}) => {
|
|
@@ -2292,7 +2315,7 @@ async function procs(args) {
|
|
|
2292
2315
|
if (values.kill !== true || pids.length === 0 || values.yes !== true) {
|
|
2293
2316
|
show();
|
|
2294
2317
|
if (values.json !== true && protectedCount > 0)
|
|
2295
|
-
console.error(paint(
|
|
2318
|
+
console.error(paint(process19.stderr).dim(`${protectedCount} more left out: shells, terminals and this command's ancestors are never touched`));
|
|
2296
2319
|
if (values.kill === true && pids.length > 0)
|
|
2297
2320
|
console.error(`dry run: would stop ${pids.length} ${pids.length === 1 ? "process" : "processes"} (${pids.join(" ")}), pass --yes to do it`);
|
|
2298
2321
|
return 0;
|
|
@@ -2308,7 +2331,7 @@ async function procs(args) {
|
|
|
2308
2331
|
}
|
|
2309
2332
|
|
|
2310
2333
|
// src/proc-cli.ts
|
|
2311
|
-
|
|
2334
|
+
process20.exitCode = await runCli({
|
|
2312
2335
|
name: "proc",
|
|
2313
2336
|
about: "what a process is, who listens on a port and which dev processes are worth cleaning, read from /proc",
|
|
2314
2337
|
replaces: "cat /proc/<pid>/..., ss -ltnp, lsof -i and pgrep -f one-liners",
|