@kaynooo/cli-proc 0.7.0 → 0.7.1

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kaynooo/cli-proc",
3
3
  "type": "module",
4
- "version": "0.7.0",
4
+ "version": "0.7.1",
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.36.0"
38
+ "@kaynooo/cli-kit": "0.37.0"
39
39
  }
40
40
  }
package/src/ports.ts CHANGED
@@ -2,7 +2,7 @@ import type { Container, Protocol, Socket } from './parse'
2
2
  import type { ProcInfo } from './read'
3
3
  import { basename } from 'node:path'
4
4
  import process from 'node:process'
5
- import { badUsage, duration, fail, jsonOption, paint, parse, printJson, table, truncate, watching, watchOption, wholeNumber, withoutWatch } from '@kaynooo/cli-kit'
5
+ import { badUsage, duration, fail, jsonOption, paint, parse, printJson, problem, table, truncate, watching, watchOption, wholeNumber, withoutWatch } from '@kaynooo/cli-kit'
6
6
  import { confirm } from '@kaynooo/cli-kit/prompt'
7
7
  import { dockerContainers } from './docker'
8
8
  import { protectionOf, terminate } from './kill'
@@ -23,6 +23,7 @@ const options = {
23
23
  watch: watchOption('the ports'),
24
24
  force: { type: 'boolean', description: 'with --kill, SIGKILL what SIGTERM did not stop within --grace' },
25
25
  yes: { type: 'boolean', short: 'y', description: 'with --kill, do not ask first' },
26
+ quiet: { type: 'boolean', short: 'q', description: 'with --kill, print nothing about what was stopped' },
26
27
  grace: { type: 'string', description: 'with --force, how long SIGTERM gets before SIGKILL (5s)' },
27
28
  } as const
28
29
 
@@ -103,7 +104,7 @@ function render(rows: PortRow[]): string {
103
104
  return table([['PORT', 'PROTO', 'ADDRESS', 'PID', 'COMMAND', 'CWD'], ...cells], { header: true })
104
105
  }
105
106
 
106
- async function stop(port: number, rows: PortRow[], procs: Map<number, ProcInfo>, values: { force?: boolean | undefined, yes?: boolean | undefined, grace?: string | undefined }): Promise<number> {
107
+ async function stop(port: number, rows: PortRow[], procs: Map<number, ProcInfo>, values: { force?: boolean | undefined, yes?: boolean | undefined, quiet?: boolean | undefined, grace?: string | undefined }): Promise<number> {
107
108
  const guard = { uid: process.getuid?.() ?? -1, mine: new Set(ancestry(process.pid, procs)) }
108
109
  const targets = new Map<number, PortRow>()
109
110
 
@@ -111,7 +112,7 @@ async function stop(port: number, rows: PortRow[], procs: Map<number, ProcInfo>,
111
112
  const reason = refusal(row, row.pid === undefined ? undefined : procs.get(row.pid), guard)
112
113
 
113
114
  if (reason !== undefined)
114
- console.error(`port ${port}: ${reason}`)
115
+ problem(`port ${port}: ${reason}`)
115
116
  else if (row.pid !== undefined)
116
117
  targets.set(row.pid, row)
117
118
  }
@@ -131,11 +132,11 @@ async function stop(port: number, rows: PortRow[], procs: Map<number, ProcInfo>,
131
132
 
132
133
  const outcome = await terminate([...targets.keys()], { escalate: values.force === true, graceMs: duration(values.grace, 'grace', 5000) })
133
134
 
134
- for (const pid of [...outcome.terminated, ...outcome.killed])
135
+ for (const pid of values.quiet === true ? [] : [...outcome.terminated, ...outcome.killed])
135
136
  console.log(`stopped ${pid}${outcome.killed.includes(pid) ? ' (SIGKILL)' : ''}`)
136
137
 
137
138
  for (const pid of outcome.survived)
138
- console.error(`${pid} is still running${values.force === true ? '' : ', pass --force to SIGKILL it'}`)
139
+ problem(`${pid} is still running${values.force === true ? '' : ', pass --force to SIGKILL it'}`)
139
140
 
140
141
  return outcome.survived.length === 0 ? 0 : 1
141
142
  }
@@ -175,7 +176,7 @@ export async function ports(args: string[]): Promise<number> {
175
176
  const rows = buildRows(wanted, owners, byPid, needsDocker ? await dockerContainers() : [])
176
177
 
177
178
  if (port !== undefined && rows.length === 0) {
178
- console.error(`nothing listens on port ${port}`)
179
+ problem(`nothing listens on port ${port}`)
179
180
 
180
181
  return 1
181
182
  }
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, watching, watchOption, withoutWatch } from '@kaynooo/cli-kit'
5
+ import { duration, fail, formatFileSize, formatSeconds, jsonOption, paint, parse, parseSize, printJson, problem, 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'
@@ -21,6 +21,7 @@ const options = {
21
21
  'kill': { type: 'boolean', conflicts: ['watch'], description: 'stop the listed processes: a dry run without --yes' },
22
22
  'watch': watchOption('the processes'),
23
23
  'yes': { type: 'boolean', short: 'y', description: 'with --kill, really stop them, without asking' },
24
+ 'quiet': { type: 'boolean', short: 'q', description: 'with --kill --yes, print nothing about what was stopped' },
24
25
  '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' },
25
26
  'older': { type: 'string', description: 'only processes at least this old: 30m, 2h, 1d' },
26
27
  'grace': { type: 'string', description: 'how long SIGTERM gets before SIGKILL (5s)' },
@@ -107,13 +108,14 @@ export async function procs(args: string[]): Promise<number> {
107
108
 
108
109
  const outcome = await terminate(pids, { escalate: true, graceMs: duration(values.grace, 'grace', 5000) })
109
110
 
110
- show({ stopped: [...outcome.terminated, ...outcome.killed], killed: outcome.killed, survived: outcome.survived })
111
+ if (values.quiet !== true || values.json === true)
112
+ show({ stopped: [...outcome.terminated, ...outcome.killed], killed: outcome.killed, survived: outcome.survived })
111
113
 
112
- if (values.json !== true)
114
+ if (values.json !== true && values.quiet !== true)
113
115
  console.log(`stopped ${outcome.terminated.length + outcome.killed.length} of ${pids.length}${outcome.killed.length === 0 ? '' : `, ${outcome.killed.length} needed SIGKILL`}`)
114
116
 
115
117
  if (outcome.survived.length > 0)
116
- console.error(`still running: ${outcome.survived.join(' ')}`)
118
+ problem(`still running: ${outcome.survived.join(' ')}`)
117
119
 
118
120
  return outcome.survived.length === 0 ? 0 : 1
119
121
  }