mirror of
https://github.com/amark/gun.git
synced 2025-07-01 02:12:33 +00:00
🦅 Wrap everything in a try & catch for error handling… (#1105)
* 🦅 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 😋 Co-authored-by: noctisatrae <pro.hector.kub@gmail.com>
This commit is contained in:
parent
9735a2c3fa
commit
e25d53a56f
79
lib/hub.js
79
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);
|
||||
let watcher;
|
||||
try {
|
||||
// Set up the file watcher.
|
||||
watcher = chokidar.watch(what, {
|
||||
ignored: /(^|[\/\\])\../, // ignore dotfiles
|
||||
persistent: true
|
||||
});
|
||||
|
||||
// Handle events !
|
||||
const log = console.log.bind(console);
|
||||
|
||||
watcher
|
||||
.on('add', async function(path) {
|
||||
// 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) {
|
||||
|
||||
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!'))
|
||||
|
||||
})
|
||||
.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,
|
||||
log(`File ${path} has been added`);
|
||||
gun.get('hub').get(modifiedPath + '/' + path).put(fs.readFileSync(path, 'utf-8'))
|
||||
|
||||
})
|
||||
.on('change', async function(path) {
|
||||
|
||||
})
|
||||
.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'))
|
||||
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.')
|
||||
}
|
||||
}
|
||||
|
||||
gun.get('hub').on(data => {
|
||||
console.log(data);
|
||||
})
|
||||
|
||||
module.exports = { watch : watch, }
|
Loading…
x
Reference in New Issue
Block a user