1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Update exec node to handle binary stdout

This commit is contained in:
dceejay 2015-01-07 21:12:01 +00:00
parent 25537e01d4
commit 00429ebe70

View File

@ -18,6 +18,7 @@ module.exports = function(RED) {
"use strict"; "use strict";
var spawn = require('child_process').spawn; var spawn = require('child_process').spawn;
var exec = require('child_process').exec; var exec = require('child_process').exec;
var isUtf8 = require('is-utf8');
function ExecNode(n) { function ExecNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
@ -40,12 +41,14 @@ module.exports = function(RED) {
var ex = spawn(node.cmd,arg); var ex = spawn(node.cmd,arg);
ex.stdout.on('data', function (data) { ex.stdout.on('data', function (data) {
//console.log('[exec] stdout: ' + data); //console.log('[exec] stdout: ' + data);
msg.payload = data.toString(); if (isUtf8(data)) { msg.payload = data.toString(); }
else { msg.payload = data; }
node.send([msg,null,null]); node.send([msg,null,null]);
}); });
ex.stderr.on('data', function (data) { ex.stderr.on('data', function (data) {
//console.log('[exec] stderr: ' + data); //console.log('[exec] stderr: ' + data);
msg.payload = data.toString(); if (isUtf8(data)) { msg.payload = data.toString(); }
else { msg.payload = new Buffer(data); }
node.send([null,msg,null]); node.send([null,msg,null]);
}); });
ex.on('close', function (code) { ex.on('close', function (code) {
@ -63,7 +66,7 @@ module.exports = function(RED) {
else { else {
var cl = node.cmd+" "+msg.payload+" "+node.append; var cl = node.cmd+" "+msg.payload+" "+node.append;
if (RED.settings.verbose) { node.log(cl); } if (RED.settings.verbose) { node.log(cl); }
var child = exec(cl, function (error, stdout, stderr) { var child = exec(cl, {encoding: 'binary'}, function (error, stdout, stderr) {
msg.payload = stdout; msg.payload = stdout;
var msg2 = {payload:stderr}; var msg2 = {payload:stderr};
var msg3 = null; var msg3 = null;