gun/lib/hub.js
Hector 0b158667a7
Update hub ! (#1134)
* Fix the "/" bug.

* Update the "hub-test" to make them work everywhere!
2021-09-16 08:54:01 -07:00

121 lines
3.4 KiB
JavaScript

const fs = require('fs');
const Gun = require('../index.js');
const gun = Gun();
let chokidar;
try { chokidar = require('chokidar') } catch (error) {
} // Must install chokidar to use this feature.
/**
* 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, hubignore: false }
options.msg = options.msg ?? true;
options.hubignore = options.hubignore ?? false;
let modifiedPath = (options.file ?? "");
let watcher;
try {
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.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.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.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) {
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 }