Kay
Kay8mo ago

Log output from Deno.ChildProcess while its running

I want to log all output immediately when its logged in the child process. how would i do that?
19 Replies
lucsoft
lucsoft8mo ago
// Create a new Command that runs the unix command uptime
await new Deno.Command('uptime', {
stdout: "piped" // Mark the stdout as piped so we can read it
})
.spawn() // make a new process
.output() // wait for the output
.then(x => new Response(x.stdout).text()) // read the output as a text using the Response object
.then(console.log); // then print out the text
// Create a new Command that runs the unix command uptime
await new Deno.Command('uptime', {
stdout: "piped" // Mark the stdout as piped so we can read it
})
.spawn() // make a new process
.output() // wait for the output
.then(x => new Response(x.stdout).text()) // read the output as a text using the Response object
.then(console.log); // then print out the text
@Kay here this should help you if you have more questions please let me know if you don't need spawn() then its simpler:
new Deno.Command("uptime")
.output()
.then((x) => new Response(x.stdout).text())
.then(console.log);
new Deno.Command("uptime")
.output()
.then((x) => new Response(x.stdout).text())
.then(console.log);
as with that the stdout is piped by default
Kay
KayOP8mo ago
i do need to spawn it but i dont want to wait for the output. is there a way to get live output from the process?
lucsoft
lucsoft8mo ago
yes you can use WHATWG Stream for that
Kay
KayOP8mo ago
whats that and how do i implement it?
lucsoft
lucsoft8mo ago
const process = new Deno.Command("top", { stdout: "piped" }).spawn();

async function reader() {
const stream = process.stdout
.pipeThrough(new TextDecoderStream())
for await (const message of stream) {
console.log(message);
}
}

reader(); // not calling await so its "non blocking"
const process = new Deno.Command("top", { stdout: "piped" }).spawn();

async function reader() {
const stream = process.stdout
.pipeThrough(new TextDecoderStream())
for await (const message of stream) {
console.log(message);
}
}

reader(); // not calling await so its "non blocking"
you can get the stream of stdout (which itself is a stream) and pipe and modify it. we get raw binary as stream but we want that a string stream so we can print it out or do other stuff with it the deno std also has stream helpers like a csv stream parser or json stream parser when you are parsing multiple lines
Kay
KayOP8mo ago
i did a quick search and stackoverflow said i also could use inherit for the stdout and stderr. whats the difference?
lucsoft
lucsoft8mo ago
inherit means it uses the same stdout as deno so if you process prints out Hello World deno would print our Hello World
Kay
KayOP8mo ago
yhea the process im running is deno so that should be fine right?
lucsoft
lucsoft8mo ago
but because its not pipe though "javascript" you itself can't read it
Kay
KayOP8mo ago
ohh
lucsoft
lucsoft8mo ago
yes, but btw if you subprocess is deno again maybe look into workers typo*
lucsoft
lucsoft8mo ago
Kay
KayOP8mo ago
yhea im just using Deno.Command to easely generate the command. i want to limit it to only the apis it needs and nothing else and to create a huge list in the deno.json file is a bit cluttered so i wanted to just generate it and store it in a way that can be read and modified better
lucsoft
lucsoft8mo ago
oh so maybe you want to look into deno tasks?
Kay
KayOP8mo ago
couse im pretty sure there is no other way to specify allows and denies other than in the tasks
lucsoft
lucsoft8mo ago
you just write deno task stat and it runns the command in the screenshot
No description
Kay
KayOP8mo ago
i did. its just that a huge list of websites in the command is a pain to read and modify
lucsoft
lucsoft8mo ago
ahh okay i see
Kay
KayOP8mo ago
so id have smth like --allow-net=discord.com,gateway.discord.com,deno.land,0.0.0.0,someotherthing.net and so on