D
Deno

help

Deno.Command output as it runs

Bbombillazo12/25/2023
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.
DAHDeno AI Helper12/25/2023
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.
Rraunioroo12/25/2023
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);
});
Bbombillazo12/31/2023
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.
Rraunioroo12/31/2023
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)

Looking for more? Join the community!

Recommended Posts
electrons using denoIn the recent deno version I see it is possible to runn executable in ./bin in node_modules using Cannot find moduleHi <:vmathi:812484657547313212> I'm trying to run a project with Deno, but i'm getting "Cannot find Uncaught error during deno deploy of vite appWhen creating a new vite app with vue and typescript I get this error message. This error is reprodDeno, TypeScript, ESBuild, WebGL, VSCodeHello! As the title suggests, I am using these technologies to create a web application: * Deno * TPossible issue with re-export compatibility with NodeIn https://github.com/japa/runner/issues/35, I have described an issue where Deno seems to be incompMake deno.serve handler asynchronousHow can i make the Deno.serve handler asynchronous? or like make it so that it can execute asynchronWhy my place.x -= 5 not working?At this part if(moveKey.right.pressed) { finishPostion += 5 quota exceededi hit the free limit tonight and upgraded to pro 20 minutes ago— when do my sites stop erroring withDeploying WASM via Deno Deploy Github ActionsHello, I'm trying to deploy a Leptos project that serves a .wasm bundle for the frontend, and uses Get only the first records with deno kv but reversed orderedI'm using Deno KV to store articles. Generating the ID for the articles: ``` export function generaerror: Uncaught (in promise) SyntaxError: Invalid regular expression flagsAfter Updating my fresh to latest version I got following error ``` deno task start Task start deno third party module repo changeI'm the author of a third party deno module: https://deno.land/x/streamdal_protos@v0.0.126. My com