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:
parent
789d64f7ed
commit
61285a0ee8
@ -32,6 +32,7 @@
|
||||
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
|
||||
<input type="text" id="node-input-name" placeholder="Name">
|
||||
</div>
|
||||
<div class="form-tips">Tip: <i>spawn</i> expects only one command word - and appended args to be comma separated.</div>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="exec">
|
||||
@ -39,6 +40,7 @@
|
||||
<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>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>
|
||||
</script>
|
||||
|
||||
|
@ -21,8 +21,8 @@ var exec = require('child_process').exec;
|
||||
|
||||
function ExecNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
this.cmd = n.command;
|
||||
this.append = n.append || "";
|
||||
this.cmd = n.command.trim();
|
||||
this.append = n.append.trim() || "";
|
||||
this.useSpawn = n.useSpawn;
|
||||
|
||||
var node = this;
|
||||
@ -32,25 +32,30 @@ function ExecNode(n) {
|
||||
if (this.useSpawn == true) {
|
||||
// make the extra args into an array
|
||||
// then prepend with the msg.payload
|
||||
var arg = node.append.split(",");
|
||||
if (msg.payload != " ") { arg.unshift(msg.payload); }
|
||||
node.log(node.cmd+" "+arg);
|
||||
var ex = spawn(node.cmd,arg);
|
||||
ex.stdout.on('data', function (data) {
|
||||
//console.log('[exec] stdout: ' + data);
|
||||
msg.payload = data;
|
||||
node.send([msg,null,null]);
|
||||
});
|
||||
ex.stderr.on('data', function (data) {
|
||||
//console.log('[exec] stderr: ' + data);
|
||||
msg.payload = data;
|
||||
node.send([null,msg,null]);
|
||||
});
|
||||
ex.on('close', function (code) {
|
||||
//console.log('[exec] result: ' + code);
|
||||
msg.payload = code;
|
||||
node.send([null,null,msg]);
|
||||
});
|
||||
if (typeof(msg.payload !== "string")) { msg.payload = msg.payload.toString(); }
|
||||
var 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);
|
||||
ex.stdout.on('data', function (data) {
|
||||
//console.log('[exec] stdout: ' + data);
|
||||
msg.payload = data.toString();
|
||||
node.send([msg,null,null]);
|
||||
});
|
||||
ex.stderr.on('data', function (data) {
|
||||
//console.log('[exec] stderr: ' + data);
|
||||
msg.payload = data.toString();
|
||||
node.send([null,msg,null]);
|
||||
});
|
||||
ex.on('close', function (code) {
|
||||
//console.log('[exec] result: ' + code);
|
||||
msg.payload = code;
|
||||
node.send([null,null,msg]);
|
||||
});
|
||||
}
|
||||
else { node.error("Spawn command must be just the command - no spaces or extra parameters"); }
|
||||
}
|
||||
|
||||
else {
|
||||
|
Loading…
Reference in New Issue
Block a user