1 | <img src="documentation/logo.png" width="1026" height="100" alt="logo">
|
2 | <hr>
|
3 |
|
4 | [![CircleCI](https://circleci.com/gh/Originate/observable-process.svg?style=shield)](https://circleci.com/gh/Originate/observable-process)
|
5 | [![Dependency Status](https://david-dm.org/originate/observable-process.svg)](https://david-dm.org/originate/observable-process)
|
6 | [![devDependency Status](https://david-dm.org/originate/observable-process/dev-status.svg)](https://david-dm.org/originate/observable-process#info=devDependencies)
|
7 |
|
8 |
|
9 | High-level support for running, observing, and interacting with child processes
|
10 | in Node.js 4 and above.
|
11 |
|
12 |
|
13 | ```js
|
14 | const ObservableProcess = require('observableProcess')
|
15 | var myProcess = new ObservableProcess('echo hello')
|
16 | myProcess.on('ended', function ({exitCode}) {
|
17 | // ...
|
18 | })
|
19 | ```
|
20 |
|
21 | You can also provide the process to run as an _argv_ array:
|
22 |
|
23 | ```js
|
24 | myProcess = new ObservableProcess(['echo', 'hello'])
|
25 | ```
|
26 |
|
27 |
|
28 | ## Set the working directory of the subshell
|
29 |
|
30 | ```js
|
31 | myProcess = new ObservableProcess('echo hello', { cwd: '~/tmp' })
|
32 | ```
|
33 |
|
34 |
|
35 | ## Set environment variables in the subshell
|
36 |
|
37 |
|
38 | ```js
|
39 | myProcess = new ObservableProcess('echo hello', { env: { foo: 'bar' } })
|
40 | ```
|
41 |
|
42 | ## Working with output
|
43 |
|
44 | ObservableProcess provides powerful mechanisms to work with output
|
45 | generated by the subprocess.
|
46 | By default, the output of the observed process is printed on the console.
|
47 | You can also customize logging by providing custom `stdout` and `stderr` objects
|
48 | (which needs to have the method `write`):
|
49 |
|
50 | ```js
|
51 | const myStdOut = {
|
52 | write: (text) => {
|
53 | // ...
|
54 | }
|
55 | }
|
56 | const myStdErr = {
|
57 | write: (text) => {
|
58 | // ...
|
59 | }
|
60 | }
|
61 | myProcess = new ObservableProcess('echo hello', { stdout: myStdOut, stderr: myStdErr })
|
62 | ```
|
63 |
|
64 | You can use [dimConsole](https://github.com/kevgo/dim-console-node)
|
65 | to print output from the subshell dimmed,
|
66 | so that it is easy to distinguish from output of the main thread.
|
67 |
|
68 | ```js
|
69 | const dimConsole = require('dim-console')
|
70 | myProcess = new ObservableProcess('echo hello', { stdout: dimConsole.stdout, stderr: dimConsole.stderr })
|
71 | ```
|
72 |
|
73 | To get more detailed output like lifecycle events of the subshell
|
74 | in the error stream:
|
75 |
|
76 | ```js
|
77 | myProcess = new ObservableProcess('echo hello', { verbose: true })
|
78 | ```
|
79 |
|
80 | You can retrieve the output that has accumulated so far to `stdout` and `stderr`
|
81 | merged into a single string:
|
82 |
|
83 | ```js
|
84 | myProcess.fullOutput() // returns all the output produced by the subprocess so far
|
85 | ```
|
86 |
|
87 | You can be notified when the process prints given text on `stdout` or `stderr`.
|
88 | This is useful for waiting until slow-starting services are fully booted up.
|
89 |
|
90 | ```js
|
91 | myProcess.waitForText('listening on port 3000').then(function () {
|
92 | // this method runs after the process prints "listening on port 3000"
|
93 | })
|
94 | ```
|
95 |
|
96 | To disable output altogether:
|
97 |
|
98 | ```js
|
99 | myProcess = new ObservableProcess('my-server', { stdout: null, stderr: null })
|
100 | ```
|
101 |
|
102 |
|
103 |
|
104 | ## Input
|
105 |
|
106 | You can enter text into the running process via:
|
107 |
|
108 | ```js
|
109 | myProcess.enter('text')
|
110 | ```
|
111 |
|
112 |
|
113 | ## Kill the process
|
114 |
|
115 | If the process is running, you can kill it via:
|
116 |
|
117 | ```js
|
118 | myProcess.kill()
|
119 | ```
|
120 |
|
121 | This sets the `killed` property on the ObservableProcess instance,
|
122 | so that manual kills can be distinguished from crashes.
|
123 |
|
124 | To let ObservableProcess notify you when a process ended:
|
125 |
|
126 | ```js
|
127 | myProcess.waitForEnd(({exitCode, killed}) => {
|
128 | // the process has ended here
|
129 | })
|
130 | ```
|
131 | You can also access the exit code from the process object:
|
132 |
|
133 | ```js
|
134 | myProcess.exitCode
|
135 | ```
|
136 |
|
137 | ## Get the process id
|
138 |
|
139 | ```
|
140 | myProcess.pid()
|
141 | ```
|
142 |
|
143 |
|
144 | ## related libraries
|
145 |
|
146 | * [nexpect](https://github.com/nodejitsu/nexpect):
|
147 | Allows to define expectations on command output,
|
148 | and send it input,
|
149 | but doesn't allow to add more listeners to existing long-running processes,
|
150 | which makes declarative testing hard.
|