# glob-watcher

> Watch globs and execute a function upon change, with intelligent defaults for debouncing and queueing.

Latest version **6.0.0** (published 2023-05-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install glob-watcher
pnpm add glob-watcher
yarn add glob-watcher
bun add glob-watcher
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 6.0.0 |
| Published | 2023-05-31 |
| First published | 2013-12-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/glob-watcher) |
| Module format | CommonJS |
| Node | >= 10.13.0 |
| Dependencies | 2 |
| Unpacked size | 9.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 80 |
| Author | Gulp Team |
| Maintainers | yocontra, phated |
| Keywords | watch, glob, async, queue, debounce, callback |

## Links

- npm: https://www.npmjs.com/package/glob-watcher
- Repository: https://github.com/gulpjs/glob-watcher
- Homepage: https://github.com/gulpjs/glob-watcher#readme
- Issues: https://github.com/gulpjs/glob-watcher/issues
- npm.io page: https://npm.io/package/glob-watcher

## Dependencies (2)

- [chokidar](https://npm.io/package/chokidar.md) ^3.5.3
- [async-done](https://npm.io/package/async-done.md) ^2.0.0

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 6.0.0 (latest) — 2023-05-31
- 5.0.5 — 2020-07-23
- 5.0.4 — 2020-07-23
- 5.0.3 — 2018-11-16
- 5.0.2 — 2018-11-16
- 5.0.1 — 2018-02-14
- 5.0.0 — 2017-12-30
- 4.0.0 — 2017-06-29
- 3.2.0 — 2017-04-19
- 3.1.0 — 2016-12-26
- 3.0.0 — 2016-04-28
- 2.0.0 — 2015-03-22
- 0.0.8 — 2015-02-22
- 0.0.7 — 2014-11-13
- 0.0.6 — 2014-04-17
- … 5 more at https://npm.io/package/glob-watcher/versions

## README

<p align="center">
  <a href="https://gulpjs.com">
    <img height="257" width="114" src="https://raw.githubusercontent.com/gulpjs/artwork/master/gulp-2x.png">
  </a>
</p>

# glob-watcher

[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coveralls Status][coveralls-image]][coveralls-url]

Watch globs and execute a function upon change, with intelligent defaults for debouncing and queueing.

## Usage

```js
var watch = require('glob-watcher');

watch(['./*.js', '!./something.js'], function (done) {
  // This function will be called each time a globbed file is changed
  // but is debounced with a 200ms delay (default) and queues subsequent calls

  // Make sure to signal async completion with the callback
  // or by returning a stream, promise, observable or child process
  done();

  // if you need access to the `path` or `stat` object, listen
  // for the `change` event (see below)

  // if you need to listen to specific events, use the returned
  // watcher instance (see below)
});

// Raw chokidar instance
var watcher = watch(['./*.js', '!./something.js']);

// Listen for the 'change' event to get `path`/`stat`
// No async completion available because this is the raw chokidar instance
watcher.on('change', function (path, stat) {
  // `path` is the path of the changed file
  // `stat` is an `fs.Stat` object (not always available)
});

// Listen for other events
// No async completion available because this is the raw chokidar instance
watcher.on('add', function (path, stat) {
  // `path` is the path of the changed file
  // `stat` is an `fs.Stat` object (not always available)
});
```

## API

### `watch(globs[, options][, fn])`

Takes a path string, an array of path strings, a [glob][micromatch] string or an array of [glob][micromatch] strings as `globs` to watch on the filesystem. Also optionally takes `options` to configure the watcher and a `fn` to execute when a file changes.

**Note: As of 5.0.0, globs must use `/` as the separator character because `\\` is reserved for escape sequences (as per the Bash 4.3 & Micromatch specs). This means you can't use `path.join()` or `**dirname`in Windows environments. If you need to use`path.join()`, you can use [normalize-path][normalize-path] against your paths afterwards. If you need to use `**dirname`, you can set it as the `cwd` option that gets passed directly to [chokidar][chokidar]. The [micromatch docs][micromatch-backslashes] contain more information about backslashes.**

Returns an instance of [chokidar][chokidar].

#### `fn([callback])`

If the `fn` is passed, it will be called when the watcher emits a `change`, `add` or `unlink` event. It is automatically debounced with a default delay of 200 milliseconds and subsequent calls will be queued and called upon completion. These defaults can be changed using the `options`.

The `fn` is passed a single argument, `callback`, which is a function that must be called when work in the `fn` is complete. Instead of calling the `callback` function, [async completion][async-completion] can be signalled by:

- Returning a `Stream` or `EventEmitter`
- Returning a `Child Process`
- Returning a `Promise`
- Returning an `Observable`

Once async completion is signalled, if another run is queued, it will be executed.

#### `options`

##### `options.ignoreInitial`

If set to `false` the `fn` is called during [chokidar][chokidar] instantiation as it discovers the file paths. Useful if it is desirable to trigger the `fn` during startup.

**Passed through to [chokidar][chokidar], but defaulted to `true` instead of `false`.**

Type: `Boolean`

Default: `true`

##### `options.delay`

The delay to wait before triggering the `fn`. Useful for waiting on many changes before doing the work on changed files, e.g. find-and-replace on many files.

Type: `Number`

Default: `200` (milliseconds)

##### `options.queue`

Whether or not a file change should queue the `fn` execution if the `fn` is already running. Useful for a long running `fn`.

Type: `Boolean`

Default: `true`

##### `options.events`

An event name or array of event names to listen for. Useful if you only need to watch specific events.

Type: `String | Array<String>`

Default: `[ 'add', 'change', 'unlink' ]`

##### other

Options are passed directly to [chokidar][chokidar].

## License

MIT

<!-- prettier-ignore-start -->
[downloads-image]: https://img.shields.io/npm/dm/glob-watcher.svg?style=flat-square
[npm-url]: https://npmjs.com/package/glob-watcher
[npm-image]: https://img.shields.io/npm/v/glob-watcher.svg?style=flat-square

[ci-url]: https://github.com/gulpjs/glob-watcher/actions?query=workflow:dev
[ci-image]: https://img.shields.io/github/actions/workflow/status/gulpjs/glob-watcher/dev.yml?branch=master&style=flat-square

[coveralls-url]: https://coveralls.io/r/gulpjs/glob-watcher
[coveralls-image]: https://img.shields.io/coveralls/gulpjs/glob-watcher/master.svg?style=flat-square
<!-- prettier-ignore-end -->

<!-- prettier-ignore-start -->
[micromatch]: https://github.com/micromatch/micromatch
[normalize-path]: https://www.npmjs.com/package/normalize-path
[micromatch-backslashes]: https://github.com/micromatch/micromatch#backslashes
[async-completion]: https://github.com/gulpjs/async-done#completion-and-error-resolution
[chokidar]: https://github.com/paulmillr/chokidar
<!-- prettier-ignore-end -->

---
_Source: https://npm.io/package/glob-watcher · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
