mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Merge 0c15a8e38286d1b86bebeaaf63ee286ae32fee14 into 2feb290ae3c6cd88c16e4c27c2006a569e0146e2
This commit is contained in:
commit
16c3d53137
@ -18,6 +18,8 @@
|
|||||||
<div class="form-row node-input-filename">
|
<div class="form-row node-input-filename">
|
||||||
<label for="node-input-files"><i class="fa fa-file"></i> <span data-i18n="watch.label.files"></span></label>
|
<label for="node-input-files"><i class="fa fa-file"></i> <span data-i18n="watch.label.files"></span></label>
|
||||||
<input id="node-input-files" type="text" tabindex="1" data-i18n="[placeholder]watch.placeholder.files">
|
<input id="node-input-files" type="text" tabindex="1" data-i18n="[placeholder]watch.placeholder.files">
|
||||||
|
<!-- added to enable context type setup -->
|
||||||
|
<input type="hidden" id="node-input-filesType">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label> </label>
|
<label> </label>
|
||||||
@ -38,10 +40,14 @@
|
|||||||
name: {value:""},
|
name: {value:""},
|
||||||
files: {value:"",required:true,
|
files: {value:"",required:true,
|
||||||
label:RED._("node-red:watch.label.files")},
|
label:RED._("node-red:watch.label.files")},
|
||||||
recursive: {value:""}
|
// property added to enable context type setup
|
||||||
|
filesType: {value: "str"},
|
||||||
|
recursive: {value:""},
|
||||||
|
// property added to enable number of inputs change
|
||||||
|
inputs: {value:0}
|
||||||
},
|
},
|
||||||
color:"BurlyWood",
|
color:"BurlyWood",
|
||||||
inputs:0,
|
inputs: 0,
|
||||||
outputs:1,
|
outputs:1,
|
||||||
icon: "watch.svg",
|
icon: "watch.svg",
|
||||||
label: function() {
|
label: function() {
|
||||||
@ -49,6 +55,20 @@
|
|||||||
},
|
},
|
||||||
labelStyle: function() {
|
labelStyle: function() {
|
||||||
return this.name?"node_label_italic":"";
|
return this.name?"node_label_italic":"";
|
||||||
|
},
|
||||||
|
// function added to enable context type setup
|
||||||
|
oneditprepare: function() {
|
||||||
|
$("#node-input-files").typedInput({
|
||||||
|
default: 'str',
|
||||||
|
types: ['str','flow','global','env','msg'],
|
||||||
|
typeField: $("#node-input-filesType")
|
||||||
|
});
|
||||||
|
},
|
||||||
|
// function added to set node input number
|
||||||
|
oneditsave: function() {
|
||||||
|
// if controlled by msg there has to be a input
|
||||||
|
this.inputs = $("#node-input-filesType").typedInput("type") === "msg" ? 1 : 0;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
@ -20,20 +20,17 @@ module.exports = function(RED) {
|
|||||||
const fs = require("fs")
|
const fs = require("fs")
|
||||||
const path = require("path")
|
const path = require("path")
|
||||||
|
|
||||||
function WatchNode(n) {
|
function configureWatcher(node, n) {
|
||||||
RED.nodes.createNode(this,n);
|
if (node.filesType != 'msg') node.files = (n.files || "");
|
||||||
|
node.files = node.files.split(",");
|
||||||
this.recursive = n.recursive || false;
|
for (var f=0; f < node.files.length; f++) {
|
||||||
this.files = (n.files || "").split(",");
|
node.files[f] = node.files[f].trim();
|
||||||
for (var f=0; f < this.files.length; f++) {
|
|
||||||
this.files[f] = this.files[f].trim();
|
|
||||||
}
|
}
|
||||||
this.p = (this.files.length === 1) ? this.files[0] : JSON.stringify(this.files);
|
node.p = (node.files.length === 1) ? node.files[0] : JSON.stringify(node.files);
|
||||||
const node = this;
|
|
||||||
|
|
||||||
const watcher = watch(this.files, { recursive: this.recursive });
|
node.watcher = watch(node.files, { recursive: node.recursive });
|
||||||
|
|
||||||
watcher.on('change', function (event, fpath) {
|
node.watcher.on('change', function (event, fpath) {
|
||||||
const file = path.basename(fpath)
|
const file = path.basename(fpath)
|
||||||
let stat;
|
let stat;
|
||||||
try {
|
try {
|
||||||
@ -60,13 +57,44 @@ module.exports = function(RED) {
|
|||||||
node.send(msg);
|
node.send(msg);
|
||||||
});
|
});
|
||||||
|
|
||||||
watcher.on('error', function (error) {
|
node.watcher.on('error', function (error) {
|
||||||
const msg = { payload: "" };
|
const msg = { payload: "" };
|
||||||
node.error(error,msg);
|
node.error(error,msg);
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
this.close = function() {
|
function WatchNode(n) {
|
||||||
watcher.close();
|
RED.nodes.createNode(this,n);
|
||||||
|
var node = this;
|
||||||
|
node.recursive = n.recursive || false;
|
||||||
|
node.files = n.files || "";
|
||||||
|
node.filesType = n.filesType || "str";
|
||||||
|
node.inputs = n.inputs || 0;
|
||||||
|
// added to enable context setup
|
||||||
|
switch (n.filesType) {
|
||||||
|
case "global":
|
||||||
|
n.files = node.context().global.get(`${n.files}`);
|
||||||
|
break;
|
||||||
|
case "flow":
|
||||||
|
n.files = node.context().flow.get(`${n.files}`);
|
||||||
|
break;
|
||||||
|
case "env":
|
||||||
|
n.files = node.context().env.get(`${n.files}`);
|
||||||
|
case "msg":
|
||||||
|
node.on('input', function(msg) {
|
||||||
|
if (msg[n.files] != node.files) { // new path/files list received
|
||||||
|
if (node.watcher) node.watcher.close(); // if watcher active close it
|
||||||
|
node.files = msg[n.files]; // save new path/files list
|
||||||
|
configureWatcher(node, n); // reconfigure watcher
|
||||||
|
};
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (n.filesType != 'msg') configureWatcher(node, n);
|
||||||
|
|
||||||
|
node.close = function() {
|
||||||
|
if (node.watcher) node.watcher.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("watch", WatchNode);
|
RED.nodes.registerType("watch", WatchNode);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user