bombillazo
bombillazo7mo ago

Deno.Command output as it runs

Hello, I am trying to run a command and have its output be logged as it runs , but for some reason the command runs but no output is displayed. I tried using both ouput with await and outputSync, setting stdout to piped and inherit but nothing.
4 Replies
Deno AI Helper
Deno AI Helper7mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the information you've provided, it seems like you might be trying to capture the output of a command as it is running. If you want to log the output of a command as it runs, you should set the stdout to inherit to ensure that the output of the command is directly printed to the parent process's stdout. Here's a basic example of how you might run a command and have its output logged in real-time:
const p = Deno.run({
cmd: ["echo", "hello"],
stdout: "inherit",
stderr: "inherit"
});

await p.status();
p.close();
const p = Deno.run({
cmd: ["echo", "hello"],
stdout: "inherit",
stderr: "inherit"
});

await p.status();
p.close();
In this example, the echo command's output should be immediately visible in the terminal because stdout and stderr are set to inherit. Please note that this advice is not based on the Deno documentation, and I would advise you to double-check the accuracy of this advice. If you continue to experience issues, you might want to explore the Deno community resources or forums for more specific guidance.
raunioroo
raunioroo7mo ago
use piped. first start and get the handle of the child process with const child = cmd.spawn(). then get a ReadableStreamReader from child.stdout.getReader(). You can then use the reader to keep capturing output while the program runs - I don't have a very concise example handy but let me know if you need help with that. Here's how I use the reader to process text output line by line, not sure if there are simpler methods. You can also process raw bytes if you need that for example, but I guess this text-line is the more common use case. My processes output JSON (each result a stringified json object on a new line) and this works well to capture and parse the invidual JSON lines, for example, as it chunks invidual line outputs for you for easy processing. May have some errors as I improvised this example from my slightly more complex code:
import { TextLineStream } from "https://deno.land/std@0.210.0/streams/text_line_stream.ts";

listenStream(stream, callback) {
const tls = new TextLineStream();
const tds = new TextDecoderStream();
const reader = stream.pipeThrough(tds).pipeThrough(tls).getReader();

while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
if (value) {
callback(value);
}
}
}

listenStream(child.stdout, function(line) {
console.log("GOT OUTPUT: " + line);
});
import { TextLineStream } from "https://deno.land/std@0.210.0/streams/text_line_stream.ts";

listenStream(stream, callback) {
const tls = new TextLineStream();
const tds = new TextDecoderStream();
const reader = stream.pipeThrough(tds).pipeThrough(tls).getReader();

while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
if (value) {
callback(value);
}
}
}

listenStream(child.stdout, function(line) {
console.log("GOT OUTPUT: " + line);
});
bombillazo
bombillazo6mo ago
Hey thanks for the help. I figured out the problem, basically the command was outputting warnings to to stderr instead of stdout, and stderr is piped by default, so I had to set it to inherit as well besides stdout.
raunioroo
raunioroo6mo ago
haha, now that you mentioned it, yeah I was confused about the exact some time thing at some point (didn't expect things to go to stderr)