diff --git a/lib/hub.js b/lib/hub.js index 9186f02e..58ff6adb 100644 --- a/lib/hub.js +++ b/lib/hub.js @@ -15,37 +15,94 @@ try { chokidar = require('chokidar') } catch (error) { * @param {Object} options - https://gun.eco/docs/hub.js#options */ function watch(what, options) { - options = options || { msg: true } + options = options ?? { msg: true, hubignore: false } - let modifiedPath = (options.file || ""); + options.msg = options.msg ?? true; + options.hubignore = options.hubignore ?? false; + + let modifiedPath = (options.file ?? ""); let watcher; try { - // Set up the file watcher. - watcher = chokidar.watch(what, { - ignored: /(^|[\/\\])\../, // ignore dotfiles - persistent: true - }); + + if (options.hubignore) { + + watcher = chokidar.watch(what, { + persistent: true + }); + + } else if (!options.hubignore) { + + watcher = chokidar.watch(what, { + ignored: /(^|[\/\\])\../, // ignore dotfiles + persistent: true + }); + + } const log = console.log.bind(console); + let hubignore; + // Handle events ! watcher .on('add', async function(path) { - if (options.msg) log(`File ${path} has been added`); - gun.get('hub').get(modifiedPath + '/' + path).put(fs.readFileSync(path, 'utf-8')) + + if (options.hubignore && path.includes('.hubignore')) { + + hubignore = fs.readFileSync(what + '/.hubignore', 'utf-8'); + + } else if (!path.includes('.hubignore') && !hubignore?.includes(path.substring(path.lastIndexOf("/") + 1))) { + + if (options.msg) log(`File ${path} has been added`); + + if(path[path.search(/^./gm)] === "/" || ".") { + gun.get('hub').get(modifiedPath + path).put(fs.readFileSync(path, 'utf-8')) + } else { + gun.get('hub').get(modifiedPath + '/' + path).put(fs.readFileSync(path, 'utf-8')) + } + + } else { + + if(options.msg) log(`The addition of ${path} has been ignored !`) + + } }) .on('change', async function(path) { - - if (options.msg) log(`File ${path} has been changed`); - gun.get('hub').get(modifiedPath + '/' + path).put(fs.readFileSync(path, 'utf-8')) + + if (options.hubignore && path.includes('.hubignore')) { + + hubignore = fs.readFileSync(what + '/.hubignore', 'utf-8'); + + } else if (!path.includes('.hubignore') && !hubignore?.includes(path.substring(path.lastIndexOf('/') + 1))) { + + if (options.msg) log(`File ${path} has been changed`); + gun.get('hub').get(modifiedPath + '/' + path).put(fs.readFileSync(path, 'utf-8')) + + } else { + + if(options.msg) log(`The changes on ${path} has been ignored.`) + + } }) - .on('unlink', async function (path) { - - if(options.msg) log(`File ${path} has been removed`); - gun.get('hub').get(modifiedPath + '/' + path).put(null) + .on('unlink', async function (path) { + + if (options.hubignore && path.includes('.hubignore')) { + + hubignore = fs.readFileSync(what + '/.hubignore', 'utf-8'); + + } else if (!path.includes('.hubignore') && !hubignore?.includes(path.substring(path.lastIndexOf('/') + 1))) { + + if(options.msg) log(`File ${path} has been removed`); + gun.get('hub').get(modifiedPath + '/' + path).put(null) + + } else { + + if(options.msg) log(`The deletion of ${path} has been ignored!`) + + } }) if (options.msg) { diff --git a/test/hub/.hubignore b/test/hub/.hubignore new file mode 100644 index 00000000..44cd6488 --- /dev/null +++ b/test/hub/.hubignore @@ -0,0 +1 @@ +/whatever/aws-key.txt \ No newline at end of file diff --git a/test/hub/hub-test.js b/test/hub/hub-test.js new file mode 100644 index 00000000..9687b92a --- /dev/null +++ b/test/hub/hub-test.js @@ -0,0 +1,9 @@ +// const Gun = require('../..'); +// const gun = Gun(); + +// gun.get('hub').on(data => { +// console.log(data['/home/noctisatrae/gun/test/hub/index.html']) +// }) + +const hub = require('../../lib/hub'); +hub.watch('/home/noctisatrae/gun/test/hub', {msg: true, hubignore: true}) \ No newline at end of file diff --git a/test/hub/index.html b/test/hub/index.html new file mode 100644 index 00000000..56efbdba --- /dev/null +++ b/test/hub/index.html @@ -0,0 +1,12 @@ + + + + + + + Document + + + + + \ No newline at end of file diff --git a/test/hub/whatever/aws-key.txt b/test/hub/whatever/aws-key.txt new file mode 100644 index 00000000..da508084 --- /dev/null +++ b/test/hub/whatever/aws-key.txt @@ -0,0 +1,2 @@ +I'M A SUPER SECRET KEY WHICH SHALL NOT BE LEAKED +FOR YOUR OWN SAKE. \ No newline at end of file