2

I'm writing my own yeoman generator for a Flask-Bootstrap framework. After I create all of the directories, copy/template all of the files, I would ideally like to create a local git repo (which I'm able to do), then initialize a remote repository and push the first commit to it.

It get's to the point of creating the local repo, making the first local commit and adding the remote origin, but then gets hung up when trying to push to origin master.

On an aside, I haven't been able to find solid documentation on Yeoman's exec() function, would anyone be able to point me in the right direction regarding this.

input.js

...

git: function(){
  var done = this.async();

  // create git repository and start remote github repository
  exec('cd ' + this.appName + ' && git init', function(err, stdout) {
    console.log('Initialized local git repository\n', stdout);
  });

  exec('cd ' + this.appName + ' && curl -u ' + this.githubUsername + ' https://api.github.com/user/repos -d \'{"name":"' + this.appName + '"}\'', function(err, stdout) {
    console.log('Initialized remote github repository!\n', stdout);
  });

  exec('cd ' + this.appName + ' && git remote add origin git@github.com:' + this.githubUsername + '/' + this.appName + '.git', function(err, stdout) {
    console.log('Added github remote origin\n', stdout);
  });

  exec('cd ' + this.appName + ' && git add .', function(err, stdout) {
    console.log('Added all files to git staging area\n', stdout);
  });

  exec('cd ' + this.appName + ' && git commit -m "init commit"', function(err, stdout) {
    console.log('Made first commit!\n', stdout);
  });

  exec('cd ' + this.appName + ' && git push origin master', function(err, stdout) {
    console.log('Pushed first commit to github\n', stdout);
  });

  done();
},

...

Output

...

Initialized local git repository
Initialized empty Git repository in /Users/joey/Desktop/trippity/.git/
Made first commit!
Added all files to git staging area
Added github remote origin

//Never prints line corresponding to pushing to the remote 

...

I am referencing this SO question about creating a remote GitHub repo from the CLI.

Update

So I've tried using the GitHub API on a test repo to try creating a remote repo from the CLI. Once you use curl you are then prompted for your github password (which makes sense). I'm not seeing this prompt in the yeoman generator to enter my password which is likely why it is getting hung up, any ideas on how to show this prompt to the user?

joey@JoeyOrlandoMacBookAir:~/Desktop/trippity$ curl -u joeyorlando https://api.github.com/user/repos -d '{"name":"trippity"}'
Enter host password for user 'joeyorlando':
Community
  • 1
  • 1
Joey Orlando
  • 1,408
  • 2
  • 17
  • 33
  • I fail to see the point of using yeoman commands to use git. Why not use git commands directly? And let the user use svn or mercurial or whatever if he prefers to? – JB Nizet Aug 09 '15 at 16:45
  • I'm mostly interested in creating this generator for personal usage and I solely use git – Joey Orlando Aug 09 '15 at 16:59

1 Answers1

2

First off, let's make a clear distinction. What you're trying to do has nothing to do with Yeoman itself.

You want to create a git repository, commit and push to the remote. This will be done using Node.js apis and it'll work like any normal JavaScript code. Yeoman is not magic, it's only a framework with helpers to scaffold applications and making generators more easily reusable and allowing their distribution.

From what I can see, you have multiple bugs in your code:

First off, why are you always cding into the app directory? Once you're inside of it, you won't move out unless you tell it to do so.

Second, you're calling exec a bunch, and you provide callbacks. This should hint you to the fact that this API is asynchronous. This mean it won't run in order. And as a matter of fact, reading the log, you can see that files get added to the staging area after you tried to commit.

You'd probably had realized if you'd catch the exception (function (err, etc...) in your callbacks). But you're not, so you're not notified of any errors occurring.

So where to do from now? Well, maybe start by reading about asynchronous coding and patterns. Maybe you'll want to use async to orchestrate actions depending on each others. Next up, using exec commands is pretty raw and not super maintainable. There's multiple node.js tools to work with git. You should checkout nodegit (there's also plenty other options on NPM).

Simon Boudrias
  • 42,953
  • 16
  • 99
  • 134