From 14afddfc67d2a91bbc597e0f5a7e31874e694fc8 Mon Sep 17 00:00:00 2001 From: Shane Osbourne Date: Mon, 23 Mar 2015 21:55:24 +0000 Subject: [PATCH] feat(watchers): switch to chokidar for file-watching, implement callback interface on per-pattern basis --- lib/async.js | 1 - lib/cli/cli-options.js | 4 --- lib/file-watcher.js | 3 -- lib/hooks.js | 6 ++-- test/specs/cli/cli.options.files.js | 1 - test/specs/e2e/cli/e2e.cli.files.js | 8 ++--- test/specs/e2e/e2e.file.watching.js | 52 ++++++++++++++++++++++++----- test/specs/e2e/e2e.options.js | 21 +++++++++--- test/specs/files/files.watching.js | 8 ++--- test/specs/plugins/bs.options.js | 11 +++--- 10 files changed, 78 insertions(+), 37 deletions(-) diff --git a/lib/async.js b/lib/async.js index 65261e68c..988754254 100644 --- a/lib/async.js +++ b/lib/async.js @@ -97,7 +97,6 @@ module.exports = { * @param {Function} done */ setOptions: function (bs, done) { - done(null, { options: { urls: utils.getUrlOptions(bs.options), diff --git a/lib/cli/cli-options.js b/lib/cli/cli-options.js index 7bb5984d8..8719060c7 100644 --- a/lib/cli/cli-options.js +++ b/lib/cli/cli-options.js @@ -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) { diff --git a/lib/file-watcher.js b/lib/file-watcher.js index 677c2bd21..cfe24e7d5 100644 --- a/lib/file-watcher.js +++ b/lib/file-watcher.js @@ -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)} diff --git a/lib/hooks.js b/lib/hooks.js index 81e9194d8..175397a02 100644 --- a/lib/hooks.js +++ b/lib/hooks.js @@ -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 = { @@ -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); diff --git a/test/specs/cli/cli.options.files.js b/test/specs/cli/cli.options.files.js index a120d4ab6..4ac1b8095 100644 --- a/test/specs/cli/cli.options.files.js +++ b/test/specs/cli/cli.options.files.js @@ -2,7 +2,6 @@ var cli = require("../../../lib/cli/"); var merge = cli.options.merge; -var hooks = require("../../../lib/hooks"); var assert = require("chai").assert; diff --git a/test/specs/e2e/cli/e2e.cli.files.js b/test/specs/e2e/cli/e2e.cli.files.js index a08bc6f4c..a6837f1c1 100644 --- a/test/specs/e2e/cli/e2e.cli.files.js +++ b/test/specs/e2e/cli/e2e.cli.files.js @@ -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)); }); }); @@ -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)); }); }); diff --git a/test/specs/e2e/e2e.file.watching.js b/test/specs/e2e/e2e.file.watching.js index 87157f78d..cffdc1a0e 100644 --- a/test/specs/e2e/e2e.file.watching.js +++ b/test/specs/e2e/e2e.file.watching.js @@ -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(); }); }); @@ -50,9 +50,7 @@ describe("file-watching", function () { file = path.join(outpath, "watch-func.txt"); var config = { - files: { - "*.html": true - }, + files: "*.html", logLevel: "silent" }; @@ -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(); }); }); diff --git a/test/specs/e2e/e2e.options.js b/test/specs/e2e/e2e.options.js index 58656188f..fffce79ed 100644 --- a/test/specs/e2e/e2e.options.js +++ b/test/specs/e2e/e2e.options.js @@ -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: [] + } }); }); }); @@ -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: [] + } }); }); }); @@ -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: [] + } }); }); }); @@ -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: [] + } }); }); }); diff --git a/test/specs/files/files.watching.js b/test/specs/files/files.watching.js index c1fbe97c4..5c8266000 100644 --- a/test/specs/files/files.watching.js +++ b/test/specs/files/files.watching.js @@ -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 } @@ -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) { diff --git a/test/specs/plugins/bs.options.js b/test/specs/plugins/bs.options.js index a97df52ad..b338e12f3 100644 --- a/test/specs/plugins/bs.options.js +++ b/test/specs/plugins/bs.options.js @@ -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; }); });