Skip to content

Commit 14afddf

Browse files
shakyShaneShane Osbourne
authored andcommitted
feat(watchers): switch to chokidar for file-watching, implement callback interface on per-pattern basis
1 parent 5a66cbd commit 14afddf

File tree

10 files changed

+78
-37
lines changed

10 files changed

+78
-37
lines changed

lib/async.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ module.exports = {
9797
* @param {Function} done
9898
*/
9999
setOptions: function (bs, done) {
100-
101100
done(null, {
102101
options: {
103102
urls: utils.getUrlOptions(bs.options),

lib/cli/cli-options.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,6 @@ opts.callbacks = {
237237
namespaces.core.globs = [];
238238
namespaces.core.objs = [];
239239

240-
if (value === false) {
241-
return false;
242-
}
243-
244240
var processed = opts.makeFilesArg(value);
245241

246242
if (processed.globs.length) {

lib/file-watcher.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
"use strict";
22

3-
var Immutable = require("immutable");
4-
var isFunction = require("lodash/lang/isFunction");
5-
var isString = require("lodash/lang/isString");
63
/**
74
* Plugin interface
85
* @returns {*|function(this:exports)}

lib/hooks.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
var _ = require("lodash");
44
var Immutable = require("immutable");
5-
var isMap = Immutable.Map.isMap;
6-
var isList = Immutable.List.isList;
75
var snippetUtils = require("./snippet").utils;
8-
var utils = require("./utils");
96

107
module.exports = {
118

@@ -81,6 +78,9 @@ module.exports = {
8178
if (pluginOptions) {
8279
opts = Immutable.fromJS(pluginOptions);
8380
opts.forEach(function (value, key) {
81+
if (!value) {
82+
return;
83+
}
8484
var files = value.get("files");
8585
if (files) {
8686
var fileArg = require("./cli/cli-options").makeFilesArg(files);

test/specs/cli/cli.options.files.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
var cli = require("../../../lib/cli/");
44
var merge = cli.options.merge;
5-
var hooks = require("../../../lib/hooks");
65

76
var assert = require("chai").assert;
87

test/specs/e2e/cli/e2e.cli.files.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ describe("E2E CLI `files` arg - multi globs", function () {
3434
instance.cleanup();
3535
});
3636
it("Converts cli files arg to correct namespaced watchers", function () {
37-
assert.equal(instance.options.getIn(["files", "core"]).size, 2);
38-
assert.isTrue(Array.isArray(instance.watchers.core));
37+
assert.equal(instance.options.getIn(["files", "core", "globs"]).size, 2);
38+
assert.isTrue(Array.isArray(instance.watchers.core.watchers));
3939
});
4040
});
4141

@@ -66,8 +66,8 @@ describe("E2E CLI `files` arg, single glob", function () {
6666
instance.cleanup();
6767
});
6868
it("Converts cli files arg to correct namespaced watchers", function () {
69-
assert.equal(instance.options.getIn(["files", "core"]).size, 1);
69+
assert.equal(instance.options.getIn(["files", "core", "globs"]).size, 1);
7070

71-
assert.isTrue(Array.isArray(instance.watchers.core));
71+
assert.isTrue(Array.isArray(instance.watchers.core.watchers));
7272
});
7373
});

test/specs/e2e/e2e.file.watching.js

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ describe("file-watching", function () {
3333

3434
it("Watches files with no namespace", function (done) {
3535

36-
assert.ok(instance.watchers.core);
37-
assert.ok(instance.watchers.core[0]);
36+
assert.ok(instance.watchers.core.watchers);
37+
assert.equal(instance.watchers.core.watchers.length, 1);
3838
done();
3939
});
4040
});
@@ -50,9 +50,7 @@ describe("file-watching", function () {
5050
file = path.join(outpath, "watch-func.txt");
5151

5252
var config = {
53-
files: {
54-
"*.html": true
55-
},
53+
files: "*.html",
5654
logLevel: "silent"
5755
};
5856

@@ -63,10 +61,48 @@ describe("file-watching", function () {
6361
instance.cleanup();
6462
});
6563

66-
it("Watches files when object given", function (done) {
64+
it("Watches files when multi given", function (done) {
6765

68-
assert.ok(instance.watchers.core);
69-
assert.ok(instance.watchers.core[0]);
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);
70106
done();
71107
});
72108
});

test/specs/e2e/e2e.options.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,12 @@ describe("e2e options test", function () {
8888
assert.equal(match, 3500);
8989
});
9090
it("set's the files option", function () {
91+
console.log(instance.options.get("files").toJS());
9192
assert.deepEqual(instance.options.get("files").toJS(), {
92-
core: ["*.html"]
93+
core: {
94+
globs: ["*.html"],
95+
objs: []
96+
}
9397
});
9498
});
9599
});
@@ -326,7 +330,10 @@ describe("e2e options test", function () {
326330

327331
it("Sets the files option with the old API", function () {
328332
assert.deepEqual(instance.options.get("files").toJS(), {
329-
core: ["*.html"]
333+
core: {
334+
globs: ["*.html"],
335+
objs: []
336+
}
330337
});
331338
});
332339
});
@@ -348,7 +355,10 @@ describe("e2e options test", function () {
348355

349356
it("Sets the files option with the old API", function () {
350357
assert.deepEqual(instance.options.get("files").toJS(), {
351-
core: ["*.html"]
358+
core: {
359+
globs: ["*.html"],
360+
objs: []
361+
}
352362
});
353363
});
354364
});
@@ -370,7 +380,10 @@ describe("e2e options test", function () {
370380

371381
it("Sets the files option with the old API", function () {
372382
assert.deepEqual(instance.options.get("files").toJS(), {
373-
core: ["*.html"]
383+
core: {
384+
globs: ["*.html"],
385+
objs: []
386+
}
374387
});
375388
});
376389
});

test/specs/files/files.watching.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ describe("File Watcher Module", function () {
2929

3030
it("Passes options for chokidar", function (done) {
3131
var imm = merge({
32-
files: {
33-
"css/*.css": true
34-
},
32+
files: "css/*.css",
3533
watchOptions: {
3634
debounceDelay: 4000
3735
}
@@ -41,8 +39,8 @@ describe("File Watcher Module", function () {
4139
var emitter = new events.EventEmitter();
4240
var watchers = fileWatcher.plugin(imm, emitter);
4341

44-
assert.equal(watchers["core"][0].options.debounceDelay, 4000);
45-
assert.equal(watchers["core"].length, 1);
42+
assert.equal(watchers.core.watchers.length, 1);
43+
assert.equal(watchers.core.watchers[0].options.debounceDelay, 4000);
4644
done();
4745
});
4846
it("should emit events about changed files in core namespace", function (done) {

test/specs/plugins/bs.options.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@ describe("Plugins: Retrieving options via API", function () {
2121
};
2222

2323
browserSync.use({
24-
2524
plugin: function (opts, bs) {
2625

26+
assert.equal(opts.files, "*.css");
2727
assert.ok(require("immutable").Map.isMap(bs.getOptions()));
2828
instance.cleanup();
29-
done();
3029
},
3130
"plugin:name": "test"
32-
});
3331

34-
instance = browserSync(config).instance;
32+
}, {files: "*.css"});
33+
34+
instance = browserSync(config, function (err, bs) {
35+
assert.equal(bs.watchers.test.watchers.length, 1);
36+
done();
37+
}).instance;
3538
});
3639
});

0 commit comments

Comments
 (0)