@push.rocks/smartwatch

A smart watch module for the TypeScript space.

readme.md for @push.rocks/smartwatch

A cross-runtime file watcher with glob pattern support for Node.js, Deno, and Bun.

Issue Reporting and Security

For reporting bugs, issues, or security vulnerabilities, please visit community.foss.global/. This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a code.foss.global/ account to submit Pull Requests directly.

Install

npm install @push.rocks/smartwatch
# or
pnpm add @push.rocks/smartwatch

Features

How It Works

smartwatch selects the best file watching backend for the current runtime:

Runtime Backend
Node.js/Bun chokidar v5 (uses fs.watch() internally)
Deno Native Deno.watchFs() API

On Node.js and Bun, chokidar provides robust cross-platform file watching with features like atomic write detection, inode tracking, and write stabilization. On Deno, native APIs are used directly with built-in debouncing and temporary file filtering.

Glob patterns are handled through picomatch — base paths are extracted from patterns and watched natively, while events are filtered through matchers before emission.

Usage

Basic Setup

import { Smartwatch } from '@push.rocks/smartwatch';

// Create a watcher with glob patterns
const watcher = new Smartwatch([
  './src/**/*.ts',
  './public/assets/**/*'
]);

// Start watching
await watcher.start();

Subscribing to File Events

Use RxJS observables to react to file system changes:

// Get an observable for file changes
const changeObservable = await watcher.getObservableFor('change');
changeObservable.subscribe(([path, stats]) => {
  console.log(`File changed: ${path}`);
  console.log(`New size: ${stats?.size} bytes`);
});

// Watch for new files
const addObservable = await watcher.getObservableFor('add');
addObservable.subscribe(([path]) => {
  console.log(`File added: ${path}`);
});

// Watch for deleted files
const unlinkObservable = await watcher.getObservableFor('unlink');
unlinkObservable.subscribe(([path]) => {
  console.log(`File deleted: ${path}`);
});

Supported Events

Event Description
add File has been added
addDir Directory has been added
change File has been modified
unlink File has been removed
unlinkDir Directory has been removed
error Error occurred
ready Initial scan complete

Dynamic Watch Management

Add or remove patterns while the watcher is running:

const watcher = new Smartwatch(['./src/**/*.ts']);
await watcher.start();

// Add more patterns to watch
watcher.add(['./tests/**/*.spec.ts', './config/*.json']);

// Remove a pattern
watcher.remove('./src/**/*.test.ts');

Stopping the Watcher

await watcher.stop();

Complete Example

import { Smartwatch } from '@push.rocks/smartwatch';

async function watchProject() {
  const watcher = new Smartwatch([
    './src/**/*.ts',
    './package.json'
  ]);

  await watcher.start();
  console.log('Watching for changes...');

  const changes = await watcher.getObservableFor('change');
  changes.subscribe(([path, stats]) => {
    console.log(`Modified: ${path} (${stats?.size ?? 'unknown'} bytes)`);
  });

  const additions = await watcher.getObservableFor('add');
  additions.subscribe(([path]) => {
    console.log(`New file: ${path}`);
  });

  const deletions = await watcher.getObservableFor('unlink');
  deletions.subscribe(([path]) => {
    console.log(`Deleted: ${path}`);
  });

  // Handle graceful shutdown
  process.on('SIGINT', async () => {
    await watcher.stop();
    process.exit(0);
  });
}

watchProject();

API Reference

Smartwatch

Constructor

new Smartwatch(patterns: string[])

Creates a new Smartwatch instance with the given glob patterns.

Parameters:

Methods

Method Returns Description
start() Promise<void> Starts watching for file changes
stop() Promise<void> Stops the file watcher and cleans up resources
add(patterns: string[]) void Adds additional patterns to watch
remove(pattern: string) void Removes a pattern from the watch list
getObservableFor(event: TFsEvent) Promise<Observable<[string, Stats]>> Returns an RxJS observable for the specified event

Properties

Property Type Description
status 'idle' | 'starting' | 'watching' Current watcher status

Types

type TFsEvent = 'add' | 'addDir' | 'change' | 'error' | 'unlink' | 'unlinkDir' | 'ready' | 'raw';
type TSmartwatchStatus = 'idle' | 'starting' | 'watching';

Requirements

Runtime Version
Node.js 20+
Deno Any version with Deno.watchFs() support
Bun Uses Node.js compatibility layer

This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the LICENSE file.

Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.

Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.

Company Information

Task Venture Capital GmbH Registered at District Court Bremen HRB 35230 HB, Germany

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.

changelog.md for @push.rocks/smartwatch

2026-03-23 - 6.4.0 - feat(watchers)

add Rust-powered watcher backend with runtime fallback and cross-platform test coverage

2026-03-23 - 6.3.1 - fix(watcher)

unref lingering FSWatcher handles after stopping the node watcher

2025-12-11 - 6.3.0 - feat(watchers)

Integrate chokidar-based Node watcher, expose awaitWriteFinish options, and update docs/tests

2025-12-11 - 6.2.5 - fix(watcher.node)

Normalize paths and improve Node watcher robustness: restart/rescan on errors (including ENOSPC), clear stale state, and remove legacy throttler

2025-12-11 - 6.2.4 - fix(tests)

Stabilize tests and document chokidar-inspired Node watcher architecture

2025-12-11 - 6.2.3 - fix(watcher.node)

Improve handling of temporary files from atomic editor writes in Node watcher

2025-12-11 - 6.2.2 - fix(watcher.node)

Defer events during initial scan, track full event sequences, and harden watcher shutdown

2025-12-10 - 6.2.1 - fix(watcher.node)

Handle fs.watch close without spurious restarts; add tests and improve test runner

2025-12-10 - 6.2.0 - feat(watchers)

Improve Node watcher robustness: file-level inode tracking, abortable restarts, restart race guards, and untracked-file handling

2025-12-08 - 6.1.1 - fix(watchers/watcher.node)

Improve Node watcher robustness: inode tracking, ENOSPC detection, enhanced health checks and temp-file handling

2025-12-08 - 6.1.0 - feat(watcher.node)

Add automatic restart, periodic health checks, and safe event emission to Node watcher; improve logging and stat handling

2025-12-08 - 6.0.0 - BREAKING CHANGE(watchers)

Replace polling-based write stabilization with debounce-based event coalescing and simplify watcher options

2025-12-08 - 5.1.0 - feat(watchers)

Improve write stabilization and ignore temporary editor files

2025-11-30 - 5.0.0 - BREAKING CHANGE(@push.rocks/smartwatch)

Rename package and update branding/docs: switch from @push.rocks/smartchok to @push.rocks/smartwatch, update repository/homepage/bugs URLs and author, and refresh README examples and install instructions.

2025-11-30 - 4.0.1 - fix(readme)

Update README: refine description and clarify trademark/legal information

2025-11-30 - 4.0.0 - BREAKING CHANGE(watchers)

Replace chokidar with native platform watchers and add cross-runtime support (Node.js, Deno, Bun); introduce write stabilization and internal glob matching

2025-11-30 - 3.0.0 - BREAKING CHANGE(smartwatch)

Introduce Smartwatch: cross-runtime native file watching for Node.js, Deno and Bun; rename smartchok to smartwatch and bump major version to 2.0.0

2025-11-29 - 1.2.0 - feat(core)

Migrate to chokidar 5.x, add picomatch filtering and update test/dev dependencies

2025-06-26 - 1.1.1 - fix(package.json)

Add packageManager field to package.json for pnpm configuration

2025-06-26 - 1.1.0 - feat(Smartchok)

Migrate to chokidar 4.x with picomatch glob support, update documentation and tests

2024-05-29 – 1.0.34 – general

This release improves the project description.

2024-05-06 – 1.0.33 – core

This release includes a mix of bug fixes and configuration updates.

2024-02-29 – 1.0.32 to 1.0.28 – core fixes

Releases 1.0.32 through 1.0.28 were dedicated to routine core fixes.
(This range covers versions that only included “fix(core): update” changes.)

2024-01-28 – 1.0.27 – core

This release not only fixed core issues but also adjusted the organization scheme.

2021-12-01 – 1.0.26 to 1.0.14 – core fixes

Releases 1.0.26 through 1.0.14 were devoted to routine core fixes.
(No additional details beyond the core update were recorded.)

2018-02-28 – 1.0.13 to 1.0.11 – ci updates

Releases 1.0.13 through 1.0.11 focused on updating the continuous integration configuration.

2017-06-30 – 1.0.10 – general

This release delivered several improvements beyond a simple version bump.

2017-06-30 – 1.0.9 – general

This release addressed module loading and code hygiene.

2017-06-30 – 1.0.8 – general

A targeted update to align output with expectations.

2017-04-09 – 1.0.7 – ci

An update to the continuous integration configuration.

2017-04-09 – 1.0.6 – npm

This release updated extra npm configuration.

2017-02-15 – 1.0.5 – general

Standardization work was undertaken with new organizational practices.

2016-11-18 – 1.0.4 – general

This release refreshed dependency settings.

2016-11-18 – 1.0.3 – general

Readability and developer guidance were improved.

2016-11-18 – 1.0.2 – general

Minor documentation and CI configuration enhancements were added.

2016-09-22 – 1.0.1 – general

A fix was applied to ensure the process exits correctly.

2016-09-22 – 1.0.0 – general

The project’s initial setup was established along with CI configuration.