Skip to content

Commit

Permalink
Merge pull request #34 from DominicBoettger/master
Browse files Browse the repository at this point in the history
Updated repository
  • Loading branch information
yibn2008 committed Oct 22, 2020
2 parents 4002d35 + 13b2ce8 commit 9c5ebac
Show file tree
Hide file tree
Showing 9 changed files with 2,976 additions and 49 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/nodejs.yml
@@ -0,0 +1,28 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node: [4, 10, 12, 14]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
2 changes: 1 addition & 1 deletion bin/find-process.js
Expand Up @@ -61,7 +61,7 @@ find(type, keyword)
if (list.length) {
console.log('Found %s process' + (list.length === 1 ? '' : 'es') + '\n', list.length)

for (let item of list) {
for (const item of list) {
console.log(chalk.cyan('[%s]'), item.name || 'unknown')
console.log('pid: %s', chalk.white(item.pid))
console.log('cmd: %s', chalk.white(item.cmd))
Expand Down
36 changes: 18 additions & 18 deletions lib/find_pid.js
Expand Up @@ -37,13 +37,13 @@ const finders = {
}

// replace header
let data = utils.stripLine(stdout.toString(), 2)
let found = utils.extractColumns(data, [0, 3, 8], 10)
const data = utils.stripLine(stdout.toString(), 2)
const found = utils.extractColumns(data, [0, 3, 8], 10)
.filter(row => {
return !!String(row[0]).match(/^(udp|tcp)/)
})
.find(row => {
let matches = String(row[1]).match(/\.(\d+)$/)
const matches = String(row[1]).match(/\.(\d+)$/)
if (matches && matches[1] === String(port)) {
return true
}
Expand All @@ -62,7 +62,7 @@ const finders = {
sunos: 'darwin',
linux (port) {
return new Promise((resolve, reject) => {
let cmd = 'netstat -tunlp'
const cmd = 'netstat -tunlp'

utils.exec(cmd, function (err, stdout, stderr) {
if (err) {
Expand All @@ -75,16 +75,16 @@ const finders = {
}

// replace header
let data = utils.stripLine(stdout.toString(), 2)
let columns = utils.extractColumns(data, [3, 6], 7).find(column => {
let matches = String(column[0]).match(/:(\d+)$/)
const data = utils.stripLine(stdout.toString(), 2)
const columns = utils.extractColumns(data, [3, 6], 7).find(column => {
const matches = String(column[0]).match(/:(\d+)$/)
if (matches && matches[1] === String(port)) {
return true
}
})

if (columns && columns[1]) {
let pid = columns[1].split('/', 1)[0]
const pid = columns[1].split('/', 1)[0]

if (pid.length) {
resolve(parseInt(pid, 10))
Expand All @@ -111,9 +111,9 @@ const finders = {
}

// replace header
let data = utils.stripLine(stdout.toString(), 4)
let columns = utils.extractColumns(data, [1, 4], 5).find(column => {
let matches = String(column[0]).match(/:(\d+)$/)
const data = utils.stripLine(stdout.toString(), 4)
const columns = utils.extractColumns(data, [1, 4], 5).find(column => {
const matches = String(column[0]).match(/:(\d+)$/)
if (matches && matches[1] === String(port)) {
return true
}
Expand All @@ -136,9 +136,9 @@ const finders = {
// warning as an error, `util.exec()` will get nothing but the error. To
// get the true output of the command, we need to save it to a tmpfile and
// read that file instead.
let dir = os.tmpdir() + '/.find-process'
let file = dir + '/' + process.pid
let cmd = 'netstat -tunp >> "' + file + '"'
const dir = os.tmpdir() + '/.find-process'
const file = dir + '/' + process.pid
const cmd = 'netstat -tunp >> "' + file + '"'

ensureDir(dir).then(() => {
utils.exec(cmd, () => {
Expand All @@ -148,15 +148,15 @@ const finders = {
reject(err)
} else {
data = utils.stripLine(data, 2)
let columns = utils.extractColumns(data, [3, 6], 7).find(column => {
let matches = String(column[0]).match(/:(\d+)$/)
const columns = utils.extractColumns(data, [3, 6], 7).find(column => {
const matches = String(column[0]).match(/:(\d+)$/)
if (matches && matches[1] === String(port)) {
return true
}
})

if (columns && columns[1]) {
let pid = columns[1].split('/', 1)[0]
const pid = columns[1].split('/', 1)[0]

if (pid.length) {
resolve(parseInt(pid, 10))
Expand All @@ -175,7 +175,7 @@ const finders = {
}

function findPidByPort (port) {
let platform = process.platform
const platform = process.platform

return new Promise((resolve, reject) => {
if (!(platform in finders)) {
Expand Down
16 changes: 8 additions & 8 deletions lib/find_process.js
Expand Up @@ -61,7 +61,7 @@ const finders = {
if ('pid' in cond) {
cmd = `ps -p ${cond.pid} -ww -o pid,ppid,uid,gid,args`
} else {
cmd = `ps ax -ww -o pid,ppid,uid,gid,args`
cmd = 'ps ax -ww -o pid,ppid,uid,gid,args'
}

utils.exec(cmd, function (err, stdout, stderr) {
Expand All @@ -80,8 +80,8 @@ const finders = {
return
}

let data = utils.stripLine(stdout.toString(), 1)
let columns = utils.extractColumns(data, [0, 1, 2, 3, 4], 5).filter(column => {
const data = utils.stripLine(stdout.toString(), 1)
const columns = utils.extractColumns(data, [0, 1, 2, 3, 4], 5).filter(column => {
if (column[0] && cond.pid) {
return column[0] === String(cond.pid)
} else if (column[4] && cond.name) {
Expand Down Expand Up @@ -131,7 +131,7 @@ const finders = {
if (code !== 0) {
return reject(new Error('Command \'' + cmd + '\' terminated with code: ' + code))
}
let list = utils.parseTable(lines.join('\n'))
const list = utils.parseTable(lines.join('\n'))
.filter(row => {
if ('pid' in cond) {
return row.ProcessId === String(cond.pid)
Expand Down Expand Up @@ -161,7 +161,7 @@ const finders = {
},
android (cond) {
return new Promise((resolve, reject) => {
let cmd = 'ps'
const cmd = 'ps'

utils.exec(cmd, function (err, stdout, stderr) {
if (err) {
Expand All @@ -179,8 +179,8 @@ const finders = {
return
}

let data = utils.stripLine(stdout.toString(), 1)
let columns = utils.extractColumns(data, [0, 3], 4).filter(column => {
const data = utils.stripLine(stdout.toString(), 1)
const columns = utils.extractColumns(data, [0, 3], 4).filter(column => {
if (column[0] && cond.pid) {
return column[0] === String(cond.pid)
} else if (column[1] && cond.name) {
Expand Down Expand Up @@ -217,7 +217,7 @@ const finders = {
}

function findProcess (cond) {
let platform = process.platform
const platform = process.platform

return new Promise((resolve, reject) => {
if (!(platform in finders)) {
Expand Down
28 changes: 14 additions & 14 deletions lib/utils.js
Expand Up @@ -38,7 +38,7 @@ const utils = {
let idx = 0

while (num-- > 0) {
let nIdx = text.indexOf('\n', idx)
const nIdx = text.indexOf('\n', idx)
if (nIdx >= 0) {
idx = nIdx + 1
}
Expand All @@ -55,7 +55,7 @@ const utils = {
* @return {Array}
*/
split (line, max) {
let cols = line.trim().split(/\s+/)
const cols = line.trim().split(/\s+/)

if (cols.length > max) {
cols[max - 1] = cols.slice(max - 1).join(' ')
Expand Down Expand Up @@ -90,16 +90,16 @@ const utils = {
* @return {Array}
*/
extractColumns (text, idxes, max) {
let lines = text.split(/(\r\n|\n|\r)/)
let columns = []
const lines = text.split(/(\r\n|\n|\r)/)
const columns = []

if (!max) {
max = Math.max.apply(null, idxes) + 1
}

lines.forEach(line => {
let cols = utils.split(line, max)
let column = []
const cols = utils.split(line, max)
const column = []

idxes.forEach(idx => {
column.push(cols[idx] || '')
Expand Down Expand Up @@ -130,17 +130,17 @@ const utils = {
* @return {Array}
*/
parseTable (data) {
let lines = data.split(/(\r\n|\n|\r)/).filter(line => {
const lines = data.split(/(\r\n|\n|\r)/).filter(line => {
return line.trim().length > 0
})

let matches = lines.shift().trim().match(/(\w+\s*)/g)
const matches = lines.shift().trim().match(/(\w+\s*)/g)
if (!matches) {
return []
}
let ranges = []
let headers = matches.map((col, i) => {
let range = []
const ranges = []
const headers = matches.map((col, i) => {
const range = []

if (i === 0) {
range[0] = 0
Expand All @@ -157,10 +157,10 @@ const utils = {
ranges[ranges.length - 1][1] = Infinity

return lines.map(line => {
let row = {}
const row = {}
ranges.forEach((r, i) => {
let key = headers[i]
let value = line.substring(r[0], r[1]).trim()
const key = headers[i]
const value = line.substring(r[0], r[1]).trim()

row[key] = value
})
Expand Down

0 comments on commit 9c5ebac

Please sign in to comment.