我希望有人能够澄清一下Yeoman的spawnCommand()方法是如何工作的。我正在开发一个生成器,希望它在结束时初始化git repo并提交和推送生成的应用程序。
我认为第二个参数是一个数组,其中包含要在进程下运行的命令。因此,类似这样的内容将运行“git init”,然后是“git remote add origin”等等。
输出结果对我来说有些不太理解:
我认为第二个参数是一个数组,其中包含要在进程下运行的命令。因此,类似这样的内容将运行“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'.
开始觉得这可能不是最好的做法。