🎉 Introducing a new feature ! (#1121)

* 🎉 .hubignore is ready !

* 🧪 Add tests for hub.js !
This commit is contained in:
Hector 2021-08-30 23:29:55 +02:00 committed by GitHub
parent 0eec835c31
commit 084e2907da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 97 additions and 16 deletions

View File

@ -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) {

1
test/hub/.hubignore Normal file
View File

@ -0,0 +1 @@
/whatever/aws-key.txt

9
test/hub/hub-test.js Normal file
View File

@ -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})

12
test/hub/index.html Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
</html>

View File

@ -0,0 +1,2 @@
I'M A SUPER SECRET KEY WHICH SHALL NOT BE LEAKED
FOR YOUR OWN SAKE.