From e25d53a56f5bf709f94e6a4cd7bd1a76d1cfc6af Mon Sep 17 00:00:00 2001 From: Hector <46224745+noctisatrae@users.noreply.github.com> Date: Fri, 6 Aug 2021 05:30:02 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=85=20Wrap=20everything=20in=20a=20try?= =?UTF-8?q?=20&=20catch=20for=20error=20handling=E2=80=A6=20=20(#1105)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🦅 Wrap everything in a try & catch for error handling & speed improvement. * 📦 Finally here : opt.file for the hub feature ! * 📦 Finally here : opt.file for the hub feature ! And also : fixed indentation :yum: Co-authored-by: noctisatrae --- lib/hub.js | 85 ++++++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 44 deletions(-) diff --git a/lib/hub.js b/lib/hub.js index 49cafe04..021e6415 100644 --- a/lib/hub.js +++ b/lib/hub.js @@ -6,58 +6,55 @@ const gun = Gun(); let chokidar; try { chokidar = require('chokidar') } catch (error) { - console.log('Type "npm i chokidar" if you want to use the hub feature !') } // Must install chokidar to use this feature. -function watch(what) { +function watch(what, opt) { + opt = opt || { } - // Set up the file watcher ! - const watcher = chokidar.watch(what, { - ignored: /(^|[\/\\])\../, // ignore dotfiles - persistent: true - }); + let modifiedPath = (opt.file || ""); - const log = console.log.bind(console); - - // Handle events ! - - watcher - .on('add', async function(path) { - - log(`File ${path} has been added`); - gun.get('hub').get(path).put({ - - file: path, // Add the path to the file. - content: fs.readFileSync(path, 'utf8') // Add the content of the file - - }).then(console.log('Done!')); - - }) - .on('change', async function(path) { + let watcher; + try { + // Set up the file watcher. + watcher = chokidar.watch(what, { + ignored: /(^|[\/\\])\../, // ignore dotfiles + persistent: true + }); - log(`File ${path} has been changed`); - - gun.get('hub').get(path).put({ - content: fs.readFileSync(path, 'utf8') // Just update the content not the path. (Performance) - }).then(console.log('Done!')) + const log = console.log.bind(console); - }) - .on('unlink', async function (path) { - - log(`File ${path} has been removed`); - - gun.get('hub').get(path).put({ - file: null, // Delete references to the file givent that it's been deleted. - content: null, + // 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')) + }) - - }) - .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')) + .on('change', async function(path) { + + 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`); + 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')) + + } 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, } \ No newline at end of file +gun.get('hub').on(data => { + console.log(data); +}) + +module.exports = { watch : watch, }