Tuesday, 18 April 2017

Awkward string passing into shell by nodejs

I've been using a script lib written by millermedeiros (thanks to him) to execute multiple shell commands synchronously in nodejs.

But interestingly whatever I've tried, I could not pass a string containing white spaces.

So, here is the lib code (./shellHelper.js):

exports.series = function(cmds, cb){
var execNext = function(){
    exports.exec(cmds.shift(), function(err){
        if (err) {
            cb(err);
        } else {
            if (cmds.length) execNext();
            else cb(null);
        }
    });
};
execNext();
};

And here is my code (./gitcommit.js):

var shell = require('./shellHelper');
...
var regex = '/\s([A-Z0-9])\w+/g';
var message = input.replace(regex); //input string is coming from prompt module
...
var command1 = 'git add .';
var command2 = 'git commit -m \"' + message + '\"';
var command3 = 'git push origin master';
...
shell.series([ command1, command2, command3], function(err){
   console.log('executed many commands in a row');
   console.log(command2);
  });
});

It does work, unless I type a message with spaces. Console stdout is:

$ ..\nodejs (master)
$ node gitcommit.js
Enter commit message :
prompt: name:  something contains white spaces
error: pathspec 'contains' did not match any file(s) known to git.
error: pathspec 'white' did not match any file(s) known to git.
error: pathspec 'spaces"' did not match any file(s) known to git.
executed many commands in a row
git commit -m "something contains white spaces"



via CagCak

No comments:

Post a Comment