0

First, Please understand that it's not smooth because I use a translator.

I want to create an API server using the child_process module in node.js to send git command. ex) git clone ..., git pull, git push , ...

And i succeeded!

But there's a big problem here. ;(

execSync(cmd, {stdio: 'inherit', cwd: cwd});

This is my code.

'npm start' And use this API.

Logs are displayed in the cmd window.

It is displayed in the log like this, but I can't allocate it as a variable in my node code. Like stdout, stderr.

If you don't use '{stdio: 'inherit'}' this option, you're successfully assigned to the variable.

But I want to show the log of the server and assign it to the variables.

How can we catch two rabbits?

torek
  • 448,244
  • 59
  • 642
  • 775
2nan
  • 11
  • 5
  • This isn't specific to Git: you'll find this applies to all child processes. (I don't do enough npm / node.js to know what you should use here though.) – torek Jan 27 '22 at 10:29

1 Answers1

0

This would print the log (stdout), and the log is assigned to data.

const childProcess = require("child_process");
const git = childProcess.exec("git pull");

git.stdout.on("data", data => {
   console.log(`Git replied: ${data}`);
});
// Git replied: Already up to date.
Yaniv Peretz
  • 1,118
  • 2
  • 13
  • 22