Skip to content

Commit 8d3955e

Browse files
committed
Update docs
1 parent db0e651 commit 8d3955e

File tree

1 file changed

+78
-38
lines changed

1 file changed

+78
-38
lines changed

docs/process-promise.md

+78-38
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,114 @@
11
# ProcessPromise
22

3-
## `pipe()`
3+
## `stdin`
44

5-
The `zx` supports Node.js streams and special `pipe()` method can be used to
6-
redirect stdout.
5+
Returns a writable stream of stdin of a child process. Accessing
6+
this getter will trigger execution of a subprocess with `stdio('pipe')`[#stdio].
77

8-
```js
9-
await $`echo "Hello, stdout!"`
10-
.pipe(fs.createWriteStream('/tmp/output.txt'))
8+
Do not forget to end the stream.
119

12-
await $`cat /tmp/output.txt`
10+
```js
11+
let p = $`while read; do echo $REPLY; done`
12+
p.stdin.write('Hello, World!\n')
13+
p.stdin.end()
1314
```
1415

15-
Processes created with `$` gets stdin from `process.stdin`, but we can also
16-
write to child process too:
16+
By default, each process is created with stdin in _inherit_ mode.
17+
18+
## `stdout`/`stderr`
19+
20+
Returns a readable streams of stdout and stderr of a child process.
1721

1822
```js
19-
let p = $`read var; echo "$var";`
20-
p.stdin.write('Hello, stdin!\n')
23+
const p = $`npm init`
24+
for await (const chunk of p.stdout) {
25+
echo(chunk)
26+
}
27+
```
28+
29+
## `exitCode`
2130

22-
let {stdout} = await p
31+
Returns a promise which resolves to the exit code of the child process.
32+
33+
```js
34+
if (await $`[[ -d path ]]`.exitCode == 0) {
35+
...
36+
}
2337
```
2438

25-
Pipes can be used to show real-time output of programs:
39+
## `pipe()`
40+
41+
Redirects the stdout of the child process.
2642

2743
```js
28-
$.verbose = false
44+
await $`echo "Hello, stdout!"`
45+
.pipe(fs.createWriteStream('/tmp/output.txt'))
2946

30-
await $`echo 1; sleep 1; echo 2; sleep 1; echo 3;`
31-
.pipe(process.stdout)
47+
await $`cat /tmp/output.txt`
3248
```
3349

34-
Pipe both stdout and stderr:
50+
Pipes can be used to show a real-time output of the child process:
3551

3652
```js
37-
let echo = $`echo stdout; echo stderr 1>&2`
38-
echo.stdout.pipe(process.stdout)
39-
echo.stderr.pipe(process.stdout)
40-
await echo
53+
await $`echo 1; sleep 1; echo 2; sleep 1; echo 3;`
54+
.pipe(process.stdout)
4155
```
4256

43-
Also, the `pipe()` method can combine `$` programs. Same as `|` in bash:
57+
The `pipe()` method can combine `$` processes. Same as `|` in bash:
4458

4559
```js
4660
let greeting = await $`printf "hello"`
4761
.pipe($`awk '{printf $1", world!"}'`)
4862
.pipe($`tr '[a-z]' '[A-Z]'`)
4963

50-
console.log(greeting.stdout)
64+
echo(greeting)
5165
```
5266

53-
Use combinations of `pipe()` and [`nothrow()`](https://github.com/google/zx#nothrow):
67+
Use combinations of `pipe()` and [`nothrow()`](#nothrow):
5468

5569
```js
5670
await $`find ./examples -type f -print0`
57-
.pipe(nothrow($`xargs -0 grep ${'missing' + 'part'}`))
71+
.pipe($`xargs -0 grep ${'missing' + 'part'}`.nothrow())
5872
.pipe($`wc -l`)
5973
```
6074

61-
## `nothrow()`
75+
## `kill()`
6276

63-
Changes behavior of `$` to not throw an exception on non-zero exit codes.
77+
Kills the child process and all its children.
78+
79+
By default, signal `SIGTERM` is sent. You can specify a signal via an argument.
6480

65-
```ts
66-
function nothrow<P>(p: P): P
81+
```js
82+
let p = $`sleep 999`
83+
setTimeout(() => p.kill('SIGINT'), 100)
84+
await p
6785
```
6886

69-
Usage:
87+
## `stdio()`
88+
89+
Specifies a stdio for the child process.
90+
91+
Default is `.stdio('inherit', 'pipe', 'pipe')`.
7092

7193
```js
72-
await nothrow($`grep something from-file`)
94+
let p = $`read`.stdio('pipe')
95+
```
96+
97+
## `nothrow()`
98+
99+
Changes behavior of `$` to not throw an exception on non-zero exit codes.
100+
101+
```js
102+
await $`grep something from-file`.nothrow()
73103

74104
// Inside a pipe():
75105

76106
await $`find ./examples -type f -print0`
77-
.pipe(nothrow($`xargs -0 grep something`))
107+
.pipe($`xargs -0 grep something`.nothrow())
78108
.pipe($`wc -l`)
79109
```
80110

81-
If only the `exitCode` is needed, you can use the next code instead:
111+
If only the `exitCode` is needed, you can use [`exitCode`](#exitcode) directly:
82112

83113
```js
84114
if (await $`[[ -d path ]]`.exitCode == 0) {
@@ -87,17 +117,27 @@ if (await $`[[ -d path ]]`.exitCode == 0) {
87117

88118
// Equivalent of:
89119

90-
if ((await nothrow($`[[ -d path ]]`)).exitCode == 0) {
120+
if ((await $`[[ -d path ]]`.nothrow()).exitCode == 0) {
91121
...
92122
}
93123
```
94124

95-
## `withTimeout()`
125+
## `quiet()`
126+
127+
Changes behavior of `$` to disable verbose output.
128+
129+
```js
130+
// Command and output will not be displayed.
131+
await $`grep something from-file`.quiet()
132+
```
133+
134+
## `timeout()`
96135

97-
Runs and sets a timeout for a cmd.
136+
Kills the child process after specified timeout.
98137

99138
```js
100-
import {withTimeout} from 'zx/experimental'
139+
await $`sleep 999`.timeout('5s')
101140

102-
await withTimeout(100, 'SIGTERM')`sleep 9999`
141+
// Or with a specific signal.
142+
await $`sleep 999`.timeout('5s', 'SIGKILL')
103143
```

0 commit comments

Comments
 (0)