Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the output of a command in Deno?

Tags:

deno

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 ?

like image 749
Wong Jia Hau Avatar asked May 28 '20 09:05

Wong Jia Hau


1 Answers

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
like image 57
Marcos Casagrande Avatar answered Oct 12 '22 21:10

Marcos Casagrande