在对问题进行一些挖掘之后 git.clone(repoURL, localPath, options, handlerFn());
,您必须将引用传递给函数而不是实际的回调,就像这样git.clone(repoURL, localPath, options, handlerFn);
。
完整的实现如下:
const git = require('simple-git')();
const fs = require('fs')
const url = require('url');
this.gitURL = 'https://github.com/torvalds/linux.git';
const localURL = url.parse(this.gitURL);
const localRepoName = (localURL.hostname + localURL.path)
.replace('com', '')
.replace('/', '')
.replace('/', '.')
.replace('.git', '')
this.localPath = `./${localRepoName}`;
this.options = ['--depth', '1'];
this.callback = () => {
console.log('DONE')
}
if (fs.existsSync(this.localPath)) {
// something
} else {
git.outputHandler((command, stdout, stderr) => {
stdout.pipe(process.stdout);
stderr.pipe(process.stderr)
stdout.on('data', (data) => {
// Print data
console.log(data.toString('utf8'))
})
})
.clone(this.gitURL, this.localPath, this.options, this.callback)
}