@kaynooo/cli-proc 0.2.1 → 0.4.0

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.
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env -S bun --no-env-file
2
+
3
+ import process from 'node:process'
4
+ import { runCli } from '@kaynooo/cli-kit'
5
+ import { pid } from './pid'
6
+ import { ports } from './ports'
7
+ import { procs } from './procs'
8
+
9
+ process.exitCode = await runCli({
10
+ name: 'proc',
11
+ about: 'what a process is, who listens on a port and which dev processes are worth cleaning, read from /proc',
12
+ replaces: 'cat /proc/<pid>/..., ss -ltnp, lsof -i and pgrep -f one-liners',
13
+ examples: ['proc pid 1234', 'proc ports', ['proc procs', 'the leftover or heavy processes, dry-run first']],
14
+ commands: {
15
+ pid: { summary: 'what a process is: executable, parents, memory, limits and listening ports', run: pid },
16
+ ports: { summary: 'what listens on this machine, with its owner, and a safe --kill', run: ports },
17
+ procs: { summary: 'the leftover or heavy processes of the calling user, grouped by origin', run: procs },
18
+ },
19
+ })
package/src/procs.ts CHANGED
@@ -2,7 +2,7 @@ import type { Group } from './select'
2
2
  import { existsSync } from 'node:fs'
3
3
  import { homedir } from 'node:os'
4
4
  import process from 'node:process'
5
- import { duration, fail, formatFileSize, formatSeconds, jsonOption, paint, parse, parseSize, printJson, table, truncate } from '@kaynooo/cli-kit'
5
+ import { duration, fail, formatFileSize, formatSeconds, jsonOption, paint, parse, parseSize, printJson, table, truncate, watching, watchOption, withoutWatch } from '@kaynooo/cli-kit'
6
6
  import { terminate } from './kill'
7
7
  import { maskArgs } from './mask'
8
8
  import { ancestry, snapshot } from './read'
@@ -18,7 +18,8 @@ Exits 0 when it listed or stopped everything, 1 when a process survived SIGKILL,
18
18
 
19
19
  const options = {
20
20
  'json': jsonOption('{ groups: [{ origin, kind, count, rss, oldestSeconds, tools, orphans, pids, processes }], skipped }'),
21
- 'kill': { type: 'boolean', description: 'stop the listed processes: a dry run without --yes' },
21
+ 'kill': { type: 'boolean', conflicts: ['watch'], description: 'stop the listed processes: a dry run without --yes' },
22
+ 'watch': watchOption('the processes'),
22
23
  'yes': { type: 'boolean', short: 'y', description: 'with --kill, really stop them, without asking' },
23
24
  'min-mem': { type: 'string', description: 'heavy from this resident size (500M), and the least a left over process must use to be listed; a bare number counts megabytes' },
24
25
  'older': { type: 'string', description: 'only processes at least this old: 30m, 2h, 1d' },
@@ -73,6 +74,9 @@ export async function procs(args: string[]): Promise<number> {
73
74
  if (values['min-mem'] !== undefined && minMem === undefined)
74
75
  fail(`--min-mem takes a size such as 500M or 1.5G, not ${values['min-mem']}`, 2)
75
76
 
77
+ if (values.watch !== undefined)
78
+ return watching(values.watch, async () => procs(withoutWatch(args)))
79
+
76
80
  const olderMs = values.older === undefined ? undefined : duration(values.older, 'older', 0, 'm')
77
81
  const uid = process.getuid?.() ?? -1
78
82
  const all = await snapshot('/proc', owner => owner === uid)