1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | require('core-js')
|
11 | var from = require('core-js/library/fn/array/from')
|
12 | var Promise = require('bluebird')
|
13 | var mm = require('minimatch')
|
14 | var Glob = require('glob').Glob
|
15 | var fs = Promise.promisifyAll(require('graceful-fs'))
|
16 | var pathLib = require('path')
|
17 |
|
18 | var File = require('./file')
|
19 | var Url = require('./url')
|
20 | var helper = require('./helper')
|
21 | var _ = helper._
|
22 | var log = require('./logger').create('watcher')
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | var GLOB_OPTS = {
|
28 | cwd: '/',
|
29 | follow: true,
|
30 | nodir: true,
|
31 | sync: true
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function byPath (a, b) {
|
38 | if (a.path > b.path) return 1
|
39 | if (a.path < b.path) return -1
|
40 |
|
41 | return 0
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | var List = function (patterns, excludes, emitter, preprocess, batchInterval) {
|
52 |
|
53 | this._patterns = patterns
|
54 | this._excludes = excludes
|
55 | this._emitter = emitter
|
56 | this._preprocess = Promise.promisify(preprocess)
|
57 | this._batchInterval = batchInterval
|
58 |
|
59 |
|
60 | this.buckets = new Map()
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | this._refreshing = Promise.resolve()
|
69 |
|
70 | var self = this
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | function emit () {
|
76 | self._emitter.emit('file_list_modified', self.files)
|
77 | }
|
78 | var throttledEmit = _.throttle(emit, self._batchInterval, {leading: false})
|
79 | self._emitModified = function (immediate) {
|
80 | immediate ? emit() : throttledEmit()
|
81 | }
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | List.prototype._isExcluded = function (path) {
|
94 | return _.find(this._excludes, function (pattern) {
|
95 | return mm(path, pattern)
|
96 | })
|
97 | }
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | List.prototype._isIncluded = function (path) {
|
105 | return _.find(this._patterns, function (pattern) {
|
106 | return mm(path, pattern.pattern)
|
107 | })
|
108 | }
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | List.prototype._findFile = function (path, pattern) {
|
118 | if (!path || !pattern) return
|
119 | if (!this.buckets.has(pattern.pattern)) return
|
120 |
|
121 | return _.find(from(this.buckets.get(pattern.pattern)), function (file) {
|
122 | return file.originalPath === path
|
123 | })
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | List.prototype._exists = function (path) {
|
132 | var self = this
|
133 |
|
134 | var patterns = this._patterns.filter(function (pattern) {
|
135 | return mm(path, pattern.pattern)
|
136 | })
|
137 |
|
138 | return !!_.find(patterns, function (pattern) {
|
139 | return self._findFile(path, pattern)
|
140 | })
|
141 | }
|
142 |
|
143 |
|
144 | List.prototype._isRefreshing = function () {
|
145 | return this._refreshing.isPending()
|
146 | }
|
147 |
|
148 |
|
149 | List.prototype._refresh = function () {
|
150 | var self = this
|
151 | var buckets = this.buckets
|
152 |
|
153 | return Promise.all(this._patterns.map(function (patternObject) {
|
154 | var pattern = patternObject.pattern
|
155 |
|
156 | if (helper.isUrlAbsolute(pattern)) {
|
157 | buckets.set(pattern, new Set([new Url(pattern)]))
|
158 | return Promise.resolve()
|
159 | }
|
160 |
|
161 | var mg = new Glob(pathLib.normalize(pattern), GLOB_OPTS)
|
162 | var files = mg.found
|
163 | buckets.set(pattern, new Set())
|
164 |
|
165 | if (_.isEmpty(files)) {
|
166 | log.warn('Pattern "%s" does not match any file.', pattern)
|
167 | return
|
168 | }
|
169 |
|
170 | return Promise.all(files.map(function (path) {
|
171 | if (self._isExcluded(path)) {
|
172 | log.debug('Excluded file "%s"', path)
|
173 | return Promise.resolve()
|
174 | }
|
175 |
|
176 | var mtime = mg.statCache[path].mtime
|
177 | var doNotCache = patternObject.nocache
|
178 | var file = new File(path, mtime, doNotCache)
|
179 |
|
180 | if (file.doNotCache) {
|
181 | log.debug('Not preprocessing "%s" due to nocache')
|
182 | return Promise.resolve(file)
|
183 | }
|
184 |
|
185 | return self._preprocess(file).then(function () {
|
186 | return file
|
187 | })
|
188 | }))
|
189 | .then(function (files) {
|
190 | files = _.compact(files)
|
191 |
|
192 | if (_.isEmpty(files)) {
|
193 | log.warn('All files matched by "%s" were excluded.', pattern)
|
194 | } else {
|
195 | buckets.set(pattern, new Set(files))
|
196 | }
|
197 | })
|
198 | }))
|
199 | .cancellable()
|
200 | .then(function () {
|
201 | self.buckets = buckets
|
202 | self._emitModified(true)
|
203 | return self.files
|
204 | })
|
205 | .catch(Promise.CancellationError, function () {
|
206 |
|
207 | return self._refreshing
|
208 | })
|
209 | }
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | Object.defineProperty(List.prototype, 'files', {
|
215 | get: function () {
|
216 | var self = this
|
217 |
|
218 | var served = this._patterns.filter(function (pattern) {
|
219 | return pattern.served
|
220 | })
|
221 | .map(function (p) {
|
222 | return from(self.buckets.get(p.pattern) || []).sort(byPath)
|
223 | })
|
224 |
|
225 | var included = this._patterns.filter(function (pattern) {
|
226 | return pattern.included
|
227 | })
|
228 | .map(function (p) {
|
229 | return from(self.buckets.get(p.pattern) || []).sort(byPath)
|
230 | })
|
231 |
|
232 | var uniqFlat = function (list) {
|
233 | return _.uniq(_.flatten(list), 'path')
|
234 | }
|
235 |
|
236 | return {
|
237 | served: uniqFlat(served),
|
238 | included: uniqFlat(included)
|
239 | }
|
240 | }
|
241 | })
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | List.prototype.refresh = function () {
|
248 | if (this._isRefreshing()) {
|
249 | this._refreshing.cancel()
|
250 | }
|
251 |
|
252 | this._refreshing = this._refresh()
|
253 | return this._refreshing
|
254 | }
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 | List.prototype.reload = function (patterns, excludes) {
|
265 | this._patterns = patterns
|
266 | this._excludes = excludes
|
267 |
|
268 |
|
269 |
|
270 | return this.refresh()
|
271 | }
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 | List.prototype.addFile = function (path) {
|
281 | var self = this
|
282 |
|
283 |
|
284 | var excluded = this._isExcluded(path)
|
285 | if (excluded) {
|
286 | log.debug('Add file "%s" ignored. Excluded by "%s".', path, excluded)
|
287 |
|
288 | return Promise.resolve(this.files)
|
289 | }
|
290 |
|
291 | var pattern = this._isIncluded(path)
|
292 |
|
293 | if (!pattern) {
|
294 | log.debug('Add file "%s" ignored. Does not match any pattern.', path)
|
295 | return Promise.resolve(this.files)
|
296 | }
|
297 |
|
298 | if (this._exists(path)) {
|
299 | log.debug('Add file "%s" ignored. Already in the list.', path)
|
300 | return Promise.resolve(this.files)
|
301 | }
|
302 |
|
303 | var file = new File(path)
|
304 | this.buckets.get(pattern.pattern).add(file)
|
305 |
|
306 | return Promise.all([
|
307 | fs.statAsync(path),
|
308 | this._refreshing
|
309 | ]).spread(function (stat) {
|
310 | file.mtime = stat.mtime
|
311 | return self._preprocess(file)
|
312 | })
|
313 | .then(function () {
|
314 | log.info('Added file "%s".', path)
|
315 | self._emitModified()
|
316 | return self.files
|
317 | })
|
318 | }
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 | List.prototype.changeFile = function (path) {
|
328 | var self = this
|
329 |
|
330 | var pattern = this._isIncluded(path)
|
331 | var file = this._findFile(path, pattern)
|
332 |
|
333 | if (!pattern || !file) {
|
334 | log.debug('Changed file "%s" ignored. Does not match any file in the list.', path)
|
335 | return Promise.resolve(this.files)
|
336 | }
|
337 |
|
338 | return Promise.all([
|
339 | fs.statAsync(path),
|
340 | this._refreshing
|
341 | ]).spread(function (stat) {
|
342 | if (stat.mtime <= file.mtime) throw new Promise.CancellationError()
|
343 |
|
344 | file.mtime = stat.mtime
|
345 | return self._preprocess(file)
|
346 | })
|
347 | .then(function () {
|
348 | log.info('Changed file "%s".', path)
|
349 | self._emitModified()
|
350 | return self.files
|
351 | })
|
352 | .catch(Promise.CancellationError, function () {
|
353 | return self.files
|
354 | })
|
355 | }
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 | List.prototype.removeFile = function (path) {
|
365 | var self = this
|
366 |
|
367 | return Promise.try(function () {
|
368 | var pattern = self._isIncluded(path)
|
369 | var file = self._findFile(path, pattern)
|
370 |
|
371 | if (!pattern || !file) {
|
372 | log.debug('Removed file "%s" ignored. Does not match any file in the list.', path)
|
373 | return self.files
|
374 | }
|
375 |
|
376 | self.buckets.get(pattern.pattern).delete(file)
|
377 |
|
378 | log.info('Removed file "%s".', path)
|
379 | self._emitModified()
|
380 | return self.files
|
381 | })
|
382 | }
|
383 |
|
384 |
|
385 | List.$inject = ['config.files', 'config.exclude', 'emitter', 'preprocess',
|
386 | 'config.autoWatchBatchDelay']
|
387 |
|
388 |
|
389 | module.exports = List
|