Kay
Kay4mo 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
lucsoft4mo 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
Kay4mo 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
lucsoft4mo ago
yes you can use WHATWG Stream for that
Kay
Kay4mo ago
whats that and how do i implement it?
lucsoft
lucsoft4mo 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
Kay4mo ago
i did a quick search and stackoverflow said i also could use inherit for the stdout and stderr. whats the difference?
lucsoft
lucsoft4mo 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
Kay4mo ago
yhea the process im running is deno so that should be fine right?
lucsoft
lucsoft4mo ago
but because its not pipe though "javascript" you itself can't read it
Kay
Kay4mo ago
ohh
lucsoft
lucsoft4mo ago
yes, but btw if you subprocess is deno again maybe look into workers typo*
lucsoft
lucsoft4mo ago
Kay
Kay4mo 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
lucsoft4mo ago
oh so maybe you want to look into deno tasks?
Kay
Kay4mo ago
couse im pretty sure there is no other way to specify allows and denies other than in the tasks
lucsoft
lucsoft4mo ago
you just write deno task stat and it runns the command in the screenshot
No description
Kay
Kay4mo ago
i did. its just that a huge list of websites in the command is a pain to read and modify
lucsoft
lucsoft4mo ago
ahh okay i see
Kay
Kay4mo ago
so id have smth like --allow-net=discord.com,gateway.discord.com,deno.land,0.0.0.0,someotherthing.net and so on