file-watcher

View on GitHub

File Watcher

A minimal, type-safe file watcher for JavaScript/TypeScript. You can use it to watch directories or files for changes and perform actions when changes are detected. It is a thin wrapper around chokidar with a type-safe, minimal API and improved logging.

Features:

Installation

npm install @coursebook/file-watcher

Usage

A minimal, type-safe file watcher for JavaScript/TypeScript. Use it to watch directories or files for changes and perform actions when changes are detected.

Basic Example

import { FileWatcherImpl, type FileWatcher } from "@coursebook/file-watcher";

const fileWatcher: FileWatcher = new FileWatcherImpl({
  source: "./source-folder",
  // Exclude any file that includes "tmp" in the name
  exclude: ["**/*tmp*"],
});

fileWatcher.setLogLevel("info"); // set to "trace" to see more detailed logs

fileWatcher.watch(async (path) => {
  // Do something with the file
  console.log(`File changed: ${path}`);
});

Configuration Options

interface WatchOptions {
  /** Source directory to watch */
  source: string | string[];
  /** Patterns to ignore, relative to source directory */
  exclude?: string | string[];
  /** Whether to use polling instead of native watchers */
  usePolling?: boolean;
}

FileWatcher Interface

interface FileWatcher {
  /**
   * Start watching files/directories
   * @param onChange - The function to call when a file changes
   */
  watch(onChange: (path: string) => Promise<void>): void;

  /**
   * Set the log level for the file watcher
   * @param level - The log level to set ("trace" | "info")
   */
  setLogLevel(level: LogLevel): void;
}

Error Handling

The component uses custom error types for better error handling:

enum FileWatcherErrorType {
  WATCH_ERROR = "WATCH_ERROR",
  CONFIG_ERROR = "CONFIG_ERROR",
}

class FileWatcherError extends Error {
  constructor(
    public type: FileWatcherErrorType,
    message: string,
    public cause?: Error,
  ) {
    super(message);
    this.name = "FileWatcherError";
  }
}

Logging

The file watcher uses the LogManager for detailed operation logging:

Set the log level using setLogLevel("trace" | "info").

Cloning the Repository

To make your workflow more organized, it’s a good idea to clone this repository into a directory named file-watcher-workspace. This helps differentiate the workspace from the file-watcher located in the packages directory.

git clone https://github.com/proj-coursebook/file-watcher file-watcher-workspace

cd file-watcher-workspace

Repository Structure

How to Use This Repo

Using a VSCode Multi-root Workspace

With Visual Studio Code, you can enhance your development experience by using a multi-root workspace to access packages, examples, and playgrounds simultaneously. This approach is more efficient than opening the root directory, or each package or example separately.

To set up a multi-root workspace:

  1. Open Visual Studio Code.
  2. Navigate to File > Open Workspace from File....
  3. Select the file-watcher.code-workspace file located at the root of the repository. This action will open all specified folders in one workspace.

The file-watcher.code-workspace file can be customized to include different folders or settings. Here’s a typical configuration:

{
  "folders": [
    {
      "path": "packages/file-watcher"
    },
    {
      "path": "examples/simple"
    },
    {
      "path": "playgrounds/chokidar"
    }
  ],
  "settings": {
    // Add any workspace-specific settings here, for example:
    "git.openRepositoryInParentFolders": "always"
  }
}

Developing the Package

Change to the package directory and install dependencies:

cd packages/file-watcher
npm install

Package Management

When you are ready to publish your package:

npm run release

This single command will:

[!TIP] For detailed information about package publishing, versioning, and local development workflows, see the NPM Package Management Guide.