Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

This is related to Running shell script in rails only outputs the 1 line, but is seems like the --progress option described there isn't supported by git submodule. You should be able to get the desired progress output by letting the Git process inherit the stderr file descriptor from the current process (might as well throw in stdout at the same time), which according to the child_process documentation can be done like this:

var git = spawn(
  'git', 
  ['submodule', 'add', 'git://github.com/PathTo/Submodule.git', 'submodule'],
  { stdio: ['pipe', process.stdout, process.stderr] );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top