https://github.com/samccone/monocle
a tool for watching things
https://github.com/samccone/monocle
Last synced: about 1 year ago
JSON representation
a tool for watching things
- Host: GitHub
- URL: https://github.com/samccone/monocle
- Owner: samccone
- License: mit
- Created: 2013-02-18T23:00:52.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2017-10-06T21:21:19.000Z (over 8 years ago)
- Last Synced: 2025-03-26T09:51:17.621Z (over 1 year ago)
- Language: JavaScript
- Size: 56.6 KB
- Stars: 17
- Watchers: 5
- Forks: 12
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/samccone/monocle)
# Monocle -- a tool for watching things
[](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: ,
fileFilter: ,
directoryFilter: ,
listener: fn(fs.stat+ object), //triggered on file change / addition
complete: //file watching all set up
});
```
The listener will recive an object with the following
```js
name: ,
path: ,
fullPath: ,
parentDir: ,
fullParentDir: ,
stat:
```
[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: , //triggered on file / addition
complete: //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: , // `*.js` for example
listener: , //triggered on file / addition
complete: //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