Files

Return to Package Diff Home.
Brought to you by Intrinsic.

Package Diff: find-process @ 1.2.1 .. 1.4.1

HISTORY.md

@@ -1,4 +1,39 @@
+1.4.1 / 2019-03-22
+==================
+
+ * chore: bump to 1.4.1
+ * fix: fix issue [#9](http://github.com/yibn2008/find-process/issues/9)
+
+1.4.0 / 2019-03-22
+==================
+
+ * chore: bump to 1.4.0
+ * feat: throw error when run on *nix system
+
+1.3.0 / 2019-03-22
+==================
+
+ * chore: bump to 1.3.0
+ * feat: support executable path
+ * docs: add bin prop to get execute path
+
+1.2.3 / 2019-03-22
+==================
+
+ * chore: bump to 1.2.3
+
+1.2.2 / 2019-03-22
+==================
+
+ * fix: fix name issue
+ * chore: bump to 1.2.2
+
+1.2.1 / 2018-11-15
+==================
+
+ * chore: add changelog
+
1.2.0 / 2018-10-19
==================

lib/find.js

@@ -11,20 +11,26 @@
const findProcess = require('./find_process')
const findBy = {
- port (port) {
- return findPid(port)
+ port (port, strict) {
+ return findPid(port, strict)
.then(pid => {
- return findBy.pid(pid)
+ return findBy.pid(pid, strict)
}, () => {
// return empty array when pid not found
return []
})
},
- pid (pid) {
- return findProcess({pid: pid})
+ pid (pid, strict) {
+ return findProcess({
+ pid: pid,
+ strict
+ })
},
name (name, strict) {
- return findProcess({name: name, strict: strict || false})
+ return findProcess({
+ name: name,
+ strict
+ })
}
}

lib/find_pid.js

@@ -14,15 +14,13 @@
const utils = require('./utils')
const ensureDir = (path) => new Promise((resolve, reject) => {
- fs.exists(path, exists => {
- if (exists) {
+ if (fs.existsSync(path)) {
resolve()
} else {
fs.mkdir(path, err => {
err ? reject(err) : resolve()
})
}
- })
})
const finders = {
@@ -54,7 +52,7 @@
if (found && found[2].length) {
resolve(parseInt(found[2], 10))
} else {
- reject(`pid of port (${port}) not found`)
+ reject(new Error(`pid of port (${port}) not found`))
}
}
})
@@ -68,7 +66,7 @@
let cmd = 'netstat -tunlp'
if (process.getuid() > 0) {
- cmd = 'sudo ' + cmd
+ throw new Error('netstat need root to run')
}
utils.exec(cmd, function (err, stdout, stderr) {
@@ -96,10 +94,10 @@
if (pid.length) {
resolve(parseInt(pid, 10))
} else {
- reject(`pid of port (${port}) not found`)
+ reject(new Error(`pid of port (${port}) not found`))
}
} else {
- reject(`pid of port (${port}) not found`)
+ reject(new Error(`pid of port (${port}) not found`))
}
}
})
@@ -126,10 +124,10 @@
}
})
- if (columns && columns[1].length) {
+ if (columns && columns[1].length && parseInt(columns[1], 10) > 0) {
resolve(parseInt(columns[1], 10))
} else {
- reject(`pid of port (${port}) not found`)
+ reject(new Error(`pid of port (${port}) not found`))
}
}
})
@@ -168,10 +166,10 @@
if (pid.length) {
resolve(parseInt(pid, 10))
} else {
- reject(`pid of port (${port}) not found`)
+ reject(new Error(`pid of port (${port}) not found`))
}
} else {
- reject(`pid of port (${port}) not found`)
+ reject(new Error(`pid of port (${port}) not found`))
}
}
})

lib/find_process.js

@@ -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
}
})

package.json

@@ -1,14 +1,12 @@
{
"name": "find-process",
- "version": "1.2.1",
+ "version": "1.4.1",
"description": "find process info by port/pid/name etc.",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"test": "mocha test/*.test.js && standard",
- "lint": "standard --fix && npm-ensure -t deps",
- "release": "git release $npm_package_version && npm publish",
- "beta": "git release $npm_package_version && npm publish --tag beta"
+ "lint": "standard --fix && npm-ensure -t deps"
},
"bin": {
"find-process": "bin/find-process.js"
@@ -55,12 +53,11 @@
"dependencies": {
"chalk": "^2.0.1",
"commander": "^2.11.0",
- "debug": "^2.6.8",
- "lodash": "^4.17.11"
+ "debug": "^2.6.8"
},
"devDependencies": {
- "mocha": "^2.3.4",
+ "mocha": "3.x",
"npm-ensure": "^1.1.2",
- "standard": "^5.4.1"
+ "standard": "12.x"
}
}

README.md

@@ -75,8 +75,7 @@
- `type` the type of find, support: *port|pid|name*
- `value` the value of type, can be RegExp if type is *name*
-- `strict` the optional strict mode is for checking *name* exactly matches the give one.
- (on Windows, `.exe` can be omitted)
+- `strict` the optional strict mode is for checking *name* exactly matches the give one. (on Windows, `.exe` can be omitted)
**Return**
@@ -91,6 +90,7 @@
uid: [user id (for *nix)],
gid: [user group id (for *nix)],
name: <command/process name>,
+ bin: <execute path (for *nix)>,
cmd: <full command with args>
}, ...]
```