Skip to content

Commit

Permalink
fix(file-watching): bind public running instance to watch callbacks g…
Browse files Browse the repository at this point in the history
…iven in options - fixes #631
  • Loading branch information
shakyShane committed May 21, 2015
1 parent b45f630 commit d7c96e4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 50 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ function create(name, emitter) {
var instance = {
name: name,
instance: browserSync,
init: require("./lib/public/init")(browserSync, name, pjson),
exit: require("./lib/public/exit")(browserSync),
notify: require("./lib/public/notify")(browserSync),
pause: require("./lib/public/pause")(browserSync),
Expand All @@ -301,6 +300,9 @@ function create(name, emitter) {
watch: require("./lib/file-watcher").watch
};

browserSync.publicInstance = instance;
instance.init = require("./lib/public/init")(browserSync, name, pjson);

Object.defineProperty(instance, "active", {
get: function () {
return browserSync.active;
Expand Down
2 changes: 1 addition & 1 deletion lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ module.exports = {
setFileWatchers: function (bs, done) {
done(null, {
instance: {
watchers: bs.pluginManager.get("file:watcher")(bs.options, bs.events)
watchers: bs.pluginManager.get("file:watcher")(bs)
}
});
},
Expand Down
7 changes: 5 additions & 2 deletions lib/file-watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ var _ = require("lodash");
* Plugin interface
* @returns {*|function(this:exports)}
*/
module.exports.plugin = function (options, emitter) {
module.exports.plugin = function (bs) {

var options = bs.options;
var emitter = bs.emitter;

var defaultWatchOptions = require("immutable").Map({
ignored: /[\/\\]\./
Expand Down Expand Up @@ -41,7 +44,7 @@ module.exports.plugin = function (options, emitter) {

if (jsItem.objs.length) {
jsItem.objs.forEach(function (item) {
var watcher = watch(item.match, item.options || defaultWatchOptions, item.fn);
var watcher = watch(item.match, item.options || defaultWatchOptions, item.fn.bind(bs.publicInstance));
if (!map[namespace]) {
map[namespace] = {
watchers: [watcher]
Expand Down
74 changes: 33 additions & 41 deletions test/specs/files/files.watching.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,13 @@ var merge = require("../../../lib/cli/cli-options").merge;

var events = require("events");
var path = require("path");
var fs = require("graceful-fs");
var assert = require("chai").assert;

var outpath = path.join(__dirname, "../../fixtures");

var tempFileContent = "A test generated this file and it is safe to delete";

var writeTimeout = 500; // Wait for it to get to the filesystem

var writeFileWait = function (name, content, cb) {
if (!cb) {
cb = function () {};
}
setTimeout(function () {
fs.writeFile(name, content, cb);
}, writeTimeout);
};

describe("File Watcher Module", function () {

it("Passes options for chokidar", function (done) {
it("Passes options for chokidar", function () {
var imm = merge({
files: "css/*.css",
watchOptions: {
Expand All @@ -37,13 +23,12 @@ describe("File Watcher Module", function () {
imm = imm.set("files", hooks["files:watch"]([], imm.get("files"), {}));

var emitter = new events.EventEmitter();
var watchers = fileWatcher.plugin(imm, emitter);
var watchers = fileWatcher.plugin({options: imm, emitter: emitter});

assert.equal(watchers.core.watchers.length, 1);
assert.equal(watchers.core.watchers[0].options.debounceDelay, 4000);
done();
});
it("Passes separate options for chokidar when multi given", function (done) {
it("Passes separate options for chokidar when multi given", function () {
var imm = merge({
files: [
"css/*.css",
Expand All @@ -64,39 +49,46 @@ describe("File Watcher Module", function () {
imm = imm.set("files", hooks["files:watch"]([], imm.get("files"), {}));

var emitter = new events.EventEmitter();
var watchers = fileWatcher.plugin(imm, emitter);
var watchers = fileWatcher.plugin({options: imm, emitter: emitter});

assert.equal(watchers.core.watchers.length, 2);
assert.equal(watchers.core.watchers[0].options.interval, 200);
assert.equal(watchers.core.watchers[1].options.interval, 100);
done();
});
it("should emit events about changed files in core namespace", function (done) {

var tempFile = path.join(outpath, "watch-func.txt");
var called = false;

fs.writeFile(tempFile, tempFileContent, function () {
// assert: it works if it calls done
var bs = browserSync.create();

// assert: it works if it calls done
browserSync.reset();
browserSync.create().init({
files: tempFile,
ui: false,
online: false,
logSnippet: false
}, function (err, bs) {

bs.events.on("file:changed", function (data) {
assert.equal(data.namespace, "core");
assert.equal(data.path, path.resolve(tempFile));
bs.cleanup();
done();
});

// act: change file
writeFileWait(tempFile, tempFileContent + " changed");
writeFileWait(tempFile, tempFileContent + " changed");
});
bs.init({
files: [
{
options: {
ignoreInitial: true
},
match: tempFile,
fn: function (event, file) {
assert.equal(event, "change");
assert.equal(file, tempFile);
assert.isFunction(this.reload);
assert.isFunction(this.notify);
bs.cleanup();
if (!called) {
done();
called = true;
}
}
}
],
ui: false,
online: false,
logSnippet: false,
logLevel: "silent"
}, function (err, bs) {
bs.watchers.core.watchers[0]._events.all("change", tempFile);
});
});
});
10 changes: 5 additions & 5 deletions test/specs/hooks/files.watch.hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ describe("files:watch hook", function () {
var out = hook([], imm.get("files"), pluginOptions);
imm = imm.set("files", out);

var watchers = require("../../../lib/file-watcher").plugin(imm, {});
var watchers = require("../../../lib/file-watcher").plugin({options: imm});
assert.equal(watchers.core.watchers.length, 2);
assert.equal(watchers.plugin1.watchers.length, 2);
});
Expand Down Expand Up @@ -238,7 +238,7 @@ describe("files:watch hook", function () {

imm = imm.set("files", out);

var watchers = require("../../../lib/file-watcher").plugin(imm, {});
var watchers = require("../../../lib/file-watcher").plugin({options: imm});

assert.equal(3, watchers.core.watchers.length);
assert.equal(2, watchers.plugin1.watchers.length);
Expand Down Expand Up @@ -268,7 +268,7 @@ describe("files:watch hook", function () {

imm = imm.set("files", out);

var watchers = require("../../../lib/file-watcher").plugin(imm, {});
var watchers = require("../../../lib/file-watcher").plugin({options: imm});

assert.equal(1, watchers.core.watchers.length);
});
Expand Down Expand Up @@ -296,7 +296,7 @@ describe("files:watch hook", function () {

imm = imm.set("files", out);

var watchers = require("../../../lib/file-watcher").plugin(imm, {});
var watchers = require("../../../lib/file-watcher").plugin({options: imm});

assert.equal(1, watchers.plugin1.watchers.length);
});
Expand Down Expand Up @@ -326,7 +326,7 @@ describe("files:watch hook", function () {

imm = imm.set("files", out);

var watchers = require("../../../lib/file-watcher").plugin(imm, {});
var watchers = require("../../../lib/file-watcher").plugin({options: imm});

assert.equal(2, watchers.plugin1.watchers.length);
assert.equal(1, watchers.core.watchers.length);
Expand Down

0 comments on commit d7c96e4

Please sign in to comment.