Skip to content

Commit

Permalink
feat(watchers): switch to chokidar for file-watching, implement callb…
Browse files Browse the repository at this point in the history
…ack interface on per-pattern basis
  • Loading branch information
shakyShane authored and Shane Osbourne committed Apr 9, 2015
1 parent 5a66cbd commit 14afddf
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 37 deletions.
1 change: 0 additions & 1 deletion lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ module.exports = {
* @param {Function} done
*/
setOptions: function (bs, done) {

done(null, {
options: {
urls: utils.getUrlOptions(bs.options),
Expand Down
4 changes: 0 additions & 4 deletions lib/cli/cli-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,6 @@ opts.callbacks = {
namespaces.core.globs = [];
namespaces.core.objs = [];

if (value === false) {
return false;
}

var processed = opts.makeFilesArg(value);

if (processed.globs.length) {
Expand Down
3 changes: 0 additions & 3 deletions lib/file-watcher.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
"use strict";

var Immutable = require("immutable");
var isFunction = require("lodash/lang/isFunction");
var isString = require("lodash/lang/isString");
/**
* Plugin interface
* @returns {*|function(this:exports)}
Expand Down
6 changes: 3 additions & 3 deletions lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

var _ = require("lodash");
var Immutable = require("immutable");
var isMap = Immutable.Map.isMap;
var isList = Immutable.List.isList;
var snippetUtils = require("./snippet").utils;
var utils = require("./utils");

module.exports = {

Expand Down Expand Up @@ -81,6 +78,9 @@ module.exports = {
if (pluginOptions) {
opts = Immutable.fromJS(pluginOptions);
opts.forEach(function (value, key) {
if (!value) {
return;
}
var files = value.get("files");
if (files) {
var fileArg = require("./cli/cli-options").makeFilesArg(files);
Expand Down
1 change: 0 additions & 1 deletion test/specs/cli/cli.options.files.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var cli = require("../../../lib/cli/");
var merge = cli.options.merge;
var hooks = require("../../../lib/hooks");

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

Expand Down
8 changes: 4 additions & 4 deletions test/specs/e2e/cli/e2e.cli.files.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ describe("E2E CLI `files` arg - multi globs", function () {
instance.cleanup();
});
it("Converts cli files arg to correct namespaced watchers", function () {
assert.equal(instance.options.getIn(["files", "core"]).size, 2);
assert.isTrue(Array.isArray(instance.watchers.core));
assert.equal(instance.options.getIn(["files", "core", "globs"]).size, 2);
assert.isTrue(Array.isArray(instance.watchers.core.watchers));
});
});

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

assert.isTrue(Array.isArray(instance.watchers.core));
assert.isTrue(Array.isArray(instance.watchers.core.watchers));
});
});
52 changes: 44 additions & 8 deletions test/specs/e2e/e2e.file.watching.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ describe("file-watching", function () {

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

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

var config = {
files: {
"*.html": true
},
files: "*.html",
logLevel: "silent"
};

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

it("Watches files when object given", function (done) {
it("Watches files when multi given", function (done) {

assert.ok(instance.watchers.core);
assert.ok(instance.watchers.core[0]);
assert.ok(instance.watchers.core.watchers);
assert.ok(instance.watchers.core.watchers[0]);
done();
});
});

describe("E2E Adding namespaced watchers", function () {

var instance, file;

before(function (done) {

browserSync.reset();

file = path.join(outpath, "watch-func.txt");

var config = {
files: [
"*.html",
{
match: "*.css",
fn: function (event, file) {
console.log(file);
}
}
],
logLevel: "silent"
};

instance = browserSync(config, done).instance;
});

after(function () {
instance.cleanup();
});

it("Watches files when multi given + objs", function (done) {

assert.ok(instance.watchers.core.watchers);
assert.equal(instance.watchers.core.watchers.length, 2);
done();
});
});
Expand Down
21 changes: 17 additions & 4 deletions test/specs/e2e/e2e.options.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ describe("e2e options test", function () {
assert.equal(match, 3500);
});
it("set's the files option", function () {
console.log(instance.options.get("files").toJS());
assert.deepEqual(instance.options.get("files").toJS(), {
core: ["*.html"]
core: {
globs: ["*.html"],
objs: []
}
});
});
});
Expand Down Expand Up @@ -326,7 +330,10 @@ describe("e2e options test", function () {

it("Sets the files option with the old API", function () {
assert.deepEqual(instance.options.get("files").toJS(), {
core: ["*.html"]
core: {
globs: ["*.html"],
objs: []
}
});
});
});
Expand All @@ -348,7 +355,10 @@ describe("e2e options test", function () {

it("Sets the files option with the old API", function () {
assert.deepEqual(instance.options.get("files").toJS(), {
core: ["*.html"]
core: {
globs: ["*.html"],
objs: []
}
});
});
});
Expand All @@ -370,7 +380,10 @@ describe("e2e options test", function () {

it("Sets the files option with the old API", function () {
assert.deepEqual(instance.options.get("files").toJS(), {
core: ["*.html"]
core: {
globs: ["*.html"],
objs: []
}
});
});
});
Expand Down
8 changes: 3 additions & 5 deletions test/specs/files/files.watching.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ describe("File Watcher Module", function () {

it("Passes options for chokidar", function (done) {
var imm = merge({
files: {
"css/*.css": true
},
files: "css/*.css",
watchOptions: {
debounceDelay: 4000
}
Expand All @@ -41,8 +39,8 @@ describe("File Watcher Module", function () {
var emitter = new events.EventEmitter();
var watchers = fileWatcher.plugin(imm, emitter);

assert.equal(watchers["core"][0].options.debounceDelay, 4000);
assert.equal(watchers["core"].length, 1);
assert.equal(watchers.core.watchers.length, 1);
assert.equal(watchers.core.watchers[0].options.debounceDelay, 4000);
done();
});
it("should emit events about changed files in core namespace", function (done) {
Expand Down
11 changes: 7 additions & 4 deletions test/specs/plugins/bs.options.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ describe("Plugins: Retrieving options via API", function () {
};

browserSync.use({

plugin: function (opts, bs) {

assert.equal(opts.files, "*.css");
assert.ok(require("immutable").Map.isMap(bs.getOptions()));
instance.cleanup();
done();
},
"plugin:name": "test"
});

instance = browserSync(config).instance;
}, {files: "*.css"});

instance = browserSync(config, function (err, bs) {
assert.equal(bs.watchers.test.watchers.length, 1);
done();
}).instance;
});
});

0 comments on commit 14afddf

Please sign in to comment.