/
subprocesses-output.ts
36 lines (32 loc) · 1.17 KB
/
subprocesses-output.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* @title Subprocesses: Collecting Output
* @difficulty intermediate
* @tags cli
* @run --allow-run <url>
* @resource {https://deno.land/api?s=Deno.Command} Doc: Deno.Command
*
* We don't often write programs in isolation. In a lot of cases we want
* to interact with the outside system and spawning a subprocess is a
* common way to do this.
*/
// The Deno namespace has a unified api for interacting with the outside system
// called Deno.Command. With it, we can initialize some information about the
// command but it will not be executed immediately.
const command = new Deno.Command("deno", {
args: [
"eval",
"\
console.log('hello from deno'); \
console.error('hello from stderr'); \
",
],
});
// In the most simple case we just want to run the process to completion. This
// can be achieved using command.output()
let result = await command.output();
// It can also be achieved synchronously using command.outputSync()
result = command.outputSync();
// We can now interact with stdout and stderr
const textDecoder = new TextDecoder();
console.log("stdout:", textDecoder.decode(result.stdout));
console.log("stderr:", textDecoder.decode(result.stderr));