From bee9e20827e979326675213d8a21a102099135f8 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Thu, 28 Apr 2022 14:07:13 +0100 Subject: [PATCH] Update Watch node to use node-watch module --- package.json | 2 +- .../@node-red/nodes/core/storage/23-watch.js | 56 +++++++------------ .../node_modules/@node-red/nodes/package.json | 2 +- 3 files changed, 22 insertions(+), 38 deletions(-) diff --git a/package.json b/package.json index 772eebf16..2ce87e021 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,6 @@ "express-session": "1.17.2", "form-data": "4.0.0", "fs-extra": "10.1.0", - "fs.notify": "0.0.4", "got": "11.8.3", "hash-sum": "2.0.0", "hpagent": "0.1.2", @@ -65,6 +64,7 @@ "multer": "1.4.4", "mustache": "4.2.0", "node-red-admin": "^3.0.0", + "node-watch": "0.7.3", "nopt": "5.0.0", "oauth2orize": "1.11.1", "on-headers": "1.0.2", diff --git a/packages/node_modules/@node-red/nodes/core/storage/23-watch.js b/packages/node_modules/@node-red/nodes/core/storage/23-watch.js index 9b6c4bc62..4a1be0ec7 100644 --- a/packages/node_modules/@node-red/nodes/core/storage/23-watch.js +++ b/packages/node_modules/@node-red/nodes/core/storage/23-watch.js @@ -16,24 +16,9 @@ module.exports = function(RED) { "use strict"; - var Notify = require("fs.notify"); - var fs = require("fs"); - var path = require("path"); - - var getAllDirs = function (dir, filelist) { - filelist = filelist || []; - fs.readdirSync(dir).forEach(file => { - try { - if (fs.statSync(path.join(dir, file)).isDirectory() ) { - filelist.push(path.join(dir, file)); - getAllDirs(path.join(dir, file), filelist); - } - } catch (error) { - //should we raise an error? - } - }); - return filelist; - } + const watch = require('node-watch') + const fs = require("fs") + const path = require("path") function WatchNode(n) { RED.nodes.createNode(this,n); @@ -44,25 +29,24 @@ module.exports = function(RED) { this.files[f] = this.files[f].trim(); } this.p = (this.files.length === 1) ? this.files[0] : JSON.stringify(this.files); - var node = this; + const 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])); - } - } - } + const watcher = watch(this.files, { recursive: true }); - var notifications = new Notify(node.files); - notifications.on('change', function (file, event, fpath) { - var stat; + watcher.on('change', function (event, fpath) { + const file = path.basename(fpath) + let stat; try { - if (fs.statSync(fpath).isDirectory()) { fpath = path.join(fpath,file); } stat = fs.statSync(fpath); } catch(e) { } - var type = "none"; - var msg = { payload:fpath, topic:node.p, file:file, filename:fpath }; + let type = "none"; + const msg = { + payload:fpath, + topic:node.p, + file:file, + filename:fpath, + event: event + }; if (stat) { if (stat.isFile()) { type = "file"; msg.size = stat.size; } else if (stat.isBlockDevice()) { type = "blockdevice"; } @@ -82,14 +66,14 @@ module.exports = function(RED) { node.send(msg); }); - notifications.on('error', function (error, fpath) { - var msg = { payload:fpath }; + watcher.on('error', function (error) { + const msg = { payload: "" }; node.error(error,msg); }); this.close = function() { - notifications.close(); + watcher.close(); } } - RED.nodes.registerType("watch",WatchNode); + RED.nodes.registerType("watch", WatchNode); } diff --git a/packages/node_modules/@node-red/nodes/package.json b/packages/node_modules/@node-red/nodes/package.json index 5751c15ce..8394d78df 100644 --- a/packages/node_modules/@node-red/nodes/package.json +++ b/packages/node_modules/@node-red/nodes/package.json @@ -28,7 +28,6 @@ "denque": "2.0.1", "form-data": "4.0.0", "fs-extra": "10.1.0", - "fs.notify": "0.0.4", "got": "11.8.3", "hash-sum": "2.0.0", "hpagent": "0.1.2", @@ -39,6 +38,7 @@ "mqtt": "4.3.7", "multer": "1.4.4", "mustache": "4.2.0", + "node-watch": "0.7.3", "on-headers": "1.0.2", "raw-body": "2.5.1", "tough-cookie": "4.0.0",