#[Solved] Keep writing to a spawn process

1 messages · Page 1 of 1 (latest)

burnt pebble
#

Hey
I'm trying to do an automate account creation with Bun and the rclone binary.
When I run the rclone command I get this

No remotes found, make a new one?
n) New remote
s) Set configuration password
q) Quit config

After answering n then now the next option is to enter the name

Enter name for new remote.
name>

Is there a way to spawn the process in Bun and keep sending input while analyzing the binary (rclone in my case) responses?

Something like this

proc.stdin!.write("n"); // first answer
proc.stdin!.write("GoogleDrive"); // second

Thanks

#

Keep writing to a spawn process

tidal arrow
#

proc.stdin.write should work though you might need to also run proc.stdin!.flush()

burnt pebble
#

This was my first thinking
So I've tried this

const proc = Bun.spawn(["rclone", "config"], {
    stdin: "pipe", // return a FileSink for writing
});

const text = await new Response(proc.stdout).text();
console.log(text);
proc.stdin!.write("n\n");
proc.stdin!.flush();
console.log(await new Response(proc.stdout).text()); 

But the next response never arrived

#

Is there infinity loop or some event mechanism I should implement?

#

So I was able to run the first n command

const proc = Bun.spawn(["rclone", "config"], {
    stdin: "pipe", // return a FileSink for writing

});
proc.stdin!.write("n\n");
proc.stdin!.flush();
proc.stdin!.write("gd\n");
proc.stdin!.flush();
proc.stdin!.write("18\n");
proc.stdin!.flush();
proc.stdin!.end();

But the command and writes that come after aren't being processed

tidal arrow
burnt pebble
#

Checking
Thanks 😄

#

That seem to do the work
Thanks a lot 👍

#

[Solved] Keep writing to a spawn process

burnt pebble
#

This is the final working flow

await sendCommand('n'); // new
await sendCommand('gda1'); // name
await sendCommand('18'); // type
await sendCommand(''); // client ID
await sendCommand(''); // Client Secret
await sendCommand('1'); // Access level?
await sendCommand(''); // service account
await sendCommand(''); // advance?
await sendCommand('n'); // has browser
await sendCommand(''); // Access token
await sendCommand('n'); // Shared drive
await sendCommand('y'); // Save
await sendCommand('q'); // Finish

async function sendCommand(command: string) {
    await Bun.sleep(200);
    proc.stdin!.write(`${command}\n`);
    proc.stdin!.flush();
    const {value: output, done} = await reader.read();
    const t                     = await new Response(output).text();

    console.write(chalk.greenBright(t));
    console.log('\n');

}
tidal arrow
#

why is the sleep necessary?

#

you can do await proc.stdin.flush()

burnt pebble
#

Nice I will update it one sec