From 7029541b4f4ab749b6c0cebb449440d419c3d412 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Tue, 7 Feb 2017 20:32:16 +0000 Subject: [PATCH] Let watch node recurse into subdirectories to close #1140 --- nodes/core/io/23-watch.html | 8 ++++++- nodes/core/io/23-watch.js | 29 +++++++++++++++++++++++++- nodes/core/locales/en-US/messages.json | 11 ++++++---- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/nodes/core/io/23-watch.html b/nodes/core/io/23-watch.html index 637094c01..781049c64 100644 --- a/nodes/core/io/23-watch.html +++ b/nodes/core/io/23-watch.html @@ -19,6 +19,11 @@ +
+ + + +
@@ -46,7 +51,8 @@ category: 'advanced-input', defaults: { name: {value:""}, - files: {value:"",required:true} + files: {value:"",required:true}, + recursive: {value:""} }, color:"BurlyWood", inputs:0, diff --git a/nodes/core/io/23-watch.js b/nodes/core/io/23-watch.js index c4fd0b858..b6adbc392 100644 --- a/nodes/core/io/23-watch.js +++ b/nodes/core/io/23-watch.js @@ -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; diff --git a/nodes/core/locales/en-US/messages.json b/nodes/core/locales/en-US/messages.json index 6bd28b834..0ad39f7a8 100644 --- a/nodes/core/locales/en-US/messages.json +++ b/nodes/core/locales/en-US/messages.json @@ -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"