@satyajit_me/file-watcher
v1.0.1
Published
Watch files and directories for changes with debouncing and filtering
Maintainers
Readme
File Watcher
Watch files and directories for changes with debouncing and filtering.
Installation
npm install file-watcherQuick Start
import { watch, Watcher } from 'file-watcher';
// Simple usage
const watcher = watch('./src');
watcher.onChange(event => {
console.log('Changed:', event.path);
});
// Stop watching
watcher.stop();API
watch(paths, options)
const watcher = watch('./src', {
recursive: true, // Watch subdirectories
debounce: 100, // Debounce ms
ignore: ['node_modules', /\.git/],
extensions: ['.js', '.ts']
});Watcher Class
const watcher = new Watcher(['./src', './lib'], options);
watcher
.onChange(event => console.log('Changed:', event.path))
.onDelete(event => console.log('Deleted:', event.path))
.onError(err => console.error(err))
.start();
// Later
watcher.stop();Events
watcher.on('change', event => {}); // Any change
watcher.on('rename', event => {}); // File added/renamed
watcher.on('delete', event => {}); // File deleted
watcher.on('ready', () => {}); // Watcher started
watcher.on('close', () => {}); // Watcher stopped
watcher.on('error', err => {}); // Error occurredEvent Object
{
type: 'rename' | 'change',
path: '/full/path/to/file.js',
filename: 'file.js',
timestamp: Date,
isDirectory: boolean,
size: number,
mtime: Date,
deleted: boolean
}getAllFiles(dir, options)
Scan directory for initial file list.
import { getAllFiles } from 'file-watcher';
const files = getAllFiles('./src', {
recursive: true,
extensions: ['.js'],
ignore: ['node_modules']
});Options
| Option | Default | Description |
|--------|---------|-------------|
| recursive | true | Watch subdirectories |
| debounce | 100 | Debounce delay in ms |
| ignore | [] | Patterns to ignore (string, RegExp, or function) |
| extensions | [] | File extensions to include (empty = all) |
License
MIT
