Skip to content

Commit 58e71a4

Browse files
committed
add search command; add help
1 parent 9adc2a9 commit 58e71a4

File tree

4 files changed

+122
-31
lines changed

4 files changed

+122
-31
lines changed

README.md

+41-5
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,61 @@ $ npm install -g pwatch
1919
Add `pwatch` at the end of your command.
2020

2121
```
22-
$ sleep 10 | pwatch
22+
pwatch <pid | command>
23+
24+
25+
Commands:
26+
search [name] Lists all processes by PID that match "name"
27+
28+
29+
Examples:
30+
31+
– Notifies after 10 seconds
32+
33+
${'$ sleep 10 | pwatch'.cyan}
34+
35+
– See all PID for node related processes
36+
37+
${'$ pwatch search node'.cyan}
38+
39+
– Notify me when process 4030 ends
40+
41+
${'$ pwatch 4030'.cyan}
42+
43+
– Run in background with a &
44+
45+
${'$ sleep 10 | pwatch &'.cyan}
46+
`)
47+
```
48+
49+
### Examples
50+
51+
```
52+
sleep 10 | pwatch
2353
```
2454
or
2555

2656
```
27-
$ sleep 10 && pwatch
57+
sleep 10 && pwatch
2858
```
2959
or
3060

3161
```
32-
$ sleep 10; pwatch
62+
sleep 10; pwatch
63+
```
64+
65+
Optionally add a `&` so pwatch runs in the background
66+
67+
```
68+
sleep 10 | pwatch &
3369
```
3470

3571
Or if you forget to call it when running your command
3672

3773
```
3874
$ scp somelargefile.tar.gz root@remotehost:~
39-
$ ps aux | grep scp
40-
justink 60132 0.0 0.2 3041904 31712 s018 S+ 12:15PM 0:00.20 scp somelargefile.tar.gz root@remotehost:~
75+
$ pwatch search scp
76+
60132 - 0:00.20 scp somelargefile.tar.gz root@remotehost:~
4177
$ pwatch 60132
4278
```
4379

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "pwatch",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "Process Watcher",
55
"main": "./index.js",
66
"bin": {
7-
"pwatch": "./index.js"
7+
"pwatch": "./index.js"
88
},
99
"scripts": {
1010
"prepublish": "babel src --out-dir dist",
@@ -19,6 +19,8 @@
1919
"jest": "^18.1.0"
2020
},
2121
"dependencies": {
22-
"node-notifier": "^5.0.2"
22+
"colors": "^1.1.2",
23+
"node-notifier": "^5.0.2",
24+
"ps-man": "jkrup/node-ps"
2325
}
2426
}

src/main.js

+68-23
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,83 @@
11
import tty from 'tty';
2-
import { exec } from 'child_process';
32
import notifier from 'node-notifier';
3+
import ps from 'ps-man';
4+
import 'colors';
45

5-
function checkPid(pid, cb = function() { notifier.notify(`Process ${pid}: Completed`) }) {
6-
exec(`ps ${pid}`, (error, stdout, stderr) => {
7-
if (error) {
8-
return cb();
9-
}
10-
setTimeout(checkPid.bind(this, pid, cb), 1000);
11-
});
6+
function checkPid(pid, cb = () => { notifier.notify(`Process ${pid}: Completed`); }) {
7+
ps.list({ pid }, (err, res) => {
8+
if (res.length === 0) {
9+
return cb();
10+
}
11+
return setTimeout(checkPid.bind(this, pid, cb), 1000);
12+
});
1213
}
1314

1415
function handlePiped() {
15-
process.stdin.on('data', () => { }); // required so that readable is called on end
16-
process.stdin.on('end', () => {
17-
notifier.notify(`Process Completed`);
18-
});
16+
process.stdin.on('data', () => { }); // required so that readable is called on end
17+
process.stdin.on('end', () => {
18+
notifier.notify('Process Completed');
19+
});
20+
}
21+
22+
function search(term) {
23+
ps.list({ name: new RegExp(term, 'i') }, (err, res) => {
24+
const result = res
25+
.filter(el => !el.command.match(/pwatch search/))
26+
.map(el => `${el.pid.yellow} - ${el.command}`)
27+
.join('\n');
28+
console.log(result);
29+
});
30+
}
31+
32+
function help() {
33+
console.log(`
34+
${'pwatch'.bold} <pid | command>
35+
36+
37+
Commands:
38+
search [name] Lists all processes by PID that match "name"
39+
40+
41+
Examples:
42+
43+
${'–'.grey} Notifies after 10 seconds
44+
45+
${'$ sleep 10 | pwatch'.cyan}
46+
47+
${'–'.grey} See all PID for node related processes
48+
49+
${'$ pwatch search node'.cyan}
50+
51+
${'–'.grey} Notify me when process 4030 ends
52+
53+
${'$ pwatch 4030'.cyan}
54+
55+
${'–'.grey} Run in background with a &
56+
57+
${'$ sleep 10 | pwatch &'.cyan}
58+
`);
1959
}
2060

2161
function handlePid(pid) {
22-
if (!pid) {
23-
notifier.notify('No pid specified');
24-
process.exit(0);
25-
}
62+
if (!pid) {
63+
notifier.notify('No pid specified');
64+
process.exit(0);
65+
}
66+
if (String(pid).match(/search/i)) {
67+
search(process.argv[3]);
68+
} else if (Number.isNaN(Number(pid))) {
69+
help();
70+
} else {
2671
checkPid(pid);
72+
}
2773
}
2874

29-
3075
export default function main() {
31-
const isCalledViaPipe = !tty.isatty();
32-
if (isCalledViaPipe) {
33-
handlePiped();
34-
} else {
35-
handlePid(process.argv[2])
36-
}
76+
const isCalledViaPipe = !tty.isatty();
77+
if (isCalledViaPipe) {
78+
handlePiped();
79+
} else {
80+
handlePid(process.argv[2]);
81+
}
3782
}
3883

yarn.lock

+8
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,10 @@ [email protected]:
753753
version "1.0.3"
754754
resolved "https://registry.yarnpkg.com/colors/-/colors-1.0.3.tgz#0433f44d809680fdeb60ed260f1b0c262e82a40b"
755755

756+
colors@^1.1.2:
757+
version "1.1.2"
758+
resolved "https://registry.yarnpkg.com/colors/-/colors-1.1.2.tgz#168a4701756b6a7f51a12ce0c97bfa28c084ed63"
759+
756760
combined-stream@^1.0.5, combined-stream@~1.0.5:
757761
version "1.0.5"
758762
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.5.tgz#938370a57b4a51dea2c77c15d5c5fdf895164009"
@@ -2090,6 +2094,10 @@ prr@~0.0.0:
20902094
version "0.0.0"
20912095
resolved "https://registry.yarnpkg.com/prr/-/prr-0.0.0.tgz#1a84b85908325501411853d0081ee3fa86e2926a"
20922096

2097+
ps-man@jkrup/node-ps:
2098+
version "1.1.4"
2099+
resolved "https://codeload.github.com/jkrup/node-ps/tar.gz/b6070bb6290a2bc6b6aa914aa868724e13cff5c2"
2100+
20932101
punycode@^1.4.1:
20942102
version "1.4.1"
20952103
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e"

0 commit comments

Comments
 (0)