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

add watched filename to msg.filename so can feed direct to file in node

This commit is contained in:
Dave Conway-Jones 2018-09-23 10:35:18 +01:00
parent f5bebef37f
commit f0957c838f
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
2 changed files with 9 additions and 10 deletions

View File

@ -36,7 +36,7 @@
<p>You can enter a list of comma separated directories and/or files. You will <p>You can enter a list of comma separated directories and/or files. You will
need to put quotes "..." around any that have spaces in.</p> need to put quotes "..." around any that have spaces in.</p>
<p>On Windows you must use double back-slashes \\ in any directory names.</p> <p>On Windows you must use double back-slashes \\ in any directory names.</p>
<p>The full filename of the file that actually changed is put into <code>msg.payload</code>, <p>The full filename of the file that actually changed is put into <code>msg.payload</code> and <code>msg.filename</code>,
while a stringified version of the watch list is returned in <code>msg.topic</code>.</p> while a stringified version of the watch list is returned in <code>msg.topic</code>.</p>
<p><code>msg.file</code> contains just the short filename of the file that changed. <p><code>msg.file</code> contains just the short filename of the file that changed.
<code>msg.type</code> has the type of thing changed, usually <i>file</i> or <i>directory</i>, <code>msg.type</code> has the type of thing changed, usually <i>file</i> or <i>directory</i>,

View File

@ -18,7 +18,6 @@ module.exports = function(RED) {
"use strict"; "use strict";
var Notify = require("fs.notify"); var Notify = require("fs.notify");
var fs = require("fs"); var fs = require("fs");
var sep = require("path").sep;
var path = require("path"); var path = require("path");
var getAllDirs = function (dir, filelist) { var getAllDirs = function (dir, filelist) {
@ -52,14 +51,14 @@ module.exports = function(RED) {
} }
var notifications = new Notify(node.files); var notifications = new Notify(node.files);
notifications.on('change', function (file, event, path) { notifications.on('change', function (file, event, fpath) {
var stat; var stat;
try { try {
if (fs.statSync(path).isDirectory()) { path = path + sep + file; } if (fs.statSync(fpath).isDirectory()) { fpath = path.join(fpath,file); }
stat = fs.statSync(path); stat = fs.statSync(fpath);
} catch(e) { } } catch(e) { }
var type = "none"; 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) {
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"; }
@ -69,8 +68,8 @@ module.exports = function(RED) {
else if (stat.isDirectory()) { else if (stat.isDirectory()) {
type = "directory"; type = "directory";
if (node.recursive) { if (node.recursive) {
notifications.add([path]); notifications.add([fpath]);
notifications.add(getAllDirs(path)); notifications.add(getAllDirs(fpath));
} }
} }
else { type = "n/a"; } else { type = "n/a"; }
@ -79,8 +78,8 @@ module.exports = function(RED) {
node.send(msg); node.send(msg);
}); });
notifications.on('error', function (error, path) { notifications.on('error', function (error, fpath) {
var msg = { payload:path }; var msg = { payload:fpath };
node.error(error,msg); node.error(error,msg);
}); });