Skip to content

Commit

Permalink
feat: minor code impr, tech release
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jan 29, 2025
1 parent 1cea555 commit 90295f7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
29 changes: 15 additions & 14 deletions src/main/ts/ps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EOL as SystemEOL } from 'node:os'
import { parse, TIngridResponse } from '@webpod/ingrid'
import { exec, TSpawnCtx } from 'zurk/spawn'

const EOL = /(\r\n)|(\n\r)|\n|\r/
const EOL = /\n\r?|\r\n?/
const IS_WIN = process.platform === 'win32'
const isBin = (f: string): boolean => {
if (f === '') return false
Expand Down Expand Up @@ -108,9 +108,9 @@ const _lookup = ({
: {
cmd: 'ps',
args,
run(cb) {cb()},
sync,
callback,
sync,
run(cb) {cb()},
}

exec(ctx)
Expand All @@ -119,8 +119,6 @@ const _lookup = ({
}

export const parseProcessList = (output: string, query: TPsLookupQuery = {}) => {
type TFilterKeys = keyof Pick<TPsLookupQuery, 'command' | 'arguments'| 'ppid'>

const processList = parseGrid(output.trim())
const pidList= (query.pid === undefined ? [] : [query.pid].flat(1)).map(v => v + '')
const filters: Array<(p: TPsLookupEntry) => boolean> = [
Expand All @@ -137,14 +135,16 @@ export const parseProcessList = (output: string, query: TPsLookupQuery = {}) =>
export const extractWmic = (stdout: string): string => {
const _stdout = stdout.split(EOL)
// Find the line index for the titles
const beginRow = _stdout.findIndex(out => out?.indexOf('CommandLine') === 0)
// const beginRow = _stdout.findIndex(out => out?.indexOf('CommandLine') === 0)
const beginRow = _stdout.findIndex(out => out.startsWith('CommandLine'))

// get rid of the start (copyright) and the end (current pwd)
// eslint-disable-next-line unicorn/prefer-negative-index
_stdout.splice(_stdout.length - 1, 1)
_stdout.splice(0, beginRow)
// _stdout.splice(_stdout.length - 1, 1)
// _stdout.splice(0, beginRow)
// return _stdout.join(SystemEOL)

return _stdout.join(SystemEOL)
return _stdout.slice(beginRow + 1, -1).join(SystemEOL)
}

export type TPsTreeOpts = {
Expand Down Expand Up @@ -310,22 +310,23 @@ export const formatOutput = (data: TIngridResponse): TPsLookupEntry[] =>

export type PromiseResolve<T = any> = (value?: T | PromiseLike<T>) => void

const makeDeferred = <T = any, E = any>(): { promise: Promise<T>, resolve: PromiseResolve<T>, reject: PromiseResolve<E> } => {
type Deferred<T, E> = { promise: Promise<T>, resolve: PromiseResolve<T>, reject: PromiseResolve<E> }

const makeDeferred = <T = any, E = any>(): Deferred<T, E> => {
let resolve
let reject
const promise = new Promise<T>((res, rej) => { resolve = res; reject = rej })
return { resolve, reject, promise } as any
}

const makePseudoDeferred = <T = any, E = any>(r = {}): { promise: any, resolve: any, reject: any } => {
return {
const makePseudoDeferred = <T = any, E = any>(r = {}): Deferred<any, E> =>
({
promise: r as any,
resolve: identity,
reject(e: any) {
throw e
}
}
}
})

const noop = () => {/* noop */}

Expand Down
19 changes: 18 additions & 1 deletion src/test/ts/ps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, it, before, after } from 'node:test'
import process from 'node:process'
import * as cp from 'node:child_process'
import * as path from 'node:path'
import { kill, lookup, lookupSync, tree, treeSync } from '../../main/ts/ps.ts'
import { kill, lookup, lookupSync, tree, treeSync, extractWmic } from '../../main/ts/ps.ts'

const __dirname = new URL('.', import.meta.url).pathname
const marker = Math.random().toString(16).slice(2)
Expand Down Expand Up @@ -134,3 +134,20 @@ describe('treeSync()', () => {
assert.equal((await lookup({ arguments: marker })).length, 0)
})
})

describe('extractWmic()', () => {
it('extracts wmic output', () => {
const input = `CommandLine
ParentProcessId ProcessId
0 0
7548 1400
"C:\\Windows\\System32\\Wbem\\WMIC.exe" process get ProcessId,ParentProcessId,CommandLine
1400 17424
PS C:\\Users\\user>`

const sliced = extractWmic(input)

assert.equal(input.slice('CommandLine'.length + 1, -'PS C:\\Users\\user>'.length -1), sliced)
})
})

0 comments on commit 90295f7

Please sign in to comment.