git - Yeoman 生成器 - 创建远程 github 存储库

标签 git github yeoman yeoman-generator

我正在为 Flask-Bootstrap 框架编写自己的 yeoman 生成器。创建所有目录、复制/模板化所有文件后,理想情况下我希望创建一个本地 git 存储库(我可以做到),然后初始化远程存储库并将第一次提交推送到它。

它已经到了创建本地存储库、进行第一次本地提交并添加远程源的地步,但在尝试推送到源主机时却挂起了。

顺便说一句,我还没有找到有关 Yeoman 的 exec() 函数的可靠文档,任何人都可以为我指出正确的方向。

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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="47202e3307202e332f32256924282a" rel="noreferrer noopener nofollow">[email protected]</a>:' + 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();
},

...

输出

...

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 

...

我正在引用this关于从 CLI 创建远程 GitHub 存储库的问题。

更新

因此,我尝试在测试存储库上使用 GitHub API 来尝试从 CLI 创建远程存储库。一旦你使用了curl,系统就会提示你输入你的github密码(这是有道理的)。我在 yeoman 生成器中没有看到输入密码的提示,这可能是它挂起的原因,关于如何向用户显示此提示的任何想法?

joey@JoeyOrlandoMacBookAir:~/Desktop/trippity$ curl -u joeyorlando https://api.github.com/user/repos -d '{"name":"trippity"}'
Enter host password for user 'joeyorlando':

最佳答案

首先,让我们明确区分。您想要做的事情与 Yeoman 本身无关。

您想要创建一个 git 存储库,提交并推送到远程。这将使用 Node.js api 来完成,并且它将像任何正常的 JavaScript 代码一样工作。 Yeoman 并不神奇,它只是一个框架,带有帮助程序来搭建应用程序并使生成器更容易重用并允许其分发。

据我所知,您的代码中有多个错误:

首先,为什么你总是cd进入应用程序目录?一旦你进入其中,除非你告诉它这样做,否则你将不会离开。

其次,您多次调用exec,并提供回调。这应该会提示您这个 API 是异步的。这意味着它不会按顺序运行。事实上,阅读日志,您可以看到在您尝试提交后文件被添加到暂存区域。

您可能已经意识到是否会捕获异常(回调中的function (err, etc...))。但您没有,因此您不会收到发生任何错误的通知。

那么现在该做什么呢?好吧,也许可以从阅读异步编码和模式开始。也许您会想使用 async协调相互依赖的行动。接下来,使用 exec 命令非常原始并且不易于维护。有多种 Node.js 工具可以与 git 配合使用。您应该 checkout nodegit (NPM 上还有很多其他选项)。

关于git - Yeoman 生成器 - 创建远程 github 存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31906031/

相关文章:

node.js - 使用 npm 开关在 Windows 下运行 Yeoman Generator

git - 在 git 中更改较旧提交中的小错误

git - IntelliJ 和 Git 分支名称

c++ - OpenCV 3.0.0 使用 FFMPEG 出错

github - 版本控制: How does forking a repository work on source code hosting facilities?

github - 如何将文件添加到 github 上的新存储库?

Github 和 Git - 在 win7 中使用 git 终端需要什么?

git 获取以前的文件版本时出现问题

javascript - Grunt 在 cdnify 上失败并出现警告 : "Warning: Arguments to path.join must be strings"?

node.js - openshift 上的 socket.io 和 Angular-fullstack