This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 604
Expand file tree
/
Copy pathwatcher.coffee
More file actions
executable file
·75 lines (64 loc) · 2.24 KB
/
watcher.coffee
File metadata and controls
executable file
·75 lines (64 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
fs = require("fs")
nodePath = require 'path'
{EventEmitter} = require 'events'
walk = (options, callback) ->
{dir,groupName} = options
results = []
fs.readdir dir, (err, list) ->
return callback(err) if err
pending = list.length
return callback(null, results) unless pending
list.forEach (file) ->
file = dir + "/" + file
fs.stat file, (err, stat) ->
if stat and stat.isDirectory()
walk {dir:file}, (err, res) ->
results = results.concat(res)
callback null, results unless --pending
else
results.push {file : nodePath.normalize(file), mtime : stat.mtime, groupName}
callback null, results unless --pending
module.exports = class Watcher extends EventEmitter
constructor:(@options)->
@watchList = {}
@init @options
@options.interval ?= 1000
@on "ready",()=>
setInterval =>
@startWatching()
,@options.interval
init:(options)->
that = @
groupCount = 0
groupCount++ for groupName,val of options.groups
for groupName,val of options.groups
do (groupName,val) ->
for folder in val.folders
do (groupName,folder)->
walk {dir:folder,groupName},(err,res)->
watchList = {}
for file in res
that.watchList[file.file] = {mtime : file.mtime, groupName}
groupCount--
that.emit "ready",watchList if groupCount is 0
startWatching:(fileList = @watchList) ->
watcher = @
for path,info of fileList
do (path,info) ->
fs.stat path, (err, stat) ->
if stat.mtime > info.mtime
change = {path,groupName:info.groupName}
console.log "change happened",change
watcher.emit "change", change
watcher.options.groups[info.groupName].onChange? change.path
watcher.options.onChange? change
info.mtime = stat.mtime
# a = new Watcher
# groups:
# a :
# folders : ['../../client']
# onRead : (file,callback) ->
# onChange : (path) ->
# console.log "#{path} has changed."
# onChange: (change)->
# console.log "#{change.path} has changed on group #{change.groupName}"