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",
"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",

View File

@ -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);
}

View File

@ -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",