@kaynooo/cli-proc 0.7.2 → 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 +149 -42
- package/dist/proc-cli.js +156 -49
- package/dist/procs-cli.js +83 -34
- package/package.json +2 -2
- package/src/read.ts +19 -3
package/dist/ports-cli.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/ports-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";
|
|
@@ -365,6 +365,9 @@ function formatSeconds(total) {
|
|
|
365
365
|
const next = amounts[first + 1];
|
|
366
366
|
return next === undefined || next.startsWith("0") ? amounts[first] ?? "0s" : `${amounts[first]} ${next}`;
|
|
367
367
|
}
|
|
368
|
+
function formatDuration(ms) {
|
|
369
|
+
return ms < 60000 ? `${(Math.max(0, ms) / 1000).toFixed(1)}s` : formatSeconds(ms / 1000);
|
|
370
|
+
}
|
|
368
371
|
function upTo(text, separator) {
|
|
369
372
|
const end = text.indexOf(separator);
|
|
370
373
|
return end === -1 ? text : text.slice(0, end);
|
|
@@ -425,7 +428,7 @@ function printJson(value) {
|
|
|
425
428
|
|
|
426
429
|
// ../kit/src/help.ts
|
|
427
430
|
import process8 from "process";
|
|
428
|
-
var OPTIONAL =
|
|
431
|
+
var OPTIONAL = /(?<!\x1B)\[[^\]]+\]/g;
|
|
429
432
|
var USAGE_PREFIX = /^usage:\s*/i;
|
|
430
433
|
var FULL_STOP = /\.$/;
|
|
431
434
|
function usageLine(usage, colors = paint(process8.stdout)) {
|
|
@@ -464,29 +467,40 @@ function exampleRows(examples, colors = paint(process8.stdout)) {
|
|
|
464
467
|
return [];
|
|
465
468
|
return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
|
|
466
469
|
}
|
|
470
|
+
var MAX_INLINE_CHOICES = 24;
|
|
467
471
|
function optionRows(options, colors = paint(process8.stdout)) {
|
|
468
472
|
const rows = Object.entries(options).map(([name, option]) => {
|
|
469
473
|
const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
|
|
470
|
-
const
|
|
474
|
+
const choices = option.choices?.join("|");
|
|
475
|
+
const inline = choices !== undefined && choices.length <= MAX_INLINE_CHOICES;
|
|
476
|
+
const named = inline ? `<${choices}>` : "<value>";
|
|
471
477
|
const placeholder = option.optional === undefined ? named : `[${named}]`;
|
|
472
478
|
const value = option.type === "string" ? ` ${colors.dim(placeholder)}` : "";
|
|
473
|
-
|
|
479
|
+
const described = choices === undefined || inline ? option.description ?? "" : [option.description, `one of ${option.choices.join(", ")}`].filter(Boolean).join("; ");
|
|
480
|
+
return { flag: `${short}${colors.cyan(`--${name}`)}${value}`, width: `-x, --${name}`.length + (option.type === "string" ? placeholder.length + 1 : 0), description: described };
|
|
474
481
|
});
|
|
475
482
|
const width = Math.max(0, ...rows.map((row) => row.width));
|
|
476
483
|
return rows.map((row) => row.description === "" ? ` ${row.flag}` : ` ${row.flag}${" ".repeat(width - row.width)} ${colors.dim(row.description)}`);
|
|
477
484
|
}
|
|
478
485
|
|
|
479
486
|
// ../kit/src/text.ts
|
|
480
|
-
var ANSI = new RegExp(`${String.fromCharCode(27)}\\[[\\d;?]*[a-z]`, "gi");
|
|
481
487
|
function distance(a, b) {
|
|
482
|
-
|
|
483
|
-
|
|
488
|
+
const left = [...a];
|
|
489
|
+
const right = [...b];
|
|
490
|
+
let before = [];
|
|
491
|
+
let previous = Array.from({ length: right.length + 1 }, (_, index) => index);
|
|
492
|
+
for (const [i, x] of left.entries()) {
|
|
484
493
|
const current = [i + 1];
|
|
485
|
-
for (const [j,
|
|
486
|
-
|
|
494
|
+
for (const [j, y] of right.entries()) {
|
|
495
|
+
let cost = Math.min((previous[j + 1] ?? 0) + 1, (current[j] ?? 0) + 1, (previous[j] ?? 0) + (x === y ? 0 : 1));
|
|
496
|
+
if (i > 0 && j > 0 && x === right[j - 1] && left[i - 1] === y)
|
|
497
|
+
cost = Math.min(cost, (before[j - 1] ?? 0) + 1);
|
|
498
|
+
current.push(cost);
|
|
499
|
+
}
|
|
500
|
+
before = previous;
|
|
487
501
|
previous = current;
|
|
488
502
|
}
|
|
489
|
-
return previous[
|
|
503
|
+
return previous[right.length] ?? 0;
|
|
490
504
|
}
|
|
491
505
|
function closest(name, names) {
|
|
492
506
|
const started = names.find((known) => known.startsWith(name));
|
|
@@ -856,13 +870,31 @@ var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
|
856
870
|
// ../kit/src/cli.ts
|
|
857
871
|
import { readFileSync as readFileSync3, realpathSync as realpathSync2 } from "fs";
|
|
858
872
|
import { dirname as dirname3, join as join5 } from "path";
|
|
859
|
-
import
|
|
873
|
+
import process14 from "process";
|
|
874
|
+
|
|
875
|
+
// ../kit/src/notify.ts
|
|
876
|
+
import process12 from "process";
|
|
877
|
+
var NOTIFY_AFTER_MS = 1e4;
|
|
878
|
+
var CANCELLED = 130;
|
|
879
|
+
function base64(text) {
|
|
880
|
+
return Buffer.from(text).toString("base64");
|
|
881
|
+
}
|
|
882
|
+
function notification(title, body, env = process12.env) {
|
|
883
|
+
const id = `kit${process12.pid}`;
|
|
884
|
+
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\\`];
|
|
885
|
+
return codes.map((code) => env.TMUX === undefined || env.TMUX === "" ? code : `\x1BPtmux;${code.replaceAll("\x1B", "\x1B\x1B")}\x1B\\`).join("");
|
|
886
|
+
}
|
|
887
|
+
function notifyEnd(command, code, ms, { stream = process12.stderr, env = process12.env } = {}) {
|
|
888
|
+
if (ms < NOTIFY_AFTER_MS || code === CANCELLED || outputMode(stream, env) !== "terminal")
|
|
889
|
+
return;
|
|
890
|
+
stream.write(notification(command, code === 0 ? `done in ${formatDuration(ms)}` : `failed with exit ${code} after ${formatDuration(ms)}`, env));
|
|
891
|
+
}
|
|
860
892
|
|
|
861
893
|
// ../kit/src/usage.ts
|
|
862
894
|
import { appendFileSync, mkdirSync } from "fs";
|
|
863
895
|
import { homedir as homedir3 } from "os";
|
|
864
896
|
import { dirname as dirname2, isAbsolute, join as join4 } from "path";
|
|
865
|
-
import
|
|
897
|
+
import process13 from "process";
|
|
866
898
|
|
|
867
899
|
// ../kit/src/logs.ts
|
|
868
900
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -876,14 +908,14 @@ var usageError;
|
|
|
876
908
|
function noteUsageError(message) {
|
|
877
909
|
usageError = message;
|
|
878
910
|
}
|
|
879
|
-
function stateHome(env =
|
|
911
|
+
function stateHome(env = process13.env) {
|
|
880
912
|
const dir = env.XDG_STATE_HOME;
|
|
881
913
|
return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
|
|
882
914
|
}
|
|
883
|
-
function usagePath(env =
|
|
915
|
+
function usagePath(env = process13.env) {
|
|
884
916
|
return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
885
917
|
}
|
|
886
|
-
function usageRecorded(env =
|
|
918
|
+
function usageRecorded(env = process13.env) {
|
|
887
919
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
888
920
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
889
921
|
}
|
|
@@ -897,7 +929,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
897
929
|
usageError = undefined;
|
|
898
930
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
899
931
|
}
|
|
900
|
-
function recordUsage(entry, env =
|
|
932
|
+
function recordUsage(entry, env = process13.env) {
|
|
901
933
|
if (!usageRecorded(env))
|
|
902
934
|
return;
|
|
903
935
|
try {
|
|
@@ -987,8 +1019,8 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
|
|
|
987
1019
|
`);
|
|
988
1020
|
}
|
|
989
1021
|
function printError(prefix, message, hint, cause) {
|
|
990
|
-
const colors = paint(
|
|
991
|
-
const styled = takesControlCodes(
|
|
1022
|
+
const colors = paint(process14.stderr);
|
|
1023
|
+
const styled = takesControlCodes(process14.stderr);
|
|
992
1024
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
993
1025
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
994
1026
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -1031,11 +1063,11 @@ async function dispatch(level, typed, { version, redact }) {
|
|
|
1031
1063
|
}
|
|
1032
1064
|
}
|
|
1033
1065
|
if (name === undefined && level.path.length > 1) {
|
|
1034
|
-
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
1066
|
+
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process14.stderr));
|
|
1035
1067
|
return 2;
|
|
1036
1068
|
}
|
|
1037
1069
|
if (name === undefined || name === "help" || name === "--help" || name === "-h") {
|
|
1038
|
-
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
1070
|
+
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process14.stdout));
|
|
1039
1071
|
return 0;
|
|
1040
1072
|
}
|
|
1041
1073
|
if (level.path.length === 1 && (name === "--version" || name === "-v")) {
|
|
@@ -1170,10 +1202,13 @@ async function runCli(cli, argv = scriptArgs()) {
|
|
|
1170
1202
|
const version = cli.version ?? packageVersion();
|
|
1171
1203
|
if (argv[0] === COMPLETE)
|
|
1172
1204
|
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
1205
|
+
const started = performance.now();
|
|
1173
1206
|
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 });
|
|
1207
|
+
const command = commandPath(scopeOf(cli.commands, cli.extraCommands, cli.aliases), argv);
|
|
1208
|
+
notifyEnd(command === undefined ? cli.name : `${cli.name} ${command}`, code, performance.now() - started);
|
|
1174
1209
|
if (cli.usage !== false) {
|
|
1175
1210
|
try {
|
|
1176
|
-
recordUsage(usageEntry(cli.name,
|
|
1211
|
+
recordUsage(usageEntry(cli.name, command, argv, code));
|
|
1177
1212
|
} catch {}
|
|
1178
1213
|
}
|
|
1179
1214
|
return code;
|
|
@@ -1201,11 +1236,13 @@ async function runMain({ name, version, run, redact, usage }, argv = scriptArgs(
|
|
|
1201
1236
|
return 0;
|
|
1202
1237
|
}
|
|
1203
1238
|
let code;
|
|
1239
|
+
const started = performance.now();
|
|
1204
1240
|
try {
|
|
1205
1241
|
code = await run(argv);
|
|
1206
1242
|
} catch (error) {
|
|
1207
1243
|
code = reportThrown(name, error, redact, wantsJson(argv));
|
|
1208
1244
|
}
|
|
1245
|
+
notifyEnd(name, code, performance.now() - started);
|
|
1209
1246
|
if (usage !== false) {
|
|
1210
1247
|
try {
|
|
1211
1248
|
recordUsage(usageEntry(name, undefined, argv, code));
|
|
@@ -1237,7 +1274,7 @@ function parseSize(text, bareUnit = 1) {
|
|
|
1237
1274
|
return Math.round(Number(match[1]) * (suffix === "" ? bareUnit : 1024 ** POWERS[suffix]));
|
|
1238
1275
|
}
|
|
1239
1276
|
// ../kit/src/watch.ts
|
|
1240
|
-
import
|
|
1277
|
+
import process15 from "process";
|
|
1241
1278
|
import { format } from "util";
|
|
1242
1279
|
var CLEAR_SCREEN = "\x1B[H\x1B[2J";
|
|
1243
1280
|
var DEFAULT_SECONDS = 2;
|
|
@@ -1259,7 +1296,7 @@ function withoutWatch(args) {
|
|
|
1259
1296
|
}
|
|
1260
1297
|
return rest;
|
|
1261
1298
|
}
|
|
1262
|
-
async function watching(value, step, stream =
|
|
1299
|
+
async function watching(value, step, stream = process15.stdout, sleep = Bun.sleep, rounds = Number.POSITIVE_INFINITY) {
|
|
1263
1300
|
const interval = value === "" ? DEFAULT_SECONDS * 1000 : duration(value, "watch", DEFAULT_SECONDS * 1000);
|
|
1264
1301
|
if (!takesControlCodes(stream))
|
|
1265
1302
|
badUsage("--watch redraws the screen, so it needs a terminal");
|
|
@@ -1267,12 +1304,12 @@ async function watching(value, step, stream = process14.stdout, sleep = Bun.slee
|
|
|
1267
1304
|
for (let round = 0;round < rounds; round++) {
|
|
1268
1305
|
const held = [];
|
|
1269
1306
|
const { log, info } = console;
|
|
1270
|
-
const write =
|
|
1307
|
+
const write = process15.stdout.write.bind(process15.stdout);
|
|
1271
1308
|
const hold = (...parts) => void held.push(`${format(...parts)}
|
|
1272
1309
|
`);
|
|
1273
1310
|
console.log = hold;
|
|
1274
1311
|
console.info = hold;
|
|
1275
|
-
|
|
1312
|
+
process15.stdout.write = (text) => {
|
|
1276
1313
|
held.push(typeof text === "string" ? text : Buffer.from(text).toString());
|
|
1277
1314
|
return true;
|
|
1278
1315
|
};
|
|
@@ -1281,7 +1318,7 @@ async function watching(value, step, stream = process14.stdout, sleep = Bun.slee
|
|
|
1281
1318
|
} finally {
|
|
1282
1319
|
console.log = log;
|
|
1283
1320
|
console.info = info;
|
|
1284
|
-
|
|
1321
|
+
process15.stdout.write = write;
|
|
1285
1322
|
}
|
|
1286
1323
|
stream.write(`${CLEAR_SCREEN}${held.join("")}`);
|
|
1287
1324
|
await sleep(interval);
|
|
@@ -1290,10 +1327,24 @@ async function watching(value, step, stream = process14.stdout, sleep = Bun.slee
|
|
|
1290
1327
|
}
|
|
1291
1328
|
// src/ports.ts
|
|
1292
1329
|
import { basename as basename2 } from "path";
|
|
1293
|
-
import
|
|
1330
|
+
import process19 from "process";
|
|
1294
1331
|
|
|
1295
1332
|
// ../kit/src/prompt.ts
|
|
1296
|
-
import
|
|
1333
|
+
import process18 from "process";
|
|
1334
|
+
|
|
1335
|
+
// ../kit/src/job.ts
|
|
1336
|
+
import process17 from "process";
|
|
1337
|
+
function stopJob() {
|
|
1338
|
+
process17.kill(0, "SIGTSTP");
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
// ../kit/src/picker/line.ts
|
|
1342
|
+
var EDIT_KEYS = new Set(["left", "right", "wordleft", "wordright", "linestart", "lineend", "erase", "delete", "word", "killword", "killnext", "clear", "killend", "yank"]);
|
|
1343
|
+
var BACKWARD_KILLS = new Set(["word", "killword", "clear"]);
|
|
1344
|
+
var FORWARD_KILLS = new Set(["killnext", "killend"]);
|
|
1345
|
+
function isEditKey(key) {
|
|
1346
|
+
return EDIT_KEYS.has(key);
|
|
1347
|
+
}
|
|
1297
1348
|
|
|
1298
1349
|
// ../kit/src/picker/score.ts
|
|
1299
1350
|
var BOUNDARY_AFTER = new Set([" ", "/", "-", "_", ".", ":", "\\", "(", "[", ",", "\t"]);
|
|
@@ -1325,10 +1376,29 @@ var KEYS = {
|
|
|
1325
1376
|
"\n": "enter",
|
|
1326
1377
|
"\x03": "cancel",
|
|
1327
1378
|
"\x1B": "cancel",
|
|
1379
|
+
"\x1B[D": "left",
|
|
1380
|
+
"\x1BOD": "left",
|
|
1381
|
+
"\x1B[C": "right",
|
|
1382
|
+
"\x1BOC": "right",
|
|
1383
|
+
"\x1B[1;5D": "wordleft",
|
|
1384
|
+
"\x1B[1;3D": "wordleft",
|
|
1385
|
+
"\x1Bb": "wordleft",
|
|
1386
|
+
"\x1B[1;5C": "wordright",
|
|
1387
|
+
"\x1B[1;3C": "wordright",
|
|
1388
|
+
"\x1Bf": "wordright",
|
|
1389
|
+
"\x01": "linestart",
|
|
1390
|
+
"\x05": "lineend",
|
|
1328
1391
|
"\x7F": "erase",
|
|
1329
|
-
"\
|
|
1392
|
+
"\x1B[3~": "delete",
|
|
1393
|
+
"\b": "killword",
|
|
1394
|
+
"\x1B\x7F": "killword",
|
|
1395
|
+
"\x1B[3;5~": "killnext",
|
|
1396
|
+
"\x1Bd": "killnext",
|
|
1330
1397
|
"\x15": "clear",
|
|
1398
|
+
"\v": "killend",
|
|
1331
1399
|
"\x17": "word",
|
|
1400
|
+
"\x19": "yank",
|
|
1401
|
+
"\x1A": "suspend",
|
|
1332
1402
|
"\t": "tab",
|
|
1333
1403
|
"\x1F": "help",
|
|
1334
1404
|
"\x1BOP": "help",
|
|
@@ -1358,10 +1428,21 @@ function parseKeys(input, paste = { active: false }, ctrls = []) {
|
|
|
1358
1428
|
});
|
|
1359
1429
|
}
|
|
1360
1430
|
|
|
1431
|
+
// ../kit/src/redraw.ts
|
|
1432
|
+
var SYNC_START = "\x1B[?2026h";
|
|
1433
|
+
var SYNC_END = "\x1B[?2026l";
|
|
1434
|
+
function overwrite(lines, columns) {
|
|
1435
|
+
return lines.map((line) => textWidth(line) % columns === 0 ? `\x1B[2K${line}` : `${line}\x1B[K`).join(`
|
|
1436
|
+
`);
|
|
1437
|
+
}
|
|
1438
|
+
function rowsTaken(lines, columns) {
|
|
1439
|
+
return lines.reduce((rows, line) => rows + Math.max(1, Math.ceil(textWidth(line) / columns)), 0);
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1361
1442
|
// ../kit/src/prompt.ts
|
|
1362
|
-
var PROMPT_KEYS = new Set(["up", "down", "pageup", "pagedown", "home", "end", "enter", "cancel", "
|
|
1443
|
+
var PROMPT_KEYS = new Set(["up", "down", "pageup", "pagedown", "home", "end", "enter", "cancel", "suspend", "tab"]);
|
|
1363
1444
|
function readKeys(input, paste = { active: false }) {
|
|
1364
|
-
return parseKeys(input, paste).filter((key) => typeof key === "object" ? "text" in key : PROMPT_KEYS.has(key));
|
|
1445
|
+
return parseKeys(input, paste).filter((key) => typeof key === "object" ? "text" in key : PROMPT_KEYS.has(key) || isEditKey(key));
|
|
1365
1446
|
}
|
|
1366
1447
|
async function confirm(question, { default: fallback, ...settings } = {}) {
|
|
1367
1448
|
let answered = fallback;
|
|
@@ -1385,7 +1466,7 @@ function asking(colors, question, typed, keys) {
|
|
|
1385
1466
|
function answer(colors, question, state, picked) {
|
|
1386
1467
|
return state === "enter" ? `${colors.green("\u2713")} ${question} ${colors.cyan(picked)}` : `${colors.red("\u2717")} ${question} ${colors.dim("cancelled")}`;
|
|
1387
1468
|
}
|
|
1388
|
-
async function ask(question, { hint, terminal =
|
|
1469
|
+
async function ask(question, { hint, terminal = process18 }, render, press) {
|
|
1389
1470
|
const { stdin, stderr } = terminal;
|
|
1390
1471
|
if (!isInteractive(terminal))
|
|
1391
1472
|
fail(`cannot ask "${question}" off a terminal`, 2, hint);
|
|
@@ -1395,12 +1476,22 @@ async function ask(question, { hint, terminal = process16 }, render, press) {
|
|
|
1395
1476
|
const columns = () => stderr.columns || 80;
|
|
1396
1477
|
const draw = (state, last = false) => {
|
|
1397
1478
|
const frame = render(colors, state, { columns: columns(), rows: stderr.rows || 24 });
|
|
1398
|
-
|
|
1399
|
-
|
|
1479
|
+
const rows = rowsTaken(frame, columns());
|
|
1480
|
+
stderr.write(`${SYNC_START}\r${overwrite(frame, columns())}\x1B[0J${last || rows < 2 ? "" : `\x1B[${rows - 1}A`}${last ? "" : "\r"}${SYNC_END}`);
|
|
1400
1481
|
current = state;
|
|
1401
1482
|
};
|
|
1402
1483
|
const redraw = () => draw(current);
|
|
1403
1484
|
const paste = { active: false };
|
|
1485
|
+
const stop = terminal.stop ?? (terminal === process18 ? stopJob : undefined);
|
|
1486
|
+
const suspend = () => {
|
|
1487
|
+
if (stop === undefined)
|
|
1488
|
+
return;
|
|
1489
|
+
stderr.write("\x1B[0J\x1B[?25h\x1B[?2004l");
|
|
1490
|
+
stdin.setRawMode(false);
|
|
1491
|
+
stop();
|
|
1492
|
+
stdin.setRawMode(true);
|
|
1493
|
+
stderr.write("\x1B[?25l\x1B[?2004h");
|
|
1494
|
+
};
|
|
1404
1495
|
try {
|
|
1405
1496
|
stderr.on?.("resize", redraw);
|
|
1406
1497
|
stdin.setRawMode(true);
|
|
@@ -1410,6 +1501,10 @@ async function ask(question, { hint, terminal = process16 }, render, press) {
|
|
|
1410
1501
|
return await new Promise((resolve) => {
|
|
1411
1502
|
function onData(data) {
|
|
1412
1503
|
for (const key of readKeys(data.toString(), paste)) {
|
|
1504
|
+
if (key === "suspend") {
|
|
1505
|
+
suspend();
|
|
1506
|
+
continue;
|
|
1507
|
+
}
|
|
1413
1508
|
const pressed = press(key);
|
|
1414
1509
|
if (pressed !== undefined) {
|
|
1415
1510
|
settle(pressed);
|
|
@@ -1696,10 +1791,22 @@ async function socketsOf(pid, root = "/proc") {
|
|
|
1696
1791
|
return inodes;
|
|
1697
1792
|
}
|
|
1698
1793
|
async function socketOwners(root = "/proc") {
|
|
1699
|
-
const
|
|
1794
|
+
const holders = new Map;
|
|
1700
1795
|
for (const pid of await listPids(root)) {
|
|
1701
1796
|
for (const inode of await socketsOf(pid, root))
|
|
1702
|
-
|
|
1797
|
+
holders.set(inode, [...holders.get(inode) ?? [], pid]);
|
|
1798
|
+
}
|
|
1799
|
+
const owners = new Map;
|
|
1800
|
+
const parents = new Map;
|
|
1801
|
+
for (const [inode, pids] of holders) {
|
|
1802
|
+
const [first = 0] = pids;
|
|
1803
|
+
if (pids.length > 1) {
|
|
1804
|
+
for (const pid of pids) {
|
|
1805
|
+
if (!parents.has(pid))
|
|
1806
|
+
parents.set(pid, parseStat(await text2(`${root}/${pid}/stat`) ?? "")?.ppid);
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
owners.set(inode, pids.find((pid) => !pids.includes(parents.get(pid) ?? -1)) ?? first);
|
|
1703
1810
|
}
|
|
1704
1811
|
return owners;
|
|
1705
1812
|
}
|
|
@@ -1881,7 +1988,7 @@ function refusal(row, proc, guard) {
|
|
|
1881
1988
|
return reason === undefined ? undefined : `pid ${row.pid} is ${reason}`;
|
|
1882
1989
|
}
|
|
1883
1990
|
function render(rows) {
|
|
1884
|
-
const colors = paint(
|
|
1991
|
+
const colors = paint(process19.stdout);
|
|
1885
1992
|
const cells = rows.map((row) => [
|
|
1886
1993
|
colors.cyan(String(row.port)),
|
|
1887
1994
|
row.protocol,
|
|
@@ -1893,7 +2000,7 @@ function render(rows) {
|
|
|
1893
2000
|
return table([["PORT", "PROTO", "ADDRESS", "PID", "COMMAND", "CWD"], ...cells], { header: true });
|
|
1894
2001
|
}
|
|
1895
2002
|
async function stop(port, rows, procs, values) {
|
|
1896
|
-
const guard = { uid:
|
|
2003
|
+
const guard = { uid: process19.getuid?.() ?? -1, mine: new Set(ancestry(process19.pid, procs)) };
|
|
1897
2004
|
const targets = new Map;
|
|
1898
2005
|
for (const row of rows) {
|
|
1899
2006
|
const reason = refusal(row, row.pid === undefined ? undefined : procs.get(row.pid), guard);
|
|
@@ -1906,7 +2013,7 @@ async function stop(port, rows, procs, values) {
|
|
|
1906
2013
|
return 1;
|
|
1907
2014
|
const names = [...targets].map(([pid, row]) => `${pid} (${truncate(row.command ?? "", 40)})`).join(", ");
|
|
1908
2015
|
if (values.yes !== true) {
|
|
1909
|
-
if (!
|
|
2016
|
+
if (!process19.stdin.isTTY)
|
|
1910
2017
|
fail(`stopping ${names} needs a terminal to ask on: pass --yes`, 2);
|
|
1911
2018
|
if (await confirm(`Stop ${names}?`, { default: false }) !== true)
|
|
1912
2019
|
return 1;
|
|
@@ -1938,7 +2045,7 @@ async function ports(args) {
|
|
|
1938
2045
|
badUsage(`<port> takes a whole number from 1 to 65535, not ${given}`);
|
|
1939
2046
|
if (values.kill === true && port === undefined)
|
|
1940
2047
|
badUsage("--kill takes the port to free: ports <port> --kill");
|
|
1941
|
-
const [sockets, owners, procs] = await Promise.all([readSockets(), socketOwners(), snapshot("/proc", (uid) => uid ===
|
|
2048
|
+
const [sockets, owners, procs] = await Promise.all([readSockets(), socketOwners(), snapshot("/proc", (uid) => uid === process19.getuid?.())]);
|
|
1942
2049
|
const byPid = new Map(procs.map((proc) => [proc.pid, proc]));
|
|
1943
2050
|
const wanted = sockets.filter((socket) => port === undefined || socket.port === port);
|
|
1944
2051
|
const needsDocker = wanted.some((socket) => {
|
|
@@ -1959,9 +2066,9 @@ async function ports(args) {
|
|
|
1959
2066
|
console.log(render(rows));
|
|
1960
2067
|
const unknown = rows.filter((row) => row.pid === undefined && row.container === undefined).length;
|
|
1961
2068
|
if (unknown > 0)
|
|
1962
|
-
console.error(paint(
|
|
2069
|
+
console.error(paint(process19.stderr).dim(`${unknown} ${unknown === 1 ? "port has" : "ports have"} an owner this user cannot read, shown as -`));
|
|
1963
2070
|
return 0;
|
|
1964
2071
|
}
|
|
1965
2072
|
|
|
1966
2073
|
// src/ports-cli.ts
|
|
1967
|
-
|
|
2074
|
+
process20.exitCode = await runMain({ name: "ports", run: ports });
|