From 1be6c5ed62fddc7395b688ac8b2c240387e3d9fc Mon Sep 17 00:00:00 2001 From: Hector <46224745+noctisatrae@users.noreply.github.com> Date: Wed, 25 Aug 2021 07:25:09 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A6=20You=20can=20now=20disable=20mess?= =?UTF-8?q?ages=20from=20hub=20&=20more...=20(#1120)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/hub.js | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/hub.js b/lib/hub.js index dc3c8fe8..9186f02e 100644 --- a/lib/hub.js +++ b/lib/hub.js @@ -8,10 +8,16 @@ let chokidar; try { chokidar = require('chokidar') } catch (error) { } // Must install chokidar to use this feature. -function watch(what, opt) { - opt = opt || { } +/** + * Watches a directory and send all its content in the database + * @constructor + * @param {string} what - Which directory hub should watch. + * @param {Object} options - https://gun.eco/docs/hub.js#options + */ +function watch(what, options) { + options = options || { msg: true } - let modifiedPath = (opt.file || ""); + let modifiedPath = (options.file || ""); let watcher; try { @@ -26,31 +32,33 @@ function watch(what, opt) { // Handle events ! watcher .on('add', async function(path) { - - log(`File ${path} has been added`); - gun.get('hub').get(modifiedPath + '/' + path).put(fs.readFileSync(path, 'utf-8').replace(/\n/gm, "")) + if (options.msg) log(`File ${path} has been added`); + gun.get('hub').get(modifiedPath + '/' + path).put(fs.readFileSync(path, 'utf-8')) }) .on('change', async function(path) { - log(`File ${path} has been changed`); - gun.get('hub').get(modifiedPath + '/' + path).put(fs.readFileSync(path, 'utf-8').replace(/\n/gm, "")) + if (options.msg) log(`File ${path} has been changed`); + gun.get('hub').get(modifiedPath + '/' + path).put(fs.readFileSync(path, 'utf-8')) }) .on('unlink', async function (path) { - log(`File ${path} has been removed`); + if(options.msg) log(`File ${path} has been removed`); gun.get('hub').get(modifiedPath + '/' + path).put(null) }) - .on('addDir', path => log(`Directory ${path} has been added`)) - .on('unlinkDir', path => log(`Directory ${path} has been removed`)) - .on('error', error => log(`Watcher error: ${error}`)) - .on('ready', () => log('Initial scan complete. Ready for changes')) + if (options.msg) { + watcher + .on('addDir', path => log(`Directory ${path} has been added`)) + .on('unlinkDir', path => log(`Directory ${path} has been removed`)) + .on('error', error => log(`Watcher error: ${error}`)) + .on('ready', () => log('Initial scan complete. Ready for changes')) + } } catch (err) { console.log('If you want to use the hub feature, you must install `chokidar` by typing `npm i chokidar` in your terminal.') } } -module.exports = { watch : watch } +module.exports = { watch : watch } \ No newline at end of file