Reputation: 199
I'm executing a few git commands in sequence, in particular, I'm following a git flow model,
e.g. when finishing a feature, I run,
git pull
git push -u origin branchname
git pull origin develop
git checkout develop
git merge branchname
git push
I run these commands using the exec
module in node.js and I chain them one after another, like:
exec(command1,
(err, stdout, stderr) => {
if(err != null) {
//display an error message
return
} else {
exec(command2, (err, stdout, stderr) =>{ ... }
}
}
)
And so on. The outputs are working correctly and the order is working. However, if one of those commands fail, I am breaking out of the chain.
I know that there is an async library that I could use with the same effect, per here. However, is this the best possible way to do that without resorting to a third-party library? How have other people done it?
Upvotes: 0
Views: 1741
Reputation: 665455
However, is this the best possible way to do that without resorting to a third-party library?
No.
How have other people done it?
There are three kinds of other solutions:
use promises! (And partial application)
function makeExec(command) {
return function(/* ignore results */) {
return new Promise(function(resolve, reject) {
child_process.exec(command, function(err, stdout, stderr) {
if (err != null) reject(Object.assign(err, {stderr}));
else resolve(stdout);
});
});
});
}
Promise.resolve()
.then(makeExec("git pull"))
.then(makeExec("git push -u origin branchname"))
.then(makeExec("git pull origin develop"))
.then(makeExec("git checkout develop"))
.then(makeExec("git merge branchname"))
.then(makeExec("git push"))
.catch(function(err) {
//display an error message
})
Upvotes: 1
Reputation: 211740
Using async
you can do this with eachSeries
:
var commands = [
'git pull',
'git checkout',
'...'
];
eachSeries(commands, (command, cb) => {
exec(command, (err, stdout, stderr) => {
cb(err);
});
});
It's generally better to find a library that does this than to have to wrangle your own solution by hand.
Upvotes: 1