Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env coffee
- fs = require 'fs'
- fp = require 'path'
- EventEmitter = require 'events'
- class Watcher extends EventEmitter
- constructor: (globs, cb) ->
- super()
- @watch globs, cb if globs?.length
- watch: (globs, cb) ->
- path = globs[0] # TODO: process all, not just first one
- @watchPath path, cb
- baseFilter: (base, cb) -> (type, path) ->
- if path.startsWith(base) and (path[base.length] or '/') is '/'
- type = fs.existsSync(path) and 'update' or 'remove'
- cb type, path
- watchPath: (path, cb) ->
- parent = fp.dirname path
- base = fp.basename path
- @fswatcher = fs.watch parent, recursive: true
- .on 'change', @baseFilter base, cb # TODO: debounce cb
- @
- new Watcher ['app', 'vendor'], (type, path) -> console.log "[#{type}] #{path}"
- ### NOTE: Test as follows:
- watch.coffee &
- mkdir -p app/b/c/d/e
- rm -rf app
- ###
Add Comment
Please, Sign In to add comment