How to get the output of a command in Deno?
Asked Answered
T

1

11

For example, suppose I have the following code:

Deno.run({cmd: ['echo', 'hello']})

How do I collect the output of that command which is hello ?

Transept answered 28/5, 2020 at 9:56 Comment(0)
S
10

Deno.run returns an instance of Deno.Process. Use the method .output() to get the buffered output. Don't forget to pass "piped" to stdout/stderr options if you want to read the contents.

const cmd = Deno.run({
  cmd: ["echo", "hello"], 
  stdout: "piped",
  stderr: "piped"
});

const output = await cmd.output() // "piped" must be set

cmd.close(); // Don't forget to close it

.output() returns a Promise which resolves to a Uint8Array so if you want the output as a UTF-8 string you need to use TextDecoder

const outStr = new TextDecoder().decode(output); // hello
Subantarctic answered 28/5, 2020 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.