1
1
# ProcessPromise
2
2
3
- ## ` pipe() `
3
+ ## ` stdin `
4
4
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 ] .
7
7
8
- ``` js
9
- await $` echo "Hello, stdout!"`
10
- .pipe (fs .createWriteStream (' /tmp/output.txt' ))
8
+ Do not forget to end the stream.
11
9
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 ()
13
14
```
14
15
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.
17
21
18
22
``` 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 `
21
30
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
+ }
23
37
```
24
38
25
- Pipes can be used to show real-time output of programs:
39
+ ## ` pipe() `
40
+
41
+ Redirects the stdout of the child process.
26
42
27
43
``` js
28
- $ .verbose = false
44
+ await $` echo "Hello, stdout!"`
45
+ .pipe (fs .createWriteStream (' /tmp/output.txt' ))
29
46
30
- await $` echo 1; sleep 1; echo 2; sleep 1; echo 3;`
31
- .pipe (process .stdout )
47
+ await $` cat /tmp/output.txt`
32
48
```
33
49
34
- Pipe both stdout and stderr :
50
+ Pipes can be used to show a real-time output of the child process :
35
51
36
52
``` 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 )
41
55
```
42
56
43
- Also, the ` pipe() ` method can combine ` $ ` programs . Same as ` | ` in bash:
57
+ The ` pipe() ` method can combine ` $ ` processes . Same as ` | ` in bash:
44
58
45
59
``` js
46
60
let greeting = await $` printf "hello"`
47
61
.pipe ($` awk '{printf $1", world!"}'` )
48
62
.pipe ($` tr '[a-z]' '[A-Z]'` )
49
63
50
- console . log (greeting . stdout )
64
+ echo (greeting)
51
65
```
52
66
53
- Use combinations of ` pipe() ` and [ ` nothrow() ` ] ( https://github.com/google/zx #nothrow) :
67
+ Use combinations of ` pipe() ` and [ ` nothrow() ` ] ( #nothrow ) :
54
68
55
69
``` js
56
70
await $` find ./examples -type f -print0`
57
- .pipe (nothrow ( $` xargs -0 grep ${ ' missing' + ' part' } ` ))
71
+ .pipe ($` xargs -0 grep ${ ' missing' + ' part' } ` . nothrow ( ))
58
72
.pipe ($` wc -l` )
59
73
```
60
74
61
- ## ` nothrow ()`
75
+ ## ` kill ()`
62
76
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.
64
80
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
67
85
```
68
86
69
- Usage :
87
+ ## ` stdio() `
88
+
89
+ Specifies a stdio for the child process.
90
+
91
+ Default is ` .stdio('inherit', 'pipe', 'pipe') ` .
70
92
71
93
``` 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 ()
73
103
74
104
// Inside a pipe():
75
105
76
106
await $` find ./examples -type f -print0`
77
- .pipe(nothrow( $`xargs -0 grep something`))
107
+ .pipe ($` xargs -0 grep something` . nothrow ( ))
78
108
.pipe ($` wc -l` )
79
109
```
80
110
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 :
82
112
83
113
``` js
84
114
if (await $` [[ -d path ]]` .exitCode == 0 ) {
@@ -87,17 +117,27 @@ if (await $`[[ -d path ]]`.exitCode == 0) {
87
117
88
118
// Equivalent of:
89
119
90
- if ((await nothrow( $ ` [[ - d path ]]` )).exitCode == 0) {
120
+ if ((await $` [[ -d path ]]` . nothrow ( )).exitCode == 0 ) {
91
121
...
92
122
}
93
123
```
94
124
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() `
96
135
97
- Runs and sets a timeout for a cmd .
136
+ Kills the child process after specified timeout .
98
137
99
138
``` js
100
- import {withTimeout} from 'zx/experimental'
139
+ await $ ` sleep 999 ` . timeout ( ' 5s ' )
101
140
102
- await withTimeout(100, 'SIGTERM') ` sleep 9999 `
141
+ // Or with a specific signal.
142
+ await $` sleep 999` .timeout (' 5s' , ' SIGKILL' )
103
143
```
0 commit comments