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

Merge branch 'pr_2908' into dev

This commit is contained in:
Nick O'Leary 2021-03-31 23:50:30 +01:00
commit f0f2eefb59
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
3 changed files with 58 additions and 15 deletions

View File

@ -21,8 +21,8 @@
</div> </div>
<div class="form-row"> <div class="form-row">
<label><i class="fa fa-plus"></i> <span data-i18n="exec.label.append"></span></label> <label><i class="fa fa-plus"></i> <span data-i18n="exec.label.append"></span></label>
<input type="checkbox" id="node-input-addpay" style="display:inline-block; width:auto; vertical-align:top;"> <input type="checkbox" id="node-input-addpay-cb" style="display:inline-block; width:auto;">
&nbsp;msg.payload <input type="text" id="node-input-addpay" style="margin-left: 5px; width:160px;">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-append"> </label> <label for="node-input-append"> </label>
@ -52,7 +52,7 @@
color:"darksalmon", color:"darksalmon",
defaults: { defaults: {
command: {value:""}, command: {value:""},
addpay: {value:false}, addpay: {value:""},
append: {value:""}, append: {value:""},
useSpawn: {value:"false"}, useSpawn: {value:"false"},
timer: {value:""}, timer: {value:""},
@ -79,6 +79,24 @@
if ($("#node-input-useSpawn").val() === null) { if ($("#node-input-useSpawn").val() === null) {
$("#node-input-useSpawn").val(this.useSpawn.toString()); $("#node-input-useSpawn").val(this.useSpawn.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-addpay").val(addpayValue);
$("#node-input-addpay").typedInput({
default: "msg",
types: ["msg"]
});
$("#node-input-addpay-cb").trigger("change")
},
oneditsave: function() {
if (!$("#node-input-addpay-cb").prop("checked")) {
$("#node-input-addpay").val("");
}
} }
}); });
</script> </script>

View File

@ -26,6 +26,9 @@ module.exports = function(RED) {
this.cmd = (n.command || "").trim(); this.cmd = (n.command || "").trim();
if (n.addpay === undefined) { n.addpay = true; } if (n.addpay === undefined) { n.addpay = true; }
this.addpay = n.addpay; this.addpay = n.addpay;
if (this.addpay === true) {
this.addpay = "payload";
}
this.append = (n.append || "").trim(); this.append = (n.append || "").trim();
this.useSpawn = (n.useSpawn == "true"); this.useSpawn = (n.useSpawn == "true");
this.timer = Number(n.timer || 0)*1000; this.timer = Number(n.timer || 0)*1000;
@ -61,12 +64,17 @@ module.exports = function(RED) {
} }
else { else {
var child; var child;
// make the extra args into an array
// then prepend with the msg.payload
var arg = node.cmd;
if (node.addpay) {
var value = RED.util.getMessageProperty(msg, node.addpay);
if (value !== undefined) {
arg += " " + value;
}
}
if (node.append.trim() !== "") { arg += " " + node.append; }
if (this.useSpawn === true) { if (this.useSpawn === true) {
// make the extra args into an array
// then prepend with the msg.payload
var arg = node.cmd;
if ((node.addpay === true) && msg.hasOwnProperty("payload")) { arg += " "+msg.payload; }
if (node.append.trim() !== "") { arg += " "+node.append; }
// slice whole line by spaces and removes any quotes since spawn can't handle them // slice whole line by spaces and removes any quotes since spawn can't handle them
arg = arg.match(/(?:[^\s"]+|"[^"]*")+/g).map((a) => { arg = arg.match(/(?:[^\s"]+|"[^"]*")+/g).map((a) => {
if (/^".*"$/.test(a)) { if (/^".*"$/.test(a)) {
@ -126,12 +134,9 @@ module.exports = function(RED) {
}); });
} }
else { else {
var cl = node.cmd;
if ((node.addpay === true) && msg.hasOwnProperty("payload")) { cl += " "+msg.payload; }
if (node.append.trim() !== "") { cl += " "+node.append; }
/* istanbul ignore else */ /* istanbul ignore else */
if (RED.settings.verbose) { node.log(cl); } if (RED.settings.verbose) { node.log(arg); }
child = exec(cl, node.execOpt, function (error, stdout, stderr) { child = exec(arg, node.execOpt, function (error, stdout, stderr) {
var msg2, msg3; var msg2, msg3;
delete msg.payload; delete msg.payload;
if (stderr) { if (stderr) {

View File

@ -41,7 +41,7 @@ describe('exec node', function() {
n1.should.have.property("name", "exec1"); n1.should.have.property("name", "exec1");
n1.should.have.property("cmd", ""); n1.should.have.property("cmd", "");
n1.should.have.property("append", ""); n1.should.have.property("append", "");
n1.should.have.property("addpay",true); n1.should.have.property("addpay","payload");
n1.should.have.property("timer",0); n1.should.have.property("timer",0);
n1.should.have.property("oldrc","false"); n1.should.have.property("oldrc","false");
done(); done();
@ -114,8 +114,28 @@ describe('exec node', function() {
}); });
}); });
it('should exec a simple command with appended value from message', function (done) {
var flow = [{id:"n1", type:"exec", wires:[["n2"]], command:"echo", addpay:"topic", append:"more", oldrc:"false"},
{id:"n2", type:"helper"}];
helper.load(execNode, flow, function () {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function (msg) {
try {
msg.should.have.property("payload");
msg.payload.should.be.a.String();
msg.payload.should.equal("bar more\n");
done();
} catch(err) {
done(err)
}
});
n1.receive({payload:"foo", topic:"bar"});
});
});
it('should exec a simple command with extra parameters', function(done) { it('should exec a simple command with extra parameters', function(done) {
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:"more", oldrc:"false"}, var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:"payload", append:"more", oldrc:"false"},
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}]; {id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
var spy = sinon.stub(child_process, 'exec', var spy = sinon.stub(child_process, 'exec',
function(arg1, arg2, arg3, arg4) { function(arg1, arg2, arg3, arg4) {