venego
venego
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
Either way, thanks a lot for helping @pyrote and @raunioroo. You saved me a lot of reading ❤️
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
I guess I'll try to mix between oneliner and this method, and maybe gradually find new ways to split commands into smaller independent chunks.
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
How does this code smell?
const vars = {
comandRunning: false,
}
const sleep = (t: number) => new Promise((resolve) => setTimeout(resolve, t));

listen(process.stdout, (data) => {
if(data == 'endofcommand[uniquestufuniquestufuniquestuf]'){
vars.comandRunning = false;
}
console.log("bash stdout says: " + data);
});
listen(process.stderr, (data) => {
console.log("bash stderr says: " + data);
});
const bash = async (cmdString) => {
while(1){
if(!vars.commandRunnsing){
vars.comandRunning = true;
await writer.write(textEncoder.encode(`${cmdString}; echo "endofcommand[uniquestufuniquestufuniquestuf]"\n`));
}

await sleep(500);
}
};
const vars = {
comandRunning: false,
}
const sleep = (t: number) => new Promise((resolve) => setTimeout(resolve, t));

listen(process.stdout, (data) => {
if(data == 'endofcommand[uniquestufuniquestufuniquestuf]'){
vars.comandRunning = false;
}
console.log("bash stdout says: " + data);
});
listen(process.stderr, (data) => {
console.log("bash stderr says: " + data);
});
const bash = async (cmdString) => {
while(1){
if(!vars.commandRunnsing){
vars.comandRunning = true;
await writer.write(textEncoder.encode(`${cmdString}; echo "endofcommand[uniquestufuniquestufuniquestuf]"\n`));
}

await sleep(500);
}
};
Not too bad right?
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
I'm thinking of a complex way that involves bash variables after each command, might be the worst way to do it tho.
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
The last line tho, seems to be unnecessary as deno does that without it (or something weird is blocking the process from exiting).
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
Thanks, I think this is exactly what I needed. I'm just confused by what you just said: if the output's end can't be detected, how do I make sure I don't the second command while the 1st still running?
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
No rush, I'm bruteforcing some random methods myself 🙂
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
If the input could be written to (like const output = await process.output(); but for inputs), that would be really nice; no streams are required for what I'm trying to input. I imagine it would be like: await process.setInput('inputtt').
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
If I don't close the writer/stdin, it just hangs there with no output.
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
const command = new Deno.Command("bash", {
args: [],
stdin: "piped",
stdout: "piped",
stderr: "piped",
});
const process = command.spawn();
const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();

const bash = async (cmdString)=>{
const writer = process.stdin.getWriter();
writer.write(textEncoder.encode(cmdString));
writer.releaseLock();
await process.stdin.close();

// const reader = await process.stdout.getReader();
// const ll = await reader.read();
// console.log(ll)
const output = await process.output();

const stdout = output.stdout;
console.log(textDecoder.decode(stdout));

const stderr = output.stderr;
console.log(textDecoder.decode(stderr));
}

await bash('echo "finally works"');
await bash('echo "finally works"');
const command = new Deno.Command("bash", {
args: [],
stdin: "piped",
stdout: "piped",
stderr: "piped",
});
const process = command.spawn();
const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();

const bash = async (cmdString)=>{
const writer = process.stdin.getWriter();
writer.write(textEncoder.encode(cmdString));
writer.releaseLock();
await process.stdin.close();

// const reader = await process.stdout.getReader();
// const ll = await reader.read();
// console.log(ll)
const output = await process.output();

const stdout = output.stdout;
console.log(textDecoder.decode(stdout));

const stderr = output.stderr;
console.log(textDecoder.decode(stderr));
}

await bash('echo "finally works"');
await bash('echo "finally works"');
The input has to be open and closed multiple times is what I assume. I've tried await process.stdin.open(); but there is no such method. here is the error i get on the second call to bash:
error: Uncaught (in promise) TypeError: The stream is closing or is closed.
writer.write(textEncoder.encode(cmdString));
error: Uncaught (in promise) TypeError: The stream is closing or is closed.
writer.write(textEncoder.encode(cmdString));
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
Oh, let me share an update...
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
That's good to know.
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
Thanks I'll give the mdn page a good read.
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
The MDN link seems to be talking about network streams, does deno use network to talk to sub-processes? or is it just the same protocol?
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
How do I release the lock for the following commands?
error: Uncaught (in promise) TypeError: Can't collect output because stdout is locked
const output = await process.output();
error: Uncaught (in promise) TypeError: Can't collect output because stdout is locked
const output = await process.output();
output.releaseLock() didn't work!
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
Here is what I tried: It works now
const command = new Deno.Command("bash", {
args: [],
stdin: "piped",
stdout: "piped",
stderr: "piped",
});
const process = command.spawn();
const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();

const bash = async (cmdString)=>{

const writer = process.stdin.getWriter();
writer.write(textEncoder.encode(cmdString));
writer.releaseLock();
await process.stdin.close();

const output = await process.output();

const stdout = output.stdout;
console.log(textDecoder.decode(stdout));

const stderr = output.stderr;
console.log(textDecoder.decode(stderr));
}

bash('echo "finally works');
const command = new Deno.Command("bash", {
args: [],
stdin: "piped",
stdout: "piped",
stderr: "piped",
});
const process = command.spawn();
const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();

const bash = async (cmdString)=>{

const writer = process.stdin.getWriter();
writer.write(textEncoder.encode(cmdString));
writer.releaseLock();
await process.stdin.close();

const output = await process.output();

const stdout = output.stdout;
console.log(textDecoder.decode(stdout));

const stderr = output.stderr;
console.log(textDecoder.decode(stderr));
}

bash('echo "finally works');
No output is shown in the console.
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
Got it, the code the AI wrote barely works, I got other erros that makes me want to write this from scratch, would you please suggest a link to the docs about this topic?
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
This a really clean code that works, but I really need one shell so I can set variables and stuff, (creating a post-installation script for installing Debian).
38 replies
DDeno
Created by venego on 1/10/2024 in #help
Can't spawn a shell properly using Deno!
The stdin appears to not have a function write!
error: Uncaught (in promise) TypeError: process.stdin.write is not a function
await process.stdin.write(encoder.encode("echo lll\n"));
^
at file:///home/venego/home/dev/lab/test.js:42:21
error: Uncaught (in promise) TypeError: process.stdin.write is not a function
await process.stdin.write(encoder.encode("echo lll\n"));
^
at file:///home/venego/home/dev/lab/test.js:42:21
38 replies