# monocle

> a tool for watching directories for file changes

Latest version **1.1.51** (published 2013-11-02) · BSD license · 0 weekly downloads

## Install

```sh
npm install monocle
pnpm add monocle
yarn add monocle
bun add monocle
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.51 |
| Published | 2013-11-02 |
| First published | 2013-02-18 |
| Weekly downloads | 0 |
| License | BSD |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17 |
| Author | Sam Saccone |
| Maintainers | samccone |
| Keywords | watch, filesystem, folders, fs |

## Links

- npm: https://www.npmjs.com/package/monocle
- Repository: https://github.com/samccone/monocle
- Issues: https://github.com/samccone/monocle/issues
- npm.io page: https://npm.io/package/monocle

## Dependencies (1)

- [readdirp](https://npm.io/package/readdirp.md) ~0.2.3

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 1.1.51 (latest) — 2013-11-02
- 1.1.50 — 2013-06-20
- 0.1.50 — 2013-06-10
- 0.1.48 — 2013-05-26
- 0.1.47 — 2013-04-30
- 0.1.46 — 2013-04-22
- 0.1.45 — 2013-04-21
- 0.1.43 — 2013-04-16
- 0.1.42 — 2013-04-16
- 0.1.41 — 2013-04-16
- 0.1.40 — 2013-04-16
- 0.1.4 — 2013-04-15
- 0.1.3 — 2013-03-05
- 0.1.2 — 2013-02-20
- 0.1.15 — 2013-02-19
- … 2 more at https://npm.io/package/monocle/versions

## README

[![Build Status](https://travis-ci.org/samccone/monocle.png?branch=master)](https://travis-ci.org/samccone/monocle)

# Monocle -- a tool for watching things

[![logo](https://raw.github.com/samccone/monocle/master/logo.png)](https://raw.github.com/samccone/monocle/master/logo.png)

Have you ever wanted to watch a folder and all of its files/nested folders for changes. well now you can!

## Installation

```
npm install monocle
```

## Usage

### Watch a directory:

```js
var monocle = require('monocle')()
monocle.watchDirectory({
  root: <root directory>,
  fileFilter: <optional>,
  directoryFilter: <optional>,
  listener: fn(fs.stat+ object), //triggered on file change / addition
  complete: <fn> //file watching all set up
});
```

The listener will recive an object with the following

```js
  name: <filename>,
  path: <filepath-relative>,
  fullPath: <filepath-absolute>,
  parentDir: <parentDir-relative>,
  fullParentDir: <parentDir-absolute>,
  stat: <see fs.stats>
```

[fs.stats](http://nodejs.org/api/fs.html#fs_class_fs_stats)

When a new file is added to the directoy it triggers a file change and thus will be passed to your specified listener.

The two filters are passed through to `readdirp`.  More documentation can be found [here](https://github.com/thlorenz/readdirp#filters)

### Watch a list of files:

```js
Monocle.watchFiles({
  files: [], //path of file(s)
  listener: <fn(fs.stat+ object)>, //triggered on file / addition
  complete: <fn> //file watching all set up
});
```

### Just watch path

Just an alias of `watchFiles` and `watchDirectory` so you don't need to tell if that's a file or a directory by yourself. Parameter passed to `path` can be a `string` or a `array` of `string`.

```js
Monocle.watchPaths({
  path: [], //list of paths, or a string of path
  fileFilter: <optional>, // `*.js` for example
  listener: <fn(fs.stat+ object)>, //triggered on file / addition
  complete: <fn> //file watching all set up
});
```

### Force to use fs.watch

You can use the USE_FS_WATCH env variable set to true, to force this behavior regardless of platform.

## Why not just use fs.watch ?

  - file watching is really bad cross platforms in node
  - you need to be smart when using fs.watch as compared to fs.watchFile
  - Monocle takes care of this logic for you!
  - windows systems use fs.watch
  - osx and linux uses fs.watchFile

## License

BSD

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