Guest User

Untitled

a guest
Feb 22nd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #!/usr/bin/env coffee
  2.  
  3. fs = require 'fs'
  4. fp = require 'path'
  5. EventEmitter = require 'events'
  6.  
  7. class Watcher extends EventEmitter
  8. constructor: (globs, cb) ->
  9. super()
  10. @watch globs, cb if globs?.length
  11.  
  12. watch: (globs, cb) ->
  13. path = globs[0] # TODO: process all, not just first one
  14. @watchPath path, cb
  15.  
  16. baseFilter: (base, cb) -> (type, path) ->
  17. if path.startsWith(base) and (path[base.length] or '/') is '/'
  18. type = fs.existsSync(path) and 'update' or 'remove'
  19. cb type, path
  20.  
  21. watchPath: (path, cb) ->
  22. parent = fp.dirname path
  23. base = fp.basename path
  24. @fswatcher = fs.watch parent, recursive: true
  25. .on 'change', @baseFilter base, cb # TODO: debounce cb
  26. @
  27.  
  28. new Watcher ['app', 'vendor'], (type, path) -> console.log "[#{type}] #{path}"
  29.  
  30. ### NOTE: Test as follows:
  31.  
  32. watch.coffee &
  33. mkdir -p app/b/c/d/e
  34. rm -rf app
  35.  
  36. ###
Add Comment
Please, Sign In to add comment