-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
175 lines (139 loc) · 3.71 KB
/
index.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict'
const fs = require('fs')
const path = require('path')
const level = require('levelup')
const Watch = require('./watch')
const defaultPath = path.join(__dirname, '.cache')
module.exports = function (opts, cb) {
const cache = level(opts.cache || defaultPath, opts)
let paused = !!opts.paused
let watch = Watch(opts)
cache.on('pause', () => {
paused = true
})
cache.on('resume', () => {
paused = false
cache.emit('resumed')
})
cache.remove = (filepaths, cb) => {
paused = true
if (!filepaths.length) return
filepaths = filepaths.map(p => {
return { type: 'del', key: p }
})
cache.batch(filepaths, cb)
}
cache.add = (filepaths, cb) => {
paused = true
if (!filepaths.length) return
filepaths = filepaths.map(p => {
return { type: 'put', key: p, value: '' }
})
cache.batch(filepaths, cb)
}
const readFiles = (dir, done) => {
fs.readdir(dir, (err, list) => {
if (err) return done(err)
let i = 0
;(function next () {
let file = list[i++]
if (!file) return done(null)
let filepath = path.join(dir, file)
fs.stat(filepath, (err, stat) => {
if (err) return done(err)
if (stat && stat.isDirectory()) {
return readFiles(filepath, next)
} else if (!opts.pattern.test(file)) {
return next()
}
cache.get(filepath, (err, value) => {
if (!err) return next()
cache.put(filepath, '', (err) => {
if (err) return cb(err)
if (paused) return next()
watch.emit('added', filepath)
next()
})
})
})
})()
})
}
const ready = () => {
const removed = (p, isDirectory) => {
if (paused) return
if (isDirectory) {
const rs = cache
.createReadStream({
gte: p,
lte: p + '~',
values: false
})
rs.on('data', key => {
cache.get(key, (err) => {
if (err) return
cache.del(key, (err) => {
if (err) return
watch.emit('removed', key)
})
})
})
} else {
cache.del(p, (err) => {
if (err) return
watch.emit('removed', p)
})
}
}
const modified = (p, isDirectory) => {
if (paused) return
if (isDirectory) {
watch.addDir(p)
return readFiles(p, (err) => {
if (err) watch.emit('error', err)
})
}
cache.get(p, err => {
if (!err) return watch.emit('modified', p)
cache.put(p, '', err => {
if (err) return watch.emit('error', err)
watch.emit('added', p)
})
})
}
watch.on('_removed', removed)
watch.on('_modified', modified)
}
let waiting = 0
cache
.createReadStream({ values: false })
.on('data', key => {
++waiting
// check to see if the file that we know about exists,
// if not, complain that it has been deleted from the
// watched directory. Unless the watcher starts in the
// "paused" state.
fs.stat(key, (err, _) => {
if (!err) {
--waiting
return
}
cache.del(key, (err) => {
--waiting
if (err) return watch.emit('error', err)
if (!paused) watch.emit('removed', key)
})
})
})
.on('end', () => {
let wait = setInterval(() => {
if (!waiting) {
clearInterval(wait)
watch.emit('ready')
readFiles(opts.dir, ready)
}
}, 100)
})
process.nextTick(() => cb(null, watch))
return cache
}