in my project I have a typescript class that is used to create a server-proxy. To do this I have to run a command which then in the terminal prompts me to input data such as username etc. I have created this function to start the server proxy and use stdin and stdout to read and write into the terminal. The majority of this works but there is prompt that fails which is listed: Listed Prompt
My Code to handle this prompt ( I want to select User credentials (NTLM) which is always the first option )
const process = exec(command);
process.stdout?.on('data', (data) => {
const output = data.toString();
if (output.includes('User credentials')) {
process.stdin?.write('\n');
}
});
I have also tried process.stdin?.write('1\n');
to make sure it selects the first one but this also doesn't work. The debug console is showing me that it selects the last option. Any help from this would be greatly appreciated.
in my project I have a typescript class that is used to create a server-proxy. To do this I have to run a command which then in the terminal prompts me to input data such as username etc. I have created this function to start the server proxy and use stdin and stdout to read and write into the terminal. The majority of this works but there is prompt that fails which is listed: Listed Prompt
My Code to handle this prompt ( I want to select User credentials (NTLM) which is always the first option )
const process = exec(command);
process.stdout?.on('data', (data) => {
const output = data.toString();
if (output.includes('User credentials')) {
process.stdin?.write('\n');
}
});
I have also tried process.stdin?.write('1\n');
to make sure it selects the first one but this also doesn't work. The debug console is showing me that it selects the last option. Any help from this would be greatly appreciated.
- Do you want to simulate an Enter press? If you want it, better rethink the idea. A correct approach would be some named function that you call both in your keyboard even handler and elsewhere. That “elsewhere” would be your simulation. If you goal is different, please clarify. Just in case, please read: XY Problem. – Sergey A Kryukov Commented Feb 17 at 23:52
1 Answer
Reset to default 1Replace the exec
function with spawn
. spawn
provides a stream of output as it becomes available, unlike exec
which buffers output. Accumulate the incoming output from the process's standard output into a single string.
Check the accumulated output string for the presence of the prompt text.
Introduce a short delay (e.g., using setTimeout
) after detecting the prompt and before writing to the process's standard input. This delay accounts for the asynchronous nature of the interaction and gives the child process time to become ready to receive input.
Addresses the primary issues of timing and partial output by ensuring the entire prompt is received and a delay is introduced.
The setTimeout
trick is crucial for dealing with the asynchronous nature of the interaction in most of these solutions.