So to start off a post like this it would be a good idea to start off with a simple hello world style example of the exec method. The first step would be to get a reference to the method by requiring in the child process module, and getting a reference of the exec method of that module.
Next I can now use the exec method to call and external command that might be there to work with in the operating system. For this example I am calling git, and preforming a status which would end in two very different ways depending if it is called in a git folder or not. So I just call exec, and then I give the command that I would call in the command line to do a git status check in the form of a string. An object is then returned by the exec method to which I can then use to attach some events.
When it comes to events I generally want to attach at least three handlers in most cases, one for the standard out, another for the standard error, and one last hander for the exit event of the command. So when I call this script a git status will be called, and the handers for the standard out, and error will be used to log out the results of that command to the console. When the command is over one way or another I will get the program has ended message when all is done.
There is a wide range of options that can be set for the instance of the exec call by way of an object that can be passed as the second argument for the method. One option that I thing is worth mentioning right away is the cwd option that can be used as a way to set what the current working directory for the command will be.
So I can take the example before where I am just calling git, and add an options object for the command. The path module can be used as a way to parse a path this is given or not as an argument wheh calling the script.
So if I where to copy this code to a local folder and save it as something like option_cwd.js I could then test it out on a git folder, as well as a folder that is not a git folder.
The script will yield different results depending if the folder that is set to the current working folder is a got folder or not.
Another option that might be worth pointing out is the option that can be used to set the size for the buffer that will be used for standard out and standard error.
The node exec method can be combined with promises to create methods that will work great in promise chains. For example say I want a method that will just make use of the git status command to check if a given folder is a git folder or not. If the given folder is a git folder the promise will resolve and things will progress to the next then call, if it is not a git folder things will progress to the next catch call.