@kaynooo/cli-proc 0.7.1 → 0.7.3

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 CHANGED
@@ -2,7 +2,7 @@
2
2
  // @bun
3
3
 
4
4
  // src/pid-cli.ts
5
- import process16 from "process";
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 = [], replaces, exits = [] } = {}) {
460
- return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
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
- const width = Math.max(...exits.map(([code]) => String(code).length));
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 process10 from "process";
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
- process10.kill(-proc.pid, killing);
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 readFileSync2, realpathSync } from "fs";
882
+ import { readFileSync as readFileSync3, realpathSync as realpathSync2 } from "fs";
863
883
  import { dirname as dirname3, join as join5 } from "path";
864
- import process12 from "process";
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 process11 from "process";
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 = process11.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 = process11.env) {
908
+ function usagePath(env = process12.env) {
889
909
  return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
890
910
  }
891
- function usageRecorded(env = process11.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 = process11.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(process12.stderr);
997
- const styled = takesControlCodes(process12.stderr);
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]), process12.stderr));
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]), process12.stdout));
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
- let index = 0;
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
- index += before.length - at - 1;
1124
+ typed.push(...before.slice(at + 1));
1106
1125
  break;
1107
1126
  }
1108
1127
  if (!word.startsWith("-"))
1109
- index++;
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 (index < positionals.length)
1116
- return positionals[index];
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 name = positionalBeingTyped(words.slice(0, -1), completing);
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
- for (let dir = dirname3(realpathSync(entry));; dir = dirname3(dir)) {
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(readFileSync2(join5(dir, "package.json"), "utf8"));
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 = process12.argv.slice(2)) {
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 = process12.argv.slice(2)) {
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 process13 from "process";
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 = process13.stdout, sleep = Bun.sleep, rounds = Number.POSITIVE_INFINITY) {
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 = process13.stdout.write.bind(process13.stdout);
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
- process13.stdout.write = (text) => {
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
- process13.stdout.write = write;
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 process15 from "process";
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(process15.stdout);
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: [
@@ -1734,4 +1757,4 @@ async function pid(args) {
1734
1757
  }
1735
1758
 
1736
1759
  // src/pid-cli.ts
1737
- process16.exitCode = await runMain({ name: "pid", run: pid });
1760
+ process17.exitCode = await runMain({ name: "pid", run: pid });