Appearance
进程 Promise
¥Process Promise
$
返回 ProcessPromise
实例。解决后,它就变成了 ProcessOutput
。
¥The $
returns a ProcessPromise
instance. When resolved, it becomes a ProcessOutput
.
js
const p = $`command` // ProcessPromise
const o = await p // ProcessOutput
stdin
返回 stdin 进程的可写流。访问此 getter 将触发执行具有 stdio('pipe')
的子进程。
¥Returns a writable stream of the stdin process. Accessing this getter will trigger execution of a subprocess with stdio('pipe')
.
不要忘记结束流。
¥Do not forget to end the stream.
js
const p = $`while read; do echo $REPLY; done`
p.stdin.write('Hello, World!\n')
p.stdin.end()
默认情况下,每个进程都是使用 stdin 以继承模式创建的。
¥By default, each process is created with stdin in inherit mode.
stdout
/stderr
返回 stdout/stderr 进程的可读流。
¥Returns a readable streams of stdout/stderr process.
js
const p = $`npm init`
for await (const chunk of p.stdout) {
echo(chunk)
}
exitCode
返回一个解析为进程退出代码的 promise。
¥Returns a promise which resolves to the exit code of the process.
js
if (await $`[[ -d path ]]`.exitCode == 0) {
...
}
pipe()
重定向进程的标准输出。
¥Redirects the stdout of the process.
js
await $`echo "Hello, stdout!"`
.pipe(fs.createWriteStream('/tmp/output.txt'))
await $`cat /tmp/output.txt`
管道可用于显示进程的实时输出:
¥Pipes can be used to show a real-time output of the process:
js
await $`echo 1; sleep 1; echo 2; sleep 1; echo 3;`
.pipe(process.stdout)
pipe()
方法可以组合 $
过程。与 bash 中的 |
相同:
¥The pipe()
method can combine $
processes. Same as |
in bash:
js
const greeting = await $`printf "hello"`
.pipe($`awk '{printf $1", world!"}'`)
.pipe($`tr '[a-z]' '[A-Z]'`)
echo(greeting)
使用 pipe()
和 nothrow()
的组合:
¥Use combinations of pipe()
and nothrow()
:
js
await $`find ./examples -type f -print0`
.pipe($`xargs -0 grep ${'missing' + 'part'}`.nothrow())
.pipe($`wc -l`)
kill()
杀死进程和所有子进程。
¥Kills the process and all children.
默认情况下,发送信号 SIGTERM
。你可以通过参数指定信号。
¥By default, signal SIGTERM
is sent. You can specify a signal via an argument.
js
const p = $`sleep 999`
setTimeout(() => p.kill('SIGINT'), 100)
await p
stdio()
指定进程的 stdio。
¥Specifies a stdio for the process.
默认为 .stdio('inherit', 'pipe', 'pipe')
。
¥Default is .stdio('inherit', 'pipe', 'pipe')
.
js
const p = $`read`.stdio('pipe')
nothrow()
更改 $
的行为,使其在非零退出代码时不引发异常。
¥Changes behavior of $
to not throw an exception on non-zero exit codes.
js
await $`grep something from-file`.nothrow()
// Inside a pipe():
await $`find ./examples -type f -print0`
.pipe($`xargs -0 grep something`.nothrow())
.pipe($`wc -l`)
如果只需要 exitCode
,可以直接使用 exitCode
:
¥If only the exitCode
is needed, you can use exitCode
directly:
js
if (await $`[[ -d path ]]`.exitCode == 0) {
...
}
// Equivalent of:
if ((await $`[[ -d path ]]`.nothrow()).exitCode == 0) {
...
}
quiet()
更改 $
的行为以禁用详细输出。
¥Changes behavior of $
to disable verbose output.
js
// Command and output will not be displayed.
await $`grep something from-file`.quiet()
timeout()
在指定的超时后终止进程。
¥Kills the process after a specified timeout.
js
await $`sleep 999`.timeout('5s')
// Or with a specific signal.
await $`sleep 999`.timeout('5s', 'SIGKILL')