Skip to content

Commit

Permalink
fix(files): added watchEvents option
Browse files Browse the repository at this point in the history
 - this will allow the user to provide an array of supported file event names to which Browsersync will respond.

  fixes #1291

  #1291
  • Loading branch information
shakyShane committed Feb 13, 2017
1 parent 5d1e084 commit de2e2fa
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 7 deletions.
4 changes: 4 additions & 0 deletions lib/cli/opts.start.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@
"type": "number",
"desc": "Specify a port for the UI to use"
},
"watchEvents": {
"type": "array",
"desc": "Specify which file events to respond to"
},
"no-notify": {
"desc": "Disable the notify element in browsers"
},
Expand Down
14 changes: 12 additions & 2 deletions lib/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ module.exports = {
*/
files: false,

/**
* Specify which file events to respond to.
* Available events: `add`, `change`, `unlink`, `addDir`, `unlinkDir`
* @property watchEvents
* @type Array
* @default ["change"]
* @since 2.18.8
*/
watchEvents: ["change"],

/**
* File watching options that get passed along to [Chokidar](https://github.com/paulmillr/chokidar).
* Check their docs for available options
Expand All @@ -40,11 +50,11 @@ module.exports = {
* @since 2.6.0
*/
watchOptions: {
ignoreInitial: true
/*
persistent: true,
ignored: '*.txt',
ignoreInitial: false,
followSymlinks: true,
cwd: '.',
Expand Down Expand Up @@ -130,7 +140,7 @@ module.exports = {
* @type string
* @default undefined
* @since 2.18.0
*/
*/

/**
* Clicks, Scrolls & Form inputs on any device will be mirrored to all others.
Expand Down
3 changes: 2 additions & 1 deletion lib/file-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var fileUtils = {
* `add` `unlink` etc etc so we need to check for that and only
* respond to 'change', for now.
*/
if (data.event === "change") {
if (bs.options.get("watchEvents").indexOf(data.event) > -1) {
if (!bs.paused && data.namespace === "core") {
bs.events.emit("file:reload", fileUtils.getFileInfo(data, bs.options));
}
Expand All @@ -41,6 +41,7 @@ var fileUtils = {
ext: data.ext,
path: data.path,
basename: data.basename,
event: data.event,
type: "inject"
};

Expand Down
2 changes: 1 addition & 1 deletion lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports.callbacks = {
return logger.info("{cyan:Reloading files that match: {magenta:%s", data.path);
}

logger.info("{cyan:File changed: {magenta:%s", data.path);
logger.info("{cyan:File event [" + data.event + "] : {magenta:%s", data.path);
}
},
/**
Expand Down
5 changes: 3 additions & 2 deletions test/specs/api/init.reload.stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,15 @@ describe("API: .stream()", function () {
log: false,
namespace: "core",
event: "change",
ext: "css"
ext: "css",
});
sinon.assert.calledWithExactly(emitterStub, "file:reload", {
ext: "css",
path: "/users/shakyshane/.tmp/css/core.css",
basename: "core.css",
type: "inject",
log: false
log: false,
event: "change"
});
sinon.assert.calledWithExactly(emitterStub, "stream:changed", {
changed: ["core.css"]
Expand Down
41 changes: 40 additions & 1 deletion test/specs/files/files.watching.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe("File Watcher Module", function () {
],
watchOptions: {
interval: 200

}
}, function (err, bs) {
assert.equal(bs.watchers.core.watchers.length, 2);
Expand Down Expand Up @@ -96,6 +96,45 @@ describe("File Watcher Module", function () {
bs.watchers.core.watchers[0]._events.all("change", tempFile);
});
});
it("should emit events about added files when watchEvents added", function (done) {

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

browserSync.reset();

// assert: it works if it calls done
var bs = browserSync.create();

bs.init({
watchEvents: ["add"],
files: [
{
options: {
ignoreInitial: true
},
match: tempFile,
fn: function (event, file) {
assert.equal(event, "add");
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("add", tempFile);
});
});
it("should allow obj literal with match & options, but without callback fn", function (done) {

browserSync.reset();
Expand Down

0 comments on commit de2e2fa

Please sign in to comment.