Next article

WooCommerce is widely used and very effective e-commerce plugin which allows you to set your online store in minutes only. Much beneficial compared to the...

Build basic GIT tool with NodeJS

NodeJS has become a popular platform for the new generation developers across the world. There are number of automated tools available for NodeJs application development. The following article will show how we can develop a light weight automated tool for own requirement.

When developing a large application, we often need to use the third party packages and some already developed modules which can be replicate for the different purpose. We can save a significant amount of effort by making a simple automatic scripting tool. In recent days, people have been using Git for deployment and versioning but it leads to back and forth between terminal and browser.

Let’s develop a tool that will replace the annoying switching between the terminal and browser.

NodeJS Child Processes

NodeJS provides a very powerful module, child process – that allows programmers to execute OS’ commands from their application. Following are the major ways to create child process:

  1. exec()
  2. execFile()
  3. spawn()
  4. fork()

Windows’ user can use spawn or exec as below:

const spawn = require("child_process").spawn;
var cmd = "path/to/my.cmd";
var ls = spawn(cmd, []);
      let output = null;
            ls.stdout.on("data", function(data) {
                console.log("stdout: " + data.toString());
                output = data;
            ls.stderr.on("data", function(data) {
                console.log("stderr: ", data.toString());
            ls.on("exit", function(code) {
                console.log("status completed " + code);

Now we will create a server file that will create NodeJS server. Then we’ll add a script that will show how to execute operation runtime.
Please create server.js file with following code:

var express = require("express");
var bodyParser = require("body-parser");
var fs = require("fs");
var app = express();
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
const spawn = require("child_process").spawn;
// Listen to port 5000
app.listen(5000, function() {
    console.log("Dev app listening on port 5000!");
// Render view for landing page.
app.get("/", function(req, res) {

Above code will create node server and render home.html view on http://localhost:5000.

build git tool with node js

Here, we are not going to focus on the view part so let’s move on to the next process. As you can see from the screen above, it asks for a repository URL. If you add a valid repository URL as mentioned in the example, our program will clone that repository. Below is the code that executes when clicking on clone button.

var createFile = function(url, callback, res) {
    var createStream = fs.createWriteStream("my.cmd");
    var repoNameArr = url.split("/");
    var repo = repoNameArr[repoNameArr.length - 1].split(".");
    repo_name = repo[0];
    var writeStream = fs.createWriteStream("my.cmd");
    writeStream.write("cd ..");
    writeStream.write("git clone " + url);
    writeStream.end(function() {
var commandExec = function(res) {
    console.log("In cmd execution");
    var cmd = "my.cmd";
    const ls = spawn(cmd, []);
    ls.stdout.on("data", function(data) {
        console.log("stdout: " + data.toString());
    ls.stderr.on("data", function(data) {
        console.log("stderr: ", data.toString());
    ls.on("exit", function(code) {
        console.log("clone completed " + code);
};"/clone_repo", function(req, res) {
    var url = req.body.git_url;
    createFile(url, commandExec, res);

In order to clone the repository, we need to collect username, password and repository. Fortunately, we can use below URL format and use git clone command to clone the remote repository.

To execute multiple commands, we need to create a .bat or .cmd file. In above code, createFile function generates a command file to clone user given repository. It will generate my.cmd file as below.

my.cmd file

After generating a command file, there is a callback function that executes commandExec() function. This function uses Node’s child process and executes commands of my.cmd file using spawn(). After successful execution you will see a repository created outside of your root project directory.

root project directory


  • Leave a message...