Skip to content

Commit 3c069fb

Browse files
shakyShaneShane Osbourne
authored andcommitted
feat(watchers): Allow per-watcher options hash.
1 parent e74250f commit 3c069fb

File tree

7 files changed

+172
-26
lines changed

7 files changed

+172
-26
lines changed

lib/default-config.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,23 @@ module.exports = {
4040
* @since 1.3.0
4141
*/
4242
watchOptions: {
43-
ignoreInitial: true
43+
/**
44+
*
45+
persistent: true,
46+
47+
ignored: '*.txt',
48+
ignoreInitial: false,
49+
followSymlinks: true,
50+
cwd: '.',
51+
52+
usePolling: true,
53+
alwaysStat: false,
54+
depth: undefined,
55+
interval: 100,
56+
57+
ignorePermissionErrors: false,
58+
atomic: true
59+
*/
4460
},
4561

4662
/**

lib/file-watcher.js

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@
66
*/
77
module.exports.plugin = function (options, emitter) {
88

9-
// Options to pass along to Gaze
10-
var watchOptions = options.get("watchOptions") || options.get("watchoptions");
9+
var defaultWatchOptions = require("immutable").Map({
10+
ignored: /[\/\\]\./
11+
})
12+
.mergeDeep(
13+
options.get("watchOptions") || options.get("watchoptions")
14+
)
15+
.toJS();
1116

12-
if (watchOptions) {
13-
watchOptions = watchOptions.toJS();
14-
} else {
15-
watchOptions = {};
16-
}
17-
18-
var globs = options.get("files");
19-
20-
return globs.reduce(function (map, glob, namespace) {
17+
return options.get("files").reduce(function (map, glob, namespace) {
2118

2219
/**
2320
* Default CB when not given
@@ -37,21 +34,21 @@ module.exports.plugin = function (options, emitter) {
3734
if (jsItem.globs.length) {
3835
if (!map[namespace]) {
3936
map[namespace] = {
40-
watchers: [getWatcher(jsItem.globs, watchOptions, fn)]
37+
watchers: [getWatcher(jsItem.globs, defaultWatchOptions, fn)]
4138
};
4239
} else {
43-
map[namespace].watchers.push(getWatcher(jsItem.globs, watchOptions, fn));
40+
map[namespace].watchers.push(getWatcher(jsItem.globs, defaultWatchOptions, fn));
4441
}
4542
}
4643

4744
if (jsItem.objs.length) {
4845
jsItem.objs.forEach(function (item) {
4946
if (!map[namespace]) {
5047
map[namespace] = {
51-
watchers: [getWatcher(item.match, watchOptions, item.fn)]
48+
watchers: [getWatcher(item.match, item.options || defaultWatchOptions, item.fn)]
5249
};
5350
} else {
54-
map[namespace].watchers.push(getWatcher(item.match, watchOptions, item.fn));
51+
map[namespace].watchers.push(getWatcher(item.match, item.options || defaultWatchOptions, item.fn));
5552
}
5653
});
5754
}
@@ -62,10 +59,5 @@ module.exports.plugin = function (options, emitter) {
6259
};
6360

6461
function getWatcher (patterns, opts, fn) {
65-
66-
if (!opts.ignored) {
67-
opts.ignored = /[\/\\]\./;
68-
}
69-
7062
return require("chokidar").watch(patterns, opts).on("all", fn);
7163
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
"pre-release": "npm test && npm run pro-local && npm run pro"
3535
},
3636
"dependencies": {
37-
"anymatch": "^1.1.0",
37+
"anymatch": "^1.2.1",
3838
"async-each-series": "^0.1.1",
3939
"browser-sync-client": "^1.0.1",
40-
"browser-sync-ui": "^0.5.2",
41-
"chokidar": "^1.0.0-rc5",
40+
"browser-sync-ui": "^0.5.3",
41+
"chokidar": "^1.0.0-rc6",
4242
"connect": "^3.3.5",
4343
"dev-ip": "^1.0.1",
4444
"easy-extender": "^2.3.0",

test/specs/e2e/e2e.file.changed.js renamed to test/specs/e2e/files/e2e.file.changed.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
var browserSync = require("../../../index");
3+
var browserSync = require("../../../../index");
44

55
var path = require("path");
66
var sinon = require("sinon");
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"use strict";
2+
3+
var browserSync = require("../../../../");
4+
5+
var path = require("path");
6+
var assert = require("chai").assert;
7+
8+
var outpath = path.join(__dirname, "../../fixtures");
9+
10+
describe("file-watching", function () {
11+
12+
describe("E2E Adding namespaced watchers", function () {
13+
14+
var instance, file;
15+
16+
before(function (done) {
17+
18+
browserSync.reset();
19+
20+
file = path.join(outpath, "watch-func.txt");
21+
22+
var config = {
23+
files: file,
24+
logLevel: "silent"
25+
};
26+
27+
instance = browserSync(config, done).instance;
28+
});
29+
30+
after(function () {
31+
instance.cleanup();
32+
});
33+
34+
it("Watches files with no namespace", function (done) {
35+
36+
assert.ok(instance.watchers.core.watchers);
37+
assert.equal(instance.watchers.core.watchers.length, 1);
38+
done();
39+
});
40+
});
41+
42+
describe("E2E Adding namespaced watchers", function () {
43+
44+
var instance, file;
45+
46+
before(function (done) {
47+
48+
browserSync.reset();
49+
50+
file = path.join(outpath, "watch-func.txt");
51+
52+
var config = {
53+
files: "*.html",
54+
logLevel: "silent"
55+
};
56+
57+
instance = browserSync(config, done).instance;
58+
});
59+
60+
after(function () {
61+
instance.cleanup();
62+
});
63+
64+
it("Watches files when multi given", function (done) {
65+
66+
assert.ok(instance.watchers.core.watchers);
67+
assert.ok(instance.watchers.core.watchers[0]);
68+
done();
69+
});
70+
});
71+
72+
describe("E2E Adding namespaced watchers", function () {
73+
74+
var instance, file;
75+
76+
before(function (done) {
77+
78+
browserSync.reset();
79+
80+
file = path.join(outpath, "watch-func.txt");
81+
82+
var config = {
83+
files: [
84+
"*.html",
85+
{
86+
match: "*.css",
87+
fn: function (event, file) {
88+
console.log(file);
89+
}
90+
}
91+
],
92+
logLevel: "silent"
93+
};
94+
95+
instance = browserSync(config, done).instance;
96+
});
97+
98+
after(function () {
99+
instance.cleanup();
100+
});
101+
102+
it("Watches files when multi given + objs", function (done) {
103+
104+
assert.ok(instance.watchers.core.watchers);
105+
assert.equal(instance.watchers.core.watchers.length, 2);
106+
done();
107+
});
108+
});
109+
});

test/specs/e2e/e2e.file.watching.js renamed to test/specs/e2e/files/e2e.file.watching.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
var browserSync = require("../../../");
3+
var browserSync = require("../../../../");
44

55
var path = require("path");
66
var assert = require("chai").assert;

test/specs/files/files.watching.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,34 @@ describe("File Watcher Module", function () {
4343
assert.equal(watchers.core.watchers[0].options.debounceDelay, 4000);
4444
done();
4545
});
46+
it("Passes separate options for chokidar when multi given", function (done) {
47+
var imm = merge({
48+
files: [
49+
"css/*.css",
50+
{
51+
match: "*.html",
52+
fn: function (event) {
53+
console.log(event);
54+
},
55+
options: {
56+
interval: 100
57+
}
58+
}
59+
],
60+
watchOptions: {
61+
interval: 200
62+
}
63+
});
64+
imm = imm.set("files", hooks["files:watch"]([], imm.get("files"), {}));
65+
66+
var emitter = new events.EventEmitter();
67+
var watchers = fileWatcher.plugin(imm, emitter);
68+
69+
assert.equal(watchers.core.watchers.length, 2);
70+
assert.equal(watchers.core.watchers[0].options.interval, 200);
71+
assert.equal(watchers.core.watchers[1].options.interval, 100);
72+
done();
73+
});
4674
it("should emit events about changed files in core namespace", function (done) {
4775

4876
var tempFile = path.join(outpath, "watch-func.txt");
@@ -67,6 +95,7 @@ describe("File Watcher Module", function () {
6795

6896
// act: change file
6997
writeFileWait(tempFile, tempFileContent + " changed");
98+
writeFileWait(tempFile, tempFileContent + " changed");
7099
});
71100
});
72101
});

0 commit comments

Comments
 (0)