4

I have created a node script which should run as a background process. Right after the script starts running run I need to get some user data (username & password).

After getting the user data I wish to close the terminal but the process should continue running in the background.

To complicate things, the node script is being built with pkg lib and will be started as an .exe

pkg on NPM

Here is a the code that will be executed:

async function init() {
  try {
    await fetchConfig();
    const credentials = await getCredentials(); // this one prompts the request for user input
    watchForNewFiles(credentials); // that one should use the credentials and continue running in the background.
  } catch (e) {
    console.error(e);
  }
}

init();

vlio20
  • 8,955
  • 18
  • 95
  • 180

2 Answers2

1

Here's a solution:

// Neccesary libs
let fs = require("fs");
let child_process = require("child_process");

// Filename should end in .bat
let filename = "__tmp.bat";

// Main function, call this to prompt the values.
function prompt() {
// Write batch script
// Prompts 2 variables & writes them as JSON into the same file
fs.writeFileSync(filename,`@echo off
set /p Var1="Name: "
set /p Var2="Password: "
echo {"name": "%Var1%", "password": "%Var2%"} > ${filename}
exit`);

// Execute the script in a cmd.exe window & write the result into stdout & parse it as JSON
let result = JSON.parse(child_process.execSync(`start /w cmd /c ${filename} && type ${filename}`));

// Delete the file
fs.unlinkSync(filename);
return results;
}

// Example usage:
console.log(prompt()) // prints { name: 'hi', password: 'world' }

Tested as a node14-win-x64 pkg binary, works perfectly.

lxhom
  • 650
  • 4
  • 15
  • Can you please explain what's going on here. – vlio20 Apr 01 '21 at 10:28
  • We write a batch file that batch file asks you for a few variables. (`set /p`) Those variables get written into the same file when the batch file gets executed. (`echo ... %Var1% ... > ${filename}`). Then we start the batch file in a cmd.exe window (`child_process.execSync('start /w cmd /c ${filename}`) and read the contents the batch file has written (`type ${filename}`) and parse it as JSON (`JSON.parse`). Then we delete the file (which contains the output, which isnt needed anymore, `fs.unlinkSync(filename)`). We need to do that because CMD.exe doesnt do that natively. – lxhom Apr 01 '21 at 10:46
  • But currently the original script is prompting the request for the user input – vlio20 Apr 01 '21 at 10:52
  • Isn't that what it's supposed to do? Am I missing something? – lxhom Apr 01 '21 at 10:55
  • Currently the script asks the user for username and password. And then runnung while keeping the terminal window open. I would like to keep the same behavior, but instead of keeping the terminal windown open, I would like to close it but keep the process running in the background. – vlio20 Apr 01 '21 at 11:07
  • I am not sure how the how to use your answer with the current script. – vlio20 Apr 01 '21 at 11:07
  • The batch script kills/closes the window after the user input. And you can use the answer by copying my code to the top of your program (except for the Example usage) and then calling `let result = prompt()` and then you can access user/password with `result.name` and `result.password`. And if you want to let your node program running during the prompt with asynchronous JavaScript (it currently waits for the user) you can use `let promise = new Promise(resolve => resolve(prompt))` and then you can do `await promise` or `promise.then()`. – lxhom Apr 01 '21 at 14:34
  • I have added some code to the question. Could you please show how would you use it with your answer? – vlio20 Apr 06 '21 at 07:06
  • You'd add my code (except for the example [the last 2 lines]) at the top of your program and then replace `await getCredentials();` with `prompt();` – lxhom Apr 07 '21 at 13:42
0

AFAIU this is not possible to do from inside node.js's running code by itself.

You have to use some kind of tool to put the node.js program in the background.

This answer lists several tools for this purpose.

This one is also useful.

Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73