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

Fixes to spawn behaviour - (well more to catching bad behaviour)

It needs a redo - but patched so it doesn't die horribly.
This commit is contained in:
Dave C-J 2014-04-06 23:25:40 +01:00
parent 789d64f7ed
commit 61285a0ee8
2 changed files with 28 additions and 21 deletions

View File

@ -32,6 +32,7 @@
<label for="node-input-name"><i class="icon-tag"></i> Name</label> <label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
</div> </div>
<div class="form-tips">Tip: <i>spawn</i> expects only one command word - and appended args to be comma separated.</div>
</script> </script>
<script type="text/x-red" data-help-name="exec"> <script type="text/x-red" data-help-name="exec">
@ -39,6 +40,7 @@
<p>Provides 3 outputs... stdout, stderr, and return code.</p> <p>Provides 3 outputs... stdout, stderr, and return code.</p>
<p>By default uses exec() which calls the command, blocks while waiting for completion, and then returns the complete result in one go, along with any errors.</p> <p>By default uses exec() which calls the command, blocks while waiting for completion, and then returns the complete result in one go, along with any errors.</p>
<p>Optionally can use spawn() instead, which returns output from stdout and stderr as the command runs (ie one line at a time). On completion it then returns a return code (on the 3rd output).</p> <p>Optionally can use spawn() instead, which returns output from stdout and stderr as the command runs (ie one line at a time). On completion it then returns a return code (on the 3rd output).</p>
<p>Spawn only expect one command word, with all extra parameters to be comma separated and passed as the append.</p>
<p>The optional append gets added to the command after the <b>msg.payload</b> - so you can do things like pipe the result to another command.</p> <p>The optional append gets added to the command after the <b>msg.payload</b> - so you can do things like pipe the result to another command.</p>
</script> </script>

View File

@ -21,8 +21,8 @@ var exec = require('child_process').exec;
function ExecNode(n) { function ExecNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.cmd = n.command; this.cmd = n.command.trim();
this.append = n.append || ""; this.append = n.append.trim() || "";
this.useSpawn = n.useSpawn; this.useSpawn = n.useSpawn;
var node = this; var node = this;
@ -32,18 +32,21 @@ function ExecNode(n) {
if (this.useSpawn == true) { if (this.useSpawn == true) {
// make the extra args into an array // make the extra args into an array
// then prepend with the msg.payload // then prepend with the msg.payload
var arg = node.append.split(","); if (typeof(msg.payload !== "string")) { msg.payload = msg.payload.toString(); }
if (msg.payload != " ") { arg.unshift(msg.payload); } var arg = [];
node.log(node.cmd+" "+arg); if (node.append.length > 0) { arg = node.append.split(","); }
if (msg.payload.trim() != "") { arg.unshift(msg.payload); }
node.log(node.cmd+" ["+arg+"]");
if (node.cmd.indexOf(" ") == -1) {
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; msg.payload = data.toString();
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; msg.payload = data.toString();
node.send([null,msg,null]); node.send([null,msg,null]);
}); });
ex.on('close', function (code) { ex.on('close', function (code) {
@ -52,6 +55,8 @@ function ExecNode(n) {
node.send([null,null,msg]); node.send([null,null,msg]);
}); });
} }
else { node.error("Spawn command must be just the command - no spaces or extra parameters"); }
}
else { else {
var cl = node.cmd+" "+msg.payload+" "+node.append; var cl = node.cmd+" "+msg.payload+" "+node.append;