Socket
Socket
Sign inDemoInstall

watchpack

Package Overview
Dependencies
109
Maintainers
1
Versions
55
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    watchpack

Wrapper library for directory and file watching.


Version published
Weekly downloads
26M
increased by0.13%
Maintainers
1
Install size
695 kB
Created
Weekly downloads
 

Package description

What is watchpack?

The watchpack npm package is a wrapper around the file-watching functionality of Node.js and chokidar. It provides a higher-level API for watching file changes in a specified directory or set of directories. It is often used in build tools and development servers to watch for file changes and trigger rebuilds or reloads.

What are watchpack's main functionalities?

Watching Files and Directories

This feature allows you to watch for changes in specific files and directories. You can listen for 'change' events on individual files and 'aggregated' events for batch changes.

const Watchpack = require('watchpack');

const wp = new Watchpack({
  // options
});

wp.watch({
  files: ['file1.js', 'file2.js'],
  directories: ['dir1', 'dir2'],
  missing: ['file3.js', 'file4.js']
});

wp.on('change', (filePath, mtime, explanation) => {
  console.log(`${filePath} changed`, mtime, explanation);
});

wp.on('aggregated', (changes, removals) => {
  console.log('Changes:', changes);
  console.log('Removals:', removals);
});

Pausing and Resuming Watch

This feature allows you to temporarily pause the watching process and resume it later. This can be useful when performing batch operations that should not trigger watch events.

const wp = new Watchpack({});

// Start watching
wp.watch({ files: ['file1.js'] });

// Pause watching
wp.pause();

// After some time, resume watching
wp.watch({ files: ['file1.js'] });

Getting Watched Items

This feature allows you to retrieve a list of all files that are currently being watched. This can be useful for debugging or logging purposes.

const wp = new Watchpack({});

wp.watch({ files: ['file1.js'] });

const watchedFiles = wp.getWatchedFiles();
console.log(watchedFiles); // Outputs: ['file1.js']

Other packages similar to watchpack

Readme

Source

watchpack

Wrapper library for directory and file watching.

Build Status Build status Test coverage

Concept

watchpack high level API doesn't map directly to watchers. Instead a three level architecture ensures that for each directory only a single watcher exists.

  • The high level API requests DirectoryWatchers from a WatcherManager, which ensures that only a single DirectoryWatcher per directory is created.
  • A user-faced Watcher can be obtained from a DirectoryWatcher and provides a filtered view on the DirectoryWatcher.
  • Reference-counting is used on the DirectoryWatcher and Watcher to decide when to close them.
  • The real watchers (currently chokidar) are created by the DirectoryWatcher.
  • Files are never watched directly. This should keep the watcher count low.
  • Watching can be started in the past. This way watching can start after file reading.
  • Symlinks are not followed, instead the symlink is watched.

API

var Watchpack = require("watchpack");

var wp = new Watchpack({
	// options:
	aggregateTimeout: 1000
	// fire "aggregated" event when after a change for 1000ms no additional change occurred
	// aggregated defaults to undefined, which doesn't fire an "aggregated" event

	poll: true
	// poll: true - use polling with the default interval
	// poll: 10000 - use polling with an interval of 10s
	// poll defaults to undefined, which prefer native watching methods
	// Note: enable polling when watching on a network path

	ignored: /node_modules/,
	// anymatch-compatible definition of files/paths to be ignored
	// see https://github.com/paulmillr/chokidar#path-filtering
});

// Watchpack.prototype.watch(string[] files, string[] directories, [number startTime])
wp.watch(listOfFiles, listOfDirectories, Date.now() - 10000);
// starts watching these files and directories
// calling this again will override the files and directories

wp.on("change", function(filePath, mtime) {
	// filePath: the changed file
	// mtime: last modified time for the changed file
});

wp.on("aggregated", function(changes) {
	// changes: an array of all changed files
});

// Watchpack.prototype.pause()
wp.pause();
// stops emitting events, but keeps watchers open
// next "watch" call can reuse the watchers

// Watchpack.prototype.close()
wp.close();
// stops emitting events and closes all watchers

// Watchpack.prototype.getTimes()
var fileTimes = wp.getTimes();
// returns an object with all know change times for files
// this include timestamps from files not directly watched
// key: absolute path, value: timestamp as number

FAQs

Last updated on 16 May 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc