-
Notifications
You must be signed in to change notification settings - Fork 1
/
watch.js
46 lines (33 loc) · 1.09 KB
/
watch.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
"use strict";
var defaults = require("lodash/object/defaults"),
flattenDeep = require("lodash/array/flattenDeep"),
last = require("lodash/array/last"),
watchGlobs = require("glob-watcher"),
common = require("./common"),
normalizeOptions = common.normalizeOptions,
popOptions = common.popOptions;
module.exports = watch;
function watch(/* globs..., [options] */) {
var globs = flattenDeep(arguments),
options = popOptions(globs),
debug = options.debug;
var watcher = watchGlobs(globs);
watcher.on("change",function(evt) {
debug("File changed:",evt.path);
});
return middlewareFactory.bind(null,watcher,options);
}
function middlewareFactory(watcher, defaultOptions, builder, options) {
var rebuild = builder.rebuild,
wait = builder.wait;
options = normalizeOptions(defaults(options,defaultOptions));
watcher.on("change",rebuild.bind(builder,options.eager));
middleware.wait = wait;
middleware.rebuild = rebuild;
if (options.eager)
rebuild(true);
return middleware;
function middleware(req, res, next) {
wait(next);
}
}