git - 在 yeoman 生成器中推送到 git repo?

标签 git yeoman yeoman-generator

我希望有人能稍微解释一下 Yeoman 的 spawnCommand() 方法是如何工作的。我正在开发一个生成器,我希望它能初始化一个 git 存储库,最后提交并推送生成的应用程序。

我的印象是第二个参数是一个数组,它是一个将在进程下运行的命令的数组。因此,类似这样的操作会运行“git init”,然后运行“git remote add origin”等。

end: function () {
    if(this.repo !== '') {
      this.spawnCommand('git', ['init', 
        'remote add origin ' + this.repo, 
        'add --all', 
        'commit -m "initial commit from generator"', 
        'push -u origin master']
      );
    }
    console.log(yosay('I believe we\'re done here.'));
}

不幸的是,它只是抛出一个使用错误:

usage: git init [-q | --quiet] [--bare] ...

然后我尝试自己执行 init,然后像这样执行其他操作:

end: function () {
    if(this.repo !== '') {
      this.spawnCommand('git', ['init']);
      this.spawnCommand('git', ['remote add origin ' + this.repo, 
        'add --all', 
        'commit -m "initial commit from generator"', 
        'push -u origin master']
      );
    }
    console.log(yosay('I believe we\'re done here.'));
}

然后输出对我来说意义不大:

git: 'remote add origin {URL}' is not a git command. See 'git --help'.
Initialized empty Git repository in /my/project/.git/

这让我觉得它们是异步运行的,这可能是添加远程源失败的原因,但除此之外我很困惑。

是否有另一种方法让生成器推送到 git,或者我最好不要尝试自动执行初始推送?

编辑:

将每个命令作为其自己的 spawnCommand() 运行也不起作用。

this.spawnCommand('git', ['init']);
this.spawnCommand('git', ['remote add origin ', this.repo]);
this.spawnCommand('git', ['add --all']);
this.spawnCommand('git', ['commit -m "initial commit from generator"']);
this.spawnCommand('git', ['push -u origin master']);

输出:

error: invalid key: pager.remote add origin 
error: invalid key: pager.add --all
error: invalid key: alias.remote add origin
error: invalid key: alias.add --all
error: invalid key: pager.commit -m "initial commit from generator"
error: invalid key: pager.push -u origin master
Initialized empty Git repository in /my/project/.git/
error: invalid key: alias.commit -m "initial commit from generator"
error: invalid key: alias.push -u origin master
git: 'remote add origin ' is not a git command. See 'git --help'.
git: 'push -u origin master' is not a git command. See 'git --help'.
git: 'add --all' is not a git command. See 'git --help'.
git: 'commit -m "initial commit from generator"' is not a git command. See 'git --help'.

开始认为这可能不是最好的方法。

最佳答案

每个要运行的 git 命令使用一个 this.spawnCommandSync()

this.spawnCommandSync('git', ['init']);
this.spawnCommandSync('git', ['remote', 'add', 'origin', this.repo]);
this.spawnCommandSync('git', ['add', '--all']);
this.spawnCommandSync('git', ['commit', '-m', '"initial commit from generator"']);
this.spawnCommandSync('git', ['push', '-u', 'origin', 'master']);

数组是字符串参数的数组。这是与 node.js spawn 相同的界面- 除了为 Windows 支持包装 cross-spawn 之外,没有其他魔法。

this.spawnCommand() 是异步的,所以如果你使用它,你需要控制流程,这样命令就不会同时运行并且可能出错命令。考虑到这是一个 yeoman 生成器,使用同步命令通常就足够了。

关于git - 在 yeoman 生成器中推送到 git repo?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29832785/

相关文章:

git - 如何查看提交中的文件内容——比如 `git show`

node.js - `yo angular` 给出错误 : npm ERR! 代码 ENOENT npm ERR! errno 34(是的,我已经清理了缓存并设置了 .npmignore)

angularjs - Yeoman grunt复制整个bower_component文件夹以进行分发

javascript - 如何使用 Yeoman 检查目录是否存在?

javascript - 生成器在 async() 调用后退出

ruby-on-rails - gem.add_dependency Jeweler 如何格式化 git 源代码?

java - 如何将 Eclipse 项目上传到 GitHub?

git - 在git中更改分支的根

windows - Yeoman 命令无法在 Windows 7 中运行

javascript - 咕噜服务 :dist not working with Backbone Generator RequireJS