@@ -8,7 +8,6 @@
'use strict'
const path = require('path')
-const endsWith = require('lodash/endsWith')
const utils = require('./utils')
function matchName (text, name) {
@@ -19,6 +18,39 @@
return text.match(name)
}
+function fetchBin (cmd) {
+ const pieces = cmd.split(path.sep)
+ const last = pieces[pieces.length - 1]
+ if (last) {
+ pieces[pieces.length - 1] = last.split(' ')[0]
+ }
+ const fixed = []
+ for (const part of pieces) {
+ const optIdx = part.indexOf(' -')
+ if (optIdx >= 0) {
+ // case: /aaa/bbb/ccc -c
+ fixed.push(part.substring(0, optIdx).trim())
+ break
+ } else if (part.endsWith(' ')) {
+ // case: node /aaa/bbb/ccc.js
+ fixed.push(part.trim())
+ break
+ }
+ fixed.push(part)
+ }
+ return fixed.join(path.sep)
+}
+
+function fetchName (fullpath) {
+ if (process.platform === 'darwin') {
+ const idx = fullpath.indexOf('.app/')
+ if (idx >= 0) {
+ return path.basename(fullpath.substring(0, idx))
+ }
+ }
+ return path.basename(fullpath)
+}
+
const finders = {
darwin (cond) {
return new Promise((resolve, reject) => {
@@ -57,14 +89,16 @@
})
let list = columns.map(column => {
- let cmd = String(column[4]).split(' ', 1)[0]
+ const cmd = String(column[4])
+ const bin = fetchBin(cmd)
return {
pid: parseInt(column[0], 10),
ppid: parseInt(column[1], 10),
uid: parseInt(column[2], 10),
gid: parseInt(column[3], 10),
- name: path.basename(cmd),
+ name: fetchName(bin),
+ bin: bin,
cmd: column[4]
}
})
@@ -83,7 +117,7 @@
freebsd: 'darwin',
win32 (cond) {
return new Promise((resolve, reject) => {
- const cmd = 'WMIC path win32_process get Name,Processid,ParentProcessId,Commandline'
+ const cmd = 'WMIC path win32_process get Name,Processid,ParentProcessId,Commandline,ExecutablePath'
const lines = []
const proc = utils.spawn('cmd', ['/c', cmd], { detached: false, windowsHide: true })
@@ -92,18 +126,18 @@
})
proc.on('close', code => {
if (code !== 0) {
- return reject('Command \'' + cmd + '\' terminated with code: ' + code)
+ return reject(new Error('Command \'' + cmd + '\' terminated with code: ' + code))
}
let list = utils.parseTable(lines.join('\n'))
.filter(row => {
- if (cond.pid) {
+ if ('pid' in cond) {
return row.ProcessId === String(cond.pid)
} else if (cond.name) {
if (cond.strict) {
- return row.Name === cond.name ||
- (endsWith(row.Name, '.exe') && row.Name.slice(0, -4) === cond.name)
+ return row.Name === cond.name || (row.Name.endsWith('.exe') && row.Name.slice(0, -4) === cond.name)
} else {
- return matchName(row.CommandLine, cond.name)
+ // fix #9
+ return matchName(row.CommandLine || row.Name, cond.name)
}
} else {
return true
@@ -114,6 +148,7 @@
ppid: parseInt(row.ParentProcessId, 10),
// uid: void 0,
// gid: void 0,
+ bin: row.ExecutablePath,
name: row.Name,
cmd: row.CommandLine
}))
@@ -153,15 +188,17 @@
})
let list = columns.map(column => {
- let cmd = String(column[1]).split(' ', 1)[0]
+ const cmd = String(column[1])
+ const bin = fetchBin(cmd)
return {
pid: parseInt(column[0], 10),
// ppid: void 0,
// uid: void 0,
// gid: void 0,
- name: path.basename(cmd),
- cmd: column[1]
+ name: fetchName(bin),
+ bin,
+ cmd
}
})