You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

watch

Package Overview
Dependencies
Maintainers
3
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

watch

Utilities for watching file trees.

1.0.2
latest
Source
npmnpm
Version published
Weekly downloads
1.7M
70.08%
Maintainers
3
Weekly downloads
 
Created

What is watch?

The 'watch' npm package is a utility that allows you to monitor files and directories for changes. It can be used to trigger actions when files are modified, added, or deleted, making it useful for tasks such as automated testing, live reloading, and continuous integration.

What are watch's main functionalities?

Watch a single file

This feature allows you to watch a single file for changes. When the file is modified, the callback function is triggered.

const watch = require('watch');

watch.watchFile('/path/to/file', function (curr, prev) {
  console.log('File changed');
});

Watch a directory

This feature allows you to watch a directory and its subdirectories for changes. The callback function is triggered when files are added, removed, or modified.

const watch = require('watch');

watch.watchTree('/path/to/dir', function (f, curr, prev) {
  if (typeof f == 'object' && prev === null && curr === null) {
    console.log('Finished walking the tree');
  } else if (prev === null) {
    console.log('New file added: ' + f);
  } else if (curr.nlink === 0) {
    console.log('File removed: ' + f);
  } else {
    console.log('File changed: ' + f);
  }
});

Ignore specific files or directories

This feature allows you to ignore specific files or directories while watching. In this example, dot files and the 'node_modules' directory are ignored.

const watch = require('watch');

watch.watchTree('/path/to/dir', {
  ignoreDotFiles: true,
  filter: function (f, stat) {
    return !/node_modules/.test(f);
  }
}, function (f, curr, prev) {
  console.log('File changed: ' + f);
});

Other packages similar to watch

Keywords

util

FAQs

Package last updated on 17 Feb 2017

Did you know?

Socket

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