tiny-watcher
A tiny wrapper around fs.watch for added, removed and changed events
Last updated 7 years ago by bpostlethwaite .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install tiny-watcher 
SYNC missed versions from official npm registry.

tiny-watcher

There is no such thing as fs.watch and this is a wrapper around it. Another one. Yep, there are lots of wrappers and I tried a couple for my project but found that they were all buggy in their own idiosyncratic ways. I needed one that was buggy in a specific way, so I coded up tiny-wrapper.

Example

var tiny = require('../.')
  , fs = require('fs')
  , rimraf = require('rimraf')
  , path = require('path')


var somedir = "somedir"

fs.mkdirSync(somedir)
var watcher = tiny(somedir, ["test", "total"])

watcher.on("added", function (file) {
  console.log("EXAMPLE: added", file)
})

watcher.on("removed", function (file) {
  console.log("EXAMPLE: removed", file)
})

watcher.on("changed", function (file) {
  console.log("EXAMPLE: changed", file)
})

// ADD TEST
fs.writeFileSync(path.join(somedir, "test"), "data")
// REMOVE TEST
fs.unlinkSync(path.join(somedir, "test"))
// ADD TOTAL
fs.writeFileSync(path.join(somedir, "total"), "data")
// CHANGE TOTAL
fs.writeFileSync(path.join(somedir, "total"), "new data")
// ADD TONNES (NOT IN SPECIFIED FILE LIST)
fs.writeFileSync(path.join(somedir, "tonnes"), "data")

/*
 * Setting watcher kill should stop the remove event
 * when total is removed with rimraf
 */
setTimeout( function () {

  watcher.close()
  rimraf(somedir, function (err) {

    if (err) throw err

    process.exit()

  })
}, 1000)

which produces

EXAMPLE: added test
EXAMPLE: removed test
EXAMPLE: added total
EXAMPLE: changed total
EXAMPLE: added test
EXAMPLE: removed test

API

tiny

The main object is a function that produces watchers for a specific directory an optional set of files.

var watcher = tiny("path/to/dir" [, ["list", "of", "files"]])

watcher.on

Three events, added, removed and changed are supported. The callback returns the filename.

watcher.kill

Calls the base fs method watcher.close()

INSTALL

npm install tiny-watcher

LICENSE

MIT

Current Tags

  • 0.1.2                                ...           latest (7 years ago)

3 Versions

  • 0.1.2                                ...           7 years ago
  • 0.1.1                                ...           7 years ago
  • 0.1.0                                ...           7 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (1)
Dependents (1)

Copyright 2014 - 2017 © taobao.org |