From f0957c838ff0d965cb0eae47db7df9522f70ec5f Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Sun, 23 Sep 2018 10:35:18 +0100 Subject: [PATCH] add watched filename to msg.filename so can feed direct to file in node --- .../@node-red/nodes/core/io/23-watch.html | 2 +- .../@node-red/nodes/core/io/23-watch.js | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/io/23-watch.html b/packages/node_modules/@node-red/nodes/core/io/23-watch.html index a0259becd..c66159c7b 100644 --- a/packages/node_modules/@node-red/nodes/core/io/23-watch.html +++ b/packages/node_modules/@node-red/nodes/core/io/23-watch.html @@ -36,7 +36,7 @@

You can enter a list of comma separated directories and/or files. You will need to put quotes "..." around any that have spaces in.

On Windows you must use double back-slashes \\ in any directory names.

-

The full filename of the file that actually changed is put into msg.payload, +

The full filename of the file that actually changed is put into msg.payload and msg.filename, while a stringified version of the watch list is returned in msg.topic.

msg.file contains just the short filename of the file that changed. msg.type has the type of thing changed, usually file or directory, diff --git a/packages/node_modules/@node-red/nodes/core/io/23-watch.js b/packages/node_modules/@node-red/nodes/core/io/23-watch.js index b6adbc392..e43317bb4 100644 --- a/packages/node_modules/@node-red/nodes/core/io/23-watch.js +++ b/packages/node_modules/@node-red/nodes/core/io/23-watch.js @@ -18,7 +18,6 @@ module.exports = function(RED) { "use strict"; var Notify = require("fs.notify"); var fs = require("fs"); - var sep = require("path").sep; var path = require("path"); var getAllDirs = function (dir, filelist) { @@ -52,14 +51,14 @@ module.exports = function(RED) { } var notifications = new Notify(node.files); - notifications.on('change', function (file, event, path) { + notifications.on('change', function (file, event, fpath) { var stat; try { - if (fs.statSync(path).isDirectory()) { path = path + sep + file; } - stat = fs.statSync(path); + if (fs.statSync(fpath).isDirectory()) { fpath = path.join(fpath,file); } + stat = fs.statSync(fpath); } catch(e) { } var type = "none"; - var msg = { payload:path, topic:node.p, file:file }; + var msg = { payload:fpath, topic:node.p, file:file, filename:fpath }; if (stat) { if (stat.isFile()) { type = "file"; msg.size = stat.size; } else if (stat.isBlockDevice()) { type = "blockdevice"; } @@ -69,8 +68,8 @@ module.exports = function(RED) { else if (stat.isDirectory()) { type = "directory"; if (node.recursive) { - notifications.add([path]); - notifications.add(getAllDirs(path)); + notifications.add([fpath]); + notifications.add(getAllDirs(fpath)); } } else { type = "n/a"; } @@ -79,8 +78,8 @@ module.exports = function(RED) { node.send(msg); }); - notifications.on('error', function (error, path) { - var msg = { payload:path }; + notifications.on('error', function (error, fpath) { + var msg = { payload:fpath }; node.error(error,msg); });