mirror of
https://github.com/amark/gun.git
synced 2025-06-07 14:46:44 +00:00
🎉 Introducing a new feature ! (#1121)
* 🎉 .hubignore is ready ! * 🧪 Add tests for hub.js !
This commit is contained in:
parent
0eec835c31
commit
084e2907da
63
lib/hub.js
63
lib/hub.js
@ -15,38 +15,95 @@ try { chokidar = require('chokidar') } catch (error) {
|
|||||||
* @param {Object} options - https://gun.eco/docs/hub.js#options
|
* @param {Object} options - https://gun.eco/docs/hub.js#options
|
||||||
*/
|
*/
|
||||||
function watch(what, 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;
|
let watcher;
|
||||||
try {
|
try {
|
||||||
// Set up the file watcher.
|
|
||||||
|
if (options.hubignore) {
|
||||||
|
|
||||||
|
watcher = chokidar.watch(what, {
|
||||||
|
persistent: true
|
||||||
|
});
|
||||||
|
|
||||||
|
} else if (!options.hubignore) {
|
||||||
|
|
||||||
watcher = chokidar.watch(what, {
|
watcher = chokidar.watch(what, {
|
||||||
ignored: /(^|[\/\\])\../, // ignore dotfiles
|
ignored: /(^|[\/\\])\../, // ignore dotfiles
|
||||||
persistent: true
|
persistent: true
|
||||||
});
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
const log = console.log.bind(console);
|
const log = console.log.bind(console);
|
||||||
|
|
||||||
|
let hubignore;
|
||||||
|
|
||||||
// Handle events !
|
// Handle events !
|
||||||
watcher
|
watcher
|
||||||
.on('add', async function(path) {
|
.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 (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'))
|
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) {
|
.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`);
|
if (options.msg) log(`File ${path} has been changed`);
|
||||||
gun.get('hub').get(modifiedPath + '/' + path).put(fs.readFileSync(path, 'utf-8'))
|
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) {
|
.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`);
|
if(options.msg) log(`File ${path} has been removed`);
|
||||||
gun.get('hub').get(modifiedPath + '/' + path).put(null)
|
gun.get('hub').get(modifiedPath + '/' + path).put(null)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
if(options.msg) log(`The deletion of ${path} has been ignored!`)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
if (options.msg) {
|
if (options.msg) {
|
||||||
watcher
|
watcher
|
||||||
|
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