UNPKG

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