@kaynooo/cli-proc 0.7.3 → 0.7.4
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 +80 -31
- package/dist/ports-cli.js +142 -42
- package/dist/proc-cli.js +149 -49
- package/dist/procs-cli.js +83 -34
- package/package.json +2 -2
- package/src/read.ts +19 -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 process18 from "process";
|
|
6
6
|
// ../kit/src/args.ts
|
|
7
7
|
import process9 from "process";
|
|
8
8
|
import { parseArgs } from "util";
|
|
@@ -390,6 +390,9 @@ function formatSeconds(total) {
|
|
|
390
390
|
const next = amounts[first + 1];
|
|
391
391
|
return next === undefined || next.startsWith("0") ? amounts[first] ?? "0s" : `${amounts[first]} ${next}`;
|
|
392
392
|
}
|
|
393
|
+
function formatDuration(ms) {
|
|
394
|
+
return ms < 60000 ? `${(Math.max(0, ms) / 1000).toFixed(1)}s` : formatSeconds(ms / 1000);
|
|
395
|
+
}
|
|
393
396
|
function upTo(text, separator) {
|
|
394
397
|
const end = text.indexOf(separator);
|
|
395
398
|
return end === -1 ? text : text.slice(0, end);
|
|
@@ -450,7 +453,7 @@ function printJson(value) {
|
|
|
450
453
|
|
|
451
454
|
// ../kit/src/help.ts
|
|
452
455
|
import process8 from "process";
|
|
453
|
-
var OPTIONAL =
|
|
456
|
+
var OPTIONAL = /(?<!\x1B)\[[^\]]+\]/g;
|
|
454
457
|
var USAGE_PREFIX = /^usage:\s*/i;
|
|
455
458
|
var FULL_STOP = /\.$/;
|
|
456
459
|
function usageLine(usage, colors = paint(process8.stdout)) {
|
|
@@ -489,29 +492,40 @@ function exampleRows(examples, colors = paint(process8.stdout)) {
|
|
|
489
492
|
return [];
|
|
490
493
|
return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
|
|
491
494
|
}
|
|
495
|
+
var MAX_INLINE_CHOICES = 24;
|
|
492
496
|
function optionRows(options, colors = paint(process8.stdout)) {
|
|
493
497
|
const rows = Object.entries(options).map(([name, option]) => {
|
|
494
498
|
const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
|
|
495
|
-
const
|
|
499
|
+
const choices = option.choices?.join("|");
|
|
500
|
+
const inline = choices !== undefined && choices.length <= MAX_INLINE_CHOICES;
|
|
501
|
+
const named = inline ? `<${choices}>` : "<value>";
|
|
496
502
|
const placeholder = option.optional === undefined ? named : `[${named}]`;
|
|
497
503
|
const value = option.type === "string" ? ` ${colors.dim(placeholder)}` : "";
|
|
498
|
-
|
|
504
|
+
const described = choices === undefined || inline ? option.description ?? "" : [option.description, `one of ${option.choices.join(", ")}`].filter(Boolean).join("; ");
|
|
505
|
+
return { flag: `${short}${colors.cyan(`--${name}`)}${value}`, width: `-x, --${name}`.length + (option.type === "string" ? placeholder.length + 1 : 0), description: described };
|
|
499
506
|
});
|
|
500
507
|
const width = Math.max(0, ...rows.map((row) => row.width));
|
|
501
508
|
return rows.map((row) => row.description === "" ? ` ${row.flag}` : ` ${row.flag}${" ".repeat(width - row.width)} ${colors.dim(row.description)}`);
|
|
502
509
|
}
|
|
503
510
|
|
|
504
511
|
// ../kit/src/text.ts
|
|
505
|
-
var ANSI = new RegExp(`${String.fromCharCode(27)}\\[[\\d;?]*[a-z]`, "gi");
|
|
506
512
|
function distance(a, b) {
|
|
507
|
-
|
|
508
|
-
|
|
513
|
+
const left = [...a];
|
|
514
|
+
const right = [...b];
|
|
515
|
+
let before = [];
|
|
516
|
+
let previous = Array.from({ length: right.length + 1 }, (_, index) => index);
|
|
517
|
+
for (const [i, x] of left.entries()) {
|
|
509
518
|
const current = [i + 1];
|
|
510
|
-
for (const [j,
|
|
511
|
-
|
|
519
|
+
for (const [j, y] of right.entries()) {
|
|
520
|
+
let cost = Math.min((previous[j + 1] ?? 0) + 1, (current[j] ?? 0) + 1, (previous[j] ?? 0) + (x === y ? 0 : 1));
|
|
521
|
+
if (i > 0 && j > 0 && x === right[j - 1] && left[i - 1] === y)
|
|
522
|
+
cost = Math.min(cost, (before[j - 1] ?? 0) + 1);
|
|
523
|
+
current.push(cost);
|
|
524
|
+
}
|
|
525
|
+
before = previous;
|
|
512
526
|
previous = current;
|
|
513
527
|
}
|
|
514
|
-
return previous[
|
|
528
|
+
return previous[right.length] ?? 0;
|
|
515
529
|
}
|
|
516
530
|
function closest(name, names) {
|
|
517
531
|
const started = names.find((known) => known.startsWith(name));
|
|
@@ -881,13 +895,31 @@ var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
|
881
895
|
// ../kit/src/cli.ts
|
|
882
896
|
import { readFileSync as readFileSync3, realpathSync as realpathSync2 } from "fs";
|
|
883
897
|
import { dirname as dirname3, join as join5 } from "path";
|
|
884
|
-
import
|
|
898
|
+
import process14 from "process";
|
|
899
|
+
|
|
900
|
+
// ../kit/src/notify.ts
|
|
901
|
+
import process12 from "process";
|
|
902
|
+
var NOTIFY_AFTER_MS = 1e4;
|
|
903
|
+
var CANCELLED = 130;
|
|
904
|
+
function base64(text) {
|
|
905
|
+
return Buffer.from(text).toString("base64");
|
|
906
|
+
}
|
|
907
|
+
function notification(title, body, env = process12.env) {
|
|
908
|
+
const id = `kit${process12.pid}`;
|
|
909
|
+
const codes = [`\x1B]99;i=${id}:d=0:o=unfocused:e=1;${base64(title)}\x1B\\`, `\x1B]99;i=${id}:p=body:e=1;${base64(body)}\x1B\\`];
|
|
910
|
+
return codes.map((code) => env.TMUX === undefined || env.TMUX === "" ? code : `\x1BPtmux;${code.replaceAll("\x1B", "\x1B\x1B")}\x1B\\`).join("");
|
|
911
|
+
}
|
|
912
|
+
function notifyEnd(command, code, ms, { stream = process12.stderr, env = process12.env } = {}) {
|
|
913
|
+
if (ms < NOTIFY_AFTER_MS || code === CANCELLED || outputMode(stream, env) !== "terminal")
|
|
914
|
+
return;
|
|
915
|
+
stream.write(notification(command, code === 0 ? `done in ${formatDuration(ms)}` : `failed with exit ${code} after ${formatDuration(ms)}`, env));
|
|
916
|
+
}
|
|
885
917
|
|
|
886
918
|
// ../kit/src/usage.ts
|
|
887
919
|
import { appendFileSync, mkdirSync } from "fs";
|
|
888
920
|
import { homedir as homedir3 } from "os";
|
|
889
921
|
import { dirname as dirname2, isAbsolute, join as join4 } from "path";
|
|
890
|
-
import
|
|
922
|
+
import process13 from "process";
|
|
891
923
|
|
|
892
924
|
// ../kit/src/logs.ts
|
|
893
925
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -901,14 +933,14 @@ var usageError;
|
|
|
901
933
|
function noteUsageError(message) {
|
|
902
934
|
usageError = message;
|
|
903
935
|
}
|
|
904
|
-
function stateHome(env =
|
|
936
|
+
function stateHome(env = process13.env) {
|
|
905
937
|
const dir = env.XDG_STATE_HOME;
|
|
906
938
|
return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
|
|
907
939
|
}
|
|
908
|
-
function usagePath(env =
|
|
940
|
+
function usagePath(env = process13.env) {
|
|
909
941
|
return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
910
942
|
}
|
|
911
|
-
function usageRecorded(env =
|
|
943
|
+
function usageRecorded(env = process13.env) {
|
|
912
944
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
913
945
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
914
946
|
}
|
|
@@ -922,7 +954,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
922
954
|
usageError = undefined;
|
|
923
955
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
924
956
|
}
|
|
925
|
-
function recordUsage(entry, env =
|
|
957
|
+
function recordUsage(entry, env = process13.env) {
|
|
926
958
|
if (!usageRecorded(env))
|
|
927
959
|
return;
|
|
928
960
|
try {
|
|
@@ -1012,8 +1044,8 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
|
|
|
1012
1044
|
`);
|
|
1013
1045
|
}
|
|
1014
1046
|
function printError(prefix, message, hint, cause) {
|
|
1015
|
-
const colors = paint(
|
|
1016
|
-
const styled = takesControlCodes(
|
|
1047
|
+
const colors = paint(process14.stderr);
|
|
1048
|
+
const styled = takesControlCodes(process14.stderr);
|
|
1017
1049
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
1018
1050
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
1019
1051
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -1056,11 +1088,11 @@ async function dispatch(level, typed, { version, redact }) {
|
|
|
1056
1088
|
}
|
|
1057
1089
|
}
|
|
1058
1090
|
if (name === undefined && level.path.length > 1) {
|
|
1059
|
-
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
1091
|
+
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process14.stderr));
|
|
1060
1092
|
return 2;
|
|
1061
1093
|
}
|
|
1062
1094
|
if (name === undefined || name === "help" || name === "--help" || name === "-h") {
|
|
1063
|
-
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
1095
|
+
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process14.stdout));
|
|
1064
1096
|
return 0;
|
|
1065
1097
|
}
|
|
1066
1098
|
if (level.path.length === 1 && (name === "--version" || name === "-v")) {
|
|
@@ -1195,10 +1227,13 @@ async function runCli(cli, argv = scriptArgs()) {
|
|
|
1195
1227
|
const version = cli.version ?? packageVersion();
|
|
1196
1228
|
if (argv[0] === COMPLETE)
|
|
1197
1229
|
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
1230
|
+
const started = performance.now();
|
|
1198
1231
|
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 });
|
|
1232
|
+
const command = commandPath(scopeOf(cli.commands, cli.extraCommands, cli.aliases), argv);
|
|
1233
|
+
notifyEnd(command === undefined ? cli.name : `${cli.name} ${command}`, code, performance.now() - started);
|
|
1199
1234
|
if (cli.usage !== false) {
|
|
1200
1235
|
try {
|
|
1201
|
-
recordUsage(usageEntry(cli.name,
|
|
1236
|
+
recordUsage(usageEntry(cli.name, command, argv, code));
|
|
1202
1237
|
} catch {}
|
|
1203
1238
|
}
|
|
1204
1239
|
return code;
|
|
@@ -1226,11 +1261,13 @@ async function runMain({ name, version, run, redact, usage }, argv = scriptArgs(
|
|
|
1226
1261
|
return 0;
|
|
1227
1262
|
}
|
|
1228
1263
|
let code;
|
|
1264
|
+
const started = performance.now();
|
|
1229
1265
|
try {
|
|
1230
1266
|
code = await run(argv);
|
|
1231
1267
|
} catch (error) {
|
|
1232
1268
|
code = reportThrown(name, error, redact, wantsJson(argv));
|
|
1233
1269
|
}
|
|
1270
|
+
notifyEnd(name, code, performance.now() - started);
|
|
1234
1271
|
if (usage !== false) {
|
|
1235
1272
|
try {
|
|
1236
1273
|
recordUsage(usageEntry(name, undefined, argv, code));
|
|
@@ -1262,7 +1299,7 @@ function parseSize(text, bareUnit = 1) {
|
|
|
1262
1299
|
return Math.round(Number(match[1]) * (suffix === "" ? bareUnit : 1024 ** POWERS[suffix]));
|
|
1263
1300
|
}
|
|
1264
1301
|
// ../kit/src/watch.ts
|
|
1265
|
-
import
|
|
1302
|
+
import process15 from "process";
|
|
1266
1303
|
import { format } from "util";
|
|
1267
1304
|
var CLEAR_SCREEN = "\x1B[H\x1B[2J";
|
|
1268
1305
|
var DEFAULT_SECONDS = 2;
|
|
@@ -1284,7 +1321,7 @@ function withoutWatch(args) {
|
|
|
1284
1321
|
}
|
|
1285
1322
|
return rest;
|
|
1286
1323
|
}
|
|
1287
|
-
async function watching(value, step, stream =
|
|
1324
|
+
async function watching(value, step, stream = process15.stdout, sleep = Bun.sleep, rounds = Number.POSITIVE_INFINITY) {
|
|
1288
1325
|
const interval = value === "" ? DEFAULT_SECONDS * 1000 : duration(value, "watch", DEFAULT_SECONDS * 1000);
|
|
1289
1326
|
if (!takesControlCodes(stream))
|
|
1290
1327
|
badUsage("--watch redraws the screen, so it needs a terminal");
|
|
@@ -1292,12 +1329,12 @@ async function watching(value, step, stream = process14.stdout, sleep = Bun.slee
|
|
|
1292
1329
|
for (let round = 0;round < rounds; round++) {
|
|
1293
1330
|
const held = [];
|
|
1294
1331
|
const { log, info } = console;
|
|
1295
|
-
const write =
|
|
1332
|
+
const write = process15.stdout.write.bind(process15.stdout);
|
|
1296
1333
|
const hold = (...parts) => void held.push(`${format(...parts)}
|
|
1297
1334
|
`);
|
|
1298
1335
|
console.log = hold;
|
|
1299
1336
|
console.info = hold;
|
|
1300
|
-
|
|
1337
|
+
process15.stdout.write = (text) => {
|
|
1301
1338
|
held.push(typeof text === "string" ? text : Buffer.from(text).toString());
|
|
1302
1339
|
return true;
|
|
1303
1340
|
};
|
|
@@ -1306,7 +1343,7 @@ async function watching(value, step, stream = process14.stdout, sleep = Bun.slee
|
|
|
1306
1343
|
} finally {
|
|
1307
1344
|
console.log = log;
|
|
1308
1345
|
console.info = info;
|
|
1309
|
-
|
|
1346
|
+
process15.stdout.write = write;
|
|
1310
1347
|
}
|
|
1311
1348
|
stream.write(`${CLEAR_SCREEN}${held.join("")}`);
|
|
1312
1349
|
await sleep(interval);
|
|
@@ -1316,7 +1353,7 @@ async function watching(value, step, stream = process14.stdout, sleep = Bun.slee
|
|
|
1316
1353
|
// src/procs.ts
|
|
1317
1354
|
import { existsSync as existsSync3 } from "fs";
|
|
1318
1355
|
import { homedir as homedir4 } from "os";
|
|
1319
|
-
import
|
|
1356
|
+
import process17 from "process";
|
|
1320
1357
|
|
|
1321
1358
|
// src/read.ts
|
|
1322
1359
|
import { readdir, readFile, readlink } from "fs/promises";
|
|
@@ -1563,10 +1600,22 @@ async function socketsOf(pid, root = "/proc") {
|
|
|
1563
1600
|
return inodes;
|
|
1564
1601
|
}
|
|
1565
1602
|
async function socketOwners(root = "/proc") {
|
|
1566
|
-
const
|
|
1603
|
+
const holders = new Map;
|
|
1567
1604
|
for (const pid of await listPids(root)) {
|
|
1568
1605
|
for (const inode of await socketsOf(pid, root))
|
|
1569
|
-
|
|
1606
|
+
holders.set(inode, [...holders.get(inode) ?? [], pid]);
|
|
1607
|
+
}
|
|
1608
|
+
const owners = new Map;
|
|
1609
|
+
const parents = new Map;
|
|
1610
|
+
for (const [inode, pids] of holders) {
|
|
1611
|
+
const [first = 0] = pids;
|
|
1612
|
+
if (pids.length > 1) {
|
|
1613
|
+
for (const pid of pids) {
|
|
1614
|
+
if (!parents.has(pid))
|
|
1615
|
+
parents.set(pid, parseStat(await text2(`${root}/${pid}/stat`) ?? "")?.ppid);
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
owners.set(inode, pids.find((pid) => !pids.includes(parents.get(pid) ?? -1)) ?? first);
|
|
1570
1619
|
}
|
|
1571
1620
|
return owners;
|
|
1572
1621
|
}
|
|
@@ -1840,7 +1889,7 @@ function summary(group) {
|
|
|
1840
1889
|
};
|
|
1841
1890
|
}
|
|
1842
1891
|
function render(groups) {
|
|
1843
|
-
const colors = paint(
|
|
1892
|
+
const colors = paint(process17.stdout);
|
|
1844
1893
|
const rows = groups.map((group) => [
|
|
1845
1894
|
group.origin,
|
|
1846
1895
|
String(group.members.length),
|
|
@@ -1868,9 +1917,9 @@ async function procs(args) {
|
|
|
1868
1917
|
if (values.watch !== undefined)
|
|
1869
1918
|
return watching(values.watch, async () => procs(withoutWatch(args)));
|
|
1870
1919
|
const olderMs = values.older === undefined ? undefined : duration(values.older, "older", 0, "m");
|
|
1871
|
-
const uid =
|
|
1920
|
+
const uid = process17.getuid?.() ?? -1;
|
|
1872
1921
|
const all = await snapshot("/proc", (owner) => owner === uid);
|
|
1873
|
-
const mine = new Set(ancestry(
|
|
1922
|
+
const mine = new Set(ancestry(process17.pid, new Map(all.map((proc) => [proc.pid, proc]))));
|
|
1874
1923
|
const { groups, protectedCount } = selectGroups(all, { guard: { uid, mine }, now: Date.now(), home: homedir4(), exists: existsSync3, minMem, olderMs });
|
|
1875
1924
|
const pids = groups.flatMap((group) => group.members.map(({ proc }) => proc.pid));
|
|
1876
1925
|
const show = (extra = {}) => {
|
|
@@ -1883,7 +1932,7 @@ async function procs(args) {
|
|
|
1883
1932
|
if (values.kill !== true || pids.length === 0 || values.yes !== true) {
|
|
1884
1933
|
show();
|
|
1885
1934
|
if (values.json !== true && protectedCount > 0)
|
|
1886
|
-
console.error(paint(
|
|
1935
|
+
console.error(paint(process17.stderr).dim(`${protectedCount} more left out: shells, terminals and this command's ancestors are never touched`));
|
|
1887
1936
|
if (values.kill === true && pids.length > 0)
|
|
1888
1937
|
console.error(`dry run: would stop ${pids.length} ${pids.length === 1 ? "process" : "processes"} (${pids.join(" ")}), pass --yes to do it`);
|
|
1889
1938
|
return 0;
|
|
@@ -1899,4 +1948,4 @@ async function procs(args) {
|
|
|
1899
1948
|
}
|
|
1900
1949
|
|
|
1901
1950
|
// src/procs-cli.ts
|
|
1902
|
-
|
|
1951
|
+
process18.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.4",
|
|
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.39.0"
|
|
39
39
|
}
|
|
40
40
|
}
|
package/src/read.ts
CHANGED
|
@@ -143,13 +143,29 @@ export async function socketsOf(pid: number, root = '/proc'): Promise<Set<number
|
|
|
143
143
|
return inodes
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
-
// Which pid holds each socket inode, for the processes whose fd directory can be read.
|
|
146
|
+
// Which pid holds each socket inode, for the processes whose fd directory can be read. Forked workers share the listen socket of their master: the holder whose parent holds it too is passed over.
|
|
147
147
|
export async function socketOwners(root = '/proc'): Promise<Map<number, number>> {
|
|
148
|
-
const
|
|
148
|
+
const holders = new Map<number, number[]>()
|
|
149
149
|
|
|
150
150
|
for (const pid of await listPids(root)) {
|
|
151
151
|
for (const inode of await socketsOf(pid, root))
|
|
152
|
-
|
|
152
|
+
holders.set(inode, [...holders.get(inode) ?? [], pid])
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const owners = new Map<number, number>()
|
|
156
|
+
const parents = new Map<number, number | undefined>()
|
|
157
|
+
|
|
158
|
+
for (const [inode, pids] of holders) {
|
|
159
|
+
const [first = 0] = pids
|
|
160
|
+
|
|
161
|
+
if (pids.length > 1) {
|
|
162
|
+
for (const pid of pids) {
|
|
163
|
+
if (!parents.has(pid))
|
|
164
|
+
parents.set(pid, parseStat(await text(`${root}/${pid}/stat`) ?? '')?.ppid)
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
owners.set(inode, pids.find(pid => !pids.includes(parents.get(pid) ?? -1)) ?? first)
|
|
153
169
|
}
|
|
154
170
|
|
|
155
171
|
return owners
|