I have the following code that should open a terminal and run a command:
import { ChildProcess, spawn, ChildProcessWithoutNullStreams, ChildProcessByStdio } from 'child_process';
import { Writable, Readable } from 'stream';
const terminal:
ChildProcessWithoutNullStreams |
ChildProcessByStdio<Writable, Readable, Readable> |
ChildProcess = spawn('open', ['-b', "com.googlecode.iterm2"]);
terminal.on('spawn', () => {
console.log("spawn_0")
if (terminal.stdin) {
console.log("spawn_1")
terminal.stdin.write(`tail -n +1 -f ${cacheDir}/${nameFile}.txt\n`);
terminal.stdin.end();
}
});
terminal.on('open', () => {
console.log("open_0")
if (terminal.stdin) {
console.log("open_1")
terminal.stdin.write(`tail -n +1 -f ${cacheDir}/${nameFile}.txt\n`);
terminal.stdin.end();
}
});
currently when this code is run, this is printed in the console:
spawn_0
spawn_1
The terminal opens correctly, but the "ls" command does not execute.
Info:
I have a txt file, which is continuously updated. I have to make sure that when the user calls this part of code, the terminal chosen by the user is started, redirecting the file output to the terminal.
fs
to list the contents of a directory instead? That would be significantly more performant (at scale) and require way less code. Isls
a placeholder for something else that doesn't have any Node bindings (or a way to write them)? If so, it might make more sense if you're able to update your code to be a bit closer to representative of your actual use case. – Vinita\n
that would execute the command in the terminal in question. Duplicate of Nodejs Child Process: write to stdin from an already initialised process – Vinita