From fb4f515c6e663ae887211bab433547b2a5ae1428 Mon Sep 17 00:00:00 2001 From: Gerrit Riessen Date: Fri, 13 Sep 2024 16:12:55 +0200 Subject: [PATCH] changes to add stdin support to the exec node --- .../nodes/core/function/90-exec.html | 12 +++++++ .../@node-red/nodes/core/function/90-exec.js | 32 +++++++++++++++++-- .../nodes/locales/en-US/messages.json | 8 +++-- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/function/90-exec.html b/packages/node_modules/@node-red/nodes/core/function/90-exec.html index 137807d2f..1042a7808 100644 --- a/packages/node_modules/@node-red/nodes/core/function/90-exec.html +++ b/packages/node_modules/@node-red/nodes/core/function/90-exec.html @@ -23,6 +23,11 @@ + +
@@ -57,6 +62,7 @@ defaults: { command: {value:""}, addpay: {value:"", validate: RED.validators.typedInput({ type: 'msg', allowBlank: true })}, + addpayTo: { value: "parameter" }, append: {value:""}, useSpawn: {value:"false"}, timer: {value:""}, @@ -84,10 +90,16 @@ if ($("#node-input-useSpawn").val() === null) { $("#node-input-useSpawn").val(this.useSpawn.toString()); } + + if ($("#node-input-addpayTo").val() === null) { + $("#node-input-addpayTo").val(this.addpayTo.toString()); + } + $("#node-input-addpay-cb").prop("checked", this.addpay === true || (this.addpay !== false && this.addpay !== "")) var addpayValue = (this.addpay === true)?"payload":((this.addpay === false || this.addpay === "")?"payload":this.addpay); $("#node-input-addpay-cb").on("change", function(evt) { $("#node-input-addpay").typedInput("disable",!$("#node-input-addpay-cb").prop("checked")); + $("#node-input-addpayTo").prop("disabled", !$("#node-input-addpay-cb").prop("checked")); }); $("#node-input-addpay").val(addpayValue); diff --git a/packages/node_modules/@node-red/nodes/core/function/90-exec.js b/packages/node_modules/@node-red/nodes/core/function/90-exec.js index 70aec8d2b..df418a6ee 100644 --- a/packages/node_modules/@node-red/nodes/core/function/90-exec.js +++ b/packages/node_modules/@node-red/nodes/core/function/90-exec.js @@ -20,6 +20,8 @@ module.exports = function(RED) { var exec = require('child_process').exec; var fs = require('fs'); var isUtf8 = require('is-utf8'); + var stream = require('stream') + const isWindows = process.platform === 'win32' function ExecNode(n) { @@ -30,6 +32,12 @@ module.exports = function(RED) { if (this.addpay === true) { this.addpay = "payload"; } + + this.addpayTo = n.addpayTo; + if ( this.addpayTo === undefined ) { + this.addpayTo = "parameter" + } + this.append = (n.append || "").trim(); this.useSpawn = (n.useSpawn == "true"); this.timer = Number(n.timer || 0)*1000; @@ -69,7 +77,7 @@ module.exports = function(RED) { // make the extra args into an array // then prepend with the msg.payload var arg = node.cmd; - if (node.addpay) { + if (node.addpay && node.addpayTo == "parameter") { var value = RED.util.getMessageProperty(msg, node.addpay); if (value !== undefined) { arg += " " + value; @@ -87,7 +95,7 @@ module.exports = function(RED) { }); var cmd = arg.shift(); // Since 18.20.2/20.12.2, it is invalid to call spawn on Windows with a .bat/.cmd file - // without using shell: true. + // without using shell: true. const opts = isWindows ? { ...node.spawnOpt, shell: true } : node.spawnOpt /* istanbul ignore else */ node.debug(cmd+" ["+arg+"]"); @@ -137,6 +145,16 @@ module.exports = function(RED) { node.error(code,RED.util.cloneMessage(msg)); } }); + + if (node.addpay && node.addpayTo == "stdin") { + var value = RED.util.getMessageProperty(msg, node.addpay); + if (value !== undefined) { + var stdinStream = new stream.Readable(); + stdinStream.push(value); // Add data to the internal queue for users of the stream to consume + stdinStream.push(null); // Signals the end of the stream (EOF) + stdinStream.pipe(child.stdin); + } + } } else { /* istanbul ignore else */ @@ -181,6 +199,16 @@ module.exports = function(RED) { child.tout = setTimeout(function() { cleanup(child.pid); }, node.timer); } node.activeProcesses[child.pid] = child; + + if (node.addpay && node.addpayTo == "stdin") { + var value = RED.util.getMessageProperty(msg, node.addpay); + if (value !== undefined) { + var stdinStream = new stream.Readable(); + stdinStream.push(value); // Add data to the internal queue for users of the stream to consume + stdinStream.push(null); // Signals the end of the stream (EOF) + stdinStream.pipe(child.stdin); + } + } } } }); diff --git a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json index bc89992e2..01fcf1779 100644 --- a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json @@ -233,14 +233,18 @@ "stdout": "stdout", "stderr": "stderr", "retcode": "return code", - "winHide": "Hide console" + "winHide": "Hide console", + "as": "as" + }, "placeholder": { "extraparams": "extra input parameters" }, "opt": { "exec": "when the command is complete - exec mode", - "spawn": "while the command is running - spawn mode" + "spawn": "while the command is running - spawn mode", + "parameter": "parameter", + "stdin": "stdin" }, "oldrc": "Use old style output (compatibility mode)" },