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

Update Watch node to use node-watch module

This commit is contained in:
Nick O'Leary 2022-04-28 14:07:13 +01:00
parent 12f527a120
commit bee9e20827
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
3 changed files with 22 additions and 38 deletions

View File

@ -45,7 +45,6 @@
"express-session": "1.17.2", "express-session": "1.17.2",
"form-data": "4.0.0", "form-data": "4.0.0",
"fs-extra": "10.1.0", "fs-extra": "10.1.0",
"fs.notify": "0.0.4",
"got": "11.8.3", "got": "11.8.3",
"hash-sum": "2.0.0", "hash-sum": "2.0.0",
"hpagent": "0.1.2", "hpagent": "0.1.2",
@ -65,6 +64,7 @@
"multer": "1.4.4", "multer": "1.4.4",
"mustache": "4.2.0", "mustache": "4.2.0",
"node-red-admin": "^3.0.0", "node-red-admin": "^3.0.0",
"node-watch": "0.7.3",
"nopt": "5.0.0", "nopt": "5.0.0",
"oauth2orize": "1.11.1", "oauth2orize": "1.11.1",
"on-headers": "1.0.2", "on-headers": "1.0.2",

View File

@ -16,24 +16,9 @@
module.exports = function(RED) { module.exports = function(RED) {
"use strict"; "use strict";
var Notify = require("fs.notify"); const watch = require('node-watch')
var fs = require("fs"); const fs = require("fs")
var path = require("path"); const 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;
}
function WatchNode(n) { function WatchNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
@ -44,25 +29,24 @@ module.exports = function(RED) {
this.files[f] = this.files[f].trim(); this.files[f] = this.files[f].trim();
} }
this.p = (this.files.length === 1) ? this.files[0] : JSON.stringify(this.files); this.p = (this.files.length === 1) ? this.files[0] : JSON.stringify(this.files);
var node = this; const node = this;
if (node.recursive) { const watcher = watch(this.files, { recursive: true });
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); watcher.on('change', function (event, fpath) {
notifications.on('change', function (file, event, fpath) { const file = path.basename(fpath)
var stat; let stat;
try { try {
if (fs.statSync(fpath).isDirectory()) { fpath = path.join(fpath,file); }
stat = fs.statSync(fpath); stat = fs.statSync(fpath);
} catch(e) { } } catch(e) { }
var type = "none"; let type = "none";
var msg = { payload:fpath, topic:node.p, file:file, filename:fpath }; const msg = {
payload:fpath,
topic:node.p,
file:file,
filename:fpath,
event: event
};
if (stat) { if (stat) {
if (stat.isFile()) { type = "file"; msg.size = stat.size; } if (stat.isFile()) { type = "file"; msg.size = stat.size; }
else if (stat.isBlockDevice()) { type = "blockdevice"; } else if (stat.isBlockDevice()) { type = "blockdevice"; }
@ -82,14 +66,14 @@ module.exports = function(RED) {
node.send(msg); node.send(msg);
}); });
notifications.on('error', function (error, fpath) { watcher.on('error', function (error) {
var msg = { payload:fpath }; const msg = { payload: "" };
node.error(error,msg); node.error(error,msg);
}); });
this.close = function() { this.close = function() {
notifications.close(); watcher.close();
} }
} }
RED.nodes.registerType("watch",WatchNode); RED.nodes.registerType("watch", WatchNode);
} }

View File

@ -28,7 +28,6 @@
"denque": "2.0.1", "denque": "2.0.1",
"form-data": "4.0.0", "form-data": "4.0.0",
"fs-extra": "10.1.0", "fs-extra": "10.1.0",
"fs.notify": "0.0.4",
"got": "11.8.3", "got": "11.8.3",
"hash-sum": "2.0.0", "hash-sum": "2.0.0",
"hpagent": "0.1.2", "hpagent": "0.1.2",
@ -39,6 +38,7 @@
"mqtt": "4.3.7", "mqtt": "4.3.7",
"multer": "1.4.4", "multer": "1.4.4",
"mustache": "4.2.0", "mustache": "4.2.0",
"node-watch": "0.7.3",
"on-headers": "1.0.2", "on-headers": "1.0.2",
"raw-body": "2.5.1", "raw-body": "2.5.1",
"tough-cookie": "4.0.0", "tough-cookie": "4.0.0",