I'm trying to write a little module that uses child_process.spawn to clone a git repo and return a Promise but it fails on me. When I'm using spawnSync it works.
Here is the sync code that works.
import {spawnSync} from 'child_process';
export default async function clone(options: { url: string; path: string; }) {
const url = options.url;
const target = options.path;
const args = ['clone', url, target];
return spawnSync('git', args);
}
This is the async code that fails and returns undefined
import {spawn} from 'child_process';
export default async function clone(options: { url: string; path: string; }) {
const url = options.url;
const target = options.path;
const args = ['clone', url, target];
const process = spawn('git', args);
process.on('close', function () {
return new Promise(function (resolve, reject) {
process.addListener('error', reject);
process.addListener('exit', resolve);
});
});
}
I also tried:
process.on('close', function (code) {
return code;
});
or
process.on('close', function (code) {
return Promise.resolve(code);
});
and several other things like …on('exit', function(code){
return code
})
Any suggestions how to do this?