Running the contents of a .bat file as a child process without the .bat file in Node.js

Viewed 34

I'm writing a node web-app that can create and execute scripts. Users have the option of either turning their script into a batch file and running it themselves or using the web-app itself to execute the same content that's in the batch.

If a user decides to use the web-app to run the script, I then provide them a window of the console output so they can follow along any errors.

My problem is that currently, I create a batch file in the server filesystem than call that batch file using a spawn - child process, like such:

  fs.mkdirSync(fileDir, { recursive: true })
  let file = path.join(fileDir, fileName);
  fs.writeFile(file, batchContent, (err) => {
    console.log(file)
    if (err) {
      console.log("Error writing file:")
    } else {
      scriptWorkers[script.id] = spawn(String.raw`${file}`)
    }
  });

I would like my code to not create a file, but instead run all the lines in my batch without the batch file existing. Something like the following:

  scriptWorkers[script.id] = spawn(String.raw`${batchContent}`)

I may be using the wrong child-process type for this, but if someone can please explain the best way to achieve this it would be much appreciated.

Thanks.

0 Answers
Related