Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switched to PS Core with a legacy PS fallback #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ declare type Options = {
logLevel?: LogLevelDesc;
}

declare function find(type: "name" | "pid" | "port", value: string | number | RegExp, strict?: boolean | Option): Promise<{
declare function find(type: "name" | "pid" | "port", value: string | number | RegExp, strict?: boolean | Options): Promise<{
pid: number;
ppid?: number;
uid?: number;
Expand Down
9 changes: 7 additions & 2 deletions lib/find_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,16 @@ const finders = {
sunos: 'darwin',
freebsd: 'darwin',
win32 (cond) {
return new Promise((resolve, reject) => {
return new Promise(async (resolve, reject) => {
const cmd = 'Get-CimInstance -className win32_process | select Name,ProcessId,ParentProcessId,CommandLine,ExecutablePath'
const lines = []

const proc = utils.spawn('powershell.exe', ['/c', cmd], { detached: false, windowsHide: true })
const pwshBinary = await utils.findFirstInPath('pwsh.exe') || await utils.findFirstInPath('powershell.exe')
if(!pwshBinary) {
throw new Error('No powershell binary was found')
}

const proc = utils.spawn(pwshBinary, ['/c', cmd], { detached: false, windowsHide: true })
proc.stdout.on('data', data => {
lines.push(data.toString())
})
Expand Down
18 changes: 17 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
'use strict'

const cp = require('child_process')
const path = require('path')
const fs = require('fs')

const UNIT_MB = 1024 * 1024

Expand All @@ -27,6 +29,20 @@ const utils = {
spawn (cmd, args, options) {
return cp.spawn(cmd, args, options)
},
async findFirstInPath(executable) {
const paths = process.env.path.split(process.platform === 'win32' ? ';' : ':').filter(f => f.trim())

for (const i in paths) {
try {
const search = path.join(paths[i], executable)
await fs.promises.lstat(search)
return search
}
catch (ex) { continue }
}

return null
},
/**
* Strip top lines of text
*
Expand Down Expand Up @@ -134,7 +150,7 @@ const utils = {
* @return {Array}
*/
parseTable (data) {
const lines = data.split(/(\r\n\r\n|\r\n\n|\n\r\n)|\n\n/).filter(line => {
const lines = data.replace(/\x1b\[[0-9;]*m/gm, '').split(/(\r\n\r\n|\r\n\n|\n\r\n)|\n\n/).filter(line => {
return line.trim().length > 0
}).map((e) => e.split(/(\r\n|\n|\r)/).filter(line => line.trim().length > 0))

Expand Down