在yeoman生成器中向git仓库推送代码?

9
我希望有人能够澄清一下Yeoman的spawnCommand()方法是如何工作的。我正在开发一个生成器,希望它在结束时初始化git repo并提交和推送生成的应用程序。
我认为第二个参数是一个数组,其中包含要在进程下运行的命令。因此,类似这样的内容将运行“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/

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

是否有其他方法使生成器推送到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'.

开始觉得这可能不是最好的做法。

1个回答

11

每运行一个 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生成器,使用同步命令通常已经足够了。


这个程序的输出抛出了一堆错误。命令似乎也没有按顺序运行? - Josh Vickerson
编辑了我的答案并添加了更多细节。 - Simon Boudrias
谢谢你的澄清。我无法使用async.series,尽管它似乎是最干净的解决方案。我最终使用回调完成了代码,像这样:this.spawnCommand('git', ['command']).on('close', function( ) { });虽然不是很干净,但考虑到步骤数量是固定的,这种方法看起来还可以接受。感谢您的帮助! - Josh Vickerson
对于同步命令,您可以使用“spawnCommandSync()”函数 - http://yeoman.io/generator/actions_spawn_command.html - Ivan Novakov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接