npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

als-watcher

v0.1.0

Published

Watch directory changes with events, file types filters and depth control.

Downloads

4

Readme

als-watcher

als-watcher is a Node.js file system watcher which monitors a specific folder and emits events for file actions such as adding, deleting, changing, or renaming files. It allows filtering by file types and controls the depth of directory monitoring. It's compatible with ES Modules and CommonJS.

Install

npm install als-watcher

Basic usage

import watchDirectory from 'als-watcher';
// const watchDirectory = require('als-watcher');
const srcDir = 'path_to_your_directory';
const extensions = ['js','html']; // watch only js and html files
const depth = 4; // watch maximum to 4th level depth
const watcher = watchDirectory(srcDir,extensions,depth);

watcher.emitter.on('rename', (oldName, newName) => console.log(`Renamed: ${oldName} -> ${newName}`));
watcher.emitter.on('change', filename => console.log(`Changed: ${filename}`));
watcher.emitter.on('add', filename => console.log(`Added: ${filename}`));
watcher.emitter.on('unlink', filename => console.log(`Removed: ${filename}`));
watcher.emitter.on('operation', (event,...params) => console.log(event,...params));
watcher.emitter.on('error', error => console.log('Error',error));

Events

als-watcher Events Explanation

  • rename Event

    • Trigger: When a file or directory is renamed.
    • Parameters: oldName, newName.
    • Usage: Track when a file or directory gets a new name, useful for updating references in databases or file indexes.
  • change Event

    • Trigger: When the contents of a file are modified.
    • Parameters: filename.
    • Usage: Detect edits or updates to a file, trigger backups, logging, or real-time file synchronization.
  • add Event

    • Trigger: When a new file or directory is added.
    • Parameters: filename.
    • Usage: Monitor new files creation, automate file processing, indexing, or notify users of new content.
  • unlink Event

    • Trigger: When a file or directory is deleted.
    • Parameters: filename.
    • Usage: Maintain accurate file system state, remove references to deleted files, or trigger cleanup processes.
  • operation Event

    • Trigger: A generic event for all operations (all events except error).
    • Parameters: event, ...params.
    • Usage: A catch-all for different file system events, provides flexibility in handling with a single handler.
  • error Event

    • Trigger: In case of any errors during file watching.
    • Parameters: error.
    • Usage: Essential for error handling and logging, identifies issues with the file watching process for corrective actions or alerts.

Examples

Example 1: Automated Backup on File Change

Scenario:You want to create backups of certain files whenever they change, ensuring that you always have a recent version saved.

Implementation

import watchDirectory from 'als-watcher';
const srcDir = 'path_to_documents';
const extensions = ['docx', 'xlsx']; // Watch only Word and Excel files
const watcher = watchDirectory(srcDir, extensions);

watcher.emitter.on('change', (filename) => {
  // Logic to create a backup of the modified file
  console.log(`Backup created for changed file: ${filename}`);
});

Example2: Monitoring and Logging File System Activities

Scenario: For security or management purposes, you need to monitor a directory and log every file addition, deletion, or renaming activity.

Implementation

import watchDirectory from 'als-watcher';
const srcDir = 'path_to_monitored_directory';
const watcher = watchDirectory(srcDir);
watcher.emitter.on('add', (filename) => console.log(`File added: ${filename}`))
watcher.emitter.on('unlink', (filename) => console.log(`File removed: ${filename}`))
watcher.emitter.on('rename', (oldName, newName) => console.log(`File renamed from ${oldName} to ${newName}`));

Limitations of the File Watcher

This program does not support the tracking of symbolic links in the file system.

  1. Recursive Monitoring
  • Platform Dependency: The recursive option in fs.watch is not consistently supported across all platforms. For instance, it works well on Windows, but may not be available on other operating systems like Linux. Users should be aware of this limitation when running the watcher on different systems.
  • Limited Depth: On platforms where recursive monitoring is supported, the depth of the recursion (i.e., how many subdirectories deep the monitoring is effective) might be limited and inconsistent.
  1. Performance and Scalability
  • Resource Intensive: Continuously monitoring a directory, especially one with a large number of files, can be resource-intensive. The performance may degrade as the size of the directory or the frequency of file updates increases.
  1. Handling Concurrent Events
  • Event Overlap: Rapid, concurrent file events might not be accurately captured or might lead to race conditions. The system might struggle to correctly differentiate between various types of events (like add, change, and rename) when they occur in quick succession.
  • Delay in Detection: There can be a delay between the occurrence of file changes and their detection by the watcher, leading to potential issues in real-time applications.
  • Order of Events: The order in which events are received and processed might not always reflect the actual order of file changes, especially during high file activity.
  1. General Usage Notes
  • Designed for Basic Monitoring: This implementation is intended for basic file change monitoring. It may not be suitable for complex scenarios requiring high accuracy or real-time processing.