Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/main/ts/ps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { exec, type TSpawnCtx } from 'zurk/spawn'

const IS_WIN = process.platform === 'win32'
const IS_WIN2025_PLUS = IS_WIN && Number.parseInt(os.release().split('.')[2], 10) >= 26_000 // WMIC will be missing in Windows 11 25H2 (kernel >= 26000)
const IS_BUSYBOX = !IS_WIN && fs.existsSync('/bin/busybox')
const LOOKUPS: Record<string, {
cmd: string,
args?: string[],
parse: (stdout: string) => TIngridResponse
fallback?: string
}> = {
wmic: {
cmd: 'wmic process get ProcessId,ParentProcessId,CommandLine',
Expand All @@ -21,6 +23,14 @@ const LOOKUPS: Record<string, {
ps: {
cmd: 'ps',
args: ['-lx'],
parse(stdout: string) {
return parse(stdout, { format: 'unix' })
},
fallback: 'psFallback',
},
psFallback: {
cmd: 'ps',
args: ['-eo', 'pid,ppid,args'],
parse(stdout: string) {
return parse(stdout, { format: 'unix' })
}
Expand Down Expand Up @@ -125,19 +135,41 @@ const _lookup = ({
const pFactory = sync ? makePseudoDeferred.bind(null, []) : makeDeferred
const { promise, resolve, reject } = pFactory()
const result: TPsLookupEntry[] = []
const lookupFlow = IS_WIN ? IS_WIN2025_PLUS ? 'pwsh' : 'wmic' : 'ps'
const lookupFlow = IS_WIN ? IS_WIN2025_PLUS ? 'pwsh' : 'wmic' : IS_BUSYBOX ? 'psFallback' : 'ps'
const {
parse,
cmd,
args
} = LOOKUPS[lookupFlow]
const lookup = LOOKUPS[lookupFlow]
const callback: TSpawnCtx['callback'] = (err, {stdout}) => {
if (err) {
reject(err)
cb(err)
return
}
result.push(...filterProcessList(normalizeOutput(parse(stdout)), query))
const parsed = filterProcessList(normalizeOutput(parse(stdout)), query)
if (parsed.length === 0 && lookup.fallback) {
const fb = LOOKUPS[lookup.fallback]
exec({
cmd: fb.cmd,
args: fb.args,
callback: (err2, {stdout: stdout2}) => {
if (err2) {
resolve(result)
cb(null, result)
return
}
result.push(...filterProcessList(normalizeOutput(fb.parse(stdout2)), query))
resolve(result)
cb(null, result)
},
sync,
run(cb) { cb() },
})
return
}
result.push(...parsed)
resolve(result)
cb(null, result)
}
Expand Down