Let watch node recurse into subdirectories

to close #1140
This commit is contained in:
Dave Conway-Jones 2017-02-07 20:32:16 +00:00
parent ada8e447cc
commit 7029541b4f
3 changed files with 42 additions and 6 deletions

View File

@ -19,6 +19,11 @@
<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">
</div>
<div class="form-row">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-recursive" style="display:inline-block; width:auto; vertical-align:top;">
<label for="node-input-recursive" style="width:70%;"> <span data-i18n="watch.label.recursive"></span></label>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
@ -46,7 +51,8 @@
category: 'advanced-input',
defaults: {
name: {value:""},
files: {value:"",required:true}
files: {value:"",required:true},
recursive: {value:""}
},
color:"BurlyWood",
inputs:0,

View File

@ -19,10 +19,23 @@ module.exports = function(RED) {
var Notify = require("fs.notify");
var fs = require("fs");
var sep = require("path").sep;
var path = require("path");
var getAllDirs = function (dir, filelist) {
filelist = filelist || [];
fs.readdirSync(dir).forEach(file => {
if (fs.statSync(path.join(dir, file)).isDirectory() ) {
filelist.push(path.join(dir, file));
getAllDirs(path.join(dir, file), filelist);
}
});
return filelist;
}
function WatchNode(n) {
RED.nodes.createNode(this,n);
this.recursive = n.recursive || false;
this.files = (n.files || "").split(",");
for (var f=0; f < this.files.length; f++) {
this.files[f] = this.files[f].trim();
@ -30,6 +43,14 @@ module.exports = function(RED) {
this.p = (this.files.length === 1) ? this.files[0] : JSON.stringify(this.files);
var node = this;
if (node.recursive) {
for (var fi in node.files) {
if (node.files.hasOwnProperty(fi)) {
node.files = node.files.concat(getAllDirs( node.files[fi]));
}
}
}
var notifications = new Notify(node.files);
notifications.on('change', function (file, event, path) {
var stat;
@ -41,11 +62,17 @@ module.exports = function(RED) {
var msg = { payload:path, topic:node.p, file:file };
if (stat) {
if (stat.isFile()) { type = "file"; msg.size = stat.size; }
else if (stat.isDirectory()) { type = "directory"; }
else if (stat.isBlockDevice()) { type = "blockdevice"; }
else if (stat.isCharacterDevice()) { type = "characterdevice"; }
else if (stat.isSocket()) { type = "socket"; }
else if (stat.isFIFO()) { type = "fifo"; }
else if (stat.isDirectory()) {
type = "directory";
if (node.recursive) {
notifications.add([path]);
notifications.add(getAllDirs(path));
}
}
else { type = "n/a"; }
}
msg.type = type;

View File

@ -233,13 +233,14 @@
"output": {
"string": "the string",
"number": "the number",
"existing": "the existing msg.payload",
"original": "the original msg.payload",
"latest": "the latest msg.payload",
"existing": "the existing msg object",
"original": "the original msg object",
"latest": "the latest msg object",
"nothing": "nothing"
},
"wait-reset": "wait to be reset",
"wait-for": "wait for",
"wait-loop": "resend it every",
"duration": {
"ms": "Milliseconds",
"s": "Seconds",
@ -250,6 +251,7 @@
"label": {
"trigger": "trigger",
"trigger-block": "trigger & block",
"trigger-loop": "resend every",
"reset": "Reset the trigger if:",
"resetMessage":"msg.reset is set",
"resetPayload":"msg.payload equals",
@ -369,7 +371,8 @@
},
"watch": {
"label": {
"files": "File(s)"
"files": "File(s)",
"recursive": "Watch sub-directories recursively"
},
"placeholder": {
"files": "Comma-separated list of files and/or directories"