mirror of
https://github.com/amark/gun.git
synced 2025-06-05 13:46:43 +00:00
🎉 Introducing a new feature ! (#1121)
* 🎉 .hubignore is ready ! * 🧪 Add tests for hub.js !
This commit is contained in:
parent
0eec835c31
commit
084e2907da
89
lib/hub.js
89
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) {
|
||||
|
1
test/hub/.hubignore
Normal file
1
test/hub/.hubignore
Normal file
@ -0,0 +1 @@
|
||||
/whatever/aws-key.txt
|
9
test/hub/hub-test.js
Normal file
9
test/hub/hub-test.js
Normal 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
12
test/hub/index.html
Normal 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>
|
2
test/hub/whatever/aws-key.txt
Normal file
2
test/hub/whatever/aws-key.txt
Normal file
@ -0,0 +1,2 @@
|
||||
I'M A SUPER SECRET KEY WHICH SHALL NOT BE LEAKED
|
||||
FOR YOUR OWN SAKE.
|
Loading…
x
Reference in New Issue
Block a user