Search code examples
node.jsgityeoman-generator

Show Feedback From GIt When Launching as Spawned Node Process


I'm trying to setup a boiler plate for new projects by creating a new yeoman generator, one thing that I need to have happen is a large Git repository set up as a submodule and then checked out at a certain tag. I have everything working, but I'd like to provide some progress feedback on the checkout.

When you run submodule add manually you get updated like so:

Receiving objects:  14% (22925/163744), 5.41 MiB | 1.30 MiB/s

I'd like to have that output show during my node script submodule add, but I can't seem to get it to show anything. Here's what I have:

MyGenerator.prototype.addSubmodule = function() {
  var done = this.async();

  console.log('Initializing submodule. This may take a minute.');

  var git = spawn('git', ['submodule', 'add', 'git://github.com/PathTo/Submodule.git', 'submodule']);$

  git.stdout.on('data', function(data){
    console.log(data);
  });

  git.stderr.on('data', function(data){
    console.log(data);
  });

  git.on('close', function(){$
    process.chdir('submodule');$

    console.log('Checking out %s branch of Submodule', this.submoduleVersion);

    var checkout = spawn('git', ['checkout', this.submoduleVersion]);

    checkout.stdout.on('data', function(data) {
      console.log(data);
    });

    checkout.on('close', function() {
      process.chdir('../');
      done();
    });

  });

thanks in advance.


Solution

  • If your code is run at the shell, then I believe using { stdio: 'inherit'} would fix it:

    var git = spawn('git', ['submodule', 'add', 
                            'git://github.com/PathTo/Submodule.git', 'submodule'], 
                    { stdio: 'inherit' });
    

    Explanation: through tests in a bash shell I've found that if git thinks it is not communicating with a tty, it does not output any progress information. If your software is run from the shell and your tell spawn to just pass stdin, stdout, and stderr to the child process, then the child should see a tty and output progress information.