Ed, Edd n Eddy
Ed, Edd n Eddy•9mo ago

Migrating from Deno.run to Deno.command, how to readLines?

At the moment I have this:
const command = new Deno.command('...');
for await (const line of readLines(command.stderr!)) {
// ...
break;
}

// .. Continue using sun process
const command = new Deno.command('...');
for await (const line of readLines(command.stderr!)) {
// ...
break;
}

// .. Continue using sun process
How would I now do this if using Deno.Command? I'm currently spawning a long running subprocess that doesn't exit on it's own, so simply running the command, and using outputSync() probably isnt an option
11 Replies
Deno AI Helper
Deno AI Helper•9mo ago
To migrate from Deno.run to Deno.Command and use readLines with the new API, you'll need to adjust your code to work with the Deno.Command class and its method for spawning a subprocess. The Deno.Command class provides a spawn method that returns a Deno.Process object, which you can use to interact with the subprocess's input and output streams. Here's how you can use Deno.Command to spawn a long-running subprocess and read lines from its standard error stream:
import { readLines } from "https://deno.land/std/io/mod.ts";

const command = new Deno.Command("your-command", {
args: ["your", "args", "here"],
stderr: "piped", // Ensure that stderr is piped
});

const process = command.spawn();

// Assuming you want to read from stderr
for await (const line of readLines(process.stderr)) {
console.log(line);
// ... your logic here
}

// Continue using the subprocess
// ...
import { readLines } from "https://deno.land/std/io/mod.ts";

const command = new Deno.Command("your-command", {
args: ["your", "args", "here"],
stderr: "piped", // Ensure that stderr is piped
});

const process = command.spawn();

// Assuming you want to read from stderr
for await (const line of readLines(process.stderr)) {
console.log(line);
// ... your logic here
}

// Continue using the subprocess
// ...
In this example, replace "your-command" and ["your", "args", "here"] with the actual command and arguments you want to run. The readLines function is used to asynchronously iterate over the lines of the subprocess's standard error stream. Remember that since the subprocess is long-running and doesn't exit on its own, you'll need to manage the lifecycle of the subprocess yourself. This includes potentially killing the subprocess when it's no longer needed or handling any cleanup logic. The documentation mentions that if you want to communicate with a started subprocess, you must use the "piped" option for stdin, stdout, and stderr as needed. This allows you to read from or write to the subprocess's streams. If you encounter any issues with the readLines function or the Deno.Command class, please make sure you are using the latest version of Deno and
crowlKats
crowlKats•9mo ago
hey long time no see!
import { TextLineStream } from "jsr:@std/streams";

const command = new Deno.command('...');
const child = command.spawn();
for await (const line of command.stderr.pipeThrough(new TextDecoderStream()).pipeThrough(new TextLineStream())) {}
import { TextLineStream } from "jsr:@std/streams";

const command = new Deno.command('...');
const child = command.spawn();
for await (const line of command.stderr.pipeThrough(new TextDecoderStream()).pipeThrough(new TextLineStream())) {}
Ed, Edd n Eddy
Ed, Edd n EddyOP•9mo ago
Hey 🙂 Whats this "jsr" import syntax? Also seems to work perfectly 🙂
crowlKats
crowlKats•9mo ago
its for https://jsr.io/ !
JSR: the JavaScript Registry
JSR is the open-source package registry for modern JavaScript. JSR natively supports TypeScript, and works with all JS runtimes and package managers.
Ed, Edd n Eddy
Ed, Edd n EddyOP•9mo ago
I am VERY behind, will give it a read does this replace deno.land/std and deno.land/x/?
crowlKats
crowlKats•9mo ago
yea, at some point we will make /x/ read-only, and expecting people to rather use JSR yep, havent seen you around in a long time
Ed, Edd n Eddy
Ed, Edd n EddyOP•9mo ago
Its probs been like a year or two or something lol, wanna get back into hobby coding a bit but im so behind on deno lol, everything i knew before is probably lost in time now xD Don’t suppose you’d know the reason for why a test file may hang, there’s no errors or leaking ops message, it just hangs on <test step name> … ok (4s), I’d assume there’s a promise not being resolved but wouldn’t deno throw an error message for this? It’s still using sub processes and is only an issue on windows
crowlKats
crowlKats•9mo ago
I’d assume there’s a promise not being resolved but wouldn’t deno throw an error message for this?
usually yes 🤔 could you share it?
Ed, Edd n Eddy
Ed, Edd n EddyOP•9mo ago
probs quite a complex case cause its sinco, the one that runs chrome as a subprocess so unfortunately the test cases aren't helpful cause we'd need to dig down into the src code, i can still share regardless, bear with i'm still going thru it trying to see if there's any thing i haven't resolved
Ed, Edd n Eddy
Ed, Edd n EddyOP•9mo ago
GitHub
sinco/tests/unit/client_test.ts at fix-tests · drashland/sinco
Browser Automation and Testing Tool for Deno, written in full TypeScript - drashland/sinco

Did you find this page helpful?