Skip to content

Commit

Permalink
1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
shuritch committed Oct 28, 2023
1 parent d523395 commit 6cf075f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased][unreleased]

## [1.2.0][] - 2023-10-28

- Prevent unexpected behaviour

## [1.1.0][] - 2023-10-26

- Methods chaining
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ const snitch = new Snitch({
deep: false, // Include nested directories
home: process.cwd(), // Removes root path from emits, Warning: ignore will work on full paths
});
snitch.watch('/home/sashapop10/Downloads');
snitch.watch('/home/sashapop10/Documents');
snitch.watch('/home/user/Downloads').watch('/home/user/Documents');
snitch.on('before', updates => console.log({ before: updates }));
snitch.on('change', path => console.log({ changed: path }));
snitch.on('delete', path => console.log({ deleted: path }));
Expand Down
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ const [fs, { join, sep }] = [require('node:fs'), require('node:path')];
const { access: accessible, scheduler } = require('./lib');
const { EventEmitter } = require('node:events');

module.exports = function Watcher(options) {
module.exports = function Watcher(options = {}) {
const { timeout, ignore = [], home, deep } = options;
const [watchers, bridge] = [new Map(), new EventEmitter()];
const emit = scheduler(options?.timeout, bridge.emit.bind(bridge));
const access = accessible.bind(null, options?.ignore ?? []);
const emit = scheduler(timeout, bridge.emit.bind(bridge));
const access = accessible.bind(null, [...ignore]);

const lookup = path => (err, files) =>
void (!err && files.forEach(f => f.isDirectory() && bridge.watch(join(path, f.name))));
Expand All @@ -17,9 +18,9 @@ module.exports = function Watcher(options) {
const target = path.endsWith(sep + filename) ? path : join(path, filename);
if (!access(target)) return;
fs.stat(target, (err, stats) => {
const parsed = options?.home ? target.replace(options.home, '') : target;
const parsed = home ? target.replace(home, '') : target;
if (err) return void (bridge.unwatch(target), emit('delete', parsed));
stats.isDirectory() && options?.deep && fs.readdir(target, READ_OPTS, lookup(path));
stats.isDirectory() && deep && fs.readdir(target, READ_OPTS, lookup(path));
return void emit('change', parsed);
});
};
Expand All @@ -32,7 +33,7 @@ module.exports = function Watcher(options) {
fs.stat(path, (err, stats) => {
if (err || watchers.has(path)) return;
watchers.set(path, fs.watch(path, listener(path)));
stats.isDirectory() && options?.deep && fs.readdir(path, READ_OPTS, lookup(path));
stats.isDirectory() && deep && fs.readdir(path, READ_OPTS, lookup(path));
});
return bridge;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"license": "MIT",
"version": "1.1.0",
"version": "1.2.0",
"type": "commonjs",
"name": "filesnitch",
"homepage": "https://astrohelm.ru",
Expand Down

0 comments on commit 6cf075f

Please sign in to comment.