Komal Thamke
Komal Thamke

Reputation: 213

How can i show progress of node chid_process commands?

I have to show progress of node chid_process commands on UI. I am not able to track the progress of commands using any js lib like progress-bar. How can i show say "git clone" progress on UI so that user knows the status of the process ?

Upvotes: 0

Views: 1720

Answers (2)

user1693593
user1693593

Reputation:

If you mean in general and not for functions you define specifically for this purpose and want to run separately as child process, you really can't unless the child process provides this information itself via for example STDOUT.

And if so, you can only grab this raw output for then having to parse it to find something you could use to indicate progress. This of course, has its own quirks as the output is typically buffered which require you to think through how you parse the buffer(ing).

On top of that you can run into cases where the output format or order changes in the future in such a way that your program no longer can find the key information it needs.

In the case of git, there is really no progress per-se, only stages - which is fine and can act as a form of progress (stage 1 of 4 etc.).

To grab the output you would use something like:

const spawn = require("child_process").spawn;
const child = spawn("git" , ["clone", "https://some.rep"]);

child.stdout.on("data", data => {
  // parse data here...
});

...

and the same for stderr. See Node documentation for more details.

Upvotes: 3

David R
David R

Reputation: 15639

Try the farmhand package.

As the description goes, It is an abstration over child_process that makes it easy to run a function in the background, get its progress, result, and cancel if necessary.

Hope this helps!

Upvotes: -1

Related Questions