javascript - Node spawn process and store output -


this part of script trying spawn child going clone hard drive. works issue having when encounter error , want store output, stores first line of output, excluding things need. have run command outside of script , gives me 2 lines of output, second line being error if fails. so, how store entire output. appreciated, thank you!

ntfspartition.prototype.writefs = function(filename, progress, success, error) {     console.log('writing ntfs fs');     var s = spawn("ntfsclone", ['--restore-image', '--overwrite', this.dev, filename]);     var err_msg = '';     s.on('error', function(err) {         err_msg = err;     });     s.stderr.on('data', function(data) {         err_msg += data.tostring();     });     s.stdout.on('data', function(data) {         var match = data.tostring().match(kntfswritefsprogressregex);         if(!match) {             return;         }         progress(match[1]);     });     s.on('exit', function(code) {         if(code != 0) {             console.log(err_msg);             return error('error: ' + code + ' - ' + err_msg);         }         success();     }); } 

to answer question, can't test this, suspect removing s.stderr.on('data', ...) handler allow ensure err_msg error object.

also taking note of this warning:

note: 'exit' event may or may not fire after error has occurred. when listening both 'exit' , 'error' events, important guard against accidentally invoking handler functions multiple times.

i see possible solution looking this:

ntfspartition.prototype.writefs = function(filename, progress, success, error) {     console.log('writing ntfs fs');     var s = spawn("ntfsclone", ['--restore-image', '--overwrite', this.dev, filename]);     var errors = [];     // if possible, might multiple of these     // if not, still works single error     s.on('error', function(err) {         errors.push(err)     });     s.stdout.on('data', function(data) {         var match = data.tostring().match(kntfswritefsprogressregex);         if(!match) {             return;         }         progress(match[1]);     });     // guaranteed called, whereas 'exit' not(?)     s.on('close', function(code) {         if(code != 0) {             var stacks = errors.map(function (err) {               return err.stack;             });             // errors             return error('error: ' + code + '\n\n' + stacks.join('\n\n'))         }         success();     }); } 

one of keys use error.stack property, since errors typically tend log message property instead default, when coerced string. property single-line feedback getting in code, since never checked err_msg.stack.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -