I would like to git pull, commit and push from nodeJS with child_process - is this suppose to work?
var cmd = require('child_process');
var commmandString = "cd c:\\xampp\\htdocs\\MenuMakerServer\\experiments\\editormenu && git commit -am 'menu.json changes' && git push origin main";
cmd.exec(commmandString , function (error: any, stdout, stderr) {
if (error) {
callback(error.stack, null);
}
});
EDIT:
OK, I managed to get this to work:
var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout) }
var options = {cwd:"c:\\xampp\\htdocs\\MenuMakerServer\\projects\\editormenu"};
exec("git status && git pull && git commit -am 'menu changed' && git push", options, puts);
Define a node.js module something like below code.
exports.series = function(cmds, callback){
var execNext = function(){
exports.exec(cmds.shift(), function(error){
if (error) {
callback(error);
} else {
if (cmds.length) execNext();
else callback(null);
}
});
};
execNext();
};
Then you can run it:
myProcessor.series([
'cd c:\\xampp\\htdocs\\MenuMakerServer\\experiments\\editormenu'
'git commit -am "menu.json changes"',
'git push origin main '
], function(err){
console.log('executed many commands in a row');
});
NOTE: Here myProcessor is the require variable name (somethig like var myProcessor = require('./path/to/above/code/file');) for the above code snippet.
No that won't work... it looks like you are combining both DOS shell commands and Unix shell commands. Specifically c:\ is DOS and using && to chain commands is Unix shell. Which environment are you using?
If you are using DOS then you need make a .bat and call the batch. This is nice becasuse you can use parameters.